0
$\begingroup$

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?

$\endgroup$

1 Answer 1

1
$\begingroup$

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 histplot automatically 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.

x = rand(200,1)
y = x.^2
histplot(20,y) 

histogram

To check that this histogram follows the theoretical pdf $f(y)=\frac{1}{2\sqrt{y}}$, I added

t = 0.01:0.01:1
f = 0.5*t.^(-1/2)
plot(t,f)

The result is below.

pdf comparison

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .