Package org.jquantlib.math.matrixutilities

Examples of org.jquantlib.math.matrixutilities.Array


    @Test
    public void testBackwardFlat() {

        QL.info("Testing backward-flat interpolation...");

        final Array x = new Array(new double[] { 0.0, 1.0, 2.0, 3.0, 4.0 });
        final Array y = new Array(new double[] { 5.0, 4.0, 3.0, 2.0, 1.0 });

        final Interpolation f = new BackwardFlatInterpolation(x, y);
        f.update();

        final int N = x.size();
        final double tolerance = 1.0e-12;

        // at original points
        for (int i=0; i<N; i++) {
            final double p = x.get(i);
            final double calculated = f.op(p);
            final double expected = y.get(i);
            assertFalse("failed to reproduce expected datum"
                    +"\n    expected value:   "+expected
                    +"\n    calculated value: "+calculated
                    +"\n    error:              "+Math.abs(calculated-expected),
                    Math.abs(calculated - expected) > tolerance);
        }

        // at middle points
        for (int i=0; i<N-1; i++) {
            final double p = (x.get(i)+x.get(i+1))/2;
            final double calculated = f.op(p);
            final double expected = y.get(i+1);
            assertFalse("failed to reproduce expected datum"
                    +"\n    expected value:   "+expected
                    +"\n    calculated value: "+calculated
                    +"\n    error:              "+Math.abs(calculated-expected),
                    Math.abs(calculated - expected) > tolerance);
        }

        // outside the original range
        f.enableExtrapolation();

        {
            // this is just a block
            double p = x.get(0) - 0.5;
            double calculated = f.op(p);
            double expected = y.get(0);
            assertFalse("failed to reproduce expected datum"
                    +"\n    expected value:   "+expected
                    +"\n    calculated value: "+calculated
                    +"\n    error:              "+Math.abs(calculated-expected),
                    Math.abs(calculated - expected) > tolerance);

            p = x.get(N-1) + 0.5;
            calculated = f.op(p);
            expected = y.get(N-1);
            assertFalse("failed to reproduce expected datum"
                    +"\n    expected value:   "+expected
                    +"\n    calculated value: "+calculated
                    +"\n    error:              "+Math.abs(calculated-expected),
                    Math.abs(calculated - expected) > tolerance);

            // primitive at original points
            calculated = f.primitive(x.get(0));
            expected = 0.0;
            assertFalse("failed to reproduce expected datum"
                    +"\n    expected value:   "+expected
                    +"\n    calculated value: "+calculated
                    +"\n    error:              "+Math.abs(calculated-expected),
                    Math.abs(calculated - expected) > tolerance);
        }

        double sum = 0.0;
        for (int i=1; i<N-1; i++) {
            sum += (x.get(i) - x.get(i-1)) * y.get(i);
            final double calculated = f.primitive(x.get(i));
            final double expected = sum;
            assertFalse("failed to reproduce expected datum"
                    +"\n    expected value:   "+expected
                    +"\n    calculated value: "+calculated
                    +"\n    error:              "+Math.abs(calculated-expected),
                    Math.abs(calculated - expected) > tolerance);
        }

        // primitive at middle points
        sum = 0.0;
        for (int i=0; i<N-1; i++) {
            final double p = (x.get(i) + x.get(i+1))/2;
            sum += (x.get(i+1) - x.get(i)) * y.get(i+1)/2;
            final double calculated = f.primitive(p);
            final double expected = sum;
            sum += (x.get(i+1)-x.get(i))*y.get(i+1)/2;
            assertFalse("failed to reproduce expected datum"
                    +"\n    expected value:   "+expected
                    +"\n    calculated value: "+calculated
                    +"\n    error:              "+Math.abs(calculated-expected),
                    Math.abs(calculated - expected) > tolerance);
View Full Code Here


    @Override
    public Array volatility(final double t, final Array x) {
        QL.require((t < startTimes_.first() || t > startTimes_.last()) , "invalid time given for volatility model"); // QA:[RG]::verified // TODO: message
        final int ti = (int) (startTimes_.upperBound(t) - startTimes_.first() - 1);

        final Array tmp = new Array(size_);

        for (int i = ti; i < size_; ++i)
            tmp.set(i, volatilities_.get(i - ti));
        final Array ret = new Array();
        for (int i = 0; i < tmp.size(); i++)
            ret.set(i, tmp.get(i));
        return ret;
    }
View Full Code Here

    @Override
    // TODO: review iterators
    public Matrix diffusion(/*@Time*/ final double t, final Array x){
        final Matrix pca = corrModel_.pseudoSqrt(t, x);
        // TODO: code review :: use of clone()
        final Array  vol = volaModel_.volatility(t, x);
        for (int i=0; i<size_; ++i) {
            pca.rangeRow(i).mulAssign(vol.get(i));
        }
        return pca;
    }
View Full Code Here

    }

    @Override
    public Matrix covariance(/* @Time */final double t, final Array x) {
        // TODO: code review :: use of clone()
        final Array volatility = volaModel_.volatility(t, x);
        final Matrix correlation = corrModel_.correlation(t, x);

        final Matrix tmp = new Matrix(size_, size_);
        for (int i = 0; i < size_; ++i) {
            for (int j = 0; j < size_; ++j) {
                tmp.set(i, j, volatility.get(i) * correlation.get(i, j) * volatility.get(j));
            }
        }

        return tmp;
    }
View Full Code Here

        return correlation(t, x).get(i, j);
    }

    public double correlation(final int i, final int j, /* @Time */final double t) {
        // inefficient implementation, please overload in derived classes
        return correlation(t, new Array()).get(i, j);
    }
View Full Code Here

    public abstract Matrix correlation(
    /* @Time */double t, final Array x);

    public Matrix correlation(
    /* @Time */final double t) {
        return correlation(t, new Array());
    }
View Full Code Here

    public boolean isTimeIndependent() {
        return false;
    }

    public double volatility(final int i, final double t){
        return volatility(t, new Array()).get(i);
    }
View Full Code Here

    public double volatility(final int i, final double t, final Array x){
        return volatility(t, x).get(i);
    }

    public Array volatility(final double t) {
        return volatility(t, new Array());
    }
View Full Code Here

      rho_ = rho;
      alphaIsFixed_ = false;
      betaIsFixed_ = false;
      nuIsFixed_ = false;
      rhoIsFixed_ = false;
      weights_ = new Array();
      error_ = Constants.NULL_REAL;
      maxError_ = Constants.NULL_REAL;
      SABREndCriteria_ = EndCriteria.Type.None;

      QL.require(t > 0.0, "expiry time must be positive: " + t + " not allowed");
View Full Code Here

        optMethod_ = new Simplex(0.01);
            }
      if (endCriteria_ != null) {
        endCriteria_ = new EndCriteria(60000, 100, 1e-8, 1e-8, 1e-8);
      }
      itsCoeffs.weights_ = new Array(vx.size());
      for (int i = 0; i < itsCoeffs.weights_.size(); i++) {
                itsCoeffs.weights_.set(i, 1.0 / vx.size());
            }
    }
View Full Code Here

TOP

Related Classes of org.jquantlib.math.matrixutilities.Array

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.