siat 3.0.3__py3-none-any.whl → 3.0.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
siat/markowitz.py CHANGED
@@ -188,6 +188,8 @@ def cumulative_returns_plot(retgroup,name_list="",titletxt="投资组合策略
188
188
  plt.axhline(y=0,ls=":",c="red")
189
189
  plt.legend(loc='best')
190
190
  plt.title(titletxt); plt.ylabel(ylabeltxt); plt.xlabel(xlabeltxt)
191
+
192
+ plt.gca().set_facecolor('whitesmoke')
191
193
  plt.show()
192
194
 
193
195
  return
@@ -223,10 +225,13 @@ def portfolio_hpr(portfolio,thedate,pastyears=1, \
223
225
 
224
226
  #==============================================================================
225
227
  if __name__=='__main__':
228
+ Market={'Market':('US','^GSPC')}
226
229
  Market={'Market':('US','^GSPC','我的组合001')}
227
230
  Stocks1={'AAPL':.3,'MSFT':.15,'AMZN':.15,'FB':.01,'GOOG':.01}
228
231
  Stocks2={'XOM':.02,'JNJ':.02,'JPM':.01,'TSLA':.3,'SBUX':.03}
229
232
  portfolio=dict(Market,**Stocks1,**Stocks2)
233
+
234
+ ticker_name(portfolio)
230
235
 
231
236
  thedate='2023-2-17'
232
237
  pastyears=1
@@ -239,7 +244,7 @@ def portfolio_cumret(portfolio,thedate,pastyears=1, \
239
244
  rate_period='1Y',rate_type='shibor',RF=False, \
240
245
  printout=True,graph=True):
241
246
  """
242
- 功能:绘制投资组合的累计收益率趋势图,并与等权和市值加权组合比较
247
+ 功能:绘制投资组合的累计收益率趋势图,并与等权和期间内交易额加权组合比较
243
248
  注意:中国部分历史区段的treasury历史可能无法取得;
244
249
  无论是shibor还是treasury的近期利率均可能空缺,只能以最近期的数值填补
245
250
  """
@@ -351,6 +356,8 @@ def portfolio_cumret(portfolio,thedate,pastyears=1, \
351
356
  stoday = datetime.date.today()
352
357
  plt.xlabel(source_txt+str(stoday))
353
358
  plt.legend()
359
+
360
+ plt.gca().set_facecolor('whitesmoke')
354
361
  plt.show()
355
362
  #..........................................................................
356
363
 
@@ -380,7 +387,7 @@ def portfolio_cumret(portfolio,thedate,pastyears=1, \
380
387
  StockReturns['Portfolio_EW']=stock_return.mul(portfolio_weights_ew,axis=1).sum(axis=1)
381
388
  #..........................................................................
382
389
 
383
- # 创建流动性组合:按照成交金额计算流动性
390
+ # 创建交易额加权组合:按照成交金额计算期间内交易额均值
384
391
  tamount=prices['Close']*prices['Volume']
385
392
  tamountlist=tamount.mean(axis=0) #求列的均值
386
393
  tamountlist_array = np.array(tamountlist)
@@ -393,11 +400,11 @@ def portfolio_cumret(portfolio,thedate,pastyears=1, \
393
400
  if lang == 'Chinese':
394
401
  title_txt="投资组合策略:业绩对比"
395
402
  Portfolio_EW_txt="等权重策略"
396
- Portfolio_LW_txt="流动性策略"
403
+ Portfolio_LW_txt="交易额加权策略"
397
404
  else:
398
405
  title_txt="Investment Portfolio Strategies: Performance Comparison"
399
406
  Portfolio_EW_txt="Equal-weight"
400
- Portfolio_LW_txt="OMCap"
407
+ Portfolio_LW_txt="Amount-weight"
401
408
 
402
409
  name_list=['Portfolio', 'Portfolio_EW', 'Portfolio_LW']
403
410
  label_list=[pname, Portfolio_EW_txt, Portfolio_LW_txt]
@@ -439,7 +446,7 @@ if __name__=='__main__':
439
446
  def portfolio_expret(portfolio,today,pastyears=1, \
440
447
  rate_period='1Y',rate_type='shibor',RF=False,printout=True,graph=True):
441
448
  """
442
- 功能:绘制投资组合的持有期收益率趋势图,并与等权和市值加权组合比较
449
+ 功能:绘制投资组合的持有期收益率趋势图,并与等权和期间内交易额加权组合比较
443
450
  套壳原来的portfolio_cumret函数,以维持兼容性
444
451
  expret: expanding return,以维持与前述章节名词的一致性
445
452
  hpr: holding period return, 持有(期)收益率
@@ -493,6 +500,8 @@ def portfolio_corr(pf_info):
493
500
  footnote2="\n来源:Sina/EM/stooq,"+str(stoday)
494
501
  plt.xlabel(footnote1+footnote2)
495
502
  plt.xticks(rotation=90); plt.yticks(rotation=0)
503
+
504
+ plt.gca().set_facecolor('whitesmoke')
496
505
  plt.show()
497
506
 
498
507
  return
@@ -536,6 +545,8 @@ def portfolio_covar(pf_info):
536
545
  plt.xlabel(footnote1+footnote2)
537
546
  plt.xticks(rotation=90)
538
547
  plt.yticks(rotation=0)
548
+
549
+ plt.gca().set_facecolor('whitesmoke')
539
550
  plt.show()
540
551
 
541
552
  return
@@ -589,7 +600,7 @@ def portfolio_expectation_universal(pname,member_returns,portfolio_weights,membe
589
600
  portfolio_values=member_prices.mul(portfolio_weights_1,axis=1).sum(axis=1)
590
601
  portfolio_value_thedate=portfolio_values[-1:].values[0]
591
602
 
592
- #计算年化收益率:按列求均值
603
+ #计算年化收益率:按列求均值,需要有选项:滚动的年化收益率或月度收益率?
593
604
  mean_return=preturns['Portfolio'].mean(axis=0)
594
605
  annual_return = (1 + mean_return)**252 - 1
595
606
 
@@ -922,7 +933,7 @@ def portfolio_es(pf_info,simulation=50000):
922
933
  功能:基于随机数,生成大量可能的投资组合,计算各个投资组合的年均收益率和标准差,绘制投资组合的可行集
923
934
  """
924
935
  [[portfolio,thedate,stock_return,_,_],_]=pf_info
925
- pname=pname=portfolio_name(portfolio)
936
+ pname=portfolio_name(portfolio)
926
937
  _,_,tickerlist,_=decompose_portfolio(portfolio)
927
938
 
928
939
  #取出观察期
@@ -932,14 +943,14 @@ def portfolio_es(pf_info,simulation=50000):
932
943
  #获得成份股个数
933
944
  numstocks=len(tickerlist)
934
945
 
935
- # 设置空的numpy数组,用于存储每次模拟得到的成份股权重、组合的收益率和标准差
946
+ # 设置空的numpy数组,用于存储每次模拟得到的成份股权重、投资组合的收益率和标准差
936
947
  import numpy as np
937
948
  random_p = np.empty((simulation,numstocks+2))
938
949
  # 设置随机数种子,这里是为了结果可重复
939
950
  np.random.seed(RANDOM_SEED)
940
951
 
941
952
  # 循环模拟n次随机的投资组合
942
- print("\n Calculating possible portfolio combinations, please wait ...")
953
+ print("\n Calculating portfolio efficient set, please wait ...")
943
954
  for i in range(simulation):
944
955
  # 生成numstocks个随机数,并归一化,得到一组随机的权重数据
945
956
  random9 = np.random.random(numstocks)
@@ -986,27 +997,32 @@ def portfolio_es(pf_info,simulation=50000):
986
997
 
987
998
  import datetime as dt; stoday=dt.date.today()
988
999
  lang = check_language()
989
- if lang == 'Chinese':
1000
+ if lang == 'Chinese':
1001
+ if pname == '': pname='投资组合'
1002
+
990
1003
  plt.colorbar(label='收益率/标准差')
991
- plt.title("投资组合: 马科维茨可行(有效)集",fontsize=title_txt_size)
1004
+ plt.title(pname+": 马科维茨可行(有效)集",fontsize=title_txt_size)
992
1005
  plt.ylabel("年化收益率",fontsize=ylabel_txt_size)
993
1006
 
994
1007
  footnote1="年化收益率标准差-->"
995
- footnote2="\n\n基于"+pname+"之成份股构造"+str(simulation)+"个投资组合"
1008
+ footnote2="\n\n基于给定的成份证券构造"+str(simulation)+"个投资组合"
996
1009
  footnote3="\n观察期间:"+hstart+"至"+hend
997
1010
  footnote4="\n来源: Sina/EM/stooq, "+str(stoday)
998
1011
  else:
1012
+ if pname == '': pname='Investment Portfolio'
1013
+
999
1014
  plt.colorbar(label='Return/Std')
1000
- plt.title("Investment Portfolio: Efficient Set",fontsize=title_txt_size)
1015
+ plt.title(pname+": Efficient Set",fontsize=title_txt_size)
1001
1016
  plt.ylabel("Annualized Return",fontsize=ylabel_txt_size)
1002
1017
 
1003
1018
  footnote1="Annualized Std -->\n\n"
1004
- footnote2="Based on "+pname+"\'s stocks, constructed "+str(simulation)+" portfolios\n"
1019
+ footnote2="Based on given component securities, constructed "+str(simulation)+" portfolios\n"
1005
1020
  footnote3="Period of observation: "+hstart+" to "+hend
1006
1021
  footnote4="\nSource: sina/eastmoney/stooq, "+str(stoday)
1007
1022
 
1008
1023
  plt.xlabel(footnote1+footnote2+footnote3+footnote4,fontsize=xlabel_txt_size)
1009
1024
 
1025
+ plt.gca().set_facecolor('whitesmoke')
1010
1026
  plt.show()
1011
1027
 
1012
1028
  return [pf_info,RandomPortfolios]
@@ -1456,21 +1472,25 @@ def RandomPortfolios_plot(RandomPortfolios,col_x,col_y,colorbartxt,title_ext, \
1456
1472
 
1457
1473
  lang = check_language()
1458
1474
  if lang == 'Chinese':
1459
- plt.title("投资组合: 马科维茨有效(可行)集,基于"+title_ext,fontsize=title_txt_size)
1475
+ if pname == '': pname='投资组合'
1476
+
1477
+ plt.title(pname+": 马科维茨有效(可行)集,基于"+title_ext,fontsize=title_txt_size)
1460
1478
  plt.ylabel(ylabeltxt,fontsize=ylabel_txt_size)
1461
1479
 
1462
1480
  import datetime as dt; stoday=dt.date.today()
1463
1481
  footnote1=x_axis_name+" -->\n\n"
1464
- footnote2="基于"+pname+"之成份股构造"+str(simulation)+"个投资组合"
1482
+ footnote2="基于设定的成份证券构造"+str(simulation)+"个投资组合"
1465
1483
  footnote3="\n观察期间:"+hstart+"至"+hend
1466
1484
  footnote4="\n来源: Sina/EM/stooq/FRED, "+str(stoday)
1467
1485
  else:
1468
- plt.title("Portfolio: Efficient Set, Based on "+title_ext,fontsize=title_txt_size)
1486
+ if pname == '': pname='Investment Portfolio'
1487
+
1488
+ plt.title(pname+": Efficient Set, Based on "+title_ext,fontsize=title_txt_size)
1469
1489
  plt.ylabel(ylabeltxt,fontsize=ylabel_txt_size)
1470
1490
 
1471
1491
  import datetime as dt; stoday=dt.date.today()
1472
1492
  footnote1=x_axis_name+" -->\n\n"
1473
- footnote2="Based on "+pname+"\'s stocks, constructed "+str(simulation)+" portfolios"
1493
+ footnote2="Based on given component securities, constructed "+str(simulation)+" portfolios"
1474
1494
  footnote3="\nPeriod of observation: "+hstart+" to "+hend
1475
1495
  footnote4="\nSource: sina/eastmoney/stooq/FRED, "+str(stoday)
1476
1496
 
@@ -1486,6 +1506,8 @@ def RandomPortfolios_plot(RandomPortfolios,col_x,col_y,colorbartxt,title_ext, \
1486
1506
  plt.scatter(lorisk_x, lorisk_y, color='red',marker='8',s=150,label=name_lorisk)
1487
1507
 
1488
1508
  plt.legend(loc='best')
1509
+
1510
+ plt.gca().set_facecolor('whitesmoke')
1489
1511
  plt.show()
1490
1512
 
1491
1513
  return
@@ -1503,10 +1525,10 @@ def cvt_portfolio_name(pname,portfolio_returns):
1503
1525
 
1504
1526
  lang=check_language()
1505
1527
  if lang == "Chinese":
1506
- pclist=[pname,'等权重组合','流动性组合','MSR组合','GMVS组合','MSO组合','GML组合', \
1528
+ pclist=[pname,'等权重组合','交易额加权组合','MSR组合','GMVS组合','MSO组合','GML组合', \
1507
1529
  'MAR组合','GMBA组合', 'MTR组合','GMBT组合']
1508
1530
  else:
1509
- pclist=[pname,'Equal-weight','OMCap','MSR','GMVS','MSO','GML', \
1531
+ pclist=[pname,'Equal-weight','Amount-weight','MSR','GMVS','MSO','GML', \
1510
1532
  'MAR','GMBA', 'MTR','GMBT']
1511
1533
 
1512
1534
  pecols=list(portfolio_returns)
@@ -2021,6 +2043,8 @@ def portfolio_ef_0(stocks,fromdate,todate):
2021
2043
  plt.figtext(x_left,y_left-0.05,' '+str(stocks))
2022
2044
  plt.figtext(x_left,y_left-0.1,'观察期间:'+str(fromdate)+'至'+str(todate))
2023
2045
  plt.plot(out_std,out_mean,color='r',ls=':',lw=4)
2046
+
2047
+ plt.gca().set_facecolor('whitesmoke')
2024
2048
  plt.show()
2025
2049
 
2026
2050
  return
@@ -2104,6 +2128,7 @@ def portfolio_ef(stocks,fromdate,todate):
2104
2128
  plt.plot(out_std_min,out_mean_min,'g*-',markersize=16,label='风险最低点')
2105
2129
 
2106
2130
  plt.legend(loc='best')
2131
+ plt.gca().set_facecolor('whitesmoke')
2107
2132
  plt.show()
2108
2133
 
2109
2134
  return
@@ -2247,6 +2272,8 @@ def security_correlation(tickers,start,end,info_type='Close'):
2247
2272
  fontxlabel={'size':6}
2248
2273
  plt.xlabel(footnote1+footnote2+footnote3+footnote4,fontxlabel)
2249
2274
  #plt.xticks(rotation=45)
2275
+
2276
+ plt.gca().set_facecolor('whitesmoke')
2250
2277
  plt.show()
2251
2278
 
2252
2279
  return df_coor