0% found this document useful (0 votes)
171 views

Stock Return Simulation

This document summarizes the steps to simulate stock returns using a correlation matrix of 10 stocks. It: 1. Loads stock price data for 10 stocks from Yahoo Finance and calculates the daily returns. 2. Calculates the correlation matrix of the daily returns. 3. Performs an eigendecomposition of the correlation matrix to obtain eigenvalues and eigenvectors. 4. Generates random numbers from a log-normal distribution and uses the eigenvalues and eigenvectors to simulate new stock returns. 5. Compares the correlation matrix of the simulated returns to the original correlation matrix.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
171 views

Stock Return Simulation

This document summarizes the steps to simulate stock returns using a correlation matrix of 10 stocks. It: 1. Loads stock price data for 10 stocks from Yahoo Finance and calculates the daily returns. 2. Calculates the correlation matrix of the daily returns. 3. Performs an eigendecomposition of the correlation matrix to obtain eigenvalues and eigenvectors. 4. Generates random numbers from a log-normal distribution and uses the eigenvalues and eigenvectors to simulate new stock returns. 5. Compares the correlation matrix of the simulated returns to the original correlation matrix.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Stock Return Simulation

YIK LUN, KEI


[email protected]
www.allenkei.weebly.com

1. Generate two empty lists


suppressPackageStartupMessages(require(tseries))
suppressPackageStartupMessages(require(zoo))
suppressPackageStartupMessages(require(quantmod))
stock <- list()
daily.return<-list()

2. Download 10 stock price


options("getSymbols.warning4.0"=FALSE)
Symbols <- c('AAPL','QIHU','SOHU','BIDU','HSBC','YOKU','IBM','YHOO','AMZN','MSFT')
for (i in 1:length(Symbols)) {
stock[[i]] <- getSymbols(Symbols[i],from="2012-01-01",
src="yahoo",auto.assign=FALSE,return.class="xts")
}

3. Convert to daily return


for (i in 1:length(Symbols)) {
daily.return[[i]] <- periodReturn(stock[[i]],period='daily')
}
daily.return<-as.data.frame(daily.return)
colnames(daily.return)<-Symbols

4. Calculate correlation matrix


correlation<-cor(daily.return,daily.return)

5. Decomposition
Eigen=eigen(correlation)
eigenvector= as.matrix(Eigen$vectors)
eigenvalue = Eigen$values
1

6. Generate random number in log-normal distribution


R<-matrix(rep(rlnorm(10000000),10),ncol=10,nrow=10000000,byrow=T)

7. Simulation
newdata<-R %*% diag(sqrt(eigenvalue)) %*% t(eigenvector)

8. Compare two correlation matrices


new.correlation<-cor(newdata,newdata)
correlation
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##
##

AAPL
QIHU
SOHU
BIDU
1.00000000 0.09622307 0.05876803 0.1056816
0.09622307 1.00000000 0.43463630 0.3705914
0.05876803 0.43463630 1.00000000 0.4167501
0.10568162 0.37059139 0.41675006 1.0000000
0.10381480 0.24225355 0.32196257 0.2985237
0.06114784 0.32929659 0.43148538 0.3911863
0.13388650 0.15493087 0.20198385 0.2075162
0.08744170 0.27985911 0.33357160 0.3586587
0.09336454 0.21061102 0.25709530 0.2810099
0.12471310 0.15247588 0.20078006 0.2255432
IBM
YHOO
AMZN
MSFT
0.1338865 0.0874417 0.09336454 0.1247131
0.1549309 0.2798591 0.21061102 0.1524759
0.2019839 0.3335716 0.25709530 0.2007801
0.2075162 0.3586587 0.28100990 0.2255432
0.3953890 0.3157743 0.27959906 0.3534442
0.1574119 0.2936927 0.18050169 0.1754273
1.0000000 0.1907733 0.22175528 0.3831316
0.1907733 1.0000000 0.32710998 0.2294383
0.2217553 0.3271100 1.00000000 0.2569132
0.3831316 0.2294383 0.25691320 1.0000000

AAPL
QIHU
SOHU
BIDU
HSBC
YOKU
IBM
YHOO
AMZN
MSFT
AAPL
QIHU
SOHU
BIDU
HSBC
YOKU
IBM
YHOO
AMZN
MSFT

HSBC
0.1038148
0.2422536
0.3219626
0.2985237
1.0000000
0.2831943
0.3953890
0.3157743
0.2795991
0.3534442

YOKU
0.06114784
0.32929659
0.43148538
0.39118632
0.28319425
1.00000000
0.15741187
0.29369271
0.18050169
0.17542728

[,5]
0.1081834
0.2475255
0.3267138
0.3020627
1.0000000
0.2884299
0.4013749

[,6]
0.06327084
0.33379897
0.43587216
0.39719169
0.28842986
1.00000000
0.16097860

new.correlation
##
##
##
##
##
##
##
##

[1,]
[2,]
[3,]
[4,]
[5,]
[6,]
[7,]

[,1]
1.00000000
0.09599929
0.05997730
0.10692410
0.10818343
0.06327084
0.13806655

[,2]
0.09599929
1.00000000
0.43812119
0.37468322
0.24752553
0.33379897
0.15976877

[,3]
0.0599773
0.4381212
1.0000000
0.4221470
0.3267138
0.4358722
0.2062883

[,4]
0.1069241
0.3746832
0.4221470
1.0000000
0.3020627
0.3971917
0.2119332
2

## [8,] 0.09016909 0.28524879 0.3387757 0.3627132 0.3222641 0.29712673


## [9,] 0.09688435 0.21508407 0.2618384 0.2869597 0.2844181 0.18717570
## [10,] 0.13013001 0.15849620 0.2064812 0.2314643 0.3576413 0.18122335
##
[,7]
[,8]
[,9]
[,10]
## [1,] 0.1380665 0.09016909 0.09688435 0.1301300
## [2,] 0.1597688 0.28524879 0.21508407 0.1584962
## [3,] 0.2062883 0.33877568 0.26183837 0.2064812
## [4,] 0.2119332 0.36271317 0.28695965 0.2314643
## [5,] 0.4013749 0.32226407 0.28441809 0.3576413
## [6,] 0.1609786 0.29712673 0.18717570 0.1812233
## [7,] 1.0000000 0.19431355 0.22464064 0.3864480
## [8,] 0.1943135 1.00000000 0.32879745 0.2333101
## [9,] 0.2246406 0.32879745 1.00000000 0.2605368
## [10,] 0.3864480 0.23331009 0.26053675 1.0000000

You might also like