MUQ  0.4.3
GaussianProcess_CO2.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
4 
8 
9 using namespace std;
10 using namespace muq::Utilities;
11 using namespace muq::Approximation;
12 
13 
14 template<typename MeanType, typename KernelType>
15 std::shared_ptr<GaussianProcess> BuildGP(MeanType const& mu, KernelType const& kernel)
16 {
17  return std::make_shared<GaussianProcess>(mu.Clone(), kernel.Clone());
18 }
19 
20 template<typename MeanType, typename KernelType>
21 std::shared_ptr<GaussianProcess> BuildStateSpaceGP(MeanType const& mu, KernelType const& kernel)
22 {
23  boost::property_tree::ptree options;
24  options.put("SDE.dt", 1e-2);
25 
26  return std::make_shared<StateSpaceGP>(mu.Clone(), kernel.Clone(), options);
27 }
28 
29 int main()
30 {
31  // Open and read in data
32  string dataFile = "data/MaunaLoaCO2.h5";
33  H5Object f = OpenFile(dataFile);
34 
35  Eigen::VectorXd times = f["/Weekly/Dates"];
36  Eigen::VectorXd concentrations = f["/Weekly/Concentrations" ];
37 
38  // Long term trend
39  double k1_var = 360;
40  double k1_length = 60;
41  double k1_nu = 5.0/2.0;
42  auto k1 = MaternKernel(1, k1_var, k1_length, k1_nu);
43 
44  // Periodic component
45  double k2_var = 1.0;
46  double k2_period = 1.0;
47  double k2_length = 0.5;
48  auto k2 = PeriodicKernel(1, k2_var, k2_length, k2_period);
49 
50  // Quasi-periodic term
51  double k3_var = 10.0;
52  double k3_length = 30;
53  double k3_nu = 3.0/2.0;
54  auto k3 = MaternKernel(1, k3_var, k3_length, k3_nu);
55 
56  // Short term trends or anomalis
57  double k4_var = 1.0;
58  double k4_length = 2.0;
59  double k4_nu = 3.0/2.0;
60  auto k4 = MaternKernel(1, k4_var, k4_length, k4_nu);
61 
62  // Combine the individual kernels
63  auto k = k1 + k2*k3 + k4;
64 
65  // Add a linear mean function
66  LinearMean mu(1.8, 370-2000.0*1.8);
67 
68  // Construct a Gaussian Process using standard Matrix backend
69  //auto gp = BuildGP(mu, k);
70 
71  // Construct a Gaussian Processing using a StateSpace formulation
72  auto gp = BuildStateSpaceGP(mu,k);
73 
74  // Define pediction points
75  int numPts = 800;
76  Eigen::MatrixXd evalPts(1, numPts);
77  evalPts.row(0) = Eigen::VectorXd::LinSpaced(numPts, 2010, 2020);
78 
79  gp->Condition(times.transpose(), concentrations.transpose(), 16);
80  Eigen::MatrixXd postMean, postVar;
81  std::tie(postMean,postVar) = gp->Predict(evalPts, GaussianProcess::DiagonalCov);
82 
83  // Write results to new file
84  string writeFile = "results/CO2_Prediction.h5";
85  H5Object fout = OpenFile(writeFile);
86 
87  fout["/Predict/Dates"] = evalPts;
88  fout["/Predict/Concentrations"] = postMean;
89  fout["/Predict/ConcentrationVariance"] = postVar;
90 
91  return 0;
92 }
std::shared_ptr< GaussianProcess > BuildGP(MeanType const &mu, KernelType const &kernel)
std::shared_ptr< GaussianProcess > BuildStateSpaceGP(MeanType const &mu, KernelType const &kernel)
int main()
H5Object OpenFile(std::string const &filename)
Open an HDF5 file and return the root object.
Definition: H5Object.cpp:321