# Example notebook for truncated regression

**URL:** <https://discourse.pymc.io/t/example-notebook-for-truncated-regression/4606>\
**Category:** Sharing\
**Created:** [March 3, 2020, 6:37pm UTC](https://discourse.pymc.io/t/example-notebook-for-truncated-regression/4606 "2020-03-03T18:37:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![drbenvincent](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/drbenvincent/32/4313_2.png) [@drbenvincent](https://discourse.pymc.io/u/drbenvincent)\
**Post date:** [March 3, 2020, 6:37pm UTC](https://discourse.pymc.io/t/example-notebook-for-truncated-regression/4606/1 "2020-03-03T18:37:19Z")

</div>

Made a notebook to demonstrate truncated regression in PyMC3 [https://github.com/drbenvincent/pymc3-demo-code/blob/master/truncated\_regression\_pymc3.ipynb](https://github.com/drbenvincent/pymc3-demo-code/blob/master/truncated_regression_pymc3.ipynb)

Very open to corrections/pull requests.

 ![Screenshot 2020-03-03 at 18.35.33](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/e/e850d5059fb7e64c83c9b7e23de6ec0399aaaa57.png)

---

<div class="post-metadata">

**Author:** ![tirthasheshpatel](https://avatars.discourse-cdn.com/v4/letter/t/ac91a4/32.png) [@tirthasheshpatel](https://discourse.pymc.io/u/tirthasheshpatel)\
**Post date:** [March 3, 2020, 6:55pm UTC](https://discourse.pymc.io/t/example-notebook-for-truncated-regression/4606/2 "2020-03-03T18:55:23Z")

</div>

Wow @drbenvincent! This is a very clean notebook. One of the few that can be understood without comments. Great work!

---

<div class="post-metadata">

**Author:** ![sammosummo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/sammosummo/32/96_2.png) [@sammosummo](https://discourse.pymc.io/u/sammosummo)\
**Post date:** [March 3, 2020, 7:49pm UTC](https://discourse.pymc.io/t/example-notebook-for-truncated-regression/4606/3 "2020-03-03T19:49:35Z")

</div>

This is indeed very clear. Thanks for sharing.

---

<div class="post-metadata">

**Author:** ![lewiso1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/lewiso1/32/2426_2.png) [@lewiso1](https://discourse.pymc.io/u/lewiso1)\
**Post date:** [March 3, 2020, 11:39pm UTC](https://discourse.pymc.io/t/example-notebook-for-truncated-regression/4606/4 "2020-03-03T23:39:50Z")

</div>

This is really nice and I can see what happened intuitively. Can anyone provide an explanation of why truncating the likelihood leads to a better model? If there are no data points outside the truncation bounds, then shouldn’t the log-likelihood of the existing points be the same whether it’s truncated or not?

---

<div class="post-metadata">

**Author:** ![drbenvincent](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/drbenvincent/32/4313_2.png) [@drbenvincent](https://discourse.pymc.io/u/drbenvincent)\
**Post date:** [March 7, 2020, 11:53am UTC](https://discourse.pymc.io/t/example-notebook-for-truncated-regression/4606/5 "2020-03-07T11:53:11Z")

</div>

Using a normal likelihood function on truncated data would lead to bias in the estimate of `mu`. So by using a truncated normal, you are expressing your knowledge that the chance of observing data outside of the bounds is zero, which allows the estimate for `mu` to “expect” the cutoff and so the `mu` estimate won’t be biased. Does that make sense?

---

<div class="post-metadata">

**Author:** ![drbenvincent](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/drbenvincent/32/4313_2.png) [@drbenvincent](https://discourse.pymc.io/u/drbenvincent)\
**Post date:** [March 9, 2020, 11:00am UTC](https://discourse.pymc.io/t/example-notebook-for-truncated-regression/4606/6 "2020-03-09T11:00:55Z")

</div>

Based on this [Help with Censored Regression](https://discourse.pymc.io/t/help-with-censored-regression/4617/8) I think that this example is wrong. It uses `TruncatedNormal` which it seems is inappropriate for truncated data 😱.

Looks like using Bounds is appropriate in the context of truncated data… Seeking advice here [Truncated observed data?](https://discourse.pymc.io/t/truncated-observed-data/4652)

---

<div class="post-metadata">

**Author:** ![lewiso1](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/lewiso1/32/2426_2.png) [@lewiso1](https://discourse.pymc.io/u/lewiso1)\
**Post date:** [April 27, 2020, 7:36am UTC](https://discourse.pymc.io/t/example-notebook-for-truncated-regression/4606/7 "2020-04-27T07:36:51Z")

</div>

Yes! thankyou.
