How to use binomial expansion to produce correct results?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Muna Tageldin
il 3 Mar 2023
Modificato: Muna Tageldin
il 3 Mar 2023
Hi, I'm working on numerical problems where I need to calculate the cumulative distribution function values using summation. Furthermore, my code utilizes binomial expansion like the following
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1313510/image.png)
I wrote a function to compute CDF values using bionomial expansion
function results = sum_testing(mu,z,n)
for i = 1:n+1
t= nchoosek(n,i-1);
results(i) = t.*(-1).^((n-i+1)).*exp(-u*(z).*(n-i+1))
end
My problem is that, for large n's (n>100), the CDF values are very large (1e+60). How can I modify the above code to produce accurate results (using bionomial expansion)?
3 Commenti
Risposta accettata
John D'Errico
il 3 Mar 2023
Modificato: John D'Errico
il 3 Mar 2023
How? Using double precision arithmetic? You often can't. You need to remember that double preciion arithmetic lives in 16 significant digits (roughly). So adding and subtracting numbers that differ by more than 16 powers of 10 will yield numerical garbage.
In some cases, you can succeed by the use of logs to compute individual terms, if there would otherwise be an underflow or an overflow, but then multiplying a very large number by a very small one would result in something managable.
You can't just assume a computer can compute anything you throw at it. You might decide that using higher precision is warranted. So you might do those computations using syms and vpa, or perhaps a high precision float tool like my HPF toolbox. But beware that any such solution, while it can succeed, will generally be exceedingly slow. This is the price you must pay. Or, spend some time in learning good numerical methods for solving such problems, which would generally teach you pretty much what I said above.
1 Commento
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Numbers and Precision in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!