In this project we are going to analyze & compare two portfolios using few risk metrics( VAR, Beta, Sharpe, Rolling, Drawdown etc) over the period of 3 montha;May 2024-August 2024. The stocks in both the portfolio are picked uding GPT-4o, all the stocks have equal weightage in their portfolios. In the first portfolio, we have:

In the second portfolio we have;

Now lets start with our analysis, first we start with loading all the libraies

 library(quantmod)
library(dygraphs)
library(PerformanceAnalytics)
options(scipen = 999)

We will retrive the stock data using the library called ‘quantmod’, the default source is Yahoo.

symbolslist<- c('AAPL','GS','JNJ','XOM','PG','JPM','MSFT','PFE','CVX','KO','^GSPC')
#Setting the time period for August 2024
getSymbols(symbolslist,from='2024-05-01', to='2024-08-1',)
##  [1] "AAPL" "GS"   "JNJ"  "XOM"  "PG"   "JPM"  "MSFT" "PFE"  "CVX"  "KO"  
## [11] "GSPC"
get
## function (x, pos = -1L, envir = as.environment(pos), mode = "any", 
##     inherits = TRUE) 
## .Internal(get(x, envir, mode, inherits))
## <bytecode: 0x0000027d42d53908>
## <environment: namespace:base>

Lets ceate the portfolios by merging the stocks into their bags and calculate their returns.

m2<-merge.xts(dailyReturn(JPM),dailyReturn(MSFT),dailyReturn(PFE),dailyReturn(CVX),dailyReturn(KO))
m1<- merge.xts(dailyReturn(GS),dailyReturn(AAPL),dailyReturn(JNJ),dailyReturn(XOM),dailyReturn(PG))
m1
##            daily.returns daily.returns.1 daily.returns.2 daily.returns.3
## 2024-05-01  0.0006093646  -0.00165113089    0.0164043774   -0.0190226583
## 2024-05-02  0.0131631220   0.02203187041   -0.0083343998    0.0018098689
## 2024-05-03  0.0129689652   0.05981625254   -0.0043356050   -0.0020646754
## 2024-05-06  0.0125291452  -0.00910676259   -0.0046225124    0.0064655172
## 2024-05-07  0.0002929528   0.00379718869    0.0009422492   -0.0049678958
## 2024-05-08  0.0070978470   0.00186409873    0.0015465017   -0.0001721326
## 2024-05-09  0.0192638665   0.01001423758    0.0060423577    0.0197158923
## 2024-05-10 -0.0018219040  -0.00823538068    0.0004003841   -0.0040527132
## 2024-05-13 -0.0025729848   0.01764542843    0.0087385600   -0.0004238337
## 2024-05-14  0.0108254778   0.00617346953    0.0010580853   -0.0020354973
## 2024-05-15  0.0166204879   0.01221794075    0.0085215566    0.0077335232
## 2024-05-16 -0.0033684639   0.00063248533    0.0105456254   -0.0059875112
## 2024-05-17  0.0068888579   0.00015802139    0.0023334237    0.0150165148
## 2024-05-20 -0.0102197870   0.00616210143   -0.0217925190   -0.0081076665
## 2024-05-21  0.0161360029   0.00685727001   -0.0001983128   -0.0069099158
## 2024-05-22 -0.0170702126  -0.00753840479    0.0149430999   -0.0201102685
## 2024-05-23 -0.0091483432  -0.02105808875   -0.0247557202   -0.0170592411
## 2024-05-24  0.0066135519   0.01658813551   -0.0182364448   -0.0007929166
## 2024-05-28 -0.0029706300   0.00005268852   -0.0176226190    0.0126961952
## 2024-05-29 -0.0057414674   0.00157896618    0.0004155531   -0.0107087180
## 2024-05-30 -0.0151803536   0.00525513708    0.0058155381    0.0031681829
## 2024-05-31  0.0139705881   0.00501859348    0.0095677272    0.0286867649
## 2024-06-03 -0.0033732981   0.00925877128    0.0072953388   -0.0239638848
## 2024-06-04  0.0007032766   0.00164926726    0.0004061023   -0.0155526328
## 2024-06-05  0.0140127500   0.00782088483   -0.0123816089    0.0039940054
## 2024-06-06 -0.0077542597  -0.00709654069    0.0030828043    0.0075141306
## 2024-06-07 -0.0069635503   0.01239203885    0.0045076060   -0.0107045820
## 2024-06-10 -0.0029896372  -0.01914776923    0.0003399718    0.0029268455
## 2024-06-11 -0.0204608070   0.07264912559   -0.0025148533   -0.0080474323
## 2024-06-12  0.0099714662   0.02857839055   -0.0091986297   -0.0110546303
## 2024-06-13 -0.0052373658   0.00549114436    0.0002750381   -0.0080230721
## 2024-06-14  0.0002464106  -0.00816840905    0.0006187442   -0.0084514749
## 2024-06-17  0.0083322163   0.01967147898    0.0028171202   -0.0068737970
## 2024-06-18  0.0161046695  -0.01098446902   -0.0020555194    0.0094130365
## 2024-06-20  0.0013553880  -0.02151290660    0.0146241330    0.0215761626
## 2024-06-21 -0.0171815202  -0.01044442607    0.0065638194   -0.0087703217
## 2024-06-24  0.0264560928   0.00313265159    0.0024873621    0.0297038719
## 2024-06-25 -0.0101928012   0.00446818457   -0.0129425479    0.0028057842
## 2024-06-26 -0.0033233187   0.01999326795   -0.0025137245    0.0003497501
## 2024-06-27 -0.0217171812   0.00398596063   -0.0069473111    0.0042828236
## 2024-06-28  0.0142614046  -0.01625413773    0.0024691399    0.0019147190
## 2024-07-01  0.0250707379   0.02910457233    0.0019157004   -0.0013898858
## 2024-07-02  0.0042056284   0.01623992744   -0.0027998064   -0.0067849581
## 2024-07-03  0.0049612941   0.00581104442   -0.0023282636    0.0050797147
## 2024-07-05 -0.0067746908   0.02162037111    0.0054224262   -0.0121122287
## 2024-07-08  0.0001506344   0.00653888403   -0.0068268708   -0.0104966253
## 2024-07-09  0.0172324322   0.00377484560    0.0107919121   -0.0110536447
## 2024-07-10  0.0128165054   0.01880358225    0.0161849002    0.0088335650
## 2024-07-11  0.0007099675  -0.02322082798    0.0018068948    0.0118835048
## 2024-07-12  0.0013563297   0.01305086728    0.0012024578    0.0001765708
## 2024-07-15  0.0257356130   0.01674330148   -0.0042700785    0.0171272402
## 2024-07-16  0.0219206017   0.00179186621    0.0118600171    0.0072042517
## 2024-07-17 -0.0016699065  -0.02529598099    0.0368850243    0.0137883356
## 2024-07-18 -0.0318013490  -0.02053483095   -0.0074083769    0.0098606228
## 2024-07-19 -0.0026326048   0.00057991296   -0.0046969228   -0.0229798256
## 2024-07-22  0.0043511763  -0.00156029980   -0.0029090241   -0.0068924188
## 2024-07-23  0.0104919211   0.00468828256   -0.0122536263   -0.0161359680
## 2024-07-24 -0.0110738225  -0.02875428371    0.0257958157    0.0141080894
## 2024-07-25  0.0102937726  -0.00480455672    0.0214998761    0.0210416322
## 2024-07-26  0.0148868387   0.00216102446    0.0062640942   -0.0008515582
## 2024-07-29 -0.0126445255   0.00128463374   -0.0129482186   -0.0104832808
## 2024-07-30  0.0262827005   0.00256597115    0.0174697548    0.0178294550
## 2024-07-31  0.0066446205   0.01499085344   -0.0215706669    0.0035541861
##            daily.returns.4
## 2024-05-01    0.0007349040
## 2024-05-02    0.0026927935
## 2024-05-03    0.0037842431
## 2024-05-06   -0.0001216361
## 2024-05-07    0.0080271956
## 2024-05-08   -0.0041625676
## 2024-05-09    0.0058762096
## 2024-05-10    0.0048784200
## 2024-05-13   -0.0058736047
## 2024-05-14   -0.0006631737
## 2024-05-15    0.0045246140
## 2024-05-16    0.0081076581
## 2024-05-17   -0.0013106233
## 2024-05-20   -0.0019685149
## 2024-05-21    0.0062160574
## 2024-05-22   -0.0032076538
## 2024-05-23   -0.0138251123
## 2024-05-24   -0.0009668479
## 2024-05-28   -0.0134276973
## 2024-05-29   -0.0089510558
## 2024-05-30    0.0057532197
## 2024-05-31    0.0120555507
## 2024-06-03    0.0006685342
## 2024-06-04    0.0143334388
## 2024-06-05   -0.0057481078
## 2024-06-06    0.0145739122
## 2024-06-07   -0.0083694643
## 2024-06-10    0.0017359544
## 2024-06-11    0.0007767530
## 2024-06-12   -0.0137330016
## 2024-06-13    0.0074464889
## 2024-06-14    0.0022834542
## 2024-06-17    0.0042568904
## 2024-06-18    0.0063283436
## 2024-06-20   -0.0052800154
## 2024-06-21    0.0035187949
## 2024-06-24    0.0011292193
## 2024-06-25   -0.0094983133
## 2024-06-26    0.0035959893
## 2024-06-27   -0.0049567145
## 2024-06-28   -0.0102028388
## 2024-07-01   -0.0133397828
## 2024-07-02    0.0072516757
## 2024-07-03   -0.0004270413
## 2024-07-05    0.0084233954
## 2024-07-08    0.0079292870
## 2024-07-09   -0.0051645483
## 2024-07-10    0.0068815608
## 2024-07-11   -0.0075540152
## 2024-07-12    0.0064637391
## 2024-07-15   -0.0121841352
## 2024-07-16    0.0144002618
## 2024-07-17    0.0149146783
## 2024-07-18   -0.0059017941
## 2024-07-19   -0.0028496540
## 2024-07-22    0.0017265615
## 2024-07-23   -0.0096879934
## 2024-07-24    0.0082823486
## 2024-07-25   -0.0065476554
## 2024-07-26    0.0132415027
## 2024-07-29    0.0048488680
## 2024-07-30   -0.0484316841
## 2024-07-31   -0.0058132496
m2
##            daily.returns daily.returns.1 daily.returns.2 daily.returns.3
## 2024-05-01 -0.0021324370    0.0059346863    0.0322825820   -0.0071525930
## 2024-05-02 -0.0010424109    0.0073428720    0.0191317311    0.0068908777
## 2024-05-03 -0.0060002564    0.0221697351    0.0039710722   -0.0029863482
## 2024-05-06  0.0078211408    0.0169183220    0.0125854149    0.0127925307
## 2024-05-07 -0.0013020833   -0.0101562415   -0.0138494102    0.0022796988
## 2024-05-08  0.0203389512    0.0029315782    0.0180050411   -0.0007991940
## 2024-05-09  0.0094556921    0.0043357498   -0.0031835922    0.0179033086
## 2024-05-10  0.0064304014    0.0058691862   -0.0060326499    0.0022363879
## 2024-05-13 -0.0002012806   -0.0024593457    0.0153516709   -0.0077795711
## 2024-05-14  0.0139888232    0.0068645372   -0.0021097529   -0.0055916743
## 2024-05-15  0.0029775501    0.0156519806    0.0155038952   -0.0034227587
## 2024-05-16  0.0017812113   -0.0049399556    0.0034698259   -0.0120208935
## 2024-05-17  0.0114584484   -0.0018527727   -0.0096819048    0.0098081934
## 2024-05-20 -0.0449728588    0.0122081935   -0.0038407369   -0.0094055376
## 2024-05-21  0.0201452214    0.0086989520    0.0010514819   -0.0072607557
## 2024-05-22 -0.0060645885    0.0034495162    0.0364145986   -0.0150027748
## 2024-05-23 -0.0070092250   -0.0081761338   -0.0307432377   -0.0039982538
## 2024-05-24  0.0192464380    0.0074004770    0.0066224686    0.0051611925
## 2024-05-28 -0.0060286317    0.0003719631   -0.0200831004    0.0081774535
## 2024-05-29 -0.0069674155   -0.0026724156   -0.0169611150   -0.0134557311
## 2024-05-30  0.0061582011   -0.0337861443    0.0136592765    0.0086679456
## 2024-05-31  0.0165554760    0.0011092952    0.0163120238    0.0255276677
## 2024-06-03 -0.0039974216   -0.0038783414    0.0226796798   -0.0298212954
## 2024-06-04 -0.0131800791    0.0061666144    0.0061412592   -0.0084466009
## 2024-06-05 -0.0095401141    0.0190833329    0.0020345699   -0.0073657152
## 2024-06-06 -0.0017742617    0.0012027529   -0.0253807113    0.0061298037
## 2024-06-07  0.0154384908   -0.0015782129   -0.0076388652    0.0009620289
## 2024-06-10 -0.0017004068    0.0094844614   -0.0178446547    0.0042286241
## 2024-06-11 -0.0263012874    0.0112417267   -0.0014249736   -0.0015949980
## 2024-06-12 -0.0145606186    0.0193676736   -0.0132001723   -0.0145057460
## 2024-06-13  0.0111209988    0.0011789530   -0.0003615412   -0.0089482873
## 2024-06-14  0.0006196174    0.0022419964   -0.0043399253   -0.0017664814
## 2024-06-17  0.0061925738    0.0131052437   -0.0199782467    0.0040636763
## 2024-06-18  0.0103600591   -0.0045275081    0.0159377432    0.0009138938
## 2024-06-20  0.0084771481   -0.0014338489    0.0120393990    0.0215874096
## 2024-06-21 -0.0119293056    0.0091541092    0.0000000000   -0.0086823328
## 2024-06-24  0.0131431574   -0.0046911498    0.0223504270    0.0259531093
## 2024-06-25 -0.0040727953    0.0073268226   -0.0130465737   -0.0053354520
## 2024-06-26 -0.0032312547    0.0026832053   -0.0203644051   -0.0158400206
## 2024-06-27  0.0088132784    0.0015260139    0.0138584668    0.0023726219
## 2024-06-28  0.0155143665   -0.0130285830    0.0064748313    0.0006396548
## 2024-07-01  0.0157717914    0.0218816389    0.0085775474    0.0006393435
## 2024-07-02  0.0164517154    0.0055831404   -0.0138199645    0.0014694334
## 2024-07-03 -0.0006703988    0.0032441871   -0.0046711880   -0.0002551406
## 2024-07-05 -0.0186880498    0.0147362213    0.0126353236   -0.0153149707
## 2024-07-08  0.0018555833   -0.0028231828   -0.0042780369    0.0001296369
## 2024-07-09  0.0119900899   -0.0143702424   -0.0075188324   -0.0087475286
## 2024-07-10  0.0008187553    0.0146015392    0.0227273118    0.0140541850
## 2024-07-11 -0.0016843412   -0.0247720918    0.0109347253    0.0008379399
## 2024-07-12 -0.0120992747   -0.0025291937    0.0090718852    0.0021254788
## 2024-07-15  0.0249341297    0.0009039878    0.0062240769    0.0157464940
## 2024-07-16  0.0169959153   -0.0097806030    0.0137456912   -0.0017083588
## 2024-07-17  0.0152139316   -0.0133475711    0.0179661250    0.0219306038
## 2024-07-18 -0.0317701828   -0.0071022591   -0.0106560638    0.0045897507
## 2024-07-19 -0.0009524571   -0.0074028880    0.0087512702   -0.0174106767
## 2024-07-22  0.0023834493    0.0133376434   -0.0123456437   -0.0135720294
## 2024-07-23  0.0002377927    0.0043121047   -0.0033783912   -0.0184725718
## 2024-07-24 -0.0082727404   -0.0358548095    0.0162711709    0.0063599910
## 2024-07-25  0.0003835363   -0.0244812314    0.0066711396    0.0159281992
## 2024-07-26  0.0171083882    0.0164196826    0.0195493753    0.0019042977
## 2024-07-29 -0.0065491866    0.0034331648   -0.0016249965   -0.0078559951
## 2024-07-30  0.0205833351   -0.0089283562    0.0218098988    0.0189655243
## 2024-07-31 -0.0111064611   -0.0108058431   -0.0270786394    0.0056401194
##            daily.returns.4
## 2024-05-01    0.0021035772
## 2024-05-02    0.0009688580
## 2024-05-03    0.0029036375
## 2024-05-06    0.0028952921
## 2024-05-07    0.0043304004
## 2024-05-08    0.0036729407
## 2024-05-09    0.0004773683
## 2024-05-10    0.0060432132
## 2024-05-13    0.0050585444
## 2024-05-14   -0.0075495965
## 2024-05-15    0.0004754769
## 2024-05-16    0.0030096408
## 2024-05-17   -0.0045799260
## 2024-05-20   -0.0072980976
## 2024-05-21    0.0054339165
## 2024-05-22    0.0014306176
## 2024-05-23   -0.0144444420
## 2024-05-24   -0.0014495112
## 2024-05-28   -0.0029032307
## 2024-05-29   -0.0019411021
## 2024-05-30    0.0043760203
## 2024-05-31    0.0154913517
## 2024-06-03    0.0000000000
## 2024-06-04    0.0160495521
## 2024-06-05   -0.0003128004
## 2024-06-06    0.0035983004
## 2024-06-07   -0.0037412576
## 2024-06-10   -0.0050070364
## 2024-06-11   -0.0006290441
## 2024-06-12   -0.0105428509
## 2024-06-13    0.0017493735
## 2024-06-14   -0.0069852743
## 2024-06-17    0.0011190999
## 2024-06-18    0.0001597275
## 2024-06-20   -0.0071850671
## 2024-06-21    0.0094885839
## 2024-06-24    0.0191174248
## 2024-06-25   -0.0020322193
## 2024-06-26    0.0032895191
## 2024-06-27   -0.0021858423
## 2024-06-28   -0.0040681947
## 2024-07-01   -0.0058130831
## 2024-07-02   -0.0020543182
## 2024-07-03    0.0028503611
## 2024-07-05    0.0067897754
## 2024-07-08   -0.0125470398
## 2024-07-09   -0.0042884444
## 2024-07-10    0.0022332622
## 2024-07-11    0.0042972566
## 2024-07-12    0.0095087528
## 2024-07-15   -0.0045526046
## 2024-07-16    0.0135624791
## 2024-07-17    0.0146258362
## 2024-07-18   -0.0003066500
## 2024-07-19    0.0015339541
## 2024-07-22   -0.0079645316
## 2024-07-23    0.0029334947
## 2024-07-24    0.0130849521
## 2024-07-25    0.0039508000
## 2024-07-26    0.0148328040
## 2024-07-29   -0.0032811515
## 2024-07-30    0.0127188157
## 2024-07-31   -0.0138889249

Let create a dataframe where we have both the portfolios along with a change in columns names.

gsr<- as.data.frame(dailyReturn(GS))
appler<- as.data.frame(dailyReturn(AAPL))
JNJR<- as.data.frame(dailyReturn(JNJ))
xomr<- as.data.frame(dailyReturn(XOM))
pgr<- as.data.frame(dailyReturn(PG))
jpmr<- as.data.frame(dailyReturn(JPM))
MSFTR<- as.data.frame(dailyReturn(MSFT))
pfer<- as.data.frame(dailyReturn(PFE))
cvxr<- as.data.frame(dailyReturn(CVX))
kor<- as.data.frame(dailyReturn(KO))
b1<- dailyReturn(GSPC)
port1<-as.data.frame( c(gsr,appler,JNJR,xomr,pgr))
port2<- as.data.frame((c(jpmr,MSFTR,pfer,cvxr,kor)))
colnames(port1)<-c('GS','APPl','JNJ','XOM','PG')
colnames(port2)<- c('JPM','MSFT','PFE','CVX','KO')
port1
##               GS           APPl           JNJ           XOM            PG
## 1   0.0006093646 -0.00165113089  0.0164043774 -0.0190226583  0.0007349040
## 2   0.0131631220  0.02203187041 -0.0083343998  0.0018098689  0.0026927935
## 3   0.0129689652  0.05981625254 -0.0043356050 -0.0020646754  0.0037842431
## 4   0.0125291452 -0.00910676259 -0.0046225124  0.0064655172 -0.0001216361
## 5   0.0002929528  0.00379718869  0.0009422492 -0.0049678958  0.0080271956
## 6   0.0070978470  0.00186409873  0.0015465017 -0.0001721326 -0.0041625676
## 7   0.0192638665  0.01001423758  0.0060423577  0.0197158923  0.0058762096
## 8  -0.0018219040 -0.00823538068  0.0004003841 -0.0040527132  0.0048784200
## 9  -0.0025729848  0.01764542843  0.0087385600 -0.0004238337 -0.0058736047
## 10  0.0108254778  0.00617346953  0.0010580853 -0.0020354973 -0.0006631737
## 11  0.0166204879  0.01221794075  0.0085215566  0.0077335232  0.0045246140
## 12 -0.0033684639  0.00063248533  0.0105456254 -0.0059875112  0.0081076581
## 13  0.0068888579  0.00015802139  0.0023334237  0.0150165148 -0.0013106233
## 14 -0.0102197870  0.00616210143 -0.0217925190 -0.0081076665 -0.0019685149
## 15  0.0161360029  0.00685727001 -0.0001983128 -0.0069099158  0.0062160574
## 16 -0.0170702126 -0.00753840479  0.0149430999 -0.0201102685 -0.0032076538
## 17 -0.0091483432 -0.02105808875 -0.0247557202 -0.0170592411 -0.0138251123
## 18  0.0066135519  0.01658813551 -0.0182364448 -0.0007929166 -0.0009668479
## 19 -0.0029706300  0.00005268852 -0.0176226190  0.0126961952 -0.0134276973
## 20 -0.0057414674  0.00157896618  0.0004155531 -0.0107087180 -0.0089510558
## 21 -0.0151803536  0.00525513708  0.0058155381  0.0031681829  0.0057532197
## 22  0.0139705881  0.00501859348  0.0095677272  0.0286867649  0.0120555507
## 23 -0.0033732981  0.00925877128  0.0072953388 -0.0239638848  0.0006685342
## 24  0.0007032766  0.00164926726  0.0004061023 -0.0155526328  0.0143334388
## 25  0.0140127500  0.00782088483 -0.0123816089  0.0039940054 -0.0057481078
## 26 -0.0077542597 -0.00709654069  0.0030828043  0.0075141306  0.0145739122
## 27 -0.0069635503  0.01239203885  0.0045076060 -0.0107045820 -0.0083694643
## 28 -0.0029896372 -0.01914776923  0.0003399718  0.0029268455  0.0017359544
## 29 -0.0204608070  0.07264912559 -0.0025148533 -0.0080474323  0.0007767530
## 30  0.0099714662  0.02857839055 -0.0091986297 -0.0110546303 -0.0137330016
## 31 -0.0052373658  0.00549114436  0.0002750381 -0.0080230721  0.0074464889
## 32  0.0002464106 -0.00816840905  0.0006187442 -0.0084514749  0.0022834542
## 33  0.0083322163  0.01967147898  0.0028171202 -0.0068737970  0.0042568904
## 34  0.0161046695 -0.01098446902 -0.0020555194  0.0094130365  0.0063283436
## 35  0.0013553880 -0.02151290660  0.0146241330  0.0215761626 -0.0052800154
## 36 -0.0171815202 -0.01044442607  0.0065638194 -0.0087703217  0.0035187949
## 37  0.0264560928  0.00313265159  0.0024873621  0.0297038719  0.0011292193
## 38 -0.0101928012  0.00446818457 -0.0129425479  0.0028057842 -0.0094983133
## 39 -0.0033233187  0.01999326795 -0.0025137245  0.0003497501  0.0035959893
## 40 -0.0217171812  0.00398596063 -0.0069473111  0.0042828236 -0.0049567145
## 41  0.0142614046 -0.01625413773  0.0024691399  0.0019147190 -0.0102028388
## 42  0.0250707379  0.02910457233  0.0019157004 -0.0013898858 -0.0133397828
## 43  0.0042056284  0.01623992744 -0.0027998064 -0.0067849581  0.0072516757
## 44  0.0049612941  0.00581104442 -0.0023282636  0.0050797147 -0.0004270413
## 45 -0.0067746908  0.02162037111  0.0054224262 -0.0121122287  0.0084233954
## 46  0.0001506344  0.00653888403 -0.0068268708 -0.0104966253  0.0079292870
## 47  0.0172324322  0.00377484560  0.0107919121 -0.0110536447 -0.0051645483
## 48  0.0128165054  0.01880358225  0.0161849002  0.0088335650  0.0068815608
## 49  0.0007099675 -0.02322082798  0.0018068948  0.0118835048 -0.0075540152
## 50  0.0013563297  0.01305086728  0.0012024578  0.0001765708  0.0064637391
## 51  0.0257356130  0.01674330148 -0.0042700785  0.0171272402 -0.0121841352
## 52  0.0219206017  0.00179186621  0.0118600171  0.0072042517  0.0144002618
## 53 -0.0016699065 -0.02529598099  0.0368850243  0.0137883356  0.0149146783
## 54 -0.0318013490 -0.02053483095 -0.0074083769  0.0098606228 -0.0059017941
## 55 -0.0026326048  0.00057991296 -0.0046969228 -0.0229798256 -0.0028496540
## 56  0.0043511763 -0.00156029980 -0.0029090241 -0.0068924188  0.0017265615
## 57  0.0104919211  0.00468828256 -0.0122536263 -0.0161359680 -0.0096879934
## 58 -0.0110738225 -0.02875428371  0.0257958157  0.0141080894  0.0082823486
## 59  0.0102937726 -0.00480455672  0.0214998761  0.0210416322 -0.0065476554
## 60  0.0148868387  0.00216102446  0.0062640942 -0.0008515582  0.0132415027
## 61 -0.0126445255  0.00128463374 -0.0129482186 -0.0104832808  0.0048488680
## 62  0.0262827005  0.00256597115  0.0174697548  0.0178294550 -0.0484316841
## 63  0.0066446205  0.01499085344 -0.0215706669  0.0035541861 -0.0058132496
table.Stats(m1)
##                 daily.returns daily.returns.1 daily.returns.2 daily.returns.3
## Observations          63.0000         63.0000         63.0000         63.0000
## NAs                    0.0000          0.0000          0.0000          0.0000
## Minimum               -0.0318         -0.0288         -0.0248         -0.0240
## Quartile 1            -0.0043         -0.0032         -0.0045         -0.0083
## Median                 0.0014          0.0038          0.0009         -0.0004
## Arithmetic Mean        0.0029          0.0044          0.0010          0.0001
## Geometric Mean         0.0028          0.0043          0.0009          0.0000
## Quartile 3             0.0129          0.0123          0.0064          0.0076
## Maximum                0.0265          0.0726          0.0369          0.0297
## SE Mean                0.0016          0.0022          0.0014          0.0015
## LCL Mean (0.95)       -0.0003          0.0001         -0.0018         -0.0030
## UCL Mean (0.95)        0.0061          0.0088          0.0038          0.0032
## Variance               0.0002          0.0003          0.0001          0.0001
## Stdev                  0.0126          0.0172          0.0113          0.0122
## Skewness              -0.2221          1.1893          0.2709          0.3012
## Kurtosis              -0.2105          3.8479          0.9854         -0.3024
##                 daily.returns.4
## Observations            63.0000
## NAs                      0.0000
## Minimum                 -0.0484
## Quartile 1              -0.0058
## Median                   0.0008
## Arithmetic Mean         -0.0002
## Geometric Mean          -0.0002
## Quartile 3               0.0063
## Maximum                  0.0149
## SE Mean                  0.0012
## LCL Mean (0.95)         -0.0027
## UCL Mean (0.95)          0.0023
## Variance                 0.0001
## Stdev                    0.0099
## Skewness                -1.8032
## Kurtosis                 7.2615
table.Stats(m2)
##                 daily.returns daily.returns.1 daily.returns.2 daily.returns.3
## Observations          63.0000         63.0000         63.0000         63.0000
## NAs                    0.0000          0.0000          0.0000          0.0000
## Minimum               -0.0450         -0.0359         -0.0307         -0.0298
## Quartile 1            -0.0060         -0.0046         -0.0076         -0.0078
## Median                 0.0006          0.0022          0.0035          0.0006
## Arithmetic Mean        0.0017          0.0011          0.0025          0.0000
## Geometric Mean         0.0016          0.0010          0.0024          0.0000
## Quartile 3             0.0117          0.0080          0.0138          0.0059
## Maximum                0.0249          0.0222          0.0364          0.0260
## SE Mean                0.0017          0.0015          0.0019          0.0014
## LCL Mean (0.95)       -0.0016         -0.0020         -0.0013         -0.0028
## UCL Mean (0.95)        0.0050          0.0041          0.0062          0.0029
## Variance               0.0002          0.0001          0.0002          0.0001
## Stdev                  0.0132          0.0121          0.0149          0.0114
## Skewness              -0.8540         -0.8223         -0.1328          0.1978
## Kurtosis               1.4584          1.1043         -0.5527          0.0266
##                 daily.returns.4
## Observations            63.0000
## NAs                      0.0000
## Minimum                 -0.0144
## Quartile 1              -0.0035
## Median                   0.0011
## Arithmetic Mean          0.0012
## Geometric Mean           0.0012
## Quartile 3               0.0043
## Maximum                  0.0191
## SE Mean                  0.0009
## LCL Mean (0.95)         -0.0006
## UCL Mean (0.95)          0.0031
## Variance                 0.0001
## Stdev                    0.0073
## Skewness                 0.2822
## Kurtosis                 0.0289

Lets create multi-plots for both the portfolios

plot(port1)

plot(port2)

Risk Analytics

Lets startb with risk analysis we will calculate & visualize the following;

Lets start with running the weights of each stock

w1<- c(0.20,0.20,0.20,0.20,0.20)
w2<- c(0.20,0.20,0.20,0.20,0.20)

We create the performance summary chart

# portfolio returns
r2<-Return.portfolio(m2,weights = w2) 
r1<-Return.portfolio(m1, weights = w1)
r3<- cbind(r1,r2)
colnames(r3)<- c("Portfolio1", "Portfolio2")
charts.PerformanceSummary(r1) # Portfolio 1

charts.PerformanceSummary(r2) #Portfolio 2

Portfolio one has more cummulative returns

Standard Deviation of each stock & Portfolio

A measure of the dispersion of returns around the average.

sapply(port1, sd)
##          GS        APPl         JNJ         XOM          PG 
## 0.012646018 0.017227933 0.011265835 0.012203117 0.009851581
sapply(port2, sd)
##         JPM        MSFT         PFE         CVX          KO 
## 0.013194791 0.012094461 0.014896498 0.011404871 0.007337365
StdDev(r1) # Standard deviation of 1st portfolio
##               [,1]
## StdDev 0.006229384
StdDev(r2) # Standard deviation of 2nd portfolio
##               [,1]
## StdDev 0.006424229

Portfolio Covariance

Covariance measures the relationship between the returns of two assets.

#calculating intra-portfolio covariance
cov1<- cov(port1)
cov2<- cov(port2)
cov1
##                  GS            APPl            JNJ             XOM
## GS    0.00015992178  0.000038075488  0.00001754701  0.000055889271
## APPl  0.00003807549  0.000296801678 -0.00004634752 -0.000040549112
## JNJ   0.00001754701 -0.000046347519  0.00012691903  0.000036610389
## XOM   0.00005588927 -0.000040549112  0.00003661039  0.000148916075
## PG   -0.00001745396 -0.000001536586  0.00002306568 -0.000007533705
##                   PG
## GS   -0.000017453963
## APPl -0.000001536586
## JNJ   0.000023065676
## XOM  -0.000007533705
## PG    0.000097053646
cov2
##                 JPM           MSFT            PFE            CVX             KO
## JPM   0.00017410250 -0.00001617804  0.00004864279  0.00005838190  0.00001660993
## MSFT -0.00001617804  0.00014627599 -0.00001286670 -0.00003191048 -0.00001504970
## PFE   0.00004864279 -0.00001286670  0.00022190566  0.00002449304  0.00005712185
## CVX   0.00005838190 -0.00003191048  0.00002449304  0.00013007107  0.00002070341
## KO    0.00001660993 -0.00001504970  0.00005712185  0.00002070341  0.00005383693

Portfolio Volatility

A measure of the overall risk of a portfolio. It quantifies the dispersion of returns around the average portfolio return.

port1vol<- sqrt(t(w1)%*%cov1%*%w1)
port2vol<- sqrt(t(w2)%*%cov2%*%w2)
Portfolio<- (c('Portfolio1', 'Portfolio2'))

volatilitytable<- rbind(Portfolio, (c(port1vol,port2vol)))
volatilitytable
##           [,1]                  [,2]                 
## Portfolio "Portfolio1"          "Portfolio2"         
##           "0.00614864575178647" "0.00640652213795473"

Wealth Index

Charts illustrating the growth of the portfolio’s value over time.

d1<- Return.portfolio(m1, weights = w1, wealth.index = TRUE)
plot(d1)

d2 <- Return.portfolio(m2,weights = w2, wealth.index = TRUE)

plot(d2)

We clearly see that portfolio 1 has a better wealth index

Sharpe Ratio

Measures risk-adjusted returns, considering excess return per unit of risk.

sr1<-SharpeRatio(r1,Rf=0.0513)
sr2<- SharpeRatio(r2,Rf=0.0513)
sr1#portfolio1
##                                 portfolio.returns
## StdDev Sharpe (Rf=5.1%, p=95%):         -7.965504
## VaR Sharpe (Rf=5.1%, p=95%):            -5.664286
## ES Sharpe (Rf=5.1%, p=95%):             -4.113561
sr2#portfolio2
##                                 portfolio.returns
## StdDev Sharpe (Rf=5.1%, p=95%):         -7.787280
## VaR Sharpe (Rf=5.1%, p=95%):            -5.548292
## ES Sharpe (Rf=5.1%, p=95%):             -4.464520

The Sharpe ratio of portfolio 2 is better than portfolio 1 , therefore Portfolio 1 is less risk adjusted

Sortino Ratio

Similar to Sharpe, but focuses on downside risk, using the Minimum Acceptable Return.

SortinoRatio(r1, MAR = 0.0513) #Portfolio1
##                             portfolio.returns
## Sortino Ratio (MAR = 5.13%)        -0.9923338
SortinoRatio(r2, MAR= 0.0513) #Portfolio2
##                             portfolio.returns
## Sortino Ratio (MAR = 5.13%)        -0.9919832

The Sortino ratio of Portfolio 2 is higher than portfolio one, hence it is better risk adjusted

Treynor Ratio

Measures excess return per unit of systematic risk (beta)

TreynorRatio(r1,b1, Rf= 0.0513)
## [1] -2.434699
TreynorRatio(r2,b1, Rf=0.0513)
## [1] -5.5196

The Treynor ratio of portfolio 2 is more than portfolio 1 hence its better in terms of risk adjustment

Beta

Measures the portfolio’s sensitivity to market movements

CAPM.beta(r1,b1)
## [1] 0.4107273
CAPM.beta(r2,b1)
## [1] 0.1811721

Portfolio 1’s beta is more than that of its counterpart hence it is more volatile

Alpha

The excess return of a portfolio compared to its expected return based on beta.

CAPM.alpha(r1,b1)
## [1] 0.001060269
CAPM.alpha(r2,b1)
## [1] 0.0009994462

The alpha of portfolio 1 ia better than alpha of portfolio2, hence return of portfolio 1 is better than portfolio 2

Var

The maximum potential loss over a specified period at a given confidence level.

VaR(r1)
##     portfolio.returns
## VaR      -0.008760184
VaR(r2)
##     portfolio.returns
## VaR      -0.009016698

The Var of r1 is better than var of portfolio 2

Drawdown Table & Chart

A table and visual representation of the largest decline in portfolio value from a peak.

table.Drawdowns(r1)
##         From     Trough         To   Depth Length To Trough Recovery
## 1 2024-05-20 2024-05-29 2024-06-24 -0.0337     24         7       17
## 2 2024-07-18 2024-07-24       <NA> -0.0232     11         5       NA
## 3 2024-06-25 2024-06-28 2024-07-01 -0.0077      5         4        1
## 4 2024-07-11 2024-07-11 2024-07-12 -0.0047      2         1        1
## 5 2024-05-10 2024-05-10 2024-05-13 -0.0019      2         1        1
chart.Drawdown(r1)

table.Drawdowns(r2)
##         From     Trough         To   Depth Length To Trough Recovery
## 1 2024-05-16 2024-06-14 2024-06-24 -0.0223     26        21        5
## 2 2024-07-18 2024-07-24 2024-07-30 -0.0203      9         5        4
## 3 2024-07-31 2024-07-31       <NA> -0.0120      2         1       NA
## 4 2024-06-25 2024-06-26 2024-07-01 -0.0097      5         2        3
## 5 2024-07-08 2024-07-09 2024-07-10 -0.0082      3         2        1
chart.Drawdown(r2)

Portfolio 2 is more volatile than Portfolio 1

Rolling return

table.RollingPeriods(r3)
##                     Portfolio1 Portfolio2
## Last 12 day Average     0.0005     0.0008
## Last 36 day Average     0.0019     0.0011
## Last 60 day Average     0.0014     0.0011
## Last 12 day Std Dev     0.0068     0.0085
## Last 36 day Std Dev     0.0060     0.0065
## Last 60 day Std Dev     0.0061     0.0065
chart.RollingPerformance(r1)

chart.RollingPerformance(r2)

Portfolio 1 has more returns than portfolio 2

Conclusion: Portfolio 1 is more riskier than Portfolio 2 but it is more rewarding too.