public Task <QuotationResult> CalculatePremiums(QuotationInput quoteparams)
        {
            try
            {
                return(Task.Run(() =>
                {
                    double d1 = 0.0;
                    double d2 = 0.0;
                    QuotationResult quotes = new QuotationResult();
                    double S = double.Parse(quoteparams.StockPrice.Replace(".", ","));
                    double K = double.Parse(quoteparams.StrikePrice.Replace(".", ","));
                    double T = double.Parse(quoteparams.TimeToMaturity.Replace(".", ","));
                    double r = double.Parse(quoteparams.InterestRate.Replace(".", ","));
                    double v = double.Parse(quoteparams.Volatility.Replace(".", ","));

                    d1 = Math.Round((Math.Log(S / K) + (r + v * v / 2.0) * T) / v / Math.Sqrt(T), 4);
                    d2 = Math.Round(d1 - v * Math.Sqrt(T), 4);

                    quotes.D1 = d1;
                    quotes.D2 = d2;

                    quotes.CallPremium = Math.Round(S * CumulativeNormDistFunction(d1) - K * Math.Exp(-r * T) * CumulativeNormDistFunction(d2), 4);
                    quotes.PutPremium = Math.Round(K * Math.Exp(-r * T) * CumulativeNormDistFunction(-d2) - S * CumulativeNormDistFunction(-d1), 4);

                    return quotes;
                }));
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        List <PointF> GenerateRandNormal(int n)
        {
            if (n % 2 == 1)
            {
                n++;
            }

            List <PointF> points = new List <PointF>(n);

            for (int i = 0; i < n / 2; i++)
            {
                do
                {
                    double u = 2 * rnd.NextDouble() - 1;
                    double v = 2 * rnd.NextDouble() - 1;

                    double s = u * u + v * v;

                    if (s < 1)
                    {
                        s = Math.Sqrt(-2 * Math.Log(s) / s);
                        points.Add(new PointF(i, (float)(u * s)));
                        points.Add(new PointF(i + 1, (float)(v * s)));
                        break;
                    }
                } while (true);
            }

            return(points);
        }
Exemplo n.º 3
0
        // Normalize coefficients à la Jenkins & Traub's RPOLY.
        // Normalization is done by scaling coefficients with a power of 2, so
        // that all the bits in the mantissa remain unchanged.
        // Use the infinity norm (max(sum(abs(a)…)) to determine the appropriate
        // scale factor. See @hkrish in #1087#issuecomment-231526156
        private static Real GetNormalizationFactor(params Real[] values)
        {
            var norm = values.Max();

            return(norm != 0 && (norm < 1e-8 || norm > 1e8)
                ? Math.Pow(2, -Math.Round(Math.Log(norm)))
                : 0f);
        }
    public double NextGaussian()
    {
        const double mean = 0, stdDev = 1;

        double u1            = 1.0 - rand.NextDouble();
        double u2            = 1.0 - rand.NextDouble();
        double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2);

        return(mean + stdDev * randStdNormal);
    }
Exemplo n.º 5
0
        /// <summary>
        /// Evaluates the delta wrt the discount rate R.
        /// </summary>
        /// <param name="amount">The amount for that period.</param>
        /// <param name="paymentDiscountFactor">The payment discount factor.</param>
        /// <param name="periodAsTimesPerYear">the compounding year fraction.</param>
        /// <param name="curveYearFraction">The time to payment year fraction.</param>
        /// <returns></returns>
        public static double Delta1ForAnAmount(double amount, double paymentDiscountFactor,
                                               double periodAsTimesPerYear, double curveYearFraction)
        {
            if (curveYearFraction == 0.0)
            {
                return(0.0);
            }
            var rate   = (double)Math.Log((double)paymentDiscountFactor) / curveYearFraction;
            var temp   = periodAsTimesPerYear * -rate;
            var result = amount * paymentDiscountFactor * curveYearFraction / (1 + temp) / 10000.0;

            return(result);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Converts a discount factor to a compounding zero rate.
        /// </summary>
        ///<param name="discountFactor"></param>
        ///<param name="yearFraction"></param>
        ///<param name="compoundingPeriod"></param>
        ///<returns></returns>
        public static double DiscountFactorToZeroRate(double discountFactor, double yearFraction,
                                                      double compoundingPeriod)
        {
            double result;

            if (compoundingPeriod == 0)
            {
                result = -Math.Log(discountFactor) / yearFraction;
            }
            else
            {
                double power = -compoundingPeriod / yearFraction;
                result = (Math.Pow(discountFactor, power) - 1.0) / compoundingPeriod;
            }
            return(result);
        }
        private static double[] DiscountFactorHelper(double[] times, double[] dfs)
        {
            var zeroes    = new List <double>();
            var dfsLength = dfs.Length;

            if (times.Length == dfsLength)
            {
                var zero = 0.0;
                for (var i = dfsLength - 1; i >= 0; i--)
                {
                    if (times[i] != 0)
                    {
                        zero = -Math.Log(dfs[i]) / times[i];
                    }
                    zeroes.Add(zero);
                }
            }
            return(zeroes.ToArray());
        }
        private static double[] DiscountFactorHelper(DateTime baseDate, IEnumerable <DateTime> dates, double[] dfs)
        {
            var zeroes    = new List <double>();
            var dfsLength = dfs.Length;
            var times     = PointCurveHelper(baseDate, dates);

            if (times.Length == dfsLength)
            {
                var zero = 0.0;
                for (var i = dfsLength - 1; i >= 0; i--)
                {
                    if (times[i] != 0)
                    {
                        zero = -Math.Log(dfs[i]) / times[i];
                    }
                    zeroes.Add(zero);
                }
            }
            return(zeroes.ToArray());
        }
Exemplo n.º 9
0
        public static double QuadraticBezierLength(Vector2D p0, Vector2D p1, Vector2D p2)
        {
            Vector2D a = new Vector2D();
            Vector2D b = new Vector2D();

            a.X = p0.X - 2 * p1.X + p2.X;
            a.Y = p0.Y - 2 * p1.Y + p2.Y;
            b.X = 2 * p1.X - 2 * p0.X;
            b.Y = 2 * p1.Y - 2 * p0.Y;
            var A = 4 * (a.X * a.X + a.Y * a.Y);
            var B = 4 * (a.X * b.X + a.Y * b.Y);
            var C = b.X * b.X + b.Y * b.Y;

            var Sabc = 2 * SysMath.Sqrt(A + B + C);
            var A_2  = SysMath.Sqrt(A);
            var A_32 = 2 * A * A_2;
            var C_2  = 2 * SysMath.Sqrt(C);
            var BA   = B / A_2;

            return((A_32 * Sabc + A_2 * B * (Sabc - C_2) + (4 * C * A - B * B) * SysMath.Log((2 * A_2 + BA + Sabc) / (BA + C_2))) / (4 * A_32));
        }
Exemplo n.º 10
0
 /// <summary>
 ///     Finds the logarithm of a number with the specified base.
 /// </summary>
 /// <param name="number">Number greater than 0.</param>
 /// <param name="logBase">Base of the logarithm in the range [0,1),(1, ∞).</param>
 /// <returns name="log">Logarithm of the number.</returns>
 /// <search>logarithm,ld,lg</search>
 public static double Log(double number, double logBase)
 {
     return(CSMath.Log(number, logBase));
 }
Exemplo n.º 11
0
 /// <summary>
 ///     Finds the natural logarithm of a number in the range (0, ∞).
 /// </summary>
 /// <param name="number">Number greater than 0.</param>
 /// <returns name="log">Natural log of the number.</returns>
 /// <search>natural,logarithm,ln</search>
 public static double Log(double number)
 {
     return(CSMath.Log(number));
 }
Exemplo n.º 12
0
 // Logarithm to base N
 public static double LogN(double x, double n)
 {
     return(MathObj.Log(x) / MathObj.Log(n));
 }
Exemplo n.º 13
0
 // Inverse Hyperbolic Cotangent
 public static double HArccotan(double x)
 {
     return(MathObj.Log((x + 1) / (x - 1)) / 2);
 }
Exemplo n.º 14
0
 public static double Log(double d)
 => Math.Log(d);
Exemplo n.º 15
0
 /// <summary>
 /// Calculates a natural logarithm of dual number
 /// </summary>
 public static DualNumber Log(DualNumber value)
 {
     return(new DualNumber((float)SMath.Log(value.Value), value.Derivative / value.Value));
 }
Exemplo n.º 16
0
 public static double NextPowerOfTwo(double n)
 {
     return((double)SysMath.Pow(2.0, SysMath.Ceiling(SysMath.Log(n, 2.0))));
 }
Exemplo n.º 17
0
 public static double Log(double value)
 {
     return(CSMath.Log(value));
 }
Exemplo n.º 18
0
 public static double Log(this int i)
 {
     return(SysMath.Log(i));
 }
Exemplo n.º 19
0
 public static double Log(this double d)
 {
     return(SysMath.Log(d));
 }
Exemplo n.º 20
0
            //IBundle
            public object Property(Interpreter interpreter, Token token, object argument)
            {
                string propertyName = (string)argument;

                //utility functions
                Func <double, double, double> NthRoot = (b, e) => {
                    if (e % 1 != 0 || e <= 0)
                    {
                        throw new ErrorHandler.RuntimeError(token, "Exponent must be a whole number above 0");
                    }
                    return(CSMath.Pow(b, 1 / e));
                };

                Func <double, double> Asinh = (x) => CSMath.Log(x + CSMath.Sqrt(x * x + 1));
                Func <double, double> Acosh = (x) => CSMath.Log(x + CSMath.Sqrt(x * x - 1));
                Func <double, double> Atanh = (x) => CSMath.Log((1 + x) / (1 - x)) / 2;

                switch (propertyName)
                {
                case "PI": return(3.14159265358979);

                case "E": return(2.71828182845905);

                case "Abs": return(new CallableArity1(this, CSMath.Abs));

                case "Floor": return(new CallableArity1(this, CSMath.Floor));

                case "Ceil": return(new CallableArity1(this, CSMath.Ceiling));

                case "Round": return(new CallableArity1(this, CSMath.Round));

                case "Pow": return(new CallableArity2(this, CSMath.Pow));

                case "Root": return(new CallableArity2(this, NthRoot));                        //doesn't exist in C# yet

                case "Log": return(new CallableArity1(this, CSMath.Log));

                case "Exp": return(new CallableArity1(this, CSMath.Exp));

                case "Sin": return(new CallableArity1(this, CSMath.Sin));

                case "Cos": return(new CallableArity1(this, CSMath.Cos));

                case "Tan": return(new CallableArity1(this, CSMath.Tan));

                case "Asin": return(new CallableArity1(this, CSMath.Asin));

                case "Acos": return(new CallableArity1(this, CSMath.Acos));

                case "Atan": return(new CallableArity1(this, CSMath.Atan));

                case "Sinh": return(new CallableArity1(this, CSMath.Sinh));

                case "Cosh": return(new CallableArity1(this, CSMath.Cosh));

                case "Tanh": return(new CallableArity1(this, CSMath.Tanh));

                case "Asinh": return(new CallableArity1(this, Asinh));                        //doesn't exist in C# yet

                case "Acosh": return(new CallableArity1(this, Acosh));                        //doesn't exist in C# yet

                case "Atanh": return(new CallableArity1(this, Atanh));                        //doesn't exist in C# yet

                default:
                    throw new ErrorHandler.RuntimeError(token, "Unknown property '" + propertyName + "'");
                }
            }
Exemplo n.º 21
0
 // Inverse Hyperbolic Sine
 public static double HArcsin(double x)
 {
     return(MathObj.Log(x + MathObj.Sqrt(x * x + 1)));
 }
Exemplo n.º 22
0
 public static Half Log(Half x,
                        Half y) =>
 (Half)M.Log(x, y);
Exemplo n.º 23
0
 public static double Log(double value1, double value2)
 {
     return(CSMath.Log(value1, value2));
 }
Exemplo n.º 24
0
 // Inverse Hyperbolic Cosine
 public static double HArccos(double x)
 {
     return(MathObj.Log(x + MathObj.Sqrt(x * x - 1)));
 }
Exemplo n.º 25
0
 public static Half Log(Half x) =>
 (Half)M.Log(x);
Exemplo n.º 26
0
 // Inverse Hyperbolic Tangent
 public static double HArctan(double x)
 {
     return(MathObj.Log((1 + x) / (1 - x)) / 2);
 }
Exemplo n.º 27
0
 public static double Log(double d, double p)
 => Math.Log(d, p);
Exemplo n.º 28
0
 // Inverse Hyperbolic Secant
 public static double HArcsec(double x)
 {
     return(MathObj.Log((MathObj.Sqrt(-x * x + 1) + 1) / x));
 }
Exemplo n.º 29
0
 /// <summary>
 /// Represents the log base two of e.
 /// </summary>
 /// <returns>System.Double.</returns>
 public static double Log2E()
 {
     return(NMath.Log(NMath.E, 2));
 }
Exemplo n.º 30
0
 // Inverse Hyperbolic Cosecant
 public static double HArccosec(double x)
 {
     return(MathObj.Log((MathObj.Sign(x) * MathObj.Sqrt(x * x + 1) + 1) / x));
 }