For the first piece of code,
I guess the reason why you have unexpected result is when you are
doing the calculations on hlp
, you try to avoid 0 values, as 0^eta
will blow up - and that is not wanted result, so you simply drop it.
But in the last step, mean(hlp)
will take all values in array hlp
,
including those 0's. Try this:
mean(hlp(hlp>0))
.
My results fall in roughly 2.x with 10,000-point simulation, 2.3x ~
2.4x with 1,000,000 points.
I was wrong. Your problem is you are using too few points. try 10,000,000 points, and you will be satisfied :)
For the second, I have a problem understanding the way you define your variables. (I don't have enough reputations to add a comment, so I put it here. ) Does the "sq" in w_sq
mean the square root of w
? Because according to the documentation of random
, the argument should be "sigma", which is standard deviation. It's natural to define SD as the square root of the variance, which, I guess, is w
. Then you are doing right in the first piece.
If so, in the second piece of your code, why do you put sqrt()
on w_sq
? Do you mean taking a 4th root of the variance? From your definition of expectation, I don't think it's correct. Please take a look at it.
On the other hand, is w_sq
a single number, or an array?
tmpp / sqrt(w_sq(1))
should be tmpp / sqrt(w_sq)
, although they don't make difference in fact.
You may want to put all codes in a for
loop. The loop iterates over w_sq
, each time it picks out one of the elements in the array (name the variable as say w_sq_elem
), and let the rest of the code do things as if it is a single number.
Anyway, (log(x)-m)/sqrt(w_sq)
and tmpp / sqrt(w_sq(1))
are telling different information on w_sq
. The first one assumes it's a number, so the division can be simply a /
, rather than ./
. The second one indicates it's an array so you are picking its first element. But an array does not make sense here because, in my understanding, you are not dividing 10,000 points in x
by 10,000 different variances.
m = 3;
w_sq = 2;
eta1 = -2;
ST = random('Lognormal',m,w_sq,10000000,1);
hlp = zeros(10000000,1);
hlp(ST<=2) = ST(ST<=2);
hlp(hlp>0) = hlp(hlp>0).^(eta1+1); % 0^(eta1+1) gives infinity
mean(hlp)
tmpp = integral(@(x) x.^(eta1) .* normpdf((log(x)-m)/w_sq),0,2) ;
tmpp / w_sq
>> untitled
ans =
0.2944
ans =
0.2948
>>