﻿#include <cmath>
#include <map>
#include <vector>
#include <limits>
#include <algorithm>

#include <stdio.h>
#include <stdlib.h>

const double DATA[] = {
40212.86065,
35977.23,
35466.50694,
33914.1735,
33629.83855,
33841.9333,
34256.4035,
33086.6242,
34458.3673,
34996.94976,
31900.023,
31935.68,
34409.817,
33525.532,
31198.488,
30977.8365,
29667.156,
28974.7908,
28737.9978,
26443.2666,
23306.283,
26304.0414,
25536.4093,
25271.67435,
28995.1018,
30157.7716,
31067.5872,
32464.7904,
33132.03065,
34132.11125,
32337.4072,
32015.0151,
31253.1696,
33160.275,
34281.723,
34511.2444,
37535.4978,
37015.04625,
37190.6721,
36821.1102,
36173.97,
36884.4402,
36090.5472,
35270.30144,
35400.8187,
35007.8652,
35230.7109,
37090.577,
35246.057,
34208.669,
33298.4613,
33649.3725,
33131.2494,
33030.8095,
32555.9739,
32566.82311,
32602.0055,
30181.2678,
29822.4366,
29954.88,
28922.74056,
28116.97824,
27064.6866,
28637.9295,
28919.12425,
26322.013,
26157.5352,
26866.38,
26992.8399,
24102.47735,
26500.0572,
26894.1448,
28660.0256,
28380.59925,
31068.0734,
32673.5118,
31979.3123,
32638.753,
32933.4453,
34987.72995,
36346.15355,
35821.7164,
39234.374,
38489.0762,
40913.1264,
43125.9716,
37870.7505,
38391.3215,
37853.5872,
38622.5849,
42028.4995,
41058.352,
40629.8,
41796.4855,
45187.173,
40810.1948,
39524.598,
40486.8256,
42225.6702,
42288.78,
43160.7555,
43144.437,
45250.17525,
41731.7775,
39264.3966,
41075.43375,
40346.2883,
40245.62175,
41144.3019,
39982.3536,
41703.4748,
41298.3873,
41629.0777,
41811.30215,
41655.3216,
45751.6444,
48072.2748,
53468.0178,
50351.64425,
48213.9112,
50525.0656,
51002.93485,
53579.5093,
57524.1131,
57038.7616,
59111.6592,
58172.52,
59948.65275,
57882.0399,
56519.23405,
58261.4656,
57690.512,
58354.0166,
56846.37024,
55591.463,
56043.195,
53795.60424,
55678.2258,
56032.55325,
60897.5235,
61591.4736,
61635.5498,
62586.5965,
64776.8184,
70383.22978,
71128.393,
70824.5049,
71388.5496,
75445.37344,
75404.71445,
73422.5557,
73850.7225,
66048.048,
68649.4632,
69901.6221,
72392.3274,
70281.60975,
71168.0364,
75290.5964,
72111.8742,
73664.928,
71493.3486,
64581.91325,
70963.89355,
65036.95605,
71070.6129,
73693.4614,
72518.5758,
75692.9477,
79154.50545,
78213.32175,
75612.51495,
78279.07185,
83017.79575,
88492.116,
86730.4386,
88422.0357,
65422.2016,
58571.7741,
57379.528,
60124.59936,
63343.68108,
61445.1075,
62453.81182,
62368.9248,
67693.01,
67850.98651,
66305.90208,
68718.3554,
67439.125,
65461.84105,
68396.434,
74501.03698,
71456.2656,
75650.65915,
78727.54535,
84667.3348,
86398.64605,
91350.6432,
95762.67584,
93015.42825,
91236.1345,
94642.9558,
100214.373,
97048.0632,
99362.70188,
107225.8052,
105224.1023,
109049.2193,
111240.1031,
109450.605,
97326.9635,
85599.9063,
82623.606,
87880.89825,
90445.3044,
91261.1106,
98700.31535,
103603.1999,
101265.0728,
105204.0566,
99269.3403,
104376.1781,
105588.8214,
102716.5053,
101630.455,
97687.90287,
102536.4135,
102186.8165,
105931.0991,
106155.4061,
110448.2434,
109204.4668,
105110.3345,
107224.9173,
102035.2634,
98127.12825,
99349.91385,
101979.133,
103428.9608,
103658.8663,
99973.92275,
100929.5198,
97213.2777,
95979.9274,
94520.80836,
93987.9072,
97900.13378,
98098.6584,
105063.8396,
104031.3759,
112745.7347,
114328.8997,
105165.2728,
99139.6004,
101304.2933,
102728.2909,
95025.0493,
100054.4129,
103599.6975,
99789.8403,
101366.4543,
98822.32486,
100534.4005,
99428.57664,
101410.7831,
95525.25786,
93219.42354,
95717.24934,
99007.29104,
107155.9988,
116458.1702,
121460.7361,
125590.1196,
128723.7219,
133233.9752,
142207.7699,
140879.1951,
145040.1254,
143963.5846,
152072.1532,
154918.4934,
146073.8976,
151820.0536,
162348.6578,
169321.433,
180535.5627,
183805.336,
200802.36,
200558.8982,
202072.9596,
213462.6866,
206114.1661,
211371.7896,
231201.6816,
219602.2649,
238351.9882,
225268.7671,
246340.9062,
256285.2589,
256041.2646,
274307.1091,
305217.6624,
305322.9316,
318520.1246,
329264.7035,
338131.9627,
287664.3075,
282075.2906,
261071.0861,
295422.0978,
288523.584,
297260.3354,
299976.0486,
308394.6983,
319349.7875,
312989.927,
324422.0905,
304474.178,
290703.5886,
273125.817,
285579.3878,
286010.6153,
308945.5283,
304265.0871,
316080.1334,
322007.0382,
313381.1802,
309082.1566,
312275.2202,
319466.6639,
319182.9314,
305762.3905,
305262.5378,
291876.8545,
309808.4319,
321007.6954,
292532.3284,
292611.679,
311356.8557,
294519.4364,
298819.3935,
298296.8929,
269832.2152,
246946.6427,
257658.825,
279079.8479,
299938.1559,
295400.9223,
293599.3476,
303477.7775,
280194.0261,
270180.6128,
243807.7613,
223985.2774,
221187.9695,
201672.5482,
220991.5214,
233076.2942,
215703.1418,
209058.577,
202424.967,
204778.6708,
225606.0635,
236955.0023,
242307.313,
247358.0172,
244555.4927,
230833.1557,
241533.5019,
247129.8306,
256763.9419,
258173.8744,
270952.9747,
252931.244,
264245.26,
265794.313,
268139.643,
267325.808,
264066.1975,
272909.7864,
266814.1034,
273785.84,
285535.5181,
278590.9432,
290523.7629,
291433.2398,
282271.6527,
294349.6079,
303240.2506,
319783.6734,
317997.3596,
328962.8544,
327265.0153,
348869.6699,
348009.8292,
362357.5889,
359585.2598,
370869.2358,
371944.3498,
352859.3468,
362127.0089,
364332.3687,
383326.8601,
392012.4126,
406268.228,
411335.2435,
429589.9378,
443684.0782,
428994.107,
436574.871,
464538.4542,
487060.767,
490390.8181,
460985.1085,
451498.2456,
470273.4741,
484438.0576,
445323.1166,
452206.0234,
392282.7065,
381092.1464,
360729.3967,
396742.4419,
407804.8559,
374284.4672,
375269.7065,
373391.9168,
316493.474,
236152.2191,
215573.1111,
209485.1703,
188959.996,
186063.7888,
202338.5944,
223973.1325,
240876.0626,
236926.686,
258389.6582,
261143.8765,
264465.2736,
263882.0752,
261159.3314,
284615.1821,
265301.461,
266595.3844,
295945.9898,
298928.5443,
262079.2348,
244846.0271,
259133.7518,
242891.4665,
263950.5945,
265581.9399,
269380.9371,
280437.6818,
289312.009,
297792.2112,
301800.2856,
311070.2976,
304485.1776,
294455.0957,
277426.2262,
254839.0184,
231327.2,
265118.495,
256544.3881,
254732.8211,
263260.5605,
291238.8183,
301121.8254,
293129.8803,
261504.8989,
277607.8718,
277457.8238,
285998.7828,
290666.2707,
297022.2352,
311469.5709,
330497.7,
366303.4966,
371861.0327,
386572.7671,
412617.2975,
427092.1353,
407398.7061,
426519.7349,
418348.3769,
437139.8355,
458071.8503,
484834.5533,
510886.8665,
478590.5837,
500981.6999,
508876.3909,
513357.1637,
518115.6935,
524544.1053,
522662.1017,
541299.4309,
553947.1753,
567742.7243,
616419.4138,
614488.5826,
591608.4057,
633538.3346,
626616.2628,
633677.8111,
661134.2513,
637547.6865,
660286.0685,
601052.2273,
575144.2701,
623213.3951,
631157.656,
606658.3787,
572703.5912,
533468.3872,
567866.3909,
554632.7192,
572709.199,
523958.6599,
549928.4504,
548368.6715,
538023.2178,
546856.9944,
598301.7365,
637281.5859,
631936.1354,
643496.8792,
648267.6044,
655126.614,
666456.7178,
675224.7018,
682796.6332,
683019.3477,
710988.7641,
726636.8377,
736548.7378,
749381.2571,
761780.7961,
717406.2318,
695883.0337,
725503.604,
727010.8486,
740923.7731,
771229.3296,
777351.2093,
798428.9493,
739063.8956,
749746.4437,
673386.8976,
};

double find_cdf_pos(const std::vector<double> &data, double cdf_target) {
	size_t max = data.size();
	size_t idx = static_cast<size_t>( std::floor((max-1) * cdf_target ) );
	if (idx == (max-1)) {
		return data[idx];
	}
	double p = data[idx];
	double n = data[idx+1];
	double ip_range = 1.0 / (max-1);
	double frac = cdf_target - (static_cast<double>(idx)/(max-1));
	return (p*(ip_range-frac)+n*frac)/ip_range; // 線形補間
}

void print_stat(int interval) {

	std::vector<double> work_data;

	size_t max = sizeof(DATA) / sizeof(DATA[0]) - interval;
	double sum = 0.0;
	double sum_sq = 0.0;
	for (size_t i=0; i<max; ++i) {
		double ratio = DATA[i+interval] / DATA[i];
		work_data.push_back(ratio);
		sum += ratio;
		sum_sq += ratio*ratio;
	}
	double avg = sum / max;
	double sq_avg = sum_sq / max;
	double sigma = std::sqrt(sq_avg - (avg*avg));

	std::sort(work_data.begin(), work_data.end());
	double low_rank_16 = find_cdf_pos(work_data, 0.16);
	double high_rank_16 = find_cdf_pos(work_data, 1-0.16);
	
	printf("%d\t%f\t%f\t%f\t%f\n", interval, sigma, high_rank_16, avg, low_rank_16);
}

int main(int argc, char **argv) {

	printf("interval\tsigma\thigh_rank_16\taverage\tlow_rank_16\n");
	for (int interval=1; interval<12; ++interval ) {
		print_stat(interval);
	}

	for (int interval=12; interval<=240; interval+=12 ) {
		print_stat(interval);
	}

	return EXIT_SUCCESS;
}
