# How was dQi and dri calculated in EP-stan?

Hi I am almost done figuring out the entire EP-stan code. there is this last lingering question in the Worker.tilted() code. I understand how you can obtain the new tilted distribution Q and r using stan sampling. But in the process of deriving dQi and dri to pass back to the master, I am lost on two points

1. why is both dgeqrf_routine and invert_normal_params called? my understanding is they both do conversion between normalized and unnormalized precision and location parameters.

2. The sampling process should be creating the new Qi and ri. But I am not sure how the code ends up with dQi, and dri. I don’t see a subtraction taking place against the original Qi and ri.

Hello,

Thank you for the questions. I’ll gladly explain.

1. `samp` contains collection of samples of phi for the tilted distribution. We need to invert the scatter matrix. We do that with QR decomposition.
`dgeqrf_routine` is used to obtain QR-decomposition of the samples (in-place) of which we only need the R matrix. `invert_normal_params` then computes the inverse and precision mean vector estimate given R. See appendix A.2 in https://arxiv.org/abs/1412.4869

2. There is the following code block which calculates the difference. Before this block, `dQi`, `dri` contains estimated tilted distribution natural parameters. After this block, they contain the change in the site distribution parameters.

``````# Calculate the difference into the output arrays
np.subtract(dQi, self.Q, out=dQi)
np.subtract(dri, self.r, out=dri)
``````

Regards,
Tuomas Sivula

Ah, now I see those subtract lines. Thank you.