Learning Curve Plus Plus (LCPP)
functionalsmoothing.h
Go to the documentation of this file.
1 /**
2  * @file functionalsmoothing.h
3  * @author Ozgur Taylan Turan
4  *
5  * Smoothing for functional data
6  *
7  *
8  *
9  */
10 #ifndef FUNCTIONALSMOOTHING_H
11 #define FUNCTIONALSMOOTHING_H
12 
13 namespace algo {
14 namespace functional {
15 /*
16  * Optimizing the bandwiths for kernel smoothing.
17  *
18  * @param inputs Training input matrix
19  * @param labels Corresponding target/output matrix
20  * @param pred_inputs Input points at which predictions will be computed
21  * @param bandwidths Bandwidth parameters for each feature
22  * @param valid Validation parameter
23  */
24 template<class KERNEL,class T=DTYPE>
25 arma::Mat<T> kernelsmoothing ( const arma::Mat<T>& inputs,
26  const arma::Mat<T>& labels,
27  const arma::Mat<T>& pred_inputs,
28  const arma::vec& bandwidths,
29  const double valid)
30 {
31  size_t N = pred_inputs.n_cols;
32  size_t M = labels.n_rows;
33 
34  arma::Mat<T> predictions = arma::zeros<arma::Mat<T>>(M, N);
35 
36  for(size_t i=0; i<M; i++)
37  {
38  arma::Row<T> label = labels.row(i);
39  arma::Row<T> temp;
40 
41  mlpack::HyperParameterTuner<algo::regression::Kernel<KERNEL>,
42  mlpack::MSE,
43  mlpack::SimpleCV>
44  hpt(valid, inputs, label);
45 
46 
47  double bandwidth;
48  std::tie(bandwidth) = hpt.Optimize(bandwidths);
49 
50  algo::regression::Kernel<KERNEL> smoother(inputs,label,bandwidth);
51 
52  smoother.Predict(pred_inputs,temp);
53  predictions.row(i) = temp;
54  }
55  return predictions;
56 }
57 
58 /*
59  * Kernel smoothing for a fixed bandwidth
60  *
61  * @param inputs Training input matrix
62  * @param labels Corresponding target/output matrix
63  * @param pred_inputs Input points at which predictions will be computed
64  * @param bandwidth Bandwidth parameters for each feature
65  */
66 template<class KERNEL,class T=DTYPE>
67 arma::Mat<T> kernelsmoothing ( const arma::Mat<T>& inputs,
68  const arma::Mat<T>& labels,
69  const arma::Mat<T>& pred_inputs,
70  const double& bandwidth )
71 {
72  size_t N = pred_inputs.n_cols;
73  size_t M = labels.n_rows;
74 
75  arma::Mat<T> predictions = arma::zeros<arma::Mat<T>>(M, N);
76 
77  for(size_t i=0; i<M; i++)
78  {
79  arma::Row<T> label = labels.row(i);
80  arma::Row<T> temp;
81 
82  algo::regression::Kernel<KERNEL> smoother(inputs,label,bandwidth);
83 
84  smoother.Predict(pred_inputs,temp);
85  predictions.row(i) = temp;
86  }
87  return predictions;
88 }
89 
90 /*
91  * Kernel smoothing for a fixed bandwidth and returning the preds at inputs
92  *
93  * @param inputs Training input matrix
94  * @param labels Corresponding target/output matrix
95  * @param bandwidth Bandwidth parameters for each feature
96  */
97 
98 template<class KERNEL,class T=DTYPE>
99 arma::Mat<T> kernelsmoothing ( const arma::Mat<T>& inputs,
100  const arma::Mat<T>& labels,
101  const double& bandwidth )
102 
103 {
104  return kernelsmoothing<KERNEL>( inputs,labels,inputs,bandwidth );
105 
106 }
107 
108 } // functional
109 } // utils
110 #endif