use of org.apache.commons.math3.optim.MaxEval in project tetrad by cmu-phil.
the class LogisticRegression2 method regress.
// I am going to try to maximize the liklehood function directly using the Powell Estimator.
public void regress(int[] target, int numValues, double[][] regressors) {
try {
int numParams = regressors.length + 1;
double[] coefficients = new double[(numValues - 1) * numParams];
// Apparently this needs to be fairly loose.
int tolerance = 250;
MultivariateOptimizer search = new PowellOptimizer(tolerance, tolerance);
PointValuePair pair = search.optimize(new InitialGuess(coefficients), new ObjectiveFunction(new FittingFunction(target, regressors)), GoalType.MAXIMIZE, new MaxEval(1000000));
this.likelihood = pair.getValue();
} catch (TooManyEvaluationsException e) {
e.printStackTrace();
this.likelihood = Double.NaN;
}
}
use of org.apache.commons.math3.optim.MaxEval in project tetrad by cmu-phil.
the class Mimbuild2 method optimizeMeasureVariancesConditionally.
private void optimizeMeasureVariancesConditionally(TetradMatrix measurescov, TetradMatrix latentscov, double[][] loadings, int[][] indicatorIndices, double[] delta) {
double[] values2 = new double[delta.length];
int count = 0;
for (int i = 0; i < delta.length; i++) {
values2[count++] = delta[i];
}
Function2 function2 = new Function2(indicatorIndices, measurescov, loadings, latentscov, delta, count);
MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
PointValuePair pair = search.optimize(new InitialGuess(values2), new ObjectiveFunction(function2), GoalType.MINIMIZE, new MaxEval(100000));
minimum = pair.getValue();
}
use of org.apache.commons.math3.optim.MaxEval in project tetrad by cmu-phil.
the class Mimbuild2 method optimizeAllParamsSimultaneously.
private void optimizeAllParamsSimultaneously(Node[][] indicators, TetradMatrix measurescov, TetradMatrix latentscov, double[][] loadings, int[][] indicatorIndices, double[] delta) {
double[] values = getAllParams(indicators, latentscov, loadings, delta);
Function4 function = new Function4(indicatorIndices, measurescov, loadings, latentscov, delta);
MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
PointValuePair pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000));
minimum = pair.getValue();
}
use of org.apache.commons.math3.optim.MaxEval in project tetrad by cmu-phil.
the class MimbuildTrek method optimizeAllParamsSimultaneously.
private void optimizeAllParamsSimultaneously(Node[][] indicators, TetradMatrix measurescov, TetradMatrix latentscov, double[][] loadings, int[][] indicatorIndices, double[] delta) {
double[] values = getAllParams(indicators, latentscov, loadings, delta);
Function4 function = new Function4(indicatorIndices, measurescov, loadings, latentscov, delta);
MultivariateOptimizer search = new PowellOptimizer(1e-7, 1e-7);
PointValuePair pair = search.optimize(new InitialGuess(values), new ObjectiveFunction(function), GoalType.MINIMIZE, new MaxEval(100000));
minimum = pair.getValue();
}
use of org.apache.commons.math3.optim.MaxEval in project tetrad by cmu-phil.
the class Lofs2 method optimizeRow.
private void optimizeRow(final int rowIndex, final TetradMatrix data, final double range, final List<List<Integer>> rows, final List<List<Double>> parameters) {
System.out.println("A");
final int numParams = rows.get(rowIndex).size();
final double[] dLeftMin = new double[numParams];
final double[] dRightMin = new double[numParams];
double[] values = new double[numParams];
double delta = 0.1;
if (false) {
// isEdgeCorrected()) {
double min = -2;
double max = 2;
int[] dims = new int[values.length];
int numBins = 5;
for (int i = 0; i < values.length; i++) dims[i] = numBins;
CombinationGenerator gen = new CombinationGenerator(dims);
int[] comb;
List<Double> maxParams = new ArrayList<>();
for (int i = 0; i < values.length; i++) maxParams.add(0.0);
double maxV = Double.NEGATIVE_INFINITY;
while ((comb = gen.next()) != null) {
if (Thread.currentThread().isInterrupted()) {
break;
}
List<Double> params = new ArrayList<>();
for (int i = 0; i < values.length; i++) {
params.add(min + (max - min) * (comb[i] / (double) numBins));
}
parameters.set(rowIndex, params);
double v = scoreRow(rowIndex, data, rows, parameters);
if (v > maxV) {
maxV = v;
maxParams = params;
}
}
System.out.println("maxparams = " + maxParams);
parameters.set(rowIndex, maxParams);
for (int i = 0; i < values.length; i++) {
dLeftMin[i] = -range;
dRightMin[i] = range;
values[i] = maxParams.get(i);
}
} else if (false) {
for (int i = 0; i < numParams; i++) {
if (Thread.currentThread().isInterrupted()) {
break;
}
parameters.get(rowIndex).set(i, -range);
double vLeft = scoreRow(rowIndex, data, rows, parameters);
double dLeft = -range;
// Search from the left for the first valley; mark that as dleft.
for (double d = -range + delta; d < range; d += delta) {
if (Thread.currentThread().isInterrupted()) {
break;
}
parameters.get(rowIndex).set(i, d);
double v = scoreRow(rowIndex, data, rows, parameters);
if (Double.isNaN(v))
continue;
if (v > vLeft)
break;
vLeft = v;
dLeft = d;
}
parameters.get(rowIndex).set(i, range);
double vRight = scoreRow(rowIndex, data, rows, parameters);
double dRight = range;
// to avoid high scores at the boundaries.
for (double d = range - delta; d > -range; d -= delta) {
if (Thread.currentThread().isInterrupted()) {
break;
}
parameters.get(rowIndex).set(i, d);
double v = scoreRow(rowIndex, data, rows, parameters);
if (Double.isNaN(v))
continue;
if (v > vRight)
break;
vRight = v;
dRight = d;
}
// If dleft dright ended up reversed, re-reverse them.
if (dLeft > dRight) {
double temp = dRight;
dLeft = dRight;
dRight = temp;
}
System.out.println("dLeft = " + dLeft + " dRight = " + dRight);
dLeftMin[i] = dLeft;
dRightMin[i] = dRight;
values[i] = (dLeft + dRight) / 2.0;
}
} else {
// Default case: search for the maximum score over the entire range.
for (int i = 0; i < numParams; i++) {
if (Thread.currentThread().isInterrupted()) {
break;
}
dLeftMin[i] = -range;
dRightMin[i] = range;
values[i] = 0;
}
}
MultivariateFunction function = new MultivariateFunction() {
public double value(double[] values) {
System.out.println(Arrays.toString(values));
for (int i = 0; i < values.length; i++) {
if (Thread.currentThread().isInterrupted()) {
break;
}
parameters.get(rowIndex).set(i, values[i]);
}
double v = scoreRow(rowIndex, data, rows, parameters);
if (Double.isNaN(v)) {
// was 10000
return Double.POSITIVE_INFINITY;
}
return -v;
}
};
try {
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();
} catch (Exception e) {
e.printStackTrace();
for (int i = 0; i < values.length; i++) {
parameters.get(rowIndex).set(i, Double.NaN);
}
}
}
Aggregations