GCBM
timeseries.h
Go to the documentation of this file.
1 #ifndef MOJA_MODULES_CBM_TIMESERIES_H_
2 #define MOJA_MODULES_CBM_TIMESERIES_H_
3 
4 #include <moja/flint/itiming.h>
5 
6 #include "moja/modules/cbm/_modules.cbm_exports.h"
7 
8 #include <boost/optional.hpp>
9 #include <vector>
10 
11 namespace moja {
12 namespace modules {
13 namespace cbm {
14 
15 enum class ExtrapType {
16  NearestYr = 0,
17  CycleYrs,
18  AvgYr
19 };
20 
21 
27 enum class DateOrigin {
28  Calendar = 0,
29  StartSim,
30  Sprout
31 };
32 
33 enum class Frequency {
34  Annual,
35  Monthly,
36  Daily,
38 };
39 
40 enum class InterpType {
41  Linear,
43 };
44 
45 enum class SubSamplingMode {
46  None,
48 };
49 
55 class CBM_API TimeSeries {
56 public:
57  TimeSeries();
58 
77  TimeSeries(int yr0, int dataPerYr, int nYrs, bool subSame,
78  const std::vector<boost::optional<double>> raw,
80  int extraSteps = 0);
81 
82  ~TimeSeries() = default;
83 
94  void setTiming(const flint::ITiming* timing);
95 
103  void setTiming(DateTime start, DateTime end);
104 
108  const std::vector<double>& series() const;
109 
114  double value() const;
115 
119  int yr0() const;
120 
124  int nYrs() const;
125 
129  int dataPerYr() const;
130 
135  bool subSame() const;
136 
140  const std::vector<boost::optional<double>> raw() const;
141 
145  DateOrigin origin() const;
146 
147  ExtrapType extrap() const;
148 
149 private:
151  std::shared_ptr<TimeSeriesPrep> _impl;
152 };
153 
154 struct CORE_API Observation {
155  DateTime date;
156  boost::optional<double> value;
157 };
158 
159 class CORE_API TimeSeries2 {
160 public:
161  TimeSeries2() : _interpType(InterpType::ColAverage),
162  _extraptype(ExtrapType::NearestYr),
163  _frequency(Frequency::Monthly),
164  _multiplier(1.0) {}
165 
166  TimeSeries2(DateTime start,
167  const std::vector<boost::optional<double>>& data,
168  Frequency frequency = Frequency::Monthly,
169  ExtrapType extraptype = ExtrapType::NearestYr,
170  InterpType interpType = InterpType::ColAverage) :
171  _interpType(interpType), _extraptype(extraptype), _frequency(frequency),
172  _start(start), _multiplier(1.0), _series(data) {}
173 
174  explicit TimeSeries2(const std::vector<Observation>& observations,
175  ExtrapType extraptype = ExtrapType::NearestYr,
176  InterpType interpType = InterpType::Linear) :
177  _interpType(interpType), _extraptype(extraptype), _frequency(Frequency::Observations),
178  _multiplier(1.0), _observations(observations) {}
179 
180  void setData(DateTime start,
181  const std::vector<boost::optional<double>>& data,
182  Frequency frequency = Frequency::Monthly) {}
183 
184  void setObservations(const std::vector<Observation>& observations) {}
185  void addObservation(Observation observation) {}
186 
187  double multiplier() const;
188  void setMultiplier(double multiplier);
189 
190  std::vector<Observation> observations() const;
191  std::vector<double> series(DateTime start, DateTime end) const;
192  double value() const; // by age(duration??) for date???
193 
194  ExtrapType extrapType() const;
195  void setExtrapType(ExtrapType extraptype);
196 
197  InterpType interpType() const;
198  void setInterpType(InterpType interpType);
199 
200  Frequency frequency() const;
201 
202 private:
206  DateTime _start;
207  double _multiplier;
208  std::vector<boost::optional<double>> _series;
209  std::vector<Observation> _observations;
210 };
211 
212 inline double TimeSeries2::multiplier() const {
213  return _multiplier;
214 }
215 
216 inline void TimeSeries2::setMultiplier(double multiplier) {
218 }
219 
220 inline std::vector<Observation> TimeSeries2::observations() const {
221  switch (_frequency)
222  {
224  return _observations;
225  } break;
226  case Frequency::Annual: {
227  auto observations = std::vector<Observation>();
228  auto date = _start;
229  for (auto value : _series) {
230  // Represents the end of the annual step.
231  auto observationDate = DateTime(date.year() + 1, 1, 1).addMicroseconds(-1);
232  observations.push_back({ observationDate, value });
233  date.addYears(1);
234  }
235  return observations;
236  } break;
237  case Frequency::Monthly: {
238  auto observations = std::vector<Observation>();
239  auto date = _start;
240  for (auto value : _series) {
241  // Represents the end of the monthly step.
242  auto observationDate = DateTime(date.year(), date.month(), 1).addMonths(1).addMicroseconds(-1);
243  observations.push_back({ observationDate, value });
244  date.addMonths(1);
245  }
246  return observations;
247  } break;
248  case Frequency::Daily: break;
249  default: break;
250  }
251  return std::vector<Observation>();
252 }
253 
254 inline std::vector<double> TimeSeries2::series(DateTime start, DateTime end) const {
255  // - Later interpolations and extrapolations require the TS table to be complete.
256  // - Empty cells are assigned the average of the column.
257  auto dataPerYr = -1;
258  switch (_frequency)
259  {
260  case Frequency::Annual: dataPerYr = 1; break;
261  case Frequency::Monthly: dataPerYr = 12; break;
262  default: break;
263  }
264  auto nYrs = _series.size() / dataPerYr;
265 
266  std::vector<double> result(_series.size());
267  for (auto c = 0; c < dataPerYr; ++c) { // Calculate col sum
268  double colAvg = 0.0;
269  int n = 0;
270  int ix = c;
271  for (auto r = 0; r < nYrs; ++r) {
272  if (_series[ix].is_initialized()) {
273  colAvg += _series[ix].get();
274  ++n;
275  }
276  ix += dataPerYr;
277  }
278 
279  if (n == 0) {
280  throw std::runtime_error("No data for timeseries column");
281  }
282 
283  colAvg /= n;
284  ix = c;
285  for (auto r = 0; r < nYrs; ++r) {
286  result[ix] = (_series[ix].is_initialized() ? _series[ix].get() : colAvg) * _multiplier;
287  ix += dataPerYr;
288  }
289  }
290 
291  return std::vector<double>();
292 }
293 
294 inline double TimeSeries2::value() const {
295  return 0.0;
296 }
297 
299  return _extraptype;
300 }
301 
302 inline void TimeSeries2::setExtrapType(ExtrapType extraptype) {
303  _extraptype = extraptype;
304 }
305 
307  return _interpType;
308 }
309 
310 inline void TimeSeries2::setInterpType(InterpType interpType) {
312 }
313 
315  return _frequency;
316 }
317 
318 } // namespace cbm
319 } // namespace modules
320 } // namespace moja
321 
322 #endif // MOJA_MODULES_CBM_TIMESERIES_H_
moja::modules::cbm::TimeSeries2::setInterpType
void setInterpType(InterpType interpType)
Definition: timeseries.h:310
moja::modules::cbm::Frequency::Daily
@ Daily
moja::modules::cbm::ExtrapType::CycleYrs
@ CycleYrs
moja::modules::cbm::TimeSeries2::_series
std::vector< boost::optional< double > > _series
Definition: timeseries.h:208
moja::modules::cbm::TimeSeries2
Definition: timeseries.h:159
moja::modules::cbm
Definition: ageclasshelper.cpp:12
moja::modules::cbm::TimeSeries2::setObservations
void setObservations(const std::vector< Observation > &observations)
Definition: timeseries.h:184
moja::modules::cbm::Frequency
Frequency
Definition: timeseries.h:33
moja::modules::cbm::TimeSeries2::_extraptype
ExtrapType _extraptype
Definition: timeseries.h:204
moja::modules::cbm::InterpType::ColAverage
@ ColAverage
moja::modules::cbm::DateOrigin
DateOrigin
The date origin of the timeseries. Calendar allows yr0 to be specified as a year....
Definition: timeseries.h:27
moja::modules::cbm::Observation::value
boost::optional< double > value
Definition: timeseries.h:156
moja::modules::cbm::DateOrigin::StartSim
@ StartSim
moja::modules::cbm::TimeSeries2::addObservation
void addObservation(Observation observation)
Definition: timeseries.h:185
moja::modules::cbm::Observation::date
DateTime date
Definition: timeseries.h:155
moja::modules::cbm::SubSamplingMode::None
@ None
moja::modules::cbm::TimeSeries2::_interpType
InterpType _interpType
Definition: timeseries.h:203
moja::modules::cbm::Observation
Definition: timeseries.h:154
moja::modules::cbm::TimeSeries
Encapsulates a timeseries of data. Normal usage allows modules to retrieve the most relevant data for...
Definition: timeseries.h:55
moja::modules::cbm::TimeSeries2::_multiplier
double _multiplier
Definition: timeseries.h:207
moja::modules::cbm::TimeSeries2::TimeSeries2
TimeSeries2()
Definition: timeseries.h:161
moja::modules::cbm::DateOrigin::Calendar
@ Calendar
moja::modules::cbm::TimeSeries2::_start
DateTime _start
Definition: timeseries.h:206
moja::modules::cbm::Frequency::Monthly
@ Monthly
moja::modules::cbm::Frequency::Observations
@ Observations
moja::modules::cbm::TimeSeries2::extrapType
ExtrapType extrapType() const
Definition: timeseries.h:298
moja::modules::cbm::TimeSeries::_impl
std::shared_ptr< TimeSeriesPrep > _impl
Definition: timeseries.h:150
moja::modules::cbm::TimeSeries2::value
double value() const
Definition: timeseries.h:294
moja::modules::cbm::TimeSeries2::observations
std::vector< Observation > observations() const
Definition: timeseries.h:220
moja::modules::cbm::TimeSeries2::TimeSeries2
TimeSeries2(const std::vector< Observation > &observations, ExtrapType extraptype=ExtrapType::NearestYr, InterpType interpType=InterpType::Linear)
Definition: timeseries.h:174
moja::modules::cbm::TimeSeries2::_frequency
Frequency _frequency
Definition: timeseries.h:205
moja::modules::cbm::TimeSeries::TimeSeriesPrep
Definition: timeseries.cpp:13
moja::modules::cbm::ExtrapType::AvgYr
@ AvgYr
moja::modules::cbm::InterpType::Linear
@ Linear
moja::modules::cbm::TimeSeries2::frequency
Frequency frequency() const
Definition: timeseries.h:314
moja::modules::cbm::SubSamplingMode::Interpolate
@ Interpolate
moja::modules::cbm::SubSamplingMode
SubSamplingMode
Definition: timeseries.h:45
moja::modules::cbm::TimeSeries2::series
std::vector< double > series(DateTime start, DateTime end) const
Definition: timeseries.h:254
moja::modules::cbm::TimeSeries2::setExtrapType
void setExtrapType(ExtrapType extraptype)
Definition: timeseries.h:302
moja::modules::cbm::TimeSeries2::interpType
InterpType interpType() const
Definition: timeseries.h:306
moja::modules::cbm::DateOrigin::Sprout
@ Sprout
moja::modules::cbm::InterpType
InterpType
Definition: timeseries.h:40
moja::modules::cbm::ExtrapType::NearestYr
@ NearestYr
moja::modules::cbm::ExtrapType
ExtrapType
Definition: timeseries.h:15
moja::modules::cbm::TimeSeries2::setMultiplier
void setMultiplier(double multiplier)
Definition: timeseries.h:216
moja::modules::cbm::TimeSeries2::setData
void setData(DateTime start, const std::vector< boost::optional< double >> &data, Frequency frequency=Frequency::Monthly)
Definition: timeseries.h:180
moja::modules::cbm::TimeSeries2::_observations
std::vector< Observation > _observations
Definition: timeseries.h:209
moja
Definition: ageclasshelper.cpp:10
moja::modules::cbm::TimeSeries2::multiplier
double multiplier() const
Definition: timeseries.h:212
moja::modules::cbm::TimeSeries2::TimeSeries2
TimeSeries2(DateTime start, const std::vector< boost::optional< double >> &data, Frequency frequency=Frequency::Monthly, ExtrapType extraptype=ExtrapType::NearestYr, InterpType interpType=InterpType::ColAverage)
Definition: timeseries.h:166
moja::modules::cbm::Frequency::Annual
@ Annual