# Generate t distributed random variable in matlab

How can we generate t distributed random variables with given mean and covariance in matlab. trnd is a function in matlab but we can get random variable with different degrees of freedom but how to get random variable with given mean and covariance?

• Do you mean "with given mean and variance"? (Covariance requires another variable.) – gung Aug 10 '15 at 12:34
• This should be considered on topic, IMO. The underlying problem is a statistical confusion about the t distribution, not about MATLAB code. – gung Aug 10 '15 at 12:38
• I agree with @gung, but then stats.stackexchange.com/questions/73513 seems a duplicate. – Juho Kokkala Aug 10 '15 at 13:11
• Thanks for pointing that out, @JuhoKokkala. Since that doesn't have any answers, & this thread does, I closed that Q as a duplicate of this one. – gung Aug 10 '15 at 13:50

Indeed, there is no ready function in MATLAB for generating random numbers from the general t-distribution and I could not find with quick googling any readily available function. Assuming that by covariance, you mean either variance (scalar case) or covariance matrix here is one way that should work.

Let the desired degree of freedom be $\nu$, the mean $m$ and variance $v^2$. (Note that we must have $\nu > 2$ for the variance to be finite!) You first need to compute the scale parameter $\sigma$ that is connected to the variance $v^2$ by $v^2 = \sigma^2 \frac{\nu}{\nu-2}$ from which $\sigma = v \sqrt{\frac{\nu-2}{\nu}}$. Then you use the fact that if $x$ follows standard t-distribution (zero mean, unit scale) with degree of freedom $\nu$ then $m + \sigma x$ follows the desired t-distribution. The matlab function trnd($\nu$) gives you the sample from $x$.

So in short, you generate t-distributed random variable using x=trnd($\nu$) and then y = m+v*sqrt(($\nu$-2)/$\nu$)*x follows t-distribution with the desired mean and variance. This should be easy to implement and test.

In the multidimensional case where the mean vector $m\in\mathbb{R}^p$ and covariance matrix $S\in\mathbb{R}^{p\times p}$ is symmetric and positive definite you do similarly, except that you need to compute the Cholesky factorization $LL^T = S$ and you can use the matlab function mvtrnd which wants the correlation structure as input which can be set to the identity matrix.

So in multidimensional case generate t-distributed random variable using x=mvtrnd(C,$\nu$)' (where C is $p\times p$ identity matrix), L = chol(S)' then y = m+sqrt(($\nu$-2)/$\nu$)* L*x follows t-distribution with the desired mean and covariance matrix.

• What will be $\mu$ for multidimensional case? – undefined Aug 11 '15 at 6:26
• @Shraddha I accidentally wrote $\mu$ instead of $\nu$ in some places so $\nu$ is the degree of freedom in both cases. I fixed it in my answer; I hope it is ok now. – MarkoJ Aug 11 '15 at 8:37
• How can we generate for Cauchy random variable, We know that if geometric power for cauchy is $\sigma$ the power for Gaussian is $\sigma/2cg$ where $cg=1.78$. Now if I want to generate Cauchy random variable with given geometric power or variance How will we generate for both one dimension as well as multi dimension? – undefined Sep 7 '15 at 12:47
• I'm not sure what is geometric power here, could you explain? Also Cauchy distribution does not have variance (or mean) defined. The above algorithm should work even if the variance and/or mean is not defined with obvious changes. (Also, I just realized that one another way to generate random numbers with any location and scale parameter would be of using the fact that multivariate t can be presented as Gaussian scale mixture, see e.g. statlect.com/mcdstu1.htm for some related details. I could extend my answer as soon as I have time for that.) – MarkoJ Sep 7 '15 at 13:51
• geometric power ,u can say scatter parameter. so if I am correct for cauchy distribution to generate random variable with scatter parameter $\sigma$ it should me just $m+\sigma x$ so in matlab it wil be $\sigma *x$ ? – undefined Sep 7 '15 at 14:13