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!


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")

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)