class_docstr: Compute performance metrics over multiple random trials (multivariate
  normal sample instances for a given graph).
  
  The graph is sampled once to obtain a penalty (via the ms_estimator) and
  then sampled and fit for n_trials for each (alpha, n_samples) parameter
  pair.
  
  Parameters
  -----------
  n_features : int (default=50)
      Fixed number of features to test.
  
  n_trials : int (default=100)
      Number of examples to draw to measure P(recovery).
  
  ms_estimator : "Model selection estimator" (default=None)
      An inverse covariance estimator instance. This estimator must be able
      to select a penalization parameter that can be accessed via the
      instance variable .lam_.
  
  mc_estimator : "Monte Carlo trial estimator" (default=None)
      An inverse covariance estimator instance. Estimator to use on each
      instance after selecting a penalty lambda. The penalty parameter 'lam'
      will be overriden by with penalty selected by ms_estimator.
      If None, this will use QuicGraphicalLasso.
  
  graph :  An instance of a class with the method .create(n_features, alpha)
      that returns (cov, prec, adj).
      graph.create() will be used to draw a new graph instance in each trial.
      default: ErdosRenyiGraph()
  
  n_samples_grid : int (default=10) or array of floats
      Grid points for choosing number of samples.
      If integer, defines a linear grid (5, 200)
      Else uses array as grid.
  
  alpha_grid : int (default=5) or array of floats
      Grid points used in making new graphs via graph.create().
      If integer, defines a logarithmic grid (0.14, 0.4)
      Else, uses array as grid.
  
  metrics : dict of functions: scalar = func(prec, prec_estimate)
      The key for each function will be used for reporting results.
      default: {'frobenius': error_fro}
  
  verbose : bool (default=False)
      Print out progress information.
  
  n_jobs: int (optional)
      number of jobs to run in parallel (default 1).
  
  sc: sparkContext (optional)
      If a sparkContext object is provided, n_jobs will be ignore and the
      work will be parallelized via spark.
  
  seed : np.random.RandomState starting seed. (default=2)
  
  
  Attributes
  ----------
  grid_ : array of size (n_samples_grid, ) or n_samples_grid
      Array of n_samples / n_features ratios.
  
  alphas_ : array of size (alpha_grid, ) or alpha_grid
      Array of alphas used to generate graphs.
  
  precision_nnz_ : array of size (len(alphas_), )
      The sparsity of each test graph precision.
  
  results_ : dict of matrices of size (len(alphas_), len(grid_))
      Each key corresponds to a function from metrics.
class_docstr_tok: ["Compute","performance","metrics","over","multiple","random","trials","(","multivariate","normal","sample","instances","for","a","given","graph",")",".","The","graph","is","sampled","once","to","obtain","a","penalty","(","via","the","ms_estimator",")","and","then","sampled","and","fit","for","n_trials","for","each","(","alpha",",","n_samples",")","parameter","pair",".","Parameters","--","--","--","--","--","-","n_features",":","int","(","default=50",")","Fixed","number","of","features","to","test",".","n_trials",":","int","(","default=100",")","Number","of","examples","to","draw","to","measure","P","(","recovery",")",".","ms_estimator",":","``","Model","selection","estimator","''","(","default=None",")","An","inverse","covariance","estimator","instance",".","This","estimator","must","be","able","to","select","a","penalization","parameter","that","can","be","accessed","via","the","instance","variable",".lam_",".","mc_estimator",":","``","Monte","Carlo","trial","estimator","''","(","default=None",")","An","inverse","covariance","estimator","instance",".","Estimator","to","use","on","each","instance","after","selecting","a","penalty","lambda",".","The","penalty","parameter","'lam'","will","be","overriden","by","with","penalty","selected","by","ms_estimator",".","If","None",",","this","will","use","QuicGraphicalLasso",".","graph",":","An","instance","of","a","class","with","the","method",".create","(","n_features",",","alpha",")","that","returns","(","cov",",","prec",",","adj",")",".","graph.create","(",")","will","be","used","to","draw","a","new","graph","instance","in","each","trial",".","default",":","ErdosRenyiGraph","(",")","n_samples_grid",":","int","(","default=10",")","or","array","of","floats","Grid","points","for","choosing","number","of","samples",".","If","integer",",","defines","a","linear","grid","(","5",",","200",")","Else","uses","array","as","grid",".","alpha_grid",":","int","(","default=5",")","or","array","of","floats","Grid","points","used","in","making","new","graphs","via","graph.create","(",")",".","If","integer",",","defines","a","logarithmic","grid","(","0.14",",","0.4",")","Else",",","uses","array","as","grid",".","metrics",":","dict","of","functions",":","scalar","=","func","(","prec",",","prec_estimate",")","The","key","for","each","function","will","be","used","for","reporting","results",".","default",":","{","'frobenius","'",":","error_fro","}","verbose",":","bool","(","default=False",")","Print","out","progress","information",".","n_jobs",":","int","(","optional",")","number","of","jobs","to","run","in","parallel","(","default","1",")",".","sc",":","sparkContext","(","optional",")","If","a","sparkContext","object","is","provided",",","n_jobs","will","be","ignore","and","the","work","will","be","parallelized","via","spark",".","seed",":","np.random.RandomState","starting","seed",".","(","default=2",")","Attributes","--","--","--","--","--","grid_",":","array","of","size","(","n_samples_grid",",",")","or","n_samples_grid","Array","of","n_samples","\/","n_features","ratios",".","alphas_",":","array","of","size","(","alpha_grid",",",")","or","alpha_grid","Array","of","alphas","used","to","generate","graphs",".","precision_nnz_",":","array","of","size","(","len","(","alphas_",")",",",")","The","sparsity","of","each","test","graph","precision",".","results_",":","dict","of","matrices","of","size","(","len","(","alphas_",")",",","len","(","grid_",")",")","Each","key","corresponds","to","a","function","from","metrics","."]
code: def fit(self, X=None, y=None):
      n_alphas = len(self.alphas_)
      n_grids = len(self.grid_)
  
      self.precision_nnz_ = []
      self.results_ = {
          k: np.zeros((n_alphas, n_grids)) for k in self.metrics
      }
  
      # build an indexed set (or generator) of grid points
      param_grid = [(a, g) for a in self.alphas_ for g in self.grid_]
      indexed_param_grid = list(zip(range(len(param_grid)), param_grid))
  
      ms_fit = partial(
          _ms_fit,
          estimator=self.ms_estimator,
          n_features=self.n_features,
          graph=self.graph,
          prng=self.prng,
      )
  
      if self.verbose:
          print("Getting parameters via model selection...")
  
      if self.sc is not None:
          ms_results = _spark_map(
              ms_fit, indexed_param_grid, self.sc, self.seed
          )
      else:
          ms_results = _cpu_map(
              ms_fit, indexed_param_grid, self.n_jobs, self.verbose
          )
  
      # ensure results are ordered
      ms_results = sorted(ms_results, key=lambda r: r[0])
  
      # track nnz of graph precision
      self.precision_nnz_ = [
          np.count_nonzero(graph[1].flat)
          for _, (graph, _, _) in ms_results
      ]
  
      # build param grid for mc trials
      # following results in an grid where nn indexes each trial of each
      # param grid:
      #  (0, graph_0, lam_0, n_samples_), (1, graph_0, lam_0, n_samples_0),..
      trial_param_grid = [
          (nn, graph, lam, n_samples)
          for _, (graph, lam, n_samples) in ms_results
          for nn in range(self.n_trials)
      ]
      indexed_trial_param_grid = list(
          zip(range(len(trial_param_grid)), trial_param_grid)
      )
  
      mc_fit = partial(
          _mc_fit, estimator=self.mc_estimator, metrics=self.metrics
      )
  
      if self.verbose:
          print("Fitting MC trials...")
  
      if self.sc is not None:
          mc_results = _spark_map(
              mc_fit,
              indexed_trial_param_grid,
              self.sc,
              self.seed + len(param_grid),
          )
      else:
          mc_results = _cpu_map(
              partial(mc_fit, prng=self.prng),
              indexed_trial_param_grid,
              self.n_jobs,
              self.verbose,
          )
  
      # ensure results are ordered correctly
      mc_results = sorted(mc_results, key=lambda r: r[0])
  
      # strip mc_results
      mc_results = [r for _, r in mc_results]
  
      # reduce
      param_grid_matrix_index = [
          (a, g)
          for a in range(len(self.alphas_))
          for g in range(len(self.grid_))
      ]
      for param_index in range(len(param_grid)):
          trial_start = param_index * self.n_trials
          trials = mc_results[trial_start : trial_start + self.n_trials]
          aidx, gidx = param_grid_matrix_index[param_index]
          for key in self.metrics:
              results_by_key = np.array([t[key] for t in trials])
              self.results_[key][aidx, gidx] = (
                  1.0 * np.sum(results_by_key) / self.n_trials
              )
  
      if self.verbose:
          for key in self.metrics:
              print(
                  "Results for {}: {}".format(
                      key, self.results_[key][aidx, :]
                  )
              )
  
      self.is_fitted = True
      return self
  
file_path: skggm/inverse_covariance/profiling/monte_carlo_profile.py
imports: from __future__ import absolute_import
  import numpy
  from functools import partial
  from sklearn.base import clone
  from joblib import Parallel, delayed
  from .metrics import error_fro
  from .erdos_renyi_graph import ErdosRenyiGraph
  from ..None import QuicGraphicalLasso, QuicGraphicalLassoCV
label_desc: Use image node_id 2 for calling the MonteCarloProfile obj's underlying member method code with example usage: obj.fit(X, y) and returns: self