'Calculating first and second derivative when coefficients stored in a csv file in python
I have a csv file with around 1000 regression results looking like this:
x^4_coeff x^3_coeff x^2_coeff x_coeff intercept
10 -.43 0.05 12 298
from the first set of coefficients I get an equation of:
10x4 -0.43x3 + 0.05x2 + 12x + 298
I want to automate calculating a first derivative which will be:
40x3 - 1.29x2 + 0.1x + 12
Then I would like to resolve this equation for 0 and find all the roots
After that I would like to get a second derivative which in this case would be: 12x2 - 2.58x + 0.1 and find both roots of this function
I would like to store the results in a csv file for a comparison, the points is to find out if there are some commonalities between all 1000 regressions and what is a difference between roots of first and second derivative for these equations.
I haven't calculated the roots manually so these values are dummy but hope you get the point
_root1 fd_root2 fd_root3 sd_root1 sd_root2
10 20 25 13 15
and do this for all my 1000 regression results. Is there a quick way to do this in python? What I have done so far was generating those 1000 regression outputs in Stata (which I don't know really well), saved the output to a csv file and thought it will be easier to carry on with Python.
Thanks for your help!
Solution 1:[1]
import pandas as pd
import numpy as np
d = {'coeff1': [2.3, 1], 'coeff2': [-5.3, -8.1], 'coeff3' : [-13.2,-111.2] , 'coeff4':[-5,-12], 'intercept':[150,200]}
df = pd.DataFrame(data=d)
df["root1"] = np.nan
df["root2"] = np.nan
for row in df.index:
p = np.poly1d([df['coeff1'][row], df['coeff2'][row], df['coeff3'][row],df['coeff4'][row], df['intercept'][row]])
# showing only second derivative roots to make the point
df["root1"].loc[row] = p.deriv().deriv().roots.item(0).real
df["root2"].loc[row] = p.deriv().deriv().roots.item(1).real
#print results
print(df)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sampak |
