PROBLEM
Generate 100 random numbers from Chi-square distribution with 12 degree freedom show the histogram of this generated data and plot the corresponding density function on the same graph.
Theory-
The chi-squared distribution with df= n ≥ 0 degrees of freedom has density-
f_n(x) = 1 / (2^(n/2) Γ(n/2)) x^(n/2-1) e^(-x/2) ; x>0.
The mean and variance are n and 2n.
R Code-
#Command to remove previous objects
rm(list=ls())
#Generating 100 random numbers from Chi-Square Distribution
x=rchisq(100,12)
#Sorting the random numbers
x1=sort(x)
#Sorted random numbers are-
x1
#Histogram(prob. plot) of the generated random numbers
hist(x,prob=T,xlim=c(0,30),ylim=c(0,0.15),border="black",col="grey",breaks=15,
main = paste("Histogram and Density Plot of \nChi-Square Distribution"))
#Chi-Square Density Plot of Generated numbers
lines(x1,dchisq(x1,12),col="red",lwd=2)
#Drawing legend for the plot
legend("topright",legend=c(expression(bold("Histogram for 100 random no. from Chi-Square Dist.{df=12)")),
expression(bold("Density Plot for 100 ranom no. from Chi-Square Dist.{df=12)")))
,fill=c("grey","red"))
Output On R Console-
> #Command to remove previous objects
> rm(list=ls())
>
> #Generating 100 random numbers from Chi-Square Distribution
> x=rchisq(100,12)
>
> #Sorting the random numbers
> x1=sort(x)
>
> #Sorted random numbers are-
> x1
[1] 3.563341 3.986165 4.790076 5.137254 5.475577 5.822501 5.866661
[8] 6.010742 6.186136 6.306583 6.470758 6.596933 6.607481 6.624999
[15] 6.670431 6.719123 6.721738 6.817978 7.048621 7.120953 7.472223
[22] 7.784390 7.818839 7.862550 7.944896 7.945428 8.018721 8.222264
[29] 8.348805 8.587792 8.602521 8.645715 8.725691 8.845125 8.909389
[36] 9.099596 9.146373 9.192539 9.227033 9.369365 9.482021 9.649494
[43] 9.696064 9.744548 9.765073 9.797461 9.874563 9.892526 9.946173
[50] 10.005752 10.042422 10.051937 10.210783 10.293323 10.339786 10.350271
[57] 10.414500 10.654812 11.020388 11.265752 11.704403 11.756806 11.887717
[64] 12.306171 12.821964 13.033215 13.123094 13.264681 13.580782 13.595597
[71] 13.662621 13.728855 13.838458 13.869891 13.975666 14.028286 14.064400
[78] 14.616696 15.047347 15.185432 15.357018 15.740422 15.773002 15.871207
[85] 16.021173 16.031287 16.034070 16.324973 17.933853 18.009785 18.030095
[92] 18.591688 19.541009 19.547178 19.620247 21.427066 21.453698 21.716946
[99] 21.722274 22.435368
>
> #Histogram(prob. plot) of the generated random numbers
> hist(x,prob=T,xlim=c(0,30),ylim=c(0,0.15),border="black",col="grey",breaks=15,
+ main = paste("Histogram and Density Plot of \nChi-Square Distribution"))
>
>#Chi-Square Density Plot of Generated numbers
> lines(x1,dchisq(x1,12),col="red",lwd=2)
>
> #Drawing legend for the plot
> legend("topright",legend=c(expression(bold("Histogram for 100 random no. from Chi-Square Dist.{df=12)")),
+ expression(bold("Density Plot for 100 ranom no. from Chi-Square Dist.{df=12)")))
+ ,fill=c("grey","red"))
Output Graph-
