From 3f81169f5742c52a1542b97c93a3321b30fe7690 Mon Sep 17 00:00:00 2001 From: John Stachurski Date: Wed, 3 Oct 2018 09:07:06 -0400 Subject: [PATCH] misc --- infinite_horizon_job_search/lininterp.py | 49 ++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 infinite_horizon_job_search/lininterp.py diff --git a/infinite_horizon_job_search/lininterp.py b/infinite_horizon_job_search/lininterp.py new file mode 100644 index 0000000..407e9b8 --- /dev/null +++ b/infinite_horizon_job_search/lininterp.py @@ -0,0 +1,49 @@ +import numpy as np +from numba import jit + +@jit(nopython=True) +def interp1d(grid, vals, x): + """ + Linearly interpolate (grid, vals) to evaluate at x. + + Parameters + ---------- + grid and vals are numpy arrays, x is a float + + Returns + ------- + a float, the interpolated value + + """ + + a, b, G = np.min(grid), np.max(grid), len(grid) + + s = (x - a) / (b - a) + + q_0 = max(min(int(s * (G - 1)), (G - 2)), 0) + v_0 = vals[q_0] + v_1 = vals[q_0 + 1] + + λ = s * (G - 1) - q_0 + + return (1 - λ) * v_0 + λ * v_1 + + +@jit(nopython=True) +def interp1d_vectorized(grid, vals, x_vec): + """ + Linearly interpolate (grid, vals) to evaluate at x_vec. + + All inputs are numpy arrays. + + Return value is a numpy array of length len(x_vec). + """ + + out = np.empty_like(x_vec) + + for i, x in enumerate(x_vec): + out[i] = interp1d(grid, vals, x) + + return out + +