christou+diez - r investimentos
TRANSCRIPT
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 1/13
Statistical Finance for Investors
Unfamiliar with Quantitative Methods
Using stockPortfolio in R
Nicolas Christou
David DiezUCLA Department of Statistics
David Diez, Nicolas Christou
stockPortfolio in R
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 2/13
Overview
The stockPortfolio package is very easy to navigate. There are threesimple steps to select an optimal portfolio using three functions:
1 Download data (automated) using a vector of stock tickers and arange of dates.
> stockData <- getReturns(vectorOfTickers,
+ start="2004-09-01", end="2009-09-01")
2 Model the stocks in one of the four offered models.
> model1 <- stockModel(stockData)
> model2 <- stockModel(stockData, model="CCM")
...
3 Identify the optimal portfolio.
> optPort <- optimalPort(model1)
David Diez, Nicolas Christou
stockPortfolio in R
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 3/13
Stock Data Investment topics Modeling stocks Omitted
Obtaining data
Pick your stocks and get theirtickers in a vector (ticker).
Raw stock data are the stockprices .
The function getReturns
retrieves the adjusted closeprices fromhttp://finance.yahoo.com andcomputes the returns .
A return is just the percentgain/loss in decimal form:10.3% gain means a return of 0.103.
> ticker <- c("C", "IBM",
+ "JPM", "WFC")
> gR <- getReturns(ticker,
+ start="2005-01-01",
+ end="2009-10-01")
David Diez, Nicolas Christou
stockPortfolio in R
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 4/13
Stock Data Investment topics Modeling stocks Omitted
Function declaration: getReturns
getReturns(ticker,
freq = c("month", "week", "day"),
get = c("overlapOnly", "all"),
start = "1970-01-01",
end = NULL)
The default, overlapOnly, will return the stock returns for which allstocks had data and drop any dates with NA.
Warning: setting get="all" often results in problems with missingvalues.
David Diez, Nicolas Christou
stockPortfolio in R
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 5/13
Stock Data Investment topics Modeling stocks Omitted
Example
We will use the tickers from the stock data in stock94Info.
> data(stock94Info)
> ticker <- stock94Info$ticker
[1] "C" "KEY" "WFC" "JPM" "SO" "DUK"
[7] "D" "HE" "EIX" "LUV" "CAL" "AMR"[13] "AMGN" "GILD" "CELG" "GENZ" "BIIB" "CAT"
[19] "DE" "HIT" "IMO" "MRO" "HES" "YPF"
[25] "∧GSPC"
> ind <- stock94Info$industry # for later
> theData <- getReturns(ticker,+ start="2004-09-01", end="2009-09-01")
The print, summary, and plot methods can be applied to theData.
David Diez, Nicolas Christou
stockPortfolio in R
S O
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 6/13
Stock Data Investment topics Modeling stocks Omitted
Types of investments
Other investments also exist, and the stockPortfolio takes intoaccount: risk-free investments and short selling.
Argument name in stockPortfolio: Rf. The value of Rf isstandardized for the period, e.g. 3% annual return equates tosetting Rf=0.0025 for monthly data.
Short selling will be referred to via shortSelling in functionarguments, and it takes values "y" and "n".
David Diez, Nicolas Christou
stockPortfolio in R
St k D t I t t t i M d li t k O itt d
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 7/13
Stock Data Investment topics Modeling stocks Omitted
Modeling stocks
There are four models offered:
Variance-covariance (default). Use R̄ , Σ, and R f to select aportfolio that minimizes risk but maximizes return.
Constant correlation model (CCM). Smooth Σ and then dovariance covariance method.
Multigroup model (MGM). Compromise strategy: do somesmoothing on Σ (less than CCM) and then optimize.
Single index model (SIM). Use a linear model to analyze stockbehavior, where we regress stock returns against some stock index.
David Diez, Nicolas Christou
stockPortfolio in R
Stock Data Investment topics Modeling stocks Omitted
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 8/13
Stock Data Investment topics Modeling stocks Omitted
Implementation
The 25th ticker – the S&P500 – is dropped for the first three models.
> model1 <- stockModel(theData, drop=25)
> model2 <- stockModel(theData, drop=25, model="CCM")
> model3 <- stockModel(theData, drop=25, model="MGM",
+ industry=ind)
> model4 <- stockModel(theData, model="SIM", index=25)
By default, Rf=0 and shortSelling="y". Short selling is always
permitted when the model is the variance-covariance or multigroupmodel.
David Diez, Nicolas Christou
stockPortfolio in R
Stock Data Investment topics Modeling stocks Omitted
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 9/13
Stock Data Investment topics Modeling stocks Omitted
Single index model
The Single Index Model is the most well-known of the four models. If R M
describes the returns of the stock index (S&P500) and R i describes thereturns of stock i , then the linear model that relates the two:
R i = αi + β i R M + i
where αi and β i are constants and i is a vector of the model errors forstock i . Example where no short selling is permitted:
> sim <- stockModel(theData, model="SIM", index=25,+ industry=ind, shortSelling="n")
David Diez, Nicolas Christou
stockPortfolio in R
Stock Data Investment topics Modeling stocks Omitted
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 10/13
Stock Data Investment topics Modeling stocks Omitted
Finding the optimal portfolio
For any model, the goal isto minimize risk whilemaximizing return. Thereis a single function toidentify the optimal
portfolio of a model:optimalPort.
The first argument is anoutput of stockModel.
The next two argumentspermit adjustments to themodel (Rf andshortSelling).
> simOP <- optimalPort(sim)
> summary(simOP)
Model: single index modelExpected return: 0.0159
Risk estimate: 0.0399
> simOP
... same output as above ...
Portfolio allocation:
...
David Diez, Nicolas Christou
stockPortfolio in R
Stock Data Investment topics Modeling stocks Omitted
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 11/13
S p g O
Visualization of optimal portfolio
The optimal stock portfolio isshown by the black dot on theefficient frontier when no shortselling is permitted. Allocationshown by heat coloring.
> plot(simOP, xlim=c(0,0.2),+ ylim=0.06*c(-1,1))
> portPossCurve(sim, 10,
+ add=TRUE)
q
q
q
q
q
q
q
q
q
q
q
q
q q
q
q
q
0.00 0.05 0.10 0.15 0.20
− 0 . 0
6
− 0 . 0
4
− 0 . 0
2
0 . 0
0
0 . 0
2
0 . 0
4
0 . 0
6
Risk and Return of Stocks
Risk
R e t u r n
David Diez, Nicolas Christou
stockPortfolio in R
Stock Data Investment topics Modeling stocks Omitted
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 12/13
p g
Other topics
Finding the optimal allocation using one of the models wouldbe a relatively simple task using getReturns, stockModel, andoptimalPort. What was not covered:
Finer details of the models.Comparison of models (testPort is useful in this respect).Creation of portfolio clouds (portCloud) and portfolio possibilitiescurves (portPossCurve).
0.02 0.04 0.06 0.08 0.10 0.12
−0.05
0.00
0.05
Risk
R e t u r n
q
qq q q
q
q
q
q
q q
q
q
qq qq
David Diez, Nicolas Christou
stockPortfolio in R
Stock Data Investment topics Modeling stocks Omitted
5/8/2018 Christou+Diez - R Investimentos - slidepdf.com
http://slidepdf.com/reader/full/christoudiez-r-investimentos 13/13
Example of testPort
Before farewells, a brief examination of the utility of these models (usingtestPort).
time
R e t u r n
2005 2006 2007 2008 2009 2010
1 . 0
1 . 5
2 .
0
SIM, SS okay
SIM, no SSEqual All.
S&P500
David Diez, Nicolas Christou
stockPortfolio in R