jeff7522553 commited on
Commit
a3a5e98
·
1 Parent(s): 424ed87

修改plt 中文 -> 英文

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. bond.py +33 -33
app.py CHANGED
@@ -131,7 +131,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="債券利率蒙地卡羅模擬") a
131
  kappa_cir = gr.Slider(0.01, 1.0, value=0.2, step=0.01, label="均值回歸速度 (kappa_cir)")
132
  theta_cir = gr.Slider(1.0, 5.0, value=2.5, step=0.1, label="長期平均利率 (%)")
133
  sigma_cir = gr.Slider(0.1, 2.0, value=0.4, step=0.1, label="波動率 (%)")
134
- n_simulations = gr.Slider(50, 100000, value=100, step=50, label="模擬次數 (n_simulations)")
135
  dt = gr.Number(value=1/252, label="時間步長 (dt)", info="以年為單位,預設為一個交易日(1/252年)。")
136
 
137
  # --- 步驟 3: 執行 ---
 
131
  kappa_cir = gr.Slider(0.01, 1.0, value=0.2, step=0.01, label="均值回歸速度 (kappa_cir)")
132
  theta_cir = gr.Slider(1.0, 5.0, value=2.5, step=0.1, label="長期平均利率 (%)")
133
  sigma_cir = gr.Slider(0.1, 2.0, value=0.4, step=0.1, label="波動率 (%)")
134
+ n_simulations = gr.Slider(50, 100000, value=50, step=50, label="模擬次數 (n_simulations)")
135
  dt = gr.Number(value=1/252, label="時間步長 (dt)", info="以年為單位,預設為一個交易日(1/252年)。")
136
 
137
  # --- 步驟 3: 執行 ---
bond.py CHANGED
@@ -303,31 +303,31 @@ def run_simulation(bond_data, n_simulations, dt, params):
303
  plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei']
304
  plt.rcParams['axes.unicode_minus'] = False
305
 
306
- # 繪製利率模擬路徑圖
307
  fig_rate_paths = plt.figure(figsize=(12, 10))
308
- plt.suptitle('利率模擬路徑 (50)', fontsize=16)
309
 
310
  ax1 = fig_rate_paths.add_subplot(2, 2, 1)
311
  ax1.plot(time_points, rf_paths[:50, :].T * 100, lw=0.5)
312
- ax1.set_title('債券風險利率 (drf) - Vasicek')
313
- ax1.set_ylabel('利率 (%)')
314
 
315
  ax2 = fig_rate_paths.add_subplot(2, 2, 2)
316
  ax2.plot(time_points, drs_paths[:50, :].T * 100, lw=0.5)
317
- ax2.set_title('有風險利率 (drs) - Vasicek + Spread')
318
- ax2.set_ylabel('利率 (%)')
319
 
320
  ax3 = fig_rate_paths.add_subplot(2, 2, 3)
321
  ax3.plot(time_points, dr_paths[:50, :].T * 100, lw=0.5)
322
- ax3.set_title('綜合利率 (dr) - GBM')
323
- ax3.set_xlabel('時間 ()')
324
- ax3.set_ylabel('利率 (%)')
325
 
326
  ax4 = fig_rate_paths.add_subplot(2, 2, 4)
327
  ax4.plot(time_points, cir_paths[:50, :].T * 100, lw=0.5)
328
- ax4.set_title('CIR 利率 (dr_cir) - Cox-Ingersoll-Ross')
329
- ax4.set_xlabel('時間 ()')
330
- ax4.set_ylabel('利率 (%)')
331
 
332
  fig_rate_paths.tight_layout(rect=[0, 0, 1, 0.96])
333
 
@@ -337,49 +337,49 @@ def run_simulation(bond_data, n_simulations, dt, params):
337
  plt.hist(drs_changes * 100, bins=100, alpha=0.6, label='drs (Risky)', density=True)
338
  plt.hist(dr_changes * 100, bins=100, alpha=0.6, label='dr (GBM)', density=True)
339
  plt.hist(cir_changes * 100, bins=100, alpha=0.6, label='dr_cir (CIR)', density=True)
340
- plt.title('每日利率變化分佈')
341
- plt.xlabel('利率變化 (%)')
342
- plt.ylabel('機率密度')
343
  plt.legend()
344
  plt.grid(True, linestyle='--', alpha=0.6)
345
 
346
  # 繪製價格模擬路徑圖
347
  fig_price_paths = plt.figure(figsize=(12, 10))
348
- plt.suptitle('債券價格模擬路徑 (50)', fontsize=16)
349
 
350
  ax_p1 = fig_price_paths.add_subplot(2, 2, 1)
351
  ax_p1.plot(time_points, price_rf_paths[:50, :].T, lw=0.5)
352
- ax_p1.set_title('基於 drf (Vasicek) 的價格')
353
- ax_p1.set_ylabel('價格')
354
 
355
  ax_p2 = fig_price_paths.add_subplot(2, 2, 2)
356
  ax_p2.plot(time_points, price_drs_paths[:50, :].T, lw=0.5)
357
- ax_p2.set_title('基於 drs (Risky) 的價格')
358
- ax_p2.set_ylabel('價格')
359
 
360
  ax_p3 = fig_price_paths.add_subplot(2, 2, 3)
361
  ax_p3.plot(time_points, price_dr_paths[:50, :].T, lw=0.5)
362
- ax_p3.set_title('基於 dr (GBM) 的價格')
363
- ax_p3.set_xlabel('時間 ()')
364
- ax_p3.set_ylabel('價格')
365
 
366
  ax_p4 = fig_price_paths.add_subplot(2, 2, 4)
367
  ax_p4.plot(time_points, price_cir_paths[:50, :].T, lw=0.5)
368
- ax_p4.set_title('基於 dr_cir (CIR) 的價格')
369
- ax_p4.set_xlabel('時間 ()')
370
- ax_p4.set_ylabel('價格')
371
 
372
  fig_price_paths.tight_layout(rect=[0, 0, 1, 0.96])
373
 
374
  # 繪製最終價格分佈圖
375
  fig_price_dist = plt.figure(figsize=(12, 5))
376
- plt.hist(final_prices_rf, bins=100, alpha=0.6, label='基於 drf (Vasicek)', density=True)
377
- plt.hist(final_prices_drs, bins=100, alpha=0.6, label='基於 drs (Risky)', density=True)
378
- plt.hist(final_prices_dr, bins=100, alpha=0.6, label='基於 dr (GBM)', density=True)
379
- plt.hist(final_prices_cir, bins=100, alpha=0.6, label='基於 dr_cir (CIR)', density=True)
380
- plt.title('最終債券價格分佈')
381
- plt.xlabel('價格')
382
- plt.ylabel('機率密度')
383
  plt.legend()
384
  plt.grid(True, linestyle='--', alpha=0.6)
385
 
 
303
  plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei']
304
  plt.rcParams['axes.unicode_minus'] = False
305
 
306
+ # 繪製利率模擬路徑圖
307
  fig_rate_paths = plt.figure(figsize=(12, 10))
308
+ plt.suptitle('Simulated Interest Rate Paths (First 50)', fontsize=16)
309
 
310
  ax1 = fig_rate_paths.add_subplot(2, 2, 1)
311
  ax1.plot(time_points, rf_paths[:50, :].T * 100, lw=0.5)
312
+ ax1.set_title('Risk-Free Rate (drf) - Vasicek')
313
+ ax1.set_ylabel('Interest Rate (%)')
314
 
315
  ax2 = fig_rate_paths.add_subplot(2, 2, 2)
316
  ax2.plot(time_points, drs_paths[:50, :].T * 100, lw=0.5)
317
+ ax2.set_title('Risky Rate (drs) - Vasicek + Spread')
318
+ ax2.set_ylabel('Interest Rate (%)')
319
 
320
  ax3 = fig_rate_paths.add_subplot(2, 2, 3)
321
  ax3.plot(time_points, dr_paths[:50, :].T * 100, lw=0.5)
322
+ ax3.set_title('Composite Rate (dr) - GBM')
323
+ ax3.set_xlabel('Time (Years)')
324
+ ax3.set_ylabel('Interest Rate (%)')
325
 
326
  ax4 = fig_rate_paths.add_subplot(2, 2, 4)
327
  ax4.plot(time_points, cir_paths[:50, :].T * 100, lw=0.5)
328
+ ax4.set_title('CIR Rate (dr_cir) - Cox-Ingersoll-Ross')
329
+ ax4.set_xlabel('Time (Years)')
330
+ ax4.set_ylabel('Interest Rate (%)')
331
 
332
  fig_rate_paths.tight_layout(rect=[0, 0, 1, 0.96])
333
 
 
337
  plt.hist(drs_changes * 100, bins=100, alpha=0.6, label='drs (Risky)', density=True)
338
  plt.hist(dr_changes * 100, bins=100, alpha=0.6, label='dr (GBM)', density=True)
339
  plt.hist(cir_changes * 100, bins=100, alpha=0.6, label='dr_cir (CIR)', density=True)
340
+ plt.title('Distribution of Daily Rate Changes')
341
+ plt.xlabel('Rate Change (%)')
342
+ plt.ylabel('Probability Density')
343
  plt.legend()
344
  plt.grid(True, linestyle='--', alpha=0.6)
345
 
346
  # 繪製價格模擬路徑圖
347
  fig_price_paths = plt.figure(figsize=(12, 10))
348
+ plt.suptitle('Simulated Bond Price Paths (First 50)', fontsize=16)
349
 
350
  ax_p1 = fig_price_paths.add_subplot(2, 2, 1)
351
  ax_p1.plot(time_points, price_rf_paths[:50, :].T, lw=0.5)
352
+ ax_p1.set_title('Prices based on drf (Vasicek)')
353
+ ax_p1.set_ylabel('Price')
354
 
355
  ax_p2 = fig_price_paths.add_subplot(2, 2, 2)
356
  ax_p2.plot(time_points, price_drs_paths[:50, :].T, lw=0.5)
357
+ ax_p2.set_title('Prices based on drs (Risky)')
358
+ ax_p2.set_ylabel('Price')
359
 
360
  ax_p3 = fig_price_paths.add_subplot(2, 2, 3)
361
  ax_p3.plot(time_points, price_dr_paths[:50, :].T, lw=0.5)
362
+ ax_p3.set_title('Prices based on dr (GBM)')
363
+ ax_p3.set_xlabel('Time (Years)')
364
+ ax_p3.set_ylabel('Price')
365
 
366
  ax_p4 = fig_price_paths.add_subplot(2, 2, 4)
367
  ax_p4.plot(time_points, price_cir_paths[:50, :].T, lw=0.5)
368
+ ax_p4.set_title('Prices based on dr_cir (CIR)')
369
+ ax_p4.set_xlabel('Time (Years)')
370
+ ax_p4.set_ylabel('Price')
371
 
372
  fig_price_paths.tight_layout(rect=[0, 0, 1, 0.96])
373
 
374
  # 繪製最終價格分佈圖
375
  fig_price_dist = plt.figure(figsize=(12, 5))
376
+ plt.hist(final_prices_rf, bins=100, alpha=0.6, label='Based on drf (Vasicek)', density=True)
377
+ plt.hist(final_prices_drs, bins=100, alpha=0.6, label='Based on drs (Risky)', density=True)
378
+ plt.hist(final_prices_dr, bins=100, alpha=0.6, label='Based on dr (GBM)', density=True)
379
+ plt.hist(final_prices_cir, bins=100, alpha=0.6, label='Based on dr_cir (CIR)', density=True)
380
+ plt.title('Distribution of Final Bond Prices')
381
+ plt.xlabel('Price')
382
+ plt.ylabel('Probability Density')
383
  plt.legend()
384
  plt.grid(True, linestyle='--', alpha=0.6)
385