Integral calculus in Java

  • Sharebar

Recently, I got an opportunity to explore integral calculus in Java. For a finance project I had to calculate the greek metrics (Fair value, Implied Volatility, Delta etc) using BlackScholes model. The formulas for calculating the metrics using Black Scholes model revolved around the following integration
94db15fc7182a86e64c21f5721d4871b.

This integration acted as a showstopper and forced me to search for the APIs available for performing mathematical calculus in Java. Google god came to rescue and provided me with Google code API for Java Calculus.

With Java calculus API in hand, I wrote the program for performing following integration.MSP1037198b609ebc5ig7eh00001de9i627b6d99e3h

...
 CalcParser calcParser2 = new CalcParser("x^3");
 CalcObject calcObject = calcParser2.parse();
 CalcINT calcInt = new CalcINT();
 CalcSymbol calcSymbol = new CalcSymbol("x");
 calcObject = calcInt.integrate(calcObject,calcSymbol);
 CalculusEngine calculusEngine = new CalculusEngine();
 calculusEngine.execute(calcObject.toString().replace('x', '5'));
 System.out.println(calculusEngine.getResult());
...

OUTPUT:
Input: MULTIPLY(POWER(5,ADD(3,1)),POWER(ADD(3,1)),-1)
Evaluation Queue: MULTIPLY(POWER(5,ADD(3,1)),POWER(ADD(3,1)),-1)
Processed Queue: MULTIPLY(-625,POWER(4))
Output: -625*4

Surprised !!! It should have been 625/4.
API wrong :( I double checked my code. I tweaked into the source code of the API and figured out that there is a minor mistake in the source code of the API in the class: javacalculus\src\javacalculus\evaluator\CalcInt (line 68 to 78).

return CALC.MULTIPLY.createFunction
         (CALC.POWER.createFunction(firstObj,
          CALC.ADD.createFunction(secondObj, CALC.ONE)),
	  CALC.POWER.createFunction(CALC.ADD.createFunction(secondObj,
         CALC.ONE)), CALC.NEG_ONE); 

And it should be fixed as follows:
Note the difference in order of closing braces in the last line

return CALC.MULTIPLY.createFunction
         (CALC.POWER.createFunction(firstObj,
          CALC.ADD.createFunction(secondObj, CALC.ONE)),
          CALC.POWER.createFunction(CALC.ADD.createFunction(secondObj,
          CALC.ONE), CALC.NEG_ONE));

With this discovery, I had 2 options either to fix the API or search for the other solution.
Being the fan of Wolfram Alpha search, I decided to look for another solution. Since, the formula had only one integration, I decided to evaluate the integral offline through WolfRam. I substituted the result of integration in place of the integrals in the formulas.

Wolfram engine provided me the following result:MSP1103198b609ebc5ig7eh00002bhgh3b116edg9h2

The "erf" function is readily available in Apache Commons Math API.

This exercise enabled me to look into the relatively unexplored area of integral calculus in Java. I hope my findings above, would be of help for the people researching in this area.

Share the bee buzz:
  • Digg
  • del.icio.us
  • Facebook
  • DZone
  • LinkedIn
  • StumbleUpon
  • Technorati
  • Twitter

Leave a Reply