def generateRecursive(self, component, all_components, builddir=None, modbuilddir=None, processed_components=None, application=None):

assert(self.configured)<EOL>if builddir is None:<EOL><INDENT>builddir = self.buildroot<EOL><DEDENT>if modbuilddir is None:<EOL><INDENT>modbuilddir = os.path.join(builddir, '<STR_LIT>')<EOL><DEDENT>if processed_components is None:<EOL><INDENT>processed_components = dict()<EOL><DEDENT>if not self.target:<EOL><INDENT>yield '<STR_LIT>' % self.target<EOL><DEDENT>toplevel = not len(processed_components)<EOL>logger.debug('<STR_LIT>' % (component, self.target))<EOL>recursive_deps = component.getDependenciesRecursive(<EOL>available_components = all_components,<EOL>target = self.target,<EOL>available_only = True,<EOL>test = True<EOL>)<EOL>dependencies = component.getDependencies(<EOL>all_components,<EOL>target = self.target,<EOL>available_only = True,<EOL>test = True<EOL>)<EOL>for name, dep in dependencies.items():<EOL><INDENT>if not dep:<EOL><INDENT>if dep.isTestDependency():<EOL><INDENT>logger.debug('<STR_LIT>' % (name, component))<EOL><DEDENT>else:<EOL><INDENT>yield '<STR_LIT>' % (name, component)<EOL><DEDENT><DEDENT><DEDENT>processed_components[component.getName()] = component<EOL>new_dependencies = OrderedDict([(name,c) for name,c in dependencies.items() if c and not name in processed_components])<EOL>self.generate(builddir, modbuilddir, component, new_dependencies, dependencies, recursive_deps, application, toplevel)<EOL>logger.debug('<STR_LIT>' % component)<EOL>for d in recursive_deps.values():<EOL><INDENT>logger.debug('<STR_LIT>' % d)<EOL><DEDENT>processed_components.update(new_dependencies)<EOL>for name, c in new_dependencies.items():<EOL><INDENT>for error in self.generateRecursive(<EOL>c, all_components, os.path.join(modbuilddir, name), modbuilddir, processed_components, application=application<EOL>):<EOL><INDENT>yield error<EOL><DEDENT><DEDENT>

generate top-level CMakeLists for this component and its
            dependencies: the CMakeLists are all generated in self.buildroot,
            which MUST be out-of-source

            !!! NOTE: experimenting with a slightly different way of doing
            things here, this function is a generator that yields any errors
            produced, so the correct use is:

            for error in gen.generateRecursive(...):
                print(error)


lib_subdirs = component.getLibs(explicit_only=True)<EOL>bin_subdirs = component.getBinaries()<EOL>ok = True<EOL>for d in lib_subdirs:<EOL><INDENT>if not os.path.exists(os.path.join(component.path, d)):<EOL><INDENT>logger.warning(<EOL>"<STR_LIT>", d, component<EOL>)<EOL>ok = False<EOL><DEDENT><DEDENT>for d in bin_subdirs:<EOL><INDENT>if not os.path.exists(os.path.join(component.path, d)):<EOL><INDENT>logger.warning(<EOL>"<STR_LIT>", d, component<EOL>)<EOL>ok = False<EOL><DEDENT><DEDENT>return ok<EOL>

Return true if all the subdirectories which this component lists in
            its module.json file exist (although their validity is otherwise
            not checked).

            If they don't, warning messages are printed.


def _listSubDirectories(self, component, toplevel):

manual_subdirs = []<EOL>auto_subdirs = []<EOL>header_subdirs = []<EOL>lib_subdirs = component.getLibs()<EOL>bin_subdirs = component.getBinaries()<EOL>test_subdirs = []<EOL>resource_subdirs = []<EOL>top_sources = []<EOL>start_on_top = "<STR_LIT:.>" in [os.path.normpath(x) for x in list(lib_subdirs.keys()) + list(bin_subdirs.keys())]<EOL>for f in sorted(os.listdir(component.path)):<EOL><INDENT>if f in Ignore_Subdirs or f.startswith('<STR_LIT:.>') or f.startswith('<STR_LIT:_>'):<EOL><INDENT>continue<EOL><DEDENT>check_cmakefile_path = os.path.join(f, '<STR_LIT>')<EOL>if os.path.isfile(os.path.join(component.path, check_cmakefile_path)) and notcomponent.ignores(check_cmakefile_path):<EOL><INDENT>self.checkStandardSourceDir(f, component)<EOL>manual_subdirs.append(f)<EOL>if f in ('<STR_LIT:test>',):<EOL><INDENT>test_subdirs.append(f)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if os.path.isfile(os.path.join(component.path, f)):<EOL><INDENT>if not component.ignores(f) and start_on_top:<EOL><INDENT>sf = self.createSourceFile(f, os.path.join(component.path, f), "<STR_LIT:.>")<EOL>if sf is not None:<EOL><INDENT>top_sources.append(sf)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>sources = self.containsSourceFiles(os.path.join(component.path, f), component)<EOL>if sources:<EOL><INDENT>if f in ('<STR_LIT:test>',):<EOL><INDENT>auto_subdirs.append((f, sources))<EOL>test_subdirs.append(f)<EOL><DEDENT>elif start_on_top:<EOL><INDENT>from yotta.lib import validate<EOL>if not validate.isPotentialTestDir(f):<EOL><INDENT>top_sources.extend(sources)<EOL>if f == component.getName():<EOL><INDENT>header_subdirs.append((f, sources))<EOL><DEDENT><DEDENT><DEDENT>elif os.path.normpath(f) in [fsutils.fullySplitPath(x)[<NUM_LIT:0>] for x in lib_subdirs] oros.path.normpath(f) in [fsutils.fullySplitPath(x)[<NUM_LIT:0>] for x in bin_subdirs]:<EOL><INDENT>for full_subpath in list(lib_subdirs.keys()) + list(bin_subdirs.keys()):<EOL><INDENT>if fsutils.fullySplitPath(full_subpath)[<NUM_LIT:0>] == os.path.normpath(f):<EOL><INDENT>sources = self.containsSourceFiles(os.path.join(component.path, full_subpath), component)<EOL>auto_subdirs.append((full_subpath, sources))<EOL><DEDENT><DEDENT><DEDENT>elif f == component.getName():<EOL><INDENT>header_subdirs.append((f, sources))<EOL><DEDENT><DEDENT>elif toplevel and((f in ('<STR_LIT:test>',)) or(os.path.normpath(f) in lib_subdirs or start_on_top) or(os.path.normpath(f) in bin_subdirs or start_on_top) and notcomponent.ignores(f)):<EOL><INDENT>logger.warning("<STR_LIT>", f, component)<EOL><DEDENT><DEDENT><DEDENT>if f in ('<STR_LIT>',):<EOL><INDENT>resource_subdirs.append(os.path.join(component.path, f))<EOL><DEDENT>check_directory_name_cases = list(lib_subdirs.keys()) + list(bin_subdirs.keys()) + ['<STR_LIT:test>', '<STR_LIT>']<EOL>if f.lower() in check_directory_name_cases + ['<STR_LIT:src>'] and notf in check_directory_name_cases and notcomponent.ignores(f):<EOL><INDENT>self.checkStandardSourceDir(f, component)<EOL><DEDENT><DEDENT>if top_sources:<EOL><INDENT>auto_subdirs.append((component.getName(), top_sources))<EOL><DEDENT>return {<EOL>"<STR_LIT>": manual_subdirs,<EOL>"<STR_LIT>": auto_subdirs,<EOL>"<STR_LIT>": header_subdirs,<EOL>"<STR_LIT>": {component.getName(): component.getName()} if (start_on_top and component.isApplication()) else bin_subdirs,<EOL>"<STR_LIT>": {component.getName(): component.getName()} if (start_on_top and not component.isApplication()) else lib_subdirs,<EOL>"<STR_LIT:test>": test_subdirs,<EOL>"<STR_LIT>": resource_subdirs<EOL>}<EOL>

return: {
                manual: [list of subdirectories with manual CMakeLists],
                  auto: [list of pairs: (subdirectories name to autogenerate, a list of source files in that dir)],
                   bin: {dictionary of subdirectory name to binary name},
                   lib: {dictionary of subdirectory name to binary name},
                  test: [list of directories that build tests],
              resource: [list of directories that contain resources]
            }


def _getConfigData(self, all_dependencies, component, builddir, build_info_header_path):

<EOL>from yotta.lib import ordered_json<EOL>add_defs_header = '<STR_LIT>'<EOL>set_definitions = '<STR_LIT>'<EOL>definitions = []<EOL>definitions.append(('<STR_LIT>', sanitizePreprocessorSymbol(self.target.getName())))<EOL>definitions.append(('<STR_LIT>' % sanitizePreprocessorSymbol(self.target.getName()),None))<EOL>full_build_info_header_path = replaceBackslashes(os.path.abspath(build_info_header_path))<EOL>logger.debug('<STR_LIT>', full_build_info_header_path)<EOL>definitions.append(('<STR_LIT>', '<STR_LIT:">'+full_build_info_header_path+'<STR_LIT:">'))<EOL>for target in self.target.getSimilarTo_Deprecated():<EOL><INDENT>if '<STR_LIT:*>' not in target:<EOL><INDENT>definitions.append(('<STR_LIT>' % sanitizePreprocessorSymbol(target),None))<EOL><DEDENT><DEDENT>merged_config = self.target.getMergedConfig()<EOL>logger.debug('<STR_LIT>', merged_config)<EOL>definitions += self._definitionsForConfig(merged_config, ['<STR_LIT>', '<STR_LIT>'])<EOL>add_defs_header += '<STR_LIT>'<EOL>for k, v in definitions:<EOL><INDENT>if v is not None:<EOL><INDENT>add_defs_header += '<STR_LIT>' % (k, v)<EOL>set_definitions += '<STR_LIT>' % (k, v)<EOL><DEDENT>else:<EOL><INDENT>add_defs_header += '<STR_LIT>' % k<EOL>set_definitions += '<STR_LIT>' % k<EOL><DEDENT><DEDENT>add_defs_header += '<STR_LIT>'<EOL>for dep in list(all_dependencies.values()) + [component]:<EOL><INDENT>add_defs_header += "<STR_LIT>" % (sanitizePreprocessorSymbol(dep.getName()), str(dep.getVersion()))<EOL>add_defs_header += "<STR_LIT>" % (sanitizePreprocessorSymbol(dep.getName()), dep.getVersion().major())<EOL>add_defs_header += "<STR_LIT>" % (sanitizePreprocessorSymbol(dep.getName()), dep.getVersion().minor())<EOL>add_defs_header += "<STR_LIT>" % (sanitizePreprocessorSymbol(dep.getName()), dep.getVersion().patch())<EOL><DEDENT>defines = component.getDefines()<EOL>if defines:<EOL><INDENT>add_defs_header += "<STR_LIT>"<EOL>for name, value in defines.items():<EOL><INDENT>add_defs_header += "<STR_LIT>" % (name, value)<EOL><DEDENT>add_defs_header += '<STR_LIT:\n>'<EOL><DEDENT>config_include_file = os.path.join(builddir, '<STR_LIT>')<EOL>config_json_file    = os.path.join(builddir, '<STR_LIT>')<EOL>set_definitions += '<STR_LIT>' % replaceBackslashes(os.path.abspath(config_json_file))<EOL>self._writeFile(<EOL>config_include_file,<EOL>'<STR_LIT>'+<EOL>'<STR_LIT>'+<EOL>add_defs_header+<EOL>'<STR_LIT>'<EOL>)<EOL>self._writeFile(<EOL>config_json_file,<EOL>ordered_json.dumps(merged_config)<EOL>)<EOL>return (config_include_file, set_definitions, config_json_file)<EOL>


def getBuildInfo(self, sourcedir, builddir):

cmake_defs = '<STR_LIT>'<EOL>preproc_defs = '<STR_LIT>'<EOL>import datetime<EOL>from yotta.lib import vcs<EOL>now = datetime.datetime.utcnow()<EOL>vcs_instance = vcs.getVCS(sourcedir)<EOL>if self.build_uuid is None:<EOL><INDENT>import uuid<EOL>self.build_uuid = uuid.uuid4()<EOL><DEDENT>definitions = [<EOL>('<STR_LIT>',   now.year,        '<STR_LIT>'),<EOL>('<STR_LIT>',  now.month,       '<STR_LIT>'),<EOL>('<STR_LIT>',    now.day,         '<STR_LIT>'),<EOL>('<STR_LIT>',   now.hour,        '<STR_LIT>'),<EOL>('<STR_LIT>', now.minute,      '<STR_LIT>'),<EOL>('<STR_LIT>', now.second,      '<STR_LIT>'),<EOL>('<STR_LIT>',   self.build_uuid, '<STR_LIT>'),<EOL>]<EOL>if vcs_instance is not None:<EOL><INDENT>commit_id = None<EOL>repotype = vcs_instance.__class__.__name__<EOL>try:<EOL><INDENT>commit_id = vcs_instance.getCommitId()<EOL><DEDENT>except vcs.VCSNotInstalled as e:<EOL><INDENT>logger.warning('<STR_LIT>', repotype)<EOL>commit_id = None<EOL><DEDENT>except vcs.VCSError as e:<EOL><INDENT>logger.debug('<STR_LIT:%s>', e)<EOL>logger.warning(<EOL>'<STR_LIT>',<EOL>str(e).split('<STR_LIT:\n>')[<NUM_LIT:0>],<EOL>repotype<EOL>)<EOL><DEDENT>if commit_id is not None:<EOL><INDENT>clean_state = int(vcs_instance.isClean())<EOL>description = vcs_instance.getDescription()<EOL>definitions += [<EOL>('<STR_LIT>',    commit_id,   '<STR_LIT>'),<EOL>('<STR_LIT>', clean_state, '<STR_LIT>'),<EOL>('<STR_LIT>', description, '<STR_LIT>')<EOL>]<EOL><DEDENT><DEDENT>for d in definitions:<EOL><INDENT>preproc_defs += '<STR_LIT>' % d<EOL>cmake_defs   += '<STR_LIT>' % d<EOL><DEDENT>buildinfo_include_file = os.path.join(builddir, '<STR_LIT>')<EOL>self._writeFile(<EOL>buildinfo_include_file,<EOL>'<STR_LIT>'+<EOL>'<STR_LIT>'+<EOL>preproc_defs+<EOL>'<STR_LIT>'<EOL>)<EOL>return (buildinfo_include_file, cmake_defs)<EOL>

Write the build info header file, and return (path_to_written_header, set_cmake_definitions)


def generate(<EOL>self, builddir, modbuilddir, component, active_dependencies, immediate_dependencies, all_dependencies, application, toplevel<EOL>):

include_root_dirs = '<STR_LIT>'<EOL>if application is not None and component is not application:<EOL><INDENT>include_root_dirs += '<STR_LIT>' % replaceBackslashes(application.path)<EOL><DEDENT>include_sys_dirs = '<STR_LIT>'<EOL>include_other_dirs = '<STR_LIT>'<EOL>for name, c in itertools.chain(((component.getName(), component),), all_dependencies.items()):<EOL><INDENT>if c is not component and c.isTestDependency():<EOL><INDENT>continue<EOL><DEDENT>include_root_dirs += '<STR_LIT>' % replaceBackslashes(c.path)<EOL>dep_sys_include_dirs = c.getExtraSysIncludes()<EOL>for d in dep_sys_include_dirs:<EOL><INDENT>include_sys_dirs += '<STR_LIT>' % replaceBackslashes(os.path.join(c.path, d))<EOL><DEDENT>dep_extra_include_dirs = c.getExtraIncludes()<EOL>for d in dep_extra_include_dirs:<EOL><INDENT>include_other_dirs += '<STR_LIT>' % replaceBackslashes(os.path.join(c.path, d))<EOL><DEDENT><DEDENT>add_depend_subdirs = '<STR_LIT>'<EOL>for name, c in active_dependencies.items():<EOL><INDENT>depend_subdir = replaceBackslashes(os.path.join(modbuilddir, name))<EOL>relpath = replaceBackslashes(os.path.relpath(depend_subdir, self.buildroot))<EOL>add_depend_subdirs +='<STR_LIT>''<STR_LIT>''<STR_LIT>''<STR_LIT>'% (depend_subdir, relpath)<EOL><DEDENT>delegate_to_existing = None<EOL>delegate_build_dir = None<EOL>module_is_empty = False<EOL>if os.path.isfile(os.path.join(component.path, '<STR_LIT>')) and not component.ignores('<STR_LIT>'):<EOL><INDENT>delegate_to_existing = component.path<EOL>add_own_subdirs = []<EOL>logger.debug("<STR_LIT>", builddir)<EOL>delegate_build_dir = os.path.join(builddir, '<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>self._validateListedSubdirsExist(component)<EOL>subdirs = self._listSubDirectories(component, toplevel)<EOL>manual_subdirs      = subdirs['<STR_LIT>']<EOL>autogen_subdirs     = subdirs['<STR_LIT>']<EOL>binary_subdirs      = subdirs['<STR_LIT>']<EOL>lib_subdirs         = subdirs['<STR_LIT>']<EOL>test_subdirs        = subdirs['<STR_LIT:test>']<EOL>resource_subdirs    = subdirs['<STR_LIT>']<EOL>header_subdirs      = subdirs['<STR_LIT>']<EOL>logger.debug("<STR_LIT>", component, lib_subdirs, binary_subdirs)<EOL>add_own_subdirs = []<EOL>for f in manual_subdirs:<EOL><INDENT>if os.path.isfile(os.path.join(component.path, f, '<STR_LIT>')):<EOL><INDENT>if f in test_subdirs and component.isTestDependency():<EOL><INDENT>continue<EOL><DEDENT>add_own_subdirs.append(<EOL>(os.path.join(component.path, f), f)<EOL>)<EOL><DEDENT><DEDENT>all_subdirs = manual_subdirs + [x[<NUM_LIT:0>] for x in autogen_subdirs]<EOL>if component.isTestDependency():<EOL><INDENT>if len(autogen_subdirs) + len(add_own_subdirs) == <NUM_LIT:0>:<EOL><INDENT>module_is_empty = True<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if len(autogen_subdirs) + len(add_own_subdirs) <= len(test_subdirs):<EOL><INDENT>module_is_empty = True<EOL><DEDENT><DEDENT>for f, source_files in autogen_subdirs:<EOL><INDENT>if f in test_subdirs:<EOL><INDENT>if component.isTestDependency():<EOL><INDENT>continue<EOL><DEDENT>self.generateTestDirList(<EOL>builddir, f, source_files, component, immediate_dependencies, toplevel=toplevel, module_is_empty=module_is_empty<EOL>)<EOL><DEDENT>else:<EOL><INDENT>if f in binary_subdirs:<EOL><INDENT>is_executable = True<EOL>object_name = binary_subdirs[f]<EOL><DEDENT>else:<EOL><INDENT>assert(f in lib_subdirs)<EOL>object_name = lib_subdirs[f]<EOL><DEDENT>for header_dir, header_files in header_subdirs:<EOL><INDENT>source_files.extend(header_files)<EOL><DEDENT>self.generateSubDirList(<EOL>builddir = builddir,<EOL>dirname = f,<EOL>source_files = source_files,<EOL>component = component,<EOL>all_subdirs = all_subdirs,<EOL>immediate_dependencies = immediate_dependencies,<EOL>object_name = object_name,<EOL>resource_subdirs = resource_subdirs,<EOL>is_executable = (f in binary_subdirs)<EOL>)<EOL><DEDENT>add_own_subdirs.append(<EOL>(os.path.join(builddir, f), f)<EOL>)<EOL><DEDENT>if component.isTestDependency():<EOL><INDENT>test_subdirs = []<EOL><DEDENT>if module_is_empty:<EOL><INDENT>if len(binary_subdirs):<EOL><INDENT>logger.warning('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>add_own_subdirs.append(self.createDummyLib(<EOL>component, builddir, [x[<NUM_LIT:0>] for x in immediate_dependencies.items() if not x[<NUM_LIT:1>].isTestDependency()]<EOL>))<EOL><DEDENT><DEDENT><DEDENT>toolchain_file_path = os.path.join(builddir, '<STR_LIT>')<EOL>if toplevel:<EOL><INDENT>template = jinja_environment.get_template('<STR_LIT>')<EOL>file_contents = template.render({  <EOL>"<STR_LIT>": self.target.getToolchainFiles()<EOL>})<EOL>self._writeFile(toolchain_file_path, file_contents)<EOL><DEDENT>template = jinja_environment.get_template('<STR_LIT>')<EOL>relpath = os.path.relpath(builddir, self.buildroot)<EOL>file_contents = template.render({ <EOL>"<STR_LIT>": toplevel,<EOL>"<STR_LIT>": self.target.getName(),<EOL>"<STR_LIT>": self.set_toplevel_definitions,<EOL>"<STR_LIT>": toolchain_file_path,<EOL>"<STR_LIT>": component,<EOL>"<STR_LIT>": relpath,<EOL>"<STR_LIT>": include_root_dirs,<EOL>"<STR_LIT>": include_sys_dirs,<EOL>"<STR_LIT>": include_other_dirs,<EOL>"<STR_LIT>": add_depend_subdirs,<EOL>"<STR_LIT>": add_own_subdirs,<EOL>"<STR_LIT>": self.config_include_file,<EOL>"<STR_LIT>": delegate_to_existing,<EOL>"<STR_LIT>": delegate_build_dir,<EOL>"<STR_LIT>": active_dependencies,<EOL>"<STR_LIT>": module_is_empty,<EOL>"<STR_LIT>": self.target.getAdditionalIncludes()<EOL>})<EOL>self._writeFile(os.path.join(builddir, '<STR_LIT>'), file_contents)<EOL>

active_dependencies is the dictionary of components that need to be
            built for this component, but will not already have been built for
            another component.


<EOL>s = b'<STR_LIT>'<EOL>n = int(n)<EOL>pack = struct.pack<EOL>while n > <NUM_LIT:0>:<EOL><INDENT>s = pack('<STR_LIT>', n & <NUM_LIT>) + s<EOL>n = n >> <NUM_LIT:32><EOL><DEDENT>for i in range(len(s)):<EOL><INDENT>if s[i] != '<STR_LIT>':<EOL><INDENT>break<EOL><DEDENT><DEDENT>else:<EOL><INDENT>s = '<STR_LIT>'<EOL>i = <NUM_LIT:0><EOL><DEDENT>s = s[i:]<EOL>if blocksize > <NUM_LIT:0> and len(s) % blocksize:<EOL><INDENT>s = (blocksize - len(s) % blocksize) * '<STR_LIT>' + s<EOL><DEDENT>return s<EOL>

long_to_bytes(n:long, blocksize:int) : string
    Convert a long integer to a byte string.

    If optional blocksize is given and greater than zero, pad the front of the
    byte string with binary zeros so that the length is a multiple of
    blocksize.


cache_dir = folders.cacheDirectory()<EOL>def fullpath(f):<EOL><INDENT>return os.path.join(cache_dir, f)<EOL><DEDENT>def getMTimeSafe(f):<EOL><INDENT>try:<EOL><INDENT>return os.stat(f).st_mtime<EOL><DEDENT>except FileNotFoundError:<EOL><INDENT>import time<EOL>return time.clock()<EOL><DEDENT><DEDENT>fsutils.mkDirP(cache_dir)<EOL>max_cached_modules = getMaxCachedModules()<EOL>for f in sorted(<EOL>[f for f in os.listdir(cache_dir) if<EOL>os.path.isfile(fullpath(f)) and not f.endswith('<STR_LIT>') and not f.endswith('<STR_LIT>')<EOL>],<EOL>key = lambda f: getMTimeSafe(fullpath(f)),<EOL>reverse = True<EOL>)[max_cached_modules:]:<EOL><INDENT>cache_logger.debug('<STR_LIT>', f)<EOL>removeFromCache(f)<EOL><DEDENT>cache_logger.debug('<STR_LIT>', max_cached_modules)<EOL>


def decorator(fn):<EOL><INDENT>@functools.wraps(fn)<EOL>def wrapped(*args, **kwargs):<EOL><INDENT>r = fn(*args, **kwargs)<EOL>if random.random() < p:<EOL><INDENT>pruneCache()<EOL><DEDENT>return r<EOL><DEDENT>return wrapped<EOL><DEDENT>return decorator<EOL>

return decorator to prune cache after calling fn with a probability of p


if cache_key is None:<EOL><INDENT>raise NotInCache('<STR_LIT>')<EOL><DEDENT>cache_key = _encodeCacheKey(cache_key)<EOL>cache_dir = folders.cacheDirectory()<EOL>fsutils.mkDirP(cache_dir)<EOL>path = os.path.join(cache_dir, cache_key)<EOL>logger.debug('<STR_LIT>', path, to_directory)<EOL>try:<EOL><INDENT>unpackFrom(path, to_directory)<EOL>try:<EOL><INDENT>shutil.copy(path + '<STR_LIT>', os.path.join(to_directory, '<STR_LIT>'))<EOL><DEDENT>except IOError as e:<EOL><INDENT>if e.errno == errno.ENOENT:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>cache_logger.debug('<STR_LIT>', cache_key, to_directory)<EOL>return<EOL><DEDENT>except IOError as e:<EOL><INDENT>if e.errno == errno.ENOENT:<EOL><INDENT>cache_logger.debug('<STR_LIT>', cache_key)<EOL>raise NotInCache('<STR_LIT>')<EOL><DEDENT><DEDENT>except OSError as e:<EOL><INDENT>if e.errno == errno.ENOTEMPTY:<EOL><INDENT>logger.error('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>

If the specified cache key exists, unpack the tarball into the
        specified directory, otherwise raise NotInCache (a KeyError subclass).


def _downloadToCache(stream, hashinfo={}, origin_info=dict()):

hash_name  = None<EOL>hash_value = None<EOL>m = None<EOL>if len(hashinfo):<EOL><INDENT>for h in ('<STR_LIT>',):<EOL><INDENT>if h in hashinfo:<EOL><INDENT>hash_name  = h<EOL>hash_value = hashinfo[h]<EOL>m = getattr(hashlib, h)()<EOL>break<EOL><DEDENT><DEDENT>if not hash_name:<EOL><INDENT>logger.warning('<STR_LIT>', hashinfo)<EOL><DEDENT><DEDENT>cache_dir = folders.cacheDirectory()<EOL>fsutils.mkDirP(cache_dir)<EOL>file_size = <NUM_LIT:0><EOL>(download_file, download_fname) = tempfile.mkstemp(dir=cache_dir, suffix='<STR_LIT>')<EOL>with os.fdopen(download_file, '<STR_LIT:wb>') as f:<EOL><INDENT>f.seek(<NUM_LIT:0>)<EOL>for chunk in stream.iter_content(<NUM_LIT>):<EOL><INDENT>f.write(chunk)<EOL>if hash_name:<EOL><INDENT>m.update(chunk)<EOL><DEDENT><DEDENT>if hash_name:<EOL><INDENT>calculated_hash = m.hexdigest()<EOL>logger.debug(<EOL>'<STR_LIT>' % (<EOL>hash_name, calculated_hash, hash_value<EOL>)<EOL>)<EOL>if hash_value and (hash_value != calculated_hash):<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT><DEDENT>file_size = f.tell()<EOL>logger.debug('<STR_LIT>', file_size, download_fname)<EOL>f.truncate()<EOL><DEDENT>extended_origin_info = {<EOL>'<STR_LIT>': hashinfo,<EOL>'<STR_LIT:size>': file_size<EOL>}<EOL>extended_origin_info.update(origin_info)<EOL>ordered_json.dump(download_fname + '<STR_LIT>', extended_origin_info)<EOL>return os.path.basename(download_fname)<EOL>

Download the specified stream to a temporary cache directory, and
        returns a cache key that can be used to access/remove the file.
        You should use either removeFromCache(cache_key) or _moveCachedFile to
        move the downloaded file to a known key after downloading.


cache_dir = folders.cacheDirectory()<EOL>from_path = os.path.join(cache_dir, from_key)<EOL>to_path   = os.path.join(cache_dir, to_key)<EOL>try:<EOL><INDENT>os.rename(from_path, to_path)<EOL>os.rename(from_path+'<STR_LIT>', to_path+'<STR_LIT>')<EOL><DEDENT>except Exception as e:<EOL><INDENT>if (isinstance(e, OSError) and e.errno == errno.ENOENT) or(isinstance(e, getattr(__builtins__, "<STR_LIT>", type(None))) and e.errno == <NUM_LIT>):<EOL><INDENT>fsutils.rmF(from_path)<EOL><DEDENT>else:<EOL><INDENT>raise<EOL><DEDENT><DEDENT>

Move a file atomically within the cache: used to make cached files
        available at known keys, so they can be used by other processes.


@sometimesPruneCache(<NUM_LIT>)<EOL>def unpackTarballStream(stream, into_directory, hash={}, cache_key=None, origin_info=dict()):

cache_key = _encodeCacheKey(cache_key)<EOL>if getMaxCachedModules() == <NUM_LIT:0>:<EOL><INDENT>cache_key = None<EOL><DEDENT>new_cache_key = _downloadToCache(stream, hash, origin_info)<EOL>unpackFromCache(new_cache_key, into_directory)<EOL>if cache_key is None:<EOL><INDENT>removeFromCache(new_cache_key)<EOL><DEDENT>else:<EOL><INDENT>_moveCachedFile(new_cache_key, cache_key)<EOL><DEDENT>

Unpack a responses stream that contains a tarball into a directory. If
        a hash is provided, then it will be used as a cache key (for future
        requests you can try to retrieve the key value from the cache first,
        before making the request)


r = []<EOL>for t in self.vcs.tags():<EOL><INDENT>logger.debug("<STR_LIT>", t)<EOL>if not len(t.strip()):<EOL><INDENT>continue<EOL><DEDENT>try:<EOL><INDENT>r.append(GitCloneVersion(t, t, self))<EOL><DEDENT>except ValueError:<EOL><INDENT>logger.debug('<STR_LIT>', t)<EOL><DEDENT><DEDENT>return r<EOL>

return a list of GitCloneVersion objects for tags which are valid
            semantic version idenfitifiers.


return [GitCloneVersion('<STR_LIT>', t, self) for t in self.vcs.tags()]<EOL>

return a list of GitCloneVersion objects for all tags


return [GitCloneVersion('<STR_LIT>', b, self) for b in self.vcs.branches()]<EOL>

return a list of GitCloneVersion objects for the tip of each branch


import re<EOL>commit_match = re.match('<STR_LIT>', spec, re.I)<EOL>if commit_match:<EOL><INDENT>return GitCloneVersion('<STR_LIT>', spec, self)<EOL><DEDENT>return None<EOL>

return a GithubComponentVersion object for a specific commit if valid


@classmethod<EOL><INDENT>def createFromSource(cls, vs, name=None):<DEDENT>

return GitComponent(vs.location, vs.spec, vs.semantic_spec)<EOL>

returns a git component for any git:// url, or None if this is not
            a git component.

            Normally version will be empty, unless the original url was of the
            form 'git://...#version', which can be used to grab a particular
            tag or branch, or ...#>=1.2.3, which can be used to specify
            semantic version specifications on tags.


<EOL>from yotta.lib import sourceparse<EOL>errors = []<EOL>available_versions = {}<EOL>for mod in dependency_list.get('<STR_LIT>', []):<EOL><INDENT>available_versions[mod['<STR_LIT:name>']] = mod['<STR_LIT:version>']<EOL><DEDENT>for mod in dependency_list.get('<STR_LIT>', []):<EOL><INDENT>for spec_info in mod.get('<STR_LIT>', []):<EOL><INDENT>name = spec_info['<STR_LIT:name>']<EOL>spec = spec_info['<STR_LIT:version>']<EOL>if spec_info.get('<STR_LIT>', False):<EOL><INDENT>continue<EOL><DEDENT>if not name in available_versions:<EOL><INDENT>errors.append('<STR_LIT>' % (<EOL>name, mod['<STR_LIT:name>']<EOL>))<EOL><DEDENT>else:<EOL><INDENT>available_version = available_versions[name]<EOL>parsed_spec = sourceparse.parseSourceURL(spec)<EOL>if not parsed_spec.semanticSpecMatches(available_version):<EOL><INDENT>errors.append('<STR_LIT>' % (<EOL>name, available_version, parsed_spec.semanticSpec(), mod['<STR_LIT:name>']<EOL>))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return errors<EOL>

return a list of errors encountered (e.g. dependency missing or
        specification not met


if isinstance(value, six.binary_type):<EOL><INDENT>return value.decode('<STR_LIT:utf-8>')<EOL><DEDENT>return value<EOL>

If input string is binary, then decode from utf-8.


assert len(lst) > <NUM_LIT:0>, '<STR_LIT>'<EOL>return lst[<NUM_LIT:0>], lst[<NUM_LIT:1>:]<EOL>

Returns first element and an iterator over rest element.

    TODO: will be useful to make it work not only with lists
          but also with any iterable.


padding = len(out)<EOL>add_padding = padding_adder(padding)<EOL>text = add_padding(text, ignore_first_line=True)<EOL>return out + text<EOL>

This method is used to append content of the `text`
    argument to the `out` argument.

    Depending on how many lines in the text, a
    padding can be added to all lines except the first
    one.

    Concatenation result is appended to the `out` argument.


def serialize_list(out, lst, delimiter=u'<STR_LIT>', max_length=<NUM_LIT:20>):

have_multiline_items = any(map(is_multiline, lst))<EOL>result_will_be_too_long = sum(map(len, lst)) > max_length<EOL>if have_multiline_items or result_will_be_too_long:<EOL><INDENT>padding = len(out)<EOL>add_padding = padding_adder(padding)<EOL>head, rest = cut_head(lst)<EOL>rest = map(add_padding, rest)<EOL>head = add_padding(head, ignore_first_line=True)<EOL>lst = chain((head,), rest)<EOL>delimiter += u'<STR_LIT:\n>'<EOL><DEDENT>else:<EOL><INDENT>delimiter += u'<STR_LIT:U+0020>'<EOL><DEDENT>return out + delimiter.join(lst)<EOL>

This method is used to serialize list of text
    pieces like ["some=u'Another'", "blah=124"]

    Depending on how many lines are in these items,
    they are concatenated in row or as a column.

    Concatenation result is appended to the `out` argument.


value_id = id(value)<EOL>if value_id in recursion_breaker.processed:<EOL><INDENT>return u'<STR_LIT>'<EOL><DEDENT>recursion_breaker.processed.add(value_id)<EOL>try:<EOL><INDENT>if isinstance(value, six.binary_type):<EOL><INDENT>return u"<STR_LIT>".format(value.decode('<STR_LIT:utf-8>'))<EOL><DEDENT>elif isinstance(value, six.text_type):<EOL><INDENT>return u"<STR_LIT>".format(value)<EOL><DEDENT>elif isinstance(value, (list, tuple)):<EOL><INDENT>values = list(map(format_value, value))<EOL>result = serialize_list(u'<STR_LIT:[>', values, delimiter=u'<STR_LIT:U+002C>') + u'<STR_LIT:]>'<EOL>return force_unicode(result)<EOL><DEDENT>elif isinstance(value, dict):<EOL><INDENT>items = six.iteritems(value)<EOL>items = (tuple(map(format_value, item))<EOL>for item in items)<EOL>items = list(items)<EOL>items.sort()<EOL>items = [<EOL>serialize_text(<EOL>u'<STR_LIT>'.format(key),<EOL>item_value)<EOL>for key, item_value in items]<EOL>result = serialize_list(u'<STR_LIT:{>', items, delimiter=u'<STR_LIT:U+002C>') + u'<STR_LIT:}>'<EOL>return force_unicode(result)<EOL><DEDENT>return force_unicode(repr(value))<EOL><DEDENT>finally:<EOL><INDENT>recursion_breaker.processed.remove(value_id)<EOL><DEDENT>

This function should return unicode representation of the value


def method(self):<EOL><INDENT>cls_name = self.__class__.__name__<EOL>if args:<EOL><INDENT>field_names = args<EOL><DEDENT>else:<EOL><INDENT>def undercored(name): return name.startswith('<STR_LIT:_>')<EOL>def is_method(name): return callable(getattr(self, name))<EOL>def good_name(name):<EOL><INDENT>return not undercored(name) and not is_method(name)<EOL><DEDENT>field_names = filter(good_name, dir(self))<EOL>field_names = sorted(field_names)<EOL><DEDENT>field_getters = zip(field_names,<EOL>map(attrgetter, field_names))<EOL>field_getters = chain(<EOL>field_getters,<EOL>kwargs.items())<EOL>fields = ((name, format_value(getter(self)))<EOL>for name, getter in field_getters)<EOL>fields = ((u'<STR_LIT>'.format(name), value)<EOL>for name, value in fields)<EOL>fields = list(starmap(serialize_text, fields))<EOL>beginning = u'<STR_LIT>'.format(<EOL>cls_name=cls_name,<EOL>)<EOL>result = serialize_list(<EOL>beginning,<EOL>fields)<EOL>result += u'<STR_LIT:>>'<EOL>if ON_PYTHON2:<EOL><INDENT>result = result.encode('<STR_LIT:utf-8>')<EOL><DEDENT>return result<EOL><DEDENT>return method<EOL>

Returns __repr__ method which returns ASCII
    representaion of the object with given fields.

    Without arguments, ``make_repr`` generates a method
    which outputs all object's non-protected (non-undercored)
    arguments which are not callables.

    Accepts ``*args``, which should be a names of object's
    attributes to be included in the output::

      __repr__ = make_repr('foo', 'bar')

    If you want to generate attribute's content on the fly,
    then you should use keyword arguments and pass a callable
    of one argument::

      __repr__ = make_repr(foo=lambda obj: obj.blah + 100500)


from numpy_sugar import epsilon<EOL>from numpy_sugar.linalg import sum2diag<EOL>from numpy_sugar.random import multivariate_normal<EOL>if random_state is None:<EOL><INDENT>random_state = RandomState()<EOL><DEDENT>m = self._mean.value()<EOL>K = self._cov.value().copy()<EOL>sum2diag(K, +epsilon.small, out=K)<EOL>return self._lik.sample(multivariate_normal(m, K, random_state), random_state)<EOL>

r"""Sample from the specified distribution.

        Parameters
        ----------
        random_state : random_state
            Set the initial random state.

        Returns
        -------
        numpy.ndarray
            Sample.


link = LogitLink()<EOL>mean, cov = _mean_cov(<EOL>offset, G, heritability, causal_variants, causal_variance, random_state<EOL>)<EOL>lik = BernoulliProdLik(link)<EOL>sampler = GGPSampler(lik, mean, cov)<EOL>return sampler.sample(random_state)<EOL>

r"""Bernoulli likelihood sampling.

    Sample according to

    .. math::

        \mathbf y \sim \prod_{i=1}^n
        \text{Bernoulli}(\mu_i = \text{logit}(z_i))
        \mathcal N(~ o \mathbf 1 + \mathbf a^\intercal \boldsymbol\alpha;
        ~ (h^2 - v_c)\mathrm G^\intercal\mathrm G +
        (1-h^2-v_c)\mathrm I ~)

    using the canonical Logit link function to define the conditional Bernoulli
    mean :math:`\mu_i`.

    The causal :math:`\mathbf a` covariates and the corresponding effect-sizes
    are randomly draw according to the following idea. The ``causal_variants``,
    if given, are first mean-zero and std-one normalized and then having
    its elements divided by the squared-root the the number of variances::

        causal_variants = _stdnorm(causal_variants, axis=0)
        causal_variants /= sqrt(causal_variants.shape[1])

    The causal effect-sizes :math:`\boldsymbol\alpha` are draw from
    :math:`\{-1, +1\}` and subsequently normalized for mean-zero and std-one""

    Parameters
    ----------
    random_state : random_state
        Set the initial random state.

    Example
    -------

    .. doctest::

        >>> from glimix_core.random import bernoulli_sample
        >>> from numpy.random import RandomState
        >>> offset = 5
        >>> G = [[1, -1], [2, 1]]
        >>> bernoulli_sample(offset, G, random_state=RandomState(0))
        array([1., 1.])


link = LogitLink()<EOL>mean, cov = _mean_cov(<EOL>offset, G, heritability, causal_variants, causal_variance, random_state<EOL>)<EOL>lik = BinomialProdLik(ntrials, link)<EOL>sampler = GGPSampler(lik, mean, cov)<EOL>return sampler.sample(random_state)<EOL>

Binomial likelihood sampling.

    Parameters
    ----------
    random_state : random_state
        Set the initial random state.

    Example
    -------

    .. doctest::

        >>> from glimix_core.random import binomial_sample
        >>> from numpy.random import RandomState
        >>> ntrials = [5, 15]
        >>> offset = 0.5
        >>> G = [[1, -1], [2, 1]]
        >>> binomial_sample(ntrials, offset, G, random_state=RandomState(0))
        array([ 2., 14.])


mean, cov = _mean_cov(<EOL>offset, G, heritability, causal_variants, causal_variance, random_state<EOL>)<EOL>link = LogLink()<EOL>lik = PoissonProdLik(link)<EOL>sampler = GGPSampler(lik, mean, cov)<EOL>return sampler.sample(random_state)<EOL>

Poisson likelihood sampling.

    Parameters
    ----------
    random_state : random_state
        Set the initial random state.

    Example
    -------

    .. doctest::

        >>> from glimix_core.random import poisson_sample
        >>> from numpy.random import RandomState
        >>> offset = -0.5
        >>> G = [[0.5, -1], [2, 1]]
        >>> poisson_sample(offset, G, random_state=RandomState(0))
        array([0, 6])


from numpy_sugar import epsilon<EOL>from numpy_sugar.linalg import sum2diag<EOL>from numpy_sugar.random import multivariate_normal<EOL>if random_state is None:<EOL><INDENT>random_state = RandomState()<EOL><DEDENT>m = self._mean.value()<EOL>K = self._cov.value().copy()<EOL>sum2diag(K, +epsilon.small, out=K)<EOL>return multivariate_normal(m, K, random_state)<EOL>


def fit(self, verbose=True, factr=<NUM_LIT>, pgtol=<NUM_LIT>):

self._maximize(verbose=verbose, factr=factr, pgtol=pgtol)<EOL>

r"""Maximise the marginal likelihood.

        Parameters
        ----------
        verbose : bool
            ``True`` for progress output; ``False`` otherwise.
            Defaults to ``True``.
        factr : float, optional
            The iteration stops when
            ``(f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= factr * eps``, where ``eps`` is
            the machine precision.
        pgtol : float, optional
            The iteration will stop when ``max{|proj g_i | i = 1, ..., n} <= pgtol``
            where ``pg_i`` is the i-th component of the projected gradient.

        Notes
        -----
        Please, refer to :func:`scipy.optimize.fmin_l_bfgs_b` for further information
        about ``factr`` and ``pgtol``.


return self.value()<EOL>

r"""Log of the marginal likelihood.

        Returns
        -------
        float
            :math:`\log p(\mathbf y)`


self._A = asarray(A, float)<EOL>self._X = asarray(X, float)<EOL>vecB = zeros((X.shape[<NUM_LIT:1>], A.shape[<NUM_LIT:0>])).ravel()<EOL>self._vecB = Vector(vecB)<EOL>self._nparams = vecB.size<EOL>Function.__init__(self, "<STR_LIT>", vecB=self._vecB)<EOL>

Constructor.

Parameters
----------
A : array_like
    p×p array.
X : array_like
    n×c array.


return self._nparams<EOL>


return self._A<EOL>


return self._X<EOL>


return kron(self.A, self.X)<EOL>


return self.AX @ self._vecB.value<EOL>

Kronecker mean function.

Returns
-------
𝐦 : ndarray
    (A⊗X)vec(B).


return {"<STR_LIT>": self.AX}<EOL>

Gradient of the linear mean function.

Returns
-------
vecB : ndarray
    Derivative of M over vec(B).


return unvec(self._vecB.value, (self.X.shape[<NUM_LIT:1>], self.A.shape[<NUM_LIT:0>]))<EOL>


self._means = [c for c in means]<EOL>Function.__init__(self, "<STR_LIT>", composite=self._means)<EOL>

Constructor.

Parameters
----------
means : list
    List of mean functions.


return add.reduce([mean.value() for mean in self._means])<EOL>

Sum of mean vectors, 𝐟₀ + 𝐟₁ + ….

Returns
-------
𝐦 : ndarray
    𝐟₀ + 𝐟₁ + ….


grad = {}<EOL>for i, f in enumerate(self._means):<EOL><INDENT>for varname, g in f.gradient().items():<EOL><INDENT>grad[f"<STR_LIT>"] = g<EOL><DEDENT><DEDENT>return grad<EOL>

Sum of mean function derivatives.

Returns
-------
∂𝐦 : dict
    ∂𝐟₀ + ∂𝐟₁ + ….


self._offset = Scalar(<NUM_LIT:0.0>)<EOL>self._offset.bounds = (-<NUM_LIT>, +<NUM_LIT:200>)<EOL>self._n = n<EOL>Function.__init__(self, "<STR_LIT>", offset=self._offset)<EOL>

Constructor.

Parameters
----------
n : int
    Size of the 𝟏 array.


return full(self._n, self._offset.value)<EOL>

Offset mean.

Returns
-------
𝐦 : (n,) ndarray
    θ⋅𝟏.


return dict(offset=ones(self._n))<EOL>

Gradient of the offset function.

Returns
-------
offset : (n,) ndarray
    Vector 𝟏.


return self._offset.value<EOL>


X = asarray(X, float)<EOL>m = X.shape[<NUM_LIT:1>]<EOL>self._effsizes = Vector(zeros(m))<EOL>self._effsizes.bounds = [(-<NUM_LIT>, +<NUM_LIT:200>)] * m<EOL>self._X = X<EOL>Function.__init__(self, "<STR_LIT>", effsizes=self._effsizes)<EOL>

Constructor.

Parameters
----------
X : array_like
    Covariates X, from X𝜶.


return self._X @ self._effsizes<EOL>

Linear mean function.

Returns
-------
𝐦 : (n,) ndarray
    X𝜶.


return dict(effsizes=self._X)<EOL>

Gradient of the linear mean function over the effect sizes.

Returns
-------
effsizes : (n, m) ndarray
    X.


return self._effsizes.value<EOL>


self._dim = dim<EOL>self._I = eye(dim)<EOL>self._logscale = Scalar(<NUM_LIT:0.0>)<EOL>Function.__init__(self, "<STR_LIT>", logscale=self._logscale)<EOL>self._logscale.bounds = (-<NUM_LIT>, +<NUM_LIT:10>)<EOL>

Constructor.

Parameters
----------
dim : int
    Matrix dimension, d.


return exp(self._logscale)<EOL>


return self._I.shape[<NUM_LIT:0>]<EOL>

Dimension of the matrix, d.

It corresponds to the number of rows and to the number of columns.


return self.scale * self._I<EOL>

Covariance matrix.

Returns
-------
K : ndarray
    s⋅I, for scale s and a d×d identity matrix I.


return dict(logscale=self.value())<EOL>

Derivative of the covariance matrix over log(s), s⋅I.

Returns
-------
logscale : ndarray
    s⋅I, for scale s and a d×d identity matrix I.


from numpy_sugar.linalg import check_symmetry<EOL>self._logscale = Scalar(<NUM_LIT:0.0>)<EOL>Function.__init__(self, "<STR_LIT>", logscale=self._logscale)<EOL>self._logscale.bounds = (-<NUM_LIT>, +<NUM_LIT:10>)<EOL>if not check_symmetry(K0):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>self._K0 = K0<EOL>

Constructor.

Parameters
----------
K0 : array_like
    A semi-definite positive matrix.


return float(exp(self._logscale.value))<EOL>


return self.scale * self._K0<EOL>

Covariance matrix, s⋅K₀.

Returns
-------
K : ndarray
    s⋅K₀.


return dict(logscale=self.scale * self._K0)<EOL>

Derivative of the covariance matrix over log(s).

Returns
-------
logscale : float
    s⋅K₀.


from numpy_sugar import epsilon<EOL>dim = int(dim)<EOL>tsize = ((dim + <NUM_LIT:1>) * dim) // <NUM_LIT:2><EOL>self._L = zeros((dim, dim))<EOL>self._tril1 = tril_indices_from(self._L, k=-<NUM_LIT:1>)<EOL>self._diag = diag_indices_from(self._L)<EOL>self._L[self._tril1] = <NUM_LIT:1><EOL>self._L[self._diag] = <NUM_LIT:0><EOL>self._epsilon = epsilon.small * <NUM_LIT:1000><EOL>self._Lu = Vector(zeros(tsize))<EOL>self._Lu.value[: tsize - dim] = <NUM_LIT:1><EOL>n = self.L.shape[<NUM_LIT:0>]<EOL>self._grad_Lu = zeros((n, n, self._Lu.shape[<NUM_LIT:0>]))<EOL>Function.__init__(self, "<STR_LIT>", Lu=self._Lu)<EOL>bounds = [(-inf, +inf)] * (tsize - dim)<EOL>bounds += [(log(epsilon.small * <NUM_LIT:1000>), +<NUM_LIT:11>)] * dim<EOL>self._Lu.bounds = bounds<EOL>self._cache = {"<STR_LIT>": None}<EOL>self.listen(self._parameters_update)<EOL>self._nparams = tsize<EOL>

Constructor.

Parameters
----------
dim : int
    Dimension d of the free-form covariance matrix.


return self._nparams<EOL>


Listen to parameters change.

Parameters
----------
func : callable
    Function to be called when a parameter changes.


n = self._L.shape[<NUM_LIT:0>]<EOL>return (n, n)<EOL>


from numpy.linalg import svd<EOL>if self._cache["<STR_LIT>"] is not None:<EOL><INDENT>return self._cache["<STR_LIT>"]<EOL><DEDENT>U, S = svd(self.L)[:<NUM_LIT:2>]<EOL>S *= S<EOL>S += self._epsilon<EOL>self._cache["<STR_LIT>"] = S, U<EOL>return self._cache["<STR_LIT>"]<EOL>

Eigen decomposition of K.

Returns
-------
S : ndarray
    The eigenvalues in ascending order, each repeated according to its
    multiplicity.
U : ndarray
    Normalized eigenvectors.


return self._Lu.value<EOL>


m = len(self._tril1[<NUM_LIT:0>])<EOL>self._L[self._tril1] = self._Lu.value[:m]<EOL>self._L[self._diag] = exp(self._Lu.value[m:])<EOL>return self._L<EOL>

Lower-triangular matrix L such that K = LLᵀ + ϵI.

Returns
-------
L : (d, d) ndarray
    Lower-triangular matrix.


from numpy.linalg import slogdet<EOL>K = self.value()<EOL>sign, logdet = slogdet(K)<EOL>if sign != <NUM_LIT:1.0>:<EOL><INDENT>msg = "<STR_LIT>"<EOL>msg += f"<STR_LIT>"<EOL>raise RuntimeError(msg)<EOL><DEDENT>return logdet<EOL>

Log of ｜K｜.

Returns
-------
float
    Log-determinant of K.


K = dot(self.L, self.L.T)<EOL>return K + self._epsilon * eye(K.shape[<NUM_LIT:0>])<EOL>

Covariance matrix.

Returns
-------
K : ndarray
    Matrix K = LLᵀ + ϵI, for a very small positive number ϵ.


L = self.L<EOL>self._grad_Lu[:] = <NUM_LIT:0><EOL>for i in range(len(self._tril1[<NUM_LIT:0>])):<EOL><INDENT>row = self._tril1[<NUM_LIT:0>][i]<EOL>col = self._tril1[<NUM_LIT:1>][i]<EOL>self._grad_Lu[row, :, i] = L[:, col]<EOL>self._grad_Lu[:, row, i] += L[:, col]<EOL><DEDENT>m = len(self._tril1[<NUM_LIT:0>])<EOL>for i in range(len(self._diag[<NUM_LIT:0>])):<EOL><INDENT>row = self._diag[<NUM_LIT:0>][i]<EOL>col = self._diag[<NUM_LIT:1>][i]<EOL>self._grad_Lu[row, :, m + i] = L[row, col] * L[:, col]<EOL>self._grad_Lu[:, row, m + i] += L[row, col] * L[:, col]<EOL><DEDENT>return {"<STR_LIT>": self._grad_Lu}<EOL>

Derivative of the covariance matrix over the parameters of L.

Returns
-------
Lu : ndarray
    Derivative of K over the lower triangular part of L.


self._covariances = [c for c in covariances]<EOL>Function.__init__(self, "<STR_LIT>", composite=self._covariances)<EOL>

Constructor.

Parameters
----------
covariances : list
    List of covariance functions.


return add.reduce([cov.value() for cov in self._covariances])<EOL>

r"""
        Sum of covariance matrices.

        Returns
        -------
        K : ndarray
            K₀ + K₁ + ⋯


grad = {}<EOL>for i, f in enumerate(self._covariances):<EOL><INDENT>for varname, g in f.gradient().items():<EOL><INDENT>grad[f"<STR_LIT>"] = g<EOL><DEDENT><DEDENT>return grad<EOL>

Sum of covariance function derivatives.

Returns
-------
dict
    ∂K₀ + ∂K₁ + ⋯


self._L = ones((n, m))<EOL>self._Lu = Vector(self._L.ravel())<EOL>Function.__init__(self, "<STR_LIT>", Lu=self._Lu)<EOL>

Constructor.

Parameters
----------
n : int
    Covariance dimension.
m : int
    Upper limit of the covariance matrix rank.


return self._L.size<EOL>


return self._L<EOL>

Matrix L from K = LLᵀ.

Returns
-------
L : (n, m) ndarray
    Parametric matrix.


return dot(self.L, self.L.T)<EOL>

Covariance matrix.

Returns
-------
K : (n, n) ndarray
    K = LLᵀ.


L = self.L<EOL>n = self.L.shape[<NUM_LIT:0>]<EOL>grad = {"<STR_LIT>": zeros((n, n, n * self._L.shape[<NUM_LIT:1>]))}<EOL>for ii in range(self._L.shape[<NUM_LIT:0>] * self._L.shape[<NUM_LIT:1>]):<EOL><INDENT>row = ii // self._L.shape[<NUM_LIT:1>]<EOL>col = ii % self._L.shape[<NUM_LIT:1>]<EOL>grad["<STR_LIT>"][row, :, ii] = L[:, col]<EOL>grad["<STR_LIT>"][:, row, ii] += L[:, col]<EOL><DEDENT>return grad<EOL>

Derivative of the covariance matrix over the lower triangular, flat part of L.

It is equal to

    ∂K/∂Lᵢⱼ = ALᵀ + LAᵀ,

where Aᵢⱼ is an n×m matrix of zeros except at [Aᵢⱼ]ᵢⱼ=1.

Returns
-------
Lu : ndarray
    Derivative of K over the lower-triangular, flat part of L.


self._cache = {"<STR_LIT>": None}<EOL>self._C0 = LRFreeFormCov(dim, rank)<EOL>self._C0.name = "<STR_LIT>"<EOL>self._C1 = FreeFormCov(dim)<EOL>self._C1.name = "<STR_LIT>"<EOL>G = atleast_2d(asarray(G, float))<EOL>self._G = G<EOL>self._Sxe = None<EOL>self._Sx = None<EOL>self._Lx = None<EOL>self._LxG = None<EOL>self._diag_LxGGLx = None<EOL>self._Lxe = None<EOL>self._LxGe = None<EOL>self._diag_LxGGLxe = None<EOL>Function.__init__(<EOL>self, "<STR_LIT>", composite=[("<STR_LIT>", self._C0), ("<STR_LIT>", self._C1)]<EOL>)<EOL>self._C0.listen(self._parameters_update)<EOL>self._C1.listen(self._parameters_update)<EOL>

Constructor.

Parameters
----------
dim : int
    Dimension d for the square matrices C₀ and C₁.
rank : int
    Maximum rank of the C₁ matrix.


return self._C0.nparams + self._C1.nparams<EOL>


from scipy.linalg import svd<EOL>from numpy_sugar.linalg import ddot<EOL>U, S, _ = svd(self._G, full_matrices=False, check_finite=False)<EOL>if U.shape[<NUM_LIT:1>] < self._G.shape[<NUM_LIT:1>]:<EOL><INDENT>return ddot(U, S)<EOL><DEDENT>return self._G<EOL>

Result of US from the SVD decomposition G = USVᵀ.


self._C0.listen(func)<EOL>self._C1.listen(func)<EOL>


self._init_svd()<EOL>return self._Lx<EOL>


from numpy_sugar.linalg import ddot<EOL>self._init_svd()<EOL>if self._cache["<STR_LIT>"] is not None:<EOL><INDENT>return self._cache["<STR_LIT>"]<EOL><DEDENT>S1, U1 = self.C1.eigh()<EOL>U1S1 = ddot(U1, <NUM_LIT:1> / sqrt(S1))<EOL>Sh, Uh = eigh(U1S1.T @ self.C0.value() @ U1S1)<EOL>self._cache["<STR_LIT>"] = {<EOL>"<STR_LIT>": (U1S1 @ Uh).T,<EOL>"<STR_LIT:D>": <NUM_LIT:1> / (kron(Sh, self._Sx) + <NUM_LIT:1>),<EOL>"<STR_LIT>": <NUM_LIT:1> / (kron(Sh, self._Sxe) + <NUM_LIT:1>),<EOL>}<EOL>return self._cache["<STR_LIT>"]<EOL>

Implements Lₕ and D.

Returns
-------
Lh : ndarray
    Uₕᵀ S₁⁻½ U₁ᵀ.
D : ndarray
    (Sₕ ⊗ Sₓ + Iₕₓ)⁻¹.


return self._LhD["<STR_LIT>"]<EOL>


return self._LhD["<STR_LIT:D>"]<EOL>

(Sₕ ⊗ Sₓ + Iₕₓ)⁻¹.


return self._G<EOL>


return self._C0<EOL>


return self._C1<EOL>


C0 = self._C0.value()<EOL>C1 = self._C1.value()<EOL>return kron(C0, self._GG) + kron(C1, self._I)<EOL>

Covariance matrix K = C₀ ⊗ GGᵀ + C₁ ⊗ I.

Returns
-------
K : ndarray
    C₀ ⊗ GGᵀ + C₁ ⊗ I.


self._init_svd()<EOL>C0 = self._C0.gradient()["<STR_LIT>"].T<EOL>C1 = self._C1.gradient()["<STR_LIT>"].T<EOL>grad = {"<STR_LIT>": kron(C0, self._X).T, "<STR_LIT>": kron(C1, self._I).T}<EOL>return grad<EOL>

Gradient of K.

Returns
-------
C0 : ndarray
    Derivative of C₀ over its parameters.
C1 : ndarray
    Derivative of C₁ over its parameters.


self._init_svd()<EOL>V = unvec(v, (self.G.shape[<NUM_LIT:0>], -<NUM_LIT:1>) + v.shape[<NUM_LIT:1>:])<EOL>r = {}<EOL>C = self._C0.gradient()["<STR_LIT>"]<EOL>r["<STR_LIT>"] = tensordot(V.T @ self.G @ self.G.T, C, axes=([-<NUM_LIT:2>], [<NUM_LIT:0>]))<EOL>r["<STR_LIT>"] = r["<STR_LIT>"].reshape(V.shape[<NUM_LIT:2>:] + (-<NUM_LIT:1>,) + (C.shape[-<NUM_LIT:1>],), order="<STR_LIT:F>")<EOL>C = self._C1.gradient()["<STR_LIT>"]<EOL>r["<STR_LIT>"] = tensordot(V.T, C, axes=([-<NUM_LIT:2>], [<NUM_LIT:0>]))<EOL>r["<STR_LIT>"] = r["<STR_LIT>"].reshape(V.shape[<NUM_LIT:2>:] + (-<NUM_LIT:1>,) + (C.shape[-<NUM_LIT:1>],), order="<STR_LIT:F>")<EOL>return r<EOL>

Implements ∂K⋅v.

Parameters
----------
v : array_like
    Vector from ∂K⋅v.

Returns
-------
C0.Lu : ndarray
    ∂K⋅v, where the gradient is taken over the C₀ parameters.
C1.Lu : ndarray
    ∂K⋅v, where the gradient is taken over the C₁ parameters.


from numpy_sugar.linalg import ddot<EOL>self._init_svd()<EOL>L = kron(self.Lh, self.Lx)<EOL>return L.T @ ddot(self.D, L @ v, left=True)<EOL>

Implements the product K⁻¹⋅v.

Parameters
----------
v : array_like
    Array to be multiplied.

Returns
-------
x : ndarray
    Solution x to the equation K⋅x = y.


self._init_svd()<EOL>return -log(self._De).sum() + self.G.shape[<NUM_LIT:0>] * self.C1.logdet()<EOL>

Implements log|K| = - log|D| + n⋅log|C₁|.

Returns
-------
logdet : float
    Log-determinant of K.


from numpy_sugar.linalg import dotd<EOL>self._init_svd()<EOL>dC0 = self._C0.gradient()["<STR_LIT>"]<EOL>grad_C0 = zeros_like(self._C0.Lu)<EOL>for i in range(self._C0.Lu.shape[<NUM_LIT:0>]):<EOL><INDENT>t = kron(dotd(self.Lh, dC0[..., i] @ self.Lh.T), self._diag_LxGGLxe)<EOL>grad_C0[i] = (self._De * t).sum()<EOL><DEDENT>dC1 = self._C1.gradient()["<STR_LIT>"]<EOL>grad_C1 = zeros_like(self._C1.Lu)<EOL>p = self._Sxe.shape[<NUM_LIT:0>]<EOL>np = self._G.shape[<NUM_LIT:0>] - p<EOL>for i in range(self._C1.Lu.shape[<NUM_LIT:0>]):<EOL><INDENT>t = (dotd(self.Lh, dC1[..., i] @ self.Lh.T) * np).sum()<EOL>t1 = kron(dotd(self.Lh, dC1[..., i] @ self.Lh.T), eye(p))<EOL>t += (self._De * t1).sum()<EOL>grad_C1[i] = t<EOL><DEDENT>return {"<STR_LIT>": grad_C0, "<STR_LIT>": grad_C1}<EOL>

Implements ∂log|K| = Tr[K⁻¹∂K].

It can be shown that::

    ∂log|K| = diag(D)ᵀdiag(L(∂K)Lᵀ) = diag(D)ᵀ(diag(Lₕ∂C₀Lₕᵀ)⊗diag(LₓGGᵀLₓᵀ)),

when the derivative is over the parameters of C₀. Similarly,

    ∂log|K| = diag(D)ᵀdiag(L(∂K)Lᵀ) = diag(D)ᵀ(diag(Lₕ∂C₁Lₕᵀ)⊗diag(I)),

over the parameters of C₁.

Returns
-------
C0 : ndarray
    Derivative of C₀ over its parameters.
C1 : ndarray
    Derivative of C₁ over its parameters.


self._init_svd()<EOL>def dot(a, b):<EOL><INDENT>r = tensordot(a, b, axes=([<NUM_LIT:1>], [<NUM_LIT:0>]))<EOL>if a.ndim > b.ndim:<EOL><INDENT>return r.transpose([<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:1>])<EOL><DEDENT>return r<EOL><DEDENT>Lh = self.Lh<EOL>V = unvec(v, (self.Lx.shape[<NUM_LIT:0>], -<NUM_LIT:1>) + v.shape[<NUM_LIT:1>:])<EOL>LdKL_dot = {<EOL>"<STR_LIT>": empty((v.shape[<NUM_LIT:0>],) + v.shape[<NUM_LIT:1>:] + (self._C0.Lu.shape[<NUM_LIT:0>],)),<EOL>"<STR_LIT>": empty((v.shape[<NUM_LIT:0>],) + v.shape[<NUM_LIT:1>:] + (self._C1.Lu.shape[<NUM_LIT:0>],)),<EOL>}<EOL>dC0 = self._C0.gradient()["<STR_LIT>"]<EOL>for i in range(self._C0.Lu.shape[<NUM_LIT:0>]):<EOL><INDENT>t = dot(self._LxG, dot(self._LxG.T, dot(V, Lh @ dC0[..., i] @ Lh.T)))<EOL>LdKL_dot["<STR_LIT>"][..., i] = t.reshape((-<NUM_LIT:1>,) + t.shape[<NUM_LIT:2>:], order="<STR_LIT:F>")<EOL><DEDENT>dC1 = self._C1.gradient()["<STR_LIT>"]<EOL>for i in range(self._C1.Lu.shape[<NUM_LIT:0>]):<EOL><INDENT>t = dot(V, Lh @ dC1[..., i] @ Lh.T)<EOL>LdKL_dot["<STR_LIT>"][..., i] = t.reshape((-<NUM_LIT:1>,) + t.shape[<NUM_LIT:2>:], order="<STR_LIT:F>")<EOL><DEDENT>return LdKL_dot<EOL>

Implements L(∂K)Lᵀv.

The array v can have one or two dimensions and the first dimension has to have
size n⋅p.

Let vec(V) = v. We have

    L(∂K)Lᵀ⋅v = ((Lₕ∂C₀Lₕᵀ) ⊗ (LₓGGᵀLₓᵀ))vec(V) = vec(LₓGGᵀLₓᵀVLₕ∂C₀Lₕᵀ),

when the derivative is over the parameters of C₀. Similarly,

    L(∂K)Lᵀv = ((Lₕ∂C₁Lₕᵀ) ⊗ (LₓLₓᵀ))vec(V) = vec(LₓLₓᵀVLₕ∂C₁Lₕᵀ),

over the parameters of C₁.


self._logscale = Scalar(<NUM_LIT:0.0>)<EOL>self._X = X<EOL>Function.__init__(self, "<STR_LIT>", logscale=self._logscale)<EOL>self._logscale.bounds = (-<NUM_LIT>, +<NUM_LIT:10>)<EOL>

Constructor.

Parameters
----------
X : array_like
    Matrix X from K = s⋅XXᵀ.


return self._X<EOL>


return exp(self._logscale.value)<EOL>


X = self.X<EOL>return self.scale * (X @ X.T)<EOL>

Covariance matrix.

Returns
-------
K : ndarray
    s⋅XXᵀ.


return dict(logscale=self.value())<EOL>

Derivative of the covariance matrix over log(s).

Returns
-------
logscale : ndarray
    s⋅XXᵀ.


def _bstar_1effect(beta, alpha, yTBy, yTBX, yTBM, XTBX, XTBM, MTBM):

from numpy_sugar import epsilon<EOL>from numpy_sugar.linalg import dotd<EOL>from numpy import sum<EOL>r = full(MTBM[<NUM_LIT:0>].shape[<NUM_LIT:0>], yTBy)<EOL>r -= <NUM_LIT:2> * add.reduce([dot(i, beta) for i in yTBX])<EOL>r -= <NUM_LIT:2> * add.reduce([i * alpha for i in yTBM])<EOL>r += add.reduce([dotd(beta.T, dot(i, beta)) for i in XTBX])<EOL>r += add.reduce([dotd(beta.T, i * alpha) for i in XTBM])<EOL>r += add.reduce([sum(alpha * i * beta, axis=<NUM_LIT:0>) for i in XTBM])<EOL>r += add.reduce([alpha * i.ravel() * alpha for i in MTBM])<EOL>return clip(r, epsilon.tiny, inf)<EOL>

Same as :func:`_bstar_set` but for single-effect.


def _bstar_set(beta, alpha, yTBy, yTBX, yTBM, XTBX, XTBM, MTBM):

from numpy_sugar import epsilon<EOL>r = yTBy<EOL>r -= <NUM_LIT:2> * add.reduce([i @ beta for i in yTBX])<EOL>r -= <NUM_LIT:2> * add.reduce([i @ alpha for i in yTBM])<EOL>r += add.reduce([beta.T @ i @ beta for i in XTBX])<EOL>r += <NUM_LIT:2> * add.reduce([beta.T @ i @ alpha for i in XTBM])<EOL>r += add.reduce([alpha.T @ i @ alpha for i in MTBM])<EOL>return clip(r, epsilon.tiny, inf)<EOL>

Compute -2𝐲ᵀBEⱼ𝐛ⱼ + (𝐛ⱼEⱼ)ᵀBEⱼ𝐛ⱼ.

For 𝐛ⱼ = [𝜷ⱼᵀ 𝜶ⱼᵀ]ᵀ.


n = self._nsamples<EOL>scale = self.null_scale<EOL>return (self._static_lml() - n * log(scale)) / <NUM_LIT:2><EOL>

Log of the marginal likelihood for the null hypothesis.

It is implemented as ::

    2·log(p(Y)) = -n·log(2𝜋s) - log｜D｜ - n,

Returns
-------
lml : float
    Log of the marginal likelihood.


ETBE = self._ETBE<EOL>yTBX = self._yTBX<EOL>A = sum(i.XTBX for i in ETBE)<EOL>b = sum(yTBX)<EOL>return rsolve(A, b)<EOL>

Optimal 𝜷 according to the marginal likelihood.

It is compute by solving the equation ::

    (XᵀBX)𝜷 = XᵀB𝐲.

Returns
-------
beta : ndarray
    Optimal 𝜷.


A = sum(i @ j.T for (i, j) in zip(self._XTQDi, self._XTQ))<EOL>return self.null_scale * pinv(A)<EOL>

Covariance of the optimal 𝜷 according to the marginal likelihood.

Returns
-------
beta_covariance : ndarray
    (Xᵀ(s(K + vI))⁻¹X)⁻¹.


return sqrt(self.null_beta_covariance.diagonal())<EOL>

Standard errors of the optimal 𝜷.

Returns
-------
beta_se : ndarray
    Square root of the diagonal of the beta covariance.


n = self._nsamples<EOL>beta = self.null_beta<EOL>sqrdot = self._yTBy - dot(sum(self._yTBX), beta)<EOL>return sqrdot / n<EOL>

Optimal s according to the marginal likelihood.

The optimal s is given by ::

    s = n⁻¹𝐲ᵀB(𝐲 - X𝜷),

where 𝜷 is optimal.

Returns
-------
scale : float
    Optimal scale.


from tqdm import tqdm<EOL>if M.ndim != <NUM_LIT:2>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>p = M.shape[<NUM_LIT:1>]<EOL>lmls = empty(p)<EOL>effsizes0 = empty((p, self._XTQ[<NUM_LIT:0>].shape[<NUM_LIT:0>]))<EOL>effsizes0_se = empty((p, self._XTQ[<NUM_LIT:0>].shape[<NUM_LIT:0>]))<EOL>effsizes1 = empty(p)<EOL>effsizes1_se = empty(p)<EOL>scales = empty(p)<EOL>if verbose:<EOL><INDENT>nchunks = min(p, <NUM_LIT:30>)<EOL><DEDENT>else:<EOL><INDENT>nchunks = min(p, <NUM_LIT:1>)<EOL><DEDENT>chunk_size = (p + nchunks - <NUM_LIT:1>) // nchunks<EOL>for i in tqdm(range(nchunks), desc="<STR_LIT>", disable=not verbose):<EOL><INDENT>start = i * chunk_size<EOL>stop = min(start + chunk_size, M.shape[<NUM_LIT:1>])<EOL>r = self._fast_scan_chunk(M[:, start:stop])<EOL>lmls[start:stop] = r["<STR_LIT>"]<EOL>effsizes0[start:stop, :] = r["<STR_LIT>"]<EOL>effsizes0_se[start:stop, :] = r["<STR_LIT>"]<EOL>effsizes1[start:stop] = r["<STR_LIT>"]<EOL>effsizes1_se[start:stop] = r["<STR_LIT>"]<EOL>scales[start:stop] = r["<STR_LIT>"]<EOL><DEDENT>return {<EOL>"<STR_LIT>": lmls,<EOL>"<STR_LIT>": effsizes0,<EOL>"<STR_LIT>": effsizes0_se,<EOL>"<STR_LIT>": effsizes1,<EOL>"<STR_LIT>": effsizes1_se,<EOL>"<STR_LIT>": scales,<EOL>}<EOL>

LMLs, fixed-effect sizes, and scales for single-marker scan.

Parameters
----------
M : array_like
    Matrix of fixed-effects across columns.
verbose : bool, optional
    ``True`` for progress information; ``False`` otherwise.
    Defaults to ``True``.

Returns
-------
lmls : ndarray
    Log of the marginal likelihoods.
effsizes0 : ndarray
    Covariate fixed-effect sizes.
effsizes1 : ndarray
    Candidate set fixed-effect sizes.
scales : ndarray
    Scales.


from numpy_sugar.linalg import ddot<EOL>from numpy_sugar import is_all_finite<EOL>M = asarray(M, float)<EOL>if M.shape[<NUM_LIT:1>] == <NUM_LIT:0>:<EOL><INDENT>return {<EOL>"<STR_LIT>": self.null_lml(),<EOL>"<STR_LIT>": self.null_beta,<EOL>"<STR_LIT>": self.null_beta_se,<EOL>"<STR_LIT>": empty((<NUM_LIT:0>)),<EOL>"<STR_LIT>": empty((<NUM_LIT:0>)),<EOL>"<STR_LIT>": self.null_scale,<EOL>}<EOL><DEDENT>if not is_all_finite(M):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>MTQ = [dot(M.T, Q) for Q in self._QS[<NUM_LIT:0>] if Q.size > <NUM_LIT:0>]<EOL>yTBM = [dot(i, j.T) for (i, j) in zip(self._yTQDi, MTQ)]<EOL>XTBM = [dot(i, j.T) for (i, j) in zip(self._XTQDi, MTQ)]<EOL>D = self._D<EOL>MTBM = [ddot(i, <NUM_LIT:1> / j) @ i.T for i, j in zip(MTQ, D) if j.min() > <NUM_LIT:0>]<EOL>return self._multicovariate_set(yTBM, XTBM, MTBM)<EOL>

LML, fixed-effect sizes, and scale of the candidate set.

Parameters
----------
M : array_like
    Fixed-effects set.

Returns
-------
lml : float
    Log of the marginal likelihood.
effsizes0 : ndarray
    Covariates fixed-effect sizes.
effsizes0_se : ndarray
    Covariates fixed-effect size standard errors.
effsizes1 : ndarray
    Candidate set fixed-effect sizes.
effsizes1_se : ndarray
    Candidate fixed-effect size standard errors.
scale : ndarray
    Optimal scale.


self._Y = asarray(Y, float)<EOL>self._A = asarray(A, float)<EOL>self._X = asarray(X, float)<EOL>self._G = asarray(G, float)<EOL>self._H = terms["<STR_LIT:H>"]<EOL>self._logdetK = terms["<STR_LIT>"]<EOL>self._W = terms["<STR_LIT>"]<EOL>self._yKiy = terms["<STR_LIT>"]<EOL>self._WA = terms["<STR_LIT>"]<EOL>self._WL0 = terms["<STR_LIT>"]<EOL>self._Lz = terms["<STR_LIT>"]<EOL>self._XRiM = terms["<STR_LIT>"]<EOL>self._ZiXRiy = terms["<STR_LIT>"]<EOL>self._ZiXRiM = terms["<STR_LIT>"]<EOL>self._MRiM = terms["<STR_LIT>"]<EOL>self._MRiXZiXRiM = terms["<STR_LIT>"]<EOL>self._MRiy = terms["<STR_LIT>"]<EOL>self._MRiXZiXRiy = terms["<STR_LIT>"]<EOL>

Constructor.

Parameters
----------
Y : (n, p) array_like
    Outcome matrix.
A : (n, n) array_like
    Trait-by-trait design matrix.
X : (n, c) array_like
    Covariates design matrix.
G : (n, r) array_like
    Matrix G from the GGᵀ term.
terms : dict
    Pre-computed terms.


np = self._nsamples * self._ntraits<EOL>scale = self.null_scale<EOL>return self._static_lml() / <NUM_LIT:2> - np * safe_log(scale) / <NUM_LIT:2> - np / <NUM_LIT:2><EOL>

Log of the marginal likelihood for the null hypothesis.

It is implemented as ::

    2·log(p(Y)) = -n·p·log(2𝜋s) - log｜K｜ - n·p,

for which s and 𝚩 are optimal.

Returns
-------
lml : float
    Log of the marginal likelihood.


return rsolve(self._MKiM, self._MKiy)<EOL>

Optimal 𝛃 according to the marginal likelihood.

It is compute by solving the equation ::

    MᵀK⁻¹M𝛃 = MᵀK⁻¹𝐲,

for 𝐲 = vec(Y) and M = (A ⊗ X)vec(𝚩).

Returns
-------
effsizes : ndarray
    Optimal 𝛃.


return self.null_scale * pinv(self._H)<EOL>

Covariance of the optimal 𝛃 according to the marginal likelihood.

Returns
-------
effsizes-covariance : ndarray
    s(MᵀK⁻¹M)⁻¹.


Standard errors of the optimal 𝛃.

Returns
-------
beta_se : ndarray
    Square root of the diagonal of the beta covariance.


np = self._nsamples * self._ntraits<EOL>b = vec(self.null_beta)<EOL>mKiy = b.T @ self._MKiy<EOL>sqrtdot = self._yKiy - mKiy<EOL>scale = sqrtdot / np<EOL>return scale<EOL>

Optimal s according to the marginal likelihood.

The optimal s is given by

    s = (n·p)⁻¹𝐲ᵀK⁻¹(𝐲 - 𝐦),

where 𝐦 = (A ⊗ X)vec(𝚩) and 𝚩 is optimal.

Returns
-------
scale : float
    Optimal scale.


from numpy import empty<EOL>from numpy.linalg import multi_dot<EOL>from numpy_sugar import epsilon, is_all_finite<EOL>from scipy.linalg import cho_solve<EOL>A1 = asarray(A1, float)<EOL>X1 = asarray(X1, float)<EOL>if not is_all_finite(A1):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not is_all_finite(X1):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if A1.shape[<NUM_LIT:1>] == <NUM_LIT:0>:<EOL><INDENT>beta_se = sqrt(self.null_beta_covariance.diagonal())<EOL>return {<EOL>"<STR_LIT>": self.null_lml(),<EOL>"<STR_LIT>": unvec(self.null_beta, (self._ncovariates, -<NUM_LIT:1>)),<EOL>"<STR_LIT>": unvec(beta_se, (self._ncovariates, -<NUM_LIT:1>)),<EOL>"<STR_LIT>": empty((<NUM_LIT:0>,)),<EOL>"<STR_LIT>": empty((<NUM_LIT:0>,)),<EOL>"<STR_LIT>": self.null_scale,<EOL>}<EOL><DEDENT>X1X1 = X1.T @ X1<EOL>XX1 = self._X.T @ X1<EOL>AWA1 = self._WA.T @ A1<EOL>A1W = A1.T @ self._W<EOL>GX1 = self._G.T @ X1<EOL>MRiM1 = kron(AWA1, XX1)<EOL>M1RiM1 = kron(A1W @ A1, X1X1)<EOL>M1Riy = vec(multi_dot([X1.T, self._Y, A1W.T]))<EOL>XRiM1 = kron(self._WL0.T @ A1, GX1)<EOL>ZiXRiM1 = cho_solve(self._Lz, XRiM1)<EOL>MRiXZiXRiM1 = self._XRiM.T @ ZiXRiM1<EOL>M1RiXZiXRiM1 = XRiM1.T @ ZiXRiM1<EOL>M1RiXZiXRiy = XRiM1.T @ self._ZiXRiy<EOL>T0 = [[self._MRiM, MRiM1], [MRiM1.T, M1RiM1]]<EOL>T1 = [[self._MRiXZiXRiM, MRiXZiXRiM1], [MRiXZiXRiM1.T, M1RiXZiXRiM1]]<EOL>T2 = [self._MRiy, M1Riy]<EOL>T3 = [self._MRiXZiXRiy, M1RiXZiXRiy]<EOL>MKiM = block(T0) - block(T1)<EOL>MKiy = block(T2) - block(T3)<EOL>beta = rsolve(MKiM, MKiy)<EOL>mKiy = beta.T @ MKiy<EOL>cp = self._ntraits * self._ncovariates<EOL>effsizes0 = unvec(beta[:cp], (self._ncovariates, self._ntraits))<EOL>effsizes1 = unvec(beta[cp:], (X1.shape[<NUM_LIT:1>], A1.shape[<NUM_LIT:1>]))<EOL>np = self._nsamples * self._ntraits<EOL>sqrtdot = self._yKiy - mKiy<EOL>scale = clip(sqrtdot / np, epsilon.tiny, inf)<EOL>lml = self._static_lml() / <NUM_LIT:2> - np * safe_log(scale) / <NUM_LIT:2> - np / <NUM_LIT:2><EOL>effsizes_se = sqrt(clip(scale * pinv(MKiM).diagonal(), epsilon.tiny, inf))<EOL>effsizes0_se = unvec(effsizes_se[:cp], (self._ncovariates, self._ntraits))<EOL>effsizes1_se = unvec(effsizes_se[cp:], (X1.shape[<NUM_LIT:1>], A1.shape[<NUM_LIT:1>]))<EOL>return {<EOL>"<STR_LIT>": lml,<EOL>"<STR_LIT>": effsizes0,<EOL>"<STR_LIT>": effsizes1,<EOL>"<STR_LIT>": scale,<EOL>"<STR_LIT>": effsizes0_se,<EOL>"<STR_LIT>": effsizes1_se,<EOL>}<EOL>

LML, fixed-effect sizes, and scale of the candidate set.

Parameters
----------
A1 : (p, e) array_like
    Trait-by-environments design matrix.
X1 : (n, m) array_like
    Variants set matrix.

Returns
-------
lml : float
    Log of the marginal likelihood for the set.
effsizes0 : (c, p) ndarray
    Fixed-effect sizes for the covariates.
effsizes0_se : (c, p) ndarray
    Fixed-effect size standard errors for the covariates.
effsizes1 : (m, e) ndarray
    Fixed-effect sizes for the candidates.
effsizes1_se : (m, e) ndarray
    Fixed-effect size standard errors for the candidates.
scale : float
    Optimal scale.


def __init__(self, y, X, QS=None, restricted=False):

from numpy_sugar import is_all_finite<EOL>from numpy_sugar.linalg import ddot, economic_svd<EOL>logistic = Scalar(<NUM_LIT:0.0>)<EOL>logistic.listen(self._delta_update)<EOL>logistic.bounds = (-numbers.logmax, +numbers.logmax)<EOL>Function.__init__(self, "<STR_LIT>", logistic=logistic)<EOL>self._logistic = logistic<EOL>y = asarray(y, float).ravel()<EOL>if not is_all_finite(y):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>X = atleast_2d(asarray(X, float).T).T<EOL>if not is_all_finite(X):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>self._optimal = {"<STR_LIT>": False, "<STR_LIT>": False}<EOL>if QS is None:<EOL><INDENT>QS = economic_qs_zeros(len(y))<EOL>self.delta = <NUM_LIT:1.0><EOL>logistic.fix()<EOL><DEDENT>else:<EOL><INDENT>self.delta = <NUM_LIT:0.5><EOL><DEDENT>if QS[<NUM_LIT:0>][<NUM_LIT:0>].shape[<NUM_LIT:0>] != len(y):<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg)<EOL><DEDENT>if y.shape[<NUM_LIT:0>] != X.shape[<NUM_LIT:0>]:<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg)<EOL><DEDENT>self._Darr = []<EOL>n = y.shape[<NUM_LIT:0>]<EOL>d = self.delta<EOL>if QS[<NUM_LIT:1>].size > <NUM_LIT:0>:<EOL><INDENT>self._Darr += [QS[<NUM_LIT:1>] * (<NUM_LIT:1> - d) + d]<EOL><DEDENT>if QS[<NUM_LIT:1>].size < n:<EOL><INDENT>self._Darr += [full(n - QS[<NUM_LIT:1>].size, d)]<EOL><DEDENT>self._y = y<EOL>self._QS = QS<EOL>SVD = economic_svd(X)<EOL>self._X = {"<STR_LIT:X>": X, "<STR_LIT>": ddot(SVD[<NUM_LIT:0>], SVD[<NUM_LIT:1>]), "<STR_LIT>": SVD[<NUM_LIT:2>]}<EOL>self._tbeta = zeros(len(SVD[<NUM_LIT:1>]))<EOL>self._scale = <NUM_LIT:1.0><EOL>self._fix = {"<STR_LIT>": False, "<STR_LIT>": False}<EOL>self._restricted = restricted<EOL>

Constructor.

Parameters
----------
y : array_like
    Outcome.
X : array_like
    Covariates as a two-dimensional array.
QS : tuple
    Economic eigendecompositon in form of ``((Q0, Q1), S0)`` of a
    covariance matrix ``K``.
restricted : bool
    ``True`` for restricted maximum likelihood optimization; ``False``
    otherwise. Defaults to ``False``.


from numpy_sugar.linalg import rsolve<EOL>return rsolve(self._X["<STR_LIT>"], rsolve(self._X["<STR_LIT>"], self.mean()))<EOL>

Fixed-effect sizes.

Returns
-------
effect-sizes : numpy.ndarray
    Optimal fixed-effect sizes.

Notes
-----
Setting the derivative of log(p(𝐲)) over effect sizes equal
to zero leads to solutions 𝜷 from equation ::

    (QᵀX)ᵀD⁻¹(QᵀX)𝜷 = (QᵀX)ᵀD⁻¹(Qᵀ𝐲).


from numpy_sugar.linalg import ddot<EOL>tX = self._X["<STR_LIT>"]<EOL>Q = concatenate(self._QS[<NUM_LIT:0>], axis=<NUM_LIT:1>)<EOL>S0 = self._QS[<NUM_LIT:1>]<EOL>D = self.v0 * S0 + self.v1<EOL>D = D.tolist() + [self.v1] * (len(self._y) - len(D))<EOL>D = asarray(D)<EOL>A = inv(tX.T @ (Q @ ddot(<NUM_LIT:1> / D, Q.T @ tX)))<EOL>VT = self._X["<STR_LIT>"]<EOL>H = lstsq(VT, A, rcond=None)[<NUM_LIT:0>]<EOL>return lstsq(VT, H.T, rcond=None)[<NUM_LIT:0>]<EOL>

Estimates the covariance-matrix of the optimal beta.

Returns
-------
beta-covariance : ndarray
    (Xᵀ(s((1-𝛿)K + 𝛿I))⁻¹X)⁻¹.

References
----------
.. Rencher, A. C., & Schaalje, G. B. (2008). Linear models in statistics. John
   Wiley & Sons.


if param == "<STR_LIT>":<EOL><INDENT>super()._fix("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>self._fix[param] = True<EOL><DEDENT>

Disable parameter optimization.

Parameters
----------
param : str
    Possible values are ``"delta"``, ``"beta"``, and ``"scale"``.


if param == "<STR_LIT>":<EOL><INDENT>self._unfix("<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>self._fix[param] = False<EOL><DEDENT>

Enable parameter optimization.

Parameters
----------
param : str
    Possible values are ``"delta"``, ``"beta"``, and ``"scale"``.


return self.scale * (<NUM_LIT:1> - self.delta)<EOL>

First variance.

Returns
-------
v0 : float
    s(1 - 𝛿).


return self.scale * self.delta<EOL>

Second variance.

Returns
-------
v1 : float
    s𝛿.


if not self._isfixed("<STR_LIT>"):<EOL><INDENT>self._maximize_scalar(desc="<STR_LIT>", rtol=<NUM_LIT>, atol=<NUM_LIT>, verbose=verbose)<EOL><DEDENT>if not self._fix["<STR_LIT>"]:<EOL><INDENT>self._update_beta()<EOL><DEDENT>if not self._fix["<STR_LIT>"]:<EOL><INDENT>self._update_scale()<EOL><DEDENT>

Maximise the marginal likelihood.

Parameters
----------
verbose : bool, optional
    ``True`` for progress output; ``False`` otherwise.
    Defaults to ``True``.


v0 = self.v0<EOL>v1 = self.v1<EOL>QS = (self._QS[<NUM_LIT:0>], v0 * self._QS[<NUM_LIT:1>])<EOL>return FastScanner(self._y, self.X, QS, v1)<EOL>

Return :class:`.FastScanner` for association scan.

Returns
-------
fast-scanner : :class:`.FastScanner`
    Instance of a class designed to perform very fast association scan.


if not self._fix["<STR_LIT>"]:<EOL><INDENT>self._update_beta()<EOL><DEDENT>if not self._fix["<STR_LIT>"]:<EOL><INDENT>self._update_scale()<EOL><DEDENT>return self.lml()<EOL>


raise NotImplementedError<EOL>


return len(self._y)<EOL>


return self._X["<STR_LIT:X>"].shape[<NUM_LIT:1>]<EOL>


reml = (self._logdetXX() - self._logdetH()) / <NUM_LIT:2><EOL>if self._optimal["<STR_LIT>"]:<EOL><INDENT>lml = self._lml_optimal_scale()<EOL><DEDENT>else:<EOL><INDENT>lml = self._lml_arbitrary_scale()<EOL><DEDENT>return lml + reml<EOL>

Log of the marginal likelihood.

Returns
-------
lml : float
    Log of the marginal likelihood.

Notes
-----
The log of the marginal likelihood is given by ::

    2⋅log(p(𝐲)) = -n⋅log(2π) - n⋅log(s) - log|D| - (Qᵀ𝐲)ᵀs⁻¹D⁻¹(Qᵀ𝐲)
                + (Qᵀ𝐲)ᵀs⁻¹D⁻¹(QᵀX𝜷)/2 - (QᵀX𝜷)ᵀs⁻¹D⁻¹(QᵀX𝜷).

By using the optimal 𝜷, the log of the marginal likelihood can be rewritten
as::

    2⋅log(p(𝐲)) = -n⋅log(2π) - n⋅log(s) - log|D| + (Qᵀ𝐲)ᵀs⁻¹D⁻¹Qᵀ(X𝜷-𝐲).

In the extreme case where 𝜷 is such that 𝐲 = X𝜷, the maximum is attained as
s→0.

For optimals 𝜷 and s, the log of the marginal likelihood can be further
simplified to ::

    2⋅log(p(𝐲; 𝜷, s)) = -n⋅log(2π) - n⋅log s - log|D| - n.


return self._X["<STR_LIT:X>"]<EOL>

Covariates matrix.

Returns
-------
X : ndarray
    Covariates.


v = float(self._logistic.value)<EOL>if v > <NUM_LIT:0.0>:<EOL><INDENT>v = <NUM_LIT:1> / (<NUM_LIT:1> + exp(-v))<EOL><DEDENT>else:<EOL><INDENT>v = exp(v)<EOL>v = v / (v + <NUM_LIT:1.0>)<EOL><DEDENT>return min(max(v, epsilon.tiny), <NUM_LIT:1> - epsilon.tiny)<EOL>


return self._scale<EOL>

Scaling factor.

Returns
-------
scale : float
    Scaling factor.

Notes
-----
Setting the derivative of log(p(𝐲; 𝜷)), for which 𝜷 is optimal, over
scale equal to zero leads to the maximum ::

    s = n⁻¹(Qᵀ𝐲)ᵀD⁻¹ Qᵀ(𝐲-X𝜷).

In the case of restricted marginal likelihood ::

    s = (n-c)⁻¹(Qᵀ𝐲)ᵀD⁻¹ Qᵀ(𝐲-X𝜷),

where s is the number of covariates.


return self._X["<STR_LIT>"] @ self._tbeta<EOL>

Mean of the prior.

Formally, 𝐦 = X𝜷.

Returns
-------
mean : ndarray
    Mean of the prior.


from numpy_sugar.linalg import ddot, sum2diag<EOL>Q0 = self._QS[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>S0 = self._QS[<NUM_LIT:1>]<EOL>return sum2diag(dot(ddot(Q0, self.v0 * S0), Q0.T), self.v1)<EOL>

Covariance of the prior.

Returns
-------
covariance : ndarray
    v₀K + v₁I.


if not self._restricted:<EOL><INDENT>return <NUM_LIT:0.0><EOL><DEDENT>ldet = slogdet(self._X["<STR_LIT>"].T @ self._X["<STR_LIT>"])<EOL>if ldet[<NUM_LIT:0>] != <NUM_LIT:1.0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return ldet[<NUM_LIT:1>]<EOL>


if not self._restricted:<EOL><INDENT>return <NUM_LIT:0.0><EOL><DEDENT>ldet = slogdet(sum(self._XTQDiQTX) / self.scale)<EOL>if ldet[<NUM_LIT:0>] != <NUM_LIT:1.0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>return ldet[<NUM_LIT:1>]<EOL>


assert self._optimal["<STR_LIT>"]<EOL>n = len(self._y)<EOL>lml = -self._df * log2pi - self._df - n * log(self.scale)<EOL>lml -= sum(npsum(log(D)) for D in self._D)<EOL>return lml / <NUM_LIT:2><EOL>

Log of the marginal likelihood for optimal scale.

Implementation for unrestricted LML::

Returns
-------
lml : float
    Log of the marginal likelihood.


s = self.scale<EOL>D = self._D<EOL>n = len(self._y)<EOL>lml = -self._df * log2pi - n * log(s)<EOL>lml -= sum(npsum(log(d)) for d in D)<EOL>d = (mTQ - yTQ for (mTQ, yTQ) in zip(self._mTQ, self._yTQ))<EOL>lml -= sum((i / j) @ i for (i, j) in zip(d, D)) / s<EOL>return lml / <NUM_LIT:2><EOL>

Log of the marginal likelihood for arbitrary scale.

Returns
-------
lml : float
    Log of the marginal likelihood.


if not self._restricted:<EOL><INDENT>return self.nsamples<EOL><DEDENT>return self.nsamples - self._X["<STR_LIT>"].shape[<NUM_LIT:1>]<EOL>


def __init__(self, Y, A, X, G, rank=<NUM_LIT:1>, restricted=False):

from numpy_sugar import is_all_finite<EOL>Y = asfortranarray(Y, float)<EOL>yrank = matrix_rank(Y)<EOL>if Y.shape[<NUM_LIT:1>] > yrank:<EOL><INDENT>warnings.warn(<EOL>f"<STR_LIT>"<EOL>+ "<STR_LIT>",<EOL>UserWarning,<EOL>)<EOL><DEDENT>A = asarray(A, float)<EOL>X = asarray(X, float)<EOL>Xrank = matrix_rank(X)<EOL>if X.shape[<NUM_LIT:1>] > Xrank:<EOL><INDENT>warnings.warn(<EOL>f"<STR_LIT>"<EOL>+ "<STR_LIT>",<EOL>UserWarning,<EOL>)<EOL><DEDENT>G = asarray(G, float).copy()<EOL>self._G_norm = max(G.min(), G.max())<EOL>G /= self._G_norm<EOL>if not is_all_finite(Y):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not is_all_finite(A):<EOL><INDENT>msg = "<STR_LIT>"<EOL>raise ValueError(msg)<EOL><DEDENT>if not is_all_finite(X):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not is_all_finite(G):<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>self._Y = Y<EOL>self._cov = Kron2SumCov(G, Y.shape[<NUM_LIT:1>], rank)<EOL>self._cov.listen(self._parameters_update)<EOL>self._mean = KronMean(A, X)<EOL>self._cache = {"<STR_LIT>": None}<EOL>self._restricted = restricted<EOL>composite = [("<STR_LIT>", self._cov.C0), ("<STR_LIT>", self._cov.C1)]<EOL>Function.__init__(self, "<STR_LIT>", composite=composite)<EOL>nparams = self._mean.nparams + self._cov.nparams<EOL>if nparams > Y.size:<EOL><INDENT>msg = "<STR_LIT>"<EOL>msg += "<STR_LIT>"<EOL>warnings.warn(msg, UserWarning)<EOL><DEDENT>

Constructor.

Parameters
----------
Y : (n, p) array_like
    Outcome matrix.
A : (n, n) array_like
    Trait-by-trait design matrix.
X : (n, c) array_like
    Covariates design matrix.
G : (n, r) array_like
    Matrix G from the GGᵀ term.
rank : optional, int
    Maximum rank of matrix C₀. Defaults to ``1``.


H = self._terms["<STR_LIT:H>"]<EOL>return inv(H)<EOL>

Estimates the covariance-matrix of the optimal beta.

Returns
-------
beta-covariance : ndarray
    (MᵀK⁻¹M)⁻¹.

References
----------
.. Rencher, A. C., & Schaalje, G. B. (2008). Linear models in statistics. John
   Wiley & Sons.


terms = self._terms<EOL>return KronFastScanner(self._Y, self._mean.A, self._mean.X, self._cov.Ge, terms)<EOL>

Return :class:`.FastScanner` for association scan.

Returns
-------
:class:`.FastScanner`
    Instance of a class designed to perform very fast association scan.


return self._mean.A<EOL>

A from the equation 𝐦 = (A ⊗ X) vec(B).

Returns
-------
A : ndarray
    A.


self._terms<EOL>return asarray(self._mean.B, float)<EOL>

Fixed-effect sizes B from 𝐦 = (A ⊗ X) vec(B).

Returns
-------
fixed-effects : ndarray
    B from 𝐦 = (A ⊗ X) vec(B).


return vec(self.B)<EOL>

Fixed-effect sizes 𝛃 = vec(B).

Returns
-------
fixed-effects : ndarray
    𝛃 from 𝛃 = vec(B).


return self._cov.C0.value() / (self._G_norm ** <NUM_LIT:2>)<EOL>

C₀ from equation K = C₀ ⊗ GGᵀ + C₁ ⊗ I.

Returns
-------
C0 : ndarray
    C₀.


return self._cov.C1.value()<EOL>

C₁ from equation K = C₀ ⊗ GGᵀ + C₁ ⊗ I.

Returns
-------
C1 : ndarray
    C₁.


self._terms<EOL>return self._mean.value()<EOL>

Mean 𝐦 = (A ⊗ X) vec(B).

Returns
-------
mean : ndarray
    𝐦.


return self._cov.value()<EOL>

Covariance K = C₀ ⊗ GGᵀ + C₁ ⊗ I.

Returns
-------
covariance : ndarray
    K.


return self._mean.X<EOL>

X from equation M = (A ⊗ X).

Returns
-------
X : ndarray
    X from M = (A ⊗ X).


return self._mean.AX<EOL>

M = (A ⊗ X).

Returns
-------
M : ndarray
    M from M = (A ⊗ X).


return self._Y.shape[<NUM_LIT:0>]<EOL>


return self._Y.shape[<NUM_LIT:1>]<EOL>


return self._mean.X.shape[<NUM_LIT:1>]<EOL>


return self.lml()<EOL>


return self._lml_gradient()<EOL>

Gradient of the log of the marginal likelihood.


terms = self._terms<EOL>yKiy = terms["<STR_LIT>"]<EOL>mKiy = terms["<STR_LIT>"]<EOL>mKim = terms["<STR_LIT>"]<EOL>lml = -self._df * log2pi + self._logdet_MM - self._logdetK<EOL>lml -= self._logdetH<EOL>lml += -yKiy - mKim + <NUM_LIT:2> * mKiy<EOL>return lml / <NUM_LIT:2><EOL>

Log of the marginal likelihood.

Let 𝐲 = vec(Y), M = A⊗X, and H = MᵀK⁻¹M. The restricted log of the marginal
likelihood is given by [R07]_::

    2⋅log(p(𝐲)) = -(n⋅p - c⋅p) log(2π) + log(｜MᵀM｜) - log(｜K｜) - log(｜H｜)
        - (𝐲-𝐦)ᵀ K⁻¹ (𝐲-𝐦),

where 𝐦 = M𝛃 for 𝛃 = H⁻¹MᵀK⁻¹𝐲.

For implementation purpose, let X = (L₀ ⊗ G) and R = (L₁ ⊗ I)(L₁ ⊗ I)ᵀ.
The covariance can be written as::

    K = XXᵀ + R.

From the Woodbury matrix identity, we have

    𝐲ᵀK⁻¹𝐲 = 𝐲ᵀR⁻¹𝐲 - 𝐲ᵀR⁻¹XZ⁻¹XᵀR⁻¹𝐲,

where Z = I + XᵀR⁻¹X. Note that R⁻¹ = (U₁S₁⁻¹U₁ᵀ) ⊗ I and ::

    XᵀR⁻¹𝐲 = (L₀ᵀW ⊗ Gᵀ)𝐲 = vec(GᵀYWL₀),

where W = U₁S₁⁻¹U₁ᵀ. The term GᵀY can be calculated only once and it will form a
r×p matrix. We similarly have ::

    XᵀR⁻¹M = (L₀ᵀWA) ⊗ (GᵀX),

for which GᵀX is pre-computed.

The log-determinant of the covariance matrix is given by

    log(｜K｜) = log(｜Z｜) - log(｜R⁻¹｜) = log(｜Z｜) - 2·n·log(｜U₁S₁⁻½｜).

The log of the marginal likelihood can be rewritten as::

    2⋅log(p(𝐲)) = -(n⋅p - c⋅p) log(2π) + log(｜MᵀM｜)
    - log(｜Z｜) + 2·n·log(｜U₁S₁⁻½｜)
    - log(｜MᵀR⁻¹M - MᵀR⁻¹XZ⁻¹XᵀR⁻¹M｜)
    - 𝐲ᵀR⁻¹𝐲 + (𝐲ᵀR⁻¹X)Z⁻¹(XᵀR⁻¹𝐲)
    - 𝐦ᵀR⁻¹𝐦 + (𝐦ᵀR⁻¹X)Z⁻¹(XᵀR⁻¹𝐦)
    + 2𝐲ᵀR⁻¹𝐦 - 2(𝐲ᵀR⁻¹X)Z⁻¹(XᵀR⁻¹𝐦).

Returns
-------
lml : float
    Log of the marginal likelihood.

References
----------
.. [R07] LaMotte, L. R. (2007). A direct derivation of the REML likelihood
   function. Statistical Papers, 48(2), 321-327.


Maximise the marginal likelihood.

Parameters
----------
verbose : bool, optional
    ``True`` for progress output; ``False`` otherwise.
    Defaults to ``True``.


from scipy.linalg import cho_solve<EOL>terms = self._terms<EOL>dC0 = self._cov.C0.gradient()["<STR_LIT>"]<EOL>dC1 = self._cov.C1.gradient()["<STR_LIT>"]<EOL>b = terms["<STR_LIT:b>"]<EOL>W = terms["<STR_LIT>"]<EOL>Lh = terms["<STR_LIT>"]<EOL>Lz = terms["<STR_LIT>"]<EOL>WA = terms["<STR_LIT>"]<EOL>WL0 = terms["<STR_LIT>"]<EOL>YW = terms["<STR_LIT>"]<EOL>MRiM = terms["<STR_LIT>"]<EOL>MRiy = terms["<STR_LIT>"]<EOL>XRiM = terms["<STR_LIT>"]<EOL>XRiy = terms["<STR_LIT>"]<EOL>ZiXRiM = terms["<STR_LIT>"]<EOL>ZiXRiy = terms["<STR_LIT>"]<EOL>WdC0 = _mdot(W, dC0)<EOL>WdC1 = _mdot(W, dC1)<EOL>AWdC0 = _mdot(WA.T, dC0)<EOL>AWdC1 = _mdot(WA.T, dC1)<EOL>MR0M = _mkron(_mdot(AWdC0, WA), self._XGGX)<EOL>MR1M = _mkron(_mdot(AWdC1, WA), self._XX)<EOL>MR0X = _mkron(_mdot(AWdC0, WL0), self._XGGG)<EOL>MR1X = _mkron(_mdot(AWdC1, WL0), self._GX.T)<EOL>MR0y = vec(_mdot(self._XGGY, _mdot(WdC0, WA)))<EOL>MR1y = vec(_mdot(self._XY, WdC1, WA))<EOL>XR0X = _mkron(_mdot(WL0.T, dC0, WL0), self._GGGG)<EOL>XR1X = _mkron(_mdot(WL0.T, dC1, WL0), self._GG)<EOL>XR0y = vec(_mdot(self._GGGY, WdC0, WL0))<EOL>XR1y = vec(_mdot(self._GY, WdC1, WL0))<EOL>yR0y = vec(_mdot(self._GY, WdC0)).T @ vec(self._GY @ W)<EOL>yR1y = (YW.T * _mdot(self._Y, WdC1).T).T.sum(axis=(<NUM_LIT:0>, <NUM_LIT:1>))<EOL>ZiXR0X = cho_solve(Lz, XR0X)<EOL>ZiXR1X = cho_solve(Lz, XR1X)<EOL>ZiXR0y = cho_solve(Lz, XR0y)<EOL>ZiXR1y = cho_solve(Lz, XR1y)<EOL>MK0y = MR0y - _mdot(XRiM.T, ZiXR0y) - _mdot(MR0X, ZiXRiy)<EOL>MK0y += _mdot(XRiM.T, ZiXR0X, ZiXRiy)<EOL>MK1y = MR1y - _mdot(XRiM.T, ZiXR1y) - _mdot(MR1X, ZiXRiy)<EOL>MK1y += _mdot(XRiM.T, ZiXR1X, ZiXRiy)<EOL>yK0y = yR0y - <NUM_LIT:2> * XR0y.T @ ZiXRiy + ZiXRiy.T @ _mdot(XR0X, ZiXRiy)<EOL>yK1y = yR1y - <NUM_LIT:2> * XR1y.T @ ZiXRiy + ZiXRiy.T @ _mdot(XR1X, ZiXRiy)<EOL>MR0XZiXRiM = _mdot(MR0X, ZiXRiM)<EOL>MK0M = MR0M - MR0XZiXRiM - MR0XZiXRiM.transpose([<NUM_LIT:1>, <NUM_LIT:0>, <NUM_LIT:2>])<EOL>MK0M += _mdot(ZiXRiM.T, XR0X, ZiXRiM)<EOL>MR1XZiXRiM = _mdot(MR1X, ZiXRiM)<EOL>MK1M = MR1M - MR1XZiXRiM - MR1XZiXRiM.transpose([<NUM_LIT:1>, <NUM_LIT:0>, <NUM_LIT:2>])<EOL>MK1M += _mdot(ZiXRiM.T, XR1X, ZiXRiM)<EOL>MK0m = _mdot(MK0M, b)<EOL>mK0y = b.T @ MK0y<EOL>mK0m = b.T @ MK0m<EOL>MK1m = _mdot(MK1M, b)<EOL>mK1y = b.T @ MK1y<EOL>mK1m = b.T @ MK1m<EOL>XRim = XRiM @ b<EOL>MRim = MRiM @ b<EOL>db = {"<STR_LIT>": cho_solve(Lh, MK0m - MK0y), "<STR_LIT>": cho_solve(Lh, MK1m - MK1y)}<EOL>grad = {<EOL>"<STR_LIT>": -trace(WdC0) * self._trGG + trace(ZiXR0X),<EOL>"<STR_LIT>": -trace(WdC1) * self.nsamples + trace(ZiXR1X),<EOL>}<EOL>if self._restricted:<EOL><INDENT>grad["<STR_LIT>"] += cho_solve(Lh, MK0M).diagonal().sum(<NUM_LIT:1>)<EOL>grad["<STR_LIT>"] += cho_solve(Lh, MK1M).diagonal().sum(<NUM_LIT:1>)<EOL><DEDENT>mKiM = MRim.T - XRim.T @ ZiXRiM<EOL>yKiM = MRiy.T - XRiy.T @ ZiXRiM<EOL>grad["<STR_LIT>"] += yK0y - <NUM_LIT:2> * mK0y + mK0m - <NUM_LIT:2> * _mdot(mKiM, db["<STR_LIT>"])<EOL>grad["<STR_LIT>"] += <NUM_LIT:2> * _mdot(yKiM, db["<STR_LIT>"])<EOL>grad["<STR_LIT>"] += yK1y - <NUM_LIT:2> * mK1y + mK1m - <NUM_LIT:2> * _mdot(mKiM, db["<STR_LIT>"])<EOL>grad["<STR_LIT>"] += <NUM_LIT:2> * _mdot(yKiM, db["<STR_LIT>"])<EOL>grad["<STR_LIT>"] /= <NUM_LIT:2><EOL>grad["<STR_LIT>"] /= <NUM_LIT:2><EOL>return grad<EOL>

Gradient of the log of the marginal likelihood.

Let 𝐲 = vec(Y), 𝕂 = K⁻¹∂(K)K⁻¹, and H = MᵀK⁻¹M. The gradient is given by::

    2⋅∂log(p(𝐲)) = -tr(K⁻¹∂K) - tr(H⁻¹∂H) + 𝐲ᵀ𝕂𝐲 - 𝐦ᵀ𝕂(2⋅𝐲-𝐦)
        - 2⋅(𝐦-𝐲)ᵀK⁻¹∂(𝐦).

Observe that

    ∂𝛃 = -H⁻¹(∂H)𝛃 - H⁻¹Mᵀ𝕂𝐲 and ∂H = -Mᵀ𝕂M.

Let Z = I + XᵀR⁻¹X and 𝓡 = R⁻¹(∂K)R⁻¹. We use Woodbury matrix identity to
write ::

    𝐲ᵀ𝕂𝐲 = 𝐲ᵀ𝓡𝐲 - 2(𝐲ᵀ𝓡X)Z⁻¹(XᵀR⁻¹𝐲) + (𝐲ᵀR⁻¹X)Z⁻¹(Xᵀ𝓡X)Z⁻¹(XᵀR⁻¹𝐲)
    Mᵀ𝕂M = Mᵀ𝓡M - 2(Mᵀ𝓡X)Z⁻¹(XᵀR⁻¹M) + (MᵀR⁻¹X)Z⁻¹(Xᵀ𝓡X)Z⁻¹(XᵀR⁻¹M)
    Mᵀ𝕂𝐲 = Mᵀ𝓡𝐲 - (MᵀR⁻¹X)Z⁻¹(Xᵀ𝓡𝐲) - (Mᵀ𝓡X)Z⁻¹(XᵀR⁻¹𝐲)
          + (MᵀR⁻¹X)Z⁻¹(Xᵀ𝓡X)Z⁻¹(XᵀR⁻¹𝐲)
    H⁻¹   = MᵀR⁻¹M - (MᵀR⁻¹X)Z⁻¹(XᵀR⁻¹M),

where we have used parentheses to separate expressions
that we will compute separately. For example, we have ::

    𝐲ᵀ𝓡𝐲 = 𝐲ᵀ(U₁S₁⁻¹U₁ᵀ ⊗ I)(∂C₀ ⊗ GGᵀ)(U₁S₁⁻¹U₁ᵀ ⊗ I)𝐲
          = 𝐲ᵀ(U₁S₁⁻¹U₁ᵀ∂C₀ ⊗ G)(U₁S₁⁻¹U₁ᵀ ⊗ Gᵀ)𝐲
          = vec(GᵀYU₁S₁⁻¹U₁ᵀ∂C₀)ᵀvec(GᵀYU₁S₁⁻¹U₁ᵀ),

when the derivative is over the parameters of C₀. Otherwise, we have

    𝐲ᵀ𝓡𝐲 = vec(YU₁S₁⁻¹U₁ᵀ∂C₁)ᵀvec(YU₁S₁⁻¹U₁ᵀ).

The above equations can be more compactly written as

    𝐲ᵀ𝓡𝐲 = vec(EᵢᵀYW∂Cᵢ)ᵀvec(EᵢᵀYW),

where W = U₁S₁⁻¹U₁ᵀ, E₀ = G, and E₁ = I. We will now just state the results for
the other instances of the aBc form, which follow similar derivations::

    Xᵀ𝓡X = (L₀ᵀW∂CᵢWL₀) ⊗ (GᵀEᵢEᵢᵀG)
    Mᵀ𝓡y = (AᵀW∂Cᵢ⊗XᵀEᵢ)vec(EᵢᵀYW) = vec(XᵀEᵢEᵢᵀYW∂CᵢWA)
    Mᵀ𝓡X = AᵀW∂CᵢWL₀ ⊗ XᵀEᵢEᵢᵀG
    Mᵀ𝓡M = AᵀW∂CᵢWA ⊗ XᵀEᵢEᵢᵀX
    Xᵀ𝓡𝐲 = GᵀEᵢEᵢᵀYW∂CᵢWL₀

From Woodbury matrix identity and Kronecker product properties we have ::

    tr(K⁻¹∂K) = tr[W∂Cᵢ]tr[EᵢEᵢᵀ] - tr[Z⁻¹(Xᵀ𝓡X)]
    tr(H⁻¹∂H) = - tr[(MᵀR⁻¹M)(Mᵀ𝕂M)] + tr[(MᵀR⁻¹X)Z⁻¹(XᵀR⁻¹M)(Mᵀ𝕂M)]

Note also that ::

    ∂𝛃 = H⁻¹Mᵀ𝕂M𝛃 - H⁻¹Mᵀ𝕂𝐲.

Returns
-------
C0.Lu : ndarray
    Gradient of the log of the marginal likelihood over C₀ parameters.
C1.Lu : ndarray
    Gradient of the log of the marginal likelihood over C₁ parameters.


from numpy_sugar.linalg import ddot, sum2diag<EOL>if self._L_cache is not None:<EOL><INDENT>return self._L_cache<EOL><DEDENT>s = self._cov["<STR_LIT>"]<EOL>d = self._cov["<STR_LIT>"]<EOL>Q = self._cov["<STR_LIT>"][<NUM_LIT:0>][<NUM_LIT:0>]<EOL>S = self._cov["<STR_LIT>"][<NUM_LIT:1>]<EOL>ddot(self.A * self._site.tau, Q, left=True, out=self._NxR)<EOL>B = dot(Q.T, self._NxR, out=self._RxR)<EOL>B *= <NUM_LIT:1> - d<EOL>sum2diag(B, <NUM_LIT:1.0> / S / s, out=B)<EOL>self._L_cache = _cho_factor(B)<EOL>return self._L_cache<EOL>

r"""Cholesky decomposition of :math:`\mathrm B`.

        .. math::

            \mathrm B = \mathrm Q^{\intercal}\tilde{\mathrm{T}}\mathrm Q
                + \mathrm{S}^{-1}


if self._mean is None or self._cov is None:<EOL><INDENT>return<EOL><DEDENT>Q = self._cov["<STR_LIT>"][<NUM_LIT:0>][<NUM_LIT:0>]<EOL>S = self._cov["<STR_LIT>"][<NUM_LIT:1>]<EOL>if S.size > <NUM_LIT:0>:<EOL><INDENT>self.tau[:] = <NUM_LIT:1> / npsum((Q * sqrt(S)) ** <NUM_LIT:2>, axis=<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>self.tau[:] = <NUM_LIT:0.0><EOL><DEDENT>self.eta[:] = self._mean<EOL>self.eta[:] *= self.tau<EOL>

r"""Initialize the mean and covariance of the posterior.

        Given that :math:`\tilde{\mathrm T}` is a matrix of zeros right before
        the first EP iteration, we have

        .. math::

            \boldsymbol\mu = \mathrm K^{-1} \mathbf m ~\text{ and }~
            \Sigma = \mathrm K

        as the initial posterior mean and covariance.


from scipy.linalg import cho_factor<EOL>from numpy_sugar.linalg import ddot, sum2diag<EOL>if self._L_cache is not None:<EOL><INDENT>return self._L_cache<EOL><DEDENT>Q = self._cov["<STR_LIT>"][<NUM_LIT:0>][<NUM_LIT:0>]<EOL>S = self._cov["<STR_LIT>"][<NUM_LIT:1>]<EOL>B = dot(Q.T, ddot(self._site.tau, Q, left=True))<EOL>sum2diag(B, <NUM_LIT:1.0> / S, out=B)<EOL>self._L_cache = cho_factor(B, lower=True)[<NUM_LIT:0>]<EOL>return self._L_cache<EOL>


from numpy_sugar.linalg import rsolve as _rsolve<EOL>try:<EOL><INDENT>beta = _rsolve(A, y)<EOL><DEDENT>except LinAlgError:<EOL><INDENT>msg = "<STR_LIT>"<EOL>msg += "<STR_LIT>"<EOL>warnings.warn(msg, RuntimeWarning)<EOL>beta = zeros(A.shape[<NUM_LIT:0>])<EOL><DEDENT>return beta<EOL>


from numpy_sugar import epsilon<EOL>n = _norm(A00, A01)<EOL>u0 = A00 - n<EOL>u1 = A01<EOL>nu = _norm(u0, u1)<EOL>with errstate(invalid="<STR_LIT:ignore>", divide="<STR_LIT:ignore>"):<EOL><INDENT>v0 = nan_to_num(u0 / nu)<EOL>v1 = nan_to_num(u1 / nu)<EOL><DEDENT>B00 = <NUM_LIT:1> - <NUM_LIT:2> * v0 * v0<EOL>B01 = <NUM_LIT:0> - <NUM_LIT:2> * v0 * v1<EOL>B11 = <NUM_LIT:1> - <NUM_LIT:2> * v1 * v1<EOL>D00 = B00 * A00 + B01 * A01<EOL>D01 = B00 * A01 + B01 * A11<EOL>D11 = B01 * A01 + B11 * A11<EOL>b0 = y0 - <NUM_LIT:2> * y0 * v0 * v0 - <NUM_LIT:2> * y1 * v0 * v1<EOL>b1 = y1 - <NUM_LIT:2> * y0 * v1 * v0 - <NUM_LIT:2> * y1 * v1 * v1<EOL>n = _norm(D00, D01)<EOL>u0 = D00 - n<EOL>u1 = D01<EOL>nu = _norm(u0, u1)<EOL>with errstate(invalid="<STR_LIT:ignore>", divide="<STR_LIT:ignore>"):<EOL><INDENT>v0 = nan_to_num(u0 / nu)<EOL>v1 = nan_to_num(u1 / nu)<EOL><DEDENT>E00 = <NUM_LIT:1> - <NUM_LIT:2> * v0 * v0<EOL>E01 = <NUM_LIT:0> - <NUM_LIT:2> * v0 * v1<EOL>E11 = <NUM_LIT:1> - <NUM_LIT:2> * v1 * v1<EOL>F00 = E00 * D00 + E01 * D01<EOL>F01 = E01 * D11<EOL>F11 = E11 * D11<EOL>F11 = (npy_abs(F11) > epsilon.small) * F11<EOL>with errstate(divide="<STR_LIT:ignore>", invalid="<STR_LIT:ignore>"):<EOL><INDENT>Fi00 = nan_to_num(F00 / F00 / F00)<EOL>Fi11 = nan_to_num(F11 / F11 / F11)<EOL>Fi10 = nan_to_num(-(F01 / F00) * Fi11)<EOL><DEDENT>c0 = Fi00 * b0<EOL>c1 = Fi10 * b0 + Fi11 * b1<EOL>x0 = E00 * c0 + E01 * c1<EOL>x1 = E01 * c0 + E11 * c1<EOL>return array([x0, x1])<EOL>

Solver for the linear equations of two variables and equations only.

It uses Householder reductions to solve A𝐱 = 𝐲 in a robust manner.

Parameters
----------
A : array_like
    Coefficient matrix.
y : array_like
    Ordinate values.

Returns
-------
ndarray
    Solution 𝐱.


Q0 = empty((n, <NUM_LIT:0>))<EOL>Q1 = eye(n)<EOL>S0 = empty(<NUM_LIT:0>)<EOL>return ((Q0, Q1), S0)<EOL>


def multivariate_normal(random, mean, cov):

from numpy.linalg import cholesky<EOL>L = cholesky(cov)<EOL>return L @ random.randn(L.shape[<NUM_LIT:0>]) + mean<EOL>

Draw random samples from a multivariate normal distribution.

Parameters
----------
random : np.random.RandomState instance
    Random state.
mean : array_like
    Mean of the n-dimensional distribution.
cov : array_like
    Covariance matrix of the distribution. It must be symmetric and
    positive-definite for proper sampling.

Returns
-------
out : ndarray
    The drawn sample.


from numpy_sugar.linalg import ddot, economic_qs, sum2diag<EOL>y = self.eta / self.tau<EOL>if self._QS is None:<EOL><INDENT>K = eye(y.shape[<NUM_LIT:0>]) / self.tau<EOL><DEDENT>else:<EOL><INDENT>Q0 = self._QS[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>S0 = self._QS[<NUM_LIT:1>]<EOL>K = dot(ddot(Q0, self.v0 * S0), Q0.T)<EOL>K = sum2diag(K, <NUM_LIT:1> / self.tau)<EOL><DEDENT>return FastScanner(y, self._X, economic_qs(K), self.v1)<EOL>

r"""Return :class:`glimix_core.lmm.FastScanner` for the current
        delta.


from numpy_sugar.linalg import ddot, sum2diag<EOL>if self._cache["<STR_LIT:value>"] is not None:<EOL><INDENT>return self._cache["<STR_LIT:value>"]<EOL><DEDENT>scale = exp(self.logscale)<EOL>delta = <NUM_LIT:1> / (<NUM_LIT:1> + exp(-self.logitdelta))<EOL>v0 = scale * (<NUM_LIT:1> - delta)<EOL>v1 = scale * delta<EOL>mu = self.eta / self.tau<EOL>n = len(mu)<EOL>if self._QS is None:<EOL><INDENT>K = zeros((n, n))<EOL><DEDENT>else:<EOL><INDENT>Q0 = self._QS[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>S0 = self._QS[<NUM_LIT:1>]<EOL>K = dot(ddot(Q0, S0), Q0.T)<EOL><DEDENT>A = sum2diag(sum2diag(v0 * K, v1), <NUM_LIT:1> / self.tau)<EOL>m = mu - self.mean()<EOL>v = -n * log(<NUM_LIT:2> * pi)<EOL>v -= slogdet(A)[<NUM_LIT:1>]<EOL>v -= dot(m, solve(A, m))<EOL>self._cache["<STR_LIT:value>"] = v / <NUM_LIT:2><EOL>return self._cache["<STR_LIT:value>"]<EOL>

r"""Log of the marginal likelihood.

        Formally,

        .. math::

            - \frac{n}{2}\log{2\pi} - \frac{1}{2} \log{\left|
                v_0 \mathrm K + v_1 \mathrm I + \tilde{\Sigma} \right|}
                    - \frac{1}{2}
                    \left(\tilde{\boldsymbol\mu} -
                    \mathrm X\boldsymbol\beta\right)^{\intercal}
                    \left( v_0 \mathrm K + v_1 \mathrm I +
                    \tilde{\Sigma} \right)^{-1}
                    \left(\tilde{\boldsymbol\mu} -
                    \mathrm X\boldsymbol\beta\right)

        Returns
        -------
        float
            :math:`\log{p(\tilde{\boldsymbol\mu})}`


return asarray(self._variables.get("<STR_LIT>").value, float)<EOL>

r"""Fixed-effect sizes.

        Returns
        -------
        :class:`numpy.ndarray`
            :math:`\boldsymbol\beta`.


return copy(self)<EOL>


from numpy_sugar.linalg import ddot, sum2diag<EOL>Q0 = self._QS[<NUM_LIT:0>][<NUM_LIT:0>]<EOL>S0 = self._QS[<NUM_LIT:1>]<EOL>return sum2diag(dot(ddot(Q0, self.v0 * S0), Q0.T), self.v1)<EOL>

r"""Covariance of the prior.

        Returns
        -------
        :class:`numpy.ndarray`
            :math:`v_0 \mathrm K + v_1 \mathrm I`.


return <NUM_LIT:1> / (<NUM_LIT:1> + exp(-self.logitdelta))<EOL>

r"""Get or set the ratio of variance between ``K`` and ``I``.

        Returns
        -------
        float
            :math:`\delta`.


Function._fix(self, _to_internal_name(var_name))<EOL>

r"""Prevent a variable to be adjusted.

        Parameters
        ----------
        var_name : str
            Variable name.


self._verbose = verbose<EOL>self._maximize(verbose=verbose, factr=factr, pgtol=pgtol)<EOL>self._verbose = False<EOL>

r"""Maximise the marginal likelihood.

        Parameters
        ----------
        verbose : bool
            ``True`` for progress output; ``False`` otherwise.
            Defaults to ``True``.
        factr : float, optional
            The iteration stops when
            ``(f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= factr * eps``, where ``eps`` is
            the machine precision.
        pgtol : float, optional
            The iteration will stop when ``max{|proj g_i | i = 1, ..., n} <= pgtol``
            where ``pg_i`` is the i-th component of the projected gradient.

        Notes
        -----
        Please, refer to :func:`scipy.optimize.fmin_l_bfgs_b` for further information
        about ``factr`` and ``pgtol``.


return self.value()<EOL>

r"""Log of the marginal likelihood.

        Returns
        -------
        float
            :math:`\log p(\mathbf y)`


from numpy_sugar.linalg import rsolve<EOL>Sigma = self.posteriori_covariance()<EOL>eta = self._ep._posterior.eta<EOL>return dot(Sigma, eta + rsolve(GLMM.covariance(self), self.mean()))<EOL>

r""" Mean of the estimated posteriori.

        This is also the maximum a posteriori estimation of the latent variable.


K = GLMM.covariance(self)<EOL>tau = self._ep._posterior.tau<EOL>return pinv(pinv(K) + diag(<NUM_LIT:1> / tau))<EOL>

r""" Covariance of the estimated posteriori.


return dot(self._X, self.beta)<EOL>

r"""Mean of the prior.

        Returns
        -------
        :class:`numpy.ndarray`
            :math:`\mathrm X\boldsymbol\beta`.


return exp(self.logscale)<EOL>

r"""Get or set the overall variance.

        Returns
        -------
        float
            :math:`s`.


Function._unfix(self, _to_internal_name(var_name))<EOL>

r"""Let a variable be adjusted.

        Parameters
        ----------
        var_name : str
            Variable name.


return self.scale * (<NUM_LIT:1> - self.delta)<EOL>

r"""First variance.

        Returns
        -------
        float
            :math:`v_0 = s (1 - \delta)`


return self.scale * self.delta<EOL>

r"""Second variance.

        Returns
        -------
        float
            :math:`v_1 = s \delta`


self._update_approx()<EOL>g = self._ep.lml_derivatives(self._X)<EOL>ed = exp(-self.logitdelta)<EOL>es = exp(self.logscale)<EOL>grad = dict()<EOL>grad["<STR_LIT>"] = g["<STR_LIT>"] * (ed / (<NUM_LIT:1> + ed)) / (<NUM_LIT:1> + ed)<EOL>grad["<STR_LIT>"] = g["<STR_LIT>"] * es<EOL>grad["<STR_LIT>"] = g["<STR_LIT>"]<EOL>return grad<EOL>

r"""Gradient of the log of the marginal likelihood.

        Returns
        -------
        dict
            Map between variables to their gradient values.


self._maximize(verbose=verbose, factr=factr, pgtol=pgtol)<EOL>


return self.value()<EOL>


return "<STR_LIT>"<EOL>


return self._outcome<EOL>


return x<EOL>


return len(self.outcome)<EOL>


return self._link.inv(x)<EOL>


return self._ntrials<EOL>


return self._nsuccesses<EOL>

r"""Get or set an array of successful trials.


return self._link.inv(x)<EOL>

r"""Mean of the number of successful trials.


return len(self.nsuccesses)<EOL>


return self._noccurrences<EOL>

r"""Get or set an array of number of occurrences.


return len(self.noccurrences)<EOL>


if not self.data_loaded:<EOL><INDENT>dim_size = self.z_variable_info[key]['<STR_LIT>']<EOL>dim_size = dim_size[<NUM_LIT:0>]<EOL>if dim_size == <NUM_LIT:0>:<EOL><INDENT>dim_size += <NUM_LIT:1><EOL><DEDENT>rec_num = self.z_variable_info[key]['<STR_LIT>']<EOL>status, data = fortran_cdf.get_z_var(self.fname, key, dim_size, rec_num)<EOL>if status == <NUM_LIT:0>:<EOL><INDENT>if dim_size == <NUM_LIT:1>:<EOL><INDENT>data = data[<NUM_LIT:0>, :]<EOL><DEDENT>return data<EOL><DEDENT>else:<EOL><INDENT>raise IOError(fortran_cdf.statusreporter(status))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return chameleon(self.fname, key, self.data[key],<EOL>self.meta[key],<EOL>self.z_variable_info[key])<EOL><DEDENT>


name = copy.deepcopy(self.fname)<EOL>stats = fortran_cdf.inquire(name)<EOL>status = stats[<NUM_LIT:0>]<EOL>if status == <NUM_LIT:0>:<EOL><INDENT>self._num_dims = stats[<NUM_LIT:1>]<EOL>self._dim_sizes = stats[<NUM_LIT:2>]<EOL>self._encoding = stats[<NUM_LIT:3>]<EOL>self._majority = stats[<NUM_LIT:4>]<EOL>self._max_rec = stats[<NUM_LIT:5>]<EOL>self._num_r_vars = stats[<NUM_LIT:6>]<EOL>self._num_z_vars = stats[<NUM_LIT:7>]<EOL>self._num_attrs = stats[<NUM_LIT:8>]<EOL><DEDENT>else:<EOL><INDENT>raise IOError(fortran_cdf.statusreporter(status))<EOL><DEDENT>

Maps to fortran CDF_Inquire.

        Assigns parameters returned by CDF_Inquire
        to pysatCDF instance. Not intended
        for regular direct use by user.


self.z_variable_info = {}<EOL>self.z_variable_names_by_num = {}<EOL>info = fortran_cdf.z_var_all_inquire(self.fname, self._num_z_vars,<EOL>len(self.fname))<EOL>status = info[<NUM_LIT:0>]<EOL>data_types = info[<NUM_LIT:1>]<EOL>num_elems = info[<NUM_LIT:2>]<EOL>rec_varys = info[<NUM_LIT:3>]<EOL>dim_varys = info[<NUM_LIT:4>]<EOL>num_dims = info[<NUM_LIT:5>]<EOL>dim_sizes = info[<NUM_LIT:6>]<EOL>rec_nums = info[<NUM_LIT:7>]<EOL>var_nums = info[<NUM_LIT:8>]<EOL>var_names = info[<NUM_LIT:9>]<EOL>if status == <NUM_LIT:0>:<EOL><INDENT>for i in np.arange(len(data_types)):<EOL><INDENT>out = {}<EOL>out['<STR_LIT>'] = data_types[i]<EOL>out['<STR_LIT>'] = num_elems[i]<EOL>out['<STR_LIT>'] = rec_varys[i]<EOL>out['<STR_LIT>'] = dim_varys[i]<EOL>out['<STR_LIT>'] = num_dims[i]<EOL>out['<STR_LIT>'] = dim_sizes[i, :<NUM_LIT:1>]<EOL>if out['<STR_LIT>'][<NUM_LIT:0>] == <NUM_LIT:0>:<EOL><INDENT>out['<STR_LIT>'][<NUM_LIT:0>] += <NUM_LIT:1><EOL><DEDENT>out['<STR_LIT>'] = rec_nums[i]<EOL>out['<STR_LIT>'] = var_nums[i]<EOL>var_name = '<STR_LIT>'.join(var_names[i].astype('<STR_LIT>'))<EOL>out['<STR_LIT>'] = var_name.rstrip()<EOL>self.z_variable_info[out['<STR_LIT>']] = out<EOL>self.z_variable_names_by_num[out['<STR_LIT>']] = var_name<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise IOError(fortran_cdf.statusreporter(status))<EOL><DEDENT>

Gets all CDF z-variable information, not data though.

        Maps to calls using var_inquire. Gets information on
        data type, number of elements, number of dimensions, etc.


self.data = {}<EOL>file_var_names = self.z_variable_info.keys()<EOL>dim_sizes = []<EOL>rec_nums = []<EOL>data_types = []<EOL>names = []<EOL>for i, name in enumerate(file_var_names):<EOL><INDENT>dim_sizes.extend(self.z_variable_info[name]['<STR_LIT>'])<EOL>rec_nums.append(self.z_variable_info[name]['<STR_LIT>'])<EOL>data_types.append(self.z_variable_info[name]['<STR_LIT>'])<EOL>names.append(name.ljust(<NUM_LIT>))<EOL><DEDENT>dim_sizes = np.array(dim_sizes)<EOL>rec_nums = np.array(rec_nums)<EOL>data_types = np.array(data_types)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_real4)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT:float>'],<EOL>fortran_cdf.get_multi_z_real4)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_real8)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_real8)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_int4)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_int4,<EOL>data_offset=<NUM_LIT:2> ** <NUM_LIT:32>)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_int2)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_int2,<EOL>data_offset=<NUM_LIT:2> ** <NUM_LIT:16>)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_int1)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_int1,<EOL>data_offset=<NUM_LIT:2> ** <NUM_LIT:8>)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_int1)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_real8,<EOL>epoch=True)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, <NUM_LIT:2> * dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_epoch16,<EOL>epoch16=True)<EOL>self._call_multi_fortran_z(names, data_types, rec_nums, dim_sizes,<EOL>self.cdf_data_types['<STR_LIT>'],<EOL>fortran_cdf.get_multi_z_tt2000,<EOL>epoch=True)<EOL>self.data_loaded = True<EOL>

Loads all variables from CDF.

        Note this routine is called automatically
        upon instantiation.