Page 1 of 1
What's the problem with my function in VBA which created pdf of noncentral chi-square?
Posted: November 9th, 2010, 3:27 pm
by lisazx
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
What's the problem with my function in VBA which created pdf of noncentral chi-square?
Posted: November 9th, 2010, 3:42 pm
by DevonFangs
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.
What's the problem with my function in VBA which created pdf of noncentral chi-square?
Posted: November 9th, 2010, 4:29 pm
by lisazx
Thank you. Look forward to your reply. any input is welcome.
What's the problem with my function in VBA which created pdf of noncentral chi-square?
Posted: November 9th, 2010, 4:57 pm
by frenchX
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.
What's the problem with my function in VBA which created pdf of noncentral chi-square?
Posted: November 10th, 2010, 2:18 pm
by 4rcher
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.
What's the problem with my function in VBA which created pdf of noncentral chi-square?
Posted: November 10th, 2010, 3:04 pm
by Cuchulainn
Why made life difficult when you can call it directlySave all that hassle.
What's the problem with my function in VBA which created pdf of noncentral chi-square?
Posted: November 10th, 2010, 3:47 pm
by DevonFangs
My vote is for the approximation you can find in this article at p 13.
What's the problem with my function in VBA which created pdf of noncentral chi-square?
Posted: November 10th, 2010, 3:48 pm
by DevonFangs
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