7

I am wondering if there is a function for sampling from a multivariate student t-distribution in Python. I have the mean vector with 14 elements, the 14x14 covariance matrix and the degrees of freedom and I want to sample a vector from this t-distribution. For the one dimensional case I used stats.t.rvs(df,loc,scale) and I was wondering if there is something similar for the multivariate case. Any help would be very much appreciated.

Thanks

Andriana
  • 367
  • 3
  • 8
  • 17

1 Answers1

6

You can find this function into sandbox directory of statsmodels GitHub repo. Link to the function: https://github.com/statsmodels/statsmodels/blob/master/statsmodels/sandbox/distributions/multivariate.py#L90

Source code of the function:

#written by Enzo Michelangeli, style changes by josef-pktd
# Student's T random variable
def multivariate_t_rvs(m, S, df=np.inf, n=1):
    '''generate random variables of multivariate t distribution
    Parameters
    ----------
    m : array_like
        mean of random variable, length determines dimension of random variable
    S : array_like
        square array of covariance  matrix
    df : int or float
        degrees of freedom
    n : int
        number of observations, return random array will be (n, len(m))
    Returns
    -------
    rvs : ndarray, (n, len(m))
        each row is an independent draw of a multivariate t distributed
        random variable
    '''
    m = np.asarray(m)
    d = len(m)
    if df == np.inf:
        x = np.ones(n)
    else:
        x = np.random.chisquare(df, n) / df
    z = np.random.multivariate_normal(np.zeros(d), S, (n,))
    return m + z/np.sqrt(x)[:,None]   # same output format as random.multivariate_normal
xjcl
  • 12,848
  • 6
  • 67
  • 89
Eduard Ilyasov
  • 3,268
  • 2
  • 20
  • 18