def temp_model(state_grid, obs_grid, source_terms, params): source_grid, source_weights = source_terms[0], source_terms[1] # Unpack params: V, W, prior_guess, prior_var obs_noise, move_noise = params[0], params[1] prior_guess, prior_var = params[2], params[3] N, p, q = state_grid[0].size, obs_grid[0].size, source_grid[0].size dx = state_grid[0][1] - state_grid[0][0] # Define relevant parameters k, rho, c_p = 0.026, 1.2, 0.001 # The standard properties of air alpha = k / (rho * c_p) dt = 0.1 * dx**2 / (2 * alpha) lam, gamma = alpha * dt / (dx**2), dt / (rho * c_p) # Construct G, F, and Q G = heat_eq_state_mat(state_grid, dx, lam) F = heat_eq_obs_mat(obs_grid, state_grid, dx) Q = heat_eq_source_mat(source_terms, state_grid, gamma) # Construct W and V W = np.identity(N) * move_noise V = np.identity(p) * obs_noise # Construct m and C m = np.array([prior_guess] * N) C = np.identity(N) * prior_var # Create and return model model = kf.DLM([G, W], [F, V], [m, C], Q) return model
def default_model(): # Create default model V, W = 0.5, 0.01 W = np.array([[W]]) space_params = [np.array([[1]]), np.array([[V]])] state_params = [np.array([[1]]), W] prior = [np.array([[0]]), np.array([[1]])] model = kf.DLM(state_params, space_params, prior) return model
def default_RWWN_model(): # Create default model # Random walk with noise V, W = 0.5, 0.1 obs_params = [np.array([1]), np.array([[V]])] state_params = [np.array([1]), np.array([[W]])] prior = [np.array([80]), np.array([[1]])] source = np.array([0]) model = kf.DLM(state_params, obs_params, prior, source) return model
def default_RWWN_vel_model(): # Create default model # Random walk with noise and time-varying velocity V, W_x, W_v = 10, 0.1, 0.5 prior_x, prior_v = 80, 1 prior_x_var, prior_v_var = 1, 1 obs_params = [np.array([[1, 0]]), np.array([[V]])] state_params = [np.array([[1, 1], [0, 1]]), np.array([[W_x, 0], [0, W_v]])] prior = [np.array([prior_x, prior_v]), np.array([[prior_x_var, 0], [0, prior_v_var]])] model = kf.DLM(state_params, obs_params, prior, np.array([0, 0])) return model
def target_model(params): # Parmaters: gamma, var_Y, var_x, var_v, prior_mean, prior_var_x, prior_var_v gamma = params[0] var_Y = params[1] var_x, var_v = params[2], params[3] prior_mean, prior_var_x, prior_var_v = params[4], params[5], params[6] I_2 = np.identity(2) Zero_2 = np.zeros((2, 2)) top = np.concatenate((I_2, I_2), axis=1) bottom = np.concatenate((Zero_2, np.array([[1, -gamma], [-gamma, 1]])), axis=1) G = np.concatenate((top, bottom)) F = np.concatenate((I_2, Zero_2), axis=1) top = np.concatenate((var_x * I_2, Zero_2), axis=1) bottom = np.concatenate((Zero_2, var_v * I_2), axis=1) W = np.concatenate((top, bottom)) V = var_Y * I_2 top = np.concatenate((prior_var_x * I_2, Zero_2), axis=1) bottom = np.concatenate((Zero_2, prior_var_v * I_2), axis=1) prior_var = np.concatenate((top, bottom)) model = kf.DLM([G, W], [F, V], [prior_mean, prior_var], np.array([0, 0, 0, 0])) return model
import kalman_filter as kf import plotting as pl active = True while active: default = input('Use default simulation parameters? Y/N: ') if default != 'Y': params = [0.0] * 5 params[0] = float(input('Enter state (movement) variance: ')) params[1] = float(input('Enter space (obervational) variance: ')) init = float(input('Enter initial state value: ')) params[2] = float(input('Enter prior mean: ')) params[3] = float(input('Enter prior variance: ')) obs_params = [np.array([1]), np.array([[params[1]]])] state_params = [np.array([1]), np.array([[params[0]]])] prior = [np.array([params[2]]), np.array([[params[3]]])] source = np.array([0]) model = kf.DLM(state_params, obs_params, prior, source) else: model = ex.default_RWWN_model() init = 80 length = int(input('Enter length of simulation: ')) print('Simulating data...') theta, Y = model.simulate(length, np.array([init])) print('Applying filter to simulated data...') fit_values, fit_var = model.kfilter(Y[::, 0]) print('Plotting results...') pl.one_d_kalman_plot(theta, Y, fit_values, fit_var) again = input('Run program again? Y/N: ') if again != 'Y': break
# Write option to enter parameters default = input('Use default simulation parameters? Y/N: ') if default != 'Y': params = [0] * 4 init_temp = float(input('Enter initial temperature: ')) init = np.array([init_temp] * N) params[0] = float(input('Enter temperature movement variance: ')) params[1] = float(input('Enter observational variance: ')) params[2] = float(input('Enter prior guess of overall temperature: ')) params[3] = float(input('Enter prior variance: ')) else: params = ex.default_Heat_params() init = np.array([70] * N) sim_model = tg.temp_model(state_grid, obs_grid, source_terms, params) source_in_filter = input( 'Allow Kalman filter to know about the sources/sinks? Y/N: ') if source_in_filter != 'Y': pred_model = kf.DLM(sim_model.state_params, sim_model.space_params, sim_model.filter_params, np.array([0] * N)) else: pred_model = sim_model length = int(input('Enter desired number of frames to simulate: ')) print('Simulating data...') theta, Y = sim_model.simulate(length, init) print('Filtering simulated data...') fit_values, fit_var = pred_model.kfilter(Y) print('Constructing animations...') anim.animate_heat_eqn_wrapper(state_grid, obs_grid, theta, fit_values, Y) again = input('Run program again? Y/N: ') if again != 'Y': break
W_v = float(input('Enter state velocity variance: ')) V = float(input('Enter space (obervational) variance: ')) init[0] = float(input('Enter initial position: ')) init[1] = float(input('Enter initial velocity: ')) prior_x = float(input('Enter prior guess for position: ')) prior_v = float(input('Enter prior guess for velocity: ')) prior_x_var = float(input('Enter prior position variance: ')) prior_v_var = float(input('Enter prior velocity variance: ')) obs_params = [np.array([[1, 0]]), np.array([[V]])] state_params = [ np.array([[1, 1], [0, 1]]), np.array([[W_x, 0], [0, W_v]]) ] prior = [ np.array([prior_x, prior_v]), np.array([[prior_x_var, 0], [0, prior_v_var]]) ] model = kf.DLM(state_params, obs_params, prior, np.array([0, 0])) else: model = ex.default_RWWN_vel_model() init = [80, 1] length = int(input('Enter length of simulation: ')) print('Simulating data...') theta, Y = model.simulate(length, np.array([init])) print('Applying filter to simulated data...') fit_values, fit_var = model.kfilter(Y[::, 0]) print('Plotting results...') pl.one_d_kalman_plot(theta, Y, fit_values, fit_var) again = input('Run program again? Y/N: ') if again != 'Y': break