normpdf is taking zeros

4 次查看(过去 30 天)
Hello, I am using the normpdf command to evaluate a function in MATLAB. When the argument of normpdf is too small, MATLAB reports a zero. Is is possible to get the exact value of normpdf instead of 0?
  4 个评论
Anjali
Anjali 2024-5-5
Hey, I'm stuck on similar problem and not able to decode it, how did you proceed further
John D'Errico
John D'Errico 2024-5-5
I showed what you can do in my answer. If you will form the product of numbers, AND some of those numbers in the product are hugely large, you can always take the logs of each term in the product, then add the logs. Then exponentiate the sum at the end.
And you should see that the log of a normal PDF is trivially easy to compute. For example, even without using syms, we can do that:
mu = 0;
sig = 1;
x = -77;
logp = -(x-mu).^2/sig^2/2 - log(sig) - log(sqrt(2*pi))
logp = -2.9654e+03
Of course, that is a pretty large negative number. But now you can work with the logs of those terms. In the end, the result will still probably be numerical garbage, and still very likely result in an underflow, becaluse logp is such a negative result. But unless you are willing to work entirely in terms of higher precision numbers, this is the best you can do.

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2021-9-25
编辑:John D'Errico 2021-9-25
No. At least, not in double precision. The "exact" value there is impossible to represent in a double precision number.
The normal pdf at z = -77 should be:
mu = 0;
sig = 1;
x = sym(-77);
p = exp(-(x-mu).^2/sig^2/2)*1/(sig*sqrt(2*sym('pi')))
p = 
vpa(p)
ans = 
Do you understand this is a number with around 1288 zeros after the decimal point, before you see any digits?
As such, the result underflows in double precision. You get ZERO.
realmin
ans = 2.2251e-308
Actually, you can go about as far as -38 or so, before an underflow occurs, with the result as what is known as a denormalized number. But that is around the limit. And even most computations with those numbers at that level will still yield numerical garbage.
normpdf(-38)
ans = 1.0972e-314
You can want what you want, but this is not possible working in double precision. If you are willing to use higher precision tools, such as syms, then yes, you can.
  1 个评论
hey yo
hey yo 2021-9-25
Thank you for taking the time to post this reply. It is very valuable.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Bounding Regions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by