Home > database >  Find the point of consolidation in a graph using Python
Find the point of consolidation in a graph using Python

Time:11-20

I have sales data for a product since its launch and want to find the place where the demand stabilizes. To get a better understanding of the demand profile I have plotted both the daily sales and the 7-day moving average:

Daily sales: Daily sales

7-day moving average of sales: 7 day moving average of sales

I was thinking of using NumPy's polyfit to estimate the graph corresponding to the data and using its derivative to determine the consolidation point.

x = data['days_since_launch']
y = data['sales_ma_7']
polyfitted = np.polyfit(x, y, 4)
fit_eq = polyfitted[0]*x**4   polyfitted[1]*x**3   polyfitted[2]*x**2   polyfitted[3]*x   polyfitted[4] 

When the value of the derivative is within a set threshold I can safely say the demand has stabilized. The polyfitted lines will look something like so:

Moving average with polyfitted line

I am not sure if this approach is feasible, however, so suggestions to other approaches are much appreciated.

CodePudding user response:

Polyfit is not really suited for this task as polynoms tend to show wired behaviour in many cases (like large deviations from the data points at the ends of the series). I would suggest to use enter image description here

orange: cubic spline approximation
green: derivative thereof

Printed output:

stability from 16.0 to 39.5

Of course, you need a definition of what stability means to you. In this example, the curve should be flat enough.

  • Related