# Vari class, operands\_and\_partials, or adj\_jac\_apply

**URL:** <https://discourse.mc-stan.org/t/vari-class-operands-and-partials-or-adj-jac-apply/12684>\
**Category:** Developers\
**Tags:** math\
**Created:** [January 17, 2020, 8:24pm UTC](https://discourse.mc-stan.org/t/vari-class-operands-and-partials-or-adj-jac-apply/12684 "2020-01-17T20:24:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlesm93](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charlesm93/32/4634_2.png) [@charlesm93](https://discourse.mc-stan.org/u/charlesm93)\
**Post date:** [January 17, 2020, 8:24pm UTC](https://discourse.mc-stan.org/t/vari-class-operands-and-partials-or-adj-jac-apply/12684/1 "2020-01-17T20:24:36Z")

</div>

I’m implementing a new lpdf function for HMMs, with discrete latent states, which uses the adjoint method @betanalpha and I have been working on. The signature is

```
template <T_omega, T_Gamma, T_rho>
hmm_marginal_lpdf(const Eigen::Matrix<T_omega, -1, -1>& log_omegas,
                  const Eigen::Matrix<T_Gamma, -1, -1>& Gamma,
                  const Eigen::Matrix<T_rho, -1, 1>& rho,
                  int n_states) {...}

```

We derived an adjoint method to get sensitivities for `log_omega`, `Gamma`, and `rho`. Currently, I wrote a custom vari class.

**benefit:** I can calculate the derivatives when constructing the vari object, and then only store these derivatives (as opposed to the matrices required to construct these derivatives) before calling `chain()`. So the forward pass only stores the log density and the derivatives.

**drawback:** I don’t need sensitivities for all three, and I’m not quite sure how adequately template the vari class / I don’t want to write 8 vari classes.

Browsing the forum there seems to be two other options: `operands_and_partials` and `Adj_jac_apply`. The first one gives me the requisite templating, but forces me to do all the calculations during the chain call (though it seems like there should be a way around this). The second method seems fine but only for rev mode.

Do I have a reasonable assessment of the situation?

---

<div class="post-metadata">

**Author:** ![charlesm93](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/charlesm93/32/4634_2.png) [@charlesm93](https://discourse.mc-stan.org/u/charlesm93)\
**Post date:** [January 17, 2020, 9:29pm UTC](https://discourse.mc-stan.org/t/vari-class-operands-and-partials-or-adj-jac-apply/12684/2 "2020-01-17T21:29:40Z")

</div>

After further inspection, all three methods can achieve what I’m after.

Note `operands_and_partials` doesn’t give you control on when the derivatives, but computes them during the forward pass. This is fine, since this is what I intend to do.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [January 23, 2020, 8:42pm UTC](https://discourse.mc-stan.org/t/vari-class-operands-and-partials-or-adj-jac-apply/12684/3 "2020-01-23T20:42:20Z")

</div>

The `n_states` argument’s redundant given that you have `Gamma` and `rho`.

When there’s a reduction to a single scalar output, the Jacobian is the same size as the number of parameters in the input. So `operands_and_partials` should be efficient enough.
