summaryrefslogtreecommitdiff
path: root/java/src/org/singinst/uf/model/RescheduledEventsRelation.java
blob: cf7faaba1921d1e3aa9a1d558ae614e3d01f975d (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package org.singinst.uf.model;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import org.singinst.uf.math.MathUtil;

public class RescheduledEventsRelation extends CalculationRelation {

	/*@Override
	protected Calculation getCalculationForYear(double year) {
		// TODO Auto-generated method stub
		return null;
	}*/

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


	protected Calculation probabilityAiIsPossible() {
		return new Calculation("Probability that AGI is possible") {
			@Override
			protected double rawEvaluate(StringBuilder htmlConsole) {
				MathUtil.ScienceSpeedModelParameters scienceSpeedModelParameters = scienceSpeedModel();
	org.singinst.uf.common.LogUtil.info(String.format(
			  "institutional_base_year: %g\n"
			+ "institutional_mean_slope_log_factor: %g\n"
			+ "institutional_stddev_slope_log_factor: %g\n"
			+ "population_base_year: %g\n"
			+ "population_mean_log_year: %g\n"
			+ "population_stddev_log_year: %g\n"
			+ "population_mean_init_log_rate: %g\n"
			+ "population_stddev_init_log_rate: %g\n"
			+ "population_mean_slope_log_rate: %g\n"
			+ "population_stddev_slope_log_rate: %g\n"
			+ "sequencedata_base_year: %g\n"
			+ "sequencedata_mean_log_year: %g\n"
			+ "sequencedata_stddev_log_year: %g\n"
			+ "sequencedata_mean_init_log_factor: %g\n"
			+ "sequencedata_stddev_init_log_factor: %g\n"
			+ "sequencedata_mean_slope_log_factor: %g\n"
			+ "sequencedata_stddev_slope_log_factor: %g\n"
			, scienceSpeedModelParameters.institutional_base_year
			, scienceSpeedModelParameters.institutional_mean_slope_log_factor
			, scienceSpeedModelParameters.institutional_stddev_slope_log_factor
			, scienceSpeedModelParameters.population_base_year
			, scienceSpeedModelParameters.population_mean_log_year
			, scienceSpeedModelParameters.population_stddev_log_year
			, scienceSpeedModelParameters.population_mean_init_log_rate
			, scienceSpeedModelParameters.population_stddev_init_log_rate
			, scienceSpeedModelParameters.population_mean_slope_log_rate
			, scienceSpeedModelParameters.population_stddev_slope_log_rate
			, scienceSpeedModelParameters.sequencedata_base_year
			, scienceSpeedModelParameters.sequencedata_mean_log_year
			, scienceSpeedModelParameters.sequencedata_stddev_log_year
			, scienceSpeedModelParameters.sequencedata_mean_init_log_factor
			, scienceSpeedModelParameters.sequencedata_stddev_init_log_factor
			, scienceSpeedModelParameters.sequencedata_mean_slope_log_factor
			, scienceSpeedModelParameters.sequencedata_stddev_slope_log_factor
			));
	return getDependency().value(NodeIDString.Q1_1, ScalarSubIDString.PROBABILITY) / 100;
			}
		};
	}
	
	MathUtil.EventDiscreteDistributionSchedule rescheduledProbabilities;
	
	@Override
	protected List<Calculation> getCalculations(final List<Double> years) {
		List<Calculation> retVal = new ArrayList<Calculation>();
		for (final double year : years) {
			retVal.add(new Calculation("Probability of AI by " + (int) year) {

				@Override
				protected double rawEvaluate(StringBuilder htmlConsole) {
					if (year == ModelUtil.BASIC_MODEL_YEARS[0]) {
						calculate(MathUtil.wrapDoubleArray(ModelUtil.BASIC_MODEL_YEARS));
					}
					return MathUtil.expc(rescheduledProbabilities.logitnerp(year).clogProbEvent);
				}
			});
		}
		return retVal;
	}
	
	private void calculate(List<Double> years) {
		double[] yearsInOtherCalculations = ModelUtil.BASIC_MODEL_YEARS;
		double[] yearsHere = MathUtil.unwrapDoubleList(years);
		//new double[years.size()];
		//for (int i=0; i<yearsHere.length; ++i)
		//	yearsHere[i] = years.get(i);
		
		TreeSet<Double> beforeYearsSortedSet = new TreeSet<Double>(MathUtil.wrapDoubleArray(yearsInOtherCalculations));
		{
			double i = ModelUtil.LATEST_YEAR - ModelUtil.EARLIEST_IA_YEAR;
			double factor =
				  (yearsInOtherCalculations[yearsInOtherCalculations.length-1] - ModelUtil.EARLIEST_IA_YEAR)
				/ (yearsInOtherCalculations[yearsInOtherCalculations.length-2] - ModelUtil.EARLIEST_IA_YEAR)
				;
			i *= factor;
			while (i <= ModelUtil.LATEST_UNACCELERATED_YEAR - ModelUtil.EARLIEST_IA_YEAR) {
				beforeYearsSortedSet.add(Math.round(i/ModelUtil.YEAR_STEPSIZE)*ModelUtil.YEAR_STEPSIZE + ModelUtil.EARLIEST_IA_YEAR);
				i *= factor;
			};
			i = ModelUtil.LATEST_YEAR - ModelUtil.EARLIEST_IA_YEAR;
			i /= factor;
			while (i <= ModelUtil.EARLIEST_YEAR - ModelUtil.EARLIEST_IA_YEAR) {
				beforeYearsSortedSet.add(Math.round(i/ModelUtil.YEAR_STEPSIZE)*ModelUtil.YEAR_STEPSIZE + ModelUtil.EARLIEST_IA_YEAR);
				i /= factor;
			};
		}
		
		double[] beforeYears = MathUtil.unwrapDoubleList(new ArrayList<Double>(beforeYearsSortedSet));
		
		//List<Double> clogPAiWrapped; 
		
		double[] clogPAi;
		//clogPAi = MathUtil.unwrapDoubleList(clogPAiWrapped);
		clogPAi = new double[beforeYears.length];
		for (int i=0; i<clogPAi.length; ++i) {
		//	clogPAI[i] = 100;
			clogPAi[i] = MathUtil.clog(ScalarValueHolder.findById(NodeIDString.C1_6, ScalarSubIDString.yearProbabilityID(beforeYears[i])).getValue());
			if (clogPAi[i]>300d) clogPAi[i]=300d;
		}
		MathUtil.EventDiscreteDistributionSchedule s_before = new MathUtil.EventDiscreteDistributionSchedule();
		MathUtil.EventDiscreteDistributionSchedule s_after;

		s_before.time = beforeYears; //ModelUtil.BASIC_MODEL_YEARS;
		//s_before.time = java.util.Arrays.copyOf(ModelUtil.BASIC_MODEL_YEARS, yearsInOtherCalculations.length+2);
		//s_before.time[yearsInOtherCalculations.length] = ModelUtil.LATEST_YEAR*(1+2*MathUtil.DOUBLE_EPS);
		//s_before.time[yearsInOtherCalculations.length+1] = ModelUtil.LATEST_YEAR*(1+4*MathUtil.DOUBLE_EPS);
		s_before.clogProbEvent = clogPAi;
		//s_before.clogProbEvent = java.util.Arrays.copyOf(clogPAI, yearsInOtherCalculations.length+2);
		s_before.logitSubevents = new double[][]{};
		
		MathUtil.ScienceSpeedModelParameters scienceSpeedModelParameters = scienceSpeedModel();

		
		s_after = MathUtil.clogMarginalScienceSpeedEventRescheduling(
				scienceSpeedModelParameters,
				s_before, yearsHere, 1000);
		
		rescheduledProbabilities = s_after;
		
		//List<Double> rescheduledProbabilities = new java.util.ArrayList<Double>(years.size());
		//for (int i=0; i<rescheduledProbabilities.size(); ++i) {
		//	rescheduledProbabilities.set(i, MathUtil.expc(s_after.clogProbEvent[i]));
		//}
		
		/*				
		yearsInOtherCalculations = new double[ModelUtil.]*/

		//return rescheduledProbabilities;

	}

	// glue equations
	private MathUtil.ScienceSpeedModelParameters scienceSpeedModel() {
		MathUtil.ScienceSpeedModelParameters o = new MathUtil.ScienceSpeedModelParameters();

		o.sequencedata_base_year               = ModelUtil.ANCHOR_FAR_YEAR + ModelUtil.RESEARCH_CAREER_DELAY;
		o.sequencedata_mean_log_year           = getDependency().value(NodeIDString.Q1_9_1, ScalarSubIDString.MEAN)*Math.log(10);
		o.sequencedata_stddev_log_year         = getDependency().value(NodeIDString.Q1_9_1, ScalarSubIDString.STD_DEV)*Math.log(10);

		o.population_base_year                 = ModelUtil.ANCHOR_FAR_YEAR + ModelUtil.RESEARCH_CAREER_DELAY;
		o.population_mean_log_year             = getDependency().value(NodeIDString.Q1_9_2, ScalarSubIDString.MEAN)*Math.log(10);
		o.population_stddev_log_year           = getDependency().value(NodeIDString.Q1_9_2, ScalarSubIDString.STD_DEV)*Math.log(10);

		double t0, t1;
		t0 = 0; // EARLIEST_IA_DELAY
		t1 = ModelUtil.LATEST_IA_DELAY;
		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;
		double mean_init, stddev_init, mean_slope, stddev_slope, mean_final, stddev_final;
		//y0low  = getDependency().value(NodeIDString.Q1_9_3, ScalarSubIDString.yearPercentileID(t0, klow ))*Math.log(10);
		//y0high = getDependency().value(NodeIDString.Q1_9_3, ScalarSubIDString.yearPercentileID(t0, khigh))*Math.log(10);
		y0low  = 0;
		y0high = 0;
		y1low  = getDependency().value(NodeIDString.Q1_9_3, ScalarSubIDString.yearPercentileID(t1, klow ))*Math.log(10);
		y1high = getDependency().value(NodeIDString.Q1_9_3, ScalarSubIDString.yearPercentileID(t1, khigh))*Math.log(10);
		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);
		mean_slope = (mean_final-mean_init)/(t1-t0);
		stddev_slope = (stddev_final-stddev_init)/(t1-t0);
		o.sequencedata_mean_init_log_factor     = mean_init;
		o.sequencedata_mean_slope_log_factor    = mean_slope;
		o.sequencedata_stddev_init_log_factor   = stddev_init;
		o.sequencedata_stddev_slope_log_factor  = stddev_slope;
		
		t0 = 0; // EARLIEST_IA_DELAY
		t1 = ModelUtil.LATEST_IA_DELAY;
		y0low  = getDependency().value(NodeIDString.Q1_9_4, ScalarSubIDString.yearPercentileID(t0, klow ))*Math.log(10);
		y0high = getDependency().value(NodeIDString.Q1_9_4, ScalarSubIDString.yearPercentileID(t0, khigh))*Math.log(10);
		y1low  = getDependency().value(NodeIDString.Q1_9_4, ScalarSubIDString.yearPercentileID(t1, klow ))*Math.log(10);
		y1high = getDependency().value(NodeIDString.Q1_9_4, ScalarSubIDString.yearPercentileID(t1, khigh))*Math.log(10);
		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);
		mean_slope = (mean_final-mean_init)/(t1-t0);
		stddev_slope = (stddev_final-stddev_init)/(t1-t0);
		o.population_mean_init_log_rate         = mean_init - Math.log(ModelUtil.UNENHANCED_RESEARCHERS);
		o.population_mean_slope_log_rate        = mean_slope - Math.log(ModelUtil.UNENHANCED_RESEARCHERS);
		o.population_stddev_init_log_rate       = stddev_init - Math.log(ModelUtil.UNENHANCED_RESEARCHERS);
		o.population_stddev_slope_log_rate      = stddev_slope - Math.log(ModelUtil.UNENHANCED_RESEARCHERS);
		
		t0 = ModelUtil.EARLIEST_YEAR;
		t1 = ModelUtil.LATEST_YEAR;
		y0low  = 0;
		y0high = 0;
		y1low  = getDependency().value(NodeIDString.Q1_9_5, ScalarSubIDString.yearPercentileID(t1, klow ))*Math.log(10);
		y1high = getDependency().value(NodeIDString.Q1_9_5, ScalarSubIDString.yearPercentileID(t1, khigh))*Math.log(10);
		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);
		mean_slope = (mean_final-mean_init)/(t1-t0);
		stddev_slope = (stddev_final-stddev_init)/(t1-t0);
		o.institutional_base_year               = ModelUtil.EARLIEST_YEAR;
		o.institutional_mean_slope_log_factor   = mean_slope;
		o.institutional_stddev_slope_log_factor = stddev_slope;
		
		return o;
	}

}