random t distribution sample

Hi!
I want to obtain a random sample of outcomes which follows a t student distribution given a mean and a std.
I have been able to create a sample of 1000 observations which follow normal dist. typing A=random('norm',mean,std,1000,1).
How should I do it for a t distribution?
Thanks in advance!

 Accepted Answer

Because the expression for the variance depends on the noncentrality parameter and the degrees of freedom. If you plug in a noncentrality parameter of 2 and degrees of freedom of 100 into the expression for the variance of the noncentral t-distribution and then take the square root, you get a value near 1.
dof = 100;
ncentparam = 2;
ncentvar = ...
(dof*(1+ncentparam^2))/(dof-2)-...
(ncentparam^2*dof)/2*(gamma((dof-1)/2)/gamma(dof/2))^2
sqrt(ncentvar)
Wayne
P.S. On MATLAB Answers you are supposed to accept the answer if you have had your question answered so other people know that they don't have to answer it.

More Answers (8)

Wayne King
Wayne King on 17 Sep 2011
Hi, I think you want trnd or nctrnd if you have the Statistics Toolbox.
The central t-distribution is not parameterized by a mean and variance like a Gaussian distribution. The central t-distribution is only parameterized by the degrees of freedom.
If you want to generate a sample from a noncentral t-distribution, see
nctrnd
By using the noncentral t, you can get the mean to be nonzero.
Hope that helps,
Wayne
Ferran
Ferran on 17 Sep 2011
Thanks Fangjun, I have already gone through that part but didn't help.
Wayne I might be wrong, I thought t distribution was like the normal but with fatter tails due the restriction of the degrees of freedom. Therefore when one has infinite degrees of freedom both distributions are exactly the same.
I am working on VaR and want to verify if t is better than normal to explain stock returns. So generating two random samples with the same mean and std and assuming a different distribution for each of them and then back testing I could get an answer. If it is only parameterized by the degrees of freedom how can I do it?It would be the same for all the companies and all the periods...
Hi, The central t-distribution is symmetric and has mean zero (as long as you have more than 1 degree of freedom). For low degrees of freedom, you are correct that the tails are wider than the standard normal, but that difference goes away as the degrees of freedom increase as you note.
Compare
tcdf(3,4)
normcdf(3,0,1)
tcdf(3,20)
The noncentral t-distribution is not symmetric. The asymmetry depends on the noncentrality parameter and the degrees of freedom.
You can see how the mean and variance depend on the noncentrality parameter and degrees of freedom here:
Wayne
Ferran
Ferran on 17 Sep 2011
Hi Wayne and thanks for answering again and sharing your knowledge with me.
I have checked the wikipedia link and it fits the distribution I am looking for.
So, if for instance I want to generate 100 outcomes of a noncentral t-distribution with mean=2 and 100 degrees of freedom, do you know what should I type?
Many thanks!
R = nctrnd(100,2,100,1);
will give you a random sample of 100 samples from a noncentral t with a mean of 2 (essentially), but note that the standard deviation will be about 1 in that case.
Again, both the mean and the variance depend on both the noncentrality parameter and the degrees of freedom.
Wayne
Ferran
Ferran on 17 Sep 2011
Thanks a lot, very helpful! Just a last question: why std will be close to 1 in this case?does it depend in the degrees of freedom? how do you reach this conclusion beforehand?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!