def main(): mean = torch.tensor(np.ones(16), dtype=torch.float32) diag = 2 * torch.tensor(np.ones(16), dtype=torch.float32) population = Gaussian_Distribution(mean=mean, diag=diag, sub=0.3, type='chain', slash=1) truth = population.invcov.numpy() n = 1000 d = population.dim print(truth) dist, sample, _, S = population.generate(n, numpy_like=True) # print(S) # print(np.array(sample)) R = np.linalg.inv(S) # print(R) # print(sample) np.random.seed(0) for alpha in np.arange(0.001, 0.010, 0.001): model = LOCOV(alpha=alpha) model.fit(S) print(model.alpha) heatmap(model.prec) score = dict() score['log_lik'] = [] score['AIC'] = [] score['non_zero'] = [] alpha_list = np.hstack((np.arange(0, 0.1, 0.001), np.arange(0.11, 0.3, 0.01))) data = np.array(sample) for alpha in alpha_list: out_dict = cross_val_score_LOCOV(data, alpha=alpha) score['log_lik'].append(out_dict['log_lik']) score['AIC'].append(out_dict['AIC']) score['non_zero'].append(out_dict['non_zero']) plt.plot(alpha_list, score['log_lik']) plt.show() plt.plot(alpha_list, score['AIC']) plt.show() plt.plot(alpha_list, score['non_zero']) plt.show()
def main(): mean = torch.tensor(np.ones(16), dtype=torch.float32) diag = 2 * torch.tensor(np.ones(16), dtype=torch.float32) population = Gaussian_Distribution(mean=mean, diag=diag, sub=0.3, type='chain', slash=1) truth = population.invcov.numpy() n = 1000 d = population.dim data = pd.read_csv("samples.csv") sample = data.iloc[1:, 1:].values emp_cov = sample_cov(sample) #dist, sample, _, emp_cov = population.generate(n, numpy_like=True) model = GD() model.fit(emp_cov, alpha=0.05, lr=0.01) heatmap(emp_cov) heatmap(model.prec)
def main(): mean = torch.tensor(np.ones(16), dtype=torch.float32) diag = 2 * torch.tensor(np.ones(16), dtype=torch.float32) population = Gaussian_Distribution(mean=mean, diag=diag, sub=0.3, type='chain', slash=1, cross=7) truth = population.invcov.numpy() n = 1000 d = population.dim print(truth) dist, sample, _, emp_cov = population.generate(n, numpy_like=True) print(emp_cov) for alpha in np.arange(1e-3, 6e-3, 5e-4): model = MarjanovicCoordinateDescent(alpha=alpha) model.fit(emp_cov) print(alpha) heatmap(model.prec)
def main(): mean = torch.tensor(np.ones(16), dtype=torch.float32) diag = torch.tensor(np.ones(16), dtype=torch.float32) population = Gaussian_Distribution(mean=mean, diag=diag, sub=-0.3, type='chain', slash=1) truth = population.invcov.numpy() n = 1000 d = population.dim print(truth) dist, sample, _, emp_cov = population.generate(n, numpy_like=True) model = Glasso(alpha=0.06) model.fit(emp_cov) heatmap(model.prec) #heatmap(np.linalg.inv(model.prec)) #return score = dict() score['log_lik'] = [] score['AIC'] = [] score['non_zero'] = [] alpha_list = np.hstack((np.arange(1e-5, 0.1, 0.005), np.arange(0.1, 0.3, 0.01))) data = np.array(sample) for alpha in alpha_list: out_dict = cross_val_score_My_Glasso(data, alpha=alpha) score['log_lik'].append(out_dict['log_lik']) score['AIC'].append(out_dict['AIC']) score['non_zero'].append(out_dict['non_zero']) plt.plot(alpha_list, score['log_lik']) plt.show() plt.plot(alpha_list, score['AIC']) plt.show() plt.plot(alpha_list, score['non_zero']) plt.show()
def report(type_report): """[summary] Get report according user selection Args: integer: [integer to be used to select chosen dataset: 1, 2, 3 or 4]. Returns: [plot]: [plot with selected report] """ # Condition to ensure user selects proper petrol station id if petrol_station_id not in get_id_by_entityType1()[0]: result = print("Please, introduce correct Petrol Station Id!") # Condition to ensure user selects proper petrol product elif petrol_product_type not in product_list: result = print("Please, introduce correct Petrol product!") # Condition to ensure user selects proper type of report elif select_report not in type_options: result = print("Please, introduce correct type of report!") else: if (type_report == "1"): #Report 1: Box Plot for one selected Id result = box_price_one_id(csv_file, petrol_station_id, petrol_product_type) elif (type_report == "2"): # Report 2: Box Plot for all Id result = box_price_all_id(csv_file, petrol_product_type) elif (type_report == "3"): # Report 3: Line Plot showing price evolution for one selected Id result = evolucion_precio_gasolinera(csv_file, petrol_station_id, petrol_product_type) elif (type_report == "4"): # Report 4: Geographical HeatMap with the prices by product of all petrol stations # Introduction of user's API api = str(input("Enter your API for google maps: ")) result = heatmap(api, csv_file, petrol_product_type) return result
def main(): mean = torch.tensor(np.ones(32), dtype=torch.float32) diag = torch.tensor(np.ones(32), dtype=torch.float32) population = Gaussian_Distribution(mean=mean, diag=diag, sub=0.25, type='chain', slash=1) #truth = torch.inverse(population.cov) truth = population.invcov #print(x.sample()) n = 1000 d = population.dim eps = 1e-4 print(truth) _, _, _, S = population.generate(n) print(S) R = torch.inverse(S) print(R) x = torch.ones(3, 3) x.requires_grad = True l = L1_penal((x)) l.backward() print(x.grad) NLogL = net(dim=d, diag=1.0, beta=0.05) param_groups = [] param_groups.append({'params': [NLogL.X]}) optimizer = GD(params=param_groups, lr=0.5, weight_decay=0) scheduler = torch.optim.lr_scheduler.MultiStepLR( optimizer, milestones=[40, 120, 300, 600], gamma=0.1) performance = dict() performance['loss'] = [] performance['iteration'] = [] max_iter = 800 for i in range(1, max_iter + 1): out = NLogL.forward(S) optimizer.zero_grad() out.backward() optimizer.step() scheduler.step() if i % 1 == 0: loss = torch.norm(NLogL.X.clone().detach() - truth).item() performance['loss'].append(loss) performance['iteration'].append(i) if i % 100 == 0: print("iteration={}".format(i)) print("Frobenius loss={}".format(loss)) #print("-log_likelihood={}".format(loss.item())) #print("gradient={}".format(NLogL.X.grad)) NLogL.X.requires_grad = False zero = torch.zeros(d, d) Y = NLogL.X Y_c = Y.where(abs(Y) > eps, zero) R_c = R.where(abs(R) > eps, zero) print("estimation={}".format(Y)) print("censored={}".format(Y_c)) print("true_precision={}".format(truth)) print(population.cov) print("sample_cov={}".format(S)) print("direct_inverse={}".format(R)) #plt.plot(performance['iteration'], performance['loss']) #plt.show() font_setting = {'fontsize': 5} sns.heatmap(torch.abs(truth).numpy(), annot=truth.numpy(), annot_kws=font_setting, cmap="YlGn", vmin=0, vmax=0.5, square=True) plt.show() sns.heatmap(torch.abs(Y_c).numpy(), annot=Y_c.numpy(), annot_kws=font_setting, cmap="YlGn", vmin=0, vmax=0.5, square=True) plt.show() sns.heatmap(torch.abs(R_c).numpy(), annot=R_c.numpy(), annot_kws=font_setting, cmap="YlGn", vmin=0, vmax=0.5, square=True) plt.show() heatmap(Y_c.numpy())