fork download
  1. import pandas as pd #資料整理與分析
  2. import numpy as np #基本計算
  3. import scipy.optimize as solver #規劃求解
  4. import statistics as stat #統計數字,回傳投資組合超額報酬率
  5. import matplotlib.pyplot as plt #畫圖
  6. from functools import reduce #做矩陣中數值的相加
  7. import statsmodels.api as reg #回歸
  8.  
  9.  
  10. # In[2]:
  11.  
  12. # 修改檔名
  13. Monthly_TEJ_return = pd.read_excel(r'C:\\Users\\cpes9\\Desktop\\hw_2.xlsx', sep = ',', sheet_name = '工作表1', index_col = '年月')
  14. # 修改無風險利率Title
  15. Monthly_TEJ_return['MB64_M'] = Monthly_TEJ_return['MB64_Y'] / 12
  16. #print(Monthly_TEJ_return.head(4))
  17.  
  18.  
  19. # In[3]:
  20.  
  21. #Excess return table
  22. # 修改需要的列跟行
  23. Monthly_excess_return = Monthly_TEJ_return.iloc[0:60,0:-2] #只取個股和Y9999[列,行]
  24. for column in Monthly_excess_return:
  25. Monthly_excess_return[column] = (Monthly_excess_return[column] - Monthly_TEJ_return['MB64_M']) / 100 #本來是顯示百分比數字
  26. #print(Monthly_excess_return)
  27.  
  28.  
  29. # In[4]:
  30.  
  31. #Markowitz approach to construct monthly covariance matrix
  32. # 修改需要的列跟行
  33. Monthly_excess_return = Monthly_excess_return.iloc[:,0:-1]
  34. Monthly_cov_matrix = Monthly_excess_return.cov()
  35. #print(Monthly_cov_matrix)
  36.  
  37.  
  38. # In[5]:
  39.  
  40. #convert monthly matrix to annually matrix
  41. Annual_cov_matrix = Monthly_cov_matrix * 12
  42. print(Annual_cov_matrix)
Runtime error #stdin #stdout #stderr 1.47s 109960KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Unable to init server: Could not connect: Connection refused
Unable to init server: Could not connect: Connection refused

(prog:28106): Gdk-CRITICAL **: 00:04:56.026: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed

(prog:28106): Gdk-CRITICAL **: 00:04:56.044: gdk_cursor_new_for_display: assertion 'GDK_IS_DISPLAY (display)' failed
Traceback (most recent call last):
  File "./prog.py", line 7, in <module>
ModuleNotFoundError: No module named 'statsmodels'