1

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!

sampak
  • 155
  • 1
  • 2
  • 12
  • Please share more details around derivative calculation. And secondly, what have you attempted so far. – Abhishek Jain Jan 13 '22 at 11:37
  • 2
    Please add a couple for rows of data to both input and output csv, so it's easier to judge what you are working with. Also, here's a thread on computing derivatives using python: https://stackoverflow.com/questions/9876290/how-do-i-compute-derivative-using-numpy – Tzane Jan 13 '22 at 11:41
  • @AbhishekJain - added more details, what I am trying to compare are inflection points of these functions, how do they differ, and depending on a definition, this would be either roots of 1st derivative or roots of 2nd derivative – sampak Jan 13 '22 at 12:04
  • ok, I think I can work it out from @Tzane link, thanks! – sampak Jan 13 '22 at 12:07

2 Answers2

2

Here's a sample script for calculating derivatives and roots of the polynomials you have. I didn't include csv reading/writing because I wasn't sure about the exact format you were working with.

from sympy import Symbol, Poly

# Define symbols
x = Symbol("x")

# Add csv reading here
input_rows = [
    [2.234, 0, 0.523, 2.3123, 4.123],
    [2, 2, 2, 2, 2]]

output_rows = []

# Iterate over each row
for r in input_rows:
    # Create polynomial from coefficients
    y = Poly(r, x)
    print(y)
    # 1st derivative and its roots
    y_dx = y.diff(x)
    y_dx_roots = y_dx.nroots()
    # 2nd derivative and its roots
    y_ddx = y_dx.diff(x)
    y_ddx_roots = y_ddx.nroots()
    # Write results to list of dicts
    output_rows.append({
        "1st deriv": y_dx.all_coeffs(),
        "2nd deriv": y_ddx.all_coeffs(),
        "1st deriv roots": y_dx_roots,
        "2nd deriv roots": y_ddx_roots})

print(*output_rows, sep="\n")
Tzane
  • 2,752
  • 1
  • 10
  • 21
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)
sampak
  • 155
  • 1
  • 2
  • 12
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 24 '22 at 21:06