5 #include <unsupported/Eigen/MatrixFunctions>
16 std::shared_ptr<LinearOperator> Lin,
17 Eigen::MatrixXd
const& Qin,
18 boost::property_tree::ptree options) : stateDim(Fin->rows()), F(Fin), L(Lin), Q(Qin)
20 if(
F->rows() !=
F->cols())
26 if(
F->rows() !=
L->rows())
38 LQLT =
L->Apply(
L->Apply(
Q).transpose().eval() );
41 LQLT = Eigen::MatrixXd::Zero(
F->rows(),
F->rows());
46 boost::property_tree::ptree options) :
LinearSDE(Fin, std::shared_ptr<
LinearOperator>(nullptr), Eigen::MatrixXd(), options){};
51 dt = options.get(
"SDE.dt",1
e-4);
53 throw std::runtime_error(
"In LinearSDE: Step size dt is not positive.");
58 std::stringstream msg;
59 msg <<
"In LinearSDE: Invalid integrator type. Given \"" <<
integratorType <<
"\" but valid options are {\"SRK4\",\"EM\"}";
60 throw std::runtime_error(msg.str());
181 boost::property_tree::ptree options)
186 for(
auto& sde : sdes){
188 stochDim += sde->L->cols();
191 std::vector<std::shared_ptr<LinearOperator>> Fs(sdes.size());
192 std::vector<std::shared_ptr<LinearOperator>> Ls(sdes.size());
194 Eigen::MatrixXd
Q = Eigen::MatrixXd::Zero(stochDim, stochDim);
197 for(
int i=0; i<sdes.size(); ++i){
198 Fs.at(i) = sdes.at(i)->GetF();
199 Ls.at(i) = sdes.at(i)->GetL();
201 Q.block(currDim, currDim, Ls.at(i)->cols(), Ls.at(i)->cols()) = sdes.at(i)->GetQ();
203 currDim += Ls.at(i)->cols();
206 auto F = std::make_shared<BlockDiagonalOperator>(Fs);
207 auto L = std::make_shared<BlockDiagonalOperator>(Ls);
209 return std::make_shared<LinearSDE>(
F,
L,
Q, options);
218 const int numTimes = std::ceil(deltaT/
dt);
222 Eigen::MatrixXd Fgamma, k1, k2, k3, k4;
225 for(
int i=0; i<numTimes-1; ++i)
228 k2 =
F->Apply(A + 0.5*
dt*k1);
229 k3 =
F->Apply(A + 0.5*
dt*k2);
230 k4 =
F->Apply(A +
dt*k3);
231 A = A + (
dt/6.0) * (k1 + 2.0*k2 + 2.0*k3 + k4);
233 Fgamma =
F->Apply(gamma);
234 k1 = Fgamma + Fgamma.transpose() +
LQLT;
235 Fgamma =
F->Apply(gamma + 0.5*
dt*k1);
236 k2 = Fgamma + Fgamma.transpose() +
LQLT;
237 Fgamma =
F->Apply(gamma + 0.5*
dt*k2);
238 k3 = Fgamma + Fgamma.transpose() +
LQLT;
239 Fgamma =
F->Apply(gamma +
dt*k3);
240 k4 = Fgamma + Fgamma.transpose() +
LQLT;
242 gamma = gamma + (
dt/6.0)*(k1 + 2.0*k2 + 2.0*k3 + k4);
245 double lastDt = deltaT-(numTimes-1)*
dt;
247 k2 =
F->Apply(A + 0.5*lastDt*k1);
248 k3 =
F->Apply(A + 0.5*lastDt*k2);
249 k4 =
F->Apply(A + lastDt*k3);
250 A = A + (lastDt/6.0) * (k1 + 2.0*k2 + 2.0*k3 + k4);
252 Fgamma =
F->Apply(gamma);
253 k1 = Fgamma + Fgamma.transpose() +
LQLT;
254 Fgamma =
F->Apply(gamma + 0.5*lastDt*k1);
255 k2 = Fgamma + Fgamma.transpose() +
LQLT;
256 Fgamma =
F->Apply(gamma + 0.5*lastDt*k2);
257 k3 = Fgamma + Fgamma.transpose() +
LQLT;
258 Fgamma =
F->Apply(gamma + lastDt*k3);
259 k4 = Fgamma + Fgamma.transpose() +
LQLT;
261 gamma = gamma + (lastDt/6.0)*(k1 + 2.0*k2 + 2.0*k3 + k4);
267 for(
int i=0; i<numTimes-1; ++i){
269 gamma +=
dt*
dt*(
F->Apply(
F->Apply(gamma).transpose().eval()).transpose()) +
dt*
LQLT;
273 double lastDt = deltaT-(numTimes-1)*
dt;
275 A += lastDt*
F->Apply(A);
276 gamma += lastDt*lastDt*(
F->Apply(
F->Apply(gamma).transpose().eval()).transpose()) + lastDt*
LQLT;
279 return std::make_pair(A,gamma);
Generic linear operator base class.
Defines a linear time invariant stochastic differential equation with Gaussian process noise.
std::shared_ptr< muq::Modeling::LinearOperator > L
void ExtractOptions(boost::property_tree::ptree options)
std::shared_ptr< muq::Modeling::LinearOperator > F
std::pair< Eigen::MatrixXd, Eigen::MatrixXd > Discretize(double deltaT)
static std::shared_ptr< LinearSDE > Concatenate(std::vector< std::shared_ptr< LinearSDE >> const &sdes, boost::property_tree::ptree options=boost::property_tree::ptree())
Combines the states of multiple SDEs into a single monolitch SDE.
const int stateDim
The dimension of the state variable .
std::string integratorType
LinearSDE(EigenType1 const &Fin, EigenType2 const &Lin, Eigen::MatrixXd const &Qin, boost::property_tree::ptree options)
Exception to throw when matrices, vectors, or arrays are the wrong size.
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values