<?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>Alphaism</title>
	<atom:link href="http://alphaism.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://alphaism.wordpress.com</link>
	<description>Exploring investment ideas and strategies</description>
	<lastBuildDate>Tue, 19 Mar 2013 19:10:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='alphaism.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Alphaism</title>
		<link>http://alphaism.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://alphaism.wordpress.com/osd.xml" title="Alphaism" />
	<atom:link rel='hub' href='http://alphaism.wordpress.com/?pushpress=hub'/>
		<item>
		<title>The Power of Mass Deployment</title>
		<link>http://alphaism.wordpress.com/2012/11/27/the-power-of-mass-deployment/</link>
		<comments>http://alphaism.wordpress.com/2012/11/27/the-power-of-mass-deployment/#comments</comments>
		<pubDate>Tue, 27 Nov 2012 21:50:02 +0000</pubDate>
		<dc:creator>alphaism</dc:creator>
				<category><![CDATA[Back-tests]]></category>

		<guid isPermaLink="false">http://alphaism.wordpress.com/?p=952</guid>
		<description><![CDATA[The power of statistics emerges as the sample size grows. I know, it has been repeated multiple times in Stats 101, a bunch of youtube videos, or maybe Statistics for Dummies. But has it stopped people from making judgement calls on purely empirical basis? Statements like &#8220;I&#8217;ve seen it working x times, so it&#8217;s legit&#8221; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=952&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The power of statistics emerges as the sample size grows. I know, it has been repeated multiple times in Stats 101, a bunch of youtube videos, or maybe Statistics for Dummies. But has it stopped people from making judgement calls on purely empirical basis? Statements like &#8220;I&#8217;ve seen it working x times, so it&#8217;s legit&#8221; or &#8220;it&#8217;s a bad indicator because I tried on several stocks and it didn&#8217;t work&#8221; don&#8217;t really make sense when you are living in a complex realm composed by incredible amount of data, multiple dimensions of reality and endless chain reactions such as public administration and stock investments.</p>
<p>To illustrate, I did a back-test using a simple combination of <a href="http://stockcharts.com/school/doku.php?st=Bollinger+Bands&amp;id=chart_school:technical_indicators:bollinger_bands">Bollinger Bands</a> and <a href="http://stockcharts.com/school/doku.php?st=macd&amp;id=chart_school:technical_indicators:moving_average_conve">MACD Indicator</a> from 2004 to 2012. It&#8217;s an end-of-day, mean-reversion strategy with a price filter and a liquidity filter. After testing it on 30 random stocks listed on TSX, this is what I got.</p>
<p><a href="http://alphaism.files.wordpress.com/2012/11/sample.png"><img src="http://alphaism.files.wordpress.com/2012/11/sample.png?w=640&#038;h=457" alt="" title="sample" width="640" height="457" class="alignnone size-full wp-image-953" /></a></p>
<p>Nope, not impressive. But it looks quite different if we deploy it to the entire market, which is about 2300 stocks listed on TSX (1400 after using survivor filter ).</p>
<p><a href="http://alphaism.files.wordpress.com/2012/11/strat.png"><img src="http://alphaism.files.wordpress.com/2012/11/strat.png?w=640&#038;h=512" alt="" title="strat" width="640" height="512" class="alignnone size-full wp-image-955" /></a></p>
<p>Commission is not a concern. As shown below, most of the time the strategy only holds less than 2 stocks, not 200.</p>
<p><a href="http://alphaism.files.wordpress.com/2012/11/holdings.png"><img src="http://alphaism.files.wordpress.com/2012/11/holdings.png?w=640&#038;h=640" alt="" title="holdings" width="640" height="640" class="alignnone size-full wp-image-957" /></a></p>
<p>The real problem for implementing this strategy, for retail investors, is computing power. Gathering latest data, completing calculation and executing trades right before market closes every day precisely is very challenging for individuals. For big players, it&#8217;s liquidity. Because the strategy targets low liquidity segments, it can&#8217;t guarantee the trading volumes will be big enough for institutional traders.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alphaism.wordpress.com/952/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alphaism.wordpress.com/952/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=952&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alphaism.wordpress.com/2012/11/27/the-power-of-mass-deployment/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3ff59ed22f22521815577d5fc2cf4bb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alphaism</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/11/sample.png" medium="image">
			<media:title type="html">sample</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/11/strat.png" medium="image">
			<media:title type="html">strat</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/11/holdings.png" medium="image">
			<media:title type="html">holdings</media:title>
		</media:content>
	</item>
		<item>
		<title>Neural Network Algorithm</title>
		<link>http://alphaism.wordpress.com/2012/11/13/neural-network-algorithm/</link>
		<comments>http://alphaism.wordpress.com/2012/11/13/neural-network-algorithm/#comments</comments>
		<pubDate>Wed, 14 Nov 2012 02:30:40 +0000</pubDate>
		<dc:creator>alphaism</dc:creator>
				<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://alphaism.wordpress.com/?p=882</guid>
		<description><![CDATA[This post is a token of gratitude to Andrew Ng at Stanford University. I&#8217;ve learned a lot of fascinating concepts and practical skills from his Machine Learning course at Coursera. Among these algorithms neural network is my favourite so I decided to convert this class assignment built in Octave to functions in R. I will [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=882&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This post is a token of gratitude to Andrew Ng at Stanford University. I&#8217;ve learned a lot of fascinating concepts and practical skills from his Machine Learning course at <a href="https://class.coursera.org/ml/lecture/preview/index">Coursera</a>. Among these algorithms neural network is my favourite so I decided to convert this class assignment built in Octave to functions in R. I will post my code below but if anyone who&#8217;d like to play with an exported package with documentations, please contact me by email, I will send you the tarball file. The code is also available on <a href="https://github.com/alphaism/Machine_Learning">my github repository</a>. </p>
<p>Comparing to the more comprehensive package &#8220;neuralnet&#8221; on CRAN, this is a lite version which consists all the basic components: single hidden layer, random initialization, sigmoid activation, regularized cost function, forward-propagation and backward-propagation. Originally it only works as a classifier, but I modified it a bit to create a similar algorithm to handle absolute values. I&#8217;m not sure if it&#8217;s the best practice for this kind of task but it worked as expected.</p>
<p>This is how the algorithm looks like, along with its source of inspiration, an actual human neural neuron. By mimicking the way a human brain (which is the most powerful computer in the universe) adjusts itself to recognize patterns and estimates the future, an algorithm should be able to solve similar tasks.<br />
<a href="http://alphaism.files.wordpress.com/2012/11/selection_001.png"><img src="http://alphaism.files.wordpress.com/2012/11/selection_001.png?w=640&#038;h=514" alt="" title="Selection_001" width="640" height="514" class="alignnone size-full wp-image-884" /></a></p>
<p><a href="http://alphaism.files.wordpress.com/2012/11/selection_002.png"><img src="http://alphaism.files.wordpress.com/2012/11/selection_002.png?w=640" alt="" title="Selection_002"   class="alignnone size-full wp-image-886" /></a></p>
<p>For the classifier, the cost function tracks the forward propagation process is defined below.</p>
<p><img src='http://s0.wp.com/latex.php?latex=J%28%5CTheta%29+%3D+%5Cfrac%7B1%7D%7Bm%7D%5Csum_%7Bi%3D1%7D%5E%7Bm%7D%5Csum_%7Bk%3D1%7D%5E%7BK%7D+%5Cleft+%5B+-y_%7Bk%7D%5E%7B%28i%29%7Dlog%28%28h_%7B%5CTheta%7D%28x%5E%7B%28i%29%7D%29%29_%7Bk%7D%29-%281-y_%7Bk%7D%5E%7B%28i%29%7D%29log%281-%28h%28x%5E%7B%28i%29%7D%29%29_%7Bk%7D%29+%5Cright+%5D&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='J(&#92;Theta) = &#92;frac{1}{m}&#92;sum_{i=1}^{m}&#92;sum_{k=1}^{K} &#92;left [ -y_{k}^{(i)}log((h_{&#92;Theta}(x^{(i)}))_{k})-(1-y_{k}^{(i)})log(1-(h(x^{(i)}))_{k}) &#92;right ]' title='J(&#92;Theta) = &#92;frac{1}{m}&#92;sum_{i=1}^{m}&#92;sum_{k=1}^{K} &#92;left [ -y_{k}^{(i)}log((h_{&#92;Theta}(x^{(i)}))_{k})-(1-y_{k}^{(i)})log(1-(h(x^{(i)}))_{k}) &#92;right ]' class='latex' /></p>
<p>Where <img src='http://s0.wp.com/latex.php?latex=J+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='J ' title='J ' class='latex' /> is the total cost we want to minimize, <img src='http://s0.wp.com/latex.php?latex=m+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='m ' title='m ' class='latex' /> represents number of training examples, <img src='http://s0.wp.com/latex.php?latex=K+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='K ' title='K ' class='latex' /> represents number of labels (for classification). When training for the ith example with label <img src='http://s0.wp.com/latex.php?latex=k+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='k ' title='k ' class='latex' /> and weights <img src='http://s0.wp.com/latex.php?latex=%5CTheta+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='&#92;Theta ' title='&#92;Theta ' class='latex' />, all corresponding correct output values will be converted to 1 and the rest will become 0, so <img src='http://s0.wp.com/latex.php?latex=-y_%7Bk%7D%5E%7B%28i%29%7D+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='-y_{k}^{(i)} ' title='-y_{k}^{(i)} ' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=%281-y_%7Bk%7D%5E%7B%28i%29%7D%29+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='(1-y_{k}^{(i)}) ' title='(1-y_{k}^{(i)}) ' class='latex' /> works as switches for different scenarios. Function <img src='http://s0.wp.com/latex.php?latex=h_%7B%5CTheta%7D%28x%5E%7B%28i%29%7D%29%29_%7Bk%7D%3Dg%28z%29+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='h_{&#92;Theta}(x^{(i)}))_{k}=g(z) ' title='h_{&#92;Theta}(x^{(i)}))_{k}=g(z) ' class='latex' /> calculates the training output as <img src='http://s0.wp.com/latex.php?latex=z%3D%5CTheta%5E%7BT%7Dx%5E%7Bi%7D+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='z=&#92;Theta^{T}x^{i} ' title='z=&#92;Theta^{T}x^{i} ' class='latex' />, then processes it with sigmoid function <img src='http://s0.wp.com/latex.php?latex=g%28z%29%3D%5Cfrac%7B1%7D%7B1%2Be%5E%7B-z%7D%7D&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='g(z)=&#92;frac{1}{1+e^{-z}}' title='g(z)=&#92;frac{1}{1+e^{-z}}' class='latex' /> in order to bound it between 0 to 1 as likelihood of being 1 (which is equivalent to probability of being true).</p>
<p>To avoid extreme bias caused by large weights, we add parameter regulation to the cost function. Defined as</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Clambda%7D%7B2m%7D%5Cleft+%5B+%5Csum_%7Bj%3D1%7D%5E%7Bh%7D%5Csum_%7Bk%3D1%7D%5E%7Bn%7D%28%5CTheta_%7Bj%2Ck%7D%5E%7B%281%29%7D%29%5E%7B2%7D%2B%5Csum_%7Bj%3D1%7D%5E%7Bo%7D%5Csum_%7Bk%3D1%7D%5E%7Bh%7D%28%5CTheta_%7Bj%2Ck%7D%5E%7B%282%29%7D%29%5E%7B2%7D+%5Cright+%5D&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='&#92;frac{&#92;lambda}{2m}&#92;left [ &#92;sum_{j=1}^{h}&#92;sum_{k=1}^{n}(&#92;Theta_{j,k}^{(1)})^{2}+&#92;sum_{j=1}^{o}&#92;sum_{k=1}^{h}(&#92;Theta_{j,k}^{(2)})^{2} &#92;right ]' title='&#92;frac{&#92;lambda}{2m}&#92;left [ &#92;sum_{j=1}^{h}&#92;sum_{k=1}^{n}(&#92;Theta_{j,k}^{(1)})^{2}+&#92;sum_{j=1}^{o}&#92;sum_{k=1}^{h}(&#92;Theta_{j,k}^{(2)})^{2} &#92;right ]' class='latex' /></p>
<p>where <img src='http://s0.wp.com/latex.php?latex=n+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='n ' title='n ' class='latex' />, <img src='http://s0.wp.com/latex.php?latex=h+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='h ' title='h ' class='latex' /> and <img src='http://s0.wp.com/latex.php?latex=o+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='o ' title='o ' class='latex' /> equal to numbers of nodes in the input layer, hidden layer and output layer respectively.</p>
<p>During the backward propagation process, we push the calculated outputs backwards through the algorithm and accumulate the gradient from each layer. Because there are only one hidden layer, we can calculate the gradient of the output layer as <img src='http://s0.wp.com/latex.php?latex=%5Cdelta%5E%7B3%7D%3Da_%7Bk%7D%5E%7B3%7D-y_%7Bk%7D+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='&#92;delta^{3}=a_{k}^{3}-y_{k} ' title='&#92;delta^{3}=a_{k}^{3}-y_{k} ' class='latex' /> and hidden layer as <img src='http://s0.wp.com/latex.php?latex=%5Cdelta%5E%7B2%7D%3D%28%5CTheta%5E%7B2%7D%29%5E%7BT%7D%5Cdelta%5E%7B3%7D%7B%5Cast%7Dg%5E%7B%27%7D%28z%5E%7B%282%29%7D%29+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='&#92;delta^{2}=(&#92;Theta^{2})^{T}&#92;delta^{3}{&#92;ast}g^{&#039;}(z^{(2)}) ' title='&#92;delta^{2}=(&#92;Theta^{2})^{T}&#92;delta^{3}{&#92;ast}g^{&#039;}(z^{(2)}) ' class='latex' />, then accumulate it <img src='http://s0.wp.com/latex.php?latex=%5CDelta%5E%7B%28l%29%7D%3D%5CDelta%5E%7B%28l%29%7D%2B%5Cdelta%5E%7B%28l%2B1%29%7D%28a%5E%7B%28l%29%7D%29%5E%7BT%7D+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='&#92;Delta^{(l)}=&#92;Delta^{(l)}+&#92;delta^{(l+1)}(a^{(l)})^{T} ' title='&#92;Delta^{(l)}=&#92;Delta^{(l)}+&#92;delta^{(l+1)}(a^{(l)})^{T} ' class='latex' /> to finally get the total unregularized gradient <img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Cpartial+%7D%7B%5Cpartial+%5CTheta%5E%7B%28l%29%7D_%7Bij%7D%7DJ%28%5CTheta%29%3D%5Cfrac%7B1%7D%7Bm%7D%5CDelta%5E%7B%28l%29%7D_%7B%28ij%29%7D+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='&#92;frac{&#92;partial }{&#92;partial &#92;Theta^{(l)}_{ij}}J(&#92;Theta)=&#92;frac{1}{m}&#92;Delta^{(l)}_{(ij)} ' title='&#92;frac{&#92;partial }{&#92;partial &#92;Theta^{(l)}_{ij}}J(&#92;Theta)=&#92;frac{1}{m}&#92;Delta^{(l)}_{(ij)} ' class='latex' />. In the end, we just need to regularize it like we did to the cost function by adding <img src='http://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Clambda%7D%7Bm%7D%5CTheta%5E%7B%28l%29%7D_%7Bij%7D+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='&#92;frac{&#92;lambda}{m}&#92;Theta^{(l)}_{ij} ' title='&#92;frac{&#92;lambda}{m}&#92;Theta^{(l)}_{ij} ' class='latex' />.</p>
<p>The absolute value algorithm is very similar. Instead of using a 0/1 (True/False) switch to train examples with same labels all at once, it trains one example at a time and calculate the cost by squared error of each prediction <img src='http://s0.wp.com/latex.php?latex=%28y%5E%7B%28i%29%7D-h_%7B%5CTheta%7D%28x%5E%7B%28i%29%7D%29%29%5E%7B2%7D+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='(y^{(i)}-h_{&#92;Theta}(x^{(i)}))^{2} ' title='(y^{(i)}-h_{&#92;Theta}(x^{(i)}))^{2} ' class='latex' />.</p>
<p>Now we can wrap up a cost function and a gradient function to test it by running optim() provided by default R package &#8220;stat&#8221;. In this case I choose L-BFGS-B method because it utilizes everything that we&#8217;ve got in hand and seems learns faster than most of the others.</p>
<p>The classifier first. Three blocks of data are randomly generated and labelled for training. Because we intentionally created them for testing purpose, it&#8217;s not necessary to divide them for cross-validation and out-of-sample testing contrast to using it in real world.</p>
<pre class="brush: r; title: ; notranslate">
X &lt;- rbind(matrix(rnorm(100, 10, 1), 20, 5),
           matrix(rnorm(200, 5, 2), 40, 5),
           matrix(rnorm(150, 1, 0.1), 30, 5));
y_cla &lt;- c(rep(3, 20), rep(1, 40), rep(2, 30));
test_cla &lt;- nnet.train.cla(X, y_cla, hidden_layer_size = 25,
                   lambda = 5, maxit = 50);
pred_cla &lt;- nnet.predict.cla(test_cla$Theta1, test_cla$Theta2, X);
plot(y_cla, col = &quot;green&quot;, pch = 15);
points(pred_cla, col = &quot;red&quot;, pch = 3);
legend(&quot;topright&quot;, legend = c(&quot;Original&quot;, &quot;Predicted&quot;), pch = c(15, 3), col = c(&quot;green&quot;, &quot;red&quot;));
</pre>
<p><a href="http://alphaism.files.wordpress.com/2012/11/clas1.png"><img src="http://alphaism.files.wordpress.com/2012/11/clas1.png?w=640" alt="" title="clas"   class="alignnone size-full wp-image-947" /></a></p>
<p>Then for absolute values. I reset y values for this test and tried several values for learning speed <img src='http://s0.wp.com/latex.php?latex=lamda+&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='lamda ' title='lamda ' class='latex' /> and number of iterations to get results below.</p>
<pre class="brush: r; title: ; notranslate">
y_abs &lt;- c(rep(-3, 20), rep(1, 40), rep(2, 30));
test_abs &lt;- nnet.train.abs(X, y_abs, hidden_layer_size = 25,
                   lambda = 0.0001, maxit = 400);
pred_abs &lt;- nnet.predict.abs(test_abs$Theta1, test_abs$Theta2, X);
plot(sigmoid(y_abs), type = 'l', col = &quot;green&quot;, ylim = c(0, 1));
lines(pred_abs, col = &quot;red&quot;);
legend(&quot;topright&quot;, legend = c(&quot;Original&quot;, &quot;Predicted&quot;), col = c(&quot;green&quot;, &quot;red&quot;), lwd = c(1, 1));
</pre>
<p><a href="http://alphaism.files.wordpress.com/2012/11/abs1.png"><img src="http://alphaism.files.wordpress.com/2012/11/abs1.png?w=640" alt="" title="abs"   class="alignnone size-full wp-image-948" /></a></p>
<p>Source code of the algorithm. Apparently it&#8217;s not very concise. Like said, package is provided upon request.</p>
<pre class="brush: r; title: ; notranslate">
## =============== random initialization ===============
randInitializeWeights &lt;- function(L_in, L_out) {
  theta &lt;- matrix(0, L_out, 1 + L_in);
  epsilon_init &lt;- 0.12;
  theta &lt;- matrix(rnorm(L_out*(L_in + 1)),
              L_out, (L_in + 1))*2*epsilon_init - epsilon_init;
  return(theta);
}
## =============== sigmoid function ===============
sigmoid &lt;- function(z) {
  g &lt;- 1 / (1 + exp(-z));
  return(g);
}
## =============== sigmoid gradient function ===============
sigmoidGradient &lt;- function(z) {
  g &lt;- matrix(0, dim(z));
  g &lt;- sigmoid(z);
  g &lt;- g * (1 -g);
  return(g);
}


# ++++++++++++++++++ absolute value predictor functions ++++++++++++++++++
## =============== cost J and gradient ===============
nnCostFuncion.abs &lt;- function(nn_params,
                           input_layer_size,
                           hidden_layer_size,
                           X, y, lambda) {
  Theta1 &lt;- matrix(nn_params[1:(hidden_layer_size *
                   (input_layer_size + 1))],
                   hidden_layer_size,
                   (input_layer_size + 1));
  Theta2 &lt;- matrix(nn_params[-1:-(hidden_layer_size *
                   (input_layer_size + 1))],
                   1,
                   (hidden_layer_size + 1));
  m &lt;- dim(X)[1];
  J &lt;- 0;
  Theta1_grad &lt;- matrix(0, nrow(Theta1), ncol(Theta1));
  Theta2_grad &lt;- matrix(0, nrow(Theta2), ncol(Theta2));

  for (i in 1:m) {
    y_sig &lt;- sigmoid(y[i]);
  ## forward propagation
    # first feed
    a1 &lt;- X[i, ];
    a1 &lt;- c(1, a1);
    # first hidden layer
    z2 &lt;- Theta1 %*% a1;
    a2 &lt;- sigmoid(z2);
    a2 &lt;- c(1, a2);
    # output layer
    z3 &lt;- Theta2 %*% a2;
    a3 &lt;- sigmoid(z3);
    # add to cost function
    J &lt;- J + (a3 - y_sig)^2 / 2;
  ## backward propagation
    delta3 &lt;- a3 * (1 - a3) * (a3 - y_sig);
    delta2 &lt;- (t(Theta2) %*% delta3)[-1] * sigmoidGradient(z2);

    Theta1_grad &lt;- Theta1_grad + (delta2 %*% a1);
    Theta2_grad &lt;- Theta2_grad + (delta3 %*% a2);    
  }
  
  J &lt;- J / m;
  Theta1_grad &lt;- Theta1_grad / m;
  Theta2_grad &lt;- Theta2_grad / m;

  # J regulization
  reg_theta1 &lt;- Theta1[, -1];
  reg_theta2 &lt;- Theta2[, -1];
  J &lt;- J + (lambda/(2*m)) * (sum(reg_theta1^2) + sum(reg_theta2^2));
  # gradient regulization
  Theta1_grad[, -1] &lt;- Theta1_grad[, -1] + (lambda/m) * Theta1[, -1];
  Theta2_grad[, -1] &lt;- Theta2_grad[, -1] + (lambda/m) * Theta2[, -1];

  # unroll gradients
  grad &lt;- c(c(Theta1_grad), c(Theta2_grad));

  return(list(J = J, grad = grad));
}
## =============== cost J function for optimization ===============
costFunction.abs &lt;- function(nn_params,
                           input_layer_size,
                           hidden_layer_size,
                           X, y, lambda) {
  costJ &lt;- nnCostFuncion.abs(nn_params = nn_params,
                          input_layer_size = input_layer_size,
                          hidden_layer_size = hidden_layer_size,
                          X = X, y = y, lambda = lambda)$J;
  return(costJ);
}
## ========== cost J gradient function for optimization ==========
gradFunction.abs &lt;- function(nn_params,
                           input_layer_size,
                           hidden_layer_size,
                           X, y, lambda) {
  grad &lt;- nnCostFuncion.abs(nn_params = nn_params,
                          input_layer_size = input_layer_size,
                          hidden_layer_size = hidden_layer_size,
                          X = X, y = y, lambda = lambda)$grad;
  return(grad);
}
#################### end of utility functions ####################

## ================ train nerual network ===============
nnet.train.abs &lt;- function(X, y, hidden_layer_size = 25,
                 lambda = 1, maxit = 50) {
  m &lt;- nrow(X);
  input_layer_size &lt;- ncol(X);
  # ================ Initializing Pameters ================
  initial_Theta1 &lt;- randInitializeWeights(input_layer_size, hidden_layer_size);
  initial_Theta2 &lt;- randInitializeWeights(hidden_layer_size, 1);
  initial_nn_params = c(c(initial_Theta1), c(initial_Theta2));

  # =================== Training NN ===================
  train_results &lt;- optim(par = initial_nn_params,
                       fn = costFunction.abs,
                       input_layer_size = input_layer_size,
                       hidden_layer_size = hidden_layer_size,
                       X = X, y = y, lambda = lambda,
                       gr = gradFunction.abs,
                       method = &quot;L-BFGS-B&quot;,
                       control = list(maxit = maxit, trace = TRUE, REPORT = 1));
  nn_params &lt;- train_results$par;
  Theta1 &lt;- matrix(nn_params[1:(hidden_layer_size*(input_layer_size+1))],
                   hidden_layer_size, (input_layer_size + 1));
  Theta2 &lt;- matrix(nn_params[-1:-(hidden_layer_size*(input_layer_size+1))],
                   1, (hidden_layer_size + 1));
  ## ================= show accuracy =================
  pred = nnet.predict.abs(Theta1, Theta2, X);
  cat(&quot;\nLogistic Prediction Error: &quot;, var(pred - sigmoid(y)), &quot;\n&quot;, sep = &quot;&quot;);
  ## =============== return thetas ===============
  return(list(Theta1 = Theta1, Theta2 = Theta2));
}

## =============== nerual network predict ===============
nnet.predict.abs &lt;- function(Theta1, Theta2, X) {
  m &lt;- nrow(X);
  if (is.null(m)) {
    m &lt;- 1; X &lt;- t(X);
  }
  p = rep(0, m);

  h1 &lt;- sigmoid(cbind(rep(1, m), X) %*% t(Theta1));
  h2 &lt;- sigmoid(cbind(rep(1, m), h1) %*% t(Theta2));
  return(h2);
}

# ++++++++++++++++++ classifier functions ++++++++++++++++++
## =============== cost J and gradient ===============
nnCostFunction.cla &lt;- function(nn_params,
                           input_layer_size,
                           hidden_layer_size,
                           num_labels,
                           X, y, lambda) {
  Theta1 &lt;- matrix(nn_params[1:(hidden_layer_size *
                   (input_layer_size + 1))],
                   hidden_layer_size,
                   (input_layer_size + 1));
  Theta2 &lt;- matrix(nn_params[-1:-(hidden_layer_size *
                   (input_layer_size + 1))],
                   num_labels,
                   (hidden_layer_size + 1));
  m &lt;- dim(X)[1];
  J &lt;- 0;
  Theta1_grad &lt;- matrix(0, nrow(Theta1), ncol(Theta1));
  Theta2_grad &lt;- matrix(0, nrow(Theta2), ncol(Theta2));

  for (i in 1:m) {
    y_label &lt;- rep(0, num_labels);
    y_label[y[i] == as.integer(levels(factor(y)))] &lt;- 1;
  ## forward propagation
    # first feed
    a1 &lt;- X[i, ];
    a1 &lt;- c(1, a1);
    # first hidden layer
    z2 &lt;- Theta1 %*% a1;
    a2 &lt;- sigmoid(z2);
    a2 &lt;- c(1, a2);
    # output layer
    z3 &lt;- Theta2 %*% a2;
    a3 &lt;- sigmoid(z3);
    # add to cost function
    J &lt;- J + sum(-y_label * log(a3) - (1 - y_label) * log(1 - a3));
  ## backward propagation
    delta3 &lt;- a3 - y_label;
    delta2 &lt;- (t(Theta2) %*% delta3)[-1] * sigmoidGradient(z2);

    Theta1_grad &lt;- Theta1_grad + (delta2 %*% a1);
    Theta2_grad &lt;- Theta2_grad + (delta3 %*% a2);    
  }
  
  J &lt;- J / m;
  Theta1_grad &lt;- Theta1_grad / m;
  Theta2_grad &lt;- Theta2_grad / m;

  # J regulization
  reg_theta1 &lt;- Theta1[, -1];
  reg_theta2 &lt;- Theta2[, -1];
  J &lt;- J + (lambda/(2*m)) * (sum(reg_theta1^2) + sum(reg_theta2^2));
  # gradient regulization
  Theta1_grad[, -1] &lt;- Theta1_grad[, -1] + (lambda/m) * Theta1[, -1];
  Theta2_grad[, -1] &lt;- Theta2_grad[, -1] + (lambda/m) * Theta2[, -1];

  # unroll gradients
  grad &lt;- c(c(Theta1_grad), c(Theta2_grad));

  return(list(J = J, grad = grad));
}
## =============== cost J function for optimization ===============
costFunction.cla &lt;- function(nn_params,
                           input_layer_size,
                           hidden_layer_size,
                           num_labels,
                           X, y, lambda) {
  costJ &lt;- nnCostFunction.cla(nn_params = nn_params,
                          input_layer_size = input_layer_size,
                          hidden_layer_size = hidden_layer_size,
                          num_labels = num_labels,
                          X = X, y = y, lambda = lambda)$J;
  return(costJ);
}
## ========== cost J gradient function for optimization ==========
gradFunction.cla &lt;- function(nn_params,
                           input_layer_size,
                           hidden_layer_size,
                           num_labels,
                           X, y, lambda) {
  grad &lt;- nnCostFunction.cla(nn_params = nn_params,
                          input_layer_size = input_layer_size,
                          hidden_layer_size = hidden_layer_size,
                          num_labels = num_labels,
                          X = X, y = y, lambda = lambda)$grad;
  return(grad);
}
#################### end of utility functions ####################

## ================ train nerual network ===============
nnet.train.cla &lt;- function(X, y, hidden_layer_size = 25,
                 lambda = 1, maxit = 50) {
  m &lt;- nrow(X);
  input_layer_size &lt;- ncol(X);
  num_labels &lt;- length(levels(factor(y)));
  # ================ Initializing Pameters ================
  initial_Theta1 &lt;- randInitializeWeights(input_layer_size, hidden_layer_size);
  initial_Theta2 &lt;- randInitializeWeights(hidden_layer_size, num_labels);
  initial_nn_params = c(c(initial_Theta1), c(initial_Theta2));

  # =================== Training NN ===================
  train_results &lt;- optim(par = initial_nn_params,
                       fn = costFunction.cla,
                       input_layer_size = input_layer_size,
                       hidden_layer_size = hidden_layer_size,
                       num_labels = num_labels,
                       X = X, y = y, lambda = lambda,
                       gr = gradFunction.cla,
                       method = &quot;L-BFGS-B&quot;,
                       control = list(maxit = maxit, trace = TRUE, REPORT = 1));
  nn_params &lt;- train_results$par;
  Theta1 &lt;- matrix(nn_params[1:(hidden_layer_size*(input_layer_size+1))],
                   hidden_layer_size, (input_layer_size + 1));
  Theta2 &lt;- matrix(nn_params[-1:-(hidden_layer_size*(input_layer_size+1))],
                   num_labels, (hidden_layer_size + 1));
  ## ================= show accuracy =================
  pred = nnet.predict.cla(Theta1, Theta2, X);
  cat(&quot;\nTraining Set Accuracy: &quot;, sum(pred == y)/length(pred)*100,
      &quot;%\n&quot;, sep = &quot;&quot;);
  ## =============== return thetas ===============
  return(list(Theta1 = Theta1, Theta2 = Theta2));
}

## =============== nerual network predict ===============
nnet.predict.cla &lt;- function(Theta1, Theta2, X) {
  num_labels &lt;- nrow(Theta2);
  m &lt;- nrow(X);
  if (is.null(m)) {
    m &lt;- 1; X &lt;- t(X);
  }
  p = rep(0, m);

  h1 &lt;- sigmoid(cbind(rep(1, m), X) %*% t(Theta1));
  h2 &lt;- sigmoid(cbind(rep(1, m), h1) %*% t(Theta2));
  p = apply(h2, 1, which.max);
  return(p);
}
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alphaism.wordpress.com/882/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alphaism.wordpress.com/882/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=882&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alphaism.wordpress.com/2012/11/13/neural-network-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3ff59ed22f22521815577d5fc2cf4bb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alphaism</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/11/selection_001.png" medium="image">
			<media:title type="html">Selection_001</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/11/selection_002.png" medium="image">
			<media:title type="html">Selection_002</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/11/clas1.png" medium="image">
			<media:title type="html">clas</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/11/abs1.png" medium="image">
			<media:title type="html">abs</media:title>
		</media:content>
	</item>
		<item>
		<title>A not-so-mathy Black-Litterman model</title>
		<link>http://alphaism.wordpress.com/2012/10/25/a-not-so-mathy-black-litterman-model/</link>
		<comments>http://alphaism.wordpress.com/2012/10/25/a-not-so-mathy-black-litterman-model/#comments</comments>
		<pubDate>Thu, 25 Oct 2012 22:54:41 +0000</pubDate>
		<dc:creator>alphaism</dc:creator>
				<category><![CDATA[Discussions]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://alphaism.wordpress.com/?p=855</guid>
		<description><![CDATA[Black-Litterman model is a very handy tool to quantify and integrate different sources of information into a portfolio. But I know a fair number of people got scared away from it once they saw this: Or this But the idea behind is really simple: we start to construct a portfolio with weights based on implied [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=855&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Black%E2%80%93Litterman_model">Black-Litterman model</a> is a very handy tool to quantify and integrate different sources of information into a portfolio. But I know a fair number of people got scared away from it once they saw this: </p>
<p><img src='http://s0.wp.com/latex.php?latex=E%5BR%5D%3D+%5B%28%5Ctau%5CSigma%29%5E%7B-1%7D%2BP%5E%7B%27%7D%5COmega%5E%7B-1%7DP%5D%5E%7B-1%7D%5B%28%5Ctau%5CSigma%29%5E%7B-1%7D%5CPi%2BP%5E%7B%27%7D%5COmega%5E%7B-1%7DQ%5D&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='E[R]= [(&#92;tau&#92;Sigma)^{-1}+P^{&#039;}&#92;Omega^{-1}P]^{-1}[(&#92;tau&#92;Sigma)^{-1}&#92;Pi+P^{&#039;}&#92;Omega^{-1}Q]' title='E[R]= [(&#92;tau&#92;Sigma)^{-1}+P^{&#039;}&#92;Omega^{-1}P]^{-1}[(&#92;tau&#92;Sigma)^{-1}&#92;Pi+P^{&#039;}&#92;Omega^{-1}Q]' class='latex' /></p>
<p>Or this </p>
<p><img src='http://s0.wp.com/latex.php?latex=w_%7Bk%7D+%3D%5B%5Clambda%5CSigma%5D%5E%7B-1%7D%5B%28%5Ctau%5CSigma%29%5E%7B-1%7D%2B+p_%7Bk%7D%5E%7B%27%7D%5Comega%5E%7B-1%7Dp_%7Bk%7D%5D%5E%7B-1%7D+%5B%28%5Ctau%5CSigma%29%5E%7B-1%7D%5CPi+%2B+p_%7Bk%7D%5E%7B%27%7D%5Comega%5E%7B-1%7DQ_%7Bk%7D%5D&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='w_{k} =[&#92;lambda&#92;Sigma]^{-1}[(&#92;tau&#92;Sigma)^{-1}+ p_{k}^{&#039;}&#92;omega^{-1}p_{k}]^{-1} [(&#92;tau&#92;Sigma)^{-1}&#92;Pi + p_{k}^{&#039;}&#92;omega^{-1}Q_{k}]' title='w_{k} =[&#92;lambda&#92;Sigma]^{-1}[(&#92;tau&#92;Sigma)^{-1}+ p_{k}^{&#039;}&#92;omega^{-1}p_{k}]^{-1} [(&#92;tau&#92;Sigma)^{-1}&#92;Pi + p_{k}^{&#039;}&#92;omega^{-1}Q_{k}]' class='latex' /></p>
<p>But the idea behind is really simple: we start to construct a portfolio with weights based on implied market equilibrium returns or market capitalization, then tune the weights based on our views. Each view, although seems subjective, actually only has 3 pieces of key information: which assets will outperform which? By how many basis points? How confident you are about this view? To illustrate, here&#8217;s an example produced by my R package &#8220;portopt&#8221;. (It&#8217;s on its final stage of development and I&#8217;ll talk more details about it in the future)</p>
<p>First of all, let&#8217;s create an optimizer object which contains all information we need for an optimization task, including historical returns of 5 hypothetical assets (normally distributed around 0; creatively named A, B, C, D and E), their market capitalization weights (20% each), scalar for views (tau), and so on. In this example, we have 2 views and they have already built in the optimizer. If we call function show.optimizer() and pass in the object as the only argument, these views will be listed below. And if we call function run.optimizer(), we can see how the portfolio weights shifted because of the views. As shown below:</p>
<p><a href="http://alphaism.files.wordpress.com/2012/10/selection_0011.png"><img src="http://alphaism.files.wordpress.com/2012/10/selection_0011.png?w=640" alt="" title="Selection_001"   class="alignnone size-full wp-image-874" /></a></p>
<p>Now let&#8217;s call function set.views() to change our views. The program will ask how many views you have, then loop you through each view with the 3 key questions. See below:</p>
<p><a href="http://alphaism.files.wordpress.com/2012/10/selection_0021.png"><img src="http://alphaism.files.wordpress.com/2012/10/selection_0021.png?w=640" alt="" title="Selection_002"   class="alignnone size-full wp-image-875" /></a></p>
<p>Now if we run optimization on this new optimizer, we&#8217;ll see corresponding weights change. We see some big shifts here because 5% and 10% are some very large numbers to a bunch of standard-normally distributed returns.</p>
<p><a href="http://alphaism.files.wordpress.com/2012/10/selection_003.png"><img src="http://alphaism.files.wordpress.com/2012/10/selection_003.png?w=640" alt="" title="Selection_003"   class="alignnone size-full wp-image-877" /></a></p>
<p>Put it all together:</p>
<p><a href="http://alphaism.files.wordpress.com/2012/10/blweights.png"><img src="http://alphaism.files.wordpress.com/2012/10/blweights.png?w=640" alt="" title="blweights"   class="alignnone size-full wp-image-879" /></a></p>
<p>This post is just an illustration of how the model is used without any details about how it works. There are many discussions and research about different components of this model such as how to set up <img src='http://s0.wp.com/latex.php?latex=%5Ctau&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='&#92;tau' title='&#92;tau' class='latex' />, how to calculate <img src='http://s0.wp.com/latex.php?latex=%5COmega&amp;bg=ffffff&amp;fg=444444&amp;s=0' alt='&#92;Omega' title='&#92;Omega' class='latex' /> vector, how to treat confidence level, etc. An investor should look into theses topics before finally decide to put this thing in production.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alphaism.wordpress.com/855/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alphaism.wordpress.com/855/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=855&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alphaism.wordpress.com/2012/10/25/a-not-so-mathy-black-litterman-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3ff59ed22f22521815577d5fc2cf4bb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alphaism</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/10/selection_0011.png" medium="image">
			<media:title type="html">Selection_001</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/10/selection_0021.png" medium="image">
			<media:title type="html">Selection_002</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/10/selection_003.png" medium="image">
			<media:title type="html">Selection_003</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/10/blweights.png" medium="image">
			<media:title type="html">blweights</media:title>
		</media:content>
	</item>
		<item>
		<title>Mean-Reversion Risk Tuning</title>
		<link>http://alphaism.wordpress.com/2012/08/31/mean-reversion-risk-tuning/</link>
		<comments>http://alphaism.wordpress.com/2012/08/31/mean-reversion-risk-tuning/#comments</comments>
		<pubDate>Fri, 31 Aug 2012 18:49:30 +0000</pubDate>
		<dc:creator>alphaism</dc:creator>
				<category><![CDATA[Back-tests]]></category>

		<guid isPermaLink="false">http://alphaism.wordpress.com/?p=843</guid>
		<description><![CDATA[This is just a test for fun. What if we buy everything that goes down to a certain point, say -%1 or -%5 at the end of the day everyday? There are probably a bunch of programs out there can do this but I prefer something home-made. To get a list of all publicly traded [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=843&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is just a test for fun. What if we buy everything that goes down to a certain point, say -%1 or -%5 at the end of the day everyday? There are probably a bunch of programs out there can do this but I prefer something home-made.</p>
<p>To get a list of all publicly traded stocks on NASDAQ, I went to <a href="http://eoddata.com/symbols.aspx">EOData</a> and grabbed a txt file for all NASDAQ tickers. But the trouble was even though the calculation I wanted to perform was simple, the volume was too large for my computer to handle. So I had to push everything to github, start an Amazon EC2 instance, configure it, pull from my own repository, run my script, then push everything back. The process itself is not complicated, just a lot of annoying details.</p>
<p>Anyhow, before everybody gets bored with my geeky life story, here are the results. The numbers in the legend represent mean-reversion signals. For instance, -0.12 means &#8220;what if we buy everything that goes down more than 12%?<br />
<a href="http://alphaism.files.wordpress.com/2012/08/nasdaqtest.png"><img src="http://alphaism.files.wordpress.com/2012/08/nasdaqtest.png?w=640&#038;h=548" alt="" title="NasdaqTest" width="640" height="548" class="alignnone size-full wp-image-844" /></a><br />
It seems as we moveing the signal further away from 0, the strategy becomes more aggressive and performance improves gradually, except for -0.19 and -0.20, which might be two outliers or trend changers.<br />
<a href="http://alphaism.files.wordpress.com/2012/08/rr.png"><img src="http://alphaism.files.wordpress.com/2012/08/rr.png?w=640" alt="" title="RR"   class="alignnone size-full wp-image-847" /></a></p>
<p>Source code in R.</p>
<pre class="brush: r; title: ; notranslate">
## functions
EODMR &lt;- function(returnsData, shockRates = seq(-0.01, -0.2, by = -0.01)) {
  output &lt;- mrTest(returnsData, shockRate = 0)
  output &lt;- calcRet(output)
  outputRet &lt;- output
  annRet &lt;- prod(1+output)^(250/length(output))-1
  annSd &lt;- sd(output)*sqrt(250)
  Sharpe &lt;- annRet/annSd
  output &lt;- data.frame(shock0 = rbind(annRet, annSd, Sharpe))
  for (i in seq(along = shockRates)) {
	testData &lt;- mrTest(returnsData, shockRate = shockRates[i])
	finalRet &lt;- calcRet(testData)
        outputRet &lt;- merge(outputRet, finalRet)
        names(outputRet)[i + 1] &lt;- shockRates[i]
        annRet &lt;- prod(1+finalRet)^(250/length(finalRet))-1
        annSd &lt;- sd(finalRet)*sqrt(250)
        Sharpe &lt;- annRet/annSd
        finalRet &lt;- data.frame(rbind(annRet, annSd, Sharpe))
        names(finalRet) &lt;- shockRates[i]
	output &lt;- cbind(output, finalRet)
	print(paste(i, &quot;out of &quot;, length(shockRates), &quot; finished&quot;))
  }
  return(list(testStat = output, testReturns = outputRet))
}

# download and format returns data
getReturns &lt;- function(tickers, start = Sys.Date() - 2520,
                       minVol = 100000) {  
  returnsData &lt;- get.hist.quote(&quot;^GSPC&quot;, start = start,
                           quote = &quot;AdjClose&quot;)
  returnsData &lt;- merge(ROC(returnsData), Next(ROC(returnsData)))
  names(returnsData) &lt;- paste(&quot;SPX&quot;, c(&quot;Ret0&quot;, &quot;Ret1&quot;), sep = &quot;.&quot;)
  
  for (i in seq(along = tickers)) {
    nextTicker &lt;- NULL
    nextTicker &lt;- try(get.hist.quote(tickers[i], start = start,
                                     quote = c(&quot;AdjClose&quot;, &quot;Volume&quot;)))
    
    if (!is.character(nextTicker[1])) {

      meanVol &lt;- as.numeric(mean(nextTicker))
      if (meanVol &gt;= minVol) {
        nextTicker &lt;- nextTicker$Adj
      } else next

      nextTicker &lt;- nextTicker[!duplicated(index(nextTicker))]
      nextTicker &lt;- merge(ROC(nextTicker), Next(ROC(nextTicker)))
      names(nextTicker) &lt;- paste(tickers[i], c(&quot;Ret0&quot;, &quot;Ret1&quot;), sep = &quot;.&quot;)
      returnsData &lt;- merge(retournsData, nextTicker)
    } else next
  print(paste(i, &quot;out of&quot;, length(tickers), sep = &quot; &quot;))
  }
  return(returnsData)
}

# perform MR test
mrTest &lt;- function(returnsData, shockRate = -0.05) {
  testRets &lt;- returnsData[, 1:2]
  thisName &lt;- gsub(&quot;.Ret0&quot;, &quot;&quot;, names(testRets)[1])
  testRets &lt;- ifelse(testRets[, 1] &lt;= shockRate, testRets[, 2], 0)
  dim(testRets) &lt;- c(length(testRets), 1)
  names(testRets) &lt;- thisName
  
  secNum &lt;- ncol(returnsData) / 2
  for (i in 2:secNum) {
    thisSec &lt;- returnsData[, (i * 2 - 1):(i * 2)]
    thisName &lt;- gsub(&quot;.Ret0&quot;, &quot;&quot;, names(thisSec)[1])
    thisSec &lt;- ifelse(thisSec[, 1] &lt;= shockRate, thisSec[, 2], 0)
    dim(thisSec) &lt;- c(length(thisSec), 1)
    names(thisSec) &lt;- thisName
    testRets &lt;- merge(testRets, thisSec)
    testRets[is.na(testRets)] &lt;- 0
  }
  return(testRets)
}

# calculate strategy results
calcRet &lt;- function(testRets) {
  timeLength &lt;- nrow(testRets)
  finalRet &lt;- rep(0, timeLength)
  for (i in 1:timeLength) {
    tmp &lt;- as.numeric(testRets[i, ])
    tmp &lt;- tmp[tmp != 0]
    finalRet[i] &lt;- mean(tmp)
  }
  finalRet[is.na(finalRet)] &lt;- 0
  finalRet &lt;- zoo(finalRet, order.by = index(testRets))
  return(finalRet)
}

#### run code
# load packages
library(tseries)
library(quantmod)
library(PerformanceAnalytics)

# perform test
tickersNasdaq &lt;- as.vector(read.table(&quot;~/R/EODMR_development/NASDAQ.txt&quot;,
                                      sep = &quot;\t&quot;)[-1, 1])
testNasdaq &lt;- EODMR(returnsNasdaq)

# visualize results
charts.PerformanceSummary(testNasdaq$testReturns, colorset = rainbow(21), ylog = 1)
chart.RiskReturnScatter(testNasdaq$testReturns, colorset = rainbow(21), symbolset = rep(3, 21))
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alphaism.wordpress.com/843/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alphaism.wordpress.com/843/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=843&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alphaism.wordpress.com/2012/08/31/mean-reversion-risk-tuning/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3ff59ed22f22521815577d5fc2cf4bb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alphaism</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/08/nasdaqtest.png" medium="image">
			<media:title type="html">NasdaqTest</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/08/rr.png" medium="image">
			<media:title type="html">RR</media:title>
		</media:content>
	</item>
		<item>
		<title>NUCFLASH</title>
		<link>http://alphaism.wordpress.com/2012/07/30/nucflash/</link>
		<comments>http://alphaism.wordpress.com/2012/07/30/nucflash/#comments</comments>
		<pubDate>Mon, 30 Jul 2012 22:17:45 +0000</pubDate>
		<dc:creator>alphaism</dc:creator>
				<category><![CDATA[Analysis]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://alphaism.wordpress.com/?p=825</guid>
		<description><![CDATA[Nucflash refers to detonation or possible detonation of a nuclear weapon which creates a risk of an outbreak of nuclear war. Recently I was engaged in an interesting conversation about extreme risks and thought it would be fun to do a study on stocks&#8217; group behavior during and after market shocks. Sort of like a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=825&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><em><br />
<blockquote><strong>Nucflash</strong> refers to detonation or possible detonation of a nuclear weapon which creates a risk of an outbreak of nuclear war.</p></blockquote>
<p></em><br />
<a href="http://alphaism.files.wordpress.com/2012/07/military_artillery_nuclear_explosions_desktop_1920x1080_wallpaper-1121754.jpg"><img src="http://alphaism.files.wordpress.com/2012/07/military_artillery_nuclear_explosions_desktop_1920x1080_wallpaper-1121754.jpg?w=640&#038;h=360" alt="" title="military_artillery_nuclear_explosions_desktop_1920x1080_wallpaper-1121754" width="640" height="360" class="alignnone size-full wp-image-833" /></a></p>
<p>Recently I was engaged in an interesting conversation about extreme risks and thought it would be fun to do a study on stocks&#8217; group behavior during and after market shocks. Sort of like a fallout analysis.</p>
<p>Around 500 stocks listed on NASDAQ were sampled for this study. For the past 10 years, whenever S&amp;P500 daily return dropped by more than 4 standard deviations from it&#8217;s mean, a shock was spotted and that day was defined as Day0. Of course, the day after Day0 would be Day1, then Day2, and so on.<br />
<a href="http://alphaism.files.wordpress.com/2012/07/visshocks.png"><img src="http://alphaism.files.wordpress.com/2012/07/visshocks.png?w=640" alt="" title="visShocks"   class="alignnone size-full wp-image-826" /></a><br />
Each stock&#8217;s average return and standard deviation from Day0 to Day4 were ploted to track their post-shock behaviors. Black lables in the plot below are their average return and standard deviation during the past 10 years (normal state).<a href="http://alphaism.files.wordpress.com/2012/07/postshock.png"><img src="http://alphaism.files.wordpress.com/2012/07/postshock.png?w=640" alt="" title="postShock"   class="alignnone size-full wp-image-828" /></a><br />
In this demonstration, you can literalliy see the market got &#8220;nuked&#8221; at Day0, quickly bounced back at Day1, then gradually converged back to its normal state. Sharpe readers may also noticed how rapidaly the market swithced its state between MR and TF within such a short time window.</p>
<p>Here&#8217;s my R code for this test. Remember to put on your radiation suit before you try it in your basement.</p>
<pre class="brush: r; title: ; notranslate">
## a function for on-line data retrieving and formatting
get.data &lt;- function(sec, start = Sys.Date() - 5000, end = Sys.Date()) {
  library(tseries)
  tickers &lt;- sec
  close &lt;- get.hist.quote(sec[1], start = start, end = end,
                          quote = 'AdjClose')
  close &lt;- close[!duplicated(index(close))]
  for (i in 2:length(sec)) {
    nextClose &lt;- NULL
    nextClose &lt;- try(get.hist.quote(sec[i], start = start,
                                    end = end, quote = 'AdjClose'))
    if (!is.character(nextClose[1])) {
      nextClose &lt;- nextClose[!duplicated(index(nextClose))]
      close &lt;- merge(close, nextClose)
    } else tickers &lt;- tickers[-i]
  }
  names(close) &lt;- tickers
  close
}

## prep data
library(quantmod)
library(PerformanceAnalytics)

sec &lt;- read.table(&quot;NASDAQ.txt&quot;, header = FALSE, sep = &quot;\t&quot;)[, 1]
sec &lt;- as.vector(sec)
sec &lt;- c(&quot;^GSPC&quot;, sec)
allData &lt;- get.data(sec)
names(allData)[1] &lt;- &quot;SP500&quot;
compData &lt;- allData[, !is.na(allData[1, ])] # survivors only
allRet &lt;- ROC(compData, na.pad = FALSE)
allRet[is.na(allRet)] &lt;- 0

## identify shocks: drop by more than 4 sds
SPmean &lt;- mean(allRet$SP500)
SPsd &lt;- sd(allRet$SP500)
shocks &lt;- SPmean - 4*SPsd
plot(allRet$SP500, main = &quot;SP500 Daily Returns&quot;) # visualize shocks
segments(900000000, shocks, 1350000000, shocks, col = &quot;red&quot;)
text(1100000000, -0.06, &quot;Market Shocks&quot;, col = &quot;red&quot;, cex = 1.5)

## calculate and plot test data
chart.Scatter(c(0, 0.5), c(-0.2, 0.1), col = &quot;white&quot;,
              main = &quot;Post-Shock Behavior&quot;)

for (i in 0:4) {
  thisDataName &lt;- paste(&quot;Day&quot;, as.character(i), sep = &quot;&quot;)
  thisTestName &lt;- paste(&quot;test&quot;, as.character(i), sep = &quot;&quot;)
  assign(thisDataName, allRet[Lag(allRet$SP500 &lt; fallOut, i),])
  thisData &lt;- get(thisDataName)
  testMean &lt;- sapply(thisData, mean, 2)
  testSd &lt;- sapply(thisData, sd, 2)
  assign(thisTestName, cbind(testSd, testMean))

  testCols = c(&quot;red&quot;, &quot;orange&quot;, &quot;yellow&quot;, &quot;blue&quot;, &quot;green&quot;)
  points(get(thisTestName), col = testCols[i+1], pch = 4)
}

normalMean &lt;- sapply(allRet, mean, 2)
normalSd &lt;- sapply(allRet, sd, 2)
normalState &lt;- cbind(normalSd, normalMean)
points(normalState, pch = 4)
legend(&quot;topright&quot;, pch = 4, c(&quot;Day0&quot;,&quot;Day1&quot;,&quot;Day2&quot;,&quot;Day3&quot;,&quot;Day4&quot;,&quot;Normal&quot;),
       col = c(testCols, &quot;Black&quot;), text.col = c(testCols, &quot;black&quot;))
## alarm dismissed
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alphaism.wordpress.com/825/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alphaism.wordpress.com/825/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=825&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alphaism.wordpress.com/2012/07/30/nucflash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3ff59ed22f22521815577d5fc2cf4bb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alphaism</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/07/military_artillery_nuclear_explosions_desktop_1920x1080_wallpaper-1121754.jpg" medium="image">
			<media:title type="html">military_artillery_nuclear_explosions_desktop_1920x1080_wallpaper-1121754</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/07/visshocks.png" medium="image">
			<media:title type="html">visShocks</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/07/postshock.png" medium="image">
			<media:title type="html">postShock</media:title>
		</media:content>
	</item>
		<item>
		<title>Calculus and statistics: different paradigms of thinking</title>
		<link>http://alphaism.wordpress.com/2012/07/24/calculus-and-statistics-different-paradigms-of-thinking/</link>
		<comments>http://alphaism.wordpress.com/2012/07/24/calculus-and-statistics-different-paradigms-of-thinking/#comments</comments>
		<pubDate>Wed, 25 Jul 2012 00:57:43 +0000</pubDate>
		<dc:creator>alphaism</dc:creator>
				<category><![CDATA[Discussions]]></category>

		<guid isPermaLink="false">http://alphaism.wordpress.com/?p=811</guid>
		<description><![CDATA[One day I got a question from an academic star with almost perfect GPA in our university, “I did everything my professor asked us to do, I did regression on PE, PB ratio and stuff for prediction, why my target price for Goldman Sachs is still around $300? That’s too much off from everybody else [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=811&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>One day I got a question from an academic star with almost perfect GPA in our university, “I did everything my professor asked us to do, I did regression on PE, PB ratio and stuff for prediction, why my target price for Goldman Sachs is still around $300? That’s too much off from everybody else in the market, I don’t understand.” I stared at him for more than 10 seconds, speechless.</p>
<p>As Arthur Benjamin proposed in his speech (link below), calculus has been on the top of the math pyramid for too long, now it’s time for statistics to take over. This matters because in general our mind is skewed too much towards the calculus-style, deterministic way of thinking. For one thing, it is much more intuitive for human mind to understand things that are either right or wrong; for another, people who claim they know everything and can “prove” that by making predictions always have much more audiences than those who say “this might be true, I could be wrong but it’s the closest we can get”. Also from my own experience, our education systems are mostly designed to reward people who get desired certainties, not people who comprehend things sensibly. Consequentially, students started to pick up the habit of “sacrifice reality for elegance” (Paul Wilmott) and the line between doing scientific research and confirming collective bias is blurred. Einstein was proven wrong about “God doesn’t play dice”, but regretfully that doesn’t stop ordinary people believing it’s possible to eliminate uncertainty in their own life. </p>
<p>On the opposite, not saying statistics is better, but it does focus more on observation and self-evaluation. Its purpose is not about to find the only perfect answer, instead it’s about seeing things from as many dimensions as possible (which is a very unnatural process to human brain). One accurate prediction doesn’t make a statistical model work; a long enough series of predictions under relatively bias free conditions with acceptable level of error does (now I kind of understand why people don’t like it…). Again, I don’t think it’s better than calculus, but I think this is the key to problems such as “if I did it right, how come I’m still losing money?” </p>
<p>Recommended Reading: <a href="http://en.wikipedia.org/wiki/Fooled_by_Randomness">Fooled by Randomness (N.Taleb)</a></p>
<p>Recommended Video: Arthur Benjamin&#8217;s formula for changing math education</p>
<span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='640' height='390' src='http://www.youtube.com/embed/BhMKmovNjvc?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0'></iframe></span>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alphaism.wordpress.com/811/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alphaism.wordpress.com/811/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=811&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alphaism.wordpress.com/2012/07/24/calculus-and-statistics-different-paradigms-of-thinking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3ff59ed22f22521815577d5fc2cf4bb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alphaism</media:title>
		</media:content>
	</item>
		<item>
		<title>Neutralizing Portfolio Beta (2)</title>
		<link>http://alphaism.wordpress.com/2012/06/16/neutralizing-portfolio-beta-2/</link>
		<comments>http://alphaism.wordpress.com/2012/06/16/neutralizing-portfolio-beta-2/#comments</comments>
		<pubDate>Sat, 16 Jun 2012 04:20:06 +0000</pubDate>
		<dc:creator>alphaism</dc:creator>
				<category><![CDATA[Analysis]]></category>
		<category><![CDATA[Back-tests]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://alphaism.wordpress.com/?p=792</guid>
		<description><![CDATA[After a quick-and-dirty test in the last post, I roughly illustrated the idea of equity neutral strategy. Here I&#8217;d like to do the same thing with same securities. Except this time all portfolios will be rebalanced on weekly basis, and both optimization and beta calculation will be based on 2-year weekly data. With less vague [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=792&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>After a quick-and-dirty test in the last post, I roughly illustrated the idea of equity neutral strategy. Here I&#8217;d like to do the same thing with same securities. Except this time all portfolios will be rebalanced on weekly basis, and both optimization and beta calculation will be based on 2-year weekly data.</p>
<pre class="brush: r; title: ; notranslate">
library(tseries)
library(quantmod)

picks &lt;- c('KO','WFC','AXP','PG','JNJ','WMT','COP','USB')
buffetWeights &lt;- c(.216, .1956, .1228, .0983, .0306, .0426, .0343, .0351)
buffetWeights &lt;- buffetWeights / sum(buffetWeights) # scale to 1

## download historical close prices
get.close &lt;- function(picks,start = Sys.Date() - 5040,end = Sys.Date()) {
  histClose &lt;- get.hist.quote(picks[1], start = start, quote = 'AdjClose')
  if(length(picks) &gt; 1) {
    for (i in 2:length(picks)) {
      histClose &lt;- cbind(histClose, get.hist.quote
                        (picks[i],start = start, quote = 'AdjClose'))
    }
  }
  names(histClose) &lt;- picks
  histClose
}

buffetPicks &lt;- get.close(picks)
SPX &lt;- get.close('^GSPC')
## download historical close prices

buffetPicks &lt;- na.locf(buffetPicks) # fill NA with the last observation
plot(buffetPicks) # same graph as in the last post
</pre>
<pre class="brush: r; title: ; notranslate">
# monthly returns
buffetReturns &lt;- ROC(buffetPicks[endpoints(buffetPicks, 'weeks'),], 
                     na.pad = FALSE)
SPXReturn &lt;- ROC(SPX[endpoints(SPX, 'weeks'), ], na.pad = FALSE)
names(SPXReturn) &lt;- 'SPX'

# long-only portfolios
eqlPort &lt;- apply(buffetReturns, 1, mean)
buffetPort &lt;- apply(buffetReturns * buffetWeights, 1, sum)

optWeights &lt;- rollapply(buffetReturns, width = 104,
                        function(x) portfolio.optim(x, reslow = 
                          rep(0, 8), reshigh = rep(1, 8))$pw, 
                        by.column = FALSE,align = 'right')
optPort &lt;- merge(buffetReturns, optWeights)
optPort[is.na(optPort)] &lt;- 0
for (i in seq(along = picks)) {
  optPort[, i] &lt;- optPort[, i] * Lag(optPort[, i + length(picks)])
}
optPort &lt;- apply(optPort[, 1:8], 1, sum)

library(PerformanceAnalytics) # only load it now because this package 
                              # causes errors to function portfolio.optim
chart.CumReturns(merge(SPXReturn, eqlPort, buffetPort, optPort),colorset
                 = rainbow(4), main = 'Long-only Portfolios vs SPX', 
                 legend = 'topleft')
</pre>
<p><a href="http://alphaism.files.wordpress.com/2012/06/buffets.png"><img src="http://alphaism.files.wordpress.com/2012/06/buffets.png?w=640&#038;h=397" alt="" title="buffets" width="640" height="397" class="alignnone size-full wp-image-795" /></a></p>
<pre class="brush: r; title: ; notranslate">
# neutralize beta
betaNeutral &lt;- function(against=SPXReturn, port=eqlPort, period=104) {
  neutPort &lt;- merge(against, port)
  beta &lt;- rollapply(neutPort, width = period, 
                    function(x)coef(lm(x[, 2] ~ x[, 1],
                                       data = as.data.frame(x))), 
                    by.column = FALSE, align = 'right')[, 2]
  neutPort &lt;- merge(neutPort, Lag(beta))
  neutPort[is.na(neutPort)] &lt;- 0
  return(neutPort[, 2] - neutPort[, 1] * neutPort[, 3])
}

eqlNeutral &lt;- betaNeutral()
buffetNeutral &lt;- betaNeutral(port = buffetPort)
optNeutral &lt;- betaNeutral(port = optPort)

# put all together
combined &lt;- merge(SPXReturn, eqlPort, buffetPort, optPort, eqlNeutral,
                  buffetNeutral, optNeutral)
names(combined)[5:7] &lt;- c('eqlNeutral', 'buffetNeutral', 'optNeutral')
combined[is.na(combined)] &lt;- 0

# neutralized portfolios
chart.CumReturns(combined[, -2:-4], colorset = rainbow(4),
                 main = 'Neutralized Portfolios vs SPX', 
                 legend = 'topleft')
</pre>
<p><a href="http://alphaism.files.wordpress.com/2012/06/neuts.png"><img src="http://alphaism.files.wordpress.com/2012/06/neuts.png?w=640&#038;h=397" alt="" title="neuts" width="640" height="397" class="alignnone size-full wp-image-796" /></a></p>
<pre class="brush: r; title: ; notranslate">
# performance summary
cols = c('red', rep('green', 3), rep('blue', 3))
charts.PerformanceSummary(combined, ylog = 1, 
                          main = 'Neutralized vs Long-only vs S&amp;P500', 
                          cex.axis= 1,colorset = cols)
</pre>
<p><a href="http://alphaism.files.wordpress.com/2012/06/neutralized.png"><img src="http://alphaism.files.wordpress.com/2012/06/neutralized.png?w=640&#038;h=397" alt="" title="neutralized" width="640" height="397" class="alignnone size-full wp-image-797" /></a></p>
<pre class="brush: r; title: ; notranslate">
# risk-return relationship
chart.RiskReturnScatter(merge(combined, buffetReturns), 
                        colorset = c(cols, rep('yellow', 8)),
                        symbolset = rep(3, 16))
</pre>
<p><a href="http://alphaism.files.wordpress.com/2012/06/riskreturn.png"><img src="http://alphaism.files.wordpress.com/2012/06/riskreturn.png?w=640&#038;h=397" alt="" title="riskReturn" width="640" height="397" class="alignnone size-full wp-image-798" /></a></p>
<pre class="brush: r; title: ; notranslate">
# correlation illustration
chart.Correlation(combined)
</pre>
<p><a href="http://alphaism.files.wordpress.com/2012/06/correlation.png"><img src="http://alphaism.files.wordpress.com/2012/06/correlation.png?w=640&#038;h=397" alt="" title="correlation" width="640" height="397" class="alignnone size-full wp-image-799" /></a></p>
<p>With less vague data and higher frequency of rebalance, market neutral strategy is more powerful. It&#8217;s also interesting to see even with the same equities, Mr. Buffet&#8217;s allocation strategy actually outperforms both equal-weighted allocation and optimal allocation strategy.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alphaism.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alphaism.wordpress.com/792/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=792&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alphaism.wordpress.com/2012/06/16/neutralizing-portfolio-beta-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3ff59ed22f22521815577d5fc2cf4bb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alphaism</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/06/buffets.png" medium="image">
			<media:title type="html">buffets</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/06/neuts.png" medium="image">
			<media:title type="html">neuts</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/06/neutralized.png" medium="image">
			<media:title type="html">neutralized</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/06/riskreturn.png" medium="image">
			<media:title type="html">riskReturn</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/06/correlation.png" medium="image">
			<media:title type="html">correlation</media:title>
		</media:content>
	</item>
		<item>
		<title>Neutralizing Portfolio Beta</title>
		<link>http://alphaism.wordpress.com/2012/06/13/neutralizing-portfolio-beta/</link>
		<comments>http://alphaism.wordpress.com/2012/06/13/neutralizing-portfolio-beta/#comments</comments>
		<pubDate>Wed, 13 Jun 2012 20:29:33 +0000</pubDate>
		<dc:creator>alphaism</dc:creator>
				<category><![CDATA[Analysis]]></category>
		<category><![CDATA[Back-tests]]></category>

		<guid isPermaLink="false">http://alphaism.wordpress.com/?p=774</guid>
		<description><![CDATA[For stock pickers, market neutral means guarding equity portfolios from market shocks by doing long/short  at the same time (for traders it probably means statistical arbitrage). A market neutral investor would hold long positions in his favourable stocks and short positions in those he doesn&#8217;t like to minimize impact from broad market movements. Both the concept and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=774&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>For stock pickers, market neutral means guarding equity portfolios from market shocks by doing long/short  at the same time (for traders it probably means statistical arbitrage). A market neutral investor would hold long positions in his favourable stocks and short positions in those he doesn&#8217;t like to minimize impact from broad market movements. Both the concept and the math behind are not that complicated. Thus tt would be interesting to visualize how effective it is by running a quick test.</p>
<p>To keep it quick-and-dirty enough so that even not-so-sophisticated investors could try it at home, I decided to get lazy and skip the due diligence part of stock picking. Instead I went to the <a href="http://warren-buffett-portfolio.com/#WarrenBuffett_LatestPortfolio_AllStocks">holy grail of stock picking</a> and directly took his 8 top holdings  as their weights (KFT and WSC were dropped for insufficient data).</p>
<p><a href="http://alphaism.files.wordpress.com/2012/06/buffetpicks.png"><img src="http://alphaism.files.wordpress.com/2012/06/buffetpicks.png?w=640&#038;h=397" alt="" title="buffetPicks" width="640" height="397" class="alignnone size-full wp-image-777" /></a></p>
<p>Out from these stocks I constructed three long-only, monthly-rebalanced portfolios by using equal weights, their original weights (scale to 1) and optimal weights (efficient portfolio) respectively.<br />
<a href="http://alphaism.files.wordpress.com/2012/06/longonly1.png"><img src="http://alphaism.files.wordpress.com/2012/06/longonly1.png?w=640&#038;h=397" alt="" title="longOnly" width="640" height="397" class="alignnone size-full wp-image-781" /></a><br />
We can see that the guru&#8217;s picks did quite well for the last 20 years cumulatively. But whenever the market encountered some headwinds, they usually plunged with it and kicked some investors out of the market despite they picked the &#8220;right&#8221; stocks (LTCM-style fail). To overcome this situation, I re-constructed three portfolios by running 12-month rolling linear-regression against the S&amp;P500 for each of them and shorting the market by the portion of their beta.<br />
<a href="http://alphaism.files.wordpress.com/2012/06/nports.png"><img src="http://alphaism.files.wordpress.com/2012/06/nports.png?w=640&#038;h=397" alt="" title="nPorts" width="640" height="397" class="alignnone size-full wp-image-783" /></a><br />
Putting them together. The green lines are long-only portfolios and blue lines are neutralized portfolios. The neutralized portfolios didn&#8217;t add too much value in terms of risk-return efficiency but protected investors from severe market downturns as expected. The correlation chart shows the neutralized portfolios have extremely low correlation with the market, which well explains why it works (number size is scaled by it&#8217;s value so a value close to 0 will be too small to read).<br />
<a href="http://alphaism.files.wordpress.com/2012/06/rplot01.png"><img src="http://alphaism.files.wordpress.com/2012/06/rplot01.png?w=640&#038;h=397" alt="" title="Rplot01" width="640" height="397" class="alignnone size-full wp-image-785" /></a><br />
<a href="http://alphaism.files.wordpress.com/2012/06/rplot02.png"><img src="http://alphaism.files.wordpress.com/2012/06/rplot02.png?w=640&#038;h=397" alt="" title="Rplot02" width="640" height="397" class="alignnone size-full wp-image-787" /></a><br />
<a href="http://alphaism.files.wordpress.com/2012/06/rplot03.png"><img src="http://alphaism.files.wordpress.com/2012/06/rplot03.png?w=640&#038;h=397" alt="" title="Rplot03" width="640" height="397" class="alignnone size-full wp-image-788" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alphaism.wordpress.com/774/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alphaism.wordpress.com/774/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=774&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alphaism.wordpress.com/2012/06/13/neutralizing-portfolio-beta/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3ff59ed22f22521815577d5fc2cf4bb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alphaism</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/06/buffetpicks.png" medium="image">
			<media:title type="html">buffetPicks</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/06/longonly1.png" medium="image">
			<media:title type="html">longOnly</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/06/nports.png" medium="image">
			<media:title type="html">nPorts</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/06/rplot01.png" medium="image">
			<media:title type="html">Rplot01</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/06/rplot02.png" medium="image">
			<media:title type="html">Rplot02</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/06/rplot03.png" medium="image">
			<media:title type="html">Rplot03</media:title>
		</media:content>
	</item>
		<item>
		<title>Optimizing Ivy Portfolio (2)</title>
		<link>http://alphaism.wordpress.com/2012/05/26/optimizing-ivy-portfolio-2/</link>
		<comments>http://alphaism.wordpress.com/2012/05/26/optimizing-ivy-portfolio-2/#comments</comments>
		<pubDate>Sat, 26 May 2012 19:57:38 +0000</pubDate>
		<dc:creator>alphaism</dc:creator>
				<category><![CDATA[Back-tests]]></category>

		<guid isPermaLink="false">http://alphaism.wordpress.com/?p=752</guid>
		<description><![CDATA[Last time we attempted to optimize an Ivy Portfolio with very conservative rules to implement the principle of &#8220;winning by not losing&#8221; and it went quite well. Now just as Quantum Financier mentioned last time, what if we loose some constrains? Presumably, we should be rewarded by more return for taking extra risk. To make it more [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=752&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Last time we attempted to <a href="http://alphaism.wordpress.com/2012/05/22/optimizing-ivy-portfolio/">optimize an Ivy Portfolio</a> with very conservative rules to implement the principle of &#8220;winning by not losing&#8221; and it went quite well. Now just as <a href="http://quantumfinancier.wordpress.com/">Quantum Financier</a> mentioned last time, what if we loose some constrains? Presumably, we should be rewarded by more return for taking extra risk.</p>
<p>To make it more observable, I started from the CVaR Ivy Portfolio in my last post and changed several rules below. All of these changes are essentially telling one thing to the model, &#8220;go out there to take more risks&#8221;.</p>
<p>1. Optimizer: Minimize CVaR -&gt; Maximize Sharpe Ratio (minimizing variance and maximizing expected return at the same time)<br />
2. Timing Signal: 12-month SMA -&gt; 12-month EMA (focusing more on short-term momentum)<br />
3. Weight Constrain: [0, 1] -&gt; [-0.5, 1.5] (higher leverage)<br />
4. Number of assets in the portfolio: 3 -&gt; 6 (broader coverage)</p>
<p style="text-align:center;"><a href="http://alphaism.files.wordpress.com/2012/05/compare.png"><img class="size-full wp-image-754 aligncenter" title="compare" src="http://alphaism.files.wordpress.com/2012/05/compare.png?w=640" alt=""   /></a><br />
<a href="http://alphaism.files.wordpress.com/2012/05/1.png"><img class="size-full wp-image-761 aligncenter" title="1" src="http://alphaism.files.wordpress.com/2012/05/1.png?w=640" alt=""   /></a></p>
<p style="text-align:left;">Yep, the plan works. And as we expected, we exposed ourselves to higher drawdown risks at the same time.<br />
<a href="http://alphaism.files.wordpress.com/2012/05/dd2.png"><img class="size-full wp-image-756 aligncenter" title="dd2" src="http://alphaism.files.wordpress.com/2012/05/dd2.png?w=640" alt=""   /></a><br />
<a href="http://alphaism.files.wordpress.com/2012/05/2.png"><img class="size-full wp-image-762 aligncenter" title="2" src="http://alphaism.files.wordpress.com/2012/05/2.png?w=640" alt=""   /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alphaism.wordpress.com/752/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alphaism.wordpress.com/752/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=752&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alphaism.wordpress.com/2012/05/26/optimizing-ivy-portfolio-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3ff59ed22f22521815577d5fc2cf4bb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alphaism</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/05/compare.png" medium="image">
			<media:title type="html">compare</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/05/1.png" medium="image">
			<media:title type="html">1</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/05/dd2.png" medium="image">
			<media:title type="html">dd2</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/05/2.png" medium="image">
			<media:title type="html">2</media:title>
		</media:content>
	</item>
		<item>
		<title>Optimizing Ivy Portfolio</title>
		<link>http://alphaism.wordpress.com/2012/05/22/optimizing-ivy-portfolio/</link>
		<comments>http://alphaism.wordpress.com/2012/05/22/optimizing-ivy-portfolio/#comments</comments>
		<pubDate>Wed, 23 May 2012 00:02:37 +0000</pubDate>
		<dc:creator>alphaism</dc:creator>
				<category><![CDATA[Back-tests]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://investmentsroy.wordpress.com/?p=729</guid>
		<description><![CDATA[Early on I posted a simple live version of GTAA strategy. It demonstrated the effectiveness of the Ivy Portfolio (M.Faber, 2009) rationale in recent market with a small sample. Again, the rationale is very simple and powerful: screen a wide range of asset classes each week/month, then invest in those that have shown the strongest [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=729&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Early on I posted a <a href="http://investmentsroy.wordpress.com/2012/03/09/my-gtaa-portfolio-in-current-market/">simple live version of GTAA strategy</a>. It demonstrated the effectiveness of the <a href="http://www.theivyportfolio.com/">Ivy Portfolio</a> (M.Faber, 2009) rationale in recent market with a small sample. Again, the rationale is very simple and powerful: screen a wide range of asset classes each week/month, then invest in those that have shown the strongest momentum. Last time I tracked 39 ETFs&#8217; 9-month SMA and equally allocated portfolio assets to the top 8. Although I got pretty good results, the sample was relatively small and those ETFs are quite different in terms of time of inception, liquidity, tracking error, etc.</p>
<p>And above all, equal allocation seems a bit, for lack of a better word, boring. This time I want to use a more general sample to see how we can improve this by implementing some optimization strategies I&#8217;ve shown in my previous post <a href="http://investmentsroy.wordpress.com/2012/05/17/backtesting-portfolio-optimization-strategies/">Backtesting Portfolio Optimization Strategies</a>.</p>
<p>Some equipment check before we launch the test.</p>
<p>Asset Classes:<br />
1. SPX Index: S&amp;P 500 LargeCap Index<br />
2. MID Index: S&amp;P 400 MidCap Index<br />
3. SML Index: S&amp;P 600 SmallCap Index<br />
4. MXEA Index: MSCI EAFE Index<br />
5. MXEF Index: MSCI Emerging Markets Index<br />
6. LBUSTRUU Index: Barclays US Agg Total Return Value Unhedged USD (U.S. investment grade bond)<br />
7. XAU Curncy: Gold/USD Spot<br />
8. SPGSCI Index: Goldman Sachs Commodity Index<br />
9. DJUSRE Index: Dow Jones U.S. Real Estate Index<br />
10. GBP Curncy: GBP/USD Spot<br />
11. EUR Curncy: EUR/USD Spot<br />
12. JPY Curncy: JPY/USD Spot<br />
13. HKD Curncy: HKD/USD Spot</p>
<p>Rules:<br />
1. Rebalance monthly<br />
2. Rank 12-month SMA; invest in the top 3<br />
3. For each asset, minimum weight = 5%; maximum weight = 95%<br />
4. Use CVaR optimization to construct the portfolio each month; confidence level = 1%</p>
<p><a href="http://alphaism.files.wordpress.com/2012/05/optivy.png"><img class="size-full wp-image-737 aligncenter" title="optivy" src="http://alphaism.files.wordpress.com/2012/05/optivy.png?w=640&#038;h=426" alt="" width="640" height="426" /></a><br />
<a href="http://alphaism.files.wordpress.com/2012/05/stat.png"><img class="size-full wp-image-738 aligncenter" title="stat" src="http://alphaism.files.wordpress.com/2012/05/stat.png?w=640" alt=""   /></a></p>
<p>Fortunately, our test didn&#8217;t fall apart and crash into the Pacific Ocean. The CVaR model seems did a good job improving the original strategy. However, it has to be pointed out that not all optimization models are better than an equal-weighted one. As demonstrated below, the minimum-variance and maximum-sharpe ratio models didn&#8217;t make much difference.<br />
<a href="http://alphaism.files.wordpress.com/2012/05/comp.png"><img class="size-full wp-image-741 aligncenter" title="comp" src="http://alphaism.files.wordpress.com/2012/05/comp.png?w=640&#038;h=426" alt="" width="640" height="426" /></a></p>
<p>roy</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/alphaism.wordpress.com/729/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/alphaism.wordpress.com/729/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=alphaism.wordpress.com&#038;blog=22083182&#038;post=729&#038;subd=alphaism&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://alphaism.wordpress.com/2012/05/22/optimizing-ivy-portfolio/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/3ff59ed22f22521815577d5fc2cf4bb9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">alphaism</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/05/optivy.png" medium="image">
			<media:title type="html">optivy</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/05/stat.png" medium="image">
			<media:title type="html">stat</media:title>
		</media:content>

		<media:content url="http://alphaism.files.wordpress.com/2012/05/comp.png" medium="image">
			<media:title type="html">comp</media:title>
		</media:content>
	</item>
	</channel>
</rss>
