def sensitivity_analysis(force=False): data_file = os.path.join(DATA_FOLDER, 'sensitivity_analysis.p') if os.path.exists(data_file) and not force: data = backup.load(data_file) return data n_good_cond = 3, 4 data = {g: {} for g in n_good_cond} for n_good in n_good_cond: d = simulation.run.get_data(n_good=n_good) alpha = [i[0] for i in d.cognitive_parameters] beta = [i[1] for i in d.cognitive_parameters] gamma = [i[2] for i in d.cognitive_parameters] observation = analysis.metric.metric.get_economy_measure( in_hand=d.in_hand, desired=d.desired, prod=d.prod, cons=d.cons, m=0) data[n_good][r'$\alpha$'] = alpha data[n_good][r'$\beta$'] = beta data[n_good][r'$\gamma$'] = gamma data[n_good]['ind0'] = observation backup.save(data, data_file) return data
def get_data(force=False): if force or not os.path.exists(DATA_FILE): data = _load__data_from_db() backup.save(obj=data, file_name=DATA_FILE) else: data = backup.load(DATA_FILE) return data
def load_if_not_existing(file_name, method, **kwargs): folder = os.path.join("data", "pickle") os.makedirs(folder, exist_ok=True) file_path = os.path.join(folder, file_name) if os.path.exists(file_path): data = load(file_path) else: data = method(**kwargs) save(obj=data, file_name=file_path) return data
def get(xp_data_list=None, room_n_good=None, room_uniform=None, extension=''): if xp_data_list is None: xp_data_list, room_n_good, room_uniform = xp.get_data() file_path = f'{DATA_FOLDER}/fit{extension}.p' if not os.path.exists(file_path): alpha, beta, gamma, mean_p, lls, bic, eco = \ produce_fit(xp_data_list, room_n_good, room_uniform) backup.save(obj=(alpha, beta, gamma, mean_p, lls, bic, eco), file_name=file_path) else: alpha, beta, gamma, mean_p, lls, bic, eco = \ backup.load(file_name=file_path) return alpha, beta, gamma, mean_p, lls, bic, eco
def phase_diagram(m=0): data_file = f'{DATA_FOLDER}/formatted_phase_diagram.p' if os.path.exists(data_file): data, labels = backup.load(data_file) return data, labels data = {} for n_good in (4, 3): d = simulation.run.get_data(n_good=n_good) dist = d.distribution n = len(dist) # Number of economies in this batch observation = metric.get_economy_measure(in_hand=d.in_hand, desired=d.desired, prod=d.prod, cons=d.cons, m=m) unq_repartition = np.unique(dist, axis=0) labels = np.unique([i[-1] for i in unq_repartition]) n_side = len(labels) scores = np.array([ np.mean([observation[i] for i in range(n) if np.all(dist[i] == r)]) for r in unq_repartition ]) d = scores.reshape(n_side, n_side).T data[n_good] = d backup.save((data, labels), data_file) return data, labels
def phase_diagram(f_name='phase.pdf'): """ plot phase diagrams with 3 and 4 goods """ # Number of column # (Plot for each good considered as money if max_col == None) max_col = 1 data_file = f'{DATA_FOLDER}/formatted_phase_diagram.p' if os.path.exists(data_file): data, labels = backup.load(data_file) else: data = [] for n_good in 3, 4: d = simulation.run.get_data(phase=True, n_good=n_good) formatted_data, labels = metric.phase_diagram( in_hand=d.in_hand, desired=d.desired, prod=d.prod, cons=d.cons, distribution=d.distribution, n_good=len(d.distribution[0])) data.append(formatted_data) backup.save((data, labels), data_file) graph.phase_diagram.plot(data=data, labels=labels, f_name=f_name, max_col=max_col)
def run(file_name='data/exp.p'): if os.path.exists(file_name): return backup.load(file_name) rooms = Room.objects.all().order_by('id') data = {} for r in rooms: n_good = r.n_type print(r.id) monetary_behavior = np.zeros((n_good, r.n_user, r.t_max)) medium = np.ones((n_good, r.n_user, r.t_max)) * -1 ordered_goods = [ n_good - 1, ] + list(range(n_good - 1)) group_users = [ User.objects.filter(room_id=r.id, production_good=Converter.reverse_value( g, n_good)).order_by('id') for g in ordered_goods ] for m in range(n_good): for t in range(r.t_max): i = 0 for users in group_users: for u in users: choices = Choice.objects.filter(room_id=r.id, t=t, user_id=u.id) for c in choices: desired = Converter.convert_value(c.desired_good, n_good=n_good) in_hand = Converter.convert_value(c.good_in_hand, n_good=n_good) prod = Converter.convert_value(u.production_good, n_good=n_good) cons = Converter.convert_value(u.consumption_good, n_good=n_good) if m in (prod, cons): monetary_conform = (in_hand, desired) == (prod, cons) medium[m, i, t] = -1 else: monetary_conform = (in_hand, desired) in [(prod, m), (m, cons)] medium[m, i, t] = monetary_conform monetary_behavior[m, i, t] = monetary_conform i += 1 distribution = economy.distributions.get(r.id) label = economy.labels.get(r.id) data[label] = { 'monetary_bhv': monetary_behavior, 'medium': medium, 'distribution': distribution } backup.save(data, file_name=file_name) return data
def load(self, data_folder): for k, v in self._files_mapping(data_folder).items(): setattr(self, v, backup.load(file_name=k))