def test_lasso_signal_primal(setup_param_lasso, expected_beta): expected_beta_lasso_signal = expected_beta['values_signal']['lasso'] calculated_beta_primal = fused_lasso_primal(**setup_param_lasso['data_' 'primal']) assert_allclose(expected_beta_lasso_signal, calculated_beta_primal, atol=2e-2)
def test_lasso_general_primal(setup_param_lasso, expected_beta): setup_param_lasso['data_primal']['X'] = np.array([[1, 0.5], [0.5, 1]]) setup_param_lasso['data_primal']['s1'] = 2.09999 expected_beta_lasso_general = expected_beta['values_general']['lasso'] calculated_beta_lagrange = fused_lasso_primal( **setup_param_lasso['data_primal']) assert_allclose(expected_beta_lasso_general, calculated_beta_lagrange, atol=2e-2)
def fit(self, X, y): """Fit unkown parameters *beta_hat* to *X* and *y*. Example: -------- >>> reg = FusedLassoEstimator( s1=1, s2=10).fit(X, y) Args: | X (np.ndarray): 2d array of independent variables. | y (np.ndarray): 1d array of dependent variables. Returns: | b.value (np.ndarray) """ self.beta = fused_lasso_primal(y, X, self.s1, self.s2) return self.beta
def test_fused_lasso_primal_wrong_dimension_penalty(setup_param_lasso): setup_param_lasso['data_primal']['s1'] = [1, 2] with pytest.raises(TypeError): fused_lasso_primal(**setup_param_lasso['data_primal'])
def test_fused_lasso_primal_negative_penalty(setup_param_lasso): setup_param_lasso['data_primal']['s1'] = -1 with pytest.raises(ValueError): fused_lasso_primal(**setup_param_lasso['data_primal'])
"""Calculate fused lasso estimates for CGH data and plot them.""" import matplotlib.pyplot as plt import numpy as np from bld.project_paths import project_paths_join as ppj from src.model_code.fused_lasso_primal import fused_lasso_primal if __name__ == "__main__": # Set penalty constants, load CGH data and estimate fused lasso. CGH_DATA = np.loadtxt(ppj("IN_DATA", "cgh.txt")) BETA_HAT = fused_lasso_primal(CGH_DATA, np.identity(len(CGH_DATA)), 160, 15) # Plot CGH data. plt.xlabel('Genome Order') plt.ylabel('Copy Number') plt.plot(CGH_DATA, "bo") plt.axhline(color='r') plt.savefig(ppj("OUT_FIGURES", "cgh_plot_raw.png")) # Plot CGH data with fused lasso estimates. plt.xlabel('Genome Order') plt.ylabel('Copy Number') plt.plot(CGH_DATA, "bo") plt.axhline(color='r') plt.plot(BETA_HAT, color='orange') plt.savefig(ppj("OUT_FIGURES", "cgh_plot_beta.png")) plt.clf()
# Load data from json file. N_FEATURES = SIM_DICT["p"] N_OBS = SIM_DICT["n"] N_SIMULATIONS = SIM_DICT['num_simulations'] # Building containers to store simulation results. BETA_HAT = np.empty((N_FEATURES, N_SIMULATIONS)) Y_HAT = np.empty((N_OBS, N_SIMULATIONS)) RESIDUALS = np.empty((N_OBS, N_SIMULATIONS)) # Calculate beta estimates for cross-validated penalty terms. for i in range(N_SIMULATIONS): start_time_estimation = time() BETA_HAT[:, i] = fused_lasso_primal(y[:, i], X, S1_OPT, S2_OPT) #fle(penalty_cv[0], penalty_cv[1]).fit(X, y[:, i]) Y_HAT[:, i] = np.matmul(X, BETA_HAT[:, i]) RESIDUALS[:, i] = y[:, i] - Y_HAT[:, i] end_time_estimation = time() # Save timings for fused lasso setting. if REG_NAME == 'fused': with open(ppj("OUT_ANALYSIS", "time_list_{}.pickle". format(SIM_NAME)), "rb") as in_file: TIMINGS = pickle.load(in_file) TIME_ESTIMATION = np.round((end_time_estimation - start_time_estimation), 2) TIMINGS.append(TIME_ESTIMATION)