# How Do I Do General Density Estimation with Stan

**URL:** <https://discourse.mc-stan.org/t/how-do-i-do-general-density-estimation-with-stan/24531>\
**Category:** Modeling\
**Created:** [September 22, 2021, 2:44pm UTC](https://discourse.mc-stan.org/t/how-do-i-do-general-density-estimation-with-stan/24531 "2021-09-22T14:44:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexhallam](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alexhallam/32/1714_2.png) [@alexhallam](https://discourse.mc-stan.org/u/alexhallam)\
**Post date:** [September 22, 2021, 2:44pm UTC](https://discourse.mc-stan.org/t/how-do-i-do-general-density-estimation-with-stan/24531/1 "2021-09-22T14:44:51Z")

</div>

I am aware of a few density estimation methods. I think that this paper is a good reference [Density estimation in R](http://www2.cs.uh.edu/~ceick/7362/T2-4.pdf)

In the past I have used average shifted histogram and KernSmooth for density estimation. I am curious what methods are are good fit for Stan? How do Stan users estimate non-parametric densities.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [September 22, 2021, 3:49pm UTC](https://discourse.mc-stan.org/t/how-do-i-do-general-density-estimation-with-stan/24531/2 "2021-09-22T15:49:56Z")

</div>

What’s the dimensionality of the density that you want to estimate? Edit: i.e. of the space over which you want a density estimate.

---

<div class="post-metadata">

**Author:** ![alexhallam](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alexhallam/32/1714_2.png) [@alexhallam](https://discourse.mc-stan.org/u/alexhallam)\
**Post date:** [September 22, 2021, 3:59pm UTC](https://discourse.mc-stan.org/t/how-do-i-do-general-density-estimation-with-stan/24531/3 "2021-09-22T15:59:12Z")

</div>

Just a one dimensional vector. Like a 0 inflated poisson like distribution.

---

<div class="post-metadata">

**Author:** ![js592](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@js592](https://discourse.mc-stan.org/u/js592)\
**Post date:** [September 22, 2021, 4:00pm UTC](https://discourse.mc-stan.org/t/how-do-i-do-general-density-estimation-with-stan/24531/4 "2021-09-22T16:00:05Z")

</div>

Bayesian density estimation, or more generally Bayesian nonparametrics, typically involves priors on distributional spaces (Dirichlet processes or in some cases gaussian processes). I would check out Ch 23 of [BDA](http://www.stat.columbia.edu/~gelman/book/BDA3.pdf). I personally do not have any experience with implementing them in Stan – my brief impression is that it will probably be difficult to do a ‘true’ Dirchlet process with HMC sampling.

---

<div class="post-metadata">

**Author:** ![alexhallam](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alexhallam/32/1714_2.png) [@alexhallam](https://discourse.mc-stan.org/u/alexhallam)\
**Post date:** [September 22, 2021, 4:09pm UTC](https://discourse.mc-stan.org/t/how-do-i-do-general-density-estimation-with-stan/24531/5 "2021-09-22T16:09:04Z")

</div>

Another thought I had. Couldn’t I just fit a spline to the CDF?

---

<div class="post-metadata">

**Author:** ![alexhallam](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alexhallam/32/1714_2.png) [@alexhallam](https://discourse.mc-stan.org/u/alexhallam)\
**Post date:** [September 22, 2021, 4:13pm UTC](https://discourse.mc-stan.org/t/how-do-i-do-general-density-estimation-with-stan/24531/6 "2021-09-22T16:13:13Z")

</div>

I thought GP was for multivariate distributions. Can it be for a single variable?

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [September 22, 2021, 5:01pm UTC](https://discourse.mc-stan.org/t/how-do-i-do-general-density-estimation-with-stan/24531/7 "2021-09-22T17:01:58Z")

</div>

> [@alexhallam](#):
>
> Like a 0 inflated poisson like distribution.

How do you want to define density over the discrete space?

---

<div class="post-metadata">

**Author:** ![alexhallam](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alexhallam/32/1714_2.png) [@alexhallam](https://discourse.mc-stan.org/u/alexhallam)\
**Post date:** [October 15, 2021, 11:12am UTC](https://discourse.mc-stan.org/t/how-do-i-do-general-density-estimation-with-stan/24531/8 "2021-10-15T11:12:12Z")

</div>

sorry. that was a bad example.
