2017-01-31 11 views
2

Pythonで多変量の生徒t-分布からサンプリングする関数があるのだろうかと思います。私は14要素、14x14共分散行列と自由度を持つ平均ベクトルを持っており、このt分布からベクトルをサンプリングしたいと思います。 1次元の場合、私はstats.t.rvs(df、loc、scale)を使用しました。多変量の場合に似たものがあるかどうか疑問に思っていました。どんな助けも非常に高く評価されるでしょう。多変量t分布のサンプルpython

おかげ

答えて

2

あなたは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 = 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 
関連する問題