20
Basic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Basic stock modeling stockPortfolio in R - david · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

  • Upload
    dodieu

  • View
    223

  • Download
    5

Embed Size (px)

Citation preview

Page 1: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Basic stock modelingstockPortfolio in R

David DiezUCLA Statistics

David Diez

stockPortfolio in R

Page 2: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Overview

There are three simple steps in stockPortfolio to identifying anoptimal portfolio:

1 Download data.

> stockData <- getReturns(vectorOfTickers,

+ start=’2004-09-01’, end=’2009-09-30’)

2 Model the stock behavior using the data.

> model1 <- stockModel(stockData)

> model2 <- stockModel(stockData, model=’CCM’)

...

3 Identify the optimal portfolio suggested by the model.

> optPort <- optimalPort(model1)

David Diez

stockPortfolio in R

Page 3: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Overview

Presentation topics:

Stock data, its format and where we get it.

Basic investment terminology.

Review several models offered in stockPortfolio.

Simple but useful plots for investing.

These topics will be discussed in the context of stockPortfolio.

David Diez

stockPortfolio in R

Page 4: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Obtaining data

We want stock returns, i.e. percentreturns.

Place stock tickers in a vector(ticker).

getReturns retrieves stockreturns from Yahoo Finance.

Returns are in decimal form,i.e. a 10.3% gain means areturn of 0.103.

> ticker <- c(’C’, ’BAC’,

+ ’JPM’, ’WFC’)

> gR <- getReturns(ticker,

+ start=’2005-01-01’,

+ end=’2009-10-01’)

David Diez

stockPortfolio in R

Page 5: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Frequency of observations

The getReturns function can get daily, weekly, or monthly data.

> byDay <- getReturns(ticker, ’d’, start=’2005-01-01’)

> byWk <- getReturns(ticker, ’w’, start=’2005-01-01’)

> byMth <- getReturns(ticker, ’m’, start=’2005-01-01’)

The default is monthly data.

Warning: daily downloads = lots of data = longer download time.

David Diez

stockPortfolio in R

Page 6: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

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)

Warning: setting get="all" often results in missing value problems.

David Diez

stockPortfolio in R

Page 7: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Example

Some tickers are available in the stock94Info data.

> data(stock94Info)

> (ticker <- stock94Info$ticker)[c(1:12, 25)]

[1] "C" "KEY" "WFC" "JPM" "SO" "DUK"

[7] "D" "HE" "EIX" "LUV" "CAL" "AMR"

[13] "∧GSPC"

> ind <- stock94Info$industry # for later

> theData <- getReturns(ticker,

+ start=’2004-09-31’, end=’2009-09-31’)

The print, summary, and plot methods can be applied to theData.

David Diez

stockPortfolio in R

Page 8: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Example, continued

The print, summary, and plot methods can be applied to theData:

> theData

... matrix of stock returns ...

> summary(theData)

25 stocks, observed once per month

between 2004-10-01 and 2009-09-01

C KEY WFC ...

Mean Return -0.016 -0.018 0.0076 ...

> plot(theData) # ugly, too many

stocks

> pairs(theData$R[,1:3])

C

−0.4 −0.2 0.0

●●

●●

●●

●●

●●

●●

●●

●●●● ●

●●

●●●●●●●●

● ●●

●●

●●●●●●

●●●

●● ●

−0.

6−

0.2

0.2

0.6

● ●

●●

●●

●●

●●

●●

● ●

●●●●●

●●

●●●● ●●●●

●●●●

●●●●

●●●●

●●●

●●●

−0.

4−

0.2

0.0

●●

●●●

●●

●●●●

●●●●●

●●

●●●●

●●●

●●●●

●●

KEY●

●●

● ●●

●●

●●● ●

●●●●

●●

●●

●●

●●

●●●

●●●●●

−0.6 −0.2 0.2 0.6

●●

● ●

●●

●●

●● ● ●

●●●●

●●

● ●●●●

●●

●●

●●●

●●●●

●●●●●●●●●

●●

●●

●●

●●

●●● ●

●●●

●●●● ●

●●

●●

●●●●

●●

●● ●●●●●

●●●

●●

−0.2 0.0 0.2 0.4

−0.

20.

00.

20.

4

WFC

David Diez

stockPortfolio in R

Page 9: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Risk-free investments

Other investments also exist, and the stockPortfolio takes one intoaccount: risk-free investments. (Nearly) risk-free investments exist as

Insured Certificates of Deposit

US Treasuries

Insured Savings Accounts

The risk-free rate of return, Rf , is the return attainable by these risk-freeinvestments.

Argument name in stockPortfolio: Rf. The value of Rf isstandardized for the period, e.g. 3% annual return equates to settingRf=0.0025 for monthly data.

David Diez

stockPortfolio in R

Page 10: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Short selling

A portfolio is the allocation of money to stocks.

Investors are also permitted to borrow stocks from other investors, sellthem, and use that money to invest in other stocks. This is called shortselling.

Short selling will be referred to via shortSelling in function arguments,and it takes values ’y’ and ’n’.

RIP Bear Stearns.

David Diez

stockPortfolio in R

Page 11: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Minimize risk, maximize return

To gain an idea of how to minimize risk and maximize return, a first stepis to look at two summary statistics:

R̄ vector of average returns for each stock.Σ variance-covariance matrix of returns.

Risk is quantified as the standard deviation of the return.

To minimize risk while maximizing reward, R̄ and Σ will be useful.

●●

●●●

●●

● ●

●●

●●

Risk

Ret

urn

0.05 0.10 0.15 0.20

−0.

020.

000.

02

● CELG

● GILD

C

David Diez

stockPortfolio in R

Page 12: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Portfolio possibiliites

By randomly construct many portfolios, a general view of theRisk-Return possibilities becomes apparent. The curve encapsulating theportfolio cloud (portCloud) is the portfolio possibilities curve(portPossCurve). The top half of this curve yields the best possibleinvestments: the efficient frontier.

0.02 0.04 0.06 0.08 0.10 0.12

−0.05

0.00

0.05

Risk

Ret

urn

●●● ● ●●

●●

● ●

●●

●● ●

David Diez

stockPortfolio in R

Page 13: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Modeling stocks

There are four models offered:

Variance covariance (default). Use R̄, Σ, and Rf 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

stockPortfolio in R

Page 14: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Implementation

The 25th “stock” – 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 alwayspermitted when the model is the variance-covariance or multigroupmodel.

David Diez

stockPortfolio in R

Page 15: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Single index model

The Single Index Model is the most well-known of the four models. If XM

describes the returns of the stock index and Xi describes the returns ofstock i , then we propose a linear model that relates the two:

Xi = αi + βiXM + ε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:

> data(stock94Info)

> sim <- stockModel(theData, model=’SIM’, index=25,

+ industry=ind, shortSelling=’n’)

David Diez

stockPortfolio in R

Page 16: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Examining a stockModel object

Basic information about a model iseasily accessible.

> summary(sim)Model: SIM

60 observations, each one month apart

Dates: 2004-10-01 to 2009-09-01

Short selling is not permitted

> plot(sim)

Stock colors correspond to theindustries.

●●

●●●●

● ●

●●

●●

RiskR

etur

n

0.05 0.10 0.15 0.20

−0.

020.

000.

02

David Diez

stockPortfolio in R

Page 17: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

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 optimalportfolio 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 model

Expected return: 0.0159

Risk estimate: 0.0399

> simOP

... same output as above ...

Portfolio allocation:

...

David Diez

stockPortfolio in R

Page 18: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

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,.2),

+ ylim=0.06*c(-1,1))

> portPossCurve(sim, 10,

+ add=TRUE)

> points(0, 0, pch=20, col=2)

●●

●●●● ●●

●●

● ●

●●

●● ●●

Risk

Ret

urn ●

0.00 0.05 0.10 0.15 0.20

−0.

050.

000.

05

David Diez

stockPortfolio in R

Page 19: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Omitted topics

Hopefully at this point creating a stock portfolio and finding the optimalallocation using one of the models would be a relatively simple task usinggetReturns, stockModel, and optimalPort. 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).

David Diez

stockPortfolio in R

Page 20: Basic stock modeling stockPortfolio in R - david  · PDF fileBasic stock modeling stockPortfolio in R David Diez UCLA Statistics David Diez stockPortfolio in R

Stock Data Investment topics Modeling stocks Omitted

Sample of testPort

Before farewells, a brief examination of the utility of these models (usingtestPort).

time

Ret

urn

2005 2006 2007 2008 2009 2010

1.0

1.5

2.0

SIM, SS okaySIM, no SSEqual All.S&P500

Models were fit annually and based on the most recent five years of data.

David Diez

stockPortfolio in R