1 #include "../include/cal_date.h" 3 CalDate::CalDate(
string name, vector<double> probabilities, vector<int> bp,
int uncal_bp,
int uncal_error, vector<int> full_bp, vector<double> full_probabilities):
5 _probabilities(probabilities),
8 _uncal_error(uncal_error),
9 _full_probabilities(full_probabilities),
13 CalDate::CalDate(
string name, vector<double> probabilities, vector<int> bp,
int uncal_bp,
int uncal_error):
15 _probabilities(probabilities),
18 _uncal_error(uncal_error)
26 std::cout <<
"calibrated_date";
27 std::cout <<
"bp: " << _uncal_bp;
28 std::cout <<
"std: " << _uncal_error;
29 std::cout << std::endl;
34 return_value[
"uncal_bp"] = _uncal_bp;
35 return_value[
"uncal_error"] = _uncal_error;
36 return_value[
"probabilities"] = prob_to_json();
37 return_value[
"bp"] = bp_to_json();
38 return_value[
"sigma_ranges"] = sigma_ranges_to_json();
42 json CalDate::prob_to_json(){
43 json return_value(_probabilities);
47 json CalDate::bp_to_json(){
48 json return_value(_bp);
61 return _full_probabilities;
65 static const double arr[] = {1 - 0.954,1 - 0.682};
66 vector<int> my_sigma_ranges;
67 for (
unsigned t = 0; t <
sizeof(arr)/
sizeof(arr[0]); t++) {
68 double this_sigma = arr[t];
69 my_sigma_ranges.clear();
70 while (my_sigma_ranges.empty() || my_sigma_ranges.size() % 2) {
71 my_sigma_ranges = sigma_range_helper(this_sigma);
73 for (
unsigned i=0;i < my_sigma_ranges.size(); i += 2) {
75 _sigma_ranges.push_back(this_sigma_range);
80 vector<int> CalDate::sigma_range_helper(
double &prob){
82 vector<double> prob_vector;
83 vector<double> cum_sum(_probabilities.size());
85 std::partial_sum(_probabilities.begin(), _probabilities.end(), cum_sum.begin(), plus<double>());
87 double max=cum_sum.back();
92 double linear = rand()*max/RAND_MAX;
93 int up = upper_bound(cum_sum.begin(), cum_sum.end(), linear) - cum_sum.begin();
94 prob_vector.push_back(_probabilities[up]);
97 std::sort (prob_vector.begin(), prob_vector.end());
99 int n = prob_vector.size();
100 int thres_index = floor( (n - 1) * prob);
101 double threshold = prob_vector[thres_index];
104 vector<double>::iterator lower;
105 vector<double>::iterator upper;
107 while ( i < _probabilities.size()) {
108 lower = find_if (_probabilities.begin()+i, _probabilities.end(),
109 bind2nd(greater<double>(),threshold));
110 upper = find_if (lower, _probabilities.end(),
111 bind2nd(less<double>(),threshold));
113 if (upper == _probabilities.end())
break;
115 index.push_back(lower - _probabilities.begin());
116 index.push_back(upper - _probabilities.begin());
117 i = upper - _probabilities.begin();
121 int ni = index.size();
122 vector<int> bp_collector;
125 for (
int j = 0; j < ni; j++)
131 double mu = (float)(threshold-_probabilities[a]) / (float)(_probabilities[b] - _probabilities[a]);
132 int this_sigma_end = round(LinearInterpolate(y1, y2, mu));
133 bp_collector.push_back(this_sigma_end);
136 std::unique (bp_collector.begin(), bp_collector.end());
137 bp_collector.erase( unique( bp_collector.begin(), bp_collector.end() ), bp_collector.end() );
141 double CalDate::LinearInterpolate(
double y1,
double y2,
double mu){
142 return(y1*(1-mu)+y2*mu);
146 return _sigma_ranges;
149 json CalDate::sigma_ranges_to_json() {
150 json sigma_ranges_json;
151 for(
auto &this_sigma_range : _sigma_ranges)
152 sigma_ranges_json.push_back(this_sigma_range.to_json());
153 return sigma_ranges_json;
157 std::stringstream ss;
159 for(
unsigned i = 0; i < _bp.size(); ++i)
161 ss << _name <<
"," << _bp[i] <<
"," << _probabilities[i] <<
"\n";
163 std::string return_value = ss.str();
vector< double > get_full_probabilities()
Represents a sigma range for a calibrated date.
vector< SigmaRange > get_sigma_ranges()
vector< int > get_full_bp()
void calculate_sigma_ranges()