I have been tasked with migrating some specific Financial functions written in F# to C# The F# library I am using is excellent (http://archive.msdn.microsoft.com/FinancialFunctions) but environmental considerations prevent the deployment of a CLR assembly to our SQL Server with Unrestricted permissions that it requires to be deployed
The specific function I am having a problem with is the MacAuley Duration formula. I am an absolute novice with F# and would appreciate if an F# expert would explain the code snippet below:
let aggrFunction acc index =
let x5 = float index - 1. + x1
let x6 = pow x3 x5
( x6 <> 0.) |> elseThrow "x6 must be different from 0)"
let x7 = (100. * coupon / frequency) / x6
let a, b = acc
a + x7 * x5, b + x7
let term2, term4 = aggrBetween 1 (int n) aggrFunction (0., 0.)
My best C# attempt looks thus and returns the wrong result (I am running the two side by side). btw frequency is an Enum hence the cast
double term2 = 0;
double term4 = 0;
for (int i = 0; i <= n ; i++)
{
var x5 = i - 1 + x1;
var x6 = Math.Pow(x3, x5);
if (x6 == 0)
{
throw new Exception("x6 must be different from 0)");
}
var x7 = (100D * coupon/(int) frequency) / x6;
term2 += (x7*x5);
term4 += x7;
}