def extrap1d_constrained_linear_regression(x, y, xEval, side='right', numPts=10):
"""Perform extrapolation using constrained linear regression on part of the data (x,y). Use numPts
from either the left or right side of the data (specified by input variable side) as the input data.
The linear regression is constrained to pass through the final point (x0, y0) (rightmost point if
side=='right', leftmost if side=='left'). Data MUST be sorted.
Inputs:
x independent variable on the smaller domain (array)
y dependent variable on the smaller domain (array)
xEval values of x at which to evaluate the linear regression model
side side of the data from which to perform linear regression ('left' or 'right')
numPts number of points to use in the linear regression (scalar)
Outputs:
yEval values of dependent variable in the linear regression model evaluated at x_eval (array)
"""
assert side=='left' or side=='right'
if side=='left':
xSide = x[:numPts]
ySide = y[:numPts]
x0 = x[0]
y0 = y[0]
elif side=='right':
xSide = x[-numPts:]
ySide = y[-numPts:]
x0 = x[-1]
y0 = y[-1]
a = least_squares_slope(xSide, ySide, x0, y0) # determine model (a, x0, y0)
b = y0 - a*x0
#y_eval = scipy.polyval([a,b], x_eval)
yEval = a*(xEval - x0) + y0 # evaluate model on desired points
return yEval
评论列表
文章目录