SplineSmooth.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2020 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 #ifndef __Pulsar_SplineSmooth_h
10 #define __Pulsar_SplineSmooth_h
11 
12 #include "ReferenceAble.h"
13 #include "Estimate.h"
14 
15 namespace Pulsar {
16 
18 
19  class SplineSmooth : public Reference::Able
20  {
21  public:
22 
25 
27  SplineSmooth (const std::string& json_string);
28 
30  ~SplineSmooth();
31 
33  void set_alpha (double _alpha) { alpha = _alpha; }
34  double get_alpha () const { return alpha; }
35 
37  void unload (const std::string&) const;
39  void load (const std::string&);
40 
41  protected:
42 
44  double evaluate (const std::vector<double>& xval);
45 
47  template<typename T>
48  void new_spline (const std::vector<T>& data_x,
49  const std::vector< Estimate<double> >& data_y);
50 
51  private:
52 
54  double alpha;
55 
56  class Handle;
57  Handle* handle;
58  };
59 
60  class SplineSmooth1D : public SplineSmooth
61  {
62  public:
63 
64  void fit (const std::vector< double >& data_x,
65  const std::vector< Estimate<double> >& data_y);
66 
67  double evaluate (double x);
68  };
69 
70  class SplineSmooth2D : public SplineSmooth
71  {
72  public:
73 
75  SplineSmooth2D () {}
76 
78  SplineSmooth2D (const std::string& json) : SplineSmooth (json) {}
79 
80  void fit (const std::vector< std::pair<double,double> >& data_x,
81  const std::vector< Estimate<double> >& data_y);
82 
83  double evaluate ( const std::pair<double,double>& );
84  };
85 
87 
95  class CrossValidatedSmooth2D
96  {
97  bool logarithmic; // smoothing factors on logarithmic scale
98 
99  unsigned npartition; // m=40 in Clark (1977)
100  double validation_fraction; // 0.1 in Clark (1977)
101 
102  SplineSmooth2D* spline; // the 2-D spline implementation
103 
104  double iqr_threshold; // Tukey's fence used to detect outliers
105  unsigned nflagged_iqr;
106 
107  double gof_step_threshold;
108  unsigned nflagged_gof;
109 
110  std::vector<double> gof_tot;
111  std::vector<unsigned> gof_count;
112 
113  std::string gof_filename;
114  std::ofstream* gof_out;
115 
116  public:
117 
119 
120  void set_gof_filename (const std::string& name) { gof_filename = name; }
121 
122  void set_spline (SplineSmooth2D* _spline) { spline = _spline; }
123 
125 
126  void set_npartition (unsigned m) { npartition = m; }
127  unsigned get_npartition () const { return npartition; }
128 
130 
131  void set_validation_fraction (double f) { validation_fraction = f; }
132  double get_validation_fraction () const { return validation_fraction; }
133 
135 
136  void set_iqr_threshold (double t) { iqr_threshold = t; }
137  double get_iqr_threshold () const { return iqr_threshold; }
138 
140 
141  void set_gof_step_threshold (double s) { gof_step_threshold = s; }
142  double get_gof_step_threshold () const { return gof_step_threshold; }
143 
145  void fit ( std::vector< std::pair<double,double> >& data_x,
146  std::vector< Estimate<double> >& data_y );
147 
148  void remove_iqr_outliers
149  ( std::vector< std::pair<double,double> >& x,
150  std::vector< Estimate<double> >& y );
151 
152  unsigned get_nflagged_iqr () const { return nflagged_iqr; }
153 
154  void find_optimal_smoothing_factor
155  ( const std::vector< std::pair<double,double> >& dat_x,
156  const std::vector< Estimate<double> >& dat_y );
157 
158  void remove_gof_outliers
159  ( std::vector< std::pair<double,double> >& x,
160  std::vector< Estimate<double> >& y );
161 
162  unsigned get_nflagged_gof () const { return nflagged_gof; }
163 
165  double get_mean_gof (const std::vector< std::pair<double,double> >& data_x,
166  const std::vector< Estimate<double> >& data_y);
167 
169  double get_mean_gof (double log_10_alpha,
170  const std::vector< std::pair<double,double> >& data_x,
171  const std::vector< Estimate<double> >& data_y);
172 
173  };
174 
175  class BootstrapUncertainty2D
176  {
177  unsigned nsample;
178  SplineSmooth2D* spline; // the spline implementation
179 
180  public:
181 
182  BootstrapUncertainty2D ();
183 
184  void set_spline (SplineSmooth2D* _spline) { spline = _spline; }
185 
186  void get_uncertainty (const std::vector< std::pair<double,double> >& data_x,
187  std::vector< Estimate<double> >& data_y);
188 
189  };
190 
191 }
192 
193 #endif
194 
void fit(const Pulsar::Parameters *model, std::vector< toa > &data, Pulsar::Parameters *postfit=NULL, bool track=false, Tempo::toa::State min_state=Tempo::toa::Normal)
void new_spline(const std::vector< T > &data_x, const std::vector< Estimate< double > > &data_y)
constructor used by derived types
Definition: SplineSmooth.C:67
void set_alpha(double _alpha)
Set the smoothing factor.
Definition: SplineSmooth.h:43
SplineSmooth()
Constructor.
Definition: SplineSmooth.C:29
void set_gof_step_threshold(double s)
Set the goodness-of-fit step threshold.
Definition: SplineSmooth.h:146
double get_mean_gof(const std::vector< std::pair< double, double > > &data_x, const std::vector< Estimate< double > > &data_y)
Return the mean goodness-of-fit for the current smoothing.
void unload(const std::string &) const
Unload spline to specified filename.
Definition: SplineSmooth.C:101
Determines the spline smoothing factor as in Clark (1977)
Definition: SplineSmooth.h:100
void set_validation_fraction(double f)
Set the fraction of data reserved for validation.
Definition: SplineSmooth.h:136
double evaluate(const std::vector< double > &xval)
evaluate method used by derived types
Definition: SplineSmooth.C:145
Defines the PSRCHIVE library.
Definition: CalSource.h:17
void load(const std::string &)
Load spline from specified filename.
Definition: SplineSmooth.C:111
void fit(std::vector< std::pair< double, double > > &data_x, std::vector< Estimate< double > > &data_y)
Fit spline to data using current configuration.
Definition: CrossValidatedSmooth2D.C:80
void set_npartition(unsigned m)
Set the number of cross-validation iterations, m.
Definition: SplineSmooth.h:131
void set_iqr_threshold(double t)
Set the multiple of IQR used to implement Tukey's fence outlier detection.
Definition: SplineSmooth.h:141
~SplineSmooth()
Destructor.
Definition: SplineSmooth.C:50

Generated using doxygen 1.8.17