Silly error somewhere in my code

Would appreciate someone's help in spotting a silly error (or bad math) somewhere here. Its difficult to explain precisely without being able to attach some equations but basically I'm trying to establish equivalents between q5 and q6 below. But instead I get 0.21 & 0.37 and can't see my own error.
q5 is the integral of a product of a quadratic polynomial and the norm pdf q6 is the normal moment-generating-function in t=2,1,0 where each moment is multiplied by its respective polynomial coefficient h1,h2,h3. I can't see why these are not equivalent. If the problem isn't clear, please email me at mark.whirdy@gmail.com and I will send the equations which should help(can't attach a doc here unfortunately).
mu = 0; sigma = 1;
h1 = -0.003;
h2 = 0.10;
h3 = 0.21;
a = -10^10; b = 10^10;
g = @(mu,sigma,h1,h2,h3)(quad(@(x)((h1*x.^2 + h2*x.^1 + h3*x.^0).*normpdf(x,mu,sigma)), a, b));
q5 = g(mu,sigma,h1,h2,h3)
t = (2:-1:0)';
h = [h1, h2, h3];
q6 = h * exp(mu*t + 0.5.*t.*sigma^2) % moment generating function

1 comentario

AC
AC el 4 de Jul. de 2012
Not the answer but a first error in the code is a square missing in q6. If I remember correctly, the moment generating function of a gaussian is
exp(mu*t + 0.5.*t.^2*sigma^2)
That doesn't solve your problem unfortunately, but it's a start!

Iniciar sesión para comentar.

 Respuesta aceptada

AC
AC el 4 de Jul. de 2012
Editada: AC el 4 de Jul. de 2012
This time a real answer: it's a math problem. If that's ok, I'll answer with some latex code.
Let f be the norm(0,1) pdf
Q5= \int (h_1 x^2+h_2 x +h_3) f(x) dx
= h_1 E(X^2) + h_2 E(X) + h_3
Editing: the following was not correct (wrong definition of moment generating function)
*********************************
Now, the moment generating function M(t) gives:
M(0)=1
M(1)=1+E(X)
M(2)=1+E(X)+2E(X^2)
So Q6 would look like this:
Q6=h_1 M(2) + h_2 M(1) + h_3 M(0)
=h_1 (1+E(X)+2E(X^2)) + h_2 (1+E(X)) + h_3
************************************
And is therefore not equal to Q5.
Convinced?

3 comentarios

AC
AC el 4 de Jul. de 2012
Editada: AC el 4 de Jul. de 2012
Oooops, sorry that's not it, forget it.
Still, Q5 is right. And Q6 is just not equal to Q5 because M(t) is not equal to the t-th moment, it equals a weighted sum of the moments (see e.g. wikipedia).
So anyway, M(1) is not equal to E(X), M(2) is not equal to E(X^2) and so on. That's why your Q6 is "wrong". Sorry for the bad maths up there!
Jan
Jan el 5 de Jul. de 2012
Mark Whirdy has written:
Hi Anne-Claire
You're absolutely right - its not a coding error, its a much more basic error in reasoning the problem.
Thanks for your help here.
Mark
[EDITED, Jan, please post comments as comments, not as answers]
Mark Whirdy
Mark Whirdy el 19 de Dic. de 2012
Editada: Mark Whirdy el 19 de Dic. de 2012
Jan,
It was posted as an Answer intentionally, to show that it was in fact answered (the answer is its a reasoning problem & not a coding problem), and that Anne-Claire provided that answer (by email). I accepted her answer above only to give her the stat. This is reasonable given the circumstances. Thanks

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 4 de Jul. de 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by