def square_root_diffusion_exact(initial_val=0.05, kappa=3.0, theta=0.02, sigma=0.1, time_year=2, num_samples=10000, num_time_interval_discretization=50): x = np.zeros((num_time_interval_discretization + 1, num_samples)) x[0] = initial_val dt = time_year / num_time_interval_discretization for t in range(1, num_time_interval_discretization + 1): df = 4 * theta * kappa / sigma**2 c = (sigma**2 * (1 - np.exp(-kappa * dt))) / (4 * kappa) nc = np.exp(-kappa * dt) / c * x[t - 1] x[t] = c * npr.noncentral_chisquare(df, nc, size=num_samples) plt.figure(figsize=(10, 6)) plt.hist(x[-1], bins=50) plt.title("Square root diffusion Exact") plt.xlabel('value') plt.ylabel('frequency') plt.show() plt.figure(figsize=(10, 6)) plt.plot(x[:, :10], lw=1.5) plt.xlabel('time') plt.ylabel('index level') plt.title('Sample Path SRD Exact') plt.show() return x
def geometric_brownian_motion_option_pricing( initial_val=100, num_samples=10000, riskless_rate=0.05, volatility_sigma=0.25, time_year=2.0, num_time_interval_discretization=50): dt = time_year / num_time_interval_discretization samples = np.zeros((num_time_interval_discretization + 1, num_samples)) samples[0] = initial_val for t in range(1, num_time_interval_discretization + 1): samples[t] = samples[t - 1] * np.exp( (riskless_rate - 0.5 * (volatility_sigma**2)) * dt + volatility_sigma * np.sqrt(dt) * npr.standard_normal(num_samples)) print(45 * "=") print(samples[1]) plt.figure(figsize=(10, 6)) plt.hist(samples[50], bins=50) plt.title("Geometric Brownian Motion") plt.xlabel('index level') plt.ylabel('frequency') plt.show() plt.figure(figsize=(10, 6)) plt.plot(samples[:, :10], lw=1.5) plt.xlabel('time') plt.ylabel('index level') plt.title('Sample Path') plt.show() return samples
def square_root_diffusion_euler(initial_val=0.05, kappa=3.0, theta=0.02, sigma=0.1, time_year=2, num_samples=10000, num_time_interval_discretization=50): dt = time_year / num_time_interval_discretization xh = np.zeros((num_time_interval_discretization + 1, num_samples)) x = np.zeros_like(xh) xh[0] = initial_val x[0] = initial_val for t in range(1, num_time_interval_discretization + 1): xh[t] = (xh[t - 1] + kappa * (theta - np.maximum(xh[t - 1], 0)) * dt + sigma * np.sqrt(np.maximum(xh[t - 1], 0)) * math.sqrt(dt) * npr.standard_normal(num_samples)) x = np.maximum(xh, 0) plt.figure(figsize=(10, 6)) plt.hist(x[-1], bins=50) plt.xlabel('value') plt.ylabel('frequency') plt.title('Square root diffusion Approx Euler') plt.show() plt.figure(figsize=(10, 6)) plt.plot(x[:, :10], lw=1.5) plt.xlabel('time') plt.ylabel('index level') plt.title('Sample Path SRD approx') plt.show() return x
def hexbin_plot(self, var1, var2, force=False): fig_name = "{}/hexbin_{}_{}.pdf".format(self.fig_folder, var1, var2) if path.exists(fig_name) and not force: return if var1 == "customer_extra_view_choices" and var2 == "delta_position": print("Doing hexbin plot '{}' against '{}'.".format(var2, var1)) x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) plt.xlim(self.range_var[var1]) plt.ylim(self.range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) hb = ax.hexbin(x=x, y=y, gridsize=20, cmap='inferno') ax.set_facecolor('black') cb = fig.colorbar(hb, ax=ax) cb.set_label('counts') plt.savefig(fig_name) if self.display: plt.show() plt.close()
def distribution(): sample_size = 500 rn1 = npr.standard_normal(sample_size) rn2 = npr.normal(100, 20, sample_size) rn3 = npr.chisquare(df=0.5, size=sample_size) rn4 = npr.poisson(lam=1.0, size=sample_size) fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(10, 8)) ax1.hist(rn1, bins=25, stacked=True) ax1.set_title('Standard normal') ax1.set_ylabel('frequency') ax2.hist(rn2, bins=25) ax2.set_title('Normal 100,20') ax3.hist(rn3, bins=25) ax3.set_title('Chi squared') ax3.set_ylabel("frequency") ax4.hist(rn4, bins=25) ax4.set_title('Poisson') plt.show()
def main(): msft = yf.Ticker("MSFT") euro_security_process, euro_trials, euro_price_mean = european_monte_carlo_valuation( trials=10000, partitions=50, time=1.0, S0=msft.info.get("previousClose"), r=0.05, sigma=0.64, strike=210, option='call') simulated_returns_data = pd.DataFrame(euro_security_process[:, :200]) simulated_returns = np.log( 1 + simulated_returns_data.astype(float).pct_change()) actual_returns_data = msft.history(period="1y")['Close'] actual_returns_data = np.log( 1 + actual_returns_data.astype(float).pct_change()) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 12)) ax1.hist(simulated_returns.values.flatten(), bins=50) ax1.set_xlabel('Theoretical Returns') ax1.set_ylabel('Frequency') ax2.hist(actual_returns_data, bins=50) ax2.set_xlabel('Observed Returns') ax2.set_ylabel('Frequency') plt.show()
def test_run(self): # 生成几何布朗运动市场环境 me_gbm = MarketEnvironment('me_gbm', dt.datetime(2020, 1, 1)) me_gbm.add_constant('initial_value', 36.0) me_gbm.add_constant('volatility', 0.2) me_gbm.add_constant('final_date', dt.datetime(2020, 12, 31)) me_gbm.add_constant('currency', 'EUR') me_gbm.add_constant('frequency', 'M') me_gbm.add_constant('paths', 1000) csr = ConstantShortRate('csr', 0.06) me_gbm.add_curve('discount_curve', csr) # 生成几何布朗运动模拟类 gbm = GeometricBrownianMotion('gbm', me_gbm) gbm.generate_time_grid() # 生成跳跃扩散市场环境 me_jd = MarketEnvironment('me_jd', dt.datetime(2020, 1, 1)) me_jd.add_constant('lambda', 0.3) me_jd.add_constant('mu', -0.75) me_jd.add_constant('delta', 0.1) me_jd.add_environment(me_gbm) # 生成跳跃扩散模拟类 jd = JumpDiffusion('jd', me_jd) paths_3 = jd.get_instrument_values() jd.update(lamb=0.9) paths_4 = jd.get_instrument_values() # 绘制图形 plt.figure(figsize=(10, 6)) p1 = plt.plot(gbm.time_grid, paths_3[:, :10], 'b') p2 = plt.plot(gbm.time_grid, paths_4[:, :10], 'r-') lengend1 = plt.legend([p1[0], p2[0]], ['low intensity', 'high intensity'], loc=3) plt.gca().add_artist(lengend1) plt.xticks(rotation=30) plt.show()
def remove_extreme_value(self, df_input): if self.is_plot: del df_input["date"] del df_input["flag"] fig, (ax0, ax1) = plt.subplots(2, 1, sharey="all") ax0.set_title('BEFORE /20130201/non_ts.csv remove extreme value') df_input.plot(ax=ax0) desc = df_input.describe() mean_add_3std = desc.loc['mean'] + desc.loc['std'] * 3 mean_minus_3std = desc.loc['mean'] - desc.loc['std'] * 3 df_input = df_input.where(df_input < mean_add_3std, mean_add_3std, axis=1) df_input = df_input.where(df_input > mean_minus_3std, mean_minus_3std, axis=1) ax1.set_title('AFTER /20130201/non_ts.csv remove extreme value') df_input.plot(ax=ax1) plt.show() else: desc = df_input.describe() mean_add_3std = desc.loc['mean'] + desc.loc['std'] * 3 mean_minus_3std = desc.loc['mean'] - desc.loc['std'] * 3 df_input = df_input.where(df_input < mean_add_3std, mean_add_3std, axis=1) df_input = df_input.where(df_input > mean_minus_3std, mean_minus_3std, axis=1) return df_input
def run(): plt_df=pd.DataFrame(index=pd.to_datetime(context.date_range),columns=['value']) init_value=context.cash initialize(context) last_prize={} for dtt in context.date_range: # context.dt=dateutil.parser.parse(dtt) dtt=pd.to_datetime(dtt) context.dt=dtt handle_data(context) value=context.cash for stock in context.positions: #考虑到停牌的情况 today_data=get_today_data(stock) if len(today_data)==0: p=last_prize[stock] else: p=get_today_data(stock)['open'] last_prize[stock]=p value += p*context.positions[stock] plt_df.loc[dtt,'value']=value #这个是最终的持仓市值 #收益率 plt_df['ratio']=(plt_df['value']-init_value)/init_value #benchmark bm_df=attribute_daterange_history(context.benchmark,context.start_date,context.end_date) bm_init=bm_df['open'][0] plt_df['benchmark_ratio']=(bm_df['open']-bm_init)/bm_init plt_df[['ratio','benchmark_ratio']].plot() plt.show() print(plt_df['ratio']) print(plt_df)
def test1(self): partition_file = '/home/zy/workspace/viscojapan/tests/share/deformation_partition.h5' res_file = '/home/zy/workspace/viscojapan/tests/share/nrough_05_naslip_11.h5' plotter = vj.inv.PredictedTimeSeriesPlotter( partition_file = partition_file, #result_file = res_file, ) site = 'J550' cmpt = 'e' #plotter.plot_cumu_disp_pred(site, cmpt) #plotter.plot_cumu_disp_pred_added(site, cmpt, color='blue') # plotter.plot_post_disp_pred_added(site, cmpt) #plotter.plot_cumu_obs_linres(site, cmpt) #plotter.plot_R_co(site, cmpt) # plotter.plot_post_disp_pred(site, cmpt) # plotter.plot_post_obs_linres(site, cmpt) #plotter.plot_E_cumu_slip(site, cmpt) # plotter.plot_E_aslip(site, cmpt) #plotter.plot_R_aslip(site, cmpt) #plt.show() #plt.close() # plotter.plot_cumu_disp_decomposition(site, cmpt) plt.show() plt.close() plotter.plot_post_disp_decomposition(site, cmpt) plt.show() plt.close()
def visual_results(Image_data, preds, Labels=None, Top=0): from pylab import plt pred_age_value = preds['age'] pred_gender_value = preds['gender'] pred_smile_value = preds['smile'] pred_glass_value = preds['glass'] Num = Image_data.shape[0] if Top == 0 else Top for k in xrange(Num): print k, Num plt.figure(1) plt.imshow(de_preprocess_image(Image_data[k])) title_str = 'Prediction: Age %0.1f, %s, %s, %s.' % ( pred_age_value[k], gender_list[pred_gender_value[k]], glass_list[pred_glass_value[k]], smile_list[pred_smile_value[k]]) x_label_str = 'GT: ' try: x_label_str = x_label_str + 'Age %0.1f' % Labels['age'][k] except: pass try: x_label_str = x_label_str + '%s, %s, %s' % (gender_list[int( Labels['gender'][k])], glass_list[int( Labels['glass'][k])], smile_list[int(Labels['smile'][k])]) except: pass plt.title(title_str) plt.xlabel(x_label_str) plt.show()
def draw(cls, t_max, agents_proportions, eco_idx, parameters): color_set = ["green", "blue", "red"] for agent_type in range(3): plt.plot(np.arange(t_max), agents_proportions[:, agent_type], color=color_set[agent_type], linewidth=2.0, label="Type-{} agents".format(agent_type)) plt.ylim([-0.1, 1.1]) plt.xlabel("$t$") plt.ylabel("Proportion of indirect exchanges") # plt.suptitle('Direct choices proportion per type of agents', fontsize=14, fontweight='bold') plt.legend(loc='upper right', fontsize=12) print(parameters) plt.title( "Workforce: {}, {}, {}; displacement area: {}; vision area: {}; alpha: {}; tau: {}\n" .format( parameters["x0"], parameters["x1"], parameters["x2"], parameters["movement_area"], parameters["vision_area"], parameters["alpha"], parameters["tau"] ), fontsize=12) if not path.exists("../../figures"): mkdir("../../figures") plt.savefig("../../figures/figure_{}.pdf".format(eco_idx)) plt.show()
def draw(cls, t_max, agents_proportions, eco_idx, parameters): color_set = ["green", "blue", "red"] for agent_type in range(3): plt.plot(np.arange(t_max), agents_proportions[:, agent_type], color=color_set[agent_type], linewidth=2.0, label="Type-{} agents".format(agent_type)) plt.ylim([-0.1, 1.1]) plt.xlabel("$t$") plt.ylabel("Proportion of indirect exchanges") # plt.suptitle('Direct choices proportion per type of agents', fontsize=14, fontweight='bold') plt.legend(loc='upper right', fontsize=12) print(parameters) plt.title( "Workforce: {}, {}, {}; displacement area: {}; vision area: {}; alpha: {}; tau: {}\n" .format(parameters["x0"], parameters["x1"], parameters["x2"], parameters["movement_area"], parameters["vision_area"], parameters["alpha"], parameters["tau"]), fontsize=12) if not path.exists("../../figures"): mkdir("../../figures") plt.savefig("../../figures/figure_{}.pdf".format(eco_idx)) plt.show()
def example_filterbank(): from pylab import plt import numpy as np x = _create_impulse(2000) gfb = GammatoneFilterbank(density=1) analyse = gfb.analyze(x) imax, slopes = gfb.estimate_max_indices_and_slopes() fig, axs = plt.subplots(len(gfb.centerfrequencies), 1) for (band, state), imx, ax in zip(analyse, imax, axs): ax.plot(np.real(band)) ax.plot(np.imag(band)) ax.plot(np.abs(band)) ax.plot(imx, 0, 'o') ax.set_yticklabels([]) [ax.set_xticklabels([]) for ax in axs[:-1]] axs[0].set_title('Impulse responses of gammatone bands') fig, ax = plt.subplots() def plotfun(x, y): ax.semilogx(x, 20 * np.log10(np.abs(y)**2)) gfb.freqz(nfft=2 * 4096, plotfun=plotfun) plt.grid(True) plt.title('Absolute spectra of gammatone bands.') plt.xlabel('Normalized Frequency (log)') plt.ylabel('Attenuation /dB(FS)') plt.axis('Tight') plt.ylim([-90, 1]) plt.show() return gfb
def plot_zipf(*freq): ''' basic plotting using matplotlib and pylab ''' ranks, frequencies = [], [] langs, colors = [], [] langs = ["English", "German", "Finnish"] colors = ['#FF0000', '#00FF00', '#0000FF'] if bonus_part: colors.extend(['#00FFFF', '#FF00FF', '#FFFF00']) langs.extend(["English (Stemmed)", "German (Stemmed)", "Finnish (Stemmed)"]) plt.subplot(111) # 1, 1, 1 num = 6 if bonus_part else 3 for i in xrange(num): ranks.append(range(1, len(freq[i]) + 1)) frequencies.append([e[1] for e in freq[i]]) # log x and y axi, both with base 10 plt.loglog(ranks[i], frequencies[i], marker='', basex=10, color=colors[i], label=langs[i]) plt.legend() plt.grid(True) plt.title("Zipf's law!") plt.xlabel('Rank') plt.ylabel('Frequency') plt.show()
def test_run(self): # 生成几何布朗运动市场环境 me_gbm = MarketEnvironment('me_gbm', dt.datetime(2020, 1, 1)) me_gbm.add_constant('initial_value', 36.0) me_gbm.add_constant('volatility', 0.2) me_gbm.add_constant('final_date', dt.datetime(2020, 12, 31)) me_gbm.add_constant('currency', 'EUR') me_gbm.add_constant('frequency', 'M') me_gbm.add_constant('paths', 1000) csr = ConstantShortRate('csr', 0.06) me_gbm.add_curve('discount_curve', csr) # 生成几何布朗运动模拟类 gbm = GeometricBrownianMotion('gbm', me_gbm) gbm.generate_time_grid() # 生成跳跃扩散市场环境 me_srd = MarketEnvironment('me_srd', dt.datetime(2020, 1, 1)) me_srd.add_constant('initial_value', .25) me_srd.add_constant('volatility', 0.05) me_srd.add_constant('final_date', dt.datetime(2020, 12, 31)) me_srd.add_constant('currency', 'EUR') me_srd.add_constant('frequency', 'W') me_srd.add_constant('paths', 10000) # specific to simualation class me_srd.add_constant('kappa', 4.0) me_srd.add_constant('theta', 0.2) me_srd.add_curve('discount_curve', ConstantShortRate('r', 0.0)) srd = SquareRootDiffusion('srd', me_srd) srd_paths = srd.get_instrument_values()[:, :10] plt.figure(figsize=(10, 6)) plt.plot(srd.time_grid, srd.get_instrument_values()[:, :10]) plt.axhline(me_srd.get_constant('theta'), color='r', ls='--', lw=2.0) plt.xticks(rotation=30) plt.show()
def test1(self): partition_file = '/home/zy/workspace/viscojapan/tests/share/deformation_partition.h5' res_file = '/home/zy/workspace/viscojapan/tests/share/nrough_05_naslip_11.h5' plotter = vj.inv.PredictedTimeSeriesPlotter( partition_file=partition_file, #result_file = res_file, ) site = 'J550' cmpt = 'e' #plotter.plot_cumu_disp_pred(site, cmpt) #plotter.plot_cumu_disp_pred_added(site, cmpt, color='blue') # plotter.plot_post_disp_pred_added(site, cmpt) #plotter.plot_cumu_obs_linres(site, cmpt) #plotter.plot_R_co(site, cmpt) # plotter.plot_post_disp_pred(site, cmpt) # plotter.plot_post_obs_linres(site, cmpt) #plotter.plot_E_cumu_slip(site, cmpt) # plotter.plot_E_aslip(site, cmpt) #plotter.plot_R_aslip(site, cmpt) #plt.show() #plt.close() # plotter.plot_cumu_disp_decomposition(site, cmpt) plt.show() plt.close() plotter.plot_post_disp_decomposition(site, cmpt) plt.show() plt.close()
def pure_data_plot(self,connect=False,suffix='',cmap=cm.jet,bg=cm.bone(0.3)): #fig=plt.figure() ax=plt.axes() plt.axhline(y=0,color='grey', zorder=-1) plt.axvline(x=0,color='grey', zorder=-2) if cmap is None: if connect: ax.plot(self.x,self.y, 'b-',lw=2,alpha=0.5) ax.scatter(self.x,self.y, marker='o', c='b', s=40) else: if connect: if cmap in [cm.jet,cm.brg]: ax.plot(self.x,self.y, 'c-',lw=2,alpha=0.5,zorder=-1) else: ax.plot(self.x,self.y, 'b-',lw=2,alpha=0.5) c=[cmap((f-self.f[0])/(self.f[-1]-self.f[0])) for f in self.f] #c=self.f ax.scatter(self.x, self.y, marker='o', c=c, edgecolors=c, zorder=True, s=40) #, cmap=cmap) #plt.axis('equal') ax.set_xlim(xmin=-0.2*amax(self.x), xmax=1.2*amax(self.x)) ax.set_aspect('equal') #, 'datalim') if cmap in [cm.jet,cm.brg]: ax.set_axis_bgcolor(bg) if self.ZorY == 'Z': plt.xlabel(r'resistance $R$ in Ohm'); plt.ylabel(r'reactance $X$ in Ohm') if self.ZorY == 'Y': plt.xlabel(r'conductance $G$ in Siemens'); plt.ylabel(r'susceptance $B$ in Siemens') if self.show: plt.show() else: plt.savefig(join(self.sdc.plotpath,'c{}_{}_circle_data'.format(self.sdc.case,self.ZorY)+self.sdc.suffix+self.sdc.outsuffix+suffix+'.png'), dpi=240) plt.close()
def main(data_folder="../data", figure_folder="../figures"): deviation_from_equilibrium = dict() for i in [(1, 0), (2, 0), (2, 1)]: deviation_from_equilibrium[i] = \ json.load(open("{}/deviation_from_equilibrium_{}.json".format(data_folder, i), mode="r")) x = np.arange(len(list(deviation_from_equilibrium.values())[0])) fig, ax = plt.subplots() for i in deviation_from_equilibrium.keys(): ax.plot(x, deviation_from_equilibrium[i], label='{} against {}'.format(i[0], i[1])) ax.legend(fontsize=12) # loc='upper center ax.set_xlabel("generation") ax.set_ylabel("actual price / equilibrium price") ax.set_title( "Price Dynamics in Scarf Three-good Economy \n(relative deviation from equilibrium prices)" ) if not path.exists(figure_folder): mkdir(figure_folder) plt.savefig("{}/figure.pdf".format(figure_folder)) plt.show()
def test_run(self): plt.style.use('seaborn') mpl.rcParams['font.family'] = 'serif' # 生成市场环境 me_gbm = MarketEnvironment('me_gbm', dt.datetime(2020, 1, 1)) me_gbm.add_constant('initial_value', 36.0) me_gbm.add_constant('volatility', 0.2) me_gbm.add_constant('final_date', dt.datetime(2020, 12, 31)) me_gbm.add_constant('currency', 'EUR') me_gbm.add_constant('frequency', 'M') me_gbm.add_constant('paths', 1000) csr = ConstantShortRate('csr', 0.06) me_gbm.add_curve('discount_curve', csr) # 生成几何布朗运动模拟类 gbm = GeometricBrownianMotion('gbm', me_gbm) gbm.generate_time_grid() print('时间节点:{0};'.format(gbm.time_grid)) paths_1 = gbm.get_instrument_values() print('paths_1: {0};'.format(paths_1.round(3))) gbm.update(volatility=0.5) paths_2 = gbm.get_instrument_values() # 可视化结果 plt.figure(figsize=(10, 6)) p1 = plt.plot(gbm.time_grid, paths_1[:, :10], 'b') p2 = plt.plot(gbm.time_grid, paths_2[:, :10], 'r-') legend1 = plt.legend([p1[0], p2[0]], ['low volatility', 'high volatility'], loc=2) plt.gca().add_artist(legend1) plt.xticks(rotation=30) plt.show()
def run(self): plt.xticks([]), plt.yticks([]) if self.mode == Mode.DISPLAY: self.animation = matplotlib.animation.FuncAnimation(self.fig, self.time_step, interval=60) elif self.mode == Mode.ON_KEY_PRESS: self.fig.canvas.mpl_connect('key_press_event', self.time_step) else: print("Creating video! Could need some time to complete!") ffmpeg_writer = matplotlib.animation.writers['ffmpeg'] metadata = dict(title=self.video_name.split(".")[0], artist='Matplotlib', comment='') writer = ffmpeg_writer(fps=15, metadata=metadata) n_frames = len(self.data) with writer.saving(self.fig, self.video_name, n_frames): for i in range(n_frames): self.time_step() writer.grab_frame() if self.mode != Mode.SAVE: plt.show()
def plot_comfort(fingers_org=range(1, 6, 1), fingers_dst=range(1, 6, 1), jumps=range(-12, 13, 1)): import seaborn from mpl_toolkits.mplot3d import Axes3D from pylab import plt xs, ys, zs, cs = calculate_comforts(fingers_org, fingers_dst, jumps) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xs, ys, zs, c=cs) ax.set_zlabel("Interval (half steps)", fontsize=15) ax.set_zlim(jumps[0], jumps[-1]) # ax.set_zticks(jumps) plt.xticks(fingers_org) plt.xlim(fingers_org[0], fingers_org[-1]) plt.xlabel("From finger", fontsize=15) plt.yticks(fingers_dst) plt.ylim(fingers_dst[0], fingers_dst[-1]) plt.ylabel("To finger", fontsize=15) plt.title("Difficulty of finger passages", fontsize=25) plt.savefig('./figures/image.png', figsize=(16, 12), dpi=300) plt.show()
def ramd(): sample_size = 500 rn1 = npr.rand(sample_size, 3) rn2 = npr.randint(0, 10, sample_size) rn3 = npr.sample(size=sample_size) a = [0, 25, 50, 75, 100] rn4 = npr.choice(a, size=sample_size) fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(10, 8)) ax1.hist(rn1, bins=25, stacked=True) ax1.set_title('rand') ax1.set_ylabel('frequency') ax2.hist(rn2, bins=25) ax2.set_title('randint') ax3.hist(rn3, bins=25) ax3.set_title('samople') ax3.set_ylabel("frequency") ax4.hist(rn4, bins=25) ax4.set_title('choice') plt.show()
def square_root_diffusion_euler(): x0 = 0.25 kappa = 3.0 theta = 0.15 sigma = 0.1 I = 10000 M = 50 dt = T / M xh = np.zeros((M + 1, I)) x = np.zeros_like(xh) xh[0] = x0 x[0] = x0 for t in range(1, M + 1): xh[t] = (xh[t - 1] + kappa * (theta - np.maximum(xh[t - 1], 0)) * dt + sigma * np.sqrt(np.maximum(xh[t - 1], 0)) * math.sqrt(dt) * npr.standard_normal(I)) x = np.maximum(xh, 0) plt.figure(figsize=(10, 6)) plt.hist(x[-1], bins=50) plt.xlabel('value(SRT(T)') plt.ylabel('frequency') plt.show() plt.figure(figsize=(10, 6)) plt.plot(x[:, :100], lw=1.5) plt.xlabel('time') plt.ylabel('index level') plt.show() return x
def jump_diffusion(): S0 = 100.0 r = 0.05 sigma = 0.2 lamb = 0.05 mu = -0.6 delta = 0.25 rj = lamb * (math.exp(mu + 0.5 * delta**2) - 1) T = 1.0 M = 50 I = 10000 dt = T / M S = np.zeros((M + 1, I)) S[0] = S0 sn1 = npr.standard_normal((M + 1, I)) sn2 = npr.standard_normal((M + 1, I)) poi = npr.poisson(lamb * dt, (M + 1, I)) for t in range(1, M + 1, 1): S[t] = S[t - 1] * (np.exp( (r - rj - 0.5 * sigma**2) * dt + sigma * math.sqrt(dt) * sn1[t]) + (np.exp(mu + delta * sn2[t]) - 1) * poi[t]) S[t] = np.maximum(S[t], 0) plt.figure(figsize=(10, 6)) plt.hist(S[-1], bins=50) plt.xlabel('value') plt.ylabel('frequency') plt.show() plt.figure(figsize=(10, 6)) plt.plot(S[:, :100], lw=1.) plt.xlabel('time') plt.ylabel('index level') plt.show()
def cumulative_freq_plot(rast, band=0, mask=None, bins=100, xlim=None, nodata=-9999): ''' Plots an empirical cumulative frequency curve for the input raster array in a given band. ''' if mask is not None: arr = binary_mask(rast, mask) else: arr = rast.copy() if nodata is not None: arr = subarray(arr) values, base = np.histogram(arr, bins=bins) cumulative = np.cumsum(values) # Evaluate the cumulative distribution plt.plot(base[:-1], cumulative, c='blue') # Plot the cumulative function plt.set_title('Empirical Cumulative Distribution: Band %d' % band) if xlim is not None: axes = plt.gca() axes.set_xlim(xlim) plt.show() return arr
def test_screenstate_1(self): from gdesk import gui from pylab import plt from pathlib import Path gui.load_layout('console') samplePath = Path(r'./samples') gui.img.select(1) gui.img.open(samplePath / 'kodim05.png') gui.img.zoom_fit() plt.plot(gui.vs.mean(2).mean(1)) plt.title('Column means of image 1') plt.xlabel('Column Number') plt.ylabel('Mean') plt.grid() plt.show() gui.img.select(2) gui.img.open(samplePath / 'kodim23.png') gui.img.zoom_full() plt.figure() plt.plot(gui.vs.mean(2).mean(0)) plt.title('Row means of image 2') plt.xlabel('Row Number') plt.ylabel('Mean') plt.grid() plt.show()
def hexbin_plot(self, var1, var2): print("Doing hexbin plot '{}' against '{}'.".format(var2, var1)) x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) plt.xlim(self.range_var[var1]) plt.ylim(self.range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) hb = ax.hexbin(x=x, y=y, gridsize=20, cmap='inferno') ax.set_facecolor('black') cb = fig.colorbar(hb, ax=ax) cb.set_label('counts') plt.savefig("{}/hexbin_{}_{}.pdf".format(self.fig_folder, var1, var2)) if self.display: plt.show() plt.close()
def curve_plot(self, variable, t_max): print("Doing curve plot for variable '{}'.".format(variable)) var = Variable(name=variable) if var.data is None: self.extract_single_dimension(var, t_max=t_max) x = np.arange(t_max) mean = var.data["mean"] std = var.data["std"] plt.plot(x, mean, c='black', lw=2) plt.plot(x, mean + std, c='black', lw=.1) plt.plot(x, mean - std, c='black', lw=.1) plt.fill_between(x, mean + std, mean - std, color='black', alpha=.1) plt.xlabel("t") plt.ylabel(self.format_label(variable)) plt.savefig("{}/curve_plot_{}.pdf".format(self.fig_folder, variable)) if self.display: plt.show() plt.close()
def example_filterbank(): from pylab import plt import numpy as np x = _create_impulse(2000) gfb = GammatoneFilterbank(density=1) analyse = gfb.analyze(x) imax, slopes = gfb.estimate_max_indices_and_slopes() fig, axs = plt.subplots(len(gfb.centerfrequencies), 1) for (band, state), imx, ax in zip(analyse, imax, axs): ax.plot(np.real(band)) ax.plot(np.imag(band)) ax.plot(np.abs(band)) ax.plot(imx, 0, 'o') ax.set_yticklabels([]) [ax.set_xticklabels([]) for ax in axs[:-1]] axs[0].set_title('Impulse responses of gammatone bands') fig, ax = plt.subplots() def plotfun(x, y): ax.semilogx(x, 20*np.log10(np.abs(y)**2)) gfb.freqz(nfft=2*4096, plotfun=plotfun) plt.grid(True) plt.title('Absolute spectra of gammatone bands.') plt.xlabel('Normalized Frequency (log)') plt.ylabel('Attenuation /dB(FS)') plt.axis('Tight') plt.ylim([-90, 1]) plt.show() return gfb
def graph_error_mean_per_hour(dataset, pred, column): data = dataset.iloc[dataset.shape[0] - len(pred):] data["Pred"] = np.around(pred, 5) if column == "Diff": res = np.delete(data["Close"].to_numpy(), -1) res = np.insert(res, 0, 0) data["Pred"] = data["Pred"] + res data["AEM"] = np.abs(np.around(data.Close - data.Pred, 5)) data['Hour'] = data.Date.apply(lambda x: x.hour) data["Diff"] = data.Close.diff().apply(abs).fillna(0) plt.figure(figsize=(14, 6)) plt.hist(data.where((data.Hour > 5) & (data.Hour < 20)).dropna()["AEM"], 150, density=True, range=(0, 0.003)) plt.show() diff_mean = data.groupby("Hour").mean().Diff x_ch = diff_mean.index y_ch = diff_mean diff_mean_aem = data.groupby("Hour").mean().AEM x = diff_mean_aem.index y = diff_mean_aem plt.figure(figsize=(14, 6)) plt.bar(x_ch, y_ch, color="green") plt.bar(x, y, color="r", alpha=0.7) plt.title( "Absolute mean of error in predictions per hour compared to absolute mean of price changes", fontsize=16) plt.show()
def imshow(self, name): ''' 显示灰度图 ''' img = self.buffer2img(name) plt.imshow(img, cmap='gray') plt.axis('off') plt.show()
def plot_charts(self): print(self.final_portfolio_valuation) plt.figure(figsize=(10, 6)) plt.hist( self.final_portfolio_valuation, bins=100) plt.title("Final Exit Valuation complete Portfolio after {} year as Geometric Brownian Motion".format(self.max_year)) plt.xlabel('Exit Valuation') plt.ylabel('frequency'); plt.show()
def create_plot(x, y, styles, labels, axlabels): plt.figure(figsize=(10, 6)) plt.scatter(x[0], y[0]) plt.scatter(x[1], y[1]) plt.xlabel(axlabels[0]) plt.ylabel(axlabels[1]) plt.legend(loc=0) plt.show()
def close(self): """Does nothing.""" plt.show() self.data = [] self.axis = [] self.count = [] self.initial = [] self.scaleList = [] return
def main_single(): with open("parameters/single.json", "r") as f: parameters = json.load(f) r = model.run.run(parameters) plt.plot(r.indirect_exchanges) plt.show()
def plot_post(cfs,ifshow=False,loc=2, save_fig_path = None, file_type='png'): for cf in cfs: plot_cf(cf, color='blue') plt.legend(loc=loc) if ifshow: plt.show() if save_fig_path is not None: plt.savefig(join(save_fig_path, '%s_%s.%s'%(cf.SITE, cf.CMPT, file_type))) plt.close()
def plot(cls, data, msg=""): x = np.arange(len(data[:])) plt.plot(x, data[:, 0], c="red", linewidth=2) plt.plot(x, data[:, 1], c="blue", linewidth=2) plt.plot(x, data[:, 2], c="green", linewidth=2) plt.ylim([-0.01, 1.01]) plt.text(0, -0.12, "{}".format(msg)) plt.show()
def test1(self): partition_file = '/home/zy/workspace/viscojapan/tests/share/deformation_partition.h5' plotter = vj.inv.PredictedVelocityTimeSeriesPlotter( partition_file = partition_file ) site = 'J550' cmpt = 'e' plotter.plot_vel_decomposition(site, cmpt) plt.show() plt.close()
def plot_smoothed_alpha_comparison(self,rmsval,suffix=''): plt.plot(self.f,self.alpha,'ko',label='data set') plt.plot(self.f,self.salpha,'c-',lw=2,label='smoothed angle $\phi$') plt.xlabel('frequency in Hz') plt.ylabel('angle $\phi$ in coordinates of circle') plt.legend() ylims=plt.axes().get_ylim() plt.yticks((arange(9)-4)*0.5*pi, ['$-2\pi$','$-3\pi/2$','$-\pi$','$-\pi/2$','$0$','$\pi/2$','$\pi$','$3\pi/2$','$2\pi$']) plt.ylim(ylims) plt.title('RMS offset from smooth curve: {:.4f}'.format(rmsval)) if self.show: plt.show() else: plt.savefig(join(self.sdc.plotpath,'salpha','c{}_salpha_on_{}_circle'.format(self.sdc.case,self.ZorY)+self.sdc.suffix+self.sdc.outsuffix+suffix+'.png'), dpi=240) plt.close()
def main(): r = Route('data/libs.csv') r.draw() for i in range(10000000): if not r.simulate(): break if i % 1000 == 0: r.draw() #sleep(0.001) raw_input("Press Enter to continue...") plt.show()
def plot_overview_B(self,suffix='',ansize=8,anspread=0.15,anmode='quarters',datbg=True,datbgsource=None,checkring=False): self.start_plot() if datbg: # data background desired self.plot_bg_data(datbgsource=datbgsource) #self.plot_data() self.plot_fitcircle() if checkring: self.plot_checkring() idxlist=self.to_be_annotated(anmode) self.annotate_data_points(idxlist,ansize,anspread) self.plot_characteristic_freqs(annotate=True,size=ansize,spread=anspread) if self.show: plt.show() else: plt.savefig(join(self.sdc.plotpath,'c{}_fitted_{}_circle'.format(self.sdc.case,self.ZorY)+self.sdc.suffix+self.sdc.outsuffix+suffix+'.png'), dpi=240) plt.close()
def drawAdoptionNetworkMPL(G, fnum=1, show=False, writeFile=None): """Draws the network to matplotlib, coloring the nodes based on adoption. Looks for the node attribute 'adopted'. If the attribute is True, colors the node a different color, showing adoption visually. This function assumes that the node attributes have been pre-populated. :param networkx.Graph G: Any NetworkX Graph object. :param int fnum: The matplotlib figure number. Defaults to 1. :param bool show: :param str writeFile: A filename/path to save the figure image. If not specified, no output file is written. """ Gclean = G.subgraph([n for n in G.nodes() if n not in nx.isolates(G)]) plt.figure(num=fnum, figsize=(6,6)) # clear figure plt.clf() # Blue ('b') node color for adopters, red ('r') for non-adopters. nodecolors = ['b' if Gclean.node[n]['adopted'] else 'r' \ for n in Gclean.nodes()] layout = nx.spring_layout(Gclean) nx.draw_networkx_nodes(Gclean, layout, node_size=80, nodelist=Gclean.nodes(), node_color=nodecolors) nx.draw_networkx_edges(Gclean, layout, alpha=0.5) # width=4 # TODO: Draw labels of Ii values. Maybe vary size of node. # TODO: Color edges blue based on influences from neighbors influenceEdges = [] for a in Gclean.nodes(): for n in Gclean.node[a]['influence']: influenceEdges.append((a,n)) if len(influenceEdges)>0: nx.draw_networkx_edges(Gclean, layout, alpha=0.5, width=5, edgelist=influenceEdges, edge_color=['b']*len(influenceEdges)) #some extra space around figure plt.xlim(-0.05,1.05) plt.ylim(-0.05,1.05) plt.axis('off') if writeFile != None: plt.savefig(writeFile) if show: plt.show()
def plot_overview(self,suffix=''): x=self.x; y=self.y; r=self.radius; cx,cy=self.center.real,self.center.imag ax=plt.axes() plt.scatter(x,y, marker='o', c='b', s=40) plt.axhline(y=0,color='grey', zorder=-1) plt.axvline(x=0,color='grey', zorder=-2) t=linspace(0,2*pi,201) circx=r*cos(t) + cx circy=r*sin(t) + cy plt.plot(circx,circy,'g-') plt.plot([cx],[cy],'gx',ms=12) if self.ZorY == 'Z': philist,flist=[self.phi_a,self.phi_p,self.phi_n],[self.fa,self.fp,self.fn] elif self.ZorY == 'Y': philist,flist=[self.phi_m,self.phi_s,self.phi_r],[self.fm,self.fs,self.fr] for p,f in zip(philist,flist): if f is not None: xpos=cx+r*cos(p); ypos=cy+r*sin(p); xos=0.2*(xpos-cx); yos=0.2*(ypos-cy) plt.plot([0,xpos],[0,ypos],'co-') ax.annotate('{:.3f} Hz'.format(f), xy=(xpos,ypos), xycoords='data', xytext=(xpos+xos,ypos+yos), textcoords='data', #textcoords='offset points', arrowprops=dict(arrowstyle="->", shrinkA=0, shrinkB=10) ) #plt.xlim(0,0.16) #plt.ylim(-0.1,0.1) plt.axis('equal') if self.ZorY == 'Z': plt.xlabel(r'resistance $R$ in Ohm'); plt.ylabel(r'reactance $X$ in Ohm') if self.ZorY == 'Y': plt.xlabel(r'conductance $G$ in Siemens'); plt.ylabel(r'susceptance $B$ in Siemens') plt.title("fitting the admittance circle with Powell's method") tx1='best fit (fmin_powell):\n' tx1+='center at G+iB = {:.5f} + i*{:.8f}\n'.format(cx,cy) tx1+='radius = {:.5f}; '.format(r) tx1+='residue: {:.2e}'.format(self.resid) txt1=plt.text(-r,cy-1.1*r,tx1,fontsize=8,ha='left',va='top') txt1.set_bbox(dict(facecolor='gray', alpha=0.25)) idxlist=self.to_be_annotated('triple') ofs=self.annotation_offsets(idxlist,factor=0.1,xshift=0.15) for i,j in enumerate(idxlist): xpos,ypos = x[j],y[j]; xos,yos = ofs[i].real,ofs[i].imag ax.annotate('{:.1f} Hz'.format(self.f[j]), xy=(xpos,ypos), xycoords='data', xytext=(xpos+xos,ypos+yos), textcoords='data', #textcoords='offset points', arrowprops=dict(arrowstyle="->", shrinkA=0, shrinkB=10) ) if self.show: plt.show() else: plt.savefig(join(self.sdc.plotpath,'c{}_fitted_{}_circle'.format(self.sdc.case,self.ZorY)+suffix+'.png'), dpi=240) plt.close()
def plot(*args): """ Recibe señales que se desee graficar Y muestra todas ellas en un mismo gráfico """ import numpy as np from pylab import plt plotargs = [] for arg in args: x = np.arange(0, len(arg)) y = np.array([int(i) for i in arg]) plotargs.extend([x, y]) plt.plot(*plotargs) plt.show()
def my_2D_plot_of_arrays(xa, ya, title, xlabel, ylabel, *add_graphs): fig = plt.figure() ax = fig.add_subplot(111) lines=ax.plot(xa, ya, 'b-o', markersize=2, color="green") line = lines[0] line.set_linewidth( 1.5 ) line.set_color( 'green' ) line.set_linestyle( '-' ) line.set_marker('s') line.set_markerfacecolor('red') line.set_markeredgecolor( '0.1' ) line.set_markersize( 3 ) ## format the ticks adl = mdates.AutoDateLocator() ax.xaxis.set_major_locator(adl) myformatter = MyAutoDateFormatter(adl) #myformatter = mdates.AutoDateFormatter(adl) ax.xaxis.set_major_formatter(myformatter) ax.set_title(title) ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) labels = getp(ax, 'xticklabels') setp(labels, color='g', fontsize=9, fontname="Verdana" ) labels = getp(ax, 'yticklabels') setp(labels, color='g', fontsize=9, fontname="Verdana" ) ax.grid(True) fig.autofmt_xdate(bottom=0.18, rotation=60) min_y=min(ya) max_y=max(ya) if (min_y<0) and (max_y >0): ax.axhline(0, color='r', lw=4, alpha=0.5) ax.fill_between(xa, ya, facecolor='red', alpha=0.5, interpolate=True) #plot additional graphs if len(add_graphs): draw_add_2D_plot_of_arrays(add_graphs, ax) plt.show()
def plot_mat(data): # Three subplots sharing both x/y axes f, axarray = plt.subplots(16, sharex=True, sharey=True) for i, row in enumerate(data): axarray[i].plot(range(len(row)), row, 'g') # Fine-tune figure; make subplots close to each other and hide x ticks for # all but bottom plot. f.subplots_adjust(hspace=0) plt.setp([a.get_xticklabels() for a in f.axes], visible=False) plt.setp([a.get_yticklabels() for a in f.axes], visible=False) axarray[0].set_title('10 minute EEG Reading for Patient') axarray[int(len(axarray) / 2)].set_ylabel('Magnitude') axarray[-1].set_xlabel('Time') font = {'family': 'normal', 'weight': 'bold', 'size': 48} plt.rc('font', **font) plt.show()
def convolve(arrays, melBank, genere, filter_idx): x = [] melBank_time = np.fft.ifft(melBank) #need to transform melBank to time domain for eachClip in arrays: result = np.convolve(eachClip, melBank_time) x.append(result) plotBeforeAfterFilter(eachClip, melBank, melBank_time, result, genere, filter_idx) m = np.asmatrix(np.array(x)) fig, ax = plt.subplots() ax.matshow(m.real) #each element has imaginary part. So just plot real part plt.axis('equal') plt.axis('tight') plt.title(genere) plt.tight_layout() # filename = "./figures/convolution/Convolution_"+"Filter"+str(filter_idx)+genere+".png" # plt.savefig(filename) plt.show()
def show_prediction_result(image, label_image, clf): size = (8, 8) plt.figure(figsize=(15, 10)) plt.imshow(image, cmap='gray_r') candidates = [] predictions = [] for region in regionprops(label_image): # skip small images # if region.area < 100: # continue # draw rectangle around segmented coins minr, minc, maxr, maxc = region.bbox # make regions square maxwidth = np.max([maxr - minr, maxc - minc]) minr, maxr = int(0.5 * ((maxr + minr) - maxwidth)) - 3, int(0.5 * ((maxr + minr) + maxwidth)) + 3 minc, maxc = int(0.5 * ((maxc + minc) - maxwidth)) - 3, int(0.5 * ((maxc + minc) + maxwidth)) + 3 rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr, fill=False, edgecolor='red', linewidth=2, alpha=0.2) plt.gca().add_patch(rect) # predict digit candidate = image[minr:maxr, minc:maxc] candidate = np.array(imresize(candidate, size), dtype=float) # invert # candidate = np.max(candidate) - candidate # print im # rescale to 16 in integer candidate = (candidate - np.min(candidate)) if np.max(candidate) == 0: continue candidate /= np.max(candidate) candidate[candidate < 0.2] = 0.0 candidate *= 16 candidate = np.array(candidate, dtype=int) prediction = clf.predict(candidate.reshape(-1)) candidates.append(candidate) predictions.append(prediction) plt.text(minc - 10, minr - 10, "{}".format(prediction), fontsize=50) plt.xticks([], []) plt.yticks([], []) plt.tight_layout() plt.show() return candidates, predictions
def example_gammatone_filter(): from pylab import plt, np sample_rate = 44100 order = 4 b, a = design_filter( sample_rate=sample_rate, order=order, centerfrequency=1000.0, attenuation_half_bandwidth_db=-3, band_width_factor=1.0) x = _create_impulse(1000) y, states = fosfilter(b, a, order, x) y = y[:800] plt.plot(np.real(y), label='Re(z)') plt.plot(np.imag(y), label='Im(z)') plt.plot(np.abs(y), label='|z|') plt.legend() plt.show() return y, b, a
def plot_earth_model_file_depth_change( earth_model_file, ofig_prefix, ofig_type, if_show = False): em = EarthModelFileReader(earth_model_file) den = em.density dep = em.dep_top _plot_base(dep, den,[300,0],[2.5, 3.6], r'density ($g/cm^3$)') plt.savefig('%s_density.%s'%(ofig_prefix, ofig_type)) if if_show: plt.show() plt.close() shear = em.shear/10**9 _plot_base(dep, shear,[300,0],[15, 110], r'shear modulus ($GPa$)') plt.savefig('%s_shear.%s'%(ofig_prefix, ofig_type)) if if_show: plt.show() plt.close() bulk = em.bulk/10**9 _plot_base(dep, bulk,[300,0],[40, 200], r'bulk modulus ($GPa$)') plt.savefig('%s_bulk.%s'%(ofig_prefix, ofig_type)) if if_show: plt.show() plt.close()
def show(filename=None, labels=False): if not labels: # fix everything if in 3D mode plt.subplots_adjust(left=0.0, right=1.1, bottom=0.0, top=1.0) # also do this if in 2d mode if not is_3d: frame1 = plt.gca() frame1.axes.get_xaxis().set_visible(False) frame1.axes.get_yaxis().set_visible(False) if legend: plt.legend(loc="upper left", fontsize=8, prop={'family': "Monaco", 'weight': "roman", 'size': "x-small"}) if filename is not None: if '.' not in filename: if not os.path.isdir(filename): os.makedirs(filename) filename = os.path.abspath(os.path.join(filename, "%s.png" % util.timestamp())) figure.savefig(filename, dpi=150, facecolor=figure.get_facecolor(), edgecolor='none') plt.show()
def plot(mot_results): bool_mot_results = np.asarray(mot_results) == 1 t_max = len(bool_mot_results) average_t = np.zeros(t_max) time_window = 10 for t in range(t_max): if t < time_window: average_t[t] = np.mean(bool_mot_results[:t+1]) else: average_t[t] = np.mean(bool_mot_results[t-10:t+1]) try: plt.plot(np.arange(t_max), average_t, linewidth=2) plt.ylim([-0.01, 1.01]) plt.show("figure.pdf") except: print("Could not show the figure but here are the results:") print(average_t)
import h5py from pylab import plt def collect_results(outs_files, key): outs = [] for file in outs_files: with h5py.File(file, 'r') as fid: out = fid[key][...] outs.append(out) return outs files = sorted(glob.glob('../outs/ano_??.h5')) nrough1 = collect_results(files, 'regularization/roughening/norm') nres1 = collect_results(files, 'misfit/norm_weighted') files = sorted(glob.glob('../../run0/outs/ano_??.h5')) nrough0 = collect_results(files, 'regularization/roughening/norm') nres0 = collect_results(files, 'misfit/norm_weighted') plt.loglog(nres0, nrough0, '.', label='Result0') plt.loglog(nres1, nrough1, '.', label='Result1') plt.grid('on') plt.xlabel('norm of weighted residual') plt.ylabel('norm of solution roughness') plt.xlim([.7,5]) plt.legend() plt.savefig('compare_misfit.png') plt.show()
def get_linear_model_histogramDouble(code, ptype='f', dtype='d', start=None, end=None, vtype='close', filter='n', df=None): # 399001','cyb':'zs399006','zxb':'zs399005 # code = '999999' # code = '601608' # code = '000002' # asset = ts.get_hist_data(code)['close'].sort_index(ascending=True) # df = tdd.get_tdx_Exp_day_to_df(code, 'f').sort_index(ascending=True) # vtype='close' # if vtype == 'close' or vtype=='' # ptype= if start is not None and filter == 'y': if code not in ['999999', '399006', '399001']: index_d, dl = tdd.get_duration_Index_date(dt=start) log.debug("index_d:%s dl:%s" % (str(index_d), dl)) else: index_d = cct.day8_to_day10(start) log.debug("index_d:%s" % (index_d)) start = tdd.get_duration_price_date(code, ptype='low', dt=index_d) log.debug("start:%s" % (start)) if df is None: # df = tdd.get_tdx_append_now_df(code, ptype, start, end).sort_index(ascending=True) df = tdd.get_tdx_append_now_df_api(code, ptype, start, end).sort_index(ascending=True) if not dtype == 'd': df = tdd.get_tdx_stock_period_to_type(df, dtype).sort_index(ascending=True) asset = df[vtype] log.info("df:%s" % asset[:1]) asset = asset.dropna() dates = asset.index if not code.startswith('999') or not code.startswith('399'): if code[:1] in ['5', '6', '9']: code2 = '999999' elif code[:1] in ['3']: code2 = '399006' else: code2 = '399001' df1 = tdd.get_tdx_append_now_df_api(code2, ptype, start, end).sort_index(ascending=True) # df1 = tdd.get_tdx_append_now_df(code2, ptype, start, end).sort_index(ascending=True) if not dtype == 'd': df1 = tdd.get_tdx_stock_period_to_type(df1, dtype).sort_index(ascending=True) asset1 = df1.loc[asset.index, vtype] startv = asset1[:1] # asset_v=asset[:1] # print startv,asset_v asset1 = asset1.apply(lambda x: round(x / asset1[:1], 2)) # print asset1[:4] # 画出价格随时间变化的图像 # _, ax = plt.subplots() # fig = plt.figure() fig = plt.figure(figsize=(16, 10)) # fig = plt.figure(figsize=(16, 10), dpi=72) # fig.autofmt_xdate() #(no fact) # plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9) plt.subplots_adjust(left=0.05, bottom=0.08, right=0.95, top=0.95, wspace=0.15, hspace=0.25) # set (gca,'Position',[0,0,512,512]) # fig.set_size_inches(18.5, 10.5) # fig=plt.fig(figsize=(14,8)) ax1 = fig.add_subplot(321) # asset=asset.apply(lambda x:round( x/asset[:1],2)) ax1.plot(asset) # ax1.plot(asset1,'-r', linewidth=2) ticks = ax1.get_xticks() # start, end = ax1.get_xlim() # print start, end, len(asset) # print ticks, ticks[:-1] # (ticks[:-1] if len(asset) > end else np.append(ticks[:-1], len(asset) - 1)) ax1.set_xticklabels([dates[i] for i in (np.append(ticks[:-1], len(asset) - 1))], rotation=15) # Label x-axis with dates # 拟合 X = np.arange(len(asset)) x = sm.add_constant(X) model = regression.linear_model.OLS(asset, x).fit() a = model.params[0] b = model.params[1] # log.info("a:%s b:%s" % (a, b)) log.info("X:%s a:%s b:%s" % (len(asset), a, b)) Y_hat = X * b + a # 真实值-拟合值,差值最大最小作为价值波动区间 # 向下平移 i = (asset.values.T - Y_hat).argmin() c_low = X[i] * b + a - asset.values[i] Y_hatlow = X * b + a - c_low # 向上平移 i = (asset.values.T - Y_hat).argmax() c_high = X[i] * b + a - asset.values[i] Y_hathigh = X * b + a - c_high plt.plot(X, Y_hat, 'k', alpha=0.9); plt.plot(X, Y_hatlow, 'r', alpha=0.9); plt.plot(X, Y_hathigh, 'r', alpha=0.9); # plt.xlabel('Date', fontsize=12) plt.ylabel('Price', fontsize=12) plt.title(code + " | " + str(dates[-1])[:11], fontsize=14) plt.legend([asset.iat[-1]], fontsize=12, loc=4) plt.grid(True) # plt.legend([code]); # plt.legend([code, 'Value center line', 'Value interval line']); # fig=plt.fig() # fig.figsize = [14,8] scale = 1.1 zp = zoompan.ZoomPan() figZoom = zp.zoom_factory(ax1, base_scale=scale) figPan = zp.pan_factory(ax1) ax2 = fig.add_subplot(323) # ax2.plot(asset) # ticks = ax2.get_xticks() ax2.set_xticklabels([dates[i] for i in (np.append(ticks[:-1], len(asset) - 1))], rotation=15) # plt.plot(X, Y_hat, 'k', alpha=0.9) n = 5 d = (-c_high + c_low) / n c = c_high while c <= c_low: Y = X * b + a - c plt.plot(X, Y, 'r', alpha=0.9); c = c + d # asset=asset.apply(lambda x:round(x/asset[:1],2)) ax2.plot(asset) # ax2.plot(asset1,'-r', linewidth=2) # plt.xlabel('Date', fontsize=12) plt.ylabel('Price', fontsize=12) plt.grid(True) # plt.title(code, fontsize=14) # plt.legend([code]) # 将Y-Y_hat股价偏离中枢线的距离单画出一张图显示,对其边界线之间的区域进行均分,大于0的区间为高估,小于0的区间为低估,0为价值中枢线。 ax3 = fig.add_subplot(322) # distance = (asset.values.T - Y_hat) distance = (asset.values.T - Y_hat)[0] if code.startswith('999') or code.startswith('399'): ax3.plot(asset) plt.plot(distance) ticks = ax3.get_xticks() ax3.set_xticklabels([dates[i] for i in (np.append(ticks[:-1], len(asset) - 1))], rotation=15) n = 5 d = (-c_high + c_low) / n c = c_high while c <= c_low: Y = X * b + a - c plt.plot(X, Y - Y_hat, 'r', alpha=0.9); c = c + d ax3.plot(asset) # plt.xlabel('Date', fontsize=12) plt.ylabel('Price-center price', fontsize=14) plt.grid(True) else: as3 = asset.apply(lambda x: round(x / asset[:1], 2)) ax3.plot(as3) ax3.plot(asset1, '-r', linewidth=2) plt.grid(True) zp3 = zoompan.ZoomPan() figZoom = zp3.zoom_factory(ax3, base_scale=scale) figPan = zp3.pan_factory(ax3) # plt.title(code, fontsize=14) # plt.legend([code]) # 统计出每个区域内各股价的频数,得到直方图,为了更精细的显示各个区域的频数,这里将整个边界区间分成100份。 ax4 = fig.add_subplot(325) log.info("assert:len:%s %s" % (len(asset.values.T - Y_hat), (asset.values.T - Y_hat)[0])) # distance = map(lambda x:int(x),(asset.values.T - Y_hat)/Y_hat*100) # now_distanse=int((asset.iat[-1]-Y_hat[-1])/Y_hat[-1]*100) # log.debug("dis:%s now:%s"%(distance[:2],now_distanse)) # log.debug("now_distanse:%s"%now_distanse) distance = (asset.values.T - Y_hat) now_distanse = asset.iat[-1] - Y_hat[-1] # distance = (asset.values.T-Y_hat)[0] pd.Series(distance).plot(kind='hist', stacked=True, bins=100) # plt.plot((asset.iat[-1].T-Y_hat),'b',alpha=0.9) plt.axvline(now_distanse, hold=None, label="1", color='red') # plt.axhline(now_distanse,hold=None,label="1",color='red') # plt.axvline(asset.iat[0],hold=None,label="1",color='red',linestyle="--") plt.xlabel('Undervalue ------------------------------------------> Overvalue', fontsize=12) plt.ylabel('Frequency', fontsize=14) # plt.title('Undervalue & Overvalue Statistical Chart', fontsize=14) plt.legend([code, asset.iat[-1], str(dates[-1])[5:11]], fontsize=12) plt.grid(True) # plt.show() # import os # print(os.path.abspath(os.path.curdir)) ax5 = fig.add_subplot(326) # fig.figsize=(5, 10) log.info("assert:len:%s %s" % (len(asset.values.T - Y_hat), (asset.values.T - Y_hat)[0])) # distance = map(lambda x:int(x),(asset.values.T - Y_hat)/Y_hat*100) distance = (asset.values.T - Y_hat) / Y_hat * 100 now_distanse = ((asset.iat[-1] - Y_hat[-1]) / Y_hat[-1] * 100) log.debug("dis:%s now:%s" % (distance[:2], now_distanse)) log.debug("now_distanse:%s" % now_distanse) # n, bins = np.histogram(distance, 50) # print n, bins[:2] pd.Series(distance).plot(kind='hist', stacked=True, bins=100) # plt.plot((asset.iat[-1].T-Y_hat),'b',alpha=0.9) plt.axvline(now_distanse, hold=None, label="1", color='red') # plt.axhline(now_distanse,hold=None,label="1",color='red') # plt.axvline(asset.iat[0],hold=None,label="1",color='red',linestyle="--") plt.xlabel('Undervalue ------------------------------------------> Overvalue', fontsize=14) plt.ylabel('Frequency', fontsize=12) # plt.title('Undervalue & Overvalue Statistical Chart', fontsize=14) plt.legend([code, asset.iat[-1]], fontsize=12) plt.grid(True) ax6 = fig.add_subplot(324) h = df.loc[:, ['open', 'close', 'high', 'low']] highp = h['high'].values lowp = h['low'].values openp = h['open'].values closep = h['close'].values lr = LinearRegression() x = np.atleast_2d(np.linspace(0, len(closep), len(closep))).T lr.fit(x, closep) LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False) xt = np.atleast_2d(np.linspace(0, len(closep) + 200, len(closep) + 200)).T yt = lr.predict(xt) bV = [] bP = [] for i in range(1, len(highp) - 1): if highp[i] <= highp[i - 1] and highp[i] < highp[i + 1] and lowp[i] <= lowp[i - 1] and lowp[i] < lowp[i + 1]: bV.append(lowp[i]) bP.append(i) d, p = LIS(bV) idx = [] for i in range(len(p)): idx.append(bP[p[i]]) lr = LinearRegression() X = np.atleast_2d(np.array(idx)).T Y = np.array(d) lr.fit(X, Y) estV = lr.predict(xt) ax6.plot(closep, linewidth=2) ax6.plot(idx, d, 'ko') ax6.plot(xt, estV, '-r', linewidth=3) ax6.plot(xt, yt, '-g', linewidth=3) plt.grid(True) # plt.tight_layout() zp2 = zoompan.ZoomPan() figZoom = zp2.zoom_factory(ax6, base_scale=scale) figPan = zp2.pan_factory(ax6) # plt.ion() plt.show(block=False)
def show_plot(self): plt.show()
if raw_input("Your LSM file appears to be newer than the dE solutions. Really update (y/n)? " ).lower()[:1] != "y": print "LSM update cancelled."; sys.exit(1); # update sources print "=== Updating model sources"; A = (dex**2+dey**2)/2; B = (dex**2-dey**2)/2; for isrc,name in enumerate(SRCS): for src in model.sources: if src.name.startswith(name): if hasattr(src.flux,'Q'): I,Q = src.flux.I,src.flux.Q; src.flux.I = A[isrc]*I+B[isrc]*Q; src.flux.Q = A[isrc]*Q+B[isrc]*I; print "%8s I=%f Q=%f --> I=%f Q=%f"%(src.name,I,Q,src.flux.I,src.flux.Q); if dlm_offsets is not None: ra,dec = src.pos.ra,src.pos.dec; l,m,n = Coordinates.radec_to_lmn(ra,dec,*radec0); l += dlm_offsets[isrc,0]; m += dlm_offsets[isrc,1]; src.pos.ra,src.pos.dec = Coordinates.lm_to_radec(l,m,*radec0); print "%8s position %.8f,%.8f --> %.8f %.8f"%(src.name,ra,dec,src.pos.ra,src.pos.dec); newname = "updated-"+lsm_filename; model.save(newname); print "Wrote updated sky model",newname; if options.output_type.upper() == "X11": from pylab import plt plt.show();
def plot_variable(u, name, direc, cmap=cmaps.parula, scale='lin', numLvls=100, umin=None, umax=None, \ tp=False, \ tpAlpha=1.0, show=False, hide_ax_tick_labels=False, label_axes=True, title='', use_colorbar=True, hide_axis=False, colorbar_loc='right'): """ show -- whether to show the plot on the screen tp -- show triangle cmap -- colors: gist_yarg - grey gnuplot, hsv, gist_ncar jet - typical colors """ mesh = u.function_space().mesh() v = u.compute_vertex_values(mesh) x = mesh.coordinates()[:,0] y = mesh.coordinates()[:,1] t = mesh.cells() if not os.path.isdir( direc ): os.makedirs(direc) full_path = os.path.join(direc, name) if umin != None: vmin = umin else: vmin = v.min() if umax != None: vmax = umax else: vmax = v.max() # countour levels : if scale == 'log': v[v < vmin] = vmin + 1e-12 v[v > vmax] = vmax - 1e-12 from matplotlib.ticker import LogFormatter levels = np.logspace(np.log10(vmin), np.log10(vmax), numLvls) tick_numLvls = min( numLvls, 8 ) tick_levels = np.logspace(np.log10(vmin), np.log10(vmax), tick_numLvls) formatter = LogFormatter(10, labelOnlyBase=False) norm = colors.LogNorm() elif scale == 'lin': v[v < vmin] = vmin + 1e-12 v[v > vmax] = vmax - 1e-12 from matplotlib.ticker import ScalarFormatter levels = np.linspace(vmin, vmax, numLvls) tick_numLvls = min( numLvls, 8 ) tick_levels = np.linspace(vmin, vmax, tick_numLvls) formatter = ScalarFormatter() norm = None elif scale == 'bool': from matplotlib.ticker import ScalarFormatter levels = [0, 1, 2] formatter = ScalarFormatter() norm = None fig = plt.figure(figsize=(5,5)) ax = fig.add_subplot(111) c = ax.tricontourf(x, y, t, v, levels=levels, norm=norm, cmap=plt.get_cmap(cmap)) plt.axis('equal') if tp == True: p = ax.triplot(x, y, t, '-', lw=0.2, alpha=tpAlpha) ax.set_xlim([x.min(), x.max()]) ax.set_ylim([y.min(), y.max()]) if label_axes: ax.set_xlabel(r'$x$') ax.set_ylabel(r'$y$') if hide_ax_tick_labels: ax.set_xticklabels([]) ax.set_yticklabels([]) if hide_axis: plt.axis('off') # include colorbar : if scale != 'bool' and use_colorbar: divider = make_axes_locatable(plt.gca()) cax = divider.append_axes(colorbar_loc, "5%", pad="3%") cbar = plt.colorbar(c, cax=cax, format=formatter, ticks=tick_levels) tit = plt.title(title) if use_colorbar: plt.tight_layout(rect=[.03,.03,0.97,0.97]) else: plt.tight_layout() plt.savefig( full_path + '.eps', dpi=300) if show: plt.show() plt.close(fig)