Example usage for org.apache.commons.math3.optim MaxEval MaxEval

List of usage examples for org.apache.commons.math3.optim MaxEval MaxEval

Introduction

In this page you can find the example usage for org.apache.commons.math3.optim MaxEval MaxEval.

Prototype

public MaxEval(int max) 

Source Link

Usage

From source file:com.itemanalysis.psychometrics.irt.equating.IrtScaleLinking.java

public void computeCoefficients() {
    raschFamily = checkRaschModel();//w  ww .j a  v a 2s.  c  om

    ms.setPrecision(precision);
    mm.setPrecision(precision);
    hb.setPrecision(precision);
    sl.setPrecision(precision);

    if (raschFamily) {

        double[] sv = { mm.getIntercept() };

        UnivariateOptimizer underlying = new BrentOptimizer(1e-10, 1e-14);
        JDKRandomGenerator g = new JDKRandomGenerator();

        //Haebara method
        MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 5, g);//Five random starts to Brent optimizer.
        UnivariatePointValuePair hbPair = optimizer.optimize(new MaxEval(500),
                new UnivariateObjectiveFunction(hb), GoalType.MINIMIZE, new SearchInterval(-4, 4),
                new InitialGuess(sv));
        hb.setIntercept(hbPair.getPoint());
        hb.setScale(1.0);
        fHB = hbPair.getValue();

        //Stocking-Lord method
        UnivariatePointValuePair slPair = optimizer.optimize(new MaxEval(500),
                new UnivariateObjectiveFunction(sl), GoalType.MINIMIZE, new SearchInterval(-4, 4),
                new InitialGuess(sv));
        sl.setIntercept(slPair.getPoint());
        sl.setScale(1.0);
        fSL = slPair.getValue();

    } else {

        double[] hbStartValues = { mm.getIntercept(), mm.getScale() };
        double[] slStartValues = { mm.getIntercept(), mm.getScale() };

        if (useUncmin) {
            DefaultUncminOptimizer optimizer = new DefaultUncminOptimizer();

            try {

                optimizer.minimize(hb, hbStartValues);
                double[] param = optimizer.getParameters();
                fHB = optimizer.getFunctionValue();
                hb.setIntercept(param[0]);

                if (param.length > 1) {
                    hb.setScale(param[1]);
                } else {
                    hb.setScale(1.0);//Rasch family of models
                }

                optimizer.minimize(sl, slStartValues);
                param = optimizer.getParameters();
                fSL = optimizer.getFunctionValue();
                sl.setIntercept(param[0]);

                if (param.length > 1) {
                    sl.setScale(param[1]);
                } else {
                    sl.setScale(1.0);//Rasch family of models
                }

            } catch (UncminException ex) {
                ex.printStackTrace();
            }
        } else {

            int numIterpolationPoints = 2 * 2;//two dimensions A and B
            BOBYQAOptimizer underlying = new BOBYQAOptimizer(numIterpolationPoints);
            RandomGenerator g = new JDKRandomGenerator();
            RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(2,
                    new GaussianRandomGenerator(g));
            MultiStartMultivariateOptimizer optimizer = new MultiStartMultivariateOptimizer(underlying, 10,
                    generator);
            PointValuePair hbOptimum = optimizer.optimize(new MaxEval(1000), new ObjectiveFunction(hb),
                    GoalType.MINIMIZE, SimpleBounds.unbounded(2), new InitialGuess(hbStartValues));

            double[] hbCoefficients = hbOptimum.getPoint();
            hb.setIntercept(hbCoefficients[0]);
            hb.setScale(hbCoefficients[1]);
            fHB = hbOptimum.getValue();

            PointValuePair slOptimum = optimizer.optimize(new MaxEval(1000), new ObjectiveFunction(sl),
                    GoalType.MINIMIZE, SimpleBounds.unbounded(2), new InitialGuess(slStartValues));

            double[] slCoefficients = slOptimum.getPoint();
            sl.setIntercept(slCoefficients[0]);
            sl.setScale(slCoefficients[1]);
            fSL = slOptimum.getValue();

        }

    }

}

From source file:de.thkwalter.et.ortskurve.Ausgleichsproblem.java

/**
 * Diese Methode lst das Ausgleichsproblem.
 * /*from  www  . j a va 2 s . c om*/
 * @param startpunkt Der Startpunkt der Ausgleichsrechnung
 * @param ausgleichsproblemtyp Der Typ des Ausgleichsproblems
 * 
 * @return Die berechnete Ortskurve
 */
public Ortskurve ausgleichsproblemLoesen(double[] startpunkt, Ausgleichsproblemtyp ausgleichsproblemtyp) {
    // Die Referenzen auf die Modellgleichungen und die Jakobimatrix werden deklariert.
    MultivariateVectorFunction modellgleichungen = null;
    MultivariateMatrixFunction jakobiMatrix = null;

    // Falls das zweidimensionale Ausgleichsproblem gelst werden soll, ...
    if (ausgleichsproblemtyp == Ausgleichsproblemtyp.ORTSKURVE_2d) {
        // Die Modellgleichungen (die Kreisgleichungen) werden erzeugt.
        modellgleichungen = new Modellgleichungen2d(this.messpunkte);

        // Die Jakobi-Matrix der Modellgleichungen (der Kreisgleichungen) wird erzeugt.
        jakobiMatrix = new Jakobimatrix2d(this.messpunkte);
    }

    // Falls das dreidimensionale Ausgleichsproblem gelst werden soll, ...
    else if (ausgleichsproblemtyp == Ausgleichsproblemtyp.ORTSKURVE_3d) {
        // Die Modellgleichungen (die Kreisgleichungen) werden erzeugt.
        modellgleichungen = new Modellgleichungen(this.messpunkte);

        // Die Jakobi-Matrix der Modellgleichungen (der Kreisgleichungen) wird erzeugt.
        jakobiMatrix = new Jakobimatrix(this.messpunkte);
    }

    // Das Ausgleichsproblem wird gelst, wobei hchstens 200 Iterationsschritte durchgefhrt werden.
    double[] ortskurvenparameter = null;
    try {
        PointVectorValuePair endParameter = this.gaussNewtonOptimizer.optimize(new Weight(gewichte),
                new Target(zielwerte), new InitialGuess(startpunkt), new MaxEval(200),
                new ModelFunction(modellgleichungen), new ModelFunctionJacobian(jakobiMatrix));

        ortskurvenparameter = endParameter.getPoint();
    }

    // Falls das Verfahren nicht konvergiert hat, wird die entsprechende Ausnahme gefangen.
    catch (TooManyEvaluationsException e) {
        // Die Fehlermeldung fr den Entwickler wird erzeugt und protokolliert.
        String fehlermeldung = "Der Gauss-Newton-Algorithmus konvergiert nicht!";
        Ausgleichsproblem.logger.severe(fehlermeldung);

        // Die Ausnahme wird erzeugt und mit der Fehlermeldung fr den Benutzer initialisiert.
        String jsfMeldung = "Der Gauss-Newton-Algorithmus zur Berechnung der Ortskurve konvergiert nicht! "
                + "berprfen Sie bitte, ob die eingegebenen Punkte annhernd auf einem Kreis liegen.";
        ApplicationRuntimeException applicationRuntimeException = new ApplicationRuntimeException(jsfMeldung);

        throw applicationRuntimeException;
    }

    // Die Referenz auf die Ortskurve wird deklariert.
    Ortskurve ortskurve = null;

    // Falls das zweidimensionale Ausgleichsproblem gelst werden soll, ...
    if (ausgleichsproblemtyp == Ausgleichsproblemtyp.ORTSKURVE_2d) {
        ortskurve = new Ortskurve(new Vector2D(ortskurvenparameter[0], 0.0), ortskurvenparameter[1]);
    }

    // Falls das dreidimensionale Ausgleichsproblem gelst werden soll, ...
    else if (ausgleichsproblemtyp == Ausgleichsproblemtyp.ORTSKURVE_3d) {
        ortskurve = new Ortskurve(new Vector2D(ortskurvenparameter[0], ortskurvenparameter[1]),
                ortskurvenparameter[2]);
    }

    // Die berechnete Ortskurve wird zurckgegeben.
    return ortskurve;
}

From source file:net.sf.tweety.math.opt.solver.ApacheCommonsCMAESOptimizer.java

@Override
public Map<Variable, Term> solve(ConstraintSatisfactionProblem problem) throws GeneralMathException {
    // only optimization problems
    if (!(problem instanceof OptimizationProblem))
        throw new IllegalArgumentException("Only optimization problems allowed for this solver.");
    OptimizationProblem p = (OptimizationProblem) problem;
    // only box constraints allowed (so far)
    if (!p.isEmpty())
        throw new IllegalArgumentException(
                "Only optimization problems with box constraints on variables allowed for this solver (no other constraints.");
    final List<Variable> vars = new ArrayList<Variable>(p.getTargetFunction().getVariables());
    double[] lb = new double[vars.size()];
    double[] ub = new double[vars.size()];
    double[] s = new double[vars.size()];
    double[] sigma = new double[vars.size()];
    for (int i = 0; i < vars.size(); i++) {
        lb[i] = vars.get(i).getLowerBound();
        ub[i] = vars.get(i).getUpperBound();
        s[i] = (lb[i] + ub[i]) / 2;//  w  w  w .  j av  a  2 s . c  om
        sigma[i] = ub[i] - lb[i];
    }
    final Term targetFunction = p.getTargetFunction();
    MultivariateFunction target = new MultivariateFunction() {
        @Override
        public double value(double[] arg0) {
            return targetFunction.replaceAllTerms(arg0, vars).doubleValue();
        }
    };
    // construct solver
    CMAESOptimizer optimizer = new CMAESOptimizer(this.maxIterations, this.stopFitness, this.isActiveCMA,
            this.diagonalOnly, this.checkFeasableCount, new JDKRandomGenerator(), false,
            new SimplePointChecker<PointValuePair>(this.precision, this.precision));
    PointValuePair val = optimizer.optimize(new CMAESOptimizer.Sigma(sigma), new ObjectiveFunction(target),
            new InitialGuess(s),
            p.getType() == OptimizationProblem.MAXIMIZE ? GoalType.MAXIMIZE : GoalType.MINIMIZE,
            new MaxEval(this.maxIterations), new SimpleBounds(lb, ub),
            new CMAESOptimizer.PopulationSize(this.populationSize));
    Map<Variable, Term> result = new HashMap<Variable, Term>();
    for (int i = 0; i < vars.size(); i++)
        result.put(vars.get(i), new FloatConstant(val.getPoint()[i]));
    return result;
}

From source file:eu.crisis_economics.abm.algorithms.optimization.BrentLineSearch.java

/**
  * Perform a line search minimization. This function accepts as input:
  *   (a) a starting point (a vector),//from  www  . j av a  2s.  c  o  m
  *   (b) a direction in which to travel (a vector),
  *   (c) limits on the total distance to travel along (b).
  *   
  * With these inputs the function attempts to find the minimum of a
  * scalar-valued multivariate function along the line starting at 
  * (a) and pointing in the direction of (b).
  * 
  * @param function
  *        A scalar-valued multivariate function to minimize,
  * @param startingPoint
  *        A vector starting point from which to begin the minimization (P),
  * @param vectorDirection
  *        A vector direction along which to travel from P, (V)
  * @param maximumDistanceToTravel
  *        The maximum distance to travel in the direction of V,
  * @param maximumEvaluations
  *        The maximum number of function evaluations to identify the minimum,
  * @param relativeErrorGoal
  *        The relative error target of the minimization,
  * @param absoluteErrorGoal
  *        The absolute error target of the minimization.
  * @return
  *        A lightweight immutable struct containing the vector solution and
  *        the evaluation of the function at this point.
  */
static public LineSearchResult doLineSearch(final MultivariateFunction function, final double[] startingPoint,
        final double[] vectorDirection, final double maximumDistanceToTravel, final int maximumEvaluations,
        final double relativeErrorGoal, final double absoluteErrorGoal) {
    Preconditions.checkArgument(maximumEvaluations > 0);
    Preconditions.checkArgument(relativeErrorGoal > 0. || absoluteErrorGoal > 0.);
    Preconditions.checkArgument(maximumDistanceToTravel > 0.);
    final LineSearchObjectiveFunction lineSearcher = new LineSearchObjectiveFunction(function, startingPoint,
            vectorDirection);
    final UnivariateOptimizer optimizer = new BrentOptimizer(relativeErrorGoal, absoluteErrorGoal);
    UnivariatePointValuePair result = optimizer.optimize(new MaxEval(maximumEvaluations),
            new UnivariateObjectiveFunction(lineSearcher), GoalType.MINIMIZE,
            new SearchInterval(0, maximumDistanceToTravel, 0));
    final double[] vectorSolution = new double[startingPoint.length];
    for (int i = 0; i < vectorDirection.length; ++i)
        vectorSolution[i] = lineSearcher.startingPoint[i]
                + lineSearcher.normalizedDirection[i] * result.getPoint();
    final LineSearchResult solution = new LineSearchResult(vectorSolution, result.getValue());
    return solution;
}

From source file:com.itemanalysis.psychometrics.irt.equating.HaebaraMethodTest.java

/**
 * Item parameters and true results from Kolen's STUIRT program.
 *//*from   www  .jav a2s .  c o m*/
@Test
public void haebaraTest1() {
    System.out.println("Haebara Test 1: Actual Distribution");

    LinkedHashMap<String, ItemResponseModel> irmX = new LinkedHashMap<String, ItemResponseModel>();
    LinkedHashMap<String, ItemResponseModel> irmY = new LinkedHashMap<String, ItemResponseModel>();

    irmX.put("i1", new Irm3PL(0.4551, -0.7101, 0.2087, 1.7));
    irmX.put("i2", new Irm3PL(0.5839, -0.8567, 0.2038, 1.7));
    irmX.put("i3", new Irm3PL(0.7544, 0.0212, 0.1600, 1.7));
    irmX.put("i4", new Irm3PL(0.6633, 0.0506, 0.1240, 1.7));
    irmX.put("i5", new Irm3PL(1.0690, 0.9610, 0.2986, 1.7));
    irmX.put("i6", new Irm3PL(0.9672, 0.1950, 0.0535, 1.7));
    irmX.put("i7", new Irm3PL(0.3479, 2.2768, 0.1489, 1.7));
    irmX.put("i8", new Irm3PL(1.4579, 1.0241, 0.2453, 1.7));
    irmX.put("i9", new Irm3PL(1.8811, 1.4062, 0.1992, 1.7));
    irmX.put("i10", new Irm3PL(0.7020, 2.2401, 0.0853, 1.7));
    irmX.put("i11", new Irm3PL(1.4080, 1.5556, 0.0789, 1.7));
    irmX.put("i12", new Irm3PL(1.2993, 2.1589, 0.1075, 1.7));

    irmY.put("i1", new Irm3PL(0.4416, -1.3349, 0.1559, 1.7));
    irmY.put("i2", new Irm3PL(0.5730, -1.3210, 0.1913, 1.7));
    irmY.put("i3", new Irm3PL(0.5987, -0.7098, 0.1177, 1.7));
    irmY.put("i4", new Irm3PL(0.6041, -0.3539, 0.0818, 1.7));
    irmY.put("i5", new Irm3PL(0.9902, 0.5320, 0.3024, 1.7));
    irmY.put("i6", new Irm3PL(0.8081, -0.1156, 0.0648, 1.7));
    irmY.put("i7", new Irm3PL(0.4140, 2.5538, 0.2410, 1.7));
    irmY.put("i8", new Irm3PL(1.3554, 0.5811, 0.2243, 1.7));
    irmY.put("i9", new Irm3PL(1.0417, 0.9392, 0.1651, 1.7));
    irmY.put("i10", new Irm3PL(0.6336, 1.8960, 0.0794, 1.7));
    irmY.put("i11", new Irm3PL(1.1347, 1.0790, 0.0630, 1.7));
    irmY.put("i12", new Irm3PL(0.9255, 2.1337, 0.1259, 1.7));

    double[] points = { -4.0000, -3.1110, -2.2220, -1.3330, -0.4444, 0.4444, 1.3330, 2.2220, 3.1110, 4.0000 };
    double[] xDensity = { 0.0001008, 0.002760, 0.03021, 0.1420, 0.3149, 0.3158, 0.1542, 0.03596, 0.003925,
            0.0001862 };
    double[] yDensity = { 0.0001173, 0.003242, 0.03449, 0.1471, 0.3148, 0.3110, 0.1526, 0.03406, 0.002510,
            0.0001116 };
    UserSuppliedDistributionApproximation distX = new UserSuppliedDistributionApproximation(points, xDensity);
    UserSuppliedDistributionApproximation distY = new UserSuppliedDistributionApproximation(points, yDensity);

    HaebaraMethod hb = new HaebaraMethod(irmX, irmY, distX, distY, EquatingCriterionType.Q1Q2);
    hb.setPrecision(6);
    double[] startValues = { 0, 1 };

    int numIterpolationPoints = 2 * 2;//two dimensions A and B
    BOBYQAOptimizer underlying = new BOBYQAOptimizer(numIterpolationPoints);
    RandomGenerator g = new JDKRandomGenerator();
    RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
    MultiStartMultivariateOptimizer optimizer = new MultiStartMultivariateOptimizer(underlying, 10, generator);
    org.apache.commons.math3.optim.PointValuePair optimum = optimizer.optimize(new MaxEval(1000),
            new ObjectiveFunction(hb), GoalType.MINIMIZE, SimpleBounds.unbounded(2),
            new InitialGuess(startValues));

    double[] hbCoefficients = optimum.getPoint();
    hb.setIntercept(hbCoefficients[0]);
    hb.setScale(hbCoefficients[1]);

    System.out.println("  Iterations: " + optimizer.getEvaluations());
    System.out.println("  fmin: " + optimum.getValue());
    System.out.println("  B = " + hbCoefficients[0] + "  A = " + hbCoefficients[1]);

    assertEquals("  Intercept test", -0.471281, hb.getIntercept(), 1e-4);
    assertEquals("  Scale test", 1.067800, hb.getScale(), 1e-4);

    System.out.println();

}

From source file:com.insightml.math.optimization.AbstractOptimizable.java

private PointValuePair optimize(final MultivariateOptimizer optimizer, final double[] initialValues,
        final OptimizationData... data) {
    final OptimizationData[] d = new OptimizationData[5 + data.length];
    d[0] = new MaxIter(convergence.maxIt + 1);
    d[1] = new MaxEval(convergence.maxIt * 2);
    d[2] = new ObjectiveFunction(this);
    d[3] = GoalType.MAXIMIZE;//from  w  w  w  .j a  v a  2 s  . c o  m
    d[4] = new InitialGuess(fixBounds(initialValues));
    for (int i = 0; i < data.length; ++i) {
        d[5 + i] = data[i];
    }
    return optimizer.optimize(d);
}

From source file:com.itemanalysis.psychometrics.irt.estimation.IrtExaminee.java

/**
 * Maximum likelihood estimate (MLE) of examinee ability.
 *
 * @param thetaMin smallest possible ability estimate (lower bound on BrentOptimizer)
 * @param thetaMax largest possible ability estimate (upper bound on BrentOptimizer)
 * @return MLE of examinee ability/*from  w w w.j a  va 2 s.  co m*/
 */
public double maximumLikelihoodEstimate(double thetaMin, double thetaMax) {
    method = EstimationMethod.ML;
    UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14);
    UnivariatePointValuePair pair = optimizer.optimize(new MaxEval(100), new UnivariateObjectiveFunction(this),
            GoalType.MAXIMIZE, new SearchInterval(thetaMin, thetaMax));
    estimatedTheta = pair.getPoint();
    return estimatedTheta;
}

From source file:com.itemanalysis.psychometrics.irt.equating.StockingLordMethodTest.java

/**
 * Item parameters, quadrature points and weights, and "true" linking coefficients were obtained
 * from example1 in Kolen's STUIRT program. However, the Stocking-Lord procedure was changed in
 * the STUIRT example to produce symmetric optimization of the criterion function (i.e. SY BI BI
 * instead of SY BI NO).// w w w.j  a  va  2 s  .  c o  m
 *
 * This test runs the Stocking-Lord procedure twice: One with the UNCMIN optimizer and once with
 * the BOBYQA optimizer. Results from each are compared to teh STUIRT results and to each other.
 *
 */
@Test
public void stockingLordTest0() {
    System.out.println("StockingLordMethod Test 0: Actual Distribution");
    LinkedHashMap<String, ItemResponseModel> irmX = new LinkedHashMap<String, ItemResponseModel>();
    LinkedHashMap<String, ItemResponseModel> irmY = new LinkedHashMap<String, ItemResponseModel>();

    irmX.put("i1", new Irm3PL(0.4551, -0.7101, 0.2087, 1.7));
    irmX.put("i2", new Irm3PL(0.5839, -0.8567, 0.2038, 1.7));
    irmX.put("i3", new Irm3PL(0.7544, 0.0212, 0.1600, 1.7));
    irmX.put("i4", new Irm3PL(0.6633, 0.0506, 0.1240, 1.7));
    irmX.put("i5", new Irm3PL(1.0690, 0.9610, 0.2986, 1.7));
    irmX.put("i6", new Irm3PL(0.9672, 0.1950, 0.0535, 1.7));
    irmX.put("i7", new Irm3PL(0.3479, 2.2768, 0.1489, 1.7));
    irmX.put("i8", new Irm3PL(1.4579, 1.0241, 0.2453, 1.7));
    irmX.put("i9", new Irm3PL(1.8811, 1.4062, 0.1992, 1.7));
    irmX.put("i10", new Irm3PL(0.7020, 2.2401, 0.0853, 1.7));
    irmX.put("i11", new Irm3PL(1.4080, 1.5556, 0.0789, 1.7));
    irmX.put("i12", new Irm3PL(1.2993, 2.1589, 0.1075, 1.7));

    irmY.put("i1", new Irm3PL(0.4416, -1.3349, 0.1559, 1.7));
    irmY.put("i2", new Irm3PL(0.5730, -1.3210, 0.1913, 1.7));
    irmY.put("i3", new Irm3PL(0.5987, -0.7098, 0.1177, 1.7));
    irmY.put("i4", new Irm3PL(0.6041, -0.3539, 0.0818, 1.7));
    irmY.put("i5", new Irm3PL(0.9902, 0.5320, 0.3024, 1.7));
    irmY.put("i6", new Irm3PL(0.8081, -0.1156, 0.0648, 1.7));
    irmY.put("i7", new Irm3PL(0.4140, 2.5538, 0.2410, 1.7));
    irmY.put("i8", new Irm3PL(1.3554, 0.5811, 0.2243, 1.7));
    irmY.put("i9", new Irm3PL(1.0417, 0.9392, 0.1651, 1.7));
    irmY.put("i10", new Irm3PL(0.6336, 1.8960, 0.0794, 1.7));
    irmY.put("i11", new Irm3PL(1.1347, 1.0790, 0.0630, 1.7));
    irmY.put("i12", new Irm3PL(0.9255, 2.1337, 0.1259, 1.7));

    double[] points = { -4.0000, -3.1110, -2.2220, -1.3330, -0.4444, 0.4444, 1.3330, 2.2220, 3.1110, 4.0000 };
    double[] xDensity = { 0.0001008, 0.002760, 0.03021, 0.1420, 0.3149, 0.3158, 0.1542, 0.03596, 0.003925,
            0.0001862 };
    double[] yDensity = { 0.0001173, 0.003242, 0.03449, 0.1471, 0.3148, 0.3110, 0.1526, 0.03406, 0.002510,
            0.0001116 };
    UserSuppliedDistributionApproximation distX = new UserSuppliedDistributionApproximation(points, xDensity);
    UserSuppliedDistributionApproximation distY = new UserSuppliedDistributionApproximation(points, yDensity);

    StockingLordMethod sl = new StockingLordMethod(irmX, irmY, distX, distY, EquatingCriterionType.Q1Q2);
    sl.setPrecision(6);
    double[] startValues = { 0, 1 };
    double f = 0;

    //Run test with UNCMIN optimizer
    DefaultUncminOptimizer optimizer = new DefaultUncminOptimizer();
    double[] param1 = null;
    double[] param2 = null;
    try {
        optimizer.minimize(sl, startValues);
        param1 = optimizer.getParameters();
        f = optimizer.getFunctionValue();
        sl.setIntercept(param1[0]);
        sl.setScale(param1[1]);

    } catch (UncminException ex) {
        ex.printStackTrace();
    }

    //Check UNCMIN values against results from STUIRT.
    System.out.println("  UNCMIN Optimization");
    System.out.println("  Iterations: ");
    System.out.println("  fmin: " + f);
    System.out.println("  B = " + sl.getIntercept() + "  A = " + sl.getScale());

    assertEquals("  Intercept test", -0.487619, sl.getIntercept(), 1e-6);
    assertEquals("  Scale test", 1.083417, sl.getScale(), 1e-6);

    //Run test with BOBYQA optimizer
    int numIterpolationPoints = 2 * 2;//two dimensions A and B
    BOBYQAOptimizer underlying = new BOBYQAOptimizer(numIterpolationPoints);
    RandomGenerator g = new JDKRandomGenerator();
    RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
    MultiStartMultivariateOptimizer optimizer2 = new MultiStartMultivariateOptimizer(underlying, 10, generator);
    PointValuePair optimum = optimizer2.optimize(new MaxEval(1000), new ObjectiveFunction(sl),
            GoalType.MINIMIZE, SimpleBounds.unbounded(2), new InitialGuess(startValues));

    param2 = optimum.getPoint();
    sl.setIntercept(param2[0]);
    sl.setScale(param2[1]);

    //Check BOBYQA values against results from STUIRT.
    System.out.println();
    System.out.println("  BOBYQA Optimization");
    System.out.println("  Iterations: " + optimizer2.getEvaluations());
    System.out.println("  fmin: " + optimum.getValue());
    System.out.println("  B = " + param2[0] + "  A = " + param2[1]);

    assertEquals("  Intercept test", -0.487619, sl.getIntercept(), 1e-6);
    assertEquals("  Scale test", 1.083417, sl.getScale(), 1e-6);

    //Compare results from each optimizer
    assertEquals("  Intercept test", param1[0], param2[0], 1e-6);
    assertEquals("  Scale test", param1[1], param2[1], 1e-6);

    System.out.println();

}

From source file:com.itemanalysis.psychometrics.irt.equating.HaebaraMethodTest.java

@Test
public void haearaTest2() {
    System.out.println("Haebara Test 2: Uniform Distribution");
    int n = aX.length;
    LinkedHashMap<String, ItemResponseModel> irmX = new LinkedHashMap<String, ItemResponseModel>();
    LinkedHashMap<String, ItemResponseModel> irmY = new LinkedHashMap<String, ItemResponseModel>();
    ItemResponseModel irm;//ww  w  .j ava2 s  . com

    for (int i = 0; i < n; i++) {
        String name = "V" + i;
        irm = new Irm3PL(aX[i], bX[i], cX[i], 1.0);
        irmX.put(name, irm);

        irm = new Irm3PL(aY[i], bY[i], cY[i], 1.0);
        irmY.put(name, irm);
    }

    UniformDistributionApproximation uniform = new UniformDistributionApproximation(-4, 4, 10);

    HaebaraMethod hb = new HaebaraMethod(irmX, irmY, uniform, uniform, EquatingCriterionType.Q1Q2);
    hb.setPrecision(4);
    double[] startValues = { 0, 1 };

    int numIterpolationPoints = 2 * 2;//two dimensions A and B
    BOBYQAOptimizer underlying = new BOBYQAOptimizer(numIterpolationPoints);
    RandomGenerator g = new JDKRandomGenerator();
    RandomVectorGenerator generator = new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
    MultiStartMultivariateOptimizer optimizer = new MultiStartMultivariateOptimizer(underlying, 10, generator);
    org.apache.commons.math3.optim.PointValuePair optimum = optimizer.optimize(new MaxEval(1000),
            new ObjectiveFunction(hb), org.apache.commons.math3.optim.nonlinear.scalar.GoalType.MINIMIZE,
            SimpleBounds.unbounded(2), new InitialGuess(startValues));

    double[] slCoefficients = optimum.getPoint();
    hb.setIntercept(slCoefficients[0]);
    hb.setScale(slCoefficients[1]);

    System.out.println("  Iterations: " + optimizer.getEvaluations());
    System.out.println("  fmin: " + optimum.getValue());
    System.out.println("  B = " + slCoefficients[0] + "  A = " + slCoefficients[1]);

    assertEquals("  Intercept test", -0.4303, hb.getIntercept(), 1e-4);
    assertEquals("  Scale test", 1.0894, hb.getScale(), 1e-4);

    System.out.println();

}

From source file:com.itemanalysis.psychometrics.irt.estimation.IrtExaminee.java

/**
 * Maximum a Posteriori (MAP) estimate of examinee ability using a normal prior
 * distribution.//www.  j a  va2s .  c  om
 *
 * @param mean mean of normal prior distribution
 * @param sd standard deviation of prior distribution
 * @param thetaMin smallest possible ability estimate (lower bound on BrentOptimizer)
 * @param thetaMax largest possible ability estimate (upper bound on BrentOptimizer)
 * @return MAP estimate of examinee ability
 */
public double mapEstimate(double mean, double sd, double thetaMin, double thetaMax) {
    mapPrior = new NormalDistribution(mean, sd);
    method = EstimationMethod.MAP;
    UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14);
    UnivariatePointValuePair pair = optimizer.optimize(new MaxEval(100), new UnivariateObjectiveFunction(this),
            GoalType.MAXIMIZE, new SearchInterval(thetaMin, thetaMax));
    estimatedTheta = pair.getPoint();
    return estimatedTheta;
}