仅使用两个数据点实际上不可能实现您想要做的事情。一家公司通常有一些多项式或指数增长,但只有两个数据点,你无法拟合出如此复杂的增长曲线。只有线性插值是可能的。
但是让我们假设你有第三点
import pandas as pd
date = pd.date_range('2000-4-1', periods=3, freq='4Q') # quarter _end_!
Qsales = [54957.84767, 56285.54879, 58277.10047]
df = pd.DataFrame({'Quarter sales':Qsales}, index=pd.Index(date, name='date'))
print(df)
import matplotlib.pyplot as plt
plt.plot(df.index, df['Quarter sales'])
plt.show()
这表明:
Quarter sales
date
2000-06-30 54957.84767
2001-06-30 56285.54879
2002-06-30 58277.10047
现在我们可以做点什么了。让我们根据y = offset + factor * base^x 拟合指数曲线。
编辑:我在这里使用pd.datetime(2000, 1, 1) 作为零点。
#### curve fitting
import numpy as np
date_delta = (date - pd.datetime(2000, 1, 1)) /np.timedelta64(1,'M')
## convert data to x/y
x = date_delta.values
y = df['Quarter sales'].values
## expected function
def expFunc(x, offset, factor, base) : return offset + factor * base**x
## initial guess
guess = (53000, 1000, 1.05)
## call scipy curve fitting
from scipy.optimize import curve_fit
params = curve_fit(expFunc, x, y, guess)
## now first generate data for all quarters using interpolation
# generate new dates
date = pd.date_range('2000-1-1', periods=3*4, freq='Q') # quarter _end_!
date_delta = (date - pd.datetime(2000,1,1)) / np.timedelta64(1, 'M')
x = date_delta.values
Qsales = expFunc(x, params[0][0], params[0][1], params[0][2])
df = pd.DataFrame({'Quarter sales':Qsales}, index=pd.Index(date, name='date'))
print(df)
plt.plot(df.index, df['Quarter sales'])
plt.show()
这给出了:
Quarter sales
date
2000-03-31 54702.538666
2000-06-30 54957.847670
2000-09-30 55243.580457
2000-12-31 55560.059331
2001-03-31 55902.585284
2001-06-30 56285.548790
2001-09-30 56714.147971
2001-12-31 57188.866281
2002-03-31 57702.655211
2002-06-30 58277.100470
2002-09-30 58919.999241
2002-12-31 59632.076706
现在,事情变得顺利了。但这还不够。您需要确定每月的销售额。好吧,因为您现在知道了曲线,所以您可以根据以下公式分配每月的增长:
#now further interpolate to months
date = pd.date_range('2000-1-1', periods=3*12, freq='M') # month _end_!
date_delta = (date - pd.datetime(2000, 1, 1)) / np.timedelta64(1,'M')
x = date_delta.values
# first determine the exponential factor per month
dateFactors = expFunc(x, params[0][0], params[0][1], params[0][2])
MFactorSeries = pd.Series(dateFactors, index=date)
# now sum the exponential factors to get them for the quarters
QFactorSeries = MFactorSeries.resample('Q').sum()
# and divide them by the quartarly sales to get a monthly sales base
MSalesBase = np.divide(Qsales, QFactorSeries.values)
#now some numpy tricks to get the monthly sales
Msales = np.multiply(dateFactors.reshape(12,3), MSalesBase.reshape(12,1)).flatten()
df = pd.DataFrame({'Monthly sales':Msales}, index=pd.Index(date, name='date'))
print(df)
plt.plot(df.index, df['Monthly sales'])
plt.show()
这给出了:
Monthly sales
date
2000-01-31 18208.780004
2000-02-29 18233.319078
2000-03-31 18260.439584
2000-04-30 18290.245845
2000-05-31 18319.272021
2000-06-30 18348.329804
2000-07-31 18382.360436
2000-08-31 18414.515147
2000-09-30 18446.704874
2000-10-31 18484.774541
2000-11-30 18519.227954
2000-12-31 18556.056836
2001-01-31 18596.904560
2001-02-28 18632.486725
2001-03-31 18673.193999
2001-04-30 18718.262419
2001-05-31 18761.833781
2001-06-30 18805.452590
2001-07-31 18856.427507
2001-08-31 18904.698469
2001-09-30 18953.021996
2001-10-31 19010.040490
2001-11-30 19061.766635
2001-12-31 19117.059156
2002-01-31 19178.229496
2002-02-28 19231.653416
2002-03-31 19292.772298
2002-04-30 19360.251134
2002-05-31 19425.676408
2002-06-30 19491.172928
2002-07-31 19567.484781
2002-08-31 19639.973435
2002-09-30 19712.541026
2002-10-31 19797.887582
2002-11-30 19875.573492
2002-12-31 19958.615633
注意
我不是 pandas、scipy、numpy 等方面的专家。这正是我应该使用我的工程背景的方式。