Serving the Quantitative Finance Community

 
User avatar
lisazx
Topic Author
Posts: 0
Joined: November 1st, 2009, 10:13 pm

What's the problem with my function in VBA which created pdf of noncentral chi-square?

November 9th, 2010, 3:27 pm

To generate the function of pdf of noncentral chi-square in VBA for a application of CIR model,I followed the pdf formula in http://en.wikipedia.org/wiki/Noncentral ... ibutionThe inputs are value X, k, the number of degrees of freedom and, λ, the noncentrality parameter.I also compared the result with function in R.Result of VBA is correct when dataset is normal, such as (60,4,50).In R code: > dchisq(60,4,50)[1] 0.0233497However, when I am using X = 6908.74,k = 51.5248723089,λ= 6821.6668981824, I could get the answer correctly in R, but the function I created in VBA always don?t generate the correct answer. I think the reason is that in pdf function, the exp((-X-lamda)/2) is too small in this case and Bassel function is not large enough.Did anyone know how to solve this problem?Thank you so much!!!VBA code:Public Sub test()X = 6908.74df = 51.5248723089 'kncp = 6821.6668981824 'λDim r1, r2, r3 As Doubler1 = 1 / 2 * Exp(-(X + ncp) / 2)r2 = (X / ncp) ^ (df / 4 - 0.5)r3 = BESSELi((ncp * X) ^ 0.5, (df / 2 - 1))pdfresule = r1 * r2 * r3End Sub
Last edited by lisazx on November 8th, 2010, 11:00 pm, edited 1 time in total.
 
User avatar
DevonFangs
Posts: 0
Joined: November 9th, 2009, 1:49 pm

What's the problem with my function in VBA which created pdf of noncentral chi-square?

November 9th, 2010, 3:42 pm

Your're right, it seems you have the typical "tiny times huge" problem.There exist library functions that give you the scaled Bessel function (i.e. the one already multiplied by the exp), so that the algo is designed to produce a _small_ value which doesn't give you any overflow problem. This is probably what R does.I'll try to find out something easy and tell you.
 
User avatar
lisazx
Topic Author
Posts: 0
Joined: November 1st, 2009, 10:13 pm

What's the problem with my function in VBA which created pdf of noncentral chi-square?

November 9th, 2010, 4:29 pm

Thank you. Look forward to your reply. any input is welcome.
 
User avatar
frenchX
Posts: 11
Joined: March 29th, 2010, 6:54 pm

What's the problem with my function in VBA which created pdf of noncentral chi-square?

November 9th, 2010, 4:57 pm

I don't know if you need high precision.If you don't then you could use approximationsAN APPROXIMATION FOR THE NONCENTRAL CHI-SQUARED DISTRIBUTION It's all a problem of precision I think so maybe there is a better way to calculate the probability distribution that to use the wiki formula.I don't have access to this paper but maybe you do.An effective algo for the noncentral chi squared distributionIt's from the American Statistician 1989 by Harry Posten.It may help you.
 
User avatar
4rcher
Posts: 0
Joined: September 9th, 2008, 8:28 pm

What's the problem with my function in VBA which created pdf of noncentral chi-square?

November 10th, 2010, 2:18 pm

I recommoned you to read Glasserman "Monte-Carlo methods in Financial Engineering" section 3.4 and especially page 124.You don't have to deal with the Bessel function.
 
User avatar
Cuchulainn
Posts: 23029
Joined: July 16th, 2004, 7:38 am

What's the problem with my function in VBA which created pdf of noncentral chi-square?

November 10th, 2010, 3:04 pm

Why made life difficult when you can call it directlySave all that hassle.
Last edited by Cuchulainn on November 9th, 2010, 11:00 pm, edited 1 time in total.
 
User avatar
DevonFangs
Posts: 0
Joined: November 9th, 2009, 1:49 pm

What's the problem with my function in VBA which created pdf of noncentral chi-square?

November 10th, 2010, 3:47 pm

My vote is for the approximation you can find in this article at p 13.
 
User avatar
DevonFangs
Posts: 0
Joined: November 9th, 2009, 1:49 pm

What's the problem with my function in VBA which created pdf of noncentral chi-square?

November 10th, 2010, 3:48 pm

QuoteOriginally posted by: CuchulainnWhy made life difficult when you can call it directlySave all that hassle.Ha ha you're the bestSolve all the problem of mankind with boost