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

Ledoit-Wolf is a particular form of shrinkage covariance estimator, where the shrinkage coefficient is computed using O. Ledoit and M. Wolf’s formula.

Ledoit and M. Wolf's formula as described in "A Well-Conditioned Estimator for Large-Dimensional Covariance Matrices", Ledoit and Wolf, Journal of Multivariate Analysis, Volume 88, Issue 2, February 2004, pages 365-411.

Summary

Functions

Estimate the shrunk Ledoit-Wolf covariance matrix.

Functions

fit(x, opts \\ [])

Estimate the shrunk Ledoit-Wolf covariance matrix.

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.

Return Values

The function returns a struct with the following parameters:

  • :covariance - Tensor of shape {num_features, num_features}. Estimated covariance matrix.

  • :shrinkage - Coefficient in the convex combination used for the computation of the shrunken estimate. Range is [0, 1].

  • :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.4, 0.2], [0.2, 0.8]]), shape: {50}, type: :f32)
iex> model = Scholar.Covariance.LedoitWolf.fit(x)
iex> model.covariance
#Nx.Tensor<
  f32[2][2]
  [
    [0.3557686507701874, 0.17340737581253052],
    [0.17340737581253052, 1.0300586223602295]
  ]
>
iex> model.shrinkage
#Nx.Tensor<
  f32
  0.15034137666225433
>
iex> model.location
#Nx.Tensor<
  f32[2]
  [0.17184630036354065, 0.3276958167552948]
>

iex> key = Nx.Random.key(0)
iex> {x, _new_key} = Nx.Random.multivariate_normal(key, Nx.tensor([0.0, 0.0, 0.0]), Nx.tensor([[3.0, 2.0, 1.0], [1.0, 2.0, 3.0], [1.3, 1.0, 2.2]]), shape: {10}, type: :f32)
iex> model = Scholar.Covariance.LedoitWolf.fit(x)
iex> model.covariance
#Nx.Tensor<
  f32[3][3]
  [
    [2.5945029258728027, 1.5078359842300415, 1.1623677015304565],
    [1.5078359842300415, 2.106797456741333, 1.1812156438827515],
    [1.1623677015304565, 1.1812156438827515, 1.4606266021728516]
  ]
>
iex> model.shrinkage
#Nx.Tensor<
  f32
  0.1908363401889801
>
iex> model.location
#Nx.Tensor<
  f32[3]
  [1.1228725910186768, 0.5419300198554993, 0.8678852319717407]
>

iex> key = Nx.Random.key(0)
iex> {x, _new_key} = Nx.Random.multivariate_normal(key, Nx.tensor([0.0, 0.0, 0.0]), Nx.tensor([[3.0, 2.0, 1.0], [1.0, 2.0, 3.0], [1.3, 1.0, 2.2]]), shape: {10}, type: :f32)
iex> cov = Scholar.Covariance.LedoitWolf.fit(x, assume_centered?: true)
iex> cov.covariance
#Nx.Tensor<
  f32[3][3]
  [
    [3.8574986457824707, 2.2048025131225586, 2.1504499912261963],
    [2.2048025131225586, 2.4572863578796387, 1.7215262651443481],
    [2.1504499912261963, 1.7215262651443481, 2.154898166656494]
  ]
>