44int main(
int argc,
char* argv[])
46 word outputFolder =
"./ITHACAoutput/";
52 "Wrong initialState input");
54 int stateSize =
A.rows();
55 int obsSize = H.rows();
57 "In this tutorial we have a dynamical system in the form:\ndx/dt = A * x" <<
59 std::cout <<
"with A = \n" <<
A << std::endl;
60 std::cout <<
"We observe the state x by mean of the observation matrix \nH = \n"
63 "The objective is to reconstruct the vector state knowing H and x0 = \n" <<
65 "\nbut having a wrong A" << std::endl;
66 std::cout <<
"A_wrong =\n" << Aw << std::endl;
68 int sampleDeltaStep = 10;
70 double deltaTime = endTime / Ntimes;
71 Eigen::VectorXd time = Eigen::VectorXd::LinSpaced(Ntimes, 0, endTime);
72 Eigen::VectorXd xOld = x0;
73 Eigen::MatrixXd X(stateSize, Ntimes);
75 int sampleFlag = sampleDeltaStep;
76 int Nsamples = (Ntimes - 1) / sampleDeltaStep;
78 Eigen::MatrixXd obs(obsSize, Nsamples);
80 for (
int timeI = 0; timeI < Ntimes - 1; timeI++)
82 Eigen::VectorXd xNew = (
A * deltaTime + Eigen::MatrixXd::Identity(
A.rows(),
85 Eigen::VectorXd dNew = H * xNew;
86 X.col(timeI + 1) = xNew;
91 sampleFlag = sampleDeltaStep;
92 obs.col(sampleI) = dNew;
97 M_Assert(Nsamples == sampleI,
"Something went wrong in the sampling");
100 Eigen::VectorXd x = x0;
101 Eigen::VectorXd prior_mu = x * 0.0;
102 Eigen::MatrixXd prior_cov = Eigen::MatrixXd::Identity(stateSize,
104 auto priorDensity = std::make_shared<muq::Modeling::Gaussian>(prior_mu,
106 Eigen::VectorXd modelError_mu = x * 0.0;
107 Eigen::MatrixXd modelError_cov = Eigen::MatrixXd::Identity(stateSize,
109 auto modelErrorDensity = std::make_shared<muq::Modeling::Gaussian>
110 (modelError_mu, modelError_cov);
111 Eigen::MatrixXd posteriorSamples(stateSize, Nseeds);
112 Eigen::MatrixXd priorSamples(stateSize, Nseeds);
114 for (
int i = 0;
i < Nseeds;
i++)
116 priorSamples.col(
i) = priorDensity->Sample();
119 posteriorSamples = priorSamples;
120 Eigen::MatrixXd meas_cov = Eigen::MatrixXd::Identity(obsSize, obsSize) * 0.3;
121 auto measNoise = std::make_shared<muq::Modeling::Gaussian>
122 (Eigen::VectorXd::Zero(obsSize), meas_cov);
123 Eigen::MatrixXd posteriorMean(stateSize, Ntimes);
124 Eigen::MatrixXd minConfidence = posteriorMean;
125 Eigen::MatrixXd maxConfidence = minConfidence;
126 posteriorMean.col(0) = posteriorSamples.rowwise().mean();
127 sampleFlag = sampleDeltaStep;
129 Eigen::MatrixXd forwardSamples(stateSize, Nseeds);
131 for (
int timeI = 0; timeI < Ntimes - 1; timeI++)
133 priorSamples = posteriorSamples;
134 Eigen::MatrixXd forwardSamplesOld = forwardSamples;
137 for (
int i = 0;
i < Nseeds;
i++)
139 forwardSamples.col(
i) = (
A * deltaTime + Eigen::MatrixXd::Identity(
A.rows(),
140 A.cols())) * priorSamples.col(
i) + modelErrorDensity->Sample();
147 sampleFlag = sampleDeltaStep;
148 Eigen::VectorXd meas = obs.col(sampleI);
151 meas, meas_cov, H * forwardSamples);
156 posteriorSamples = forwardSamples;
159 posteriorMean.col(timeI + 1) = posteriorSamples.rowwise().mean();
void exportMatrix(Eigen::Matrix< T, -1, dim > &matrix, word Name, word type, word folder)
Export the reduced matrices in numpy (type=python), matlab (type=matlab) and txt (type=eigen) format ...