def find_principal_securities(marketcode, event_filename, date_range, T, filename): event_data = np.loadtxt('csv/' + event_filename + '.csv', dtype=np.unicode, delimiter=',')[1:, :] date_from = datetime.strptime(date_range[0], base.DATE_FORMAT) date_to = datetime.strptime(date_range[1], base.DATE_FORMAT) event_date_list = [] for event in event_data: date = datetime.strptime(event[1], base.DATE_FORMAT) if date_from <= date <= date_to: event_date_list.append(date.date()) offset = 0 num_clusters = 4 event_security_cluster = [] change_generator, security_list = base.observed_changes(marketcode, date_range, always=True, T=T) event_idx = 0 event_date = event_date_list[event_idx] last_data_change = np.array([]) for data_change, date in change_generator: if date > event_date: traded_securities_ids = ~np.all(last_data_change == 0, axis=0) effective_change = last_data_change[:, traded_securities_ids] traded_securities = security_list[traded_securities_ids] corr = pearson_correlation(effective_change, offset) cluster_idx = cluster_data(corr, num_clusters=num_clusters) security_clusters = np.empty(len(security_list)) * np.nan for i in range(num_clusters): security_clusters[np.in1d( security_list[:, 0], traded_securities[cluster_idx == i, 0])] = i event_security_cluster.append(security_clusters) event_idx = event_idx + 1 if event_idx >= len(event_date_list): break event_date = event_date_list[event_idx] last_data_change = data_change event_security_cluster = np.array(event_security_cluster) header = ['ISIN', 'issuer'] data_csv = np.array([header]) for row in security_list[np.all(event_security_cluster == 0, axis=0)]: data_csv = np.append(data_csv, [row], axis=0) np.savetxt('csv/' + filename + '.csv', data_csv, fmt='%s', delimiter=',')
def market_stress_matrix(marketcode, date, T, filename): num_clusters = 4 change_generator, security_list = base.observed_changes(marketcode, (date,date), T=T) change_data, date = next(change_generator) corr = pearson_correlation(change_data) order_metric = np.average(corr, axis=0) cluster_idx = cluster_data(corr, num_clusters=num_clusters) data_sorted_idx = np.lexsort([np.negative(order_metric), cluster_idx]) corr = corr[data_sorted_idx,:][:,data_sorted_idx] security_list = security_list[data_sorted_idx] header = ['isin'] + [security[0] for security in security_list] data_csv = np.array([header]) for security_idx, security in enumerate(security_list): data_csv = np.append(data_csv, np.concatenate(([[security[0]]], [corr[security_idx]]), axis=1), axis=0) np.savetxt('csv/'+filename+'.csv', data_csv, fmt='%s', delimiter=',')
param_fit_normal, param_error_normal = base.plot_baseline_stress( baseline_stress_file) ###### market_code = 'SWX' series_length = 20 event_filename = 'market_stress_events' date_range = ('01.01.2000', '31.12.2018') stress_series_file = 'stress_series_{}_{}_{}-{}'.format( market_code, series_length, base.datestamp(date_range[0]), base.datestamp(date_range[1])) if recalculate: change_generator, security_list = base.observed_changes(market_code, date_range, always=False, T=series_length) code.stress_series(change_generator, offset=0, filename=stress_series_file) else: security_list = [None] * 314 base.plot_time_series({'T=20': stress_series_file}, event_filename=event_filename) data_range_zoom = ('01.01.2016', '31.12.2016') stress_series_zoom_file = 'stress_series_{}_{}_{}-{}'.format( market_code, series_length, base.datestamp(data_range_zoom[0]), base.datestamp(data_range_zoom[1])) if recalculate: change_generator, _ = base.observed_changes(market_code, data_range_zoom, always=False,