我想弄清楚如何插入一个函数,我希望能够推断出一个超出插值范围的小方法。有一些背景理论让我期望因变量 x 的大值的行为将渐近 - A · x < sup> - n 行为(可能有一些偏移)。我想找到最合适的幂律,但仅适用于 x 的大值。有没有一种在Python中实现它的好方法?
我试图对我的幂律的对数线性化版本做最小二乘法,但它给了我一个不合适的拟合 - 我怀疑是因为编码错误而不是数学错误。我想通过在幂律中引入偏差来做到这一点,因此对数方程将是ln( - y )= ln( A ) - ñ LN( X - X <子> 0 子>)。到目前为止我所拥有的是:
X_asymptote=[]
for i in aX_vals1:
if i>20*lab_inc(T-1):
X_asymptote.append(i)
VT1_optimized_asymptote=[]
for i in X_asymptote:
a=aX_vals1.index(i)
VT1_optimized_asymptote.append(VT1_optimized_val[a])
#Converting to logs. Note the minus sign! You'll need to put this back in in the unravelling.
X_asymptote_logs=[]
for i in X_asymptote:
X_asymptote_logs.append(np.log(i))
X_asymptote_logs_array=np.asarray(X_asymptote_logs)
VT1_optimized_asymptote_logs=[]
for i in VT1_optimized_asymptote:
VT1_optimized_asymptote_logs.append(np.log(-i))
#VT1_optimized_asymptote_logs.append(-1*np.log(i))
VT1_optimized_asymptote_logs_array=np.asarray(VT1_optimized_asymptote)
#Linear fit
fitfunc=lambda p,x:p[0]+p[1]*x
errfunc=lambda p,x,y:(y-fitfunc(p,x))
out=scipy.optimize.leastsq(errfunc,[-4,5],args=(X_asymptote_logs_array,VT1_optimized_asymptote_logs_array),full_output=1)
pfinal=out[0]
covar=out[1]
index=pfinal[1]
amp=np.exp(pfinal[0])
xnew=np.linspace(X_asymptote_logs[0],aX_vals1_array[len(aX_vals1_array)-1],100)
ynew=VT1_optimized(xnew)
plt.title("Optimized value function, linear extrapolation")
plt.plot(aX_vals1_array, VT1_optimized_val_array, 'o', xnew, ynew, '-')
plt.ylabel('Value function with optimized asset allocation')
plt.xlabel('ratio X/Y')
plt.show()
有人能帮我一把吗?
非常感谢任何帮助!
更新:我有一个务实的解决方案。我刚刚为整个数据范围拟合了幂律。它似乎运行得非常好,现在代码确实想要我想要的,但不是我希望通过这个评论理解的方式。我现在的代码是:
#Linear fit
fitfunc=lambda p,x:p[0]+p[1]*(x)
errfunc=lambda p,x,y:(y-fitfunc(p,x))
out=scipy.optimize.leastsq(errfunc,[-4,5],args=(np.asarray(X_asymptote_logs),np.asarray(VT1_optimized_asymptote_logs)),full_output=1)
pfinal=out[0]
covar=out[1]
index=pfinal[1]
amp=np.exp(pfinal[0])
print(index,amp)
def VPL(X):
return -(amp*(X)**(index))
def VT1_optimized1(X):
if aX_vals1_array[len(aX_vals1_array)-1]>=X:
VT1_op=scipy.interpolate.UnivariateSpline(aX_vals1_array,VT1_optimized_val_array,k=3,s=0,check_finite=True)
return VT1_op(X)
if X>aX_vals1_array[len(aX_vals1_array)-1]:
VT1_asymptote=scipy.interpolate.UnivariateSpline(aX_vals1_array,VT1_optimized_val_array,k=5,s=0,check_finite=True)
return VPL(X)
xnew=np.linspace(aX_vals1_array[0],aX_vals1_array[len(aX_vals1_array)-1]*2,100)
y0=[]
for i in xnew:
y0.append(VT1_optimized1(i))
ynew=np.asarray(y0)
plt.title("Optimized value function, linear extrapolation")
plt.plot(np.asarray(aX_vals1), np.asarray(VT1_optimized_val), 'o', xnew, ynew, '-')
plt.ylabel('Value function with optimized asset allocation')
plt.xlabel('X')
plt.show()
#This seems to work almost too well! Let's check the asset allocation.
# Plotting
# xnew=np.linspace(aX_vals1_array[0],aX_vals1_array[len(aX_vals1_array)-1],100)
# ynew=VT1_optimized(xnew)
# plt.title("Optimized value function, linear extrapolation")
# plt.plot(aX_vals1_array, VT1_optimized_val_array, 'o', xnew, ynew, '-')
# plt.ylabel('Value function with optimized asset allocation')
# plt.xlabel('ratio X/Y')
# plt.show()
# xnew=np.linspace(aX_vals1_array[0],(aX_vals1_array[len(aX_vals1_array)-1]),100)
# y0=[]
# for i in xnew:
# y0.append(VT1_optimized1(i))
# ynew=np.asarray(y0)
# plt.title("Optimized value function, lin. extrapolation")
# plt.plot(aX_vals1_array, VT1_optimized_val_array, 'o', xnew, ynew, '-')
# plt.ylabel('asset allocation')
# plt.xlabel('X')
# plt.show()
# exit()