Package org.jquantlib.math.distributions

Examples of org.jquantlib.math.distributions.CumulativeNormalDistribution


        log_H_S = Math.log(strike / spot);

        if (variance >= Math.E) {
            D1 = log_H_S / stdDev + mu * stdDev;
            D2 = D1 - 2.0 * mu * stdDev;
            final CumulativeNormalDistribution f = new CumulativeNormalDistribution();
            cum_d1 = f.op(D1);
            cum_d2 = f.op(D2);
            n_d1 = f.derivative(D1);
            n_d2 = f.derivative(D2);
        } else {
            if (log_H_S > 0) {
                cum_d1 = 1.0;
                cum_d2 = 1.0;
            }
View Full Code Here


                cum_d1 = 1.0;
                cum_d2 = 1.0;
            } else {
                D1 = Math.log(forward / strike) / stdDev + 0.5 * stdDev;
                D2 = D1 - stdDev;
                final CumulativeNormalDistribution f = new CumulativeNormalDistribution();
                cum_d1 = f.op(D1);
                cum_d2 = f.op(D2);
                n_d1 = f.derivative(D1);
                n_d2 = f.derivative(D2);
            }
        } else {
            if (forward > strike) {
                cum_d1 = 1.0;
                cum_d2 = 1.0;
View Full Code Here

        greeks.delta = futureWeight*black.delta(forwardPrice)*forwardPrice/s;
        greeks.gamma = forwardPrice*futureWeight/(s*s)*(black.gamma(forwardPrice)*futureWeight*forwardPrice
                - pastWeight*black.delta(forwardPrice) );

        /*@Real*/ double Nx_1, nx_1;
        final CumulativeNormalDistribution CND = new CumulativeNormalDistribution();
        final NormalDistribution ND = new NormalDistribution();

        if (sigG > Constants.QL_EPSILON) {
            /*@Real*/ final double x_1  = (muG-Math.log(payoff.strike())+variance)/sigG;
            Nx_1 = CND.op(x_1);
            nx_1 = ND.op(x_1);
        } else {
            Nx_1 = (muG > Math.log(payoff.strike()) ? 1.0 : 0.0);
            nx_1 = 0.0;
        }
View Full Code Here

            moreGreeks.thetaPerDay        = black.thetaPerDay(spot, t);
            moreGreeks.strikeSensitivity  = black.strikeSensitivity();
            moreGreeks.itmCashProbability = black.itmCashProbability();
        } else {
            // early exercise can be optimal
            final CumulativeNormalDistribution cumNormalDist = new CumulativeNormalDistribution();
            final double /*@Real*/ tolerance = 1e-6;
            final double /*@Real*/ Sk = criticalPrice(payoff, riskFreeDiscount, dividendDiscount, variance, tolerance);
            final double /*@Real*/ forwardSk = Sk * dividendDiscount / riskFreeDiscount;
            final double /*@Real*/ d1 = (Math.log(forwardSk/payoff.strike()) + 0.5*variance)/Math.sqrt(variance);
            final double /*@Real*/ n = 2.0*Math.log(dividendDiscount/riskFreeDiscount)/variance;
            final double /*@Real*/ K = -2.0*Math.log(riskFreeDiscount)/(variance*(1.0-riskFreeDiscount));
            double /*@Real*/ Q, a;
            switch (payoff.optionType()) {
            case Call:
                Q = (-(n-1.0) + Math.sqrt(((n-1.0)*(n-1.0))+4.0*K))/2.0;
                a =  (Sk/Q) * (1.0 - dividendDiscount * cumNormalDist.op(d1));
                if (spot<Sk)
                    r.value = black.value() + a * Math.pow((spot/Sk), Q);
                else
                    r.value = spot - payoff.strike();
                break;
            case Put:
                Q = (-(n-1.0) - Math.sqrt(((n-1.0)*(n-1.0))+4.0*K))/2.0;
                a = -(Sk/Q) * (1.0 - dividendDiscount * cumNormalDist.op(-d1));
                if (spot>Sk)
                    r.value = black.value() + a * Math.pow((spot/Sk), Q);
                else
                    r.value = payoff.strike() - spot;
                break;
View Full Code Here

        // Newton Raphson algorithm for finding critical price Si
        double /*@Real*/ Q, LHS, RHS, bi;
        double /*@Real*/ forwardSi = Si * dividendDiscount / riskFreeDiscount;
        double /*@Real*/ d1 = (Math.log(forwardSi/payoff.strike()) + 0.5*variance) / Math.sqrt(variance);
        final CumulativeNormalDistribution cumNormalDist = new CumulativeNormalDistribution();
        final double /*@Real*/ K = (riskFreeDiscount!=1.0 ? -2.0*Math.log(riskFreeDiscount)/ (variance*(1.0-riskFreeDiscount)) : 0.0);
        final double /*@Real*/ temp = BlackFormula.blackFormula(payoff.optionType(), payoff.strike(), forwardSi, Math.sqrt(variance))*riskFreeDiscount;
        switch (payoff.optionType()) {
        case Call:
            Q = (-(n-1.0) + Math.sqrt(((n-1.0)*(n-1.0)) + 4 * K)) / 2;
            LHS = Si - payoff.strike();
            RHS = temp + (1 - dividendDiscount * cumNormalDist.op(d1)) * Si / Q;
            bi =  dividendDiscount * cumNormalDist.op(d1) * (1 - 1/Q)
            + (1 - dividendDiscount * cumNormalDist.derivative(d1) / Math.sqrt(variance)) / Q;
            while (Math.abs(LHS - RHS)/payoff.strike() > tolerance) {
                Si = (payoff.strike() + RHS - bi * Si) / (1 - bi);
                forwardSi = Si * dividendDiscount / riskFreeDiscount;
                d1 = (Math.log(forwardSi/payoff.strike())+0.5*variance)/Math.sqrt(variance);
                LHS = Si - payoff.strike();
                final double /*@Real*/ temp2 = BlackFormula.blackFormula(payoff.optionType(), payoff.strike(), forwardSi, Math.sqrt(variance))*riskFreeDiscount;
                RHS = temp2 + (1 - dividendDiscount * cumNormalDist.op(d1)) * Si / Q;
                bi = dividendDiscount * cumNormalDist.op(d1) * (1 - 1 / Q)
                + (1 - dividendDiscount * cumNormalDist.derivative(d1) / Math.sqrt(variance)) / Q;
            }
            break;
        case Put:
            Q = (-(n-1.0) - Math.sqrt(((n-1.0)*(n-1.0)) + 4 * K)) / 2;
            LHS = payoff.strike() - Si;
            RHS = temp - (1 - dividendDiscount * cumNormalDist.op(-d1)) * Si / Q;
            bi = -dividendDiscount * cumNormalDist.op(-d1) * (1 - 1/Q)
            - (1 + dividendDiscount * cumNormalDist.derivative(-d1) / Math.sqrt(variance)) / Q;
            while (Math.abs(LHS - RHS)/payoff.strike() > tolerance) {
                Si = (payoff.strike() - RHS + bi * Si) / (1 + bi);
                forwardSi = Si * dividendDiscount / riskFreeDiscount;
                d1 = (Math.log(forwardSi/payoff.strike())+0.5*variance)/Math.sqrt(variance);
                LHS = payoff.strike() - Si;
                final double /*@Real*/ temp2 = BlackFormula.blackFormula(payoff.optionType(), payoff.strike(), forwardSi, Math.sqrt(variance))*riskFreeDiscount;
                RHS = temp2 - (1 - dividendDiscount * cumNormalDist.op(-d1)) * Si / Q;
                bi = -dividendDiscount * cumNormalDist.op(-d1) * (1 - 1 / Q)
                - (1 + dividendDiscount * cumNormalDist.derivative(-d1) / Math.sqrt(variance)) / Q;
            }
            break;
        default:
            throw new LibraryException(UNKNOWN_OPTION_TYPE); // QA:[RG]::verified
        }
View Full Code Here

            moreGreeks.strikeSensitivity  = black.strikeSensitivity();
            moreGreeks.itmCashProbability = black.itmCashProbability();
        } else {
            // early exercise can be optimal
            final CumulativeNormalDistribution cumNormalDist = new CumulativeNormalDistribution();
            final NormalDistribution normalDist = new NormalDistribution();

            final double /*@Real*/ tolerance = 1e-6;
            final double /*@Real*/ Sk = BaroneAdesiWhaleyApproximationEngine.criticalPrice(payoff, riskFreeDiscount, dividendDiscount, variance, tolerance);

            final double /*@Real*/ forwardSk = Sk * dividendDiscount / riskFreeDiscount;

            final double /*@Real*/ alpha = -2.0*Math.log(riskFreeDiscount)/(variance);
            final double /*@Real*/ beta = 2.0*Math.log(dividendDiscount/riskFreeDiscount)/
            (variance);
            final double /*@Real*/ h = 1 - riskFreeDiscount;
            double /*@Real*/ phi;

            switch (payoff.optionType()) {
            case Call:
                phi = 1;
                break;
            case Put:
                phi = -1;
                break;
            default:
                throw new LibraryException(UNKNOWN_OPTION_TYPE); // QA:[RG]::verified
            }

            // TODO: study how zero interest rate could be handled
            QL.ensure(h != 0.0 , DIVIDING_BY_ZERO_INTEREST_RATE); // QA:[RG]::verified

            final double /* @Real */temp_root = Math.sqrt((beta - 1) * (beta - 1) + (4 * alpha) / h);
            final double /* @Real */lambda = (-(beta - 1) + phi * temp_root) / 2;
            final double /* @Real */lambda_prime = -phi * alpha / (h * h * temp_root);

            final double /* @Real */black_Sk = BlackFormula.blackFormula(payoff.optionType(), payoff.strike(), forwardSk, Math.sqrt(variance)) * riskFreeDiscount;
            final double /* @Real */hA = phi * (Sk - payoff.strike()) - black_Sk;

            final double /* @Real */d1_Sk = (Math.log(forwardSk / payoff.strike()) + 0.5 * variance) / Math.sqrt(variance);
            final double /* @Real */d2_Sk = d1_Sk - Math.sqrt(variance);
            final double /* @Real */part1 = forwardSk * normalDist.op(d1_Sk) / (alpha * Math.sqrt(variance));
            final double /* @Real */part2 = -phi * forwardSk * cumNormalDist.op(phi * d1_Sk) * Math.log(dividendDiscount) / Math.log(riskFreeDiscount);
            final double /* @Real */part3 = +phi * payoff.strike() * cumNormalDist.op(phi * d2_Sk);
            final double /* @Real */V_E_h = part1 + part2 + part3;

            final double /* @Real */b = (1 - h) * alpha * lambda_prime / (2 * (2 * lambda + beta - 1));
            final double /* @Real */c = -((1 - h) * alpha / (2 * lambda + beta - 1)) * (V_E_h / (hA) + 1 / h + lambda_prime / (2 * lambda + beta - 1));
            final double /* @Real */temp_spot_ratio = Math.log(spot / Sk);
            final double /* @Real */chi = temp_spot_ratio * (b * temp_spot_ratio + c);

            if (phi * (Sk - spot) > 0) {
                r.value = black.value() + hA * Math.pow((spot / Sk), lambda) / (1 - chi);
            } else {
                r.value = phi * (spot - payoff.strike());
            }

            final double /* @Real */temp_chi_prime = (2 * b / spot) * Math.log(spot / Sk);
            final double /* @Real */chi_prime = temp_chi_prime + c / spot;
            final double /* @Real */chi_double_prime = 2 * b / (spot * spot) - temp_chi_prime / spot - c / (spot * spot);
            greeks.delta = phi * dividendDiscount * cumNormalDist.op(phi * d1_Sk)
            + (lambda / (spot * (1 - chi)) + chi_prime / ((1 - chi)*(1 - chi))) *
            (phi * (Sk - payoff.strike()) - black_Sk) * Math.pow((spot/Sk), lambda);

            greeks.gamma = phi * dividendDiscount * normalDist.op(phi*d1_Sk) /
            (spot * Math.sqrt(variance))
 
View Full Code Here

    //
    // public constructors
    //

    public AnalyticBarrierEngine(final GeneralizedBlackScholesProcess process) {
        this.f = new CumulativeNormalDistribution();
        this.a = (BarrierOption.ArgumentsImpl)arguments;
        this.r = (BarrierOption.ResultsImpl)results;
        this.process = process;
        this.process.addObserver(this);
    }
View Full Code Here

                  {3.5, 0.9997673709},
                  {4.0, 0.9999683288},
                  {5.0, 0.9999997133}};


    final CumulativeNormalDistribution cnd = new CumulativeNormalDistribution();

    for (final double[] testvalue : testvalues) {
      final double z = testvalue[0];
      final double expected = testvalue[1];
      final double computed = cnd.op(z);
      final double tolerance = (Math.abs(z)<3.01) ? 1.0e-15: 1.0e-10;


      // assertEquals(expected, computed,tolerance);
      if(expected - computed > tolerance){
        fail("expected:  + " + expected + " but was " + computed);
      }

      // assertEquals(1.0, computed+ cnd.evaluate(-z),tolerance);
      if (Math.abs(1.0-(computed+cnd.op(-z)))>tolerance) {
        fail("expected: 1.0" + " but is: " + computed + cnd.op(-z));
      }
    }
  }
View Full Code Here

  public void testExtremes(){
    double z = -40;
    // double tolerance = (Math.abs(z)<3.01) ? 1.0e-15: 1.0e-10;
    final double tolerance = 1.0e-15;

    final CumulativeNormalDistribution cnd = new CumulativeNormalDistribution();

    // assertEquals(0, cnd.evaluate(z),1.0e-15);
    if (Math.abs(0.0-(cnd.op(z)))>tolerance) {
      fail("expected: 1.0" + " but is: " + cnd.op(z));
    }

    z = -10;
    // assertEquals(0, cnd.evaluate(z),1.0e-15);
    if (Math.abs(0.0-cnd.op(z))>tolerance) {
      fail("expected: 1.0" + " but is: " + cnd.op(z));
    }

    z = 10;
    // assertEquals(1.0, cnd.evaluate(z),1.0e-15);
    if (Math.abs(1.0-(cnd.op(z)))>tolerance) {
      fail("expected: 1.0" + " but is: " + cnd.op(z));
    }

    z = 40;
    // assertEquals(1.0, cnd.evaluate(z),1.0e-15);
    if (Math.abs(1.0-(cnd.op(z)))>tolerance) {
      fail("expected: 1.0" + " but is: " + cnd.op(z));
    }
  }
View Full Code Here

        double /* @Real */sigmax() {
            return sigmax_;
        }

        double /* @Real */getOperatorEq(final double /* @Real */x) {
            final CumulativeNormalDistribution phi = new CumulativeNormalDistribution();
            final double /* @Real */temp = (x - mux_) / sigmax_;
            final double /* @Real */txy = Math.sqrt(1.0 - rhoxy_ * rhoxy_);

            final Array lambda = new Array(size_);
            int /* @Size */i;
            for (i = 0; i < size_; i++) {
                final double /* @Real */tau = (i == 0 ? t_.get(0) - T_ : t_.get(i) - t_.get(i - 1));
                final double /* @Real */c = (i == size_ - 1 ? (1.0 + rate_ * tau) : rate_ * tau);
                lambda.set(i, c * A_.get(i) * Math.exp(-Ba_.get(i) * x));
            }

            final SolvingFunction function = new SolvingFunction(lambda, Bb_);
            final Brent s1d = new Brent();
            s1d.setMaxEvaluations(1000);

            final double /* @Real */yb = s1d.solve(function, 1e-6, 0.00, -100.0, 100.0);
            final double /* @Real */h1 = (yb - muy_) / (sigmay_ * txy) - rhoxy_ * (x - mux_) / (sigmax_ * txy);
            // not sure if evaluate method is equivalent of op overloading -> we have to test it ;-)
            double /* @Real */value = /* phi(-w_*h1) */phi.op(-w_ * h1);

            for (i = 0; i < size_; i++) {
                final double /* @Real */h2 = h1 + Bb_.get(i) * sigmay_ * Math.sqrt(1.0 - rhoxy_ * rhoxy_);
                final double /* @Real */kappa = -Bb_.get(i)
                * (muy_ - 0.5 * txy * txy * sigmay_ * sigmay_ * Bb_.get(i) + rhoxy_ * sigmay_ * (x - mux_) / sigmax_);
                // operator overloading problem again
                value -= lambda.get(i) * Math.exp(kappa) * /* phi(-w_*h2) */phi.op(-w_ * h2);
            }

            return Math.exp(-0.5 * temp * temp) * value / (sigmax_ * Math.sqrt(2.0 * Constants.M_PI));
        }
View Full Code Here

TOP

Related Classes of org.jquantlib.math.distributions.CumulativeNormalDistribution

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.