Unfit  3.1.1
Data fitting and optimization software
ThreeNaMarkov.hpp
1 // Unfit: Data fitting and optimization software
2 //
3 // Copyright (C) 2012- Dr Martin Buist & Dr Alberto Corrias
4 // Contacts: martin.buist _at_ nus.edu.sg; alberto _at_ nus.edu.sg
5 //
6 // See the 'Contributors' file for a list of those who have contributed
7 // to this work.
8 //
9 // This program is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 //
22 #ifndef UNFIT_EXAMPLES_THREENAMARKOV_HPP_
23 #define UNFIT_EXAMPLES_THREENAMARKOV_HPP_
24 
25 #include <vector>
26 #include "GenericCostFunction.hpp"
27 
28 namespace Unfit
29 {
30 namespace Examples
31 {
51 {
52  public:
65  ThreeNaMarkov(const std::vector<double> &open_prob, double dt)
66  : open_prob_ {open_prob},
67  dt_ {dt}
68  {}
69 
83  std::vector<double> operator()(const std::vector<double> &param)
84  {
85  // Initial conditions
86  double O = 0.0;
87  double C = 1.0;
88  double I = 0.0;
89  // Rate constants
90  const double kco = param[0];
91  const double koc = param[1];
92  const double koi = param[2];
93  const double kio = param[3];
94  // Residual initialization
95  auto residuals = open_prob_;
96  residuals[0] -= O;
97 
98  for (auto i = 1u; i < residuals.size(); ++i) {
99  const double deltaC = dt_*(koc*O - kco*C);
100  const double deltaI = dt_*(koi*O - kio*I);
101  I += deltaI;
102  C += deltaC;
103  O = 1.0 - C - I;
104  // NOTE: equivalent behaviour of "O = 1-C-I" is
105  // double deltaO = dt_*(kco*C + kio*I - (koc+koi)*O);
106  // O += deltaO;
107  residuals[i] -= O;
108  }
109  return (residuals);
110  }
111  private:
113  const std::vector<double> open_prob_;
115  const double dt_;
116 };
117 
118 } // namespace Examples
119 } // namespace Unfit
120 
121 #endif
ThreeNaMarkov(const std::vector< double > &open_prob, double dt)
Definition: ThreeNaMarkov.hpp:65
std::vector< double > operator()(const std::vector< double > &param)
Definition: ThreeNaMarkov.hpp:83
Definition: Bounds.hpp:27
Fit the time dependence of a three state hidden Markov model.
Definition: ThreeNaMarkov.hpp:50
Definition: GenericCostFunction.hpp:36
const double dt_
Definition: ThreeNaMarkov.hpp:115
const std::vector< double > open_prob_
Definition: ThreeNaMarkov.hpp:113