I am having a lot of trouble trying to graph this function in matlab and trying to get it to look like it should. I need to generate 200 random samples of a uniform random variable X on the range (0,1) and transforming by the formula $Y=X^2$. Then normalize it so the area is 1.
So I transformed $f(x) = \frac{1}{1-0}=1$ by the equation $f(y) = f(x) * \left |\frac{dx}{dy} \right |$ to get $f(y)=\frac{1}{2x}=\frac{1}{2\sqrt{y}}$ on the range from 0 to 1
Matlab Code:
x = rand(200,1)
histplot = zeros(200,1)
for j = 1:200
histplot(j) = 1/(2*x(j))
end
hist(histplot)
This results in the following graph:
https://dl.dropboxusercontent.com/u/20608771/untitled.jpg
Now there is something seriously wrong that i am missing, because I don't the graph should look somewhat like the following:
https://dl.dropboxusercontent.com/u/20608771/untitled2.jpg
How is my data so off from what it should look like?
You were told to sample the uniform distribution on $[0,1]$ and transform the samples by $Y=X^2$. Instead, you transformed them as $Y=\frac{1}{2X}$, after obtaining this formula from computation of the pdf of $Y$ (which you were not asked to do, as far as I can tell).
The following is Scilab code; the first two lines are identical to Matlab syntax, the third may be different. The Scilab command
histplotautomatically normalizes the area to one; I don't know if its Matlab counterpart does the same. I used $20$ bins; the number of bins was not specified in the problem.To check that this histogram follows the theoretical pdf $f(y)=\frac{1}{2\sqrt{y}}$, I added
The result is below.