Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matlab The histogram of N random realizations of a random variable is essentiall

ID: 3883450 • Letter: M

Question

Matlab

The histogram of N random realizations of a random variable is essentially an approximation of the probability distribution for that random variable, except for some scaling factor. Try to figure out what that scaling factor is, for the specific case of the normal distribution. Generate random samples by using z = randn(N, 1);, and then create a histogram with nbins bins (without actually plotting it) by using [n, zbins]=hist(z, nbins);. Now try plotting that histogram (with different scaling factors for n) in the same plot as the exact probability distribution function, using something like Z = -5: 0.01: 5: Zpdf = nonnpdf(Z): piot(Z, Zpdf,'k','linewidth',2): Can you figure out what the correct scaling factor is (i.e., what factor makes the histogram approximate the probability distribution)?

Explanation / Answer

figureZ=-5:0.01:5;

N=10000;

nbins=50;

Zpdf=normpdf(Z);

holdonz=randn(N,1);[n,zbins]=hist(z,nbins);

scalingFactor=.125*nbins*n/N;

bar(zbins,scalingFactor,1);

plot(Z,Zpdf,'k','linewidth',2)

holdoff%

The scaling factor is 0.125