12 using namespace numeric;
20 const auto& hmmEq = accessValueConstCast<Eigen::VectorXd>(*this->dependency(0));
22 const auto& hmmTrans = accessValueConstCast<Eigen::MatrixXd>(*this->dependency(1));
23 const auto& hmmEmis = accessValueConstCast<MatrixLik>(*this->dependency(2));
25 auto& condLik = dynamic_pointer_cast<CondLikelihood>(condLik_)->accessValueMutable();
27 auto nbSites = hmmEmis.cols();
28 auto nbStates = hmmEmis.rows();
35 parCondLik_[0] = hmmTrans.transpose() * hmmEq;
38 tscales[0] = tmp.
sum();
40 for (
auto s = 0; s < nbStates; s++)
42 condLik(s, 0) =
convert(tmp(s) / tscales[0]);
46 for (
auto i = 1; i < nbSites; i++)
48 parCondLik_[(size_t)i] = hmmTrans.transpose() * condLik.col(i - 1);
51 tscales[(size_t)i] = tmp.
sum();
54 for (
auto s = 0; s < nbStates; s++)
56 condLik(s, i) =
convert(tmp(s) / tscales[(
size_t)i]);
89 this->shared_from_this(),
90 this->dependency(0)->derive (c, node),
91 this->dependency(1)->derive (c, node),
92 this->dependency(2)->derive (c, node)}, targetDimension_);
102 const auto& hmmEq = accessValueConstCast<Eigen::VectorXd>(*this->dependency(0));
104 const auto& hmmTrans = accessValueConstCast<Eigen::MatrixXd>(*this->dependency(1));
105 const auto& hmmEmis = accessValueConstCast<MatrixLik>(*this->dependency(2));
108 auto forwardNode = dynamic_pointer_cast<ForwardHmmLikelihood_DF>(this->dependency(3));
110 const auto& condLik = forwardNode->getForwardCondLikelihood()->targetValue();
112 const auto& parCondLik = forwardNode->getParCondLik();
114 const auto& scales = forwardNode->targetValue();
116 const auto& dHmmEq = accessValueConstCast<Eigen::VectorXd>(*this->dependency(4));
118 const auto& dHmmTrans = accessValueConstCast<Eigen::MatrixXd>(*this->dependency(5));
120 const auto& dHmmEmis = accessValueConstCast<MatrixLik>(*this->dependency(6));
122 auto nbSites = hmmEmis.cols();
123 const int nbStates = (int)hmmEmis.rows();
127 auto& dCondLik = dynamic_pointer_cast<CondLikelihood>(dCondLik_)->accessValueMutable();
132 dParCondLik_[0] = dHmmTrans.transpose() * hmmEq + hmmTrans.transpose() * dHmmEq;
135 +
cwise(dHmmEmis.col(0)) *
cwise(parCondLik[0]));
136 tdScales[0] = dtmp.
sum();
140 for (
auto s = 0; s < nbStates; s++)
142 dCondLik(s, 0) =
convert((dtmp(s) - condLik(s, 0) * tdScales[0]) / scales(0));
146 for (
auto i = 1; i < nbSites; i++)
148 dParCondLik_[(size_t)i] = dHmmTrans.transpose() * condLik.col(i - 1) + hmmTrans.transpose() * dCondLik.col(i - 1);
150 cwise(dtmp) =
cwise(dParCondLik_[(
size_t)i]) *
cwise(hmmEmis.col(i)) +
cwise(parCondLik[(
size_t)i]) *
cwise(dHmmEmis.col(i));
151 tdScales[(size_t)i] = dtmp.
sum();
153 for (
auto s = 0; s < nbStates; s++)
155 dCondLik(s, i) =
convert((dtmp(s) - condLik(s, i) * tdScales[(
size_t)i]) / scales(i));
202 this->shared_from_this(),
204 this->dependency(4)->derive (c, node),
205 this->dependency(5)->derive (c, node),
206 this->dependency(6)->derive (c, node)},
219 const auto& hmmEq = accessValueConstCast<Eigen::VectorXd>(*this->dependency(0));
221 const auto& hmmTrans = accessValueConstCast<Eigen::MatrixXd>(*this->dependency(1));
222 const auto& hmmEmis = accessValueConstCast<MatrixLik>(*this->dependency(2));
225 auto forwardNode = dynamic_pointer_cast<ForwardHmmLikelihood_DF>(this->dependency(3));
227 const auto& condLik = forwardNode->getForwardCondLikelihood()->targetValue();
229 const auto& parCondLik = forwardNode->getParCondLik();
231 const auto& scales = forwardNode->targetValue();
234 const auto& dHmmEq = accessValueConstCast<Eigen::VectorXd>(*this->dependency(4));
236 const auto& dHmmTrans = accessValueConstCast<Eigen::MatrixXd>(*this->dependency(5));
238 const auto& dHmmEmis = accessValueConstCast<MatrixLik>(*this->dependency(6));
241 auto forwardDNode = dynamic_pointer_cast<ForwardHmmDLikelihood_DF>(this->dependency(7));
243 const auto& dCondLik = forwardDNode->getForwardDCondLikelihood()->targetValue();
245 const auto& parDCondLik = forwardDNode->getParDCondLik();
247 const auto& dScales = forwardDNode->targetValue();
250 const auto& d2HmmEq = accessValueConstCast<Eigen::VectorXd>(*this->dependency(8));
252 const auto& d2HmmTrans = accessValueConstCast<Eigen::MatrixXd>(*this->dependency(9));
254 const auto& d2HmmEmis = accessValueConstCast<MatrixLik>(*this->dependency(10));
259 auto nbSites = hmmEmis.cols();
260 const int nbStates =
static_cast<int>(hmmEmis.rows());
262 VDataLik td2Scales(
static_cast<size_t>(nbSites));
264 Eigen::VectorXd d2CondLik(
static_cast<int>(hmmEmis.rows()));
266 Eigen::VectorXd d2ParCondLik;
270 d2ParCondLik = d2HmmTrans.transpose() * hmmEq + 2 * dHmmTrans.transpose() * dHmmEq + hmmTrans.transpose() * d2HmmEq;
273 + 2 *
cwise(parDCondLik[0]) *
cwise(dHmmEmis.col(0))
274 +
cwise(parCondLik[0]) *
cwise(d2HmmEmis.col(0));
276 td2Scales[0] = d2tmp.
sum();
280 for (
auto s = 0; s < nbStates; s++)
282 d2CondLik(s, 0) =
convert((d2tmp(s) - 2 * dCondLik(s, 0) * dScales(0) - condLik(s, 0) * td2Scales[0]) / scales(0));
286 for (
auto i = 1; i < nbSites; i++)
288 d2ParCondLik = d2HmmTrans.transpose() * condLik.col(i)
289 + 2 * dHmmTrans.transpose() * dCondLik.col(i) + hmmTrans.transpose() * d2CondLik;
292 + 2 *
cwise(parDCondLik[(
size_t)i]) *
cwise(dHmmEmis.col(i))
293 +
cwise(parCondLik[(
size_t)i]) *
cwise(d2HmmEmis.col(i));
295 td2Scales[(size_t)i] = d2tmp.
sum();
297 for (
auto s = 0; s < nbStates; s++)
299 d2CondLik(s, i) =
convert((d2tmp(s) - 2 * dCondLik(s, i) * dScales(i) - condLik(s, i) * td2Scales[(
size_t)i]) / scales(i));
311 const auto& scales = accessValueConstCast<RowLik>(*this->dependency(0));
313 const auto& hmmTrans = accessValueConstCast<Eigen::MatrixXd>(*this->dependency(1));
315 const auto& hmmEmis = accessValueConstCast<MatrixLik>(*this->dependency(2));
317 auto nbSites = hmmEmis.cols();
318 auto nbStates = hmmEmis.rows();
320 std::vector<Eigen::VectorXd> tScales((
size_t)nbSites);
325 tScales[(size_t)(nbSites - 1)] = Eigen::VectorXd::Constant(nbStates, 1);
328 for (
auto i = nbSites - 1; i > 0; i--)
330 tScales[(size_t)(i - 1)].resize(nbStates);
334 auto tmp2 = hmmTrans * tmp;
336 for (
auto s = 0; s < nbStates; s++)
337 tScales[(
size_t)(i - 1)](s) =
convert(tmp2(s) / scales(i));
void compute() override
Computation implementation.
static std::shared_ptr< Self > create(Context &c, const Dimension< T > &dim)
Build a new ConstantOne node of the given dimension.
Context for dataflow node construction.
const ExtendedFloat & sum() const
void compute() override
Computation implementation.
static ValueRef< RowLik > create(Context &c, NodeRefVec &&deps, const Dimension< Eigen::MatrixXd > &dim)
void compute() override
Computation implementation.
static ValueRef< RowLik > create(Context &c, NodeRefVec &&deps, const Dimension< Eigen::MatrixXd > &dim)
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
void compute() override
Computation implementation.
NodeRef derive(Context &c, const Node_DF &node) final
Returns a node computing d(this_node_expression)/d(node_expression).
Base dataflow Node class.
Defines the basic types of data flow nodes.
std::vector< DataLik > VDataLik
template void copyBppToEigen(const std::vector< ExtendedFloat > &bppVector, Eigen::RowVectorXd &eigenVector)
double convert(const bpp::ExtendedFloat &ef)
std::shared_ptr< Node_DF > NodeRef