我想知道在Python中是否存在从多变量学生t分布中采样的函数。我有14个元素的平均向量,14x14协方差矩阵和自由度,我想从这个t分布中采样一个向量。对于一维情况,我使用stats.t.rvs(df,loc,scale),我想知道是否有类似的多变量情况。任何帮助将非常感谢。
由于
答案 0 :(得分:3)
您可以在statsmodels GitHub repo的沙箱目录中找到此功能。链接到该功能: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 = 1.
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