PPCKO: Principal Predictive Components for Estimating an Autoregressive Operator
 
Loading...
Searching...
No Matches
parameters_wrapper.hpp
Go to the documentation of this file.
1// Copyright (c) 2024 Andrea Enrico Franzoni (andreaenrico.franzoni@gmail.com)
2//
3// This file is part of PPCKO
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of PPCKO and associated documentation files (the PPCKO software), to deal
7// PPCKO without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of PPCKO, and to permit persons to whom PPCKO is
10// furnished to do so, subject to the following conditions:
11//
12// PPCKO IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17// OUT OF OR IN CONNECTION WITH PPCKO OR THE USE OR OTHER DEALINGS IN
18// PPCKO.
19
20#ifndef KO_WRAP_PARAMS_HPP
21#define KO_WRAP_PARAMS_HPP
22
23#include <vector>
24#include <algorithm>
25#include <numeric>
26#include <iostream>
27#include <utility>
28#include <string>
29#include <stdexcept>
30#include "traits_ko.hpp"
31
32#include "mesh.hpp"
33
34#ifdef _OPENMP
35#include <omp.h>
36#endif
37
38
44
45
46
52inline
53std::string
54wrap_string_CV_to_be_printed(const std::string &id_cv)
55{
56 if(id_cv==CV_algo::CV1){ return "no cross-validation";}
57 if(id_cv==CV_algo::CV2){ return "cross validation on regularization parameter";}
58 if(id_cv==CV_algo::CV3){ return "cross validation on number of PPCs";}
59 if(id_cv==CV_algo::CV4){ return "cross validation on both regularization parameter and number of PPCs";}
60 else
61 {
62 std::string error_message = "Wrong input string";
63 throw std::invalid_argument(error_message);
64 }
65}
66
67
68
73inline
74void
75check_threshold_ppc(const double &threshold_ppc)
76{
77 if(threshold_ppc<=0 || threshold_ppc>=1)
78 {
79 std::string error_message = "threshold_ppc has to be in (0,1)";
80 throw std::invalid_argument(error_message);
81 }
82}
83
84
85
90inline
91void
92check_alpha(const double &alpha)
93{
94 if( alpha<= 0 )
95 {
96 std::string error_message = "alpha has to be a positive real number";
97 throw std::invalid_argument(error_message);
98 }
99}
100
101
102
108inline
109void
110check_k(const int &k, const int &max_k)
111{
112 if( k < 0 )
113 {
114 std::string error_message = "k has to be a positive integer or 0";
115 throw std::invalid_argument(error_message);
116 }
117 if( k > max_k )
118 {
119 std::string error_message = "k has to be lower than the maximum number of PPCs";
120 throw std::invalid_argument(error_message);
121 }
122}
123
124
125
132inline
133void
134check_solver(bool solver_ex, const std::string &id_cv, int k)
135{
136 if(solver_ex == false)
137 {
138 if ( (k==0 && (id_cv==CV_algo::CV1 || id_cv==CV_algo::CV2)) )
139 {
140 std::string error_message = "GEP solver can be used only if the number of PPCs is imposed or retrieved through CV";
141 throw std::invalid_argument(error_message);
142 }
143 }
144}
145
146
147
154inline
155std::vector<double>
156wrap_alpha_vec(Rcpp::Nullable<Rcpp::NumericVector> alpha_vec)
157{
158 //if no alphas are given, the default value for the alphas is generated
159 if(alpha_vec.isNull())
160 {
161 std::vector<double> alphas;
162 alphas.resize(21);
163
164 std::iota(alphas.begin(),alphas.end(),static_cast<double>(-10));
165 std::transform(alphas.begin(),alphas.end(),alphas.begin(),[](double el){return(pow(static_cast<double>(10),el));});
166
167 return alphas;
168 }
169
170 std::vector<double> alphas = Rcpp::as<std::vector<double>>(alpha_vec);
171
172 //sorting into ascending order the alphas to be coherent during the algorithm
173 std::sort(alphas.begin(), alphas.end());
174
175 if(alphas[0] <= 0)
176 {
177 std::string error_message = "Every alpha has to be a positive real number";
178 throw std::invalid_argument(error_message);
179 }
180
181 return alphas;
182}
183
184
185
193inline
194std::vector<int>
195wrap_k_vec(Rcpp::Nullable<Rcpp::IntegerVector> k_vec, int k_max)
196{
197 //if no k is given, the default is looking for all the possible directions (number of discrete evaluations innthe domain of the functional object)
198 if(k_vec.isNull())
199 {
200 std::vector<int> k_s;
201 k_s.resize(k_max);
202
203 std::iota(k_s.begin(),k_s.end(),static_cast<int>(1));
204
205 return k_s;
206 }
207
208 std::vector<int> k_s = Rcpp::as<std::vector<int>>(k_vec);
209
210 //sorting into ascending order the ks to be coherent during the algorithm
211 std::sort(k_s.begin(), k_s.end());
212
213 //checking
214 if(k_s[0] < 1)
215 {
216 std::string error_message1 = "k has to be at least 1";
217 throw std::invalid_argument(error_message1);
218 }
219 if(k_s.back() > k_max)
220 {
221 std::string error_message2 = "k cannot be greater than the number of discrete evaluation of the functional object in the domain (" + std::to_string(k_max) + ")";
222 throw std::invalid_argument(error_message2);
223 }
224
225 return k_s;
226}
227
228
229
239inline
240std::vector<double>
241wrap_disc_ev(Rcpp::Nullable<Rcpp::NumericVector> disc_ev, double a, double b, int dim) //dim: row of x
242{
243 //check that domain extremes are consistent
244 if(a>=b)
245 {
246 std::string error_message1 = "Left extreme of the domain has to be smaller than the right one";
247 throw std::invalid_argument(error_message1);
248 }
249
250 //eventual default generation of the grid
251 if(disc_ev.isNull())
252 {
253 Geometry::Domain1D domain_func_data(a,b);
254 Geometry::Mesh1D grid_func_data(domain_func_data,dim-static_cast<int>(1));
255
256 return grid_func_data.nodes();
257 }
258
259 //sorting the abscissas values
260 std::vector<double> disc_ev_points = Rcpp::as<std::vector<double>>(disc_ev);
261 std::sort(disc_ev_points.begin(),disc_ev_points.end());
262
263 //checking that the passed points are inside the domain
264 if(disc_ev_points[0] < a || disc_ev_points.back() > b)
265 {
266 std::string error_message2 = "The points in which there are the discrete evaluations of the functiona data have to in the domain (" + std::to_string(a) + "," + std::to_string(b) + ")";
267 throw std::invalid_argument(error_message2);
268 }
269
270 //checking that the dimension is ok (important when wrapping both the grid for surface case)
271 if(disc_ev_points.size()!=dim)
272 {
273 std::string error_message3 = "In the grid are needed " + std::to_string(dim) + " points";
274 throw std::invalid_argument(error_message3);
275 }
276
277 return disc_ev_points;
278}
279
280
281
293inline
294std::pair<int,int>
295wrap_sizes_set_CV(Rcpp::Nullable<int> min_size_ts, Rcpp::Nullable<int> max_size_ts, int number_time_instants)
296{
297 //eventual default value for min_size_ts: ceil number_time_instants/2
298 int min_dim_ts = min_size_ts.isNull() ? static_cast<int>(std::ceil(static_cast<double>(number_time_instants)/static_cast<double>(2))) : Rcpp::as<int>(min_size_ts);
299 //eventual default value for max_size_ts: n
300 int max_dim_ts = max_size_ts.isNull() ? number_time_instants : (Rcpp::as<int>(max_size_ts)+1);
301
302 //checking that
303 if (!(min_dim_ts>1))
304 {
305 std::string error_message1 = "Min size of train set has to be at least 2";
306 throw std::invalid_argument(error_message1);
307 }
308
309 if (!(max_dim_ts<=number_time_instants))
310 {
311 std::string error_message2 = "Max size of train set has to be at most the total number of time instants (" + std::to_string(number_time_instants) + ") minus 1 (to leave room for the validation set)";
312 throw std::invalid_argument(error_message2);
313 }
314
315 if(min_dim_ts >= max_dim_ts)
316 {
317 std::string error_message = "Min size of train set (" + std::to_string(min_dim_ts) + " has to be less than the max one (" + std::to_string(max_dim_ts) + ")";
318 throw std::invalid_argument(error_message);
319 }
320
321 return std::make_pair(min_dim_ts,max_dim_ts);
322}
323
324
325
333inline
334int
335wrap_num_thread(Rcpp::Nullable<int> num_threads)
336{
337#ifndef _OPENMP
338 return 1;
339#else
340
341 //getting maximum number of cores in the machine
342 int max_n_t = omp_get_num_procs();
343
344 if(num_threads.isNull())
345 {
346 return max_n_t;
347 }
348 else
349 {
350 int n_t = Rcpp::as<int>(num_threads);
351 if(n_t < 1 || n_t > max_n_t){ return max_n_t;}
352
353 return n_t;
354 }
355#endif
356}
357
358
359
360//removing NaNs
366{
367 NR = 0,
368 MR = 1,
369 ZR = 2,
370};
371
372
373
379inline
381wrap_id_rem_nans(Rcpp::Nullable<std::string> id_rem_nan)
382{
383 if(id_rem_nan.isNull())
384 {
385 return REM_NAN::MR;
386 }
387 if(Rcpp::as< std::string >(id_rem_nan) == "NO")
388 {
389 return REM_NAN::NR;
390 }
391 if(Rcpp::as< std::string >(id_rem_nan) == "MR")
392 {
393 return REM_NAN::MR;
394 }
395 if(Rcpp::as< std::string >(id_rem_nan) == "ZR")
396 {
397 return REM_NAN::ZR;
398 }
399 else
400 {
401 std::string error_message = "Wrong input string for handling NANs";
402 throw std::invalid_argument(error_message);
403 }
404
405};
406
407
408#endif /*KO_WRAP_PARAMS_HPP*/
Definition domain.hpp:37
Definition mesh.hpp:37
std::vector< double > nodes() const
The nodes.
Definition mesh.hpp:73
Contains the class for an unidimensioanl mesh.
REM_NAN wrap_id_rem_nans(Rcpp::Nullable< std::string > id_rem_nan)
Wrapping the strategy for handling non-dummy NaNs.
Definition parameters_wrapper.hpp:381
std::vector< double > wrap_alpha_vec(Rcpp::Nullable< Rcpp::NumericVector > alpha_vec)
Wrapping the R-vector representing the regularization parameter input space into a coherent C++ objec...
Definition parameters_wrapper.hpp:156
void check_solver(bool solver_ex, const std::string &id_cv, int k)
Check if, if using 'gep_solver', the number of PPCs is not retrieved through explanatory power criter...
Definition parameters_wrapper.hpp:134
std::string wrap_string_CV_to_be_printed(const std::string &id_cv)
Creating a string to print on the screen which PPCKO version is being used.
Definition parameters_wrapper.hpp:54
std::pair< int, int > wrap_sizes_set_CV(Rcpp::Nullable< int > min_size_ts, Rcpp::Nullable< int > max_size_ts, int number_time_instants)
Wrapping the minimum and maximum dimension of the training set, checking their consitency,...
Definition parameters_wrapper.hpp:295
void check_threshold_ppc(const double &threshold_ppc)
Check if 'threshold_ppc' input is between 0 and 1. Eventually, raises and error.
Definition parameters_wrapper.hpp:75
int wrap_num_thread(Rcpp::Nullable< int > num_threads)
Wrapping the number of threads for OMP.
Definition parameters_wrapper.hpp:335
void check_k(const int &k, const int &max_k)
Check if 'k' input is an integer between 0 and the number of available evaluations of the functional ...
Definition parameters_wrapper.hpp:110
std::vector< double > wrap_disc_ev(Rcpp::Nullable< Rcpp::NumericVector > disc_ev, double a, double b, int dim)
Wrapping the points over which the discrete evaluations of the functional object are available....
Definition parameters_wrapper.hpp:241
void check_alpha(const double &alpha)
Check if 'alpha' input is greater than 0. Eventually, raises and error.
Definition parameters_wrapper.hpp:92
std::vector< int > wrap_k_vec(Rcpp::Nullable< Rcpp::IntegerVector > k_vec, int k_max)
Wrapping the R-vector representing the number of PPCs input space into a coherent C++ object,...
Definition parameters_wrapper.hpp:195
REM_NAN
The available strategy for removing non-dummy NaNs.
Definition parameters_wrapper.hpp:366
@ ZR
Replacing nans with 0s (could change the sd of the distribution)
Definition parameters_wrapper.hpp:369
@ MR
Replacing nans with mean (could change the mean of the distribution)
Definition parameters_wrapper.hpp:368
@ NR
Not replacing NaN: not to be used by the user, necessary for handling dummy NaNs.
Definition parameters_wrapper.hpp:367
static constexpr std::string CV1
No cv for parameters.
Definition traits_ko.hpp:64
static constexpr std::string CV4
Cv for both regularization parameter and number of retained PPCs.
Definition traits_ko.hpp:67
static constexpr std::string CV2
Cv for regularization parameter.
Definition traits_ko.hpp:65
static constexpr std::string CV3
Cv for number of retained PPCs.
Definition traits_ko.hpp:66
Contains customized types and enumerator for customized template parameters, exploited in the algorit...