<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Systematic Investor</title>
	<atom:link href="http://systematicinvestor.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://systematicinvestor.wordpress.com</link>
	<description>Systematic Investor Blog</description>
	<lastBuildDate>Wed, 22 Feb 2012 16:02:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='systematicinvestor.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Systematic Investor</title>
		<link>http://systematicinvestor.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://systematicinvestor.wordpress.com/osd.xml" title="Systematic Investor" />
	<atom:link rel='hub' href='http://systematicinvestor.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Multiple Factor Model – Building Risk Model</title>
		<link>http://systematicinvestor.wordpress.com/2012/02/21/multiple-factor-model-building-risk-model/</link>
		<comments>http://systematicinvestor.wordpress.com/2012/02/21/multiple-factor-model-building-risk-model/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 04:59:07 +0000</pubDate>
		<dc:creator>systematicinvestor</dc:creator>
				<category><![CDATA[Factor Model]]></category>
		<category><![CDATA[Factors]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Risk Measures]]></category>

		<guid isPermaLink="false">http://systematicinvestor.wordpress.com/?p=832</guid>
		<description><![CDATA[This is the fourth post in the series about Multiple Factor Models. I will build on the code presented in the prior post, Multiple Factor Model – Building CSFB Factors, and I will show how to build a multiple factor risk model. For an example of the multiple factor risk models, please read following references: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=832&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is the fourth post in the series about Multiple Factor Models. I will build on the code presented in the prior post, <a href="http://systematicinvestor.wordpress.com/2012/02/13/multiple-factor-model-building-csfb-factors/" target="_blank">Multiple Factor Model – Building CSFB Factors</a>, and I will show how to build a multiple factor risk model. For an example of the  multiple factor risk models, please read following references:</p>
<ul>
<li><a href="http://www.alacra.com/alacra/help/barra_handbook_US.pdf" target="_blank">MSCI Barra United States Equity Multi-Factor Model, page 101</a></li>
<li><a href="http://www.northinfo.com/documents/8.pdf" target="_blank">Northfield Fundamental Risk Model</a></li>
</ul>
<p>The outline of this post:</p>
<ul>
<li>Run cross sectional regression to estimate factor returns</li>
<li>Compute factor covariance using shrinkage estimator</li>
<li>Forecast stocks specific variances using GARCH(1,1)</li>
<li>Compute portfolio risk using multiple factor model and compare it to the historical standard deviation of portfolio returns.</li>
</ul>
<p>Let’s start by loading the CSFB factors that we saved at the end of the <a href="http://systematicinvestor.wordpress.com/2012/02/13/multiple-factor-model-building-csfb-factors/" target="_blank">prior post</a>. [If you are missing data.factors.Rdata file, please execute fm.all.factor.test() function first to create and save CSFB factors.] Next, I will run cross sectional regression to estimate factor returns.</p>
<p><pre class="brush: r;">
###############################################################################
# Load Systematic Investor Toolbox (SIT)
# http://systematicinvestor.wordpress.com/systematic-investor-toolbox/
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
    source(con)
close(con)
	#*****************************************************************
	# Load factor data that we saved at the end of the fm.all.factor.test functions
	#****************************************************************** 
	load.packages('quantmod,abind')	
		
	load(file='data.factors.Rdata')
		# remove Composite Average factor
		factors.avg = factors.avg[which(names(factors.avg) != 'AVG')]	
		
	#*****************************************************************
	# Run cross sectional regression to estimate factor returns
	#****************************************************************** 
	nperiods = nrow(next.month.ret)
	n = ncol(next.month.ret)
		
	# create sector dummy variables: binary 0/1 values for each sector
	nsectors = len(levels(sectors))	
	sectors.matrix = array(double(), c(nperiods, n, nsectors))
		dimnames(sectors.matrix)[[3]] = levels(sectors)		
	for(j in levels(sectors)) {
		sectors.matrix[,,j] = matrix(sectors == j,  nr=nperiods, nc=n, byrow=T)
	}
	
	# create matrix for each factor
	factors.matrix = abind(factors.avg, along = 3)		
	
	# combine sector dummies and all factors
	all.data = abind(sectors.matrix, factors.matrix)		
	
	# create betas and specific.return
	beta = all.data[,1,] * NA
	specific.return = next.month.ret * NA
		nfactors = ncol(beta)
		
	# append next.month.ret to all.data			
	all.data = abind(next.month.ret, all.data, along = 3)
		dimnames(all.data)[[3]][1] = 'Ret'
			
	# estimate betas (factor returns)
	for(t in 12:(nperiods-1)) {		
		temp = all.data[t:t,,]
		x = temp[,-c(1:2)]
		y = temp[,1]
		b = lm(y~x)$coefficients
		
		b[2:nsectors] = b[1] + b[2:nsectors]
		beta[(t+1),] = b		
		
		specific.return[(t+1),] = y - rowSums(temp[,-1] * matrix(b, n, nfactors, byrow=T), na.rm=T)	
	}
</pre></p>
<p>To estimate factor returns (betas), we solve for coefficients of the following multiple factor model:
<pre>
Ret = b1 * F1 + b2 * F2 + ... + bn * Fn + e
where
b1...bn are estimated factor returns
F1...Fn are factor exposures. I.e. sector dummies and CSFB factor exposures
e is stock specific return, not captured by factors F1...Fn</pre>
<p>Note that we cannot include the first sector dummy variable in the regression, otherwise we will get a linearly dependent relationship of the first sector dummy variable with all other sector dummy variables. The sector effect of the first sector dummy variable is absorbed into the intercept in the regression.</p>
<p>There are a few alternative ways of estimating this regression. For example, the robust linear model can be estimated with following code:<br />
<pre class="brush: r;">
	load.packages('MASS')
	temp = rlm(y~x)$coefficients
</pre></p>
<p>The quantile regression can can be estimated with following code:<br />
<pre class="brush: r;">
	load.packages('quantreg')
	temp = rq(y ~ x, tau = 0.5)$coefficients
</pre></p>
<p>Next let’s look at the cumulative factor returns.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# helper function
	#****************************************************************** 	
	fm.hist.plot &lt;- function(temp, smain=NULL) {			
		ntemp = ncol(temp)		
		cols = plota.colors(ntemp)	
		plota(temp, ylim = range(temp), log='y', main=smain)
		for(i in 1:ntemp) plota.lines(temp[,i], col=cols[i])
		plota.legend(colnames(temp), cols, as.list(temp))
	}

	#*****************************************************************
	# Examine historical cumulative factor returns
	#****************************************************************** 	
	temp = make.xts(beta, index(next.month.ret))
		temp = temp['2000::',]
		temp[] = apply(coredata(temp), 2, function(x) cumprod(1 + ifna(x,0)))
	
	fm.hist.plot(temp[,-c(1:nsectors)], 'Factor Returns')
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/02/plot1-small2.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/02/plot1-small2.png?w=600&#038;h=500" alt="" title="plot1.png.small" width="600" height="500" class="alignnone size-full wp-image-838" /></a></p>
<p>The Price Reversals(PR) and Small Size(SS) factors have done well. </p>
<p>Next let’s estimate the factor covariance matrix over the rolling 24 month window.</p>
<p><pre class="brush: r;">
	load.packages('BurStFin')	
	factor.covariance = array(double(), c(nperiods, nfactors, nfactors))
		dimnames(factor.covariance)[[2]] = colnames(beta)
		dimnames(factor.covariance)[[3]] = colnames(beta)

	# estimate factor covariance
	for(t in 36:(nperiods-1)) {
		factor.covariance[t,,] = var.shrink.eqcor(beta[(t-23):t,])
	}
</pre></p>
<p>Next let’s forecast stocks specific variances using GARCH(1,1). I will use the GARCH estimation routine described in the <a href="http://systematicinvestor.wordpress.com/2012/01/06/trading-using-garch-volatility-forecast/" target="_blank">Trading using Garch Volatility Forecast</a> post.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Compute stocks specific variance foreasts using GARCH(1,1)
	#****************************************************************** 	
	load.packages('tseries,fGarch')	

	specific.variance = next.month.ret * NA

	for(i in 1:n) {
		specific.variance[,i] = bt.forecast.garch.volatility(specific.return[,i], 24) 
	}
</pre></p>
<p>Now we have all the ingredients to compute a portfolio risk:
<pre>
Portfolio Risk = (common factor variance + specific variance)^0.5
	common factor variance = (portfolio factor exposure) * factor covariance matrix * (portfolio factor exposure)'
	specific variance = (specific.variance)^2 * (portfolio weights)^2</pre>
<p><pre class="brush: r;">
	#*****************************************************************
	# Compute portfolio risk
	#****************************************************************** 
	portfolio = rep(1/n, n)
		portfolio = matrix(portfolio, n, nfactors)
	
	portfolio.risk = next.month.ret[,1] * NA
	for(t in 36:(nperiods-1)) {	
		portfolio.exposure = colSums(portfolio * all.data[t,,-1], na.rm=T)
		
		portfolio.risk[t] = sqrt(
			portfolio.exposure %*% factor.covariance[t,,] %*% (portfolio.exposure) + 
			sum(specific.variance[t,]^2 * portfolio[,1]^2, na.rm=T)
			)
	}
</pre></p>
<p>Next let’s compare portfolio risk estimated using multiple factor risk model with portfolio historical risk.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Compute historical portfolio risk
	#****************************************************************** 
	portfolio = rep(1/n, n)
		portfolio = t(matrix(portfolio, n, nperiods))
	
	portfolio.returns = next.month.ret[,1] * NA
		portfolio.returns[] = rowSums(mlag(next.month.ret) * portfolio, na.rm=T)
	
	hist.portfolio.risk = runSD(portfolio.returns, 24)
	
	#*****************************************************************
	# Plot risks
	#****************************************************************** 			
	plota(portfolio.risk['2000::',], type='l')
		plota.lines(hist.portfolio.risk, col='blue')
		plota.legend('Risk,Historical Risk', 'black,blue')
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/02/plot2-small3.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/02/plot2-small3.png?w=600&#038;h=500" alt="" title="plot2.png.small" width="600" height="500" class="alignnone size-full wp-image-837" /></a></p>
<p>The multiple factor risk model does a decent job of estimating portfolio risk most of the time.</p>
<p>To view the complete source code for this example, please have a look at the <a href="https://github.com/systematicinvestor/SIT/blob/master/R/factor.model.test.r" target="_blank">fm.risk.model.test() function in factor.model.test.r at github</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/systematicinvestor.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/systematicinvestor.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/systematicinvestor.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/systematicinvestor.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/systematicinvestor.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/systematicinvestor.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/systematicinvestor.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/systematicinvestor.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/systematicinvestor.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/systematicinvestor.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/systematicinvestor.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/systematicinvestor.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/systematicinvestor.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/systematicinvestor.wordpress.com/832/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=832&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://systematicinvestor.wordpress.com/2012/02/21/multiple-factor-model-building-risk-model/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f5676a7cfc17017cd99e8266c814227b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">systematicinvestor</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/02/plot1-small2.png" medium="image">
			<media:title type="html">plot1.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/02/plot2-small3.png" medium="image">
			<media:title type="html">plot2.png.small</media:title>
		</media:content>
	</item>
		<item>
		<title>Multiple Factor Model – Building CSFB Factors</title>
		<link>http://systematicinvestor.wordpress.com/2012/02/13/multiple-factor-model-building-csfb-factors/</link>
		<comments>http://systematicinvestor.wordpress.com/2012/02/13/multiple-factor-model-building-csfb-factors/#comments</comments>
		<pubDate>Mon, 13 Feb 2012 04:39:53 +0000</pubDate>
		<dc:creator>systematicinvestor</dc:creator>
				<category><![CDATA[Factor Model]]></category>
		<category><![CDATA[Factors]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Strategy]]></category>
		<category><![CDATA[Trading Strategies]]></category>

		<guid isPermaLink="false">http://systematicinvestor.wordpress.com/?p=815</guid>
		<description><![CDATA[This is the third post in the series about Multiple Factor Models. I will build on the code presented in the prior post, Multiple Factor Model – Building Fundamental Factors, and I will show how to build majority of factors described in the CSFB Alpha Factor Framework. For details of the CSFB Alpha Factor Framework [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=815&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is the third post in the series about Multiple Factor Models. I will build on the code presented in the prior post, <a href="http://systematicinvestor.wordpress.com/2012/02/04/multiple-factor-model-building-fundamental-factors/" target="_blank">Multiple Factor Model – Building Fundamental Factors</a>, and I will show how to build majority of factors described in the CSFB Alpha Factor Framework. For details of the CSFB Alpha Factor Framework please read <a href="http://www.scribd.com/mobile/documents/62210581/download?commit=Download+Now&amp;secret_password=" target="_blank">CSFB Quantitative Research, Alpha Factor Framework on page 11, page 49 by P. N. Patel, S. Yao, R. Carlson, A. Banerji, J. Handelman</a>.</p>
<p>This post will include long sections of code to extract/format data and build factors. I created a few helper functions for data manipulations and visualizations in the <a href="https://github.com/systematicinvestor/SIT/blob/master/R/factor.model.r" target="_blank">factor.model.r at github</a>. For example, consecutive.changes function counts the number of consecutive positive changes.</p>
<p>The outline of this post:</p>
<ul>
<li>Create majority of CSFB factors</li>
<li>Create and test Composite Average factor</li>
<li>Run cross sectional regression to estimate factor loading</li>
<li>Create and test Alpha model using estimated factor loading</li>
</ul>
<p>Let&#8217;s start by getting Fundamental data and creating factors. In the prior post, I mentioned that it takes a while to download historical fundamental data for all companies in the Dow Jones index, and I recommend saving fundamental data with save(data.fund, file=’data.fund.Rdata’) command. In the following code I will just load historical fundamental data with load(file=’data.fund.Rdata’) command instead of downloading all data again.</p>
<p><pre class="brush: r;">
###############################################################################
# Load Systematic Investor Toolbox (SIT)
# http://systematicinvestor.wordpress.com/systematic-investor-toolbox/
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
    source(con)
close(con)

	#*****************************************************************
	# Find Sectors for each company in DOW 30
	#****************************************************************** 
	tickers = spl('XLY,XLP,XLE,XLF,XLV,XLI,XLB,XLK,XLU')
	tickers.desc = spl('ConsumerCyclicals,ConsumerStaples,Energy,Financials,HealthCare,Industrials,Materials,Technology,Utilities')
	
	sector.map = c()
	for(i in 1:len(tickers)) {
		sector.map = rbind(sector.map, 
				cbind(sector.spdr.components(tickers[i]), tickers.desc[i])
			)
	}
	colnames(sector.map) = spl('ticker,sector')

	#*****************************************************************
	# Load historical data
	#****************************************************************** 
	load.packages('quantmod,abind')		
	tickers = dow.jones.components()
	
	sectors = factor(sector.map[ match(tickers, sector.map[,'ticker']), 'sector'])
		names(sectors) = tickers
	
	# get fundamental data
	load(file='data.fund.Rdata')
			
	# get pricing data
	load(file='data.Rdata')
	
	#*****************************************************************
	# Combine fundamental and pricing data
	#****************************************************************** 	
	for(i in tickers) {
		fund = data.fund[[i]]
		fund.date = date.fund.data(fund)
			nperiods = ncol(fund)
			
		# D - holds all data to be merged with pricing data
		D = list()
		
		#--------------------------------------------------------------
		# Data for Traditional and Relative Value	
		#--------------------------------------------------------------
				
		# Earnings per Share		
		D$EPS = get.fund.data('Diluted EPS from Total Operations', fund, fund.date, is.12m.rolling=T)
		
		# Sales, exception not available for financial service firms
		D$SALE = get.fund.data('total revenue', fund, fund.date, is.12m.rolling=T)
		
		# Common Shares Outstanding
		D$CSHO = get.fund.data('total common shares out', fund, fund.date)

		# Common Equity
		D$CEQ = get.fund.data('total equity', fund, fund.date)

		# Dividends
		D$DV.PS = get.fund.data('dividends paid per share', fund, fund.date, is.12m.rolling=T)
				
		# Cash Flow
		D$CFL = get.fund.data('net cash from operating activities', fund, fund.date, cash.flow=T, is.12m.rolling=T)
			
		#--------------------------------------------------------------
		# Data for Historical Growth	
		#--------------------------------------------------------------
				
		# Consecutive Quarters of Positive Changes in Trailing 12 Month Cash Flow
		D$CFL.CON.CHG = consecutive.changes(D$CFL)		
			# check
			#merge(D$CFL, sign(diff(D$CFL)), consecutive.changes(D$CFL), consecutive.changes(D$CFL,F))
		
		# Consecutive Quarters of Positive Change in Quarterly Earnings
		D$EPS.CON.CHG = consecutive.changes(D$EPS)
		
		# 12 Month Change in Quarterly Cash Flow
		temp = get.fund.data('net cash from operating activities', fund, fund.date, cash.flow=T)
		D$CFL.CHG = temp / mlag(temp,4)
		
		# 3 Year Average Annual Sales Growth
		D$SALE.3YR.GR = D$SALE
		if(!all(is.na(D$SALE))) D$SALE.3YR.GR = SMA(ifna(D$SALE / mlag(D$SALE,4) - 1,NA), 3*4)

		# 3 Year Average Annual Earnings Growth
		D$EPS.3YR.GR = SMA(D$EPS / mlag(D$EPS,4) - 1, 3*4)
		
		# 12 Quarter Trendline in Trailing 12 Month Earnings		
		D$EPS.TREND = D$EPS * NA
			D$EPS.TREND[12:nperiods] = sapply(12:nperiods, function(i) beta.degree(ols(cbind(1,1:12), D$EPS[(i-12+1):i])$coefficients[2]))
			
		# Slope of Trend Line Through Last 4 Quarters of Trailing 12M Cash Flows		
		D$CFL.TREND = D$CFL * NA
			D$CFL.TREND[4:nperiods] = sapply(4:nperiods, function(i) beta.degree(ols(cbind(1,1:4), D$CFL[(i-4+1):i])$coefficients[2]))

		#--------------------------------------------------------------
		# Data for Profit Trends	
		#--------------------------------------------------------------
		RECT = get.fund.data('receivables', fund, fund.date)
		INVT = get.fund.data('inventories', fund, fund.date)
		
		D$AT = get.fund.data('total assets', fund, fund.date)
		XSGA = get.fund.data('Selling, General &amp; Administrative (SG&amp;A) Expense', fund, fund.date, is.12m.rolling=T)
		
		# Consecutive Quarters of Declines in (Receivables+Inventories) / Sales
		D$RS.CON.CHG = consecutive.changes((RECT + INVT) / D$SALE, F)
				
		# Consecutive Qtrs of Positive Change in Trailing 12M Cash Flow / Sales
		D$CS.CON.CHG = consecutive.changes(D$CFL/D$SALE)
		
		# Overhead = sales, general and administrative costs
		# Consecutive Quarters of Declines in Trailing 12 Month Overhead / Sales
		D$OS.CON.CHG = consecutive.changes(XSGA/D$SALE, F)
				
		# (Industry Relative) Trailing 12 Month (Receivables+Inventories) / Sales
		D$RS = (RECT + INVT) / D$SALE
		
		# (Industry Relative) Trailing 12 Month Sales / Assets
		D$SA = D$SALE / D$AT
		
		# Trailing 12 Month Overhead / Sales
		D$OS = XSGA / D$SALE
		
		# Trailing 12 Month Earnings / Sales
		D$ES = D$EPS / D$SALE						
							
		#--------------------------------------------------------------
		# Merge Fundamental and Pricing data
		#--------------------------------------------------------------
		
		# merge	
		data[[i]] = merge(data[[i]], as.xts(abind(D,along=2), fund.date))						
	}
	
	bt.prep(data, align='keep.all', dates='1995::2011')

	#*****************************************************************
	# Create Factors
	#****************************************************************** 
	prices = data$prices	
		prices = bt.apply.matrix(prices, function(x) ifna.prev(x))
		
	# re-map sectors
	sectors	= sectors[colnames(prices)]	

	# create factors
	factors = list()
	factor.names = list()	
</pre></p>
<p>Next let’s create majority of CSFB factors.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Create Traditional Value
	#****************************************************************** 
	factors$TV = list()
	factor.names$TV = 'Traditional Value'

	# Market Value - capitalization
	CSHO =  bt.apply(data, function(x) ifna.prev(x[, 'CSHO']))
	MKVAL = prices * CSHO
	
	# Price / Earnings
	EPS = bt.apply(data, function(x) ifna.prev(x[, 'EPS']))
	factors$TV$EP = EPS / prices
	
	# Price / Trailing Sales
	SALE = bt.apply(data, function(x) ifna.prev(x[, 'SALE']))	
	factors$TV$SP = SALE / MKVAL
	
	# Price / Trailing Cash Flow
	CFL = bt.apply(data, function(x) ifna.prev(x[, 'CFL']))
	factors$TV$CFP = CFL / MKVAL
	
	# Dividend Yield
	DV.PS = bt.apply(data, function(x) ifna.prev(x[, 'DV.PS']))
	factors$TV$DY = DV.PS / prices
	
	# Price / Book Value		
	CEQ = bt.apply(data, function(x) ifna.prev(x[, 'CEQ']))
	factors$TV$BP = CEQ	/ MKVAL

	# Eliminate Price/Sales and Price/Cash Flow for financial firms
	factors$TV$SP[, sectors == 'Financials'] = NA
	factors$TV$CFP[, sectors == 'Financials'] = NA
	
	#*****************************************************************
	# Create Historical Growth
	#****************************************************************** 
	factors$HG = list()
	factor.names$HG = 'Historical Growth'

	for(i in spl('CFL.CON.CHG,EPS.CON.CHG,CFL.CHG,SALE.3YR.GR,EPS.3YR.GR,EPS.TREND,CFL.TREND')) {
		factors$HG[[i]] = bt.apply(data, function(x) ifna.prev(x[, i]))
	}

	#*****************************************************************
	# Create Profit Trends
	#****************************************************************** 
	factors$PT = list()		
	factor.names$PT = 'Profit Trends'	
	
	for(i in spl('RS.CON.CHG,CS.CON.CHG,OS.CON.CHG,RS,SA,OS,ES')) {
		factors$PT[[i]] = bt.apply(data, function(x) ifna.prev(x[, i]))
	}
	
	#*****************************************************************
	# Create Price Momentum
	#****************************************************************** 
	factors$PM = list()
	factor.names$PM = 'Price Momentum'	
	
	# find week ends
	week.ends = endpoints(prices, 'weeks')
		week.prices = prices[week.ends,]
		week.nperiods = nrow(week.prices)
	
	#Slope of 52 Week Trend Line
	factors$PM$S52W.TREND = bt.apply.matrix(week.prices, function(x) {
		c(rep(NA,51),
		sapply(52:week.nperiods, function(i) beta.degree(ols(cbind(1,1:52), x[(i - 52 + 1):i])$coefficients[2]))
		)})
	
	#4/52 Week Price Oscillator
	factors$PM$PP04.52W = bt.apply.matrix(week.prices, EMA, 4) / bt.apply.matrix(week.prices, EMA, 52)
					
	#39 Week Return
	factors$PM$R39W = week.prices / mlag(week.prices, 39)
			
	#51 Week Volume Price Trend
	# compute weekly volume
	temp = bt.apply(data, function(x) cumsum(ifna(Vo(x),0)))
	temp = temp[week.ends,]
		week.volume = temp - mlag(temp)		
	temp = (week.prices - mlag(week.prices)) * week.volume
	factors$PM$VPT51W = bt.apply.matrix(temp, runSum, 51)
			
	# Convert weekly to daily
	for(i in 1:len(factors$PM)) {
		temp = prices * NA
		temp[week.ends,] = factors$PM[[i]]
		factors$PM[[i]] = bt.apply.matrix(temp, function(x) ifna.prev(x))
	}
	
	#Percent Above 260 Day Low
	factors$PM$P260LOW = prices / bt.apply.matrix(prices, runMin, 260)
	
	# Flip sign
	for(i in names(factors$PM)) factors$PM[[i]] = -factors$PM[[i]]
	
	#*****************************************************************
	# Create Price Reversal
	#****************************************************************** 
	factors$PR = list()
	factor.names$PR = 'Price Reversal'	
		
	#5 Day Industry Relative Return
	factors$PR$r5DR = prices/mlag(prices, 5)
		factors$PR$r5DR = factors$PR$r5DR / sector.mean(factors$PR$r5DR, sectors)
	
	#5 Day Money Flow / Volume
	factors$PR$MFV = bt.apply(data, function(x) {
		MFI(cbind(ifna.prev(Hi(x)),ifna.prev(Lo(x)),ifna.prev(Cl(x))), 5) / ifna.prev(Vo(x))
	})
			
	#10 Day MACD - Signal Line
	factors$PR$MACD = bt.apply.matrix(prices, function(x) {
		temp=MACD(x, 10)
		temp[, 'macd'] - temp[, 'signal']
		})		
	
	#14 Day RSI (Relative Strength Indicator)
	factors$PR$RSI = bt.apply.matrix(prices, RSI, 14)
			
	#14 Day Stochastic
	factors$PR$STOCH = bt.apply(data, function(x) {
		stoch(cbind(ifna.prev(Hi(x)),ifna.prev(Lo(x)),ifna.prev(Cl(x))),14)[,'slowD']
	})
		
	#4 Week Industry Relative Return
	factors$PR$rR4W = week.prices / mlag(week.prices,4)
		factors$PR$rR4W = factors$PR$rR4W / sector.mean(factors$PR$rR4W, sectors)
		
		# Convert weekly to daily
		temp = prices * NA
		temp[week.ends,] = factors$PR$rR4W
		factors$PR$rR4W = bt.apply.matrix(temp, function(x) ifna.prev(x))
	
	
	# VOMO - Volume x Momentum
	volume = bt.apply(data, function(x) ifna.prev(Vo(x)))
	factors$PR$VOMO = (prices / mlag(prices,10) - 1) * bt.apply.matrix(volume, runMean, 22) / bt.apply.matrix(volume, runMean, 66)
	
	# Flip sign
	for(i in names(factors$PR)) factors$PR[[i]] = -factors$PR[[i]]
	
	#*****************************************************************
	# Create Small Size
	#****************************************************************** 		
	factors$SS = list()
	factor.names$SS = 'Small Size'		
	
	#Log of Market Capitalization
	factors$SS$MC = log(MKVAL)
	
	#Log of Market Capitalization Cubed
	factors$SS$MC3 = log(MKVAL)^3
	
	#Log of Stock Price
	factors$SS$P = log(prices)
	
	#Log of Total Assets
	factors$SS$AT = log(bt.apply(data, function(x) ifna.prev(x[, 'AT'])))
	
	#Log of Trailing-12-Month Sales
	factors$SS$SALE = log(bt.apply(data, function(x) ifna.prev(x[, 'SALE'])))

	# Flip sign
	for(i in names(factors$SS)) factors$SS[[i]] = -factors$SS[[i]]
	
	#*****************************************************************
	# Convert to monthly
	#****************************************************************** 
	# find month ends
	month.ends = endpoints(prices, 'months')
	
	prices = prices[month.ends,]
		n = ncol(prices)
		nperiods = nrow(prices)
	
	ret = prices / mlag(prices) - 1
		next.month.ret = mlag(ret, -1)
	
	MKVAL = MKVAL[month.ends,]
	
	for(j in 1:len(factors)) {	
		for(i in 1:len(factors[[j]])) {
			factors[[j]][[i]] = factors[[j]][[i]][month.ends,]	
			factors[[j]][[i]][] = ifna(factors[[j]][[i]],NA)
		}
	}

	#*****************************************************************
	# Create Relative Value
	#****************************************************************** 
	factors$RV = list()
	factor.names$RV = 'Relative Value'		
	
	# relative 
	for(i in spl('EP,SP,CFP')) {
		factors$RV[[paste('r',i,sep='')]] = factors$TV[[i]] / sector.mean(factors$TV[[i]], sectors) 		
	}
		
	# spreads, 5 Year Avg = 60 months
	for(i in spl('rEP,rSP,rCFP')) {
		factors$RV[[paste('s',i,sep='')]] = factors$RV[[i]] - 
		apply(factors$RV[[i]], 2, function(x) if(all(is.na(x))) x else SMA(x,60) )
	}
	
	#*****************************************************************
	# Profit Trends (Relative)
	#****************************************************************** 	
	# relative 
	for(i in spl('RS,SA')) {
		factors$PT[[paste('r',i,sep='')]] = factors$PT[[i]] / sector.mean(factors$PT[[i]], sectors)
	}			
</pre></p>
<p>Next let’s create Composite Average factor and chart its performance.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Normalize and add Average factor
	#****************************************************************** 
	for(j in names(factors)) {
		factors[[j]] = normalize.normal(factors[[j]])
		factors[[j]] = add.avg.factor(factors[[j]])
	}

	#*****************************************************************
	# Create Composite Average factor
	#****************************************************************** 	
	factors.avg = list()
	for(j in names(factors)) factors.avg[[j]] = factors[[j]]$AVG
	
	factors.avg = add.avg.factor(factors.avg)

	#*****************************************************************
	# Backtest qutiles and qutile spread
	#****************************************************************** 
	plot.quantiles(factors.avg, next.month.ret, 'Average')

	plot.bt.quantiles(factors.avg$AVG, next.month.ret, 'Composite Average', data)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/02/plot1-small1.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/02/plot1-small1.png?w=600&#038;h=800" alt="" title="plot1.png.small" width="600" height="800" class="alignnone size-full wp-image-819" /></a></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/02/plot2-small2.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/02/plot2-small2.png?w=600&#038;h=500" alt="" title="plot2.png.small" width="600" height="500" class="alignnone size-full wp-image-820" /></a></p>
<p>Please note that I&#8217;m using the current Dow Jones index components through out the whole history. This is a problem because the Dow Jones index changed its composition a few times in the last 20 years. One of the signs of this bias is high spread for Small Size group of factors. It is not obvious that buying low priced stocks and selling high priced stocks should consistently make money; but it makes sense, if we know beforehand that that low priced companies will be part of Dow Jones index at some point.</p>
<p>Instead of using a simple average of all factors to rank stocks, we can run cross sectional regression to estimate factor loading, and create Alpha model using estimated factor loading. For a complete description of this process, I recommend reading <a href="http://www.quantitativeinvestment.com/documents/common.pdf" target="_blank">Commonality In The Determinants Of Expected Stock Returns by R. Haugen, N. Baker (1996)</a> pages 8-9.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Save CSFB factors to be used later to create multiple factor Risk Model
	#****************************************************************** 
	save(data, sectors, factors.avg, next.month.ret, file='data.factors.Rdata')
		# remove Composite Average factor
		factors.avg = factors.avg[which(names(factors.avg) != 'AVG')]

	#*****************************************************************
	# Run cross sectional regression and create Alpha model
	#****************************************************************** 
	nperiods = nrow(next.month.ret)
	n = ncol(next.month.ret)
			
	# create matrix for each factor
	factors.matrix = abind(factors.avg, along = 3)	
	all.data = factors.matrix
	
	# betas
	beta = all.data[,1,] * NA
	
	# append next.month.ret to all.data			
	all.data = abind(next.month.ret, all.data, along = 3)
		dimnames(all.data)[[3]][1] = 'Ret'
		
	# estimate betas (factor returns)
	for(t in 12:(nperiods-1)) {
		temp = all.data[t:t,,]
		x = temp[,-1]
		y = temp[,1]
		beta[(t+1),] = lm(y~x-1)$coefficients
	}
	
	
	# create Alpha return forecasts
	alpha = next.month.ret * NA

	for(t in 18:(nperiods-1)) {
		# average betas over the last 6 months
		coef = colMeans(beta[(t-5):t,],na.rm=T)
		alpha[t,] = rowSums(all.data[t,,-1] * t(repmat(coef, 1,n)), na.rm=T)	
	}
	
	#*****************************************************************
	# Backtest qutiles and qutile spread
	#****************************************************************** 
	layout(1:2)
	temp = compute.quantiles(alpha, next.month.ret, plot=T)
	plot.bt.quantiles(alpha, next.month.ret, 'Alpha', data)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/02/plot3-small.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/02/plot3-small.png?w=600&#038;h=800" alt="" title="plot3.png.small" width="600" height="800" class="alignnone size-full wp-image-821" /></a></p>
<p>The performance of the regression model lags the performance of the simple average of all factors to rank stocks. There are might be many reasons for this, but I want to show you one quick and rational way to increase performance of the regression model.</p>
<p>We do not restrict estimated factor loadings during regression; however, a negative coefficient for a Value factor does not make sense. I don&#8217;t want explicitly say that good Value companies should have lower ranks than bad Value companies, just because there is a negative coefficient for a Value factor. One possible solution is to only use factor loadings that are positive to construct Alpha score. Here is the modified code:</p>
<p><pre class="brush: r;">
	for(t in 18:(nperiods-1)) {
		# average betas over the last 6 months
		coef = colMeans(beta[(t-5):t,],na.rm=T)

		coef = iif(coef &gt; 0, coef, 0)

		alpha[t,] = rowSums(all.data[t,,-1] * t(repmat(coef, 1,n)), na.rm=T)	
	}
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/02/plot4-small.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/02/plot4-small.png?w=600&#038;h=800" alt="" title="plot4.png.small" width="600" height="800" class="alignnone size-full wp-image-818" /></a></p>
<p>The regression model still lags the performance of the simple average of all factors to rank stocks.</p>
<p>In the next post I will show how to create a risk model and estimate risk.</p>
<p>To view the complete source code for this example, please have a look at the <a href="https://github.com/systematicinvestor/SIT/blob/master/R/factor.model.test.r" target="_blank">fm.all.factor.test() function in factor.model.test.r at github</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/systematicinvestor.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/systematicinvestor.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/systematicinvestor.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/systematicinvestor.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/systematicinvestor.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/systematicinvestor.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/systematicinvestor.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/systematicinvestor.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/systematicinvestor.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/systematicinvestor.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/systematicinvestor.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/systematicinvestor.wordpress.com/815/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/systematicinvestor.wordpress.com/815/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/systematicinvestor.wordpress.com/815/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=815&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://systematicinvestor.wordpress.com/2012/02/13/multiple-factor-model-building-csfb-factors/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f5676a7cfc17017cd99e8266c814227b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">systematicinvestor</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/02/plot1-small1.png" medium="image">
			<media:title type="html">plot1.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/02/plot2-small2.png" medium="image">
			<media:title type="html">plot2.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/02/plot3-small.png" medium="image">
			<media:title type="html">plot3.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/02/plot4-small.png" medium="image">
			<media:title type="html">plot4.png.small</media:title>
		</media:content>
	</item>
		<item>
		<title>Multiple Factor Model – Building Fundamental Factors</title>
		<link>http://systematicinvestor.wordpress.com/2012/02/04/multiple-factor-model-building-fundamental-factors/</link>
		<comments>http://systematicinvestor.wordpress.com/2012/02/04/multiple-factor-model-building-fundamental-factors/#comments</comments>
		<pubDate>Sat, 04 Feb 2012 20:09:28 +0000</pubDate>
		<dc:creator>systematicinvestor</dc:creator>
				<category><![CDATA[Factor Model]]></category>
		<category><![CDATA[Factors]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Strategy]]></category>
		<category><![CDATA[Trading Strategies]]></category>

		<guid isPermaLink="false">http://systematicinvestor.wordpress.com/?p=787</guid>
		<description><![CDATA[This is the second post in the series about Multiple Factor Models. I will build on the code presented in the prior post, Multiple Factor Model – Fundamental Data, and I will show how to build Fundamental factors described in the CSFB Alpha Factor Framework. For details of the CSFB Alpha Factor Framework please read [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=787&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is the second post in the series about Multiple Factor Models. I will build on the code presented in the prior post, <a href="http://systematicinvestor.wordpress.com/2012/01/29/multiple-factor-model-fundamental-data/" target="_blank">Multiple Factor Model – Fundamental Data</a>, and I will show how to build Fundamental factors described in the CSFB Alpha Factor Framework. For details of the CSFB Alpha Factor Framework please read <a href="http://www.scribd.com/mobile/documents/62210581/download?commit=Download+Now&amp;secret_password=" target="_blank">CSFB Quantitative Research, Alpha Factor Framework on page 11, page 49 by P. N. Patel, S. Yao, R. Carlson, A. Banerji, J. Handelman</a>.</p>
<p>The CSFB Alpha Factor Framework has both traditional Fundamental factors and industry relative Fundamental factors. Let&#8217;s start by getting Fundamental data that we will need to create Price/Earnings, Price/Sales, Price/Cash Flow, Dividend Yield, Price/Book factors. In the prior post, I mentioned that it takes a while to download historical fundamental data for all companies in the Dow Jones index, and I recommend saving fundamental data with save(data.fund, file=’data.fund.Rdata’) command. In the following code I will just load historical fundamental data with load(file=’data.fund.Rdata’) command instead of downloading all data again.</p>
<p><pre class="brush: r;">
###############################################################################
# Load Systematic Investor Toolbox (SIT)
# http://systematicinvestor.wordpress.com/systematic-investor-toolbox/
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
    source(con)
close(con)

	#*****************************************************************
	# Find Sectors for each company in DOW 30
	#****************************************************************** 
	tickers = spl('XLY,XLP,XLE,XLF,XLV,XLI,XLB,XLK,XLU')
	tickers.desc = spl('ConsumerCyclicals,ConsumerStaples,Energy,Financials,HealthCare,Industrials,Materials,Technology,Utilities')
	
	sector.map = c()
	for(i in 1:len(tickers)) {
		sector.map = rbind(sector.map, 
				cbind(sector.spdr.components(tickers[i]), tickers.desc[i])
			)
	}
	colnames(sector.map) = spl('ticker,sector')

	#*****************************************************************
	# Load historical data
	#****************************************************************** 
	load.packages('quantmod')		
	tickers = dow.jones.components()
	
	sectors = factor(sector.map[ match(tickers, sector.map[,'ticker']), 'sector'])
		names(sectors) = tickers
	
	# get fundamental data
	load(file='data.fund.Rdata')
			
	# get pricing data
	load(file='data.Rdata')
	
	#*****************************************************************
	# Combine fundamental and pricing data
	#****************************************************************** 	
	for(i in tickers) {
		fund = data.fund[[i]]
		fund.date = date.fund.data(fund)
		
		# Earnings per Share		
		EPS = get.fund.data('Diluted EPS from Total Operations', fund, fund.date, is.12m.rolling=T)
		
		# Sales, exception not available for financial firms
		SALE = get.fund.data('total revenue', fund, fund.date, is.12m.rolling=T)
		
		# Common Shares Outstanding
		CSHO = get.fund.data('total common shares out', fund, fund.date)

		# Common Equity
		CEQ = get.fund.data('total equity', fund, fund.date)

		# Dividends
		DV.PS = get.fund.data('dividends paid per share', fund, fund.date, is.12m.rolling=T)
				
		# Cash Flow, exception not available for financial firms
		CFL = get.fund.data('net cash from operating activities', fund, fund.date, cash.flow=T, is.12m.rolling=T)
				
		# merge
		data[[i]] = merge(data[[i]], EPS, SALE, CSHO, CEQ, DV.PS, CFL)
	}

	bt.prep(data, align='keep.all', dates='1995::2011')

	#*****************************************************************
	# Create Factors
	#****************************************************************** 
	prices = data$prices
		prices = bt.apply.matrix(prices, function(x) ifna.prev(x))
	
	sectors	= sectors[colnames(prices)]	
		
	# create factors
	factors = list()	
</pre></p>
<p>In the Dow Jones index there are 4 financial firms (AXP, BAC, JPM, TRV) and Sales and Cash Flow are not really measurable for financial firms. Please read <a href="www.stern.nyu.edu/~adamodar/pdfiles/papers/finfirm09.pdf" target="_blank">Valuing Financial Service Firms by A. Damodaran</a> for detailed explanation why Sales and Cash Flow are not really measurable for financial firms.</p>
<p>Next let’s create Traditional Value factors: Price/Earnings, Price/Sales, Price/Cash Flow, Dividend Yield, Price/Book.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Traditional Value
	#****************************************************************** 
	factors$TV = list()

	# Market Value - capitalization
	CSHO =  bt.apply(data, function(x) ifna.prev(x[, 'CSHO']))
	MKVAL = prices * CSHO
			
	# Price / Earnings
	EPS = bt.apply(data, function(x) ifna.prev(x[, 'EPS']))
	factors$TV$EP = EPS / prices
	
	# Price / Trailing Sales
	SALE = bt.apply(data, function(x) ifna.prev(x[, 'SALE']))	
	factors$TV$SP = SALE / MKVAL
	
	# Price / Trailing Cash Flow
	CFL = bt.apply(data, function(x) ifna.prev(x[, 'CFL']))
	factors$TV$CFP = CFL / MKVAL
	
	# Dividend Yield
	DV.PS = bt.apply(data, function(x) ifna.prev(x[, 'DV.PS']))
	factors$TV$DY = DV.PS / prices
	
	# Price / Book Value		
	CEQ = bt.apply(data, function(x) ifna.prev(x[, 'CEQ']))
	factors$TV$BP = CEQ	/ MKVAL

	# Eliminate Price/Sales and Price/Cash Flow for financial firms
	factors$TV$SP[, sectors == 'Financials'] = NA
	factors$TV$CFP[, sectors == 'Financials'] = NA

	#*****************************************************************
	# Convert to monthly
	#****************************************************************** 
	# find month ends
	month.ends = endpoints(prices, 'months')
	
	prices = prices[month.ends,]
		n = ncol(prices)
		nperiods = nrow(prices)
	
	ret = prices / mlag(prices) - 1
		next.month.ret = mlag(ret, -1)
	
	MKVAL = MKVAL[month.ends,]
	
	for(j in 1:len(factors)) {	
		for(i in 1:len(factors[[j]])) {
			factors[[j]][[i]] = factors[[j]][[i]][month.ends,]	
		}
	}
</pre></p>
<p>To create an overall Traditional Value factor, let&#8217;s first normalize (convert to z scores) all Traditional Value factors by subtracting capitalization weighted average and dividing by standard deviation. The overall Traditional Value factor is an average of all normalized Traditional Value factors.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Create the overall Traditional Value factor 
	#****************************************************************** 
	# check missing data for financial firms
	sapply(factors$TV, count)	
	
	# normalize (convert to z scores) cross sectionaly all Traditional Value factors
	for(i in names(factors$TV)) {
		factors$TV[[i]] = (factors$TV[[i]] - cap.weighted.mean(factors$TV[[i]], MKVAL)) / 
							apply(factors$TV[[i]], 1, sd, na.rm=T)
	}

	# compute the overall Traditional Value factor
	load.packages('abind') 
	temp = abind(factors$TV, along = 3)
	factors$TV$AVG = factors$TV[[1]]
		factors$TV$AVG[] = apply(temp, c(1,2), mean, na.rm=T)
		
	# plot quintile charts for all Traditional Value factors
	layout(matrix(1:6,nc=2))
	sapply(1:len(factors$TV), function(i)
		compute.quantiles(factors$TV[[i]], next.month.ret, paste(names(factors$TV)[i], 'Traditional Value'))
	)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/02/plot1-small.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/02/plot1-small.png?w=600&#038;h=500" alt="" title="plot1.png.small" width="600" height="500" class="alignnone size-full wp-image-804" /></a></p>
<p>I created a <a href="https://github.com/systematicinvestor/SIT/blob/master/R/factor.model.r" target="_blank">compute.quantiles() function in factor.model.r at github</a> to compute and plot quantiles. For example, the quantiles chart for EP factor shows the average next month performance of stocks in each quantiles. The quantiles are created each month by ranking stocks by EP factor and grouping them into 5 quantiles. There is tendency of quantile 5 (Q5) to outperform quantile 1 (Q1) in most cases. The relationship between quantiles is not perfect, but the spread between Q5-Q1 is positive.</p>
<p>Next let’s examine quantiles for the overall Traditional Value factor in more details.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Backtest quantiles and quantile spread
	#****************************************************************** 
	out = compute.quantiles(factors$TV$AVG, next.month.ret, plot=F)	
		
	prices = data$prices
		prices = bt.apply.matrix(prices, function(x) ifna.prev(x))

	# create strategies that invest in each qutile
	models = list()
	
	for(i in 1:5) {
		data$weight[] = NA
			data$weight[month.ends,] = iif(out$quantiles == i, out$weights, 0)
			capital = 100000
			data$weight[] = (capital / prices) * (data$weight)	
		models[[paste('Q',i,sep='')]] = bt.run(data, type='share', capital=capital)
	}
	
	# spread
	data$weight[] = NA
		data$weight[month.ends,] = iif(out$quantiles == 5, out$weights, 
						iif(out$quantiles == 1, -out$weights, 0))
		capital = 100000
		data$weight[] = (capital / prices) * (data$weight)
	models$Q5_Q1 = bt.run(data, type='share', capital=capital)
	
	#*****************************************************************
	# Create Report
	#****************************************************************** 
	plotbt(models, plotX = T, log = 'y', LeftMargin = 3)	    	
		mtext('Cumulative Performance', side = 2, line = 1)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/02/plot2-small1.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/02/plot2-small1.png?w=600&#038;h=500" alt="" title="plot2.png.small" width="600" height="500" class="alignnone size-full wp-image-807" /></a></p>
<p>The quantile spread Q5-Q1 shows consistent positive performance after 2000, but is inverted from 1995 to 2000. This is a bit strange and calls for more investigation.</p>
<p>In the next posts, I will show how to run pooled cross sectional regression to create alpha scores.</p>
<p>To view the complete source code for this example, please have a look at the <a href="https://github.com/systematicinvestor/SIT/blob/master/R/factor.model.test.r" target="_blank">fm.fund.factor.test() function in factor.model.test.r at github</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/systematicinvestor.wordpress.com/787/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/systematicinvestor.wordpress.com/787/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/systematicinvestor.wordpress.com/787/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/systematicinvestor.wordpress.com/787/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/systematicinvestor.wordpress.com/787/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/systematicinvestor.wordpress.com/787/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/systematicinvestor.wordpress.com/787/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/systematicinvestor.wordpress.com/787/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/systematicinvestor.wordpress.com/787/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/systematicinvestor.wordpress.com/787/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/systematicinvestor.wordpress.com/787/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/systematicinvestor.wordpress.com/787/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/systematicinvestor.wordpress.com/787/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/systematicinvestor.wordpress.com/787/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=787&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://systematicinvestor.wordpress.com/2012/02/04/multiple-factor-model-building-fundamental-factors/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f5676a7cfc17017cd99e8266c814227b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">systematicinvestor</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/02/plot1-small.png" medium="image">
			<media:title type="html">plot1.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/02/plot2-small1.png" medium="image">
			<media:title type="html">plot2.png.small</media:title>
		</media:content>
	</item>
		<item>
		<title>Multiple Factor Model &#8211; Fundamental Data</title>
		<link>http://systematicinvestor.wordpress.com/2012/01/29/multiple-factor-model-fundamental-data/</link>
		<comments>http://systematicinvestor.wordpress.com/2012/01/29/multiple-factor-model-fundamental-data/#comments</comments>
		<pubDate>Sun, 29 Jan 2012 05:03:49 +0000</pubDate>
		<dc:creator>systematicinvestor</dc:creator>
				<category><![CDATA[Factor Model]]></category>
		<category><![CDATA[Factors]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Strategy]]></category>

		<guid isPermaLink="false">http://systematicinvestor.wordpress.com/?p=765</guid>
		<description><![CDATA[The Multiple Factor Model can be used to decompose returns and calculate risk. Following are some examples of the Multiple Factor Models: The expected returns factor model: Commonality In The Determinants Of Expected Stock Returns by R. Haugen, N. Baker (1996) The expected returns factor model: CSFB Quantitative Research, Alpha Factor Framework on page 11, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=765&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.investopedia.com/terms/m/multifactor-model.asp" target="_blank">Multiple Factor Model</a> can be used to decompose returns and calculate risk. Following are some examples of the Multiple Factor Models:</p>
<ul>
<li>The expected returns factor model: <a href="http://www.quantitativeinvestment.com/documents/common.pdf" target="_blank">Commonality In The Determinants Of Expected Stock Returns by R. Haugen, N. Baker (1996)</a></li>
<li>The expected returns factor model: <a href="http://www.scribd.com/mobile/documents/62210581/download?commit=Download+Now&amp;secret_password=" target="_blank">CSFB Quantitative Research, Alpha Factor Framework on page 11, page 49 by P. N. Patel, S. Yao, R. Carlson, A. Banerji, J. Handelman</a></li>
<li>The risk factor model: <a href="http://www.alacra.com/alacra/help/barra_handbook_US.pdf" target="_blank">MSCI Barra United States Equity Multi-Factor Model, page 101</a></li>
</ul>
<p>The factors in the model are usually created using pricing, fundamental, analyst estimates, and proprietary data. I will only show examples of factors using pricing and fundamental data because these infromation is readily available from Yahoo Fiance and ADVFN. </p>
<p>This is the first post in the series about Multiple Factor Models. In this post I will show how to get company&#8217;s Fundamental Data into R, create a simple factor, and run correlation analysis. In the next posts, I will show how to:</p>
<ul>
<li>Build Factors and compute quantiles spreads</li>
<li>Backtest Multiple Factor Model</li>
<li>Calculate Risk using Multiple Factor Model</li>
</ul>
<p>I created a <a href="https://github.com/systematicinvestor/SIT/blob/master/R/fundamental.data.r" target="_blank">fund.data() function in fundamental.data.r at github</a> to download company&#8217;s historical Fundamental data from ADVFN. Following code loads historical quarterly fundamental data for <a href="http://www.google.com/finance?q=nyse:wmt" target="_blank">Wal-Mart Stores</a> and computes rolling annual <a href="http://en.wikipedia.org/wiki/Earnings_per_share" target="_blank">Earnings per share (EPS)</a> using the  <a href="http://systematicinvestor.wordpress.com/systematic-investor-toolbox/" target="_blank">Systematic Investor Toolbox</a>:</p>
<p><pre class="brush: r;">
###############################################################################
# Load Systematic Investor Toolbox (SIT)
# http://systematicinvestor.wordpress.com/systematic-investor-toolbox/
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
    source(con)
close(con)


###############################################################################
# determine date when fundamental data is available
# use 'date preliminary data loaded' when available
# otherwise lag 'quarter end date' 2 months for Q1/2/3 and 3 months for Q4
###############################################################################		
date.fund.data &lt;- function(data)
{
	# construct date
	quarter.end.date = as.Date(paste(data['quarter end date',], '/1', sep=''), '%Y/%m/%d')	
	quarterly.indicator = data['quarterly indicator',]
	date.preliminary.data.loaded = as.Date(data['date preliminary data loaded',], '%Y-%m-%d') + 1
	
	months = seq(quarter.end.date[1], tail(quarter.end.date,1)+365, by='1 month') 
	index = match(quarter.end.date, months)
	quarter.end.date = months[ iif(quarterly.indicator == '4', index+3, index+2) + 1 ] - 1
		
	fund.date = date.preliminary.data.loaded
		fund.date[is.na(fund.date)] = quarter.end.date[is.na(fund.date)] 

	return(fund.date)
}

	#*****************************************************************
	# Load historical fundamental data
	# http://advfn.com/p.php?pid=financials&amp;symbol=NYSE:WMT&amp;mode=quarterly_reports
	#****************************************************************** 
	Symbol = 'NYSE:WMT'	
	fund = fund.data(Symbol, 80)
	
	# construct date
	fund.date = date.fund.data(fund)	
	
	#*****************************************************************
	# Create and Plot Earnings per share
	#****************************************************************** 
	EPS.Q = as.double(fund['Diluted EPS from Total Operations',])
		EPS.Q = as.xts(EPS.Q, fund.date)	
	EPS = runSum(EPS.Q, 4)

	# Plot
	layout(1:2)
	par(mar=c(2,2,2,1))
	x = barplot(EPS.Q, main='Wal-Mart Quarterly Earnings per share', border=NA)
	text(x, EPS.Q, fund['quarterly indicator',], adj=c(0.5,-0.3), cex=0.8, xpd = TRUE)

	barplot(EPS, main='Wal-Mart Rolling Annual Earnings per share', border=NA)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small4.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small4.png?w=600&#038;h=500" alt="" title="plot1.png.small" width="600" height="500" class="alignnone size-full wp-image-779" /></a></p>
<p>You can see a pronounced seasonality in the Quarterly EPS for Wal-Mart, the Q4 always strong and stands out. The common way to deal with seasonality in the income statement is to use rolling annual sum, i.e. sum last 4 quarters.</p>
<p>Next let&#8217;s align Wal-Mart prices and EPS and plot them on the same graph.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Load historical data
	#****************************************************************** 
	load.packages('quantmod')
	tickers = 'WMT'
		
	data &lt;- new.env()
	getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data, auto.assign = T)
		for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)
		
	data$WMT = merge(data$WMT, EPS)
		# back fill EPS
		data$WMT$EPS = ifna.prev(coredata(data$WMT$EPS))	
	
	# Plot
	y = data$WMT['1990::']
	plota(Cl(y), type = 'l', LeftMargin=3)
			
	plota2Y(y$EPS, type='l', las=1, col='red', col.axis = 'red')
								
	plota.legend('WMT(rhs),WMT.EPS(lhs)', 'blue,red', list(Cl(y),y$EPS))
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot2-small3.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot2-small3.png?w=600&#038;h=500" alt="" title="plot2.png.small" width="600" height="500" class="alignnone size-full wp-image-780" /></a></p>
<p>Next let&#8217;s repeat the above steps for all companies in the Dow Jones index.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Load historical data
	#****************************************************************** 
	load.packages('quantmod')		
	tickers = dow.jones.components()
	
	# get fundamental data
	data.fund &lt;- new.env()
		temp = paste(iif( nchar(tickers) &lt;= 3, 'NYSE:', 'NASDAQ:'), tickers, sep='')
		for(i in 1:len(tickers)) data.fund[[tickers[i]]] = fund.data(temp[i], 80)
	save(data.fund, file='data.fund.Rdata')
	
		
	# get pricing data
	data &lt;- new.env()
	getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
		for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)	
	save(data, file='data.Rdata')
	

	#load(file='data.fund.Rdata')
	#load(file='data.Rdata')
	
			
	# combine fundamental and pricing data
	for(i in tickers) {
		fund = data.fund[[i]]
		fund.date = date.fund.data(fund)
		
		EPS.Q = as.double(fund['Diluted EPS from Total Operations',])
			EPS.Q = as.xts(EPS.Q, fund.date)	
		EPS = runSum(EPS.Q, 4)
				
		data[[i]] = merge(data[[i]], EPS)
	}

	bt.prep(data, align='keep.all', dates='1995::2011')
</pre></p>
<p>It takes a while to download historical fundamental data for all companies in the Dow Jones index, so I recommend saving your results with save(data.fund, file=&#8217;data.fund.Rdata&#8217;) command. Later on if you want to run code one more time, just load(file=&#8217;data.fund.Rdata&#8217;) instead of downloading all data again.</p>
<p>Next let&#8217;s create monthly factors. EP factor = (Earnings per share) / Price. VOMO factor = Volume x Momentum.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Compute monthly factors
	#****************************************************************** 
	prices = data$prices
		prices = bt.apply.matrix(prices, function(x) ifna.prev(x))
	
	# create factors
	factors = list()

	# E/P
	EPS = bt.apply(data, function(x) ifna.prev(x[, 'EPS']))
	factors$EP = EPS / prices
			
	# VOMO - Volume x Momentum
	volume = bt.apply(data, function(x) ifna.prev(Vo(x)))
	factors$VOMO = (prices / mlag(prices,10) - 1) * bt.apply.matrix(volume, runMean, 22) / bt.apply.matrix(volume, runMean, 66)
		
	
	# find month ends
	month.ends = endpoints(prices, 'months')
	
	prices = prices[month.ends,]
	n = ncol(prices)
	nperiods = nrow(prices)
	
	ret = prices / mlag(prices) - 1
	next.month.ret = mlag(ret, -1)
	
	factors$EP = factors$EP[month.ends,]	
	factors$VOMO = factors$VOOM[month.ends,]			
</pre></p>
<p>Next let&#8217;s run correlation analysis for EP factor. You can do correlation analysis for VOMO factor as a homework.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Correlation Analysis
	#****************************************************************** 
	x = as.vector(factors$EP)
 	y = as.vector(next.month.ret)
 	
 	cor.test(x, y, use = 'complete.obs', method = 'pearson')			

 	# Plot
	par(mar=c(4,4,2,1)) 	 	 	
 	plot(x, y, pch=20, main='Correlation Analysis for EP factor', xlab='EP', ylab='Next Month Return')
 		abline(lm(y ~ x), col='blue', lwd=2)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot3-small3.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot3-small3.png?w=600&#038;h=500" alt="" title="plot3.png.small" width="600" height="500" class="alignnone size-full wp-image-778" /></a></p>
<pre>
&gt;  cor.test(x, y, use = 'complete.obs', method = 'pearson')
        Pearson's product-moment correlation
data:  x and y
t = 3.6931, df = 5867, p-value = 0.0002235
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
 0.02260247 0.07365350
sample estimates:
       cor
0.04815943
</pre>
<p>The correlation between EP and Next Month Returns is small, but significantly different from zero. The small correlation is not a surprise and is usual for this type of analysis. In the next posts, I will show that even this weak dependence can be profitable.</p>
<p>To view the complete source code for this example, please have a look at the <a href="https://github.com/systematicinvestor/SIT/blob/master/R/factor.model.test.r" target="_blank">fm.fund.data.test() function in factor.model.test.r at github</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/systematicinvestor.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/systematicinvestor.wordpress.com/765/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/systematicinvestor.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/systematicinvestor.wordpress.com/765/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/systematicinvestor.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/systematicinvestor.wordpress.com/765/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/systematicinvestor.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/systematicinvestor.wordpress.com/765/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/systematicinvestor.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/systematicinvestor.wordpress.com/765/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/systematicinvestor.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/systematicinvestor.wordpress.com/765/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/systematicinvestor.wordpress.com/765/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/systematicinvestor.wordpress.com/765/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=765&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://systematicinvestor.wordpress.com/2012/01/29/multiple-factor-model-fundamental-data/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f5676a7cfc17017cd99e8266c814227b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">systematicinvestor</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small4.png" medium="image">
			<media:title type="html">plot1.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot2-small3.png" medium="image">
			<media:title type="html">plot2.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot3-small3.png" medium="image">
			<media:title type="html">plot3.png.small</media:title>
		</media:content>
	</item>
		<item>
		<title>Time Series Matching with Dynamic Time Warping</title>
		<link>http://systematicinvestor.wordpress.com/2012/01/20/time-series-matching-with-dynamic-time-warping/</link>
		<comments>http://systematicinvestor.wordpress.com/2012/01/20/time-series-matching-with-dynamic-time-warping/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 19:19:03 +0000</pubDate>
		<dc:creator>systematicinvestor</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Strategy]]></category>
		<category><![CDATA[Trading Strategies]]></category>

		<guid isPermaLink="false">http://systematicinvestor.wordpress.com/?p=741</guid>
		<description><![CDATA[THIS IS NOT INVESTMENT ADVICE. The information is provided for informational purposes only. In the Time Series Matching post, I used one to one mapping to the compute distance between the query(current pattern) and reference(historical time series). Following chart visualizes this concept. The distance is the sum of vertical lines. An alternative way to map [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=741&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>THIS IS NOT INVESTMENT ADVICE. The information is provided for informational purposes only.</p>
<p>In the <a href="http://systematicinvestor.wordpress.com/2012/01/13/time-series-matching/" target="_blank">Time Series Matching</a> post, I used one to one mapping to the compute distance between the query(current pattern) and reference(historical time series). Following chart visualizes this concept. The distance is the sum of vertical lines.</p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot0-small.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot0-small.png?w=600&#038;h=500" alt="" title="plot0.png.small" width="600" height="500" class="alignnone size-full wp-image-743" /></a></p>
<p>An alternative way to map one time series to another is <a href="http://en.wikipedia.org/wiki/Dynamic_time_warping" target="_blank">Dynamic Time Warping(DTW)</a>. DTW algorithm looks for <a href="http://www.cse.unsw.edu.au/~waleed/phd/html/node38.html" target="_blank">minimum distance mapping</a> between query and reference. Following chart visualizes one to many mapping possible with DTW.</p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small3.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small3.png?w=600&#038;h=500" alt="" title="plot1.png.small" width="600" height="500" class="alignnone size-full wp-image-744" /></a></p>
<p>To check if there a difference between simple one to one mapping and DTW, I will search for time series matches that are similar to the most recent 90 days of SPY in the last 10 years of history. Following code loads historical prices from Yahoo Fiance, setups the problem and computes Euclidean distance for the historical rolling window using the  <a href="http://systematicinvestor.wordpress.com/systematic-investor-toolbox/" target="_blank">Systematic Investor Toolbox</a>:</p>
<p><pre class="brush: r;">
###############################################################################
# Load Systematic Investor Toolbox (SIT)
# http://systematicinvestor.wordpress.com/systematic-investor-toolbox/
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
    source(con)
close(con)

	#*****************************************************************
	# Load historical data
	#****************************************************************** 
	load.packages('quantmod')	
	tickers = 'SPY'

	data = getSymbols(tickers, src = 'yahoo', from = '1950-01-01', auto.assign = F)

	#*****************************************************************
	# Euclidean distance, one to one mapping
	#****************************************************************** 
	obj = bt.matching.find(Cl(data), normalize.fn = normalize.mean, dist.fn = 'dist.euclidean', plot=T)

	matches = bt.matching.overlay(obj, plot.index=1:90, plot=T)

	layout(1:2)
	matches = bt.matching.overlay(obj, plot=T, layout=T)
	bt.matching.overlay.table(obj, matches, plot=T, layout=T)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot2-small2.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot2-small2.png?w=600&#038;h=500" alt="" title="plot2.png.small" width="600" height="500" class="alignnone size-full wp-image-745" /></a></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot3-small2.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot3-small2.png?w=600&#038;h=500" alt="" title="plot3.png.small" width="600" height="500" class="alignnone size-full wp-image-746" /></a></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot4-small1.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot4-small1.png?w=600&#038;h=800" alt="" title="plot4.png.small" width="600" height="800" class="alignnone size-full wp-image-747" /></a></p>
<p>Next, let&#8217; examine the top 10 matches using Dynamic Time Warping distance. I will use the Dynamic Time Warping implementation from <a href="http://dtw.r-forge.r-project.org/" target="_blank">dtw</a> package.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Dynamic time warping distance	
	#****************************************************************** 
	# http://en.wikipedia.org/wiki/Dynamic_time_warping
	# http://dtw.r-forge.r-project.org/
	#****************************************************************** 
	load.packages('dtw')

	obj = bt.matching.find(Cl(data), normalize.fn = normalize.mean, dist.fn = 'dist.DTW', plot=T)

	matches = bt.matching.overlay(obj, plot.index=1:90, plot=T)

	layout(1:2)
	matches = bt.matching.overlay(obj, plot=T, layout=T)
	bt.matching.overlay.table(obj, matches, plot=T, layout=T)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot5-small.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot5-small.png?w=600&#038;h=500" alt="" title="plot5.png.small" width="600" height="500" class="alignnone size-full wp-image-748" /></a></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot6-small.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot6-small.png?w=600&#038;h=500" alt="" title="plot6.png.small" width="600" height="500" class="alignnone size-full wp-image-749" /></a></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot7-small.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot7-small.png?w=600&#038;h=800" alt="" title="plot7.png.small" width="600" height="800" class="alignnone size-full wp-image-742" /></a></p>
<p>Both algorithms produced very similar matches and very similar predictions. I would use these predictions as an educated guess to market action going forward. So far, it looks like the market will not be going up in full throttle in the next 22 days.</p>
<p>To view the complete source code for this example, please have a look at the <a href="https://github.com/systematicinvestor/SIT/blob/master/R/bt.test.r" target="_blank">bt.matching.dtw.test() function in bt.test.r at github</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/systematicinvestor.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/systematicinvestor.wordpress.com/741/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/systematicinvestor.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/systematicinvestor.wordpress.com/741/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/systematicinvestor.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/systematicinvestor.wordpress.com/741/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/systematicinvestor.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/systematicinvestor.wordpress.com/741/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/systematicinvestor.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/systematicinvestor.wordpress.com/741/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/systematicinvestor.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/systematicinvestor.wordpress.com/741/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/systematicinvestor.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/systematicinvestor.wordpress.com/741/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=741&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://systematicinvestor.wordpress.com/2012/01/20/time-series-matching-with-dynamic-time-warping/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f5676a7cfc17017cd99e8266c814227b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">systematicinvestor</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot0-small.png" medium="image">
			<media:title type="html">plot0.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small3.png" medium="image">
			<media:title type="html">plot1.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot2-small2.png" medium="image">
			<media:title type="html">plot2.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot3-small2.png" medium="image">
			<media:title type="html">plot3.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot4-small1.png" medium="image">
			<media:title type="html">plot4.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot5-small.png" medium="image">
			<media:title type="html">plot5.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot6-small.png" medium="image">
			<media:title type="html">plot6.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot7-small.png" medium="image">
			<media:title type="html">plot7.png.small</media:title>
		</media:content>
	</item>
		<item>
		<title>Time Series Matching strategy backtest</title>
		<link>http://systematicinvestor.wordpress.com/2012/01/17/time-series-matching-strategy-backtest/</link>
		<comments>http://systematicinvestor.wordpress.com/2012/01/17/time-series-matching-strategy-backtest/#comments</comments>
		<pubDate>Tue, 17 Jan 2012 19:02:06 +0000</pubDate>
		<dc:creator>systematicinvestor</dc:creator>
				<category><![CDATA[Backtesting]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Strategy]]></category>
		<category><![CDATA[Trading Strategies]]></category>

		<guid isPermaLink="false">http://systematicinvestor.wordpress.com/?p=735</guid>
		<description><![CDATA[This is a quick post to address comments raised in the Time Series Matching post. I will show a very simple example of backtesting a Time Series Matching strategy using a distance weighted prediction. I have to warn you, the strategy’s performance is worse then the Buy and Hold. I used the code from Time [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=735&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is a quick post to address comments raised in the <a href="http://systematicinvestor.wordpress.com/2012/01/13/time-series-matching/" target="_blank">Time Series Matching</a> post. I will show a very simple example of backtesting a Time Series Matching strategy using a distance weighted prediction. I have to warn you, the strategy’s performance is worse then the Buy and Hold.</p>
<p>I used the code from <a href="http://systematicinvestor.wordpress.com/2012/01/13/time-series-matching/" target="_blank">Time Series Matching</a> post and re-arranged it into 3 functions:<br />
bt.matching.find &#8211; finds historical matches similar to the given query (pattern).<br />
bt.matching.overlay &#8211; creates matrix that overlays all matches one on top of each other.<br />
bt.matching.overlay.table – creates and plots matches summary table.</p>
<p>I will use historical prices for <a href="http://finance.yahoo.com/q?s=^gspc" target="_blank">^GSPC</a> to extend <a href="http://finance.yahoo.com/q?s=spy" target="_blank">SPY</a> time series. I will create a monthly backtest, that trades at the end of the month, staring January 1994. Each month, I will look back for the best historical matches similar to the last 90 days in the last 10 years of history.</p>
<p>I will compute a distance weighted average prediction for the next month and will go long if prediction is positive, otherwise stay in cash. This is a very simple backtest and the strategy’s performance is worse then the Buy and Hold.</p>
<p>Following code loads historical prices from Yahoo Fiance and setups Time Series Matching strategy backtest using the  <a href="http://systematicinvestor.wordpress.com/systematic-investor-toolbox/" target="_blank">Systematic Investor Toolbox</a>:</p>
<p><pre class="brush: r;">
###############################################################################
# Load Systematic Investor Toolbox (SIT)
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
    source(con)
close(con)

	#*****************************************************************
	# Load historical data
	#****************************************************************** 
	load.packages('quantmod')	
	tickers = spl('SPY,^GSPC')

	data &lt;- new.env()
	getSymbols(tickers, src = 'yahoo', from = '1950-01-01', env = data, auto.assign = T)
	bt.prep(data, align='keep.all')

	# combine SPY and ^GSPC
	scale = as.double( data$prices$SPY['1993:01:29'] / data$prices$GSPC['1993:01:29'] )
	hist = c(scale * data$prices$GSPC['::1993:01:28'], data$prices$SPY['1993:01:29::'])

	#*****************************************************************
	# Backtest setup:
	# Starting January 1994, each month search for the 10 best matches 
	# similar to the last 90 days in the last 10 years of history data
	#
	# Invest next month if distance weighted prediction is positive
	# otherwise stay in cash
	#****************************************************************** 
	month.ends = endpoints(hist, 'months')
		month.ends = month.ends[month.ends &gt; 0]		
	
	start.index = which(date.year(index(hist[month.ends])) == 1994)[1]
	weight = hist * NA
	
	for( i in start.index : len(month.ends) ) {
		obj = bt.matching.find(hist[1:month.ends[i],], normalize.fn = normalize.first)
		matches = bt.matching.overlay(obj)
		
		# compute prediction for next month
		n.match = len(obj$min.index)
		n.query = len(obj$query)				
		month.ahead.forecast = matches[,(2*n.query+22)]/ matches[,2*n.query] - 1
		
		# Distance weighted average
		temp = round(100*(obj$dist / obj$dist[1] - 1))		
			n.weight = max(temp) + 1
			weights = (n.weight - temp) / ( n.weight * (n.weight+1) / 2)
		weights = weights / sum(weights)
			# barplot(weights)
		avg.direction = weighted.mean(month.ahead.forecast[1:n.match], w=weights)
		
		# Logic
		weight[month.ends[i]] = 0
		if( avg.direction &gt; 0 ) weight[month.ends[i]] = 1
	}
</pre></p>
<p>Next, let&#8217;s compare the Time Series Matching strategy to Buy &amp; Hold:</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Code Strategies
	#****************************************************************** 
	tickers = 'SPY'

	data &lt;- new.env()
	getSymbols(tickers, src = 'yahoo', from = '1950-01-01', env = data, auto.assign = T)
	bt.prep(data, align='keep.all')
	
	prices = data$prices  
	
	# Buy &amp; Hold	
	data$weight[] = 1
	buy.hold = bt.run(data)	

	# Strategy
	data$weight[] = NA
		data$weight[] = weight['1993:01:29::']
		capital = 100000
		data$weight[] = (capital / prices) * bt.exrem(data$weight)
	test = bt.run(data, type='share', capital=capital, trade.summary=T)
		
	#*****************************************************************
	# Create Report
	#****************************************************************** 
	plotbt.custom.report.part1(test, buy.hold, trade.summary=T)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small2.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small2.png?w=600&#038;h=500" alt="" title="plot1.png.small" width="600" height="500" class="alignnone size-full wp-image-737" /></a></p>
<p>How would you change the strategy or backtest to make it profitable?  Please share your ideas. I looking forward to exploring them.</p>
<p>To view the complete source code for this example, please have a look at the <a href="https://github.com/systematicinvestor/SIT/blob/master/R/bt.test.r" target="_blank">bt.matching.backtest.test() function in bt.test.r at github</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/systematicinvestor.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/systematicinvestor.wordpress.com/735/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/systematicinvestor.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/systematicinvestor.wordpress.com/735/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/systematicinvestor.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/systematicinvestor.wordpress.com/735/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/systematicinvestor.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/systematicinvestor.wordpress.com/735/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/systematicinvestor.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/systematicinvestor.wordpress.com/735/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/systematicinvestor.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/systematicinvestor.wordpress.com/735/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/systematicinvestor.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/systematicinvestor.wordpress.com/735/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=735&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://systematicinvestor.wordpress.com/2012/01/17/time-series-matching-strategy-backtest/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f5676a7cfc17017cd99e8266c814227b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">systematicinvestor</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small2.png" medium="image">
			<media:title type="html">plot1.png.small</media:title>
		</media:content>
	</item>
		<item>
		<title>Time Series Matching</title>
		<link>http://systematicinvestor.wordpress.com/2012/01/13/time-series-matching/</link>
		<comments>http://systematicinvestor.wordpress.com/2012/01/13/time-series-matching/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 16:31:20 +0000</pubDate>
		<dc:creator>systematicinvestor</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Strategy]]></category>
		<category><![CDATA[Trading Strategies]]></category>

		<guid isPermaLink="false">http://systematicinvestor.wordpress.com/?p=719</guid>
		<description><![CDATA[THIS IS NOT INVESTMENT ADVICE. The information is provided for informational purposes only. If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck. Do you want to know what S&#38;P 500 will do in the next week, month, quarter? One way to make an [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=719&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>THIS IS NOT INVESTMENT ADVICE. The information is provided for informational purposes only.</p>
<p><a href="http://en.wikipedia.org/wiki/Duck_test" target="_blank">If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck.</a></p>
<p>Do you want to know what S&amp;P 500 will do in the next week, month, quarter? One way to make an educated guess is to find historical periods similar to the current market environment, and examine what happened. I will call this process time series matching, but you could find a similar techniques referred as technical patterns and fractals. To get some flavor about fractals, following are two articles I read recently about fractals:</p>
<ul>
<li><a href="http://dynamichedge.com/2011/12/18/fractals-time/" target="_blank">Fractals — Time by DynamicHedge</a></li>
<li><a href="http://cssanalytics.wordpress.com/2011/12/19/fractals-and-the-importance-of-time-frame-diversification/">Fractals and The Importance of Time Frame Diversification by David Varadi</a></li>
</ul>
<p>I recommend reading following article about the time series matching to understand different approaches:</p>
<ul>
<li><a href="http://www.londonr.org/Sep%2011%20LondonR_AvettandJR.pdf" target="_blank">How to Accelerate Model Deployment using Rook by Jean-Robert Avettand-Fenoel</a>.</li>
<li><a href="http://timemachine.iic.harvard.edu/site_media/i/umaatutorial.pdf" target="_blank">Introduction to Machine Learning Research on Time Series by U. Rebbapragada</a>.</li>
<li><a href="http://www.cs.ucr.edu/~mvlachos/PKDD05/PKDD05_Handout.pdf" target="_blank">A practical Time-Series Tutorial with MATLAB</a>.</li>
</ul>
<p>I will use a simple method outlined in the <a href="http://www.londonr.org/Sep%2011%20LondonR_AvettandJR.pdf" target="_blank">How to Accelerate Model Deployment using Rook by Jean-Robert Avettand-Fenoel</a> article to find time series matches that are similar to the most recent 90 days of SPY.</p>
<p>Following code loads historical prices from Yahoo Fiance, setups the problem and computes Euclidean distance for the historical rolling window using the  <a href="http://systematicinvestor.wordpress.com/systematic-investor-toolbox/" target="_blank">Systematic Investor Toolbox</a>:</p>
<p><pre class="brush: r;">
###############################################################################
# Load Systematic Investor Toolbox (SIT)
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
    source(con)
close(con)

	#*****************************************************************
	# Load historical data
	#****************************************************************** 
	load.packages('quantmod')	
	tickers = 'SPY'

	data = getSymbols(tickers, src = 'yahoo', from = '1950-01-01', auto.assign = F)

	#*****************************************************************
	# Setup search
	#****************************************************************** 
	data = last(data, 252*10)
	reference = coredata(Cl(data))
		n = len(reference)
		query = reference[(n-90+1):n]	
		reference = reference[1:(n-90)]
		
		n.query = len(query)
		n.reference = len(reference)

	#*****************************************************************
	# Compute Distances
	#****************************************************************** 		
	dist = rep(NA, n.reference)
	query.normalized = (query - mean(query)) / sd(query)
	
	for( i in n.query : n.reference ) {
		window = reference[ (i - n.query + 1) : i]
		window.normalized = (window - mean(window)) / sd(window)
		dist[i] = stats:::dist(rbind(query.normalized, window.normalized))
	}
</pre></p>
<p>Next, let&#8217;s select the best 10 matches to the &#8216;query&#8217; pattern in the SPY history:</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Find Matches
	#****************************************************************** 			
	min.index = c()
	n.match = 10
	
	# only look at the minimums 
	temp = dist
		temp[ temp &gt; mean(dist, na.rm=T) ] = NA
		
	# remove n.query, points to the left/right of the minimums
	for(i in 1:n.match) {
		if(any(!is.na(temp))) {
			index = which.min(temp)
			min.index[i] = index
			temp[max(0,index - 2*n.query) : min(n.reference,(index + n.query))] = NA
		}
	}
	n.match = len(min.index)
		
	#*****************************************************************
	# Plot Matches
	#****************************************************************** 		
	dates = index(data)[1:len(dist)]
	
	par(mar=c(2, 4, 2, 2))
	plot(dates, dist, type='l',col='gray', main='Top Matches', ylab='Euclidean Distance', xlab='')
		abline(h = mean(dist, na.rm=T), col='darkgray', lwd=2)
		points(dates[min.index], dist[min.index], pch=22, col='red', bg='red')
		text(dates[min.index], dist[min.index], 1:n.match, adj=c(1,1), col='black',xpd=TRUE)
		

	plota(data, type='l', col='gray', main=tickers)
		plota.lines(last(data,90), col='blue')
		for(i in 1:n.match) {
		plota.lines(data[(min.index[i]-n.query + 1):min.index[i]], col='red')
		}
		text(index(data)[min.index - n.query/2], reference[min.index - n.query/2], 1:n.match, 
			adj=c(1,-1), col='black',xpd=TRUE)
		plota.legend('Pattern,Match #','blue,red')
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small1.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small1.png?w=600&#038;h=500" alt="" title="plot1.png.small" width="600" height="500" class="alignnone size-full wp-image-724" /></a></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot2-small1.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot2-small1.png?w=600&#038;h=500" alt="" title="plot2.png.small" width="600" height="500" class="alignnone size-full wp-image-725" /></a></p>
<p>Next, let&#8217;s overlay all matches with the &#8216;query&#8217; pattern and examine their historical performance after the match took place:</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Overlay all Matches
	#****************************************************************** 		
	matches = matrix(NA, nr=(n.match+1), nc=3*n.query)
	temp = c(rep(NA, n.query), reference, query)
	for(i in 1:n.match) {
		matches[i,] = temp[ (min.index[i] - n.query + 1):(min.index[i] + 2*n.query) ]	
	}

	# add the 'query' pattern
	matches[(n.match+1),] = temp[ (len(temp) - 2*n.query + 1):(len(temp) + n.query) ]		
	
	# normalize
	for(i in 1:(n.match+1)) {
		matches[i,] = matches[i,] / matches[i,n.query]
	}
		

	#*****************************************************************
	# Plot all Matches
	#****************************************************************** 				
	temp = 100 * ( t(matches[,-c(1:n.query)]) - 1)
	
	par(mar=c(2, 4, 2, 2))
	matplot(temp, type='l',col='gray',lwd=2, lty='dotted', xlim=c(1,2.5*n.query),
		main = paste('Pattern Prediction with', n.match, 'neighbours'),ylab='Normalized', xlab='')
		lines(temp[,(n.match+1)], col='black',lwd=4)
	
	points(rep(2*n.query,n.match), temp[2*n.query,1:n.match], pch=21, lwd=2, col='gray', bg='gray')
				
	bt.plot.dot.label &lt;- function(x, data, xfun, col='red') {
		for(j in 1:len(xfun)) {
			y = match.fun(xfun[[j]])(data)
			points(x, y, pch=21, lwd=4, col=col, bg=col)
			text(x, y, paste(names(xfun)[j], ':', round(y,1),'%'),
				adj=c(-0.1,0), cex = 0.8, col=col,xpd=TRUE)			
		}
	}
	
	bt.plot.dot.label(2*n.query, temp[2*n.query,1:n.match], 
		list(Min=min,Max=max,Median=median,'Bot 25%'=function(x) quantile(x,0.25),'Top 75%'=function(x) quantile(x,0.75)))
	bt.plot.dot.label(n.query, temp[n.query,(n.match+1)], list(Current=min))
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot3-small1.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot3-small1.png?w=600&#038;h=500" alt="" title="plot3.png.small" width="600" height="500" class="alignnone size-full wp-image-726" /></a></p>
<p>Next, let&#8217;s summarize all matches performance in a table:</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Table with predictions
	#****************************************************************** 		
	temp = matrix( double(), nr=(n.match+4), 6)
		rownames(temp) = c(1:n.match, spl('Current,Min,Average,Max'))
		colnames(temp) = spl('Start,End,Return,Week,Month,Quarter')
		
	# compute returns
	temp[1:(n.match+1),'Return'] = matches[,2*n.query]/ matches[,n.query]
	temp[1:(n.match+1),'Week'] = matches[,(2*n.query+5)]/ matches[,2*n.query]
	temp[1:(n.match+1),'Month'] = matches[,(2*n.query+20)]/ matches[,2*n.query]
	temp[1:(n.match+1),'Quarter'] = matches[,(2*n.query+60)]/ matches[,2*n.query]
			
	# compute average returns
	index = spl('Return,Week,Month,Quarter')
	temp['Min', index] = apply(temp[1:(n.match+1),index],2,min,na.rm=T)
	temp['Average', index] = apply(temp[1:(n.match+1),index],2,mean,na.rm=T)
	temp['Max', index] = apply(temp[1:(n.match+1),index],2,max,na.rm=T)
	
	# format
	temp[] = plota.format(100*(temp-1),1,'','%')
		
	# enter dates
	temp['Current', 'Start'] = format(index(last(data,90)[1]), '%d %b %Y')
	temp['Current', 'End'] = format(index(last(data,1)[1]), '%d %b %Y')
	for(i in 1:n.match) {
		temp[i, 'Start'] = format(index(data[min.index[i] - n.query + 1]), '%d %b %Y')
		temp[i, 'End'] = format(index(data[min.index[i]]), '%d %b %Y')	
	}
		
	# plot table
	plot.table(temp, smain='Match #')
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot4-small.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot4-small.png?w=600&#038;h=500" alt="" title="plot4.png.small" width="600" height="500" class="alignnone size-full wp-image-727" /></a></p>
<p>The Time Series Matching analysis can be used to make an educated guess what S&amp;P 500 will do in the next week, month, quarter. This educated guess is based on historical data and there is no guarantees that history will repeat itself.</p>
<p>In the next post I will examine other distance measures for Time Series Matching and I will show an example of <a href="http://en.wikipedia.org/wiki/Dynamic_time_warping" target="_blank">Dynamic time warping</a>.</p>
<p>To view the complete source code for this example, please have a look at the <a href="https://github.com/systematicinvestor/SIT/blob/master/R/bt.test.r" target="_blank">bt.matching.test() function in bt.test.r at github</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/systematicinvestor.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/systematicinvestor.wordpress.com/719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/systematicinvestor.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/systematicinvestor.wordpress.com/719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/systematicinvestor.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/systematicinvestor.wordpress.com/719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/systematicinvestor.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/systematicinvestor.wordpress.com/719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/systematicinvestor.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/systematicinvestor.wordpress.com/719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/systematicinvestor.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/systematicinvestor.wordpress.com/719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/systematicinvestor.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/systematicinvestor.wordpress.com/719/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=719&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://systematicinvestor.wordpress.com/2012/01/13/time-series-matching/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f5676a7cfc17017cd99e8266c814227b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">systematicinvestor</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small1.png" medium="image">
			<media:title type="html">plot1.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot2-small1.png" medium="image">
			<media:title type="html">plot2.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot3-small1.png" medium="image">
			<media:title type="html">plot3.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot4-small.png" medium="image">
			<media:title type="html">plot4.png.small</media:title>
		</media:content>
	</item>
		<item>
		<title>Trading using Garch Volatility Forecast</title>
		<link>http://systematicinvestor.wordpress.com/2012/01/06/trading-using-garch-volatility-forecast/</link>
		<comments>http://systematicinvestor.wordpress.com/2012/01/06/trading-using-garch-volatility-forecast/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 03:17:45 +0000</pubDate>
		<dc:creator>systematicinvestor</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Strategy]]></category>
		<category><![CDATA[Trading Strategies]]></category>

		<guid isPermaLink="false">http://systematicinvestor.wordpress.com/?p=686</guid>
		<description><![CDATA[Quantum Financier wrote an interesting article Regime Switching System Using Volatility Forecast. The article presents an elegant algorithm to switch between mean-reversion and trend-following strategies based on the market volatility. Two model are examined: one using the historical volatility and another using the Garch(1,1) Volatility Forecast. The mean-reversion strategy is modeled with RSI(2): Long when [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=686&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://quantumfinancier.wordpress.com" target="_blank">Quantum Financier</a> wrote an interesting article <a href="http://quantumfinancier.wordpress.com/2010/08/27/regime-switching-system-using-volatility-forecast/" target="_blank">Regime Switching System Using Volatility Forecast</a>. The article presents an elegant algorithm to switch between mean-reversion and trend-following strategies based on the market volatility. Two model are examined: one using the historical volatility and another using the Garch(1,1) Volatility Forecast. The mean-reversion strategy is modeled with RSI(2): Long when RSI(2), and Short otherwise. The trend-following strategy is modeled with SMA 50/200 crossover: Long when SMA(50) &gt; SMA(200), and Short otherwise.</p>
<p>I want show how to implement these ideas using the backtesting library in the <a href="http://systematicinvestor.wordpress.com/systematic-investor-toolbox/" target="_blank">Systematic Investor Toolbox</a>. </p>
<p>Following code loads historical prices from Yahoo Fiance and compares performance of the Buy and Hold, Mean-Reversion, and Trend-Following strategies using the backtesting library in the <a href="http://systematicinvestor.wordpress.com/systematic-investor-toolbox/" target="_blank">Systematic Investor Toolbox</a>:</p>
<p><pre class="brush: r;">
###############################################################################
# Load Systematic Investor Toolbox (SIT)
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
    source(con)
close(con)

	#*****************************************************************
	# Load historical data
	#****************************************************************** 
	load.packages('quantmod')	
	tickers = 'SPY'

	data &lt;- new.env()
	getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
		for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)		
	bt.prep(data, align='remove.na', dates='2000::2012')

	#*****************************************************************
	# Code Strategies
	#****************************************************************** 
	prices = data$prices  
	n = len(tickers)  
	nperiods = nrow(prices)
	
	# Buy &amp; Hold	
	data$weight[] = 1
	buy.hold = bt.run(data)	

		
	# Mean-Reversion(MR) strategy - RSI2
	rsi2 = bt.apply.matrix(prices, RSI, 2)
	data$weight[] = NA
		data$weight[] = iif(rsi2 &lt; 50, 1, -1)	
		capital = 100000
		data$weight[] = (capital / prices) * bt.exrem(data$weight)
	mr = bt.run(data, type='share', capital=capital, trade.summary=T)
				
		
	# Trend Following(TF) strategy - MA 50/200 crossover
	sma.short = bt.apply.matrix(prices, SMA, 50)
	sma.long = bt.apply.matrix(prices, SMA, 200)
	data$weight[] = NA
		data$weight[] = iif(sma.short &gt; sma.long, 1, -1)
		capital = 100000
		data$weight[] = (capital / prices) * bt.exrem(data$weight)
	tf = bt.run(data, type='share', capital=capital, trade.summary=T)
		
	#*****************************************************************
	# Create Report
	#****************************************************************** 
	plotbt.custom.report.part1(mr, tf, buy.hold)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small.png?w=600&#038;h=500" alt="" title="plot1.png.small" width="600" height="500" class="alignnone size-full wp-image-706" /></a></p>
<p>Next, let’s create a strategy that switches between mean-reversion and trend-following strategies based on historical market volatility.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Regime Switching based on historical market volatility
	# Classify current volatility by percentile using a 252 day look-back period
	# percentrank(MA(percentrank(Stdev( diff(log(close)) ,21),252),21),250)
	#****************************************************************** 
	ret.log = bt.apply.matrix(prices, ROC, type='continuous')
	hist.vol = bt.apply.matrix(ret.log, runSD, n = 21)
	vol.rank = percent.rank(SMA(percent.rank(hist.vol, 252), 21), 250)

	# Regime Switching Historical
	data$weight[] = NA
		data$weight[] = iif(vol.rank &gt; 0.5, 
					iif(rsi2 &lt; 50, 1, -1),
					iif(sma.short &gt; sma.long, 1, -1)
				)
		capital = 100000
		data$weight[] = (capital / prices) * bt.exrem(data$weight)
	regime.switching = bt.run(data, type='share', capital=capital, trade.summary=T)
	
	#*****************************************************************
	# Create Report
	#****************************************************************** 
	plotbt.custom.report.part1(regime.switching, mr, tf, buy.hold)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot2-small.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot2-small.png?w=600&#038;h=500" alt="" title="plot2.png.small" width="600" height="500" class="alignnone size-full wp-image-707" /></a></p>
<p>Next, let’s create a GARCH(1,1) Volatility Forecast. I would recommend reading following articles for anyone who wants to find what GARCH is all about or to refresh their knowledge:</p>
<ul>
<li><a href="http://www.bionicturtle.com/how-to/article/garch11/" target="_blank">GARCH(1,1) by by David Harper</a> &#8211; a very good introductory article with lots of visual diagrams.</li>
<li><a href="http://www.rmetrics.org/Meielisalp2008/Presentations/Chalabi2.pdf" target="_blank">Practical Issues in Univariate GARCH Modelling by Y. Chalabi, D. Wurtz</a> &#8211; step by step example of fitting GARCH(1,1) model with full R code.</li>
<li><a href="http://quantumfinancier.wordpress.com/?s=garch" target="_blank">Basic Introduction to GARCH</a> by <a href="http://quantumfinancier.wordpress.com" target="_blank">Quantum Financier</a> &#8211; is a series of posts that goes in to the details and assumptions of GARCH and EGARCH.</li>
</ul>
<p>There are a few R packages to fit GARCH models. I will consider garch function from tseries package and garchFit function from fGarch package. The garch function from tseries package is fast but does not always find solution. The garchFit function from fGarch package is slower but does converge more consistently. To demonstrate the speed difference between garch function and garchFit function I created a simple benchmark:</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Benchmarking Garch algorithms 
	#****************************************************************** 
	load.packages('tseries,fGarch,rbenchmark')	
	temp = garchSim(n=252)

	test1 &lt;- function() {
		fit1=garch(temp, order = c(1, 1), control = garch.control(trace = F))
	}
	test2 &lt;- function() {
		fit2=garchFit(~ garch(1,1), data = temp, include.mean=FALSE, trace=F)
	}
		 	
	benchmark(
		test1(),
		test2(),
		columns=spl('test,replications,elapsed,relative'),
		order='relative',
		replications=100
	)
</pre></p>
<p>The garchFit function is on average 6 times slower than garch function. So to forecast volatility I will try to use garch function whenever it can find a solution and garchFit function otherwise.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Forecast Volatility using Garch	
	# garch from tseries is fast, but does not consistently converge
	# garchFit from fGarch is slower, but converges consistently
	#****************************************************************** 
	load.packages('tseries,fGarch')	
			
	# Sigma[t]^2 = w + a* Sigma[t-1]^2 + b*r[t-1]^2
	garch.predict.one.day &lt;- function(fit, r.last) 
	{
		h.last = tail( fitted(fit)[,1] ,1)			
		sqrt(sum( coef(fit) * c(1,  r.last^2, h.last^2) ))	
	}

	# same as predict( fit, n.ahead=1, doplot=F)[3]
	garchFit.predict.one.day &lt;- function(fit, r.last) 
	{
		h.last = tail(sqrt(fit@h.t), 1)
		sqrt(sum( fit@fit$matcoef[,1] * c(1,  r.last^2, h.last^2) ))
	}
	
	garch.vol = NA * hist.vol
	for( i in (252+1):nperiods ) {
		temp = as.vector(ret.log[ (i-252+1):i, ])
		r.last =  tail( temp, 1 )
			
		fit = tryCatch( garch(temp, order = c(1, 1), control = garch.control(trace = F)),
	    				error=function( err ) FALSE, warning=function( warn ) FALSE )
	                    
		if( !is.logical( fit ) ) {
			if( i == 252+1 ) garch.vol[1:252] = fitted(fit)[,1]
			garch.vol[i] = garch.predict.one.day(fit, r.last)
		} else {
			fit = tryCatch( garchFit(~ garch(1,1), data = temp, include.mean=FALSE, trace=F),
	    				error=function( err ) FALSE, warning=function( warn ) FALSE )
	    				
			if( !is.logical( fit ) ) {
				if( i == 252+1 ) garch.vol[1:252] = sqrt(fit@h.t)
				garch.vol[i] = garchFit.predict.one.day(fit, r.last)
			} 
		}			
		if( i %% 100 == 0) cat(i, '\n')
	}
	garch.vol = ifna.prev(garch.vol)
</pre></p>
<p>Now, let’s create a strategy that switches between mean-reversion and trend-following strategies based on GARCH(1,1) volatility forecast.</p>
<p><pre class="brush: r;">
	#*****************************************************************
	# Regime Switching using Garch
	#****************************************************************** 		
	vol.rank = percent.rank(SMA(percent.rank(garch.vol, 252), 21), 250)

	# Regime Switching Garch
	data$weight[] = NA
		data$weight[] = iif(vol.rank &gt; 0.5, 
					iif(rsi2 &lt; 50, 1, -1),
					iif(sma.short &gt; sma.long, 1, -1)
				)
		capital = 100000
		data$weight[] = (capital / prices) * bt.exrem(data$weight)
	regime.switching.garch = bt.run(data, type='share', capital=capital, trade.summary=T)	

	#*****************************************************************
	# Create Report
	#****************************************************************** 
	plotbt.custom.report.part1(regime.switching.garch, regime.switching, buy.hold)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2012/01/plot3-small.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2012/01/plot3-small.png?w=600&#038;h=500" alt="" title="plot3.png.small" width="600" height="500" class="alignnone size-full wp-image-705" /></a></p>
<p>The switching strategy that uses GARCH(1,1) volatility forecast performed slightly better than the one that uses historical volatility.</p>
<p>There many different approaches you can take to incorporate forecasting into your models and trading strategies. R has a very rich set of packages to model and forecast time series. Here are some examples that I found interesting:</p>
<ul>
<li><a href="http://www.portfolioprobe.com/2012/01/02/market-predictions-for-years-2011-and-2012/" target="_blank">Market predictions for years 2011 and 2012</a> by Pat Burns &#8211; uses GARCH(1,1) to calibrate the significance of other people’s market predictions.</li>
<li><a href="http://theaverageinvestor.wordpress.com/tag/garch/" target="_blank">ARMA Models for Trading</a> by <a href="http://theaverageinvestor.wordpress.com" target="_blank">The Average Investor</a> &#8211; is a series of posts that shows how to forecast next day returns using ARIMA and GARCH models.</li>
<li><a href="http://timelyportfolio.blogspot.com/2011/05/wonderful-new-blog-timeseriesireland.html" target="_blank">Wonderful New Blog TimeSeriesIreland</a> at <a href="http://timelyportfolio.blogspot.com" target="_blank">Timely Portfolio</a> &#8211; uses EGRACH to create trading model.</li>
<li><a href="http://dancingeconomist.blogspot.com/2011/08/forecasting-in-r-greatest-shortcut-that.html" target="_blank">Forecasting In R: The Greatest Shortcut That Failed The Ljung-Box</a> &#8211; uses ARIMA models to forecast GDP.</li>
</ul>
<p>To view the complete source code for this example, please have a look at the <a href="https://github.com/systematicinvestor/SIT/blob/master/R/bt.test.r" target="_blank">bt.volatility.garch() function in bt.test.r at github</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/systematicinvestor.wordpress.com/686/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/systematicinvestor.wordpress.com/686/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/systematicinvestor.wordpress.com/686/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/systematicinvestor.wordpress.com/686/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/systematicinvestor.wordpress.com/686/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/systematicinvestor.wordpress.com/686/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/systematicinvestor.wordpress.com/686/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/systematicinvestor.wordpress.com/686/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/systematicinvestor.wordpress.com/686/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/systematicinvestor.wordpress.com/686/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/systematicinvestor.wordpress.com/686/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/systematicinvestor.wordpress.com/686/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/systematicinvestor.wordpress.com/686/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/systematicinvestor.wordpress.com/686/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=686&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://systematicinvestor.wordpress.com/2012/01/06/trading-using-garch-volatility-forecast/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f5676a7cfc17017cd99e8266c814227b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">systematicinvestor</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot1-small.png" medium="image">
			<media:title type="html">plot1.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot2-small.png" medium="image">
			<media:title type="html">plot2.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2012/01/plot3-small.png" medium="image">
			<media:title type="html">plot3.png.small</media:title>
		</media:content>
	</item>
		<item>
		<title>Historical Seasonality Analysis: What company in DOW 30 is likely to do well in January?</title>
		<link>http://systematicinvestor.wordpress.com/2011/12/30/historical-seasonality-analysis-what-company-in-dow-30-is-likely-to-do-well-in-january/</link>
		<comments>http://systematicinvestor.wordpress.com/2011/12/30/historical-seasonality-analysis-what-company-in-dow-30-is-likely-to-do-well-in-january/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 03:24:03 +0000</pubDate>
		<dc:creator>systematicinvestor</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://systematicinvestor.wordpress.com/?p=666</guid>
		<description><![CDATA[THIS IS NOT INVESTMENT ADVICE. The information is provided for informational purposes only. Stock Market Seasonality is easy to understand, but hard to justify if you subscribe to Efficient-market hypothesis. For a good summary of seasonality, have a look at Capitalizing On Seasonal Effects article at Investopedia. Another interesting discussion was started by Douglas R [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=666&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>THIS IS NOT INVESTMENT ADVICE. The information is provided for informational purposes only.</p>
<p>Stock Market Seasonality is easy to understand, but hard to justify if you subscribe to <a href="http://en.wikipedia.org/wiki/Efficient-market_hypothesis" target="_blank">Efficient-market hypothesis</a>. For a good summary of seasonality, have a look at <a href="http://www.investopedia.com/articles/05/seasonaltrends.asp#axzz1hxtr1mbl" target="_blank">Capitalizing On Seasonal Effects</a> article at Investopedia. Another interesting discussion was started by Douglas R Terry in his post <a href="http://www.alhambrapartners.com/2011/12/11/efficient-market-hypothesis-and-apple-seasonality/" target="_blank">Efficient Market Hypothesis and Seasonality</a> that goes into detail analysis how these two ideas can co-exist.</p>
<p>To study Seasonality, I want to introduce the new tool I developed. The <a href="http://www.systematicportfolio.com/tools" target="_blank">Seasonality Tool</a> is a user-friendly, point and click, application to create seasonality statistics and reports. </p>
<p>To demonstrate the <a href="http://www.systematicportfolio.com/tools" target="_blank">Seasonality Tool</a>, I want to show how it can be used to evaluate an investment idea. I want find which company in <a href="http://en.wikipedia.org/wiki/Dow_Jones_Industrial_Average" target="_blank">DOW 30</a> is likely to do well in January and evaluate it using <a href="http://www.systematicportfolio.com/tools" target="_blank">Seasonality Tool</a>.</p>
<p>Following code loads historical prices from Yahoo Fiance for all companies in the <a href="http://en.wikipedia.org/wiki/Dow_Jones_Industrial_Average" target="_blank">DOW 30</a> index and computes their average performance in January. I will use the <a href="http://systematicinvestor.wordpress.com/systematic-investor-toolbox/" target="_blank">Systematic Investor Toolbox</a> to load and analyze the data:</p>
<p><pre class="brush: r;">
###############################################################################
# Load Systematic Investor Toolbox (SIT)
###############################################################################
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb'))
    source(con)
close(con)

    #*****************************************************************
    # Load historical data
    #****************************************************************** 
    load.packages('quantmod')        
    tickers = dow.jones.components()
    
    data &lt;- new.env()
    getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
        for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)    
    bt.prep(data, align='keep.all', dates='1970::2011')
        
    #*****************************************************************
    # Compute monthly returns
    #****************************************************************** 
    prices = data$prices   
    n = ncol(prices)    
    
    # find month ends
    month.ends = endpoints(prices, 'months')
    
    prices = prices[month.ends,]
    ret = prices / mlag(prices) - 1

    # keep only January    
    ret = ret[date.month(index(ret)) == 1, ]
    
    # keep last 20 years
    ret = last(ret,20)

    #*****************************************************************
    # Compute stats
    #****************************************************************** 
    stats = matrix(rep(NA,2*n), nc=n)
        colnames(stats) = colnames(prices)
        rownames(stats) = spl('N,Positive')
        
    for(i in 1:n) {
        stats['N',i] = sum(!is.na(ret[,i]))
        stats['Positive',i] = sum(ret[,i]&gt;0, na.rm=T)    
    }
    sort(stats['Positive',])
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2011/12/plot1-small6.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2011/12/plot1-small6.png?w=600&#038;h=200" alt="" title="plot1.png.small" width="600" height="200" class="alignnone size-full wp-image-669" /></a></p>
<p>The <a href="http://finance.yahoo.com/q?s=dis" target="_blank">Walt Disney Co. (DIS)</a> was positive 17 times in January in the last 20 years.<br />
Let&#8217;s investigate the <a href="http://finance.yahoo.com/q?s=dis" target="_blank">Walt Disney Co. (DIS)</a> record using  the <a href="http://www.systematicportfolio.com/tools" target="_blank">Seasonality Tool</a>.</p>
<p><a href="http://systematicinvestor.files.wordpress.com/2011/12/plot2-small5.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2011/12/plot2-small5.png?w=600&#038;h=450" alt="" title="plot2.png.small" width="600" height="450" class="alignnone size-full wp-image-670" /></a></p>
<p>The Seasonality Analysis Report confirms <a href="http://finance.yahoo.com/q?s=dis" target="_blank">Walt Disney Co. (DIS)</a> outstanding track record in January. Next let&#8217;s see the details for January.</p>
<p><a href="http://systematicinvestor.files.wordpress.com/2011/12/plot3-small5.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2011/12/plot3-small5.png?w=600&#038;h=450" alt="" title="plot3.png.small" width="600" height="450" class="alignnone size-full wp-image-668" /></a></p>
<p>The Detail Seasonality Analysis Report shows that the <a href="http://finance.yahoo.com/q?s=dis" target="_blank">Walt Disney Co. (DIS)</a> had an amazing returns in January till 2008. Following by a 3 consecutive negative Januaries which most likely indicates a change in trend (regime) for this company. </p>
<p>So do I expect the <a href="http://finance.yahoo.com/q?s=dis" target="_blank">Walt Disney Co. (DIS)</a> be positive in the upcoming January? I&#8217;m not so sure anymore.</p>
<p>To view the complete source code for this example, please have a look at the <a href="https://github.com/systematicinvestor/SIT/blob/master/R/bt.test.r" target="_blank">bt.seasonality.test() function in bt.test.r at github</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/systematicinvestor.wordpress.com/666/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/systematicinvestor.wordpress.com/666/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/systematicinvestor.wordpress.com/666/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/systematicinvestor.wordpress.com/666/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/systematicinvestor.wordpress.com/666/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/systematicinvestor.wordpress.com/666/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/systematicinvestor.wordpress.com/666/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/systematicinvestor.wordpress.com/666/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/systematicinvestor.wordpress.com/666/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/systematicinvestor.wordpress.com/666/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/systematicinvestor.wordpress.com/666/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/systematicinvestor.wordpress.com/666/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/systematicinvestor.wordpress.com/666/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/systematicinvestor.wordpress.com/666/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=666&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://systematicinvestor.wordpress.com/2011/12/30/historical-seasonality-analysis-what-company-in-dow-30-is-likely-to-do-well-in-january/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f5676a7cfc17017cd99e8266c814227b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">systematicinvestor</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2011/12/plot1-small6.png" medium="image">
			<media:title type="html">plot1.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2011/12/plot2-small5.png" medium="image">
			<media:title type="html">plot2.png.small</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2011/12/plot3-small5.png" medium="image">
			<media:title type="html">plot3.png.small</media:title>
		</media:content>
	</item>
		<item>
		<title>Happy Holidays and Best Wishes for 2012</title>
		<link>http://systematicinvestor.wordpress.com/2011/12/23/happy-holidays-and-best-wishes-for-2012/</link>
		<comments>http://systematicinvestor.wordpress.com/2011/12/23/happy-holidays-and-best-wishes-for-2012/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 04:50:42 +0000</pubDate>
		<dc:creator>systematicinvestor</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://systematicinvestor.wordpress.com/?p=658</guid>
		<description><![CDATA[This is just a quick note to wish you and your family a very healthy and happy holidays and wonderful New Year! I hope you enjoyed reading my blog and thank you for your comments and emails. Here is a short R code that implements an interesting idea from the Charting the Santa Claus Rally [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=658&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is just a quick note to wish you and your family a very healthy and happy holidays and wonderful New Year! I hope you enjoyed reading my blog and thank you for your comments and emails.</p>
<p>Here is a short R code that implements an interesting idea from the <a href="http://ibankcoin.com/woodshedderblog/2011/12/15/charting-the-santa-claus-rally/" target="_blank">Charting the Santa Claus Rally</a> post by Woodshedder. I will plot and compare the SPY performance this December versus average performance in previous Decembers. </p>
<p><pre class="brush: r;">
# Load Systematic Investor Toolbox (SIT)
setInternet2(TRUE)
con = gzcon(url('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', 'rb'))
	source(con)
close(con)

	#*****************************************************************
	# Load historical data
	#****************************************************************** 
	load.packages('quantmod')	
	tickers = spl('SPY')

	data &lt;- new.env()
	getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T)
		for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)		
	bt.prep(data, align='remove.na', dates='1970::2011')

	#*****************************************************************
	# Prepare Data for the plot
	#****************************************************************** 
	prices = data$prices  
	n = len(tickers)  
	ret = prices / mlag(prices) - 1

	
	# find prices in December
	dates = index(prices)
	years = date.year(dates)	
	index = which(date.month(dates) == 12)
	
	# rearrange data in trading days
	trading.days = sapply(tapply(ret[index,], years[index], function(x) coredata(x)), function(x) x[1:22])
		
	# average return each trading days, excluding current year
	avg.trading.days = apply(trading.days[, -ncol(trading.days)], 1, mean, na.rm=T)
	current.year = trading.days[, ncol(trading.days)]
	
	# cumulative
	avg.trading.days = 100 * ( cumprod(1 + avg.trading.days) - 1 )
	current.year = 100 * ( cumprod(1 + current.year) - 1 )
	
	#*****************************************************************
	# Create Plot
	#****************************************************************** 	
	par(mar=c(4,4,1,1))
	plot(avg.trading.days, type='b', col=1,
		ylim=range(avg.trading.days,current.year,na.rm=T),
		xlab = 'Number of Trading Days in December',
		ylab = 'Avg % Profit/Loss'
		)
		lines(current.year, type='b', col=2)
	grid()
	plota.legend('Avg SPY,SPY Dec 2011', 1:2)
</pre></p>
<p><a href="http://systematicinvestor.files.wordpress.com/2011/12/plot1-small5.png" target="_blank"><img src="http://systematicinvestor.files.wordpress.com/2011/12/plot1-small5.png?w=600&#038;h=500" alt="" title="plot1.png.small" width="600" height="500" class="alignnone size-full wp-image-659" /></a></p>
<p>Hope this year will not disappoint and we will see the rally towards the year end.</p>
<p>If you want to find average performance in the other months, I recommend reading <a href="http://www.cxoadvisory.com/trading-calendar/" target="_blank">Trading Calendar</a> article by CXO Advisory.</p>
<p>To view the complete source code for this example, please have a look at the <a href="https://github.com/systematicinvestor/SIT/blob/master/R/bt.test.r" target="_blank">bt.december.trading.test() function in bt.test.r at github</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/systematicinvestor.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/systematicinvestor.wordpress.com/658/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/systematicinvestor.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/systematicinvestor.wordpress.com/658/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/systematicinvestor.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/systematicinvestor.wordpress.com/658/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/systematicinvestor.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/systematicinvestor.wordpress.com/658/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/systematicinvestor.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/systematicinvestor.wordpress.com/658/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/systematicinvestor.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/systematicinvestor.wordpress.com/658/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/systematicinvestor.wordpress.com/658/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/systematicinvestor.wordpress.com/658/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=systematicinvestor.wordpress.com&amp;blog=28096251&amp;post=658&amp;subd=systematicinvestor&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://systematicinvestor.wordpress.com/2011/12/23/happy-holidays-and-best-wishes-for-2012/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f5676a7cfc17017cd99e8266c814227b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">systematicinvestor</media:title>
		</media:content>

		<media:content url="http://systematicinvestor.files.wordpress.com/2011/12/plot1-small5.png" medium="image">
			<media:title type="html">plot1.png.small</media:title>
		</media:content>
	</item>
	</channel>
</rss>
