def infer_discrete_state_transition_from_training_data(is_non_local, penalty=1e-5): data = pd.DataFrame({ 'is_non_local': is_non_local.astype(np.float64), 'lagged_is_non_local': lagmat(is_non_local, maxlag=1).astype(np.float64).squeeze(), }).dropna() MODEL_FORMULA = 'is_non_local ~ 1 + lagged_is_non_local' response, design_matrix = dmatrices(MODEL_FORMULA, data) penalty = np.ones((design_matrix.shape[1], )) * penalty penalty[0] = 0.0 fit = penalized_IRLS(design_matrix, response, family=families.Binomial(), penalty=penalty) predict_data = { 'lagged_is_non_local': np.asarray([0, 1]), } predict_design_matrix = build_design_matrices( [design_matrix.design_info], predict_data, NA_action=NAAction(NA_types=[]))[0] non_local_probability = families.Binomial().link.inverse( predict_design_matrix @ np.squeeze(fit.coefficients)) non_local_probability[np.isnan(non_local_probability)] = 0.0 return np.asarray( [[1 - non_local_probability[0], non_local_probability[0]], [1 - non_local_probability[1], non_local_probability[1]]])
def fit_glm(response, design_matrix, penalty=None, tolerance=1E-5): if penalty is not None: penalty = np.ones((design_matrix.shape[1],)) * penalty penalty[0] = 0.0 # don't penalize the intercept else: penalty = np.finfo(np.float).eps return penalized_IRLS( design_matrix, response.squeeze(), family=families.Poisson(), penalty=penalty, tolerance=tolerance)
def fit_discrete_state_transition(speed, is_replay, penalty=1E-5, speed_knots=None, diagonal=None): """Estimate the predicted probablity of replay given speed and whether it was a replay in the previous time step. p(I_t | I_t-1, v_t-1) p_I_0, p_I_1 in Long Tao's code Parameters ---------- speed : ndarray, shape (n_time,) is_replay : boolean ndarray, shape (n_time,) speed_knots : ndarray, shape (n_knots,) Returns ------- probability_replay : ndarray, shape (n_time, 2) """ data = pd.DataFrame({ 'is_replay': is_replay.astype(np.float64), 'lagged_is_replay': lagmat(is_replay, maxlag=1).astype(np.float64).squeeze(), 'lagged_speed': lagmat(speed, maxlag=1).squeeze() }).dropna() if speed_knots is None: speed_mid_point = np.nanmedian(speed[speed > 10]) speed_knots = [1., 2., 3., speed_mid_point] MODEL_FORMULA = ( 'is_replay ~ 1 + lagged_is_replay + ' 'cr(lagged_speed, knots=speed_knots, constraints="center")') response, design_matrix = dmatrices(MODEL_FORMULA, data) penalty = np.ones((design_matrix.shape[1], )) * penalty penalty[0] = 0.0 fit = penalized_IRLS(design_matrix, response, family=FAMILY, penalty=penalty) if np.isnan(fit.AIC): logger.error("Discrete state transition failed to fit properly. " "Try specifying `speed_knots`") return partial(predict_probability, design_matrix=design_matrix, coefficients=fit.coefficients)
def fit_discrete_state_transition_no_speed(speed, is_replay, penalty=1E-5, speed_knots=None, diagonal=None): """Estimate the predicted probablity of replay and whether it was a replay in the previous time step. p(I_t | I_t-1, v_t-1) p_I_0, p_I_1 in Long Tao's code Parameters ---------- speed : ndarray, shape (n_time,) is_replay : boolean ndarray, shape (n_time,) speed_knots : ndarray, shape (n_knots,) Returns ------- probability_replay : ndarray, shape (n_time, 2) """ data = pd.DataFrame({ 'is_replay': is_replay.astype(np.float64), 'lagged_is_replay': lagmat(is_replay, maxlag=1).astype(np.float64).squeeze(), 'lagged_speed': lagmat(speed, maxlag=1).squeeze() }).dropna() MODEL_FORMULA = 'is_replay ~ 1 + lagged_is_replay' response, design_matrix = dmatrices(MODEL_FORMULA, data) penalty = np.ones((design_matrix.shape[1], )) * penalty penalty[0] = 0.0 fit = penalized_IRLS(design_matrix, response, family=FAMILY, penalty=penalty) return partial(predict_probability, design_matrix=design_matrix, coefficients=fit.coefficients)
def fit_glm_model(spikes, design_matrix, penalty=1E1): """Fits the Poisson model to the spikes from a neuron. Parameters ---------- spikes : array_like design_matrix : array_like or pandas DataFrame ind : int penalty : float, optional Returns ------- fitted_model : statsmodel results """ penalty = np.ones((design_matrix.shape[1],)) * penalty penalty[0] = 0.0 results = penalized_IRLS( np.array(design_matrix), np.array(spikes), family=families.Poisson(), penalty=penalty) return np.squeeze(results.coefficients)
def fit_replay_state_transition(speed, is_replay, penalty=1E-5): """Estimate the predicted probablity of replay given speed and whether it was a replay in the previous time step. p(I_t | I_t-1, v_t-1) p_I_0, p_I_1 in Long Tao's code Parameters ---------- speed : ndarray, shape (n_time,) is_replay : boolean ndarray, shape (n_time,) Returns ------- probability_replay : ndarray, shape (n_time, 2) """ data = pd.DataFrame({ 'is_replay': is_replay.astype(np.float64), 'lagged_is_replay': lagmat(is_replay, maxlag=1).astype(np.float64).squeeze(), 'lagged_speed': lagmat(speed, maxlag=1).squeeze() }).dropna() MODEL_FORMULA = ( 'is_replay ~ 1 + lagged_is_replay + ' 'cr(lagged_speed, knots=[1, 2, 3, 20], constraints="center")') response, design_matrix = dmatrices(MODEL_FORMULA, data) family = families.Binomial() penalty = np.ones((design_matrix.shape[1], )) * penalty penalty[0] = 0.0 fit = penalized_IRLS(design_matrix, response, family=family, penalty=penalty) return partial(predict_probability, design_matrix=design_matrix, coefficients=fit.coefficients)
def fit_glm_model(spikes, design_matrix, penalty=3): '''Fits the Poisson model to the spikes from a neuron. Parameters ---------- spikes : array_like design_matrix : array_like or pandas DataFrame ind : int penalty : float, optional Returns ------- fitted_model : statsmodel results ''' regularization_weights = np.ones((design_matrix.shape[1], )) * penalty regularization_weights[0] = 0.0 return np.squeeze( penalized_IRLS(np.array(design_matrix), np.array(spikes), family=families.Poisson(), penalty=regularization_weights).coefficients)