Exemple #1
0
        public double Price()
        {
            double price = 0.00;
            double phi   = (this.StaticData.Type == OptionType.Call) ? 1 : -1;
            double S     = this.StaticData.Spot;
            double K     = this.StaticData.Strike;
            double r     = this.StaticData.RiskFreeRate;
            double sigma = this.StaticData.Volatility;
            double ttm   = this.StaticData.TimeToMaturity;

            if (ttm > 0.00)
            {
                double d1 = (Math.Log(S / K) + (r + sigma * sigma / 2) * ttm) / (sigma * Math.Sqrt(ttm));
                double d2 = d1 - sigma * Math.Sqrt(ttm);

                Normal normal = new Normal();
                double N1     = normal.CDF(phi * d1);
                double N2     = normal.CDF(phi * d2);

                price = phi * N1 * S - phi * N2 * K * Math.Exp(-r * ttm);
            }
            else
            {
                price = Math.Max(phi * (S - K), 0);
            }

            return(price);
        }
Exemple #2
0
        public void TestMethod()
        {
            float  stddev = 0.1f;
            Normal normal = new Normal(stddev);
            double mean   = 0;

            Assert.AreEqual(0.95, normal.CDF(mean, mean + (2 * stddev)) - normal.CDF(mean, mean - (2 * stddev)), 0.01);
        }
Exemple #3
0
    static void Main()
    {
        // Create an XML reader for this file.
        using (XmlReader reader = XmlReader.Create(new StringReader(
                                                       @"<Data><Bars log_return=""-0.00870""/><Bars log_return=""-0.00840""/><Bars log_return=""0.00000""/></Data>")))
        {
            var samples = new List <double>();

            while (reader.Read())
            {
                if (reader.IsStartElement() && reader.Name == "Bars")
                {
                    samples.Add(double.Parse(reader.GetAttribute("log_return")));
                }
            }

            GetKurtosis(@"<Data><Bars log_return=""-0.00870""/><Bars log_return=""-0.00840""/><Bars log_return=""0.00000""/></Data>");
            var statistic = new Normal();

            statistic.Samples();

            Console.WriteLine(Statistics.Kurtosis(samples));
            Console.WriteLine(statistic.Skewness);
            Console.WriteLine(Normal.PDF(statistic.Mean, statistic.StdDev, 0.9));
            Console.WriteLine(Normal.CDF(statistic.Mean, statistic.StdDev, 0.9));
        }
    }
        /// <summary>
        /// Determine the value of the caplet using the Black-Caplet-formula
        /// </summary>
        /// <returns></returns>
        public double Value(YieldCurve yieldCurve, double delta, Func <double, double> volatilityFunc)
        {
            // valuation date
            var valuationDate = yieldCurve.SettleDate;

            var T = Utilities.ConvertToDayCountFraction(valuationDate, FixingDate);

            Func <double, double> func = t => volatilityFunc(T - t) * volatilityFunc(T - t);

            var sigma = Math.Sqrt(1 / T * Integrate.OnClosedInterval(func, 0, T));

            var tenor = Utilities.ConvertToDayCountFraction(FixingDate, ExpiryDate);
            var bond  = yieldCurve.GetDiscountFactors(new [] { FixingDate, ExpiryDate });

            var kappa = CapRate / (Tenor * Principal) + delta;

            var spotForwardRate = 1 / tenor * (bond[0] / bond[1] - 1);

            var logLK  = Math.Log((spotForwardRate + delta) / (kappa));
            var gamma  = T * sigma * sigma;
            var dPlus  = (logLK + 0.5 * gamma) / Math.Sqrt(gamma);
            var dMinus = (logLK - 0.5 * gamma) / Math.Sqrt(gamma);

            return(bond[1] * Principal * Tenor * (spotForwardRate * Normal.CDF(0, 1, dPlus) - kappa * Normal.CDF(0, 1, dMinus)));
        }
Exemple #5
0
        private static double CalculateAnalyticPV(
            double initialPrice,
            double strike,
            double maturity,
            double domesticRate,
            double[] foreignRate,
            double[] volatility,
            double barrier,
            bool isCall)
        {
            int signCall = isCall ? 1 : -1;

            Func <double, int, double> CalcD = (threshold, signBS) =>
            {
                double d =
                    (Math.Log(initialPrice / threshold) + (domesticRate - foreignRate[0]) * maturity)
                    / (volatility[0] * Math.Sqrt(maturity));
                return(d + signBS * 0.5 * volatility[0] * Math.Sqrt(maturity));
            };
            double dPlus         = CalcD(strike, 1);
            double dMinus        = CalcD(strike, -1);
            double dBarrierPlus  = CalcD(barrier, 1);
            double dBarrierMinus = CalcD(barrier, -1);

            double foreignProbability  = Normal.CDF(0, 1, signCall * dPlus) - Normal.CDF(0, 1, signCall * dBarrierPlus);
            double domesticProbability = Normal.CDF(0, 1, signCall * dMinus) - Normal.CDF(0, 1, signCall * dBarrierMinus);

            return
                (signCall * initialPrice * Math.Exp(-foreignRate[0] * maturity) * foreignProbability
                 - signCall * strike * Math.Exp(-domesticRate * maturity) * domesticProbability);
        }
Exemple #6
0
        private double BarrierOptionPricer()
        {
            Func <double, double> CDF = x => Normal.CDF(0, 1, x);
            Func <double, double> PDF = x => Normal.PDF(0, 1, x);

            var res = 0.0;
            var dfq = Math.Exp(-dividend * maturity);
            var dfr = Math.Exp(-rate * maturity);
            var z   = volatility * Math.Sqrt(maturity);
            var l   = (rate - dividend + Math.Pow(volatility, 2) / 2) / Math.Pow(volatility, 2);
            var y   = Math.Log(Math.Pow(barrier, 2) / (initialStock * strike)) / z + l * z;

            if (barrier < initialStock & type == Type.call & barrier < strike)
            {
                switch (knock)
                {
                case Knock.In: res = initialStock * dfq * Math.Pow(barrier / initialStock, 2 * l) * CDF(y)
                                     - strike * dfr * Math.Pow(barrier / initialStock, 2 * l - 2) * CDF(y + z);
                    break;

                case Knock.Out:
                }
            }
            return(res);
        }
Exemple #7
0
        private static double CalculateAnalyticPV(
            double[] initialPrice,
            double maturity,
            double[] foreignRate,
            double[] volatility,
            double correlation)
        {
            Func <double, double, double> CalcD = (threshold, scale) =>
            {
                double crossVolatility =
                    Math.Sqrt(
                        volatility[0] * volatility[0]
                        + volatility[1] * volatility[1]
                        - 2 * correlation * volatility[0] * volatility[1]);
                double d =
                    (Math.Log(initialPrice[1] * Math.Exp(-foreignRate[1] * maturity) / threshold))
                    / (crossVolatility * Math.Sqrt(maturity));
                return(d + scale * crossVolatility * Math.Sqrt(maturity));
            };
            double dPlus  = CalcD(initialPrice[0] * Math.Exp(-foreignRate[0] * maturity), 0.5);
            double dMinus = CalcD(initialPrice[0] * Math.Exp(-foreignRate[0] * maturity), -0.5);

            return
                (initialPrice[1] * Math.Exp(-foreignRate[1] * maturity) * Normal.CDF(0, 1, dPlus)
                 - initialPrice[0] * Math.Exp(-foreignRate[0] * maturity) * Normal.CDF(0, 1, dMinus));
        }
Exemple #8
0
        /**
         * Wald confidence interval for binomial proportions (similar to above) with optional agresti-coull corrections for better coverage
         */
        public static WaldCIResult WaldCI(double s1, double n1, double s2, double n2, double conf = 0.95, bool acCorrect = true)
        {
            double p1hat = 0;
            double p2hat = 0;

            if (acCorrect)
            {
                n1   += 2;
                n2   += 2;
                p1hat = (s1 + 1) / n1;
                p2hat = (s2 + 1) / n2;
            }
            else
            {
                p1hat = s1 / n1;
                p2hat = s2 / n2;
            }

            double z    = Math.Abs(Normal.InvCDF(Mean, StandardDeviation, (1 - conf) / 2));
            double p1se = Math.Sqrt(p1hat * (1 - p1hat) / n1);
            double p2se = Math.Sqrt(p2hat * (1 - p2hat) / n2);

            double pdiff  = p2hat - p1hat;
            double sediff = Math.Sqrt(Math.Pow(p1se, 2) + Math.Pow(p2se, 2));
            double zscore = pdiff / sediff;

            WaldCIResult result = new WaldCIResult();

            result.lift = pdiff / p1hat;
            result.ll   = (pdiff - z * sediff) / p1hat;
            result.ul   = (pdiff + z * sediff) / p1hat;
            result.pval = FixPValue(Normal.CDF(Mean, StandardDeviation, zscore));

            return(result);
        }
Exemple #9
0
        public void ValidateCumulativeDistribution(double x, double f)
        {
            var n = Normal.WithMeanStdDev(5.0, 2.0);

            AssertHelpers.AlmostEqual(f, n.CumulativeDistribution(x), 10);
            AssertHelpers.AlmostEqual(f, Normal.CDF(5.0, 2.0, x), 10);
        }
Exemple #10
0
        public static double BlackScholes(string cpflg, double S, double X, double T, double r, double b, double v)
        {
            double d1    = 0;
            double d2    = 0;
            double price = double.NaN;

            if (T == 0)
            {
                if (cpflg.Equals("c"))
                {
                    price = Math.Max(S - X, 0);
                }
                else
                {
                    price = Math.Max(X - S, 0);
                }
                return(price);
            }

            d1 = (Math.Log(S / X) + (b + v * v / 2) * T) / (v * Math.Sqrt(T));
            d2 = d1 - v * Math.Sqrt(T);
            if (cpflg.Equals("c"))
            {
                price = S * Math.Exp((b - r) * T) * Normal.CDF(0, 1, d1) - X * Math.Exp(-r * T) * Normal.CDF(0, 1, d2);
            }
            if (cpflg.Equals("p"))
            {
                price = X * Math.Exp(-r * T) * Normal.CDF(0, 1, -d2) - S * Math.Exp((b - r) * T) * Normal.CDF(0, 1, -d1);
            }
            return(price);
        }
        /// <summary>
        /// Computes theta.
        /// </summary>
        /// <param name="optionType">call or put</param>
        /// <param name="S">Underlying price</param>
        /// <param name="K">Strike price</param>
        /// <param name="T">Time to expiration in % of year</param>
        /// <param name="sigma">Volatility</param>
        /// <param name="r">continuously compounded risk-free interest rate</param>
        /// <param name="q">continuously compounded dividend yield</param>
        /// <returns></returns>
        public double Theta(OptionContractType optionType, double S, double K, double T, double sigma, double r, double q)
        {
            double d1 = D1(S, K, T, sigma, r, q);
            double d2 = D2(T, sigma, d1);

            switch (optionType)
            {
            case OptionContractType.Call:
            {
                double theta = -Math.Exp(-q * T) * (S * Normal.PDF(0, 1, d1) * sigma) / (2.0 * Math.Sqrt(T))
                               - (r * K * Math.Exp(-r * T) * Normal.CDF(0, 1, d2))
                               + q * S * Math.Exp(-q * T) * Normal.CDF(0, 1, d1);

                return(theta / 365);
            }

            case OptionContractType.Put:
            {
                double theta = -Math.Exp(-q * T) * (S * Normal.PDF(0, 1, d1) * sigma) / (2.0 * Math.Sqrt(T))
                               + (r * K * Math.Exp(-r * T) * Normal.PDF(0, 1, -d2))
                               - q * S * Math.Exp(-q * T) * Normal.CDF(0, 1, -d1);

                return(theta / 365);
            }

            default:
                throw new NotSupportedException();
            }
        }
Exemple #12
0
        public double GetTheta(DateTime currentDate,
                               OptionPricingData pricingData)
        {
            var timePeriod = GetTimePeriodToExpiry(currentDate);
            var d1         = GetD1(timePeriod, pricingData);
            var d2         = GetD2(timePeriod, pricingData);

            if (IsCall)
            {
                var term1 = -Math.Exp(-pricingData.DivYield * timePeriod) * pricingData.CurrentPrice * Normal.PDF(0, 1, d1)
                            * pricingData.Vol / (2 * Math.Sqrt(timePeriod));
                var term2 = pricingData.InterestRate * Strike * Math.Exp(-pricingData.InterestRate * timePeriod)
                            * Normal.CDF(0, 1, d2);
                var term3 = pricingData.DivYield * pricingData.CurrentPrice * Math.Exp(-pricingData.DivYield * timePeriod)
                            * Normal.CDF(0, 1, d1);
                var theta = term1 - term2 + term3;

                return(theta / TimePeriods.BusinessDaysInYear);
            }
            else
            {
                var term1 = -Math.Exp(-pricingData.DivYield * timePeriod) * pricingData.CurrentPrice * Normal.PDF(0, 1, -d1)
                            * pricingData.Vol / (2 * Math.Sqrt(timePeriod));
                var term2 = pricingData.InterestRate * Strike * Math.Exp(-pricingData.InterestRate * timePeriod)
                            * (Normal.CDF(0, 1, d2) - 1);
                var term3 = pricingData.DivYield * pricingData.CurrentPrice * Math.Exp(-pricingData.DivYield * timePeriod)
                            * (Normal.CDF(0, 1, d1) - 1);
                var theta = term1 - term2 + term3;

                return(theta / TimePeriods.BusinessDaysInYear);
            }
        }
Exemple #13
0
        static public decimal Theta(OptionType type, double s, double x, double r, double q, double sigma, int days)
        {
            double t  = Convert.ToDouble(days) / 365;
            double d1 = D1(s, x, r, q, sigma, t);
            double d2 = D2(d1, sigma, t);

            if (t > 0)
            {
                switch (type)
                {
                case OptionType.Call:
                {
                    double theta = -s *sigma *Math.Exp(-q *t) * Normal.PDF(0, 1, d1) / 2 / Math.Sqrt(t);

                    theta -= (r * x * Math.Exp(-r * t) * Normal.CDF(0, 1, d2));
                    theta += (q * s * Math.Exp(-q * t) * Normal.CDF(0, 1, d1));
                    return(Convert.ToDecimal(theta / 365));
                }

                case OptionType.Put:
                {
                    double theta = -s *sigma *Math.Exp(-q *t) * Normal.PDF(0, 1, d1) / 2 / Math.Sqrt(t);

                    theta += (r * x * Math.Exp(-r * t) * Normal.CDF(0, 1, -d2));
                    theta -= (q * s * Math.Exp(-q * t) * Normal.CDF(0, 1, -d1));
                    return(Convert.ToDecimal(theta / 365));
                }
                }
            }
            return(0);
        }
 static private double CalcDelta(
     double x,
     double delta0,
     double tau)
 {
     return(Normal.CDF(0, 1, x / (delta0 * Math.Sqrt(tau))));
 }
        public static double AutoCallable_smooth_pricer(double S0, double r, double b,
                                                        double vol, double[] fixings, double remained_T, double total_T, double ko_price, double coupon, double rebate, double funding,
                                                        double annpay, int nsims, int seed)
        {
            //annpay 0 stands for absolute,1 stands for annualized
            int             nsteps      = (int)Math.Round(remained_T * 252);
            Vector <double> payoff_vec1 = Vector <double> .Build.Dense(nsims, 0.0);

            Vector <double> payoff_vec2 = Vector <double> .Build.Dense(nsims, 0.0);

            double dt          = 1 / 252.0;
            double passed_time = total_T - remained_T;

            //check if the fixing days are valid
            if (!validate_fixing(fixings))
            {
                double error_result = double.NaN;
                return(error_result);
            }
            //get fixing days in days number
            int[]         fixing_ko_days = getFixingDays(fixings);
            System.Random rnd            = new System.Random(seed);
            for (int i = 0; i < nsims; i++)
            {
                double dW1, dW2;
                double s1 = S0;
                double s2 = S0;
                double p_not_out1, p_not_out2;
                double L1 = 1;
                double L2 = 1;
                double uniform_rand;
                foreach (int j in fixing_ko_days)
                {
                    //the day before ko observation day
                    dW1             = Normal.Sample(rnd, 0, 1);
                    dW2             = -dW1;
                    s1             *= Math.Exp((b - 0.5 * vol * vol) * dt * (j - 1) + vol * Math.Sqrt(dt * (j - 1)) * dW1);
                    s2             *= Math.Exp((b - 0.5 * vol * vol) * dt * (j - 1) + vol * Math.Sqrt(dt * (j - 1)) * dW2);
                    p_not_out1      = Normal.CDF(0, 1, (Math.Log(ko_price / s1) - (b - Math.Pow(vol, 2) / 2) * dt) / (vol * Math.Sqrt(dt)));
                    p_not_out2      = Normal.CDF(0, 1, (Math.Log(ko_price / s2) - (b - Math.Pow(vol, 2) / 2) * dt) / (vol * Math.Sqrt(dt)));
                    payoff_vec1[i] += (1 - p_not_out1) * L1 * (coupon * (Math.Pow(passed_time + j * dt, annpay)) - funding * (passed_time + j * dt))
                                      * Math.Exp(-r * j * dt);
                    payoff_vec2[i] += (1 - p_not_out2) * L2 * (coupon * (Math.Pow(passed_time + j * dt, annpay)) - funding * (passed_time + j * dt))
                                      * Math.Exp(-r * j * dt);
                    L1          *= p_not_out1;
                    L2          *= p_not_out2;
                    uniform_rand = ContinuousUniform.Sample(rnd, 0, 1);
                    dW1          = Normal.InvCDF(0, 1, p_not_out1 * uniform_rand);
                    dW2          = Normal.InvCDF(0, 1, p_not_out2 * (1 - uniform_rand));
                    s1          *= Math.Exp((b - 0.5 * vol * vol) * dt + vol * Math.Sqrt(dt) * dW1);
                    s2          *= Math.Exp((b - 0.5 * vol * vol) * dt + vol * Math.Sqrt(dt) * dW2);
                }
                payoff_vec1[i] += (rebate * Math.Pow(passed_time + fixings.Last(), annpay) * Math.Exp(-r * nsteps / 252.0) - funding * (passed_time + fixings.Last())
                                   * Math.Exp(-r * remained_T)) * L1;
                payoff_vec2[i] += (rebate * Math.Pow(passed_time + fixings.Last(), annpay) * Math.Exp(-r * nsteps / 252.0) - funding * (passed_time + fixings.Last())
                                   * Math.Exp(-r * remained_T)) * L2;
            }
            return(payoff_vec1.Average() / 2 + payoff_vec2.Average() / 2);
        }
 public void ObtenerFrecuenciaEsperada(TablaDeFrecuencia tabla)
 {
     for (int i = 0; i < tabla.Intervalos.Length; i++)
     {
         tabla.Intervalos[i].FrecuenciaEsperada = (Normal.CDF(media, Math.Sqrt(varianza), tabla.Intervalos[i].Maximo) -
                                                   Normal.CDF(media, Math.Sqrt(varianza), tabla.Intervalos[i].Minimo)) * tabla.Cantidad;
     }
 }
Exemple #17
0
        internal static double Black76CallOptionDeltaUndiscounted(Day valDate, double forwardPrice, double impliedVol,
                                                                  double strikePrice, Day expiryDate)
        {
            double timeToExpiry        = (expiryDate - valDate) / 365.0;
            double volRootTimeToExpiry = impliedVol * Math.Sqrt(timeToExpiry);
            double d1 = D1(forwardPrice, impliedVol, strikePrice, timeToExpiry, volRootTimeToExpiry);

            return(Normal.CDF(0, 1, d1));
        }
        public static double TestStatic(IList <double> sample_0, IList <double> sample_1)
        {
            //U = AUC∗nP∗nN
            //int positive_count = ToolsCollection.CountOccurance(labels, true);
            double wilcoxon_statistic = ComputeRankSumStatistic(sample_0, sample_1);
            double z_value            = ComputeZTransform(sample_0.Count, sample_1.Count, wilcoxon_statistic);

            return(1 - Normal.CDF(0.0, 1.0, z_value));
        }
Exemple #19
0
        public double PriceBinaryCallOption()
        {
            double d2 = (Math.Log(S / K) + (r - 0.5 * Math.Pow(v, 2) * T) / (v * Math.Sqrt(T)));


            double value = Math.Exp(-r * T) * Normal.CDF(0, 1, d2);

            return(value);
        }
Exemple #20
0
        public static double TestStatic(IList <double> sample_0, IList <double> sample_1)
        {
            if (sample_0.Count != sample_1.Count)
            {
                throw new Exception("Sample sizes do not match");
            }
            double statistic = ComputeSingedRankPairedStatistic(sample_0, sample_1);
            double z_value   = ComputeZTransform(sample_0.Count, statistic);

            return(1 - Normal.CDF(0.0, 1.0, z_value));
        }
 static public double CalcOptionPrice(
     InitialTerm.InitialParameter param,
     double forward,
     double strike,
     double d1,
     double d2,
     InitialTerm.CallPutType type)
 {
     return((int)type * forward * Math.Exp(-param.rateOfForeign * param.tau)
            * Normal.CDF(0, 1, (int)type * d1)
            - strike * Normal.CDF(0, 1, (int)type * d2));
 }
Exemple #22
0
        public void CalculatePremium()
        {
            CalculateD1();
            CalculateD2();

            if (D1 == 0 || D2 == 0)
            {
                Premium = Math.Max(Strike - Spot, 0);
            }
            else
            {
                Premium = Spot * Normal.CDF(0, 1, D1) -
                          Math.Exp(-Interest * Time) * Strike * Normal.CDF(0, 1, D2);
            }
        }
Exemple #23
0
        public double GetDelta(DateTime currentDate,
                               OptionPricingData pricingData)
        {
            var timePeriod = GetTimePeriodToExpiry(currentDate);
            var d1         = GetD1(timePeriod, pricingData);

            if (IsCall)
            {
                return(Math.Exp(-pricingData.DivYield * timePeriod) * Normal.CDF(0, 1, d1));
            }
            else
            {
                return(Math.Exp(-pricingData.DivYield * timePeriod) * (Normal.CDF(0, 1, d1) - 1));
            }
        }
Exemple #24
0
        static public decimal Delta(OptionType type, double s, double x, double r, double q, double sigma, int days)
        {
            double t  = Convert.ToDouble(days) / 365;
            double d1 = D1(s, x, r, q, sigma, t);

            switch (type)
            {
            case OptionType.Call:
                return(Convert.ToDecimal(Math.Exp(-r * t) * Normal.CDF(0, 1, d1)));

            case OptionType.Put:
                return(Convert.ToDecimal(-Math.Exp(-r * t) * (Normal.CDF(0, 1, d1) - 1)));
            }
            return(0);
        }
Exemple #25
0
        /// <summary>
        /// Creates the distribution by using the given mean and deviation value.
        /// </summary>
        /// <param name="allBuckets">All buckets.</param>
        /// <param name="mean">The mean for the normal distribution.</param>
        /// <param name="deviation">The standard deviation for the normal distribution.</param>
        public virtual Dictionary <double, double> CreateDistribution(List <double> allBuckets, double mean, double deviation)
        {
            Dictionary <double, double> result = new Dictionary <double, double>();

            double previousResultCache = 0;

            for (int i = 0; i < allBuckets.Count; i++)
            {
                double currentResult = Normal.CDF(mean, deviation, allBuckets[i]);
                result[allBuckets[i]] = currentResult - previousResultCache;
                previousResultCache   = currentResult;
            }

            return(DistributionUtils.AdjustToOne(result));
        }
Exemple #26
0
        // Calculate market implied volatility, by inverting Black-Scholes formula, throught Newton-Raphson method
        public double GetImpliedVolatility(OptionType type, double price, double spot, double strike, double riskFreeRate, double timeToMaturity, double volatilityGuess, double relativeTolerance, int maxAttempts)
        {
            double phi          = (type == OptionType.Call) ? 1 : -1;
            double S            = spot;
            double K            = strike;
            double r            = riskFreeRate;
            double ttm          = timeToMaturity;
            double currentError = 999999999;
            double previousIV   = volatilityGuess;
            double currentIV    = previousIV;

            int i = 0;

            while (i < maxAttempts && currentError > relativeTolerance)
            {
                double[] d           = this.GetDs(ttm, S, K, r, previousIV);
                double[] firstOrderD = this.GetFirstOrderDs(ttm, S, K, r, previousIV);

                Normal normal = new Normal();
                double cdf1   = normal.CDF(phi * d[0]);
                double cdf2   = normal.CDF(phi * d[1]);
                double f      = phi * S * cdf1 - phi * K * Math.Exp(-r * ttm) * cdf2 - price;

                double pdf1        = normal.PDF(phi * d[0]);
                double pdf2        = normal.PDF(phi * d[1]);
                double firstOrderF = S * pdf1 * firstOrderD[0] - K * Math.Exp(-r * ttm) * pdf2 * firstOrderD[1];

                currentIV    = previousIV - f / firstOrderF;
                currentError = Math.Abs(currentIV - previousIV) / Math.Abs(previousIV);

                previousIV = currentIV;
                i++;
            }

            return(currentIV);
        }
Exemple #27
0
        static public decimal Price(OptionType type, double s, double x, double r, double q, double sigma, int days)
        {
            double t  = Convert.ToDouble(days) / 365;
            double d1 = D1(s, x, r, q, sigma, t);
            double d2 = D2(d1, sigma, t);

            switch (type)
            {
            case OptionType.Call:
                return(Convert.ToDecimal(s * Math.Exp(-q * t) * Normal.CDF(0, 1, d1) - x * Math.Exp(-r * t) * Normal.CDF(0, 1, d2)));

            case OptionType.Put:
                return(Convert.ToDecimal(x * Math.Exp(-r * t) * Normal.CDF(0, 1, -d2) - s * Math.Exp(-q * t) * Normal.CDF(0, 1, -d1)));
            }
            return(0);
        }
        /// <summary>
        /// Computes delta.
        /// </summary>
        /// <param name="optionType">call or put</param>
        /// <param name="S">Underlying price</param>
        /// <param name="K">Strike price</param>
        /// <param name="T">Time to expiration in % of year</param>
        /// <param name="sigma">Volatility</param>
        /// <param name="r">continuously compounded risk-free interest rate</param>
        /// <param name="q">continuously compounded dividend yield</param>
        /// <returns></returns>
        public double Delta(OptionContractType optionType, double S, double K, double T, double sigma, double r, double q)
        {
            double d1 = D1(S, K, T, sigma, r, q);

            switch (optionType)
            {
            case OptionContractType.Call:
                return(Math.Exp(-r * T) * Normal.CDF(0, 1, d1));

            case OptionContractType.Put:
                return(-Math.Exp(-r * T) * Normal.CDF(0, 1, -d1));

            default:
                throw new NotSupportedException();
            }
        }
Exemple #29
0
        internal static double Black76CallOptionValue(Day valDate, double forwardPrice, double impliedVol, double interestRate,
                                                      double strikePrice, Day expiryDate, Day settlementDate)
        {
            double discountFactor = DiscountFactor(valDate, interestRate, settlementDate);

            double timeToExpiry        = (expiryDate - valDate) / 365.0;
            double volRootTimeToExpiry = impliedVol * Math.Sqrt(timeToExpiry);

            double d1 = D1(forwardPrice, impliedVol, strikePrice, timeToExpiry, volRootTimeToExpiry);
            double d2 = d1 - volRootTimeToExpiry;

            double callOptionValue =
                discountFactor * (forwardPrice * Normal.CDF(0, 1, d1) - strikePrice * Normal.CDF(0, 1, d2));

            return(callOptionValue);
        }
Exemple #30
0
        public static double Delta(string cpflg, double S, double X, double T, double r, double b, double v)
        {
            double d1 = 0;

            d1 = (Math.Log(S / X) + (b + v * v / 2) * T) / (v * Math.Sqrt(T));
            double delta = double.NaN;

            if (cpflg.Equals("c"))
            {
                delta = Math.Exp((b - r) * T) * Normal.CDF(0, 1, d1);
            }
            if (cpflg.Equals("p"))
            {
                delta = -Math.Exp((b - r) * T) * Normal.CDF(0, 1, -d1);
            }
            return(delta);
        }