class documentation

`class GaussDiagonalSampler(GaussSampler):`

View In Hierarchy

Multi-variate normal distribution with zero mean and diagonal covariance matrix.

Provides methods to `sample` from and `update` a multi-variate normal distribution with zero mean and diagonal covariance matrix.

## Arguments to `__init__`

`standard_deviations` (required) define the diagonal of the initial covariance matrix, and consequently also the dimensionality (attribute `dim`) of the normal distribution. If `standard_deviations` is an `int`, np.ones(standard_deviations) is used.

`constant_trace='None'`: 'arithmetic' or 'geometric' or 'aeigen' or 'geigen' (geometric mean of eigenvalues) are available to be constant.

`randn=np.random.randn` is used to generate N(0,1) numbers.

```>>> import cma, numpy as np
>>> s = cma.sampler.GaussDiagonalSampler(np.ones(4))
>>> z = s.sample(1)[0]
>>> assert s.norm([1,0,0,0]) == 1
>>> s.update([[1., 0., 0., 0]], [.9])
>>> assert s.norm([1,0,0,0]) == 1
>>> s.update([[4., 0., 0.,0]], [.5])
>>> g *= 2```

## TODO

o DONE implement CMA_diagonal with samplers

o Clean up CMAEvolutionStrategy attributes related to sampling (like usage of B, C, D, dC, sigma_vec, these are pretty substantial changes). In particular this should become compatible with any StatisticalModelSampler. Plan: keep B, C, D, dC for the time being as output-info attributes, keep sigma_vec (55 appearances) either as constant scaling or as a class. Current favorite: make a class (DONE) .

o combination of sigma_vec and C:
• update sigma_vec with y (this is wrong: use "z")
• rescale y according to the inverse update of sigma_vec (as if y is expressed in the new sigma_vec while C in the old)
• update C with the "new" y.
 Method `__imul__` sm *= factor is a shortcut for sm = sm.__imul__(factor). Method `__init__` declarative init, doesn't need to be executed Method `correlation` return correlation between variables i and j. Method `multiply_C` multiply `self.C` with `factor` updating internal states. Method `norm` compute the Mahalanobis norm that is induced by the statistical model / sample distribution, specifically by covariance matrix C. The expected Mahalanobis norm is about sqrt(dimension). Method `reset` reset distribution while keeping all other parameters Method `sample` return list of i.i.d. samples. Method `to_correlation_matrix` "re-scale" C to a correlation matrix and return the scaling factors as standard deviations. Method `to_linear_transformation` return associated linear transformation. Method `to_linear_transformation_inverse` return associated inverse linear transformation. Method `transform` apply linear transformation C**0.5 to `x`. Method `transform_inverse` apply inverse linear transformation C**-0.5 to `x`. Method `update` update/learn by natural gradient ascent. Instance Variable `C` covariance matrix diagonal Instance Variable `constant_trace` Undocumented Instance Variable `count_tell` Undocumented Instance Variable `dimension` Undocumented Instance Variable `quadratic` Undocumented Instance Variable `randn` Undocumented Property `condition_number` Undocumented Property `correlation_matrix` return correlation matrix of the distribution. Property `covariance_matrix` Undocumented Property `variances` vector of coordinate-wise (marginal) variances

Inherited from `GaussSampler`:

 Method `set_H` set Hessian w.r.t. which to compute the eigen spectrum. Method `set_H_by_f` set Hessian from f at x0. Property `chin` approximation of the expected length when isotropic with variance 1. Property `corr_condition` condition number of the correlation matrix Property `eigenspectrum` return eigen spectrum w.r.t. H like sqrt(H) C sqrt(H) Instance Variable `_left` Undocumented Instance Variable `_right` Undocumented

Inherited from `StatisticalModelSamplerWithZeroMeanBaseClass` (via `GaussSampler`):

 Method `inverse_hessian_scalar_correction` return scalar correction alpha such that X and f fit to f(x) = (x-mean) (alpha * C)**-1 (x-mean) Method `parameters` return `dict` with (default) parameters, e.g., `c1` and `cmu`. Instance Variable `_lam` Undocumented Instance Variable `_mueff` Undocumented Instance Variable `_parameters` Undocumented
def __imul__(self, factor):
overrides `cma.interfaces.StatisticalModelSamplerWithZeroMeanBaseClass.__imul__`

sm *= factor is a shortcut for sm = sm.__imul__(factor).

Multiplies the covariance matrix with `factor`.

def __init__(self, dimension, constant_trace='None', randn=np.random.randn, quadratic=False, **kwargs):
overrides `cma.sampler.GaussSampler.__init__`

declarative init, doesn't need to be executed

def correlation(self, i, j):

return correlation between variables i and j.

def multiply_C(self, factor):

multiply `self.C` with `factor` updating internal states.

`factor` can be a scalar, a vector or a matrix. The vector is used as outer product, i.e. multiply_C(diag(C)**-0.5) generates a correlation matrix.

def norm(self, x):
overrides `cma.interfaces.StatisticalModelSamplerWithZeroMeanBaseClass.norm`

compute the Mahalanobis norm that is induced by the statistical model / sample distribution, specifically by covariance matrix C. The expected Mahalanobis norm is about sqrt(dimension).

## Example

```>>> import cma, numpy as np
>>> sm = cma.sampler.GaussFullSampler(np.ones(10))
>>> x = np.random.randn(10)
>>> d = sm.norm(x)```

`d` is the norm "in" the true sample distribution, sampled points have a typical distance of sqrt(2*sm.dim), where sm.dim is the dimension, and an expected distance of close to dim**0.5 to the sample mean zero. In the example, `d` is the Euclidean distance, because C = I.

def reset(self):

reset distribution while keeping all other parameters

def sample(self, number, same_length=False):
overrides `cma.interfaces.StatisticalModelSamplerWithZeroMeanBaseClass.sample`

return list of i.i.d. samples.

 Parameters number is the number of samples. same_length Undocumented update controls a possibly lazy update of the sampler.
def to_correlation_matrix(self):

"re-scale" C to a correlation matrix and return the scaling factors as standard deviations.

See also: `to_linear_transformation`.

def to_linear_transformation(self, reset=False):
overrides `cma.interfaces.StatisticalModelSamplerWithZeroMeanBaseClass.to_linear_transformation`

return associated linear transformation.

If B = sm.to_linear_transformation() and z ~ N(0, I), then np.dot(B, z) ~ Normal(0, sm.C) and sm.C and B have the same eigenvectors. With `reset=True`, also np.dot(B, sm.sample(1)[0]) obeys the same distribution after the call.

See also: `to_unit_matrix`

def to_linear_transformation_inverse(self, reset=False):
overrides `cma.interfaces.StatisticalModelSamplerWithZeroMeanBaseClass.to_linear_transformation_inverse`

return associated inverse linear transformation.

If B = sm.to_linear_transformation_inverse() and z ~ Normal(0, sm.C), then np.dot(B, z) ~ Normal(0, I) and sm.C and B have the same eigenvectors. With `reset=True`, also sm.sample(1)[0] ~ Normal(0, I) after the call.

See also: `to_unit_matrix`

def transform(self, x):
overrides `cma.interfaces.StatisticalModelSamplerWithZeroMeanBaseClass.transform`

apply linear transformation C**0.5 to `x`.

def transform_inverse(self, x):
overrides `cma.interfaces.StatisticalModelSamplerWithZeroMeanBaseClass.transform_inverse`

apply inverse linear transformation C**-0.5 to `x`.

def update(self, vectors, weights, c1_times_delta_hsigma=0):
overrides `cma.interfaces.StatisticalModelSamplerWithZeroMeanBaseClass.update`

The natural gradient used for the update of the coordinate-wise variances is:

```np.dot(weights, vectors**2)
```

Details: The weights include the learning rate and -1 <= sum(weights[idx]) <= 1 must be `True` for idx = weights > 0 and for idx = weights < 0. The content of `vectors` with negative weights is changed.

C =

covariance matrix diagonal

constant_trace =

Undocumented

count_tell: `int` =

Undocumented

dimension =
overrides `cma.sampler.GaussSampler.dimension`

Undocumented

Undocumented

randn =

Undocumented

@property
condition_number =
overrides `cma.interfaces.StatisticalModelSamplerWithZeroMeanBaseClass.condition_number`

Undocumented

@property
correlation_matrix =

return correlation matrix of the distribution.

@property
covariance_matrix =
overrides `cma.interfaces.StatisticalModelSamplerWithZeroMeanBaseClass.covariance_matrix`

Undocumented

@property
variances =
overrides `cma.interfaces.StatisticalModelSamplerWithZeroMeanBaseClass.variances`

vector of coordinate-wise (marginal) variances