# How to obtain histograms of non-central t distributions from a normal distribution?

My Question;

I'd like to know how to generate random numbers that follows a non-central t distribution using the normal random numbers.
I made a calculation code for this using R (See Box2, below), but it did not match the desired non-central t distribution. I would like to know why and how to correct it.

According to the Wikipedia, a random variable following a non-central t distribution can be generated using the method described in Box 1 below. Following the idea of Box 1, I made a code that uses R to generate a random number that follows a non-central t distribution (See Box2,below) .

The histogram computed by my code under the conditions of 5 degrees of freedom and a non-central parameter of 3 is shown in Figure 1. The red line in Figure 1 represents the non-central t-distribution with 5 degrees of freedom and a non-central parameter of 3. The orange line is the distribution curve estimated from the histogram.

Fig.1

As we can see by comparing the two, the histogram does not seem to be the non-central t-distribution we want to find; their central axis seems to coincide with each other, but their heights do not seem to match.

Box1. Random variable following a non-central t distribution according to the

If $$Z$$ is a normally distributed random variable with unit variance and zero mean, and $$V$$ is a Chi-squared distributed random variable with ν degrees of freedom that is independent of $$Z$$, then

$$T=\frac{Z+\mu}{\sqrt{V/\nu}}\tag{1}$$ is a noncentral ''t''-distributed random variable with ν degrees of freedom and noncentrality parameter μ.

Box2 Caluculation code for R　(Wrong Code.)

#Function for generating random numbers that should follow a non-central t distribution.
nctboot <-function(df,mu){
n=df+1
x=rnorm(df+1, mean = 0, sd = 1)
Z=sum(x)/n
V=sum(x^2)

((Z+mu)/sqrt(V/df))
}

#Calculations to obtain a histogra
df=10
mu=5
numb=10000
sc<-numeric(numb)
for(i in 1:numb){
sc[i]=nctboot(df,mu)
}

#Drawing Histograms and Non-Central Distributions
hist(sc,breaks="Scott", freq=F)
lines(density(sc), col = "orange", lwd = 2)



Thanks for angryavian's answer, I modified the Box2's code (See Box3,below). The histogram computed by my Box3's code under the conditions of 5 degrees of freedom and a non-central parameter of 3 is shown in Figure 2. The histogram seem to be equal to the non-central t-distribution.

Fig.2

Box3. Modified Code

#Function for generating random numbers that should follow a non-central t distribution.
nctboot <-function(df,mu){
n=df+1
Z=rnorm(1, mean = 0, sd = 1)
x=rnorm(df, mean = 0, sd = 1)
V=sum(x^2)/df

((Z+mu)/sqrt(V))
}

#Calculations to obtain a histogra
df=10
mu=5
numb=10000
sc<-numeric(numb)
for(i in 1:numb){
sc[i]=nctboot(df,mu)
}

#Drawing Histograms and Non-Central Distributions
hist(sc,breaks="Scott", freq=F)
lines(density(sc), col = "orange", lwd = 2)


• Assuming your code is correct, this may just be an issue of a scaling mismatch between a histogram and a density; see this answer. Oct 28, 2020 at 2:21
• @angryavian Thank you for your comment. Perhaps it may not be a matter of scale. Because the red line on my Fig.1 is lower near the center line, but the orange line is higher away from the center line. If we assume that the scale is off in the x direction as well, then if we adjust the scale in the x direction (t direction), the center line will probably shift. Oct 28, 2020 at 5:07

• $$Z$$ is $$N(0,1)$$, but for some reason you generate $$\nu+1$$ standard normal RVs and take the mean, which has distribution $$N(0, 1/\sqrt{\nu+1})$$.
• $$V$$ has $$\nu$$ degrees of freedom, so it should be the sum of squares of $$\nu$$ standard normal RVs, not $$\nu+1$$
• $$Z$$ and $$V$$ must be independent, so you cannot use the same generated normal RVs x to define both.
Be a little more careful when writing your code. To generate $$Z \sim N(0,1)$$ you can just use rnorm(1). To generate $$V$$, use rnorm(df) and take the sum of squares.