probability#

gym_socks.kernel.probability.kernel_bayes_rule(G, K, Kx, alpha, regularization_param=None)[source]#

Computes the kernel Bayes’ rule using Gram matrices.

The kernel Bayes’ rule.

\[P(Y \mid x) = \frac{P(x \mid Y) P(Y)}{P(x)}, \quad P(x) = \int_{\Omega} P(x \mid y) P(d y)\]
Parameters
  • G – The Gram matrix of Y. Typically computed as G = kernel_fn(Y).

  • K – The Gram matrix of X. Typically computed as K = kernel_fn(X).

  • Kx – The feature vector of x. Typically computed as Kx = kernel_fn(X, x).

  • alpha – The coefficient vector corresponding to the marginal embedding of P(X).

  • regularization_param (Optional[float]) – The regularization parameter of the matrix inverse.

Returns

The coefficients of the conditional distribution embedding of P(Y | x).

gym_socks.kernel.probability.kernel_chain_rule(G, K, alpha, regularization_param=None)[source]#

Computes the kernel chain rule using Gram matrices.

The kernel chain rule computes the joint distribution embedding by taking the product of conditional and marginal distribution embeddings.

\[P(Y, X) = P(Y \mid X) P(X)\]
Parameters
  • G – The gram matrix of Y. Typically computed as G = kernel_fn(Y).

  • K – The Gram matrix of X. Typically computed as K = kernel_fn(X).

  • alpha – The coefficient vector corresponding to the marginal embedding of P(X).

  • regularization_param (Optional[float]) – The regularization parameter of the matrix inverse.

Returns

The coefficients of the joint distribution embedding of P(Y, X).

gym_socks.kernel.probability.kernel_sum_rule(G, K, alpha, regularization_param=None)[source]#

Computes the kernel sum rule using Gram matrices.

The kernel sum rule computes the marginal of a conditional distribution by integrating out the conditioning variables.

\[P(Y) = \int_{\Omega} P(Y \mid X) P(d X)\]
Parameters
  • G – The gram matrix of Y. Typically computed as G = kernel_fn(Y).

  • K – The Gram matrix of X. Typically computed as K = kernel_fn(X).

  • alpha – The coefficient vector corresponding to the marginal embedding of P(X).

  • regularization_param (Optional[float]) – The regularization parameter of the matrix inverse.

Returns

The coefficients of the marginal embedding of P(Y).

gym_socks.kernel.probability.maximum_mean_discrepancy(X, Y, kernel_fn=None, biased=False, squared=False)[source]#

Maximum mean discrepancy between two empirical distributions.

Compute the maximum mean discrepancy between two distributions (samples \(X\) and \(Y\)) using a kernel-based statistic. The biased estimator \(\widehat{\textrm{MMD}}_{b}^{2}(X, Y, \mathscr{H})\) uses U-statistics, but can be negative. The unbiased estimator \(\widehat{\textrm{MMD}}_{u}^{2}(X, Y, \mathscr{H})\) uses V-statistics.

\[\widehat{\textrm{MMD}}_{b}^{2}(X, Y, \mathscr{H}) = \frac{1}{m^{2}} \sum_{i=1}^{m} \sum_{j=1}^{m} k(x_{i}, x_{j}) + \frac{1}{n^{2}} \sum_{i=1}^{n} \sum_{j=1}^{n} k(y_{i}, y_{j}) - \frac{2}{mn} \sum_{i=1}^{m} \sum_{j=1}^{n} k(x_{i}, y_{j})\]
\[\widehat{\textrm{MMD}}_{u}^{2}(X, Y, \mathscr{H}) = \frac{1}{m(m - 1)} \sum_{i=1}^{m} \sum_{j \neq i}^{m} k(x_{i}, x_{j}) + \frac{1}{n(n - 1)} \sum_{i=1}^{n} \sum_{j \neq i}^{n} k(y_{i}, y_{j}) - \frac{2}{mn} \sum_{i=1}^{m} \sum_{j=1}^{n} k(x_{i}, y_{j})\]
Parameters
  • X – The observations from distribution P organized in ROWS.

  • Y – The observations from distribution Q organized in ROWS.

  • kernel_fn – The kernel function is a function that returns an ndarray where each element is the pairwise evaluation of a kernel function. See sklearn.metrics.pairwise for more info. The default is the RBF kernel.

  • biased (bool) – Whether to use the biased MMD.

  • squared (bool) – Whether or not the result is squared.

Returns

The scalar value representing the MMD.

gym_socks.kernel.probability.probability_matrix(states, X, Y)[source]#

Compute the probability matrix for discrete state space transitions.

Use the delta kernel to compute the transition probability matrix.

Parameters
  • states (numpy.ndarray) – An array containing the states of the Markov chain.

  • X (numpy.ndarray) – The observations oganized in ROWS.

  • Y (numpy.ndarray) – The observations oganized in ROWS.

Returns

The transition probability matrix P. Each row and column corresponds to a state, e.g. for states \([0, 1, 2]\), the matrix P is \(3 \times 3\) and the first element in P in the upper left corner corresponds to the probability of state 0 transitioning to state 0. The sum of each row is 1, within rounding errors.

Example

>>> import numpy as np
>>> from gym_socks.kernel.probability import probability_matrix
>>> X = np.array([[0], [0], [1], [2]], dtype=int)
>>> Y = np.array([[1], [2], [1], [2]], dtype=int)
>>> probability_matrix(np.array([[0], [1], [2]], dtype=int), X, Y)
array([[0. , 0.5, 0.5],
       [0. , 1. , 0. ],
       [0. , 0. , 1. ]])