来自多元 t 分布 python 的样本

And*_*ana 5 python statistics

我想知道是否有一个函数可以从 Python 中的多元学生 t 分布中采样。我有 14 个元素的均值向量、14x14 协方差矩阵和自由度,我想从这个 t 分布中采样一个向量。对于一维案例,我使用了 stats.t.rvs(df,loc,scale) ,我想知道多元案例是否有类似的东西。任何帮助将不胜感激。

谢谢

Edu*_*sov 6

您可以在 statsmodels GitHub 存储库的沙箱目录中找到此函数。函数链接:https://github.com/statsmodels/statsmodels/blob/master/statsmodels/sandbox/distributions/multivariate.py#L90

函数源代码:

#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
Run Code Online (Sandbox Code Playgroud)