# Understanding ADVI approx.sample(n)

**URL:** <https://discourse.pymc.io/t/understanding-advi-approx-sample-n/1121>\
**Category:** Questions\
**Created:** [April 23, 2018, 5:31am UTC](https://discourse.pymc.io/t/understanding-advi-approx-sample-n/1121 "2018-04-23T05:31:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nadheesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/nadheesh/32/650_2.png) [@Nadheesh](https://discourse.pymc.io/u/Nadheesh)\
**Post date:** [April 23, 2018, 5:31am UTC](https://discourse.pymc.io/t/understanding-advi-approx-sample-n/1121/1 "2018-04-23T05:31:21Z")

</div>

When using ADVI, we usually sample the approximation and the using those samples we perform PPC.

Consider following example,

```
w = Normal("w", mu = 0, sd = 10, shape = number_of_features)
mu = w * x
likelihood = Normal("y", mu = mu, std = 10, observed = y)
approx = pm.advi.fit(1000)
trace = approx.sample(50000) 

```

I want to understand what really happens during the sampling of approx. Is this what happens during when called approx.sample(50000) ?

```
w = Normal("w", approx.mean.eval() = 0, sd = approx.std.eval(), shape = number_of_features)
mu = w * x
likelihood = Normal("y", mu = mu, std = 10)
trace = sample(50000)

```

Moreover, if we want to use intermediate mean and std values using the tracker and then compute the PPC for those values, how can I achieve that?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 23, 2018, 5:51am UTC](https://discourse.pymc.io/t/understanding-advi-approx-sample-n/1121/2 "2018-04-23T05:51:05Z")

</div>

Yes, that is conceptually what happen, although the exact sampling happens in the graph.

> [@Nadheesh](#):
>
> Moreover, if we want to use intermediate mean and std values using the tracker and then compute the PPC for those values, how can I achieve that?

You can set the parameters by doing .set\_value, see this post for more details: [Saving ADVI results and reloading - #6 by junpenglao](https://discourse.pymc.io/t/saving-advi-results-and-reloading/1081/6)

---

<div class="post-metadata">

**Author:** ![Nadheesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/nadheesh/32/650_2.png) [@Nadheesh](https://discourse.pymc.io/u/Nadheesh)\
**Post date:** [April 23, 2018, 6:09am UTC](https://discourse.pymc.io/t/understanding-advi-approx-sample-n/1121/3 "2018-04-23T06:09:05Z")

</div>

Thanks for the clarification.

> [@junpenglao](#):
>
> You can set the parameters by doing .set\_value, see this post for more details: Saving ADVI results and reloading

It works. Can you please tell me what is the relationship between rho and std?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 23, 2018, 6:33am UTC](https://discourse.pymc.io/t/understanding-advi-approx-sample-n/1121/4 "2018-04-23T06:33:10Z")

</div>

You can find the transformation here:

> <https://github.com/pymc-devs/pymc3/blob/fcd7ac57048978f144ba8bdd5b90ff2f1b45c51a/pymc3/distributions/dist_math.py#L97>
