summaryrefslogtreecommitdiff
path: root/java/src/org/singinst/uf/model/CumulativeHazardsRelation.java
blob: 0c17a4a65ed44c4f643e1dc5ece768fa158f970e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package org.singinst.uf.model;

//import java.util.List;

import org.singinst.uf.math.MathUtil;

public class CumulativeHazardsRelation extends YearwiseCalculationRelation {

	public CumulativeHazardsRelation(Node node) {
		super(node);
	}

	@Override
	protected Calculation getCalculation(double year) {
		return new AtLeastOneHappensCalculation("Probability of nuclear or other science-disrupting catastrophe by year " + (int) year, 
				probabilityOneCumulativeHazard(NodeIDString.Q1_7, "Independent probability of nuclear catastrophe by year ", year),
				probabilityOneCumulativeHazard(NodeIDString.Q1_8, "Independent probability of non-nuclear catastrophe by year ", year));

	}

	private Calculation probabilityOneCumulativeHazard(final String nodeId, final String description, final double year) {
		return new Calculation(description + (int) year) {

			@Override
			protected double rawEvaluate(StringBuilder htmlConsole) {
				double t0, t1;
				t0 = ModelUtil.EARLIEST_YEAR;
				t1 = ModelUtil.LATEST_YEAR;
				double klow, khigh;
				klow  = NotablePercentile.PERCENTILE5.getValue();
				khigh = NotablePercentile.PERCENTILE95.getValue();
				double zlow, zhigh;
				zlow  = NotablePercentile.PERCENTILE5.getOffset();
				zhigh = NotablePercentile.PERCENTILE95.getOffset();

				double y0low, y0high, y1low, y1high;
				y0low  = getDependency().value(nodeId, ScalarSubIDString.yearPercentileID(t0, klow ))*Math.log(10);
				y0high = getDependency().value(nodeId, ScalarSubIDString.yearPercentileID(t0, khigh))*Math.log(10);
				y1low  = getDependency().value(nodeId, ScalarSubIDString.yearPercentileID(t1, klow ))*Math.log(10);
				y1high = getDependency().value(nodeId, ScalarSubIDString.yearPercentileID(t1, khigh))*Math.log(10);

				double mean_init, stddev_init, mean_final, stddev_final;
				mean_init = MathUtil.linterp(zlow, zhigh, y0low, y0high, 0);
				stddev_init = (y0high-y0low)/(zhigh-zlow);
				mean_final = MathUtil.linterp(zlow, zhigh, y1low, y1high, 0);
				stddev_final = (y1high-y1low)/(zhigh-zlow);
				
				double[] clogp = MathUtil.clogMarginalOfExponentiallyVaryingHazardModel(
						t0, t1,
						mean_init, mean_final,
						stddev_init, stddev_final,
						new double[]{year}
				);				
				
				return MathUtil.expc(clogp[0]);
			}
			
		};
	}	

}