#Maple code by Khizar Anjum, Feb. 13, 2020 # GetHist(samples,f) is a function that takes in a list `samples`. For example, it can be # the output from Sample(RandomVariable(Normal(0,1)),1000). the second argument is `f` which is # the inverse of interval spacing. It is a measure of frequency. The higher it is, the closer the # approximation to CONT. function. # It outputs a Histogram Data-Structure which is structured as [[--x-samples--],[--y-samples--]]. # The output Histogram is Normalized so y-values all add to 1. Can be plotted nicely with NiceDiscPlot. GetHist :=proc(samples, f) _local(nbins, mytable, i, x_hist_samples,n): with(Statistics): nbins := ceil((max(samples) - min(samples))*f): mytable := FrequencyTable(samples, bins = nbins): x_hist_samples := [[seq(min(samples) + n/f, n = 0 .. nbins-1)],[seq(mytable[i, 3]/100, i = 1 .. nbins)]]: end: # NiceDiscPlot(hist) is a function that nicely plots the Histogram Data-Structure. NiceDiscPlot := proc(newhist) with(DynamicSystems); print(DiscretePlot(newhist[1], newhist[2], style = stem)): end: # Tutorial() is a function that demonstrates the functioning in detail. Tutorial := proc() local X,L,newhist,f,newint,appr: print(`Lets take 1000 samples of Normal(0,1) Distribution`): with(Statistics); X := RandomVariable(Normal(0, 1)): L := Sample(X, 1000): print(`Now, lets plot it with frequency 10. i.e. 0.1 is the unit step`): f := 10: newhist := GetHist(L, f): NiceDiscPlot(newhist); print(`Now, lets add it up and see if it is normalized`): print(add(newhist[2])): #adding y-values only print(`Now, lets try an integral. what about this one?`): newint := int(sqrt(x^2+1)*exp(-x^2/2)/sqrt(2*Pi),x=-infinity..infinity): print(`int(sqrt(x^2+1)*exp(-x^2/2)/sqrt(2*Pi),x=-infinity..infinity) = `,newint, ` = `, evalf(newint)): #--x-values-- = newhist[1][i], --y-values-- = newhist[2][i] appr := add(seq(sqrt(newhist[1][i]^2 + 1)*newhist[2][i],i=1..nops(newhist[1]))): print(`To solve, we just do Reimann sum, and it comes out to be`,appr): print(`Pretty close, you can try by increasing f and number of samples`) end: