You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
real linear_interpolation_v(real x, vector x_pred, vector y_pred){
int K =rows(x_pred);
vector[K] deltas = x - x_pred;
real ans;
real t;
real w;
int i;
if(x < x_pred[1] || x > x_pred[K]) reject("x is outside of the x_pred grid!");
if(rows(y_pred) != K) reject("x_pred and y_pred aren't of the same size");
//this is which.min()
i =sort_indices_asc(fabs(deltas))[1];
if(deltas[i] <=0) i -=1;
ans = y_pred[i];
real x1 = x_pred[i];
real x2 = x_pred[i +1];
real y1 = y_pred[i];
real y2 = y_pred[i +1];
ans = y1 + (y2 - y1) * (x - x1) / (x2 - x1);
t = (x - x1) / (x2 - x1);
// 3 weight functions here to handle
w =1- t;
//w = 1 / (1 + exp(1 / (1 - t) - 1 / t));//w = 1 - 3 * pow(t, 2) + 2 * pow(t, 3);
ans = w * y1 + (1- w) * y2;
return ans;
}
coded up in https://discourse.mc-stan.org/t/linear-interpolation-and-searchsorted-in-stan/13318/6?u=spinkney
This method leaves out the bookend bins. Maybe do linear interpolation there?
The text was updated successfully, but these errors were encountered: