• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Simpson Integral Algorithm

Joined
1/13/11
Messages
1,362
Points
93
Hello! I need a suggestion. I have defined a Simpson algorithm in C# but there is one problem in general integration. When selecting the number of subintervals to choose (n) I have no clue what method to use to be adjusted to any data scale. For example I have to integrate a function X^2 on an interval [-5,5] how many subintervals should I chose? I need a general response (not in this particular case) since I'm writing a code for it. Thank you
 
It depends. You cannot derive the exact formula guiding you to select the n. Its depended on the scale of the function. There will be need to manually choose n for each function.
 
If you prefer accuracy over speed you could also start with any n, solve the integral using the Simpson Approximation, double n, solve the Integral again, and continue in this fashion until your consecutive approximations are less than some tolerance.
 
BTW which is the fastest algorithm calculating integral in 10e-14 precision (high precision generally) that you can come up?
 
Calculate simpsons rule iteratively, but in such a way that you dont ever recalculate the same value twice. You can set up the progressive mesh steps such that averaging new points with old points is sufficient, this will double the speed of your iteration.

If the 4th derivative of your function varies (aka, 5th derivative is high in absolute value in atleast one region), you might benefit from non uniform mesh. The error in Simpsons rule is related to the 4th derivative, so in regions with higher 4th derivative, it makes sense to have a denser mesh.
 
Thanks Dylan. I got a Gauss Kronrod algorithm which prooves to be faster.
 
And BTW how do you think is Gauss-Kronod algorithm(taking weights for eachpoint to calculate integral) faster than the Simpson algorithm?
 
Yes, Gaussian quadrature is typically faster.
 
Yes, Gaussian quadrature is typically faster.

Actually, as I checked Gaussian is faster in big intervals. In low intervals Simpson is more efficient. But in more than one dimensional integration Monte Carlo integration method is fastest. (At least as I found out)
 
Well Simpson often stacks when integrating over a large interval. BTW I also need another suggestion. What method do you use to find an integral of an unbounded function. In case of normal distribution an easy conversion method helps and its easy(converting to standard normal) But how would you behave in case of Gamma function?
gammfunc.gif
 
Well Simpson often stacks when integrating over a large interval. BTW I also need another suggestion. What method do you use to find an integral of an unbounded function. In case of normal distribution an easy conversion method helps and its easy(converting to standard normal) But how would you behave in case of Gamma function?
gammfunc.gif

One way is to transform an infinite interval to [0,1].

In the above specific case I use the Boost Math Toolkit.
 
One way is to transform an infinite interval to [0,1].

I have conducted an attempt to figure out that particular integral analytically. It works when "a" is a positive integer number. But in case of other functions whose integrals cannot be found analytically I have some problems when the interval is unbounded. BTW how can I transform the function to [0,1] interval? Thanks
 
I have conducted an attempt to figure out that particular integral analytically. It works when "a" is a positive integer number. But in case of other functions whose integrals cannot be found analytically I have some problems when the interval is unbounded. BTW how can I transform the function to [0,1] interval? Thanks

When is a positive integer the valus is (a-1)! (factorial).

Possible transformations
y = tanh(x)
y = x/(x+1)


Another trick is domain truncation [0, A] s.t, f(A) < epsilon.

Try them in C++/C# is a good way to get a feel for it.

//
In general, analytic hunting for an exact solution leads nowhere. Sometimes you get lucky, if you are lucky.
 
Back
Top