View Source Scholar.Stats (Scholar v0.4.0)

Statistical functions

All of the functions in this module are implemented as numerical functions and can be JIT or AOT compiled with any supported Nx compiler.

Summary

Functions

Computes correlation matrix for sample inputs x.

Computes the kurtosis (Fisher or Pearson) of a dataset.

Calculates the nth moment about the mean for a sample.

Computes the sample skewness of a data set.

Functions

correlation_matrix(x)

Computes correlation matrix for sample inputs x.

The value on the position CorrijCorr_{ij} in the CorrCorr matrix is calculated using the formula: Corr(X_i,X_j)=Cov(X_i,X_j)Cov(X_i,X_i)Cov(X_j,X_j) Corr(X\_i, X\_j) = \frac{Cov(X\_i, X\_j)}{\sqrt{Cov(X\_i, X\_i)Cov(X\_j, X\_j)}} Where:

  • XiX_i is a iith row of input

  • Cov(X_i,X_j)Cov(X\_i, X\_j) is covariance between features XiX_i and XjX_j

Time complexity of correlation estimation is O(NK2)O(N * K^2) where NN is the number of samples and KK is the number of features.

Example

iex> Scholar.Stats.correlation_matrix(Nx.tensor([[3, 6, 5], [26, 75, 3], [23, 4, 1]]))
#Nx.Tensor<
  f32[3][3]
  [
    [1.0, 0.580316960811615, -0.7997867465019226],
    [0.580316960811615, 1.0, 0.024736011400818825],
    [-0.7997867465019226, 0.024736011400818825, 1.0]
  ]
>

iex> Scholar.Stats.correlation_matrix(Nx.tensor([[3, 6], [2, 3], [7, 9], [5, 3]]))
#Nx.Tensor<
  f32[2][2]
  [
    [1.0, 0.6673083305358887],
    [0.6673083305358887, 1.0]
  ]
>

iex> x = Nx.tensor([[3, 6, 5], [26, 75, 3], [23, 4, 1]])
iex> means = Nx.mean(x, axes: [-2])
iex> Scholar.Stats.correlation_matrix(x, means)
#Nx.Tensor<
  f32[3][3]
  [
    [1.0, 0.580316960811615, -0.7997867465019226],
    [0.580316960811615, 1.0, 0.024736011400818825],
    [-0.7997867465019226, 0.024736011400818825, 1.0]
  ]
>

correlation_matrix(x, means)

kurtosis(tensor, opts \\ [])

Computes the kurtosis (Fisher or Pearson) of a dataset.

Options

  • :axes - Axes to calculate the operation. If set to nil then the operation is performed on the whole tensor. The default value is [0].

  • :keep_axes (boolean/0) - If set to true, the axes which are reduced are left. The default value is false.

  • :bias (boolean/0) - If false, then the calculations are corrected for statistical bias. The default value is true.

  • :variant - If :fisher then Fisher's definition is used, if :pearson then Pearson's definition is used. The default value is :fisher.

Examples

iex> x = Nx.tensor([[3, 5, 3], [2, 6, 1], [9, 3, 2], [1, 6, 8]])
iex> Scholar.Stats.kurtosis(x)
#Nx.Tensor<
  f32[3]
  [-0.7980852127075195, -1.0, -0.8394768238067627]
>

moment(tensor, moment, opts \\ [])

Calculates the nth moment about the mean for a sample.

Options

  • :axes - Axes to calculate the operation. If set to nil then the operation is performed on the whole tensor. The default value is [0].

  • :keep_axes (boolean/0) - If set to true, the axes which are reduced are left. The default value is false.

Examples

iex> x = Nx.tensor([[3, 5, 3], [2, 6, 1], [9, 3, 2], [1, 6, 8]])
iex> Scholar.Stats.moment(x, 2)
#Nx.Tensor<
  f32[3]
  [9.6875, 1.5, 7.25]
>

skew(tensor, opts \\ [])

Computes the sample skewness of a data set.

Options

  • :axes - Axes to calculate the operation. If set to nil then the operation is performed on the whole tensor. The default value is [0].

  • :keep_axes (boolean/0) - If set to true, the axes which are reduced are left. The default value is false.

  • :bias (boolean/0) - If false, then the calculations are corrected for statistical bias. The default value is true.

Examples

iex> x = Nx.tensor([[3, 5, 3], [2, 6, 1], [9, 3, 2], [1, 6, 8]])
iex> Scholar.Stats.skew(x)
#Nx.Tensor<
  f32[3]
  [0.9794093370437622, -0.8164965510368347, 0.9220733642578125]
>