Search in sources :

Example 1 with MultivariateOptimizer

use of org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer in project tetrad by cmu-phil.

the class GeneralizedSemIm method simulateDataMinimizeSurface.

public DataSet simulateDataMinimizeSurface(int sampleSize, boolean latentDataSaved) {
    final Map<String, Double> variableValues = new HashMap<>();
    List<Node> continuousVariables = new LinkedList<>();
    final List<Node> variableNodes = pm.getVariableNodes();
    // Work with a copy of the variables, because their type can be set externally.
    for (Node node : variableNodes) {
        ContinuousVariable var = new ContinuousVariable(node.getName());
        var.setNodeType(node.getNodeType());
        if (var.getNodeType() != NodeType.ERROR) {
            continuousVariables.add(var);
        }
    }
    DataSet fullDataSet = new ColtDataSet(sampleSize, continuousVariables);
    final Context context = new Context() {

        public Double getValue(String term) {
            Double value = parameterValues.get(term);
            if (value != null) {
                return value;
            }
            value = variableValues.get(term);
            if (value != null) {
                return value;
            }
            throw new IllegalArgumentException("No value recorded for '" + term + "'");
        }
    };
    final double[] _metric = new double[1];
    MultivariateFunction function = new MultivariateFunction() {

        double metric;

        public double value(double[] doubles) {
            for (int i = 0; i < variableNodes.size(); i++) {
                variableValues.put(variableNodes.get(i).getName(), doubles[i]);
            }
            double[] image = new double[doubles.length];
            for (int i = 0; i < variableNodes.size(); i++) {
                Node node = variableNodes.get(i);
                Expression expression = pm.getNodeExpression(node);
                image[i] = expression.evaluate(context);
                if (Double.isNaN(image[i])) {
                    throw new IllegalArgumentException("Undefined value for expression " + expression);
                }
            }
            metric = 0.0;
            for (int i = 0; i < variableNodes.size(); i++) {
                double diff = doubles[i] - image[i];
                metric += diff * diff;
            }
            for (int i = 0; i < variableNodes.size(); i++) {
                variableValues.put(variableNodes.get(i).getName(), image[i]);
            }
            _metric[0] = metric;
            return metric;
        }
    };
    MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
    // Do the simulation.
    ROW: for (int row = 0; row < sampleSize; row++) {
        // Take random draws from error distributions.
        for (Node variable : variableNodes) {
            Node error = pm.getErrorNode(variable);
            if (error == null) {
                throw new NullPointerException();
            }
            Expression expression = pm.getNodeExpression(error);
            double value = expression.evaluate(context);
            if (Double.isNaN(value)) {
                throw new IllegalArgumentException("Undefined value for expression: " + expression);
            }
            variableValues.put(error.getName(), value);
        }
        for (Node variable : variableNodes) {
            // RandomUtil.getInstance().nextUniform(-5, 5));
            variableValues.put(variable.getName(), 0.0);
        }
        while (true) {
            double[] values = new double[variableNodes.size()];
            for (int i = 0; i < values.length; i++) {
                values[i] = variableValues.get(variableNodes.get(i).getName());
            }
            PointValuePair pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000));
            values = pair.getPoint();
            for (int i = 0; i < variableNodes.size(); i++) {
                if (isSimulatePositiveDataOnly() && values[i] < 0) {
                    row--;
                    continue ROW;
                }
                if (!Double.isNaN(selfLoopCoef) && row > 0) {
                    values[i] += selfLoopCoef * fullDataSet.getDouble(row - 1, i);
                }
                variableValues.put(variableNodes.get(i).getName(), values[i]);
                fullDataSet.setDouble(row, i, values[i]);
            }
            if (_metric[0] < 0.01) {
                // while
                break;
            }
        }
    }
    if (latentDataSaved) {
        return fullDataSet;
    } else {
        return DataUtils.restrictToMeasured(fullDataSet);
    }
}
Also used : Context(edu.cmu.tetrad.calculator.expression.Context) MultivariateOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) PowellOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.PowellOptimizer) PointValuePair(org.apache.commons.math3.optim.PointValuePair) MultivariateFunction(org.apache.commons.math3.analysis.MultivariateFunction) Expression(edu.cmu.tetrad.calculator.expression.Expression)

Example 2 with MultivariateOptimizer

use of org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer in project tetrad by cmu-phil.

the class SemOptimizerPowell method optimize.

// =========================PUBLIC METHODS==========================//
public void optimize(SemIm semIm) {
    double min = Double.POSITIVE_INFINITY;
    double[] point = null;
    for (int count = 0; count < numRestarts + 1; count++) {
        // System.out.println("Trial " + (count + 1));
        SemIm _sem2 = new SemIm(semIm);
        List<Parameter> freeParameters = _sem2.getFreeParameters();
        double[] p = new double[freeParameters.size()];
        for (int i = 0; i < freeParameters.size(); i++) {
            if (freeParameters.get(i).getType() == ParamType.VAR) {
                p[i] = RandomUtil.getInstance().nextUniform(0, 1);
            } else {
                p[i] = RandomUtil.getInstance().nextUniform(-1, 1);
            }
        }
        _sem2.setFreeParamValues(p);
        MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
        PointValuePair pair = search.optimize(new InitialGuess(_sem2.getFreeParamValues()), new ObjectiveFunction(fittingFunction(semIm)), GoalType.MINIMIZE, new MaxEval(100000));
        double chisq = _sem2.getChiSquare();
        if (chisq < min) {
            min = chisq;
            point = pair.getPoint();
        }
    }
    if (point == null) {
        throw new NullPointerException("Point could not be found.");
    }
    System.arraycopy(point, 0, semIm.getFreeParamValues(), 0, point.length);
}
Also used : MultivariateOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) PowellOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.PowellOptimizer) PointValuePair(org.apache.commons.math3.optim.PointValuePair)

Example 3 with MultivariateOptimizer

use of org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer in project tetrad by cmu-phil.

the class Ling method optimizeNonGaussianity.

private void optimizeNonGaussianity(final int rowIndex, final TetradMatrix dataSetTetradMatrix, final double[][] W, List<Mapping> allMappings) {
    final List<Mapping> mappings = mappingsForRow(rowIndex, allMappings);
    MultivariateFunction function = new MultivariateFunction() {

        public double value(double[] values) {
            for (int i = 0; i < values.length; i++) {
                Mapping mapping = mappings.get(i);
                W[mapping.getI()][mapping.getJ()] = values[i];
            }
            double v = ngFullData(rowIndex, dataSetTetradMatrix, W);
            if (Double.isNaN(v))
                return 10000;
            return -(v);
        }
    };
    {
        double[] values = new double[mappings.size()];
        for (int k = 0; k < mappings.size(); k++) {
            Mapping mapping = mappings.get(k);
            values[k] = W[mapping.getI()][mapping.getJ()];
        }
        MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
        PointValuePair pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000));
        values = pair.getPoint();
        for (int k = 0; k < mappings.size(); k++) {
            Mapping mapping = mappings.get(k);
            W[mapping.getI()][mapping.getJ()] = values[k];
        }
    }
}
Also used : MultivariateFunction(org.apache.commons.math3.analysis.MultivariateFunction) MultivariateOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) PowellOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.PowellOptimizer) PointValuePair(org.apache.commons.math3.optim.PointValuePair)

Example 4 with MultivariateOptimizer

use of org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer in project tetrad by cmu-phil.

the class Mimbuild2 method optimizeNonMeasureVariancesConditionally.

private void optimizeNonMeasureVariancesConditionally(Node[][] indicators, TetradMatrix measurescov, TetradMatrix latentscov, double[][] loadings, int[][] indicatorIndices, double[] delta) {
    int count = 0;
    for (int i = 0; i < indicators.length; i++) {
        for (int j = i; j < indicators.length; j++) {
            count++;
        }
    }
    for (int i = 0; i < indicators.length; i++) {
        for (int j = 0; j < indicators[i].length; j++) {
            count++;
        }
    }
    double[] values3 = new double[count];
    count = 0;
    for (int i = 0; i < indicators.length; i++) {
        for (int j = i; j < indicators.length; j++) {
            values3[count] = latentscov.get(i, j);
            count++;
        }
    }
    for (int i = 0; i < indicators.length; i++) {
        for (int j = 0; j < indicators[i].length; j++) {
            values3[count] = loadings[i][j];
            count++;
        }
    }
    Function2 function2 = new Function2(indicatorIndices, measurescov, loadings, latentscov, delta, count);
    MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
    PointValuePair pair = search.optimize(new InitialGuess(values3), new ObjectiveFunction(function2), GoalType.MINIMIZE, new MaxEval(100000));
    minimum = pair.getValue();
}
Also used : MultivariateOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) PowellOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.PowellOptimizer) PointValuePair(org.apache.commons.math3.optim.PointValuePair)

Example 5 with MultivariateOptimizer

use of org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer in project tetrad by cmu-phil.

the class Mimbuild2 method optimizeNonMeasureVariancesQuick.

private void optimizeNonMeasureVariancesQuick(Node[][] indicators, TetradMatrix measurescov, TetradMatrix latentscov, double[][] loadings, int[][] indicatorIndices) {
    int count = 0;
    for (int i = 0; i < indicators.length; i++) {
        for (int j = i; j < indicators.length; j++) {
            count++;
        }
    }
    for (int i = 0; i < indicators.length; i++) {
        for (int j = 0; j < indicators[i].length; j++) {
            count++;
        }
    }
    double[] values = new double[count];
    count = 0;
    for (int i = 0; i < indicators.length; i++) {
        for (int j = i; j < indicators.length; j++) {
            values[count++] = latentscov.get(i, j);
        }
    }
    for (int i = 0; i < indicators.length; i++) {
        for (int j = 0; j < indicators[i].length; j++) {
            values[count++] = loadings[i][j];
        }
    }
    Function1 function1 = new Function1(indicatorIndices, measurescov, loadings, latentscov, count);
    MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
    PointValuePair pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function1), GoalType.MINIMIZE, new MaxEval(100000));
    minimum = pair.getValue();
}
Also used : MultivariateOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer) InitialGuess(org.apache.commons.math3.optim.InitialGuess) MaxEval(org.apache.commons.math3.optim.MaxEval) ObjectiveFunction(org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction) PowellOptimizer(org.apache.commons.math3.optim.nonlinear.scalar.noderiv.PowellOptimizer) PointValuePair(org.apache.commons.math3.optim.PointValuePair)

Aggregations

PointValuePair (org.apache.commons.math3.optim.PointValuePair)16 MultivariateOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.MultivariateOptimizer)16 ObjectiveFunction (org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction)16 InitialGuess (org.apache.commons.math3.optim.InitialGuess)15 MaxEval (org.apache.commons.math3.optim.MaxEval)15 PowellOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.noderiv.PowellOptimizer)13 TooManyEvaluationsException (org.apache.commons.math3.exception.TooManyEvaluationsException)4 MultivariateFunction (org.apache.commons.math3.analysis.MultivariateFunction)3 TooManyIterationsException (org.apache.commons.math3.exception.TooManyIterationsException)2 OptimizationData (org.apache.commons.math3.optim.OptimizationData)2 SimpleBounds (org.apache.commons.math3.optim.SimpleBounds)2 SimpleValueChecker (org.apache.commons.math3.optim.SimpleValueChecker)2 ObjectiveFunctionGradient (org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunctionGradient)2 CMAESOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.noderiv.CMAESOptimizer)2 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)2 Context (edu.cmu.tetrad.calculator.expression.Context)1 Expression (edu.cmu.tetrad.calculator.expression.Expression)1 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)1 RealMatrix (org.apache.commons.math3.linear.RealMatrix)1 BFGSOptimizer (org.apache.commons.math3.optim.nonlinear.scalar.gradient.BFGSOptimizer)1