# Find fit function with linear and quadratic component

**URL:** <https://discourse.pymc.io/t/find-fit-function-with-linear-and-quadratic-component/3472>\
**Category:** Questions\
**Created:** [June 23, 2019, 3:40pm UTC](https://discourse.pymc.io/t/find-fit-function-with-linear-and-quadratic-component/3472 "2019-06-23T15:40:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kanra](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kanra](https://discourse.pymc.io/u/kanra)\
**Post date:** [June 23, 2019, 3:40pm UTC](https://discourse.pymc.io/t/find-fit-function-with-linear-and-quadratic-component/3472/1 "2019-06-23T15:40:37Z")

</div>

I would like to find fit functions for data, that has linear dependency first and changes to quadratic at some point.

The fit function should then be used to predict future values. Since I am new to pymc3, I looked into some examples and tried to use them for my problem. The concept of spline regression ([https://ckrapu.github.io/2018/07/09/Spline-Regression.html](https://ckrapu.github.io/2018/07/09/Spline-Regression.html)) seemed promising. Here is the notebook with my approach: [https://nbviewer.jupyter.org/github/violairis/Spline-Regression-with-pymc3/blob/master/Spline%20Regression%20using%20pymc3.ipynb](https://nbviewer.jupyter.org/github/violairis/Spline-Regression-with-pymc3/blob/master/Spline%20Regression%20using%20pymc3.ipynb)

But the best result I was able to get looks like this:

![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/e/e9d6360cd4a9150087eae563614dd67bd4994992.png)

Could you give me a hint, where I am doing wrong? I would also be happy to hear about alternative approaches to this kind of problem, in case the spline regression is not a good way to handle this. Any help is much apprechiated, thank you!

---

<div class="post-metadata">

**Author:** ![bdyetton](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bdyetton/32/208_2.png) [@bdyetton](https://discourse.pymc.io/u/bdyetton)\
**Post date:** [June 23, 2019, 6:55pm UTC](https://discourse.pymc.io/t/find-fit-function-with-linear-and-quadratic-component/3472/2 "2019-06-23T18:55:14Z")

</div>

That data looks like it could be pretty well represented with a standard polynomial regression. Have you tried that?  
You probably need a cubic fit here due to that dip around x=1.25.

```auto
b0 = pm.Normal('b0', 0, sd=10**4)
b1 = pm.Normal('b1', 0, sd=10**4)
b2 = pm.Normal('b2', 0, sd=10**4)
model_error = pm.HalfNormal('error', 10**4)
y_obs ~ pm.Normal('y', mu=b0+b1*np.pow(x,2)+b2*np.pow(x,3), sd=model_error, observed=y)

```

I just chose relativly flat priors on b0-2, you might want something more informative
