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?

  • $\begingroup$ Do you mean "with given mean and variance"? (Covariance requires another variable.) $\endgroup$ Aug 10, 2015 at 12:34
  • 3
    $\begingroup$ This should be considered on topic, IMO. The underlying problem is a statistical confusion about the t distribution, not about MATLAB code. $\endgroup$ Aug 10, 2015 at 12:38
  • 1
    $\begingroup$ I agree with @gung, but then stats.stackexchange.com/questions/73513 seems a duplicate. $\endgroup$ Aug 10, 2015 at 13:11
  • $\begingroup$ 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. $\endgroup$ Aug 10, 2015 at 13:50

1 Answer 1


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.

  • $\begingroup$ What will be $\mu$ for multidimensional case? $\endgroup$
    – undefined
    Aug 11, 2015 at 6:26
  • $\begingroup$ @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. $\endgroup$
    – MarkoJ
    Aug 11, 2015 at 8:37
  • $\begingroup$ 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? $\endgroup$
    – undefined
    Sep 7, 2015 at 12:47
  • $\begingroup$ 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.) $\endgroup$
    – MarkoJ
    Sep 7, 2015 at 13:51
  • $\begingroup$ 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$ ? $\endgroup$
    – undefined
    Sep 7, 2015 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.