Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample from a multivariate t distribution python

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

like image 947
Andriana Avatar asked Aug 06 '26 20:08

Andriana


1 Answers

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
like image 198
Eduard Ilyasov Avatar answered Aug 08 '26 10:08

Eduard Ilyasov