Calibrator  0.1
Command line tool for 14C calibration
uncal_date.cpp
Go to the documentation of this file.
1 #include "../include/uncal_date.h"
2 
3 UncalDate::UncalDate(string name, int bp, int std):
4 _name(name),
5 _bp(bp),
6 _std(std)
7 {}
8 
10  _bp(),
11  _std()
12 {}
13 
14 void UncalDate::info()const{
15  std::cout << "name: " << _name << "\n";
16  std::cout << "bp: " << _bp << "\n";
17  std::cout << "std: " << _std << "\n";
18  std::cout << std::endl;
19 };
20 
22  int step = 5;
23  int num_elements = round((calcurve.max_bp_cal_curve() - calcurve.min_bp_cal_curve())/step);
24 
25  vector<int> full_bp(num_elements);
26  vector<int> full_c14_bp(num_elements);
27  vector<int> full_error(num_elements);
28 
29  generate_date_grid(full_bp, full_c14_bp, full_error, calcurve);
30 
31  vector<double> probs = compute_probs(full_error, full_c14_bp);
32  vector<double> probs_return;
33  vector<int> bp_return;
34  for (unsigned i=0; i < probs.size(); i++)
35  {
36  if (probs[i] > 1.0e-05)
37  {
38  probs_return.push_back(probs[i]);
39  bp_return.push_back(full_bp[i]);
40  }
41  }
42 
43  return CalDate(_name, probs_return, bp_return, _bp, _std, full_bp, probs);
44  // return CalDate(_name, probs_return, bp_return, _bp, _std);
45 };
46 
47 vector<double> UncalDate::compute_probs(vector<int> &error_cal_curve, vector<int> &full_c14_bp){
48  using boost::math::students_t; // typedef provides default type is double.
49  unsigned t = 0;
50  std::vector<double> prob_return_value;
51  students_t dist(100);
52  double prob_sum = 0;
53  int stdsq = pow(_std,2);
54  while (t < error_cal_curve.size()){
55  double this_tau = stdsq + error_cal_curve[t];
56  double this_value = (_bp - full_c14_bp[t]) / sqrt(this_tau);
57  double this_prob;
58  this_prob = pdf(dist, this_value);
59  prob_return_value.push_back(this_prob);
60  prob_sum += this_prob;
61  t++;
62  }
63  for(double& f : prob_return_value) f/=prob_sum*5; // norm to 1
64  return prob_return_value;
65 }
66 
67 void UncalDate::generate_date_grid(std::vector<int>& full_bp, std::vector<int>& full_c14_bp, std::vector<int>& full_error, CalCurve& calcurve){
68 
69  vector<int> cal_bp = calcurve.get_bp();
70  vector<int> error = calcurve.get_error();
71  vector<int> c14_bp = calcurve.get_c14_bp();
72 
73  int num_elements = round((calcurve.max_bp_cal_curve() - calcurve.min_bp_cal_curve())/5);
74  int i = 0;
75  int max_bp = calcurve.max_bp_cal_curve();
76 
77  vector<int> working_bp = cal_bp;
78  vector<int> working_c14_bp = c14_bp;
79  vector<int> working_error = error;
80 
81  std::reverse(working_bp.begin(), working_bp.end());
82  std::reverse(working_c14_bp.begin(), working_c14_bp.end());
83  std::reverse(working_error.begin(), working_error.end());
84 
85  while (i < num_elements) {
86  int this_bp = max_bp - i * 5;
87  vector<int>::iterator it=find(cal_bp.begin(), cal_bp.end(), this_bp);
88 
89  int pos = it - cal_bp.begin();
90  int this_c14;
91  int this_error;
92  if(it!=cal_bp.end()){
93  this_c14 = c14_bp[pos];
94  this_error = error[pos];
95  }
96  else{
97  auto const upper_it = std::upper_bound(working_bp.begin(), working_bp.end(), this_bp);
98  int upper_pos = upper_it - working_bp.begin();
99  int upper_bp = working_bp[upper_pos];
100  int lower_bp = working_bp[upper_pos-1];
101  int upper_c14 = working_c14_bp[upper_pos];
102  int lower_c14 = working_c14_bp[upper_pos-1];
103  int upper_error = working_error[upper_pos];
104  int lower_error = working_error[upper_pos-1];
105  double mu = (double)(this_bp-lower_bp) / (double)(upper_bp - lower_bp);
106  this_c14 = LinearInterpolateInt(lower_c14, upper_c14,mu);
107  this_error = LinearInterpolateInt(lower_error, upper_error,mu);
108  }
109  full_bp[i] = this_bp;
110  full_c14_bp[i] = this_c14;
111  full_error[i] = this_error;
112  i++;
113  }
114 }
115 
116 int UncalDate::LinearInterpolateInt(int y1, int y2, double mu){
117  return (int)round(y1*(1-mu)+y2*mu);
118 }
vector< int > get_error()
Definition: cal_curve.cpp:60
vector< int > get_bp()
Definition: cal_curve.cpp:64
int min_bp_cal_curve()
Definition: cal_curve.cpp:55
CalDate calibrate(CalCurve &calcurve)
Definition: uncal_date.cpp:21
Represents a calibrated date.
Definition: cal_date.h:33
void info() const
Definition: uncal_date.cpp:14
int max_bp_cal_curve()
Definition: cal_curve.cpp:50
Represents the calibration curve.
Definition: cal_curve.h:32
vector< int > get_c14_bp()
Definition: cal_curve.cpp:68