View Source Scholar.Covariance.ShrunkCovariance (Scholar v0.4.0)

Covariance estimator with shrinkage.

Summary

Functions

Fit the shrunk covariance model to x.

Functions

fit(x, opts \\ [])

Fit the shrunk covariance model to x.

Options

  • :assume_centered? (boolean/0) - If true, data will not be centered before computation. Useful when working with data whose mean is almost, but not exactly zero. If false, data will be centered before computation. The default value is false.

  • :shrinkage (float/0) - Coefficient in the convex combination used for the computation

        of the shrunk estimate. Range is [0, 1]. The default value is `0.1`.

Return Values

The function returns a struct with the following parameters:

  • :covariance - Tensor of shape {num_features, num_features}. Estimated covariance matrix.
  • :location - Tensor of shape {num_features,}. Estimated location, i.e. the estimated mean.

Examples

iex> key = Nx.Random.key(0)
iex> {x, _new_key} = Nx.Random.multivariate_normal(key, Nx.tensor([0.0, 0.0]), Nx.tensor([[0.8, 0.3], [0.2, 0.4]]), shape: {10}, type: :f32)
iex> model = Scholar.Covariance.ShrunkCovariance.fit(x)
iex> model.covariance
#Nx.Tensor<
  f32[2][2]
  [
    [0.7721845507621765, 0.19141492247581482],
    [0.19141492247581482, 0.33952537178993225]
  ]
>
iex> model.location
#Nx.Tensor<
  f32[2]
  [0.18202415108680725, -0.09216632694005966]
>
iex> key = Nx.Random.key(0)
iex> {x, _new_key} = Nx.Random.multivariate_normal(key, Nx.tensor([0.0, 0.0]), Nx.tensor([[0.8, 0.3], [0.2, 0.4]]), shape: {10}, type: :f32)
iex> model = Scholar.Covariance.ShrunkCovariance.fit(x, shrinkage: 0.4)
iex> model.covariance
#Nx.Tensor<
  f32[2][2]
  [
    [0.7000747323036194, 0.1276099532842636],
    [0.1276099532842636, 0.41163527965545654]
  ]
>
iex> model.location
#Nx.Tensor<
  f32[2]
  [0.18202415108680725, -0.09216632694005966]
>