Lognormdist Function in .Net – C#
/*Lognormdist = NORMSDIST((LN(x) – mu)/sigma) eg:- Lognormdist(0.367879441,3,2) will give 0.022750062 with this function and in excel it will give 0.022750132*/
/* Note: Doouble quotes for “en-US” and “F” may not be properly shown while pasting this code.you need to manaully remove the double quotes and add it again*/
public double Lognormdist( double x, double zeta, double sigma)
{
/*Gets a NumberFormatInfo associated with the en-US culture.*/
System.Globalization.NumberFormatInfo nfi =new System.Globalization.CultureInfo( “en-US”, false ).NumberFormat;nfi.NumberDecimalDigits = 9;
double u = (Math.Log(x) – zeta)/sigma; double p = this.NormalDistribution(u);
return Convert.ToDouble(p.ToString(“F”, nfi) );
}
private double NormalDistribution(double X)
{
/* Gets a NumberFormatInfo associated with the en-US culture.*/
System.Globalization.NumberFormatInfo nfi =new System.Globalization.CultureInfo( “en-US”, false ).NumberFormat;nfi.NumberDecimalDigits = 9;
double L = 0.0; double K = 0.0; double dCND = 0.0; const double a1 = 0.31938153; const double a2 = -0.356563782; const double a3 = 1.781477937; const double a4 = -1.821255978; const double a5 = 1.330274429;L = Math.Abs(X);K = 1.0 / (1.0 + 0.2316419 * L);dCND = 1.0 – 1.0 / Math.Sqrt(2 * Convert.ToDouble(Math.PI.ToString())) * Math.Exp(-L * L / 2.0) * (a1 * K + a2 * K * K + a3 * Math.Pow(K, 3.0) + a4 * Math.Pow(K, 4.0) + a5 * Math.Pow (K, 5.0));
if (X < 0){double ff = Convert.ToDouble(dCND.ToString( “F”, nfi )); return 1.0 – dCND;}
else{ return dCND;}
}