gpt4 book ai didi

python - 来自多元 t 分布 python 的样本

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:17 25 4
gpt4 key购买 nike

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

谢谢

最佳答案

您可以在 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

关于python - 来自多元 t 分布 python 的样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41957633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com