MeteoIODoc 20240518.aefd3c94
InterpolARIMA.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/***********************************************************************************/
3/* Copyright 2013 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
4/***********************************************************************************/
5/* This file is part of MeteoIO.
6 MeteoIO is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 MeteoIO is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef INTERPOLARIMA_H
21#define INTERPOLARIMA_H
22
23#include <iostream>
24#include <map>
25#include <meteoio/thirdParty/ctsa.h>
27#include <string>
28#include <vector>
29
30namespace mio {
31
32 using namespace ARIMAutils;
90 public:
92 InterpolARIMA(std::vector<double> data_in, size_t gap_loc, size_t N_gap, int s = 0);
93 InterpolARIMA(std::vector<double> data_in, size_t gap_loc, size_t N_gap, std::vector<double> xreg_vec, int s = 0);
94 InterpolARIMA(std::vector<double> data_in, size_t gap_loc, size_t n_predictions, std::string direction = "forward", int s = 0);
95
96 // Setters
97 void setAutoArimaMetaData(int max_p_param = 8, int max_d_param = 3, int max_q = 8, int start_p = 2, int start_q = 2, int max_P = 2,
98 int max_D = 1, int max_Q = 2, int start_P = 1, int start_Q = 1, bool seasonal = true,
99 bool stationary = false);
100 void setOptMetaData(ObjectiveFunction method = CSS_MLE, OptimizationMethod opt_method = BFGS, bool stepwise = true,
101 bool approximation = false, int num_models = 94);
102 void setVerbose(bool verbose = false);
104 void setManualARIMA(int p, int d, int q, int P, int D, int Q, bool fill_backward);
105
106 // Interpolation methods
107 std::vector<double> simulate(int n_steps, int seed = 0);
108 void fillGap();
109 void fillGapManual();
110
111 void interpolate();
112 std::vector<double> predict(size_t n_steps = 0);
113 // only do denormalization ARIMA predict! otherwise it will denorm twice
114 std::vector<double> ARIMApredict(size_t n_steps);
115
116 // Getters
117 std::vector<double> getData() { return norm.denormalize(data); }
118 std::vector<double> getForwardData() { return norm.denormalize(data_forward); }
119 std::vector<double> getBackwardData() { return norm.denormalize(data_backward); }
120 std::vector<double> getInterpolatedData();
121
122 // Copy constructor
124 : norm(other.norm), data(other.data), gap_loc(other.gap_loc), N_gap(other.N_gap), time(other.time), pred_forward(other.pred_forward),
125 pred_backward(other.pred_backward), xreg_vec_f(other.xreg_vec_f), xreg_vec_b(other.xreg_vec_b),
126 data_forward(other.data_forward), data_backward(other.data_backward), new_xreg_vec_f(other.new_xreg_vec_f),
127 new_xreg_vec_b(other.new_xreg_vec_b), xreg_f((xreg_vec_f.empty()) ? nullptr : &xreg_vec_f[0]),
128 xreg_b((xreg_vec_b.empty()) ? nullptr : &xreg_vec_b[0]), new_xreg_f((new_xreg_vec_f.empty()) ? nullptr : &new_xreg_vec_f[0]),
129 new_xreg_b((new_xreg_vec_b.empty()) ? nullptr : &new_xreg_vec_b[0]), amse_forward(other.amse_forward),
130 amse_backward(other.amse_backward), N_data_forward(other.N_data_forward), N_data_backward(other.N_data_backward),
131 max_p(other.max_p), max_d(other.max_d), max_q(other.max_q), start_p(other.start_p), start_q(other.start_q),
132 max_P(other.max_P), max_D(other.max_D), max_Q(other.max_Q), start_P(other.start_P), start_Q(other.start_Q), r(other.r),
133 s(other.s), method(other.method), opt_method(other.opt_method), stepwise(other.stepwise), approximation(other.approximation),
134 num_models(other.num_models), seasonal(other.seasonal), stationary(other.stationary),
135 auto_arima_forward(auto_arima_copy(other.auto_arima_forward)), auto_arima_backward(auto_arima_copy(other.auto_arima_backward)), sarima_forward(other.sarima_forward) {
136 }
137
138 // Copy assignment operator
140 operator=(const InterpolARIMA &other) {
141 if (this != &other) // protect against invalid self-assignment
142 {
143 auto_arima_forward = auto_arima_copy(other.auto_arima_forward);
144 auto_arima_backward = auto_arima_copy(other.auto_arima_backward);
145
146 // 3: copy all the other fields from the other object
147 gap_loc = other.gap_loc;
148 N_gap = other.N_gap;
149 time = other.time;
150 pred_forward = other.pred_forward;
151 pred_backward = other.pred_backward;
152 norm = other.norm;
153 data = other.data;
154 xreg_vec_f = other.xreg_vec_f;
155 xreg_vec_b = other.xreg_vec_b;
156 data_forward = other.data_forward;
157 data_backward = other.data_backward;
158 new_xreg_vec_f = other.new_xreg_vec_f;
159 new_xreg_vec_b = other.new_xreg_vec_b;
160 N_data_forward = other.N_data_forward;
161 N_data_backward = other.N_data_backward;
162 max_p = other.max_p;
163 max_d = other.max_d;
164 max_q = other.max_q;
165 start_p = other.start_p;
166 start_q = other.start_q;
167 max_P = other.max_P;
168 max_D = other.max_D;
169 max_Q = other.max_Q;
170 start_P = other.start_P;
171 start_Q = other.start_Q;
172 r = other.r;
173 s = other.s;
174 method = other.method;
175 opt_method = other.opt_method;
176 stepwise = other.stepwise;
177 approximation = other.approximation;
178 num_models = other.num_models;
179 seasonal = other.seasonal;
180 stationary = other.stationary;
181
182 // 4: handle the pointers to the vectors
183 xreg_f = (xreg_vec_f.empty()) ? nullptr : &xreg_vec_f[0];
184 xreg_b = (xreg_vec_b.empty()) ? nullptr : &xreg_vec_b[0];
185 new_xreg_f = (new_xreg_vec_f.empty()) ? nullptr : &new_xreg_vec_f[0];
186 new_xreg_b = (new_xreg_vec_b.empty()) ? nullptr : &new_xreg_vec_b[0];
187 }
188 // by convention, always return *this
189 return *this;
190 }
191
193 auto_arima_free(auto_arima_forward);
194 auto_arima_free(auto_arima_backward);
195 delete xreg_f;
196 delete xreg_b;
197 delete new_xreg_f;
198 delete new_xreg_b;
199 }
200
201 // info
202 std::string toString();
203 std::string autoArimaInfo(auto_arima_object obj);
204
205
206private:
207 // Interpolation variables
208 Normalization norm;
209 std::vector<double> data;
210 size_t gap_loc;
211 size_t N_gap;
212 std::vector<double> time;
213 std::vector<double> pred_forward, pred_backward;
214
215 // Auto Arima variables
216 // const doesnt work with c
217 std::vector<double> xreg_vec_f, xreg_vec_b, data_forward, data_backward, new_xreg_vec_f, new_xreg_vec_b;
218 double *xreg_f;
219 double *xreg_b;
220 double *new_xreg_f;
221 double *new_xreg_b;
222 std::vector<double> amse_forward, amse_backward;
223 size_t N_data_forward, N_data_backward;
224 int max_p = 8, max_d = 3, max_q = 8;
225 int start_p = 2, start_q = 2;
226 int max_P = 2, max_D = 1, max_Q = 2;
227 int start_P = 1, start_Q = 1;
228 int r = 0, s = 0;
229 ObjectiveFunction method = CSS_MLE; OptimizationMethod opt_method = BFGS;
230 bool stepwise = true, approximation = true;
231 int num_models = 94;
232 bool seasonal = true, stationary = false;
233
234 bool consistencyCheck();
235 auto_arima_object initAutoArima(size_t N_data);
236
237 // last to be initialized
238public:
239 auto_arima_object auto_arima_forward;
240 auto_arima_object auto_arima_backward;
241
242private:
243 // (S)ARIMA variables
244 bool set_manual = false;
245 bool fill_backward_manual = false;
246
247public:
248 sarima_object sarima_forward;
249};
250
251} // namespace mio
252
253#endif // INTERPOLARIMA_H
Definition: ARIMAutils.h:52
std::vector< double > denormalize(const std::vector< double > &data)
Definition: ARIMAutils.cc:50
Mode
Definition: ARIMAutils.h:54
This class is used for interpolating or predicting missing data in a time series using the Auto ARIMA...
Definition: InterpolARIMA.h:89
std::string toString()
Definition: InterpolARIMA.cc:115
sarima_object sarima_forward
Definition: InterpolARIMA.h:248
InterpolARIMA & operator=(const InterpolARIMA &other)
Definition: InterpolARIMA.h:140
InterpolARIMA()
Definition: InterpolARIMA.cc:34
std::vector< double > getBackwardData()
Definition: InterpolARIMA.h:119
std::vector< double > getForwardData()
Definition: InterpolARIMA.h:118
void fillGap()
Definition: InterpolARIMA.cc:422
~InterpolARIMA()
Definition: InterpolARIMA.h:192
std::vector< double > simulate(int n_steps, int seed=0)
Definition: InterpolARIMA.cc:382
void setAutoArimaMetaData(int max_p_param=8, int max_d_param=3, int max_q=8, int start_p=2, int start_q=2, int max_P=2, int max_D=1, int max_Q=2, int start_P=1, int start_Q=1, bool seasonal=true, bool stationary=false)
Definition: InterpolARIMA.cc:301
InterpolARIMA(const InterpolARIMA &other)
Definition: InterpolARIMA.h:123
auto_arima_object auto_arima_forward
Definition: InterpolARIMA.h:239
std::vector< double > ARIMApredict(size_t n_steps)
Definition: InterpolARIMA.cc:390
std::vector< double > getData()
Definition: InterpolARIMA.h:117
void setManualARIMA(int p, int d, int q, int P, int D, int Q, bool fill_backward)
Definition: InterpolARIMA.cc:364
void interpolate()
Definition: InterpolARIMA.cc:571
void fillGapManual()
Definition: InterpolARIMA.cc:489
void setNormalizationMode(Normalization::Mode mode)
Definition: InterpolARIMA.cc:287
std::vector< double > getInterpolatedData()
Definition: InterpolARIMA.cc:375
void setOptMetaData(ObjectiveFunction method=CSS_MLE, OptimizationMethod opt_method=BFGS, bool stepwise=true, bool approximation=false, int num_models=94)
Definition: InterpolARIMA.cc:345
std::vector< double > predict(size_t n_steps=0)
Definition: InterpolARIMA.cc:606
std::string autoArimaInfo(auto_arima_object obj)
Definition: InterpolARIMA.cc:164
void setVerbose(bool verbose=false)
Definition: InterpolARIMA.cc:359
auto_arima_object auto_arima_backward
Definition: InterpolARIMA.h:240
OptimizationMethod
Definition: ARIMAutils.h:39
@ BFGS
Definition: ARIMAutils.h:45
ObjectiveFunction
Definition: ARIMAutils.h:34
@ CSS_MLE
Definition: ARIMAutils.h:35
Definition: Config.cc:31