Package com.opengamma.analytics.math

Examples of com.opengamma.analytics.math.MathException


        f2 = f1;
        f1 = f.evaluate(temp);
      }
      i++;
      if (i > MAX_ITER) {
        throw new MathException("Could not find minimum: this should not happen because minimum should have been successfully bracketed");
      }
    }
    if (f1 < f2) {
      return x1;
    }
View Full Code Here


  public void update(final QuasiNewtonVectorMinimizer.DataBundle data) {
    final DoubleMatrix1D hDeltaGrad = (DoubleMatrix1D) MA.multiply(data.getInverseHessianEsimate(), data.getDeltaGrad());
    final double deltaXdeltaGrad = MA.getInnerProduct(data.getDeltaX(), data.getDeltaGrad());
    final double deltaGradHdeltaGrad = MA.getInnerProduct(data.getDeltaGrad(), hDeltaGrad);
    if (deltaXdeltaGrad == 0.0) {
      throw new MathException("The dot product of the change in position and the change in gradient is zero");
    }
    if (deltaGradHdeltaGrad == 0.0) {
      throw new MathException("Most likely the change in gradient is zero - should have exited");
    }

    final DoubleMatrix2D tempMat1 = MA.getOuterProduct(MA.scale(data.getDeltaX(), 1.0 / deltaXdeltaGrad), data.getDeltaX());
    final DoubleMatrix2D tempMat2 = MA.getOuterProduct(MA.scale(hDeltaGrad, -1.0 / deltaGradHdeltaGrad), hDeltaGrad);
View Full Code Here

        final double temp = Math.log(1 - z);
        res[0] = -z / temp;
        res[1] = -FunctionUtils.cube(z) / 2 / FunctionUtils.square((1 - z) * temp);
        res[2] = -1 / temp - z / (1 - z) / temp / temp;
      } else {
        throw new MathException("can't handle z=1, rho=1");
      }
      return res;
    }
    final double rhoHat = 1 + rho;
    if (CompareUtils.closeEquals(rhoHat, 0.0, RHO_EPS)) {
      if (z > -1) {
        final double temp = Math.log(1 + z);
        final double temp2 = temp * temp;
        res[0] = z / temp;
        res[1] = ((2 * z + 1) / 2 / FunctionUtils.square(1 + z) - 1 / rhoStar) * z / temp2;
        res[2] = 1 / temp - z / (1 + z) / temp2;
      } else if (z < -1) {
        if (rhoHat == 0) {
          res[0] = 0;
          final double chi = Math.log(RHO_EPS) - Math.log(-(1 + z) / rhoStar);
          final double chiRho = 1 / RHO_EPS + 1 / rhoStar - FunctionUtils.square(z / (1 + z));
          res[1] = -chiRho * z / chi / chi; //should be +infinity
          res[2] = 0.0;
        } else {
          final double chi = Math.log(rhoHat) - Math.log(-(1 + z) / rhoStar);
          res[0] = z / chi;
          final double chiRho = 1 / rhoHat + 1 / rhoStar - FunctionUtils.square(z / (1 + z));
          res[1] = -chiRho * z / chi / chi;
          res[2] = 1 / chi + z / chi / chi / (1 + z);
        }
      } else {
        throw new MathException("can't handle z=-1, rho=-1");
      }
      return res;
    }

    //now the non-edge case
View Full Code Here

  public void update(final DataBundle data) {
    final DoubleMatrix1D hDeltaGrad = (DoubleMatrix1D) MA.multiply(data.getInverseHessianEsimate(), data.getDeltaGrad());
    final double deltaXdeltaGrad = MA.getInnerProduct(data.getDeltaX(), data.getDeltaGrad());
    final double deltaGradHdeltaGrad = MA.getInnerProduct(data.getDeltaGrad(), hDeltaGrad);
    if (deltaXdeltaGrad == 0.0) {
      throw new MathException("The dot product of the change in position and the change in gradient is zero");
    }
    if (deltaGradHdeltaGrad == 0.0) {
      throw new MathException("Most likely the change in gradient is zero - should have exited");
    }

    final DoubleMatrix2D tempMat1 = MA.getOuterProduct(MA.scale(data.getDeltaX(), 1.0 / deltaXdeltaGrad), data.getDeltaX());
    final DoubleMatrix2D tempMat2 = MA.getOuterProduct(MA.scale(hDeltaGrad, -1.0 / deltaGradHdeltaGrad), hDeltaGrad);
View Full Code Here

    final double halfX = x / 2.0;
    final double logX = Math.log(halfX);
    try {
      regGammaStart = Gamma.regularizedGammaP(_dofOverTwo + _k, halfX);
    } catch (final org.apache.commons.math.MathException ex) {
      throw new MathException(ex);
    }

    double sum = _pStart * regGammaStart;
    double oldSum = Double.NEGATIVE_INFINITY;
    double p = _pStart;
View Full Code Here

      DoubleMatrix1D deltaTheta;
      try {
        decmp = _decomposition.evaluate(alpha);
        deltaTheta = decmp.solve(beta);
      } catch (final Exception e) {
        throw new MathException(e);
      }

      DoubleMatrix1D trialTheta = (DoubleMatrix1D) _algebra.add(theta, deltaTheta);

      // if the new value of theta is not in the model domain keep increasing lambda until an acceptable step is found
      if (!allowedValue.evaluate(trialTheta)) {
        lambda = increaseLambda(lambda);
        continue;
      }

      newError = getError(func, observedValues, sigma, trialTheta);
      p = getANorm(penalty, trialTheta);
      newChiSqr = getChiSqr(newError);
      newChiSqr += p;

      // Check for convergence when no improvement in chiSqr occurs
      if (Math.abs(newChiSqr - oldChiSqr) / (1 + oldChiSqr) < _eps) {

        final DoubleMatrix2D alpha0 = lambda == 0.0 ? alpha : getModifiedCurvatureMatrix(jacobian, 0.0, penalty);

        if (lambda > 0.0) {
          decmp = _decomposition.evaluate(alpha0);
        }
        return finish(alpha0, decmp, newChiSqr, jacobian, trialTheta, sigma);
      }

      if (newChiSqr < oldChiSqr) {
        lambda = decreaseLambda(lambda);
        theta = trialTheta;
        error = newError;
        jacobian = getJacobian(jac, sigma, trialTheta);
        beta = getChiSqrGrad(error, jacobian);
        temp = (DoubleMatrix1D) _algebra.multiply(penalty, theta);
        beta = (DoubleMatrix1D) _algebra.subtract(beta, temp);

        oldChiSqr = newChiSqr;
      } else {
        lambda = increaseLambda(lambda);
      }
    }
    throw new MathException("Could not converge in " + MAX_ATTEMPTS + " attempts");
  }
View Full Code Here

      DoubleMatrix1D deltaTheta;
      try {
        decmp = _decomposition.evaluate(alpha);
        deltaTheta = decmp.solve(beta);
      } catch (final Exception e) {
        throw new MathException(e);
      }

      DoubleMatrix1D trialTheta = (DoubleMatrix1D) _algebra.add(theta, deltaTheta);

      //if the new value of theta is not in the model domain or the jump is too large, keep increasing lambda until an acceptable step is found
      if (!constraints.evaluate(trialTheta) || !allowJump(deltaTheta, maxJumps)) {
        lambda = increaseLambda(lambda);
        continue;
      }

      newError = getError(func, observedValues, sigma, trialTheta);
      newChiSqr = getChiSqr(newError);

      //Check for convergence when no improvement in chiSqr occurs
      if (Math.abs(newChiSqr - oldChiSqr) / (1 + oldChiSqr) < _eps) {

        final DoubleMatrix2D alpha0 = lambda == 0.0 ? alpha : getModifiedCurvatureMatrix(jacobian, 0.0);

        //if the model is an exact fit to the data, then no more improvement is possible
        if (newChiSqr < _eps) {
          if (lambda > 0.0) {
            decmp = _decomposition.evaluate(alpha0);
          }
          return finish(alpha0, decmp, newChiSqr, jacobian, trialTheta, sigma);
        }

        final SVDecompositionCommons svd = (SVDecompositionCommons) DecompositionFactory.SV_COMMONS;

        //add the second derivative information to the Hessian matrix to check we are not at a local maximum or saddle point
        final VectorFieldSecondOrderDifferentiator diff = new VectorFieldSecondOrderDifferentiator();
        final Function1D<DoubleMatrix1D, DoubleMatrix2D[]> secDivFunc = diff.differentiate(func, constraints);
        final DoubleMatrix2D[] secDiv = secDivFunc.evaluate(trialTheta);
        final double[][] temp = new double[nParms][nParms];
        for (int i = 0; i < nObs; i++) {
          for (int j = 0; j < nParms; j++) {
            for (int k = 0; k < nParms; k++) {
              temp[j][k] -= newError.getEntry(i) * secDiv[i].getEntry(j, k) / sigma.getEntry(i);
            }
          }
        }
        final DoubleMatrix2D newAlpha = (DoubleMatrix2D) _algebra.add(alpha0, new DoubleMatrix2D(temp));

        final SVDecompositionResult svdRes = svd.evaluate(newAlpha);
        final double[] w = svdRes.getSingularValues();
        final DoubleMatrix2D u = svdRes.getU();
        final DoubleMatrix2D v = svdRes.getV();

        final double[] p = new double[nParms];
        boolean saddle = false;

        double sum = 0.0;
        for (int i = 0; i < nParms; i++) {
          double a = 0.0;
          for (int j = 0; j < nParms; j++) {
            a += u.getEntry(j, i) * v.getEntry(j, i);
          }
          final int sign = a > 0.0 ? 1 : -1;
          if (w[i] * sign < 0.0) {
            sum += w[i];
            w[i] = -w[i];
            saddle = true;
          }
        }

        //if a local maximum or saddle point is found (as indicated by negative eigenvalues), move in a direction that is a weighted
        //sum of the eigenvectors corresponding to the negative eigenvalues
        if (saddle) {
          lambda = increaseLambda(lambda);
          for (int i = 0; i < nParms; i++) {
            if (w[i] < 0.0) {
              final double scale = 0.5 * Math.sqrt(-oldChiSqr * w[i]) / sum;
              for (int j = 0; j < nParms; j++) {
                p[j] += scale * u.getEntry(j, i);
              }
            }
          }
          final DoubleMatrix1D direction = new DoubleMatrix1D(p);
          deltaTheta = direction;
          trialTheta = (DoubleMatrix1D) _algebra.add(theta, deltaTheta);
          int i = 0;
          double scale = 1.0;
          while (!constraints.evaluate(trialTheta)) {
            scale *= -0.5;
            deltaTheta = (DoubleMatrix1D) _algebra.scale(direction, scale);
            trialTheta = (DoubleMatrix1D) _algebra.add(theta, deltaTheta);
            i++;
            if (i > 10) {
              throw new MathException("Could not satify constraint");
            }
          }

          newError = getError(func, observedValues, sigma, trialTheta);
          newChiSqr = getChiSqr(newError);

          int counter = 0;
          while (newChiSqr > oldChiSqr) {
            //if even a tiny move along the negative eigenvalue cannot improve chiSqr, then exit
            if (counter > 10 || Math.abs(newChiSqr - oldChiSqr) / (1 + oldChiSqr) < _eps) {
              LOGGER.warn("Saddle point detected, but no improvement to chi^2 possible by moving away. It is recommended that a different starting point is used.");
              return finish(newAlpha, decmp, oldChiSqr, jacobian, theta, sigma);
            }
            scale /= 2.0;
            deltaTheta = (DoubleMatrix1D) _algebra.scale(direction, scale);
            trialTheta = (DoubleMatrix1D) _algebra.add(theta, deltaTheta);
            newError = getError(func, observedValues, sigma, trialTheta);
            newChiSqr = getChiSqr(newError);
            counter++;
          }
        } else {
          //this should be the normal finish - i.e. no improvement in chiSqr and at a true minimum (although there is no guarantee it is not a local minimum)
          return finish(newAlpha, decmp, newChiSqr, jacobian, trialTheta, sigma);
        }
      }

      if (newChiSqr < oldChiSqr) {
        lambda = decreaseLambda(lambda);
        theta = trialTheta;
        error = newError;
        jacobian = getJacobian(jac, sigma, trialTheta);
        beta = getChiSqrGrad(error, jacobian);

        // check for convergence
        //        if (_algebra.getNorm2(beta) < _eps * g0) {
        //          return finish(newChiSqr, jacobian, trialTheta, sigma);
        //        }
        oldChiSqr = newChiSqr;
      } else {
        lambda = increaseLambda(lambda);
      }
    }
    throw new MathException("Could not converge in " + MAX_ATTEMPTS + " attempts");
  }
View Full Code Here

        counts.put(count, x1[i - 1]);
        count = 1;
      }
    }
    if (counts.lastKey() == 1) {
      throw new MathException("Could not find mode for array; no repeated values");
    }
    return counts.lastEntry().getValue();
  }
View Full Code Here

   * @param n the number of terms in the double[] cs
   * @return the evaluated series
   */
  public static double getDCSEVL(double x, double[] cs, int n) {
    if (cs == null) {
      throw new MathException("DCSEVL: cs is null");
    }
    if (n < 1) {
      throw new MathException("DCSEVL: number of terms < 0");
    }
    if (n > 1000) {
      throw new MathException("DCSEVL: number of terms > 1000");
    }
    if (Math.abs(x) > s_onepl) {
      throw new MathException("DCSEVL: x outside of the interval [-1,+1)");
    }
    if (n > cs.length) {
      throw new MathException("DCSEVL: number of terms to compute greater than number of coefficients given (n>cs.length)");
    }
    double b2, b1, b0, twoX;
    b2 = 0;
    b1 = 0;
    b0 = 0;
View Full Code Here

   * @param eta usually 10% of machine precision, arbitrary!
   * @return the number of terms needed in the series to achieve the desired accuracy
   */
  public static int getInitds(double[] os, int nos, double eta) {
    if (os == null) {
      throw new MathException("INITDS: os is null");
    }
    if (nos != os.length) {
      throw new MathException("INITDS: nos and os.length are not equal, they should be!");
    }
    if (nos < 1) {
      throw new MathException("INITDS: Number of coeffs is less than 1");
    }
    double err = 0;
    int i = 0;
    boolean error = true;
    for (int ii = 0; ii < nos; ii++) {
      i = nos - 1 - ii;
      err += Math.abs(os[i]); // Not quite what F77 things, no cast to float.
      if (err > eta) {
        error = false;
        break;
      }
    }
    if (error) {
      throw new MathException("INITDS: Chebyshev series too short for specified accuracy");
    }
    return i;
  }
View Full Code Here

TOP

Related Classes of com.opengamma.analytics.math.MathException

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.