Package org.jquantlib.math.matrixutilities

Examples of org.jquantlib.math.matrixutilities.Array


     */
    @Test
    public void testNonRestrictiveHymanFilter() {
        final int n = 4;

        Array x, y;
        x = xRange(-2.0, 2.0, n);
        y = parabolic(x);
        final double zero=0.0;
        double interpolated;
        final double expected=0.0;
View Full Code Here


              for (int k_b=0; k_b<isBetaFixed.length; ++k_b) {
                for (int k_n=0; k_n<isNuFixed.length; ++k_n) {
                  for (int k_r=0; k_r<isRhoFixed.length; ++k_r) {
//FIXME: uncomment
                    final SABRInterpolation sabrInterpolation = new SABRInterpolation(
                            new Array(strikes), new Array(volatilities),
                            expiry, forward,
                            alphaGuess, betaGuess, nuGuess, rhoGuess,
                            isAlphaFixed[k_a], isBetaFixed[k_b],
                            isNuFixed[k_n], isRhoFixed[k_r],
                            vegaWeighted[i],
View Full Code Here

        final double dx = (finish-start)/(size-1);
        for(int i=0; i<size-1; i++) {
            x[i]=start+i*dx;
        }
        x[size-1] = finish;
        return new Array(x);
    }
View Full Code Here

            int guessIt = 0;
            for (int i = initialIndex; guessIt < x.size(); ++guessIt, ++i) {
                traits.updateGuess (ts.data(), x.get (guessIt), i);
            }
            interpolation.update();
            final Array penalties = new Array (penaltylocalisation);
            int instIterator = rateHelpersStart;
            for (int penIt = 0; instIterator != rateHelpersEnd; ++ instIterator, ++ penIt) {
                penalties.set (penIt, Math.abs (instruments[instIterator].quoteError()));
            }
            return penalties;
        }
View Full Code Here

        final double[] y = new double[x.size()];
        for(int i=0; i<x.size(); i++) {
            final double value = x.get(i);
            y[i] = exp(-value*value);
        }
        return new Array(y);
    }
View Full Code Here

        final double[] y = new double[x.size()];
        for(int i=0; i<x.size(); i++) {
            final double value = x.get(i);
            y[i] = -value*value;
        }
        return new Array(y);
    }
View Full Code Here

    private void checkNotAKnotCondition(
            final String type,
            final CubicInterpolation spline) {
        final double tolerance = 1.0e-14;

        final Array c = spline.cCoefficients();
        assertFalse(type+" interpolation failure"
                    +"\n    cubic coefficient of the first polinomial is "+c.get(0)
                    +"\n    cubic coefficient of the second polinomial is "+c.get(1),
                    abs(c.get(0)-c.get(1)) > tolerance);
        final int n=c.size();
        assertFalse(type+" interpolation failure"
                +"\n    cubic coefficient of the 2nd to last polinomial is "+c.get(n-2)
                +"\n    cubic coefficient of the last polinomial is "+c.get(n-1),
                abs(c.get(n-2)-c.get(n-1)) > tolerance);

    }
View Full Code Here

        yMinExpected_.add(xMinExpected[yMinExpected.length-1]);
        costFunctions_.add(new OneDimensionalPolynomDegreeN(coefficients));
        // Set Constraint for optimizers: unconstrained problem
        constraints_.add(new NoConstraint());
        // Set initial guess for optimizer
        final Array initialValue = new Array();
        initialValue.add(-100.0);
        initialValues_.add(initialValue);
        // Set end criteria for optimizer
        maxIterations_.add(1000); // maxIterations
        maxStationaryStateIterations_.add(100); // MaxStationaryStateIterations
        rootEpsilons_.add(1e-8); // rootEpsilon
        functionEpsilons_.add(1e-16); // functionEpsilon
        gradientNormEpsilons_.add(1e-8); // gradientNormEpsilon
        endCriterias_.add(new EndCriteria(maxIterations_.get(maxIterations_.size() - 1), maxStationaryStateIterations_
                .get(maxStationaryStateIterations_.size() - 1), rootEpsilons_.get(rootEpsilons_.size() - 1), functionEpsilons_
                .get(functionEpsilons_.size() - 1), gradientNormEpsilons_.get(gradientNormEpsilons_.size() - 1)));
        // Set optimization methods for optimizer
        final OptimizationMethodType optimizationMethodTypes[] = { OptimizationMethodType.simplex };/* OptimizationMethodType.levenbergMarquardt};*/
        final double simplexLambda = 0.1;
        final double levenbergMarquardtEpsfcn = Math.pow(10, -0.8); //FIXME: how to write this as 1e-0.8???
        final double levenbergMarquardtXtol = Math.pow(10, -0.8); //FIXME: how to write this as 1e-0.8???
        final double levenbergMarquardtGtol = Math.pow(10, -0.8); //FIXME: how to write this as 1e-0.8???

        optimizationMethods_.add(makeOptimizationMethods(optimizationMethodTypes, simplexLambda, levenbergMarquardtEpsfcn, levenbergMarquardtXtol, levenbergMarquardtGtol));
        // Set expected results for optimizer

        for (int i=0; i<costFunctions_.size(); ++i) {
            final Problem problem = new Problem(costFunctions_.get(i), constraints_.get(i), initialValues_.get(i));
            for (int j=0; j<(optimizationMethods_.get(i)).size(); ++j) {
                final EndCriteria.Type endCriteriaResult = optimizationMethods_.get(i).get(j).minimize(problem, endCriterias_.get(i));
            final Array xMinCalculated = problem.currentValue();
            final Array yMinCalculated = problem.values(xMinCalculated);
            // Check optimizatin results vs known solution
            for (int k=0; k < xMinCalculated.size(); ++k) {
                //if(Math.abs(yMinExpected_.get(k)- yMinCalculated.get(k))> functionEpsilons_.get(i)){
                //if (std::fabs(yMinExpected_[k]- yMinCalculated[k]) > functionEpsilons_[i]) {
                if (true) {
                    System.out.println(
                            "costFunction = " + String.valueOf(i) + "\n"
                          + "optimizer =  " +  j + "\n"
                          + "    x expected:    " +  xMinExpected_.get(k) + "\n"
                          + "    x calculated:  " +  xMinCalculated.get(k) + "\n"
                          + "    x difference:  " ((Double)xMinExpected_.get(k)- xMinCalculated.get(k)) + "\n"
                          + "    rootEpsilon:   " +  rootEpsilons_.get(i) + "\n"
                          + "    y expected:    " +  yMinExpected_.get(k) + "\n"
                          + "    y calculated:  " +  yMinCalculated.get(k) + "\n"
                          + "    y difference:  " ((Double)yMinExpected_.get(k)- yMinCalculated.get(k)) + "\n"
                          + "    functionEpsilon:   " +  functionEpsilons_.get(i) + "\n"
                          + "    endCriteriaResult:  " + endCriteriaResult);
                    }

                }
View Full Code Here

        for (int iteration = 0;; ++iteration) {
            // only read safe to use as a reference
            final double previousData[] = data.clone(); // TODO: verify if clone() is needed
            // restart from the previous interpolation
            if (validCurve) {
                ts.setInterpolation(interpolator.interpolate(new Array(times), new Array(data)));
            }

            for (int i=1; i<n+1; ++i) {
                /*
                for (int k = 0; k < data.size(); ++ k)
                {
                    StringBuilder sb = new StringBuilder ();
                    sb.append ("Date: ");
                    sb.append (dates[k]);
                    sb.append ("\t Time: ");
                    sb.append (df.format (times.get (k)));
                    sb.append ("\t Discount: ");
                    sb.append (df.format (data.get(k)));
                    QL.debug (sb.toString ());
                }
                */

                // calculate guess before extending interpolation
                // to ensure that any extrapolation is performed
                // using the curve bootstrapped so far and no more
                final RateHelper instrument = instruments[i-1];
                double guess = 0.0;
                if (validCurve|| iteration>0) {
                    guess = ts.data()[i];
                } else if (i==1) {
                    guess = traits.initialGuess();
                } else {
                    // most traits extrapolate
                    guess = traits.guess(ts, dates[i]);
                }

                //QL.debug (" Guess : " + ((Double)(guess)).toString());

                // bracket
                final double min = traits.minValueAfter(i, data);
                final double max = traits.maxValueAfter(i, data);

                if (guess <= min || guess >= max) {
                    guess = (min + max) / 2.0;
                }

                if (! validCurve && iteration == 0) {
                    // extend interpolation a point at a time
                    try {
                        ts.setInterpolation(interpolator.interpolate (new Array(times, i+1), new Array(data)));
                    } catch (final Exception e) {
                        // no chance to fix it in a later iteration
                        if (ts.interpolator().global()) {
                            throw new LibraryException("no chance to fix it in a later iteration");
                        }

                        // otherwise, if the target interpolation is not usable yet
                        ts.setInterpolation(new Linear().interpolate (new Array(times, i+1), new Array(data)));
                    }
                }
                // required because we just changed the data
                // is it really required?
                ts.interpolation().update();

                try {
                    final BootstrapError error = new BootstrapError(traits, ts, instrument, i);
                    final double r = solver.solve (error, ts.accuracy(), guess, min, max);
                    // redundant assignment (as it has been already performed
                    // by BootstrapError in solve procedure), but safe
                    data[i] = r;
                } catch (final Exception e) {
                    validCurve = false;
                    QL.error ("could not bootstrap");
                }
            }

            if (!interpolator.global ()) {
                break; // no need for convergence loop
            } else if (!validCurve && iteration == 0) {
                // ensure the target interpolation is used
                ts.setInterpolation(interpolator.interpolate (new Array(times), new Array(data)));

                // at least one more iteration is needed to check convergence
                continue;
            }

View Full Code Here

            // nothing
        }

        QL.info("Testing use of interpolations as functors...");

        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 LinearInterpolation(x, y);
        f.update();

        final Array x2 = new Array(new double[] { -2.0, -1.0, 0.0, 1.0, 3.0, 4.0, 5.0, 6.0, 7.0 });
        final int N = x2.size();

        final double tolerance = 1.0e-12;

        // case 1: extrapolation not allowed
        try {
            final Array y2 = x2.clone().transform(f);
            System.out.println(y2);
            throw new NotThrown();
        } catch (final NotThrown ex) {
            throw new LibraryException("failed to throw exception when trying to extrapolate");
        } catch (final Exception ex) {
            // as expected, we are OK.
            System.out.println(ex);
        }

        // case 2: enable extrapolation
        f.enableExtrapolation();
        final Array y2 = x2.clone().transform(f);
        System.out.println(y2);
        for (int i=0; i<N-1; i++) {
            final double expected = 5.0 - x2.get(i);
            final double calculated = y2.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);
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.