# Quantile function from algebra solver

**URL:** <https://discourse.mc-stan.org/t/quantile-function-from-algebra-solver/27852>\
**Category:** Modeling\
**Tags:** fitting-issues\
**Created:** [June 16, 2022, 4:19pm UTC](https://discourse.mc-stan.org/t/quantile-function-from-algebra-solver/27852 "2022-06-16T16:19:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laxmi\_Pd\_Sapkota](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/laxmi_pd_sapkota/32/11814_2.png) [@Laxmi\_Pd\_Sapkota](https://discourse.mc-stan.org/u/Laxmi_Pd_Sapkota)\
**Post date:** [June 16, 2022, 4:19pm UTC](https://discourse.mc-stan.org/t/quantile-function-from-algebra-solver/27852/1 "2022-06-16T16:19:12Z")

</div>

Hello,  
I have a CDF function and I would like to find its quantile function so that it can be used to generate Random Numbers in Stan to perform posterior predictive check. Since it is non-linear so could not find its inverse ie quantile function. Hence I convert CDF to another form so that it can be solved to get the quantile function using an algebra solver. I have read the stan users guide to learn algebra solver many times but my problem is a little bit different from the example given in the user guide. Is it possible to get a quantile function from the algebra solver so that it can be used in generated quantities block to generate RNG?  
My CDF is like this

F(x){\text{ }} = {\text{ }}1 - \frac{1} {{(1 - e^{ - \lambda } )}}\left[{1 - \exp \left\{ { - \lambda \left( {1 - e^{ - \beta /x} } \right)\exp ( - \alpha e^{ - \beta /x} )} \right\}} \right]{\text{ }}

and I converted it to this form

{\text{log(}}1 - e^{ - \beta /x} ) - \alpha e^{ - \beta /x} - \log (1 - z) = 0

here \alpha, \beta and \lambda are parameters and I wanted to solve it for x using algebra solver and its root is quantile function that can be used in generated quantities block to generate RNG

where,{\text{ }}z = 1 + \frac{1} {\lambda }\log \left\{ {1 - (1 - e^{ - \lambda } )(1 - p)} \right\};{\text{ }}0 \< p \< 1

Thank You

---

<div class="post-metadata">

**Author:** ![betanalpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/betanalpha/32/4_2.png) [@betanalpha](https://discourse.mc-stan.org/u/betanalpha)\
**Post date:** [August 16, 2022, 7:44pm UTC](https://discourse.mc-stan.org/t/quantile-function-from-algebra-solver/27852/2 "2022-08-16T19:44:21Z")

</div>

Yes, Stan’s algebraic solver can be used to implicitly evaluate a quantile function so long as all of those parameters are fixed – you just define F(x) - p = as the objective function. In practice you may have to be careful with setting initial conditions to ensure reasonably robust convergence of the numerical solver, especially if p is closer to zero or one.

---

<div class="post-metadata">

**Author:** ![Laxmi\_Pd\_Sapkota](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/laxmi_pd_sapkota/32/11814_2.png) [@Laxmi\_Pd\_Sapkota](https://discourse.mc-stan.org/u/Laxmi_Pd_Sapkota)\
**Post date:** [August 17, 2022, 5:33am UTC](https://discourse.mc-stan.org/t/quantile-function-from-algebra-solver/27852/3 "2022-08-17T05:33:48Z")

</div>

How to find a root of the above function and where I have to write this program in **functions block** or elsewhere. Please elaborate how to get a root as a function. That can be call from **generated quantities block**.

---

<div class="post-metadata">

**Author:** ![betanalpha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/betanalpha/32/4_2.png) [@betanalpha](https://discourse.mc-stan.org/u/betanalpha)\
**Post date:** [August 31, 2022, 7:05pm UTC](https://discourse.mc-stan.org/t/quantile-function-from-algebra-solver/27852/4 "2022-08-31T19:05:45Z")

</div>

I won’t be able to put together a new example for you but see [Some Ruminations on Containment Prior Modeling](https://betanalpha.github.io/assets/case_studies/some_containment_prior_models.html) for some existing examples.

---

<div class="post-metadata">

**Author:** ![Laxmi\_Pd\_Sapkota](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/laxmi_pd_sapkota/32/11814_2.png) [@Laxmi\_Pd\_Sapkota](https://discourse.mc-stan.org/u/Laxmi_Pd_Sapkota)\
**Post date:** [September 2, 2022, 4:28am UTC](https://discourse.mc-stan.org/t/quantile-function-from-algebra-solver/27852/5 "2022-09-02T04:28:02Z")

</div>

Thank you
