Skip to content

Commit 612286f

Browse files
committed
Added progress bar to full calibration.
1 parent 5368fdd commit 612286f

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

src/main/java/net/finmath/climateschool/utilities/AdamOptimizerUsingFiniteDifferences.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ public enum GradientMethod {
3030
private final GradientMethod gradientMethod;
3131

3232
private final int iterations;
33-
private boolean runnning = false;
3433
private double[] learningRate ;
3534
private final double eps ;
3635
private final double[] betas ;
3736

37+
private boolean runnning = false;
38+
private int iteration;
39+
3840
private final RandomVariableDifferentiable[] parameters ;
3941
private RandomVariableDifferentiable[] bestFitParameters;
4042
private double bestValue = Double.MAX_VALUE;
@@ -92,7 +94,7 @@ public void run() {
9294
final double[] m = new double[parameters.length];
9395
final double[] v = new double[parameters.length];
9496

95-
for(int k=0; k<iterations && runnning; k++) {
97+
for(iteration=0; iteration<iterations && runnning; iteration++) {
9698
final RandomVariable value = setValue(parameters);
9799
if (value.getAverage() < bestValue || bestFitParameters == null) {
98100
bestValue = value.getAverage();
@@ -113,18 +115,18 @@ public void run() {
113115
m[i] = (betas[0]*m[i] + (1-betas[0])*gradient);
114116
v[i] = (betas[1]*v[i] + (1-betas[1])*gradient*gradient);
115117

116-
final double update_m = m[i] / (1-Math.pow(betas[0],k+1));
117-
final double update_v = v[i] / (1-Math.pow(betas[1],k+1));
118+
final double update_m = m[i] / (1-Math.pow(betas[0], iteration+1));
119+
final double update_v = v[i] / (1-Math.pow(betas[1], iteration+1));
118120
final double stepDirection = update_m / (Math.sqrt(update_v)+eps);
119121

120122
parameters[i] = ((RandomVariableDifferentiable) parameters[i].sub(learningRate[i]*stepDirection)).getCloneIndependent();
121123
}
122124

123-
if (k % 10 == 0) {
125+
if(iteration % 10 == 0) {
124126
final double valueForPrinting = (gradientMethod == GradientMethod.AVERAGE) ? value.getAverage() :
125127
-RandomOperators.expectedShortFall(value.mult(-1.0),0.05).doubleValue();
126-
if (k % 100 == 0) {
127-
System.out.printf("iteration %8d \t\t value %8.4f %n", k, -valueForPrinting);
128+
if (iteration % 100 == 0) {
129+
System.out.printf("iteration %8d \t\t value %8.4f %n", iteration, -valueForPrinting);
128130
} else {
129131
// System.out.printf("iteration %8d \t\t value %8.4f \r", k, -valueForPrinting);
130132
}
@@ -140,7 +142,7 @@ public void run() {
140142
v[i] = randomVariableFactory.createRandomVariable(0);
141143
}
142144

143-
for(int k=0; k<iterations && runnning; k++) {
145+
for(iteration=0; iteration<iterations && runnning; iteration++) {
144146
final RandomVariable value = setValue(parameters);
145147
if (value.getAverage() < bestValue || bestFitParameters == null) {
146148
bestValue = value.getAverage();
@@ -160,16 +162,16 @@ public void run() {
160162
m[i] = m[i].mult(betas[0]).add(gradient.mult(1-betas[0]));
161163
v[i] = v[i].mult(betas[1]).add(gradient.squared().mult(1-betas[1]));
162164

163-
final RandomVariable update_m = m[i].div(1-Math.pow(betas[0],k+1));
164-
final RandomVariable update_v = v[i].div(1-Math.pow(betas[1],k+1));
165+
final RandomVariable update_m = m[i].div(1-Math.pow(betas[0], iteration+1));
166+
final RandomVariable update_v = v[i].div(1-Math.pow(betas[1], iteration+1));
165167
final RandomVariable stepDirection = update_m.div(update_v.sqrt().add(eps));
166168

167169
parameters[i] =
168170
((RandomVariableDifferentiable) parameters[i].sub(stepDirection.mult(learningRate[i]))).getCloneIndependent();
169171
}
170172

171-
if (k % 100 == 0) {
172-
System.out.printf("iteration %8.4f \t\t value %8.4f %n", (double) k,value.getAverage());
173+
if (iteration % 100 == 0) {
174+
System.out.printf("iteration %8.4f \t\t value %8.4f %n", (double)iteration,value.getAverage());
173175
}
174176
}
175177
}
@@ -209,4 +211,8 @@ private RandomVariable[] getGradient(RandomVariable[] parameters, RandomVariable
209211

210212
return gradient;
211213
}
214+
215+
public double getIteration() {
216+
return iteration;
217+
}
212218
}

src/main/java/net/finmath/climateschool/utilities/DICEModelPlots.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.ArrayList;
88
import java.util.Arrays;
99
import java.util.List;
10+
import java.util.function.Consumer;
1011
import java.util.stream.IntStream;
1112

1213
import net.finmath.climate.models.CarbonConcentration;
@@ -212,8 +213,31 @@ public void plotCost(ClimateModel climateModel, double discountRate, String para
212213
}
213214
}
214215

216+
private static void closePlot(Plot2D plot) {
217+
if(plot != null) plot.close();
218+
}
219+
220+
public void close() {
221+
closePlot(plotTemperature);
222+
closePlot(plotCarbon);
223+
closePlot(plotEmission);
224+
closePlot(plotOutput);
225+
closePlot(plotAbatement);
226+
227+
plotTemperature = null;
228+
plotCarbon = null;
229+
plotEmission = null;
230+
plotOutput = null;
231+
plotAbatement = null;
232+
233+
closeCost();
234+
}
235+
215236
public void closeCost() {
216-
if(plotCostDiscounted != null) plotCostDiscounted.close();
217-
if(plotCostPerGDP != null) plotCostPerGDP.close();
237+
closePlot(plotCostDiscounted);
238+
plotCostDiscounted = null;
239+
240+
closePlot(plotCostPerGDP);
241+
plotCostPerGDP = null;
218242
}
219243
}

src/main/java/net/finmath/experiments/ui/DICECalibrationExperimentUI.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public void runCalculation(BooleanSupplier isCancelled, DoubleConsumer progress)
7070
Arrays.fill(initialParameters, -Math.log(-Math.log(0.8)));
7171
}
7272

73+
final int maxIterations = 500;
74+
7375
final AdamOptimizerUsingFiniteDifferences optimizer = new AdamOptimizerUsingFiniteDifferences(initialParameters, 800, 0.05, GradientMethod.AVERAGE) {
7476
private int iteration = 0;
7577
@Override
@@ -114,6 +116,8 @@ public RandomVariable setValue(RandomVariable[] parameters) {
114116
else {
115117
plots.closeCost();
116118
}
119+
120+
progress.accept((double)getIteration()/maxIterations);
117121
}
118122
iteration++;
119123

0 commit comments

Comments
 (0)