def stochastic_residuals_3(s, theta, dr, f, g, parms, epsilons, weights, shape, no_deriv=False): import numpy n_t = len(theta) dr.theta = theta.copy().reshape(shape) # [x, x_s, x_theta] = dr.interpolate(s, with_theta_deriv=True) n_draws = epsilons.shape[1] [n_s, n_g] = s.shape [x, x_s, x_theta] = dr.interpolate(s, with_theta_deriv=True) # should repeat theta instead n_x = x.shape[0] # xx = np.tile(x, (1,n_draws)) # ee = np.repeat(epsilons, n_g , axis=1) # from dolo.numeric.serial_operations import serial_multiplication as stm # res = np.zeros((n_x, n_g)) dres = np.zeros((n_x, n_t, n_g)) for i in range(n_draws): tt = [epsilons[:, i : i + 1]] * n_g e = numpy.column_stack(tt) [S, S_s, S_x, S_e] = g(s, x, e, parms) [X, X_S, X_t] = dr.interpolate(S, with_theta_deriv=True) [F, F_s, F_x, F_S, F_X, F_e] = f(s, x, S, X, e, parms) res += weights[i] * F S_theta = stm(S_x, x_theta) X_theta = stm(X_S, S_theta) + X_t dF = stm(F_x, x_theta) + stm(F_S, S_theta) + stm(F_X, X_theta) dres += weights[i] * dF return [res, dres.swapaxes(1, 2)]
def stochastic_residuals_2(s, x, dr, f, g, parms, epsilons, weights, shape, deriv=False): #memory_hungry version n_t = np.prod(x.shape) dr.set_values(x) # shortcut : let's call x theta for now # [x, x_s, x_theta] = dr.interpolate(s, with_theta_deriv=True) n_draws = epsilons.shape[1] [n_s,n_g] = s.shape ss = np.tile(s, (1,n_draws)) [xx, xx_s, junk, xx_theta] = dr.interpolate(ss, with_derivative=True, with_theta_deriv=True, with_X_deriv=True) # should repeat theta instead n_x = xx.shape[0] # xx = np.tile(x, (1,n_draws)) ee = np.repeat(epsilons, n_g , axis=1) [SS, SS_ss, SS_xx, SS_ee] = g(ss, xx, ee, parms, derivs=True) [XX, XX_SS, junk, XX_t] = dr.interpolate(SS, with_derivative=True, with_theta_deriv=True, with_X_deriv=True) [F, F_ss, F_xx, F_ee, F_SS, F_XX] = f(ss, xx, ee, SS, XX, parms, derivs=True) res = np.zeros( (n_x,n_g) ) for i in range(n_draws): res += weights[i] * F[:,n_g*i:n_g*(i+1)] if not deriv: return res from dolo.numeric.serial_operations import serial_multiplication as stm SS_theta = stm( SS_xx, xx_theta) XX_theta = stm( XX_SS, SS_theta) + XX_t dF = stm(F_xx, xx_theta) + stm( F_SS, SS_theta) + stm( F_XX , XX_theta) dres = np.zeros( (n_x,n_t,n_g)) for i in range(n_draws): dres += weights[i] * dF[:,:,n_g*i:n_g*(i+1)] return [res,dres.swapaxes(1,2)]
def stochastic_residuals_2(s, theta, dr, f, g, parms, epsilons, weights, shape, no_deriv=False): n_t = len(theta) dr.theta = theta.copy().reshape(shape) # [x, x_s, x_theta] = dr.interpolate(s, with_theta_deriv=True) n_draws = epsilons.shape[1] [n_s, n_g] = s.shape ss = np.tile(s, (1, n_draws)) [xx, xx_s, xx_theta] = dr.interpolate(ss, with_theta_deriv=True) # should repeat theta instead n_x = xx.shape[0] # xx = np.tile(x, (1,n_draws)) ee = np.repeat(epsilons, n_g, axis=1) [SS, SS_ss, SS_xx, SS_ee] = g(ss, xx, ee, parms) [XX, XX_SS, XX_t] = dr.interpolate(SS, with_theta_deriv=True) [F, F_ss, F_xx, F_SS, F_XX, F_ee] = f(ss, xx, SS, XX, ee, parms) res = np.zeros((n_x, n_g)) for i in range(n_draws): res += weights[i] * F[:, n_g * i : n_g * (i + 1)] if no_deriv: return res from dolo.numeric.serial_operations import serial_multiplication as stm SS_theta = stm(SS_xx, xx_theta) XX_theta = stm(XX_SS, SS_theta) + XX_t dF = stm(F_xx, xx_theta) + stm(F_SS, SS_theta) + stm(F_XX, XX_theta) dres = np.zeros((n_x, n_t, n_g)) for i in range(n_draws): dres += weights[i] * dF[:, :, n_g * i : n_g * (i + 1)] return [res, dres.swapaxes(1, 2)]
def stochastic_residuals_3(s, theta, dr, f, g, parms, epsilons, weights, shape, no_deriv=False): import numpy n_t = len(theta) dr.theta = theta.copy().reshape(shape) # [x, x_s, x_theta] = dr.interpolate(s, with_theta_deriv=True) n_draws = epsilons.shape[1] [n_s,n_g] = s.shape [x, x_s, x_theta] = dr.interpolate(s, with_theta_deriv=True) # should repeat theta instead n_x = x.shape[0] # xx = np.tile(x, (1,n_draws)) #ee = np.repeat(epsilons, n_g , axis=1) # from dolo.numeric.serial_operations import serial_multiplication as stm # res = np.zeros( (n_x,n_g) ) dres = np.zeros( (n_x,n_t,n_g)) for i in range(n_draws): tt = [epsilons[:,i:i+1]]*n_g e = numpy.column_stack(tt) [S, S_s, S_x, S_e] = g(s, x, e, parms) [X, X_S, X_t] = dr.interpolate(S, with_theta_deriv=True) [F, F_s, F_x, F_S, F_X, F_e] = f(s, x, S, X, e, parms) res += weights[i] * F S_theta = stm(S_x, x_theta) X_theta = stm(X_S, S_theta) + X_t dF = stm(F_x, x_theta) + stm( F_S, S_theta) + stm( F_X , X_theta) dres += weights[i] * dF return [res,dres.swapaxes(1,2)]
def stochastic_residuals_2(s, theta, dr, f, g, parms, epsilons, weights, shape, no_deriv=False): n_t = len(theta) dr.theta = theta.copy().reshape(shape) # [x, x_s, x_theta] = dr.interpolate(s, with_theta_deriv=True) n_draws = epsilons.shape[1] [n_s,n_g] = s.shape ss = np.tile(s, (1,n_draws)) [xx, xx_s, xx_theta] = dr.interpolate(ss, with_theta_deriv=True) # should repeat theta instead n_x = xx.shape[0] # xx = np.tile(x, (1,n_draws)) ee = np.repeat(epsilons, n_g , axis=1) [SS, SS_ss, SS_xx, SS_ee] = g(ss, xx, ee, parms) [XX, XX_SS, XX_t] = dr.interpolate(SS, with_theta_deriv=True) [F, F_ss, F_xx, F_SS, F_XX, F_ee] = f(ss, xx, SS, XX, ee, parms) res = np.zeros( (n_x,n_g) ) for i in range(n_draws): res += weights[i] * F[:,n_g*i:n_g*(i+1)] if no_deriv: return res from dolo.numeric.serial_operations import serial_multiplication as stm SS_theta = stm( SS_xx, xx_theta) XX_theta = stm( XX_SS, SS_theta) + XX_t dF = stm(F_xx, xx_theta) + stm( F_SS, SS_theta) + stm( F_XX , XX_theta) dres = np.zeros( (n_x,n_t,n_g)) for i in range(n_draws): dres += weights[i] * dF[:,:,n_g*i:n_g*(i+1)] return [res,dres.swapaxes(1,2)]
def step_residual(s, x, dr, f, g, parms, epsilons, weights, x_bounds=None, serial_grid=True, with_derivatives=True): n_draws = epsilons.shape[1] [n_x, n_g] = x.shape from dolo.numeric.serial_operations import serial_multiplication as stm ss = np.tile(s, (1, n_draws)) xx = np.tile(x, (1, n_draws)) ee = np.repeat(epsilons, n_g, axis=1) if with_derivatives: [ssnext, g_ss, g_xx] = g(ss, xx, ee, parms)[:3] [xxnext, xxold_ss] = dr.interpolate(ssnext)[:2] if x_bounds: [lb, ub] = x_bounds(ssnext, parms) xxnext = np.maximum(np.minimum(ub, xxnext), lb) [val, f_ss, f_xx, f_ssnext, f_xxnext] = f(ss, xx, ssnext, xxnext, ee, parms)[:5] dval = f_xx + stm(f_ssnext, g_xx) + stm(f_xxnext, stm(xxold_ss, g_xx)) res = np.zeros((n_x, n_g)) for i in range(n_draws): res += weights[i] * val[:, n_g * i : n_g * (i + 1)] dres = np.zeros((n_x, n_x, n_g)) for i in range(n_draws): dres += weights[i] * dval[:, :, n_g * i : n_g * (i + 1)] if not serial_grid: dval = np.zeros((n_x, n_g, n_x, n_g)) for i in range(n_g): dval[:, i, :, i] = dres[:, :, i] else: dval = dres return [res, dval] else: [ssnext] = g(ss, xx, ee, parms, derivs=False)[:1] [xxnext] = dr.interpolate(ssnext)[:1] if x_bounds: [lb, ub] = x_bounds(ssnext, parms) xxnext = np.maximum(np.minimum(ub, xxnext), lb) [val] = f(ss, xx, ssnext, xxnext, ee, parms, derivs=False)[:1] res = np.zeros((n_x, n_g)) for i in range(n_draws): res += weights[i] * val[:, n_g * i : n_g * (i + 1)] return [res]
def stochastic_residuals_3(s, x, dr, f, g, parms, epsilons, weights, deriv=False): import numpy n_t = np.prod(x.shape) dr.set_values(x) # shortcut : let's call x theta for now n_draws = epsilons.shape[1] [n_s,n_g] = s.shape # xx = np.tile(x, (1,n_draws)) #ee = np.repeat(epsilons, n_g , axis=1) # from dolo.numeric.serial_operations import serial_multiplication as stm # if not deriv: x = dr.interpolate(s, with_theta_deriv=False, with_derivative=False) # should repeat theta instead n_x = x.shape[0] res = np.zeros( (n_x,n_g) ) for i in range(n_draws): tt = [epsilons[:,i:i+1]]*n_g e = numpy.column_stack(tt) S = g(s, x, e, parms) X = dr(S) F = f(s, x, e, S, X, parms) res += weights[i] * F return res else: [x, x_s, junk, x_theta] = dr.interpolate(s, with_derivative=True, with_theta_deriv=True, with_X_deriv=True) # should repeat theta instead n_x = x.shape[0] res = np.zeros( (n_x,n_g) ) dres = np.zeros( (n_x,n_t,n_x,n_t)) for i in range(n_draws): tt = [epsilons[:,i:i+1]]*n_g e = numpy.column_stack(tt) [S, S_s, S_x, S_e] = g(s, x, e, parms, derivs=True) [X, X_S, junk, X_t] = dr.interpolate(S, with_derivative=True, with_theta_deriv=True, with_X_deriv=True) [F, F_s, F_x, F_e, F_S, F_X] = f(s, x, e, S, X, parms, derivs=True) res += weights[i] * F S_theta = stm(S_x, x_theta) X_theta = stm(X_S, S_theta) + X_t dF = stm(F_x, x_theta) + stm( F_S, S_theta) + stm( F_X , X_theta) dres += weights[i] * dF return [res,dres.swapaxes(1,2)]
def step_residual(s, x, dr, f, g, parms, epsilons, weights, x_bounds=None, serial_grid=True, with_derivatives=True): n_draws = epsilons.shape[1] [n_x,n_g] = x.shape from dolo.numeric.serial_operations import serial_multiplication as stm ss = np.tile(s, (1,n_draws)) xx = np.tile(x, (1,n_draws)) ee = np.repeat(epsilons, n_g , axis=1) if with_derivatives: [ssnext, g_ss, g_xx] = g(ss,xx,ee,parms)[:3] [xxnext, xxold_ss] = dr.interpolate(ssnext)[:2] if x_bounds: [lb,ub] = x_bounds(ssnext, parms) xxnext = np.maximum(np.minimum(ub,xxnext),lb) [val, f_ss, f_xx, f_ssnext, f_xxnext] = f(ss,xx,ssnext,xxnext,ee,parms)[:5] dval = f_xx + stm(f_ssnext, g_xx) + stm(f_xxnext, stm(xxold_ss, g_xx)) res = np.zeros( (n_x,n_g) ) for i in range(n_draws): res += weights[i] * val[:,n_g*i:n_g*(i+1)] dres = np.zeros( (n_x,n_x,n_g) ) for i in range(n_draws): dres += weights[i] * dval[:,:,n_g*i:n_g*(i+1)] if not serial_grid: dval = np.zeros( (n_x,n_g,n_x,n_g)) for i in range(n_g): dval[:,i,:,i] = dres[:,:,i] else: dval = dres return [res, dval] else: [ssnext] = g(ss,xx,ee,parms,derivs=False)[:1] [xxnext] = dr.interpolate(ssnext)[:1] if x_bounds: [lb,ub] = x_bounds(ssnext, parms) xxnext = np.maximum(np.minimum(ub,xxnext),lb) [val] = f(ss,xx,ssnext,xxnext,ee,parms,derivs=False)[:1] res = np.zeros( (n_x,n_g) ) for i in range(n_draws): res += weights[i] * val[:,n_g*i:n_g*(i+1)] return [res]
p = 5 N = 500 import numpy.random V = numpy.random.multivariate_normal([0]*p,numpy.eye(p),size=p) print V M = numpy.zeros((p,p,N)) for i in range(N): M[:,:,i] = V from dolo.numeric.serial_operations import serial_multiplication as stm MM = numpy.zeros( (p,N) ) import time t = time.time() for i in range(100): T = serial_inversion(M) s = time.time() print('Elapsed :' + str(s-t)) tt = stm(M,T) for i in range(10): print tt[:,:,i]