<?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/"
	>

<channel>
	<title>The Panda Put</title>
	<atom:link href="http://www.perdomocore.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.perdomocore.com</link>
	<description>Finance, Coding, Economics</description>
	<lastBuildDate>Fri, 18 May 2012 14:27:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Korean BBQ tonight</title>
		<link>http://www.perdomocore.com/2012/korean-bbq-tonight/</link>
		<comments>http://www.perdomocore.com/2012/korean-bbq-tonight/#comments</comments>
		<pubDate>Fri, 18 May 2012 14:27:40 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Living]]></category>

		<guid isPermaLink="false">http://www.perdomocore.com/?p=113</guid>
		<description><![CDATA[Always delicious!]]></description>
			<content:encoded><![CDATA[<p>Always delicious!</p>
<p><a href="http://www.perdomocore.com/wp-content/uploads/2012/05/20120518-2227001.jpg"><img src="http://www.perdomocore.com/wp-content/uploads/2012/05/20120518-2227001.jpg" alt="20120518-222700.jpg" class="alignnone size-full" /></a></p>
<div class="AWD_like_button "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.perdomocore.com%2F2012%2Fkorean-bbq-tonight%2F&amp;send=false&amp;layout=standard&amp;width=&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=40" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:40px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.perdomocore.com/2012/korean-bbq-tonight/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using ggplot to make candlestick charts *alpha*</title>
		<link>http://www.perdomocore.com/2012/using-ggplot-to-make-candlestick-charts-alpha/</link>
		<comments>http://www.perdomocore.com/2012/using-ggplot-to-make-candlestick-charts-alpha/#comments</comments>
		<pubDate>Mon, 05 Mar 2012 17:04:40 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Finance]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[ggplot]]></category>
		<category><![CDATA[graphs]]></category>
		<category><![CDATA[quantmod]]></category>

		<guid isPermaLink="false">http://www.perdomocore.com/?p=107</guid>
		<description><![CDATA[In my investments class, we have to produce charts and perform technical analysis. Though quantmod has the mucho excellente chartSeries() function, I can't leave well enough alone and decided to try to write some functions that will draw a chart using ggplot and add technical indicators. I got basic functionality down, but want to continue [...]]]></description>
			<content:encoded><![CDATA[<p>In my investments class, we have to produce charts and perform technical analysis.  Though quantmod has the mucho excellente <code>chartSeries()</code> function, I can't leave well enough alone and decided to try to write some functions that will draw a chart using ggplot and add technical indicators.</p>
<p>I got basic functionality down, but want to continue to add things to the function.  call <code>ggChartSeries()</code> and provide an OHLC object from quantmod, along with start and end dates in <code>as.Date()</code> form.  It calculates moving averages and after that trims the data series, as opposed to chartSeries(), which has issues with this since it takes the pre-trimmed data as an input.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.perdomocore.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=107&amp;download=ggChartSeries.r">ggChartSeries.r</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1072"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code" id="p107code2"><pre class="rsplus" style="font-family:monospace;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>quantmod<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>ggplot2<span style="color: #080;">&#41;</span>
&nbsp;
getSymbols<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'AAPL'</span><span style="color: #080;">&#41;</span>
x<span style="color: #080;">&lt;-</span>AAPL
<span style="color: #0000FF; font-weight: bold;">start</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/Sys.Date.html"><span style="color: #0000FF; font-weight: bold;">Sys.<span style="">Date</span></span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span><span style="color: #080;">-</span><span style="color: #ff0000;">200</span>
<span style="color: #0000FF; font-weight: bold;">end</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/Sys.Date.html"><span style="color: #0000FF; font-weight: bold;">Sys.<span style="">Date</span></span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#Pass an OHLC object into this function</span>
<span style="color: #228B22;">#also pass two dates formatted as.Date()</span>
ggChartSeries <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>x, <span style="color: #0000FF; font-weight: bold;">start</span>, <span style="color: #0000FF; font-weight: bold;">end</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
&nbsp;
<span style="color: #228B22;"># the below is done redundantly for ease of maintenance later on</span>
<span style="color: #228B22;">#First, strip OHLC data (need to vectorize)</span>
  <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/date.html"><span style="color: #0000FF; font-weight: bold;">date</span></a> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.Date.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">Date</span></span></a><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">time</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/open.html"><span style="color: #0000FF; font-weight: bold;">open</span></a> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">vector</span></span></a><span style="color: #080;">&#40;</span>Op<span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  high <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">vector</span></span></a><span style="color: #080;">&#40;</span>Hi<span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  low <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">vector</span></span></a><span style="color: #080;">&#40;</span>Lo<span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/close.html"><span style="color: #0000FF; font-weight: bold;">close</span></a> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">vector</span></span></a><span style="color: #080;">&#40;</span>Cl<span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#Then build the data frame</span>
  xSubset <span style="color: #080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'date'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/date.html"><span style="color: #0000FF; font-weight: bold;">date</span></a>,<span style="color: #ff0000;">'open'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/open.html"><span style="color: #0000FF; font-weight: bold;">open</span></a>,<span style="color: #ff0000;">'high'</span><span style="color: #080;">=</span> high,<span style="color: #ff0000;">'low'</span><span style="color: #080;">=</span>low,<span style="color: #ff0000;">'close'</span><span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/close.html"><span style="color: #0000FF; font-weight: bold;">close</span></a><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#We want to construct our candlesticks  </span>
  xSubset$candleLower <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/pmin.html"><span style="color: #0000FF; font-weight: bold;">pmin</span></a><span style="color: #080;">&#40;</span>xSubset$open, xSubset$close<span style="color: #080;">&#41;</span>
  xSubset$candleMiddle <span style="color: #080;">&lt;-</span> NA
  xSubset$candleUpper <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/pmax.html"><span style="color: #0000FF; font-weight: bold;">pmax</span></a><span style="color: #080;">&#40;</span>xSubset$open, xSubset$close<span style="color: #080;">&#41;</span>
  xSubset$fill <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">''</span>
  xSubset$fill<span style="color: #080;">&#91;</span>xSubset$open <span style="color: #080;">&lt;</span> xSubset$close<span style="color: #080;">&#93;</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">'white'</span>
  xSubset$fill<span style="color: #080;">&#91;</span>xSubset$fill <span style="color: #080;">==</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#93;</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">'red'</span>
&nbsp;
<span style="color: #228B22;">#Add Moving Averages</span>
  xSubset$ma200 <span style="color: #080;">&lt;-</span> SMA<span style="color: #080;">&#40;</span>xSubset$close, <span style="color: #ff0000;">200</span><span style="color: #080;">&#41;</span>
  xSubset$ma50 <span style="color: #080;">&lt;-</span> SMA<span style="color: #080;">&#40;</span>xSubset$close, <span style="color: #ff0000;">50</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#Trim Data</span>
  xSubset <span style="color: #080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>xSubset, xSubset$date <span style="color: #080;">&gt;</span> <span style="color: #0000FF; font-weight: bold;">start</span> <span style="color: #080;">&amp;</span> xSubset$date <span style="color: #080;">&lt;</span> <span style="color: #0000FF; font-weight: bold;">end</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#Graphing Step</span>
  g <span style="color: #080;">&lt;-</span> ggplot<span style="color: #080;">&#40;</span>xSubset, aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/date.html"><span style="color: #0000FF; font-weight: bold;">date</span></a>, lower<span style="color: #080;">=</span>candleLower, middle<span style="color: #080;">=</span>candleMiddle, upper<span style="color: #080;">=</span>candleUpper, ymin<span style="color: #080;">=</span>low, ymax<span style="color: #080;">=</span>high<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> 
  g <span style="color: #080;">&lt;-</span> g <span style="color: #080;">+</span> geom_boxplot<span style="color: #080;">&#40;</span>stat<span style="color: #080;">=</span><span style="color: #ff0000;">'identity'</span>, aes<span style="color: #080;">&#40;</span>group<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/date.html"><span style="color: #0000FF; font-weight: bold;">date</span></a>, fill<span style="color: #080;">=</span>fill<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  g <span style="color: #080;">&lt;-</span> g <span style="color: #080;">+</span> geom_line<span style="color: #080;">&#40;</span>aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/date.html"><span style="color: #0000FF; font-weight: bold;">date</span></a>, y<span style="color: #080;">=</span>ma50<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">+</span> geom_line<span style="color: #080;">&#40;</span>aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/date.html"><span style="color: #0000FF; font-weight: bold;">date</span></a>, y<span style="color: #080;">=</span>ma200<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  g 
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;">#call our graphing function</span>
ggChartSeries<span style="color: #080;">&#40;</span>AAPL, <span style="color: #0000FF; font-weight: bold;">start</span>, <span style="color: #0000FF; font-weight: bold;">end</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<h2>Todo list:</h2>
<ul>
<li>Add titles and labeling</li>
<li>Add more TA indicators</li>
<li>Tweak colors</li>
<li>Add/refine options to the function</li>
<li>Add volume bars at the bottom</li>
</ul>
<div class="AWD_like_button "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.perdomocore.com%2F2012%2Fusing-ggplot-to-make-candlestick-charts-alpha%2F&amp;send=false&amp;layout=standard&amp;width=&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=40" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:40px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.perdomocore.com/2012/using-ggplot-to-make-candlestick-charts-alpha/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>R Protip: Sorting a data frame</title>
		<link>http://www.perdomocore.com/2012/r-protip-sorting-a-data-frame/</link>
		<comments>http://www.perdomocore.com/2012/r-protip-sorting-a-data-frame/#comments</comments>
		<pubDate>Sun, 04 Mar 2012 16:42:20 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[protip]]></category>

		<guid isPermaLink="false">http://www.perdomocore.com/?p=104</guid>
		<description><![CDATA[Working on my personal project today, I had to figure out how to sort out a data frame, and very little straightforward information exists about doing this, so I ended up figuring it out myself from a gaggle of incomplete internet posts. The key to doing this is using both order() and with(). Order returns [...]]]></description>
			<content:encoded><![CDATA[<p>Working on my personal project today, I had to figure out how to sort out a data frame, and very little straightforward information exists about doing this, so I ended up figuring it out myself from a gaggle of incomplete internet posts.</p>
<p>The key to doing this is using both <code>order()</code> and <code>with()</code>. Order returns the ID numbers of how the vector <em>should</em> look. With allows you to rewrite a new data frame.</p>
<p>My code ended up looking like this:</p>
<p>&nbsp;</p>
<pre>country.trim &lt;- country.trim[with(country.trim, order(-income)),]</pre>
<p>I started with my original variable <code>country.trim</code> which was a dataframe containing variables <code>country</code>, <code>income</code>, and <code>debt</code>. I desired to sort the countries by income in descending order. So what we do is sort of "play back" the existing dataframe into its sorted form. <code>with()</code> <code>country.trim</code>, we take the <code>order()</code>. <code>order()</code> will only sort in ascending order, so we make income negative. <code>order()</code> gives us a vector of what the new row names should be, and we play that back into the new <code>country.trim</code> variable.</p>
<p>In the more general case, this is what you want to do:</p>
<pre><code>dataFrame.sorted &lt;- dataFrame.original[ with(dataFrame.original, order(sortCriteria1, sortCriteria2,....)) , ]</code></pre>
<p>Do this where each sortCriteria is one of the members of the data frame. For example, in the example of my code, I don't use <code>order(-country$income)</code>. You can have as many sort criteria as you want, and remember to prepend a minus sign to any variables that should be sorted in descending order.</p>
<div class="AWD_like_button "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.perdomocore.com%2F2012%2Fr-protip-sorting-a-data-frame%2F&amp;send=false&amp;layout=standard&amp;width=&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=40" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:40px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.perdomocore.com/2012/r-protip-sorting-a-data-frame/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Revised Aumann-Serrano Function</title>
		<link>http://www.perdomocore.com/2012/revised-aumann-serrano-function/</link>
		<comments>http://www.perdomocore.com/2012/revised-aumann-serrano-function/#comments</comments>
		<pubDate>Mon, 20 Feb 2012 17:13:42 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Aumann-Serrano]]></category>
		<category><![CDATA[ggplot]]></category>
		<category><![CDATA[risk]]></category>

		<guid isPermaLink="false">http://www.perdomocore.com/?p=87</guid>
		<description><![CDATA[The other week, I posted a simple algorithm to figure out Aumann-Serrano riskiness. The algorithm is slow and not very inventive, so I have been brainstorming all week how to improve it. Since we know exactly the value we are trying to reach and the parameters of the output, I figured we could converge on [...]]]></description>
			<content:encoded><![CDATA[<p>The other week, I <a href="http://www.perdomocore.com/2012/calculate-aumann-serrano-riskiness-with-r/" title="Calculate Aumann-Serrano riskiness with R">posted</a> a simple algorithm to figure out Aumann-Serrano riskiness.  The algorithm is slow and not very inventive, so I have been brainstorming all week how to improve it.<div id="attachment_91" class="wp-caption alignright" style="width: 310px"><a href="http://www.perdomocore.com/wp-content/uploads/2012/02/Rplot03.png" rel="lightbox"><img src="http://www.perdomocore.com/wp-content/uploads/2012/02/Rplot03-300x203.png" alt="convergence illustrated" title="Rplot03" width="300" height="203" class="size-medium wp-image-91" /></a><p class="wp-caption-text">Convergence for the calculation of A-S Riskiness for weekly AAPL returns</p></div> </p>
<p>Since we know exactly the value we are trying to reach and the parameters of the output, I figured we could converge on the solution from both sides and arrive at the solution much more quickly.</p>
<p>Thus, I redesigned the algorithm to bounce back and forth between max and min values, dividing by half for each iteration. Here is the source code for my redesigned version of asRisk().  As always, feed it a vector of possible returns. <span id="more-87"></span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.perdomocore.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=87&amp;download=asRisk.r">asRisk.r</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p875"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code" id="p87code5"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">#This function bounces back and forth to find asRisk much quicker</span>
<span style="color: #228B22;">#Feed me a vector of returns</span>
asRisk <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  <span style="color: #228B22;">#If there are no negative bets and negative expected value, then return 0</span>
  <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a> <span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/mean.html"><span style="color: #0000FF; font-weight: bold;">mean</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&lt;</span><span style="color: #ff0000;">0</span><span style="color: #080;">|</span>min<span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&gt;=</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>
    <span style="color: #228B22;">#first let's use e as our first guess</span>
    asNumber <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="color: #0000FF; font-weight: bold;">exp</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
    total <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="color: #0000FF; font-weight: bold;">exp</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>x<span style="color: #080;">/</span>asNumber<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
    <span style="color: #228B22;">#If the number is too low, we will try to find a min limit</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a> <span style="color: #080;">&#40;</span>total <span style="color: #080;">&lt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="color: #0000FF; font-weight: bold;">max</span></a> <span style="color: #080;">&lt;-</span> asNumber
      <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/while.html"><span style="color: #0000FF; font-weight: bold;">while</span></a> <span style="color: #080;">&#40;</span>total <span style="color: #080;">&lt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
        asNumber <span style="color: #080;">&lt;-</span> asNumber <span style="color: #080;">/</span> <span style="color: #ff0000;">10</span>
        total <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="color: #0000FF; font-weight: bold;">exp</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>x<span style="color: #080;">/</span>asNumber<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
      <span style="color: #080;">&#125;</span>
      <span style="color: #228B22;">#we found the max, so set the min</span>
      <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a> <span style="color: #080;">&lt;-</span> asNumber
    <span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a> <span style="color: #080;">&#40;</span>total <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a> <span style="color: #080;">&lt;-</span> asNumber
      <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/while.html"><span style="color: #0000FF; font-weight: bold;">while</span></a> <span style="color: #080;">&#40;</span>total <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
        asNumber <span style="color: #080;">&lt;-</span> asNumber <span style="color: #080;">*</span> <span style="color: #ff0000;">10</span>
        total <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="color: #0000FF; font-weight: bold;">exp</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>x<span style="color: #080;">/</span>asNumber<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
      <span style="color: #080;">&#125;</span>
      <span style="color: #228B22;">#we found the min, so set the max</span>
      <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="color: #0000FF; font-weight: bold;">max</span></a> <span style="color: #080;">&lt;-</span> asNumber
    <span style="color: #080;">&#125;</span>
    <span style="color: #228B22;">#Precision is adjustable</span>
    <span style="color: #228B22;">#We will do until difference between max/min </span>
    <span style="color: #228B22;">#is within tolerance.</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/while.html"><span style="color: #0000FF; font-weight: bold;">while</span></a> <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span>max<span style="color: #080;">-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&gt;</span><span style="color: #ff0000;">0.000000001</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      asNumber <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="color: #0000FF; font-weight: bold;">max</span></a><span style="color: #080;">+</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span>
      total <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="color: #0000FF; font-weight: bold;">exp</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>x<span style="color: #080;">/</span>asNumber<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
      <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a><span style="color: #080;">&#40;</span>total <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
        <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a> <span style="color: #080;">&lt;-</span> asNumber
      <span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a> <span style="color: #080;">&#40;</span>total <span style="color: #080;">&lt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
        <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="color: #0000FF; font-weight: bold;">max</span></a> <span style="color: #080;">&lt;-</span> asNumber
      <span style="color: #080;">&#125;</span>
    <span style="color: #080;">&#125;</span>
&nbsp;
&nbsp;
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sprintf.html"><span style="color: #0000FF; font-weight: bold;">sprintf</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;%.6f&quot;</span>,asNumber<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #080;">&#125;</span>
<span style="color: #080;">&#125;</span></pre></td></tr></table></div>

<p>You can fiddle around with the code to adjust the precision, but I am currently very happy with the speed/precision tradeoff at the moment.  This new function quickly calculates very precise Aumann-Serrano riskiness, as verified by my older function.  I have yet to include warnings though.  If the problem of out of bounds, I merely return "0" rather than throwing an error.</p>
<h2>Bonus: The code to produce the graph pictured above</h2>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.perdomocore.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=87&amp;download=maxMinGraphed.r">maxMinGraphed.r</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p876"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
</pre></td><td class="code" id="p87code6"><pre class="rsplus" style="font-family:monospace;"><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>quantmod<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/library.html"><span style="color: #0000FF; font-weight: bold;">library</span></a><span style="color: #080;">&#40;</span>ggplot2<span style="color: #080;">&#41;</span>
&nbsp;
getSymbols<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;AAPL&quot;</span><span style="color: #080;">&#41;</span>
x<span style="color: #080;">&lt;-</span>weeklyReturn<span style="color: #080;">&#40;</span>AAPL<span style="color: #080;">&#41;</span>
count<span style="color: #080;">&lt;-</span><span style="color: #ff0000;">1</span>
minV <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">100</span>
maxV <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">1</span><span style="color: #080;">:</span><span style="color: #ff0000;">100</span>
&nbsp;
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a> <span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/mean.html"><span style="color: #0000FF; font-weight: bold;">mean</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&lt;</span><span style="color: #ff0000;">0</span><span style="color: #080;">|</span>min<span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&gt;=</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>
  asNumber <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="color: #0000FF; font-weight: bold;">exp</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>
  total <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="color: #0000FF; font-weight: bold;">exp</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>x<span style="color: #080;">/</span>asNumber<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  count <span style="color: #080;">&lt;-</span> count <span style="color: #080;">+</span> <span style="color: #ff0000;">1</span>
  <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a> <span style="color: #080;">&#40;</span>total <span style="color: #080;">&lt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="color: #0000FF; font-weight: bold;">max</span></a> <span style="color: #080;">&lt;-</span> asNumber
    maxV<span style="color: #080;">&#91;</span>count<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span>asNumber
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/while.html"><span style="color: #0000FF; font-weight: bold;">while</span></a> <span style="color: #080;">&#40;</span>total <span style="color: #080;">&lt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      asNumber <span style="color: #080;">&lt;-</span> asNumber <span style="color: #080;">/</span> <span style="color: #ff0000;">10</span>
      total <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="color: #0000FF; font-weight: bold;">exp</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>x<span style="color: #080;">/</span>asNumber<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
      count <span style="color: #080;">&lt;-</span>count <span style="color: #080;">+</span> <span style="color: #ff0000;">1</span>
    <span style="color: #080;">&#125;</span>
    <span style="color: #228B22;">#we found the max, so set the min</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a> <span style="color: #080;">&lt;-</span> asNumber
    minV<span style="color: #080;">&#91;</span>count<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> asNumber
  <span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a> <span style="color: #080;">&#40;</span>total <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a> <span style="color: #080;">&lt;-</span> asNumber
    minV<span style="color: #080;">&#91;</span>count<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> asNumber
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/while.html"><span style="color: #0000FF; font-weight: bold;">while</span></a> <span style="color: #080;">&#40;</span>total <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      asNumber <span style="color: #080;">&lt;-</span> asNumber <span style="color: #080;">*</span> <span style="color: #ff0000;">10</span>
      total <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="color: #0000FF; font-weight: bold;">exp</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>x<span style="color: #080;">/</span>asNumber<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
      count <span style="color: #080;">&lt;-</span> count <span style="color: #080;">+</span> <span style="color: #ff0000;">1</span>
    <span style="color: #080;">&#125;</span>
    <span style="color: #228B22;">#we found the min, so set the max</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="color: #0000FF; font-weight: bold;">max</span></a> <span style="color: #080;">&lt;-</span> asNumber
    maxV<span style="color: #080;">&#91;</span>count<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> asNumber
  <span style="color: #080;">&#125;</span>
  <span style="color: #228B22;">#Precision is adjustable</span>
  <span style="color: #228B22;">#We will do until difference between max/min </span>
  <span style="color: #228B22;">#is within tolerance.</span>
  <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/while.html"><span style="color: #0000FF; font-weight: bold;">while</span></a> <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span>max<span style="color: #080;">-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&gt;</span><span style="color: #ff0000;">0.000000001</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    asNumber <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="color: #0000FF; font-weight: bold;">max</span></a><span style="color: #080;">+</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span>
    total <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="color: #0000FF; font-weight: bold;">exp</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>x<span style="color: #080;">/</span>asNumber<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
    count <span style="color: #080;">&lt;-</span> count <span style="color: #080;">+</span> <span style="color: #ff0000;">1</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a><span style="color: #080;">&#40;</span>total <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a> <span style="color: #080;">&lt;-</span> asNumber
      minV<span style="color: #080;">&#91;</span>count<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> asNumber
    <span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a> <span style="color: #080;">&#40;</span>total <span style="color: #080;">&lt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
      <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="color: #0000FF; font-weight: bold;">max</span></a> <span style="color: #080;">&lt;-</span> asNumber
      maxV<span style="color: #080;">&#91;</span>count<span style="color: #080;">&#93;</span> <span style="color: #080;">&lt;-</span> asNumber
    <span style="color: #080;">&#125;</span>
  <span style="color: #080;">&#125;</span>
<span style="color: #080;">&#125;</span>
&nbsp;
<span style="color: #228B22;">#trim output and put it into data fram output</span>
minV <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>minV, <span style="color: #080;">&#40;</span>minV <span style="color: #080;">%%</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">!=</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>
maxV <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>maxV, <span style="color: #080;">&#40;</span>maxV <span style="color: #080;">%%</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">!=</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>minV<span style="color: #080;">&#41;</span><span style="color: #080;">&lt;</span>length<span style="color: #080;">&#40;</span>maxV<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  count <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>minV<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>
  count <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>maxV<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span>
output <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/data.frame.html"><span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span></a><span style="color: #080;">&#40;</span>count<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span>count<span style="color: #080;">&#41;</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a><span style="color: #080;">=</span>minV<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span>count<span style="color: #080;">&#93;</span>, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="color: #0000FF; font-weight: bold;">max</span></a><span style="color: #080;">=</span>maxV<span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">:</span>count<span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#let's graph</span>
g <span style="color: #080;">&lt;-</span> ggplot<span style="color: #080;">&#40;</span>output, aes<span style="color: #080;">&#40;</span>count, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
g <span style="color: #080;">&lt;-</span> g <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_point<span style="color: #080;">&#40;</span>aes<span style="color: #080;">&#40;</span>count,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="color: #0000FF; font-weight: bold;">max</span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
g <span style="color: #080;">&lt;-</span> g<span style="color: #080;">+</span> geom_ribbon<span style="color: #080;">&#40;</span>aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span>count, ymin<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a>, ymax<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/max.html"><span style="color: #0000FF; font-weight: bold;">max</span></a><span style="color: #080;">&#41;</span>, alpha<span style="color: #080;">=</span>.4<span style="color: #080;">&#41;</span>
g <span style="color: #080;">&lt;-</span> g <span style="color: #080;">+</span> opts<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/title.html"><span style="color: #0000FF; font-weight: bold;">title</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">'Convergence'</span><span style="color: #080;">&#41;</span><span style="color: #080;">+</span> scale_x_continuous<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Repetitions'</span><span style="color: #080;">&#41;</span><span style="color: #080;">+</span>scale_y_continuous<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Max and Min'</span><span style="color: #080;">&#41;</span>
g 
ggsave<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;file.png&quot;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<div class="AWD_like_button "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.perdomocore.com%2F2012%2Frevised-aumann-serrano-function%2F&amp;send=false&amp;layout=standard&amp;width=&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=40" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:40px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.perdomocore.com/2012/revised-aumann-serrano-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Will inequality continue to get worse? R to the rescue!</title>
		<link>http://www.perdomocore.com/2012/will-inequality-continue-to-get-worse-r-to-the-rescue/</link>
		<comments>http://www.perdomocore.com/2012/will-inequality-continue-to-get-worse-r-to-the-rescue/#comments</comments>
		<pubDate>Sat, 11 Feb 2012 18:50:57 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[chartjunk]]></category>
		<category><![CDATA[FRED]]></category>
		<category><![CDATA[ggplot]]></category>
		<category><![CDATA[graphs]]></category>

		<guid isPermaLink="false">http://www.perdomocore.com/?p=77</guid>
		<description><![CDATA[Recently, reading an article by Megan McArdle about income inequality, she speculated about the idea that the share of income of the 1% gets worse during a recession. She posted a graph: I wasn't a fan of the graph. The ticks distracted from the data being presented, and recessions were not highlighted on the graph, [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, reading an <a href="http://www.theatlantic.com/business/archive/2012/02/will-inequality-keep-getting-worse/252841/">article</a> by Megan McArdle about income inequality, she speculated about the idea that the share of income of the 1% gets worse during a recession. She posted a graph:<br />
<a href="http://www.perdomocore.com/wp-content/uploads/2012/02/screen.png"><img class="aligncenter size-full wp-image-79" title="Inequality" src="http://www.perdomocore.com/wp-content/uploads/2012/02/screen.png" alt="Income share of the top 1% in the US from 1913" width="570" height="417" /></a><br />
I wasn't a fan of the graph. The ticks distracted from the data being presented, and recessions were not highlighted on the graph, as they are on graphs from places like <a href="http://research.stlouisfed.org/fred2/">FRED</a>. Fortunately the data from the graph is available and we can make a run of it using R and <a href="http://had.co.nz/ggplot/" target="_blank">ggplot</a>.<span id="more-77"></span></p>
<h2>Data:</h2>
<p>-Inequality data available from Saez's <a href="http://elsa.berkeley.edu/~saez/" target="_blank">website</a>: <a href="http://elsa.berkeley.edu/~saez/TabFig2008.xls" target="_blank">Data updated through 2008 (.xls)</a>  Look for the sheet "data-Fig2" for the top 1% income including capital gains from 1913-2008<br />
-Recession data from FRED using code USREC, although a list that can be copy-pasted into R/SAS is available at the <a href="http://research.stlouisfed.org/fred2/help-faq/" target="_blank">FRED FAQ</a> page</p>
<p>The inspiration for this graph comes from a <a href="http://jeffreybreen.wordpress.com/2011/08/15/recession-bars/" target="_blank">post </a>about using R+ggplot to show recession bars and silver prices.  I thought the addition of recession bars and elimination of chartjunk would be nice.</p>
<p>Code is here, it includes all the data in a self contained package and will output a nice anti-aliased PNG using ggsave:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.perdomocore.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=77&amp;download=inequalitygraph.r">inequalitygraph.r</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p778"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
</pre></td><td class="code" id="p77code8"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">#load dependencies</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>ggplot2<span style="color: #080;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #228B22;">#grab recession data and put it into a dataframe recessions.df</span>
recessions.<span style="">df</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">table</span></span><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/textConnection.html"><span style="color: #0000FF; font-weight: bold;">textConnection</span></a><span style="color: #080;">&#40;</span>
  <span style="color: #ff0000;">&quot;Peak, Trough
1857-06-01, 1858-12-01
  1860-10-01, 1861-06-01
  1865-04-01, 1867-12-01
  1869-06-01, 1870-12-01
  1873-10-01, 1879-03-01
  1882-03-01, 1885-05-01
  1887-03-01, 1888-04-01
  1890-07-01, 1891-05-01
  1893-01-01, 1894-06-01
  1895-12-01, 1897-06-01
  1899-06-01, 1900-12-01
  1902-09-01, 1904-08-01
  1907-05-01, 1908-06-01
  1910-01-01, 1912-01-01
  1913-01-01, 1914-12-01
  1918-08-01, 1919-03-01
  1920-01-01, 1921-07-01
  1923-05-01, 1924-07-01
  1926-10-01, 1927-11-01
  1929-08-01, 1933-03-01
  1937-05-01, 1938-06-01
  1945-02-01, 1945-10-01
  1948-11-01, 1949-10-01
  1953-07-01, 1954-05-01
  1957-08-01, 1958-04-01
  1960-04-01, 1961-02-01
  1969-12-01, 1970-11-01
  1973-11-01, 1975-03-01
  1980-01-01, 1980-07-01
  1981-07-01, 1982-11-01
  1990-07-01, 1991-03-01
  2001-03-01, 2001-11-01
  2007-12-01, 2009-06-01&quot;</span><span style="color: #080;">&#41;</span>, sep<span style="color: #080;">=</span><span style="color: #ff0000;">','</span>,
colClasses<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Date'</span>, <span style="color: #ff0000;">'Date'</span><span style="color: #080;">&#41;</span>, header<span style="color: #080;">=</span>TRUE<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#grab inequality data and put it into a dataframe inequality.df</span>
inequality.<span style="">df</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">read.<span style="">table</span></span><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/textConnection.html"><span style="color: #0000FF; font-weight: bold;">textConnection</span></a><span style="color: #080;">&#40;</span>
  <span style="color: #ff0000;">&quot;year,percent
1913,0.18
1914,0.182
1915,0.176
1916,0.193
1917,0.177
1918,0.16
1919,0.164
1920,0.148
1921,0.156
1922,0.171
1923,0.156
1924,0.174
1925,0.202
1926,0.199
1927,0.21
1928,0.239
1929,0.224
1930,0.172
1931,0.155
1932,0.156
1933,0.165
1934,0.164
1935,0.167
1936,0.193
1937,0.171
1938,0.158
1939,0.162
1940,0.165
1941,0.158
1942,0.134
1943,0.123
1944,0.113
1945,0.125
1946,0.133
1947,0.12
1948,0.122
1949,0.117
1950,0.128
1951,0.118
1952,0.108
1953,0.099
1954,0.108
1955,0.111
1956,0.107
1957,0.102
1958,0.102
1959,0.106
1960,0.1
1961,0.106
1962,0.099
1963,0.099
1964,0.105
1965,0.109
1966,0.102
1967,0.107
1968,0.112
1969,0.104
1970,0.09
1971,0.094
1972,0.096
1973,0.092
1974,0.091
1975,0.089
1976,0.089
1977,0.09
1978,0.09
1979,0.1
1980,0.1
1981,0.1
1982,0.108
1983,0.116
1984,0.12
1985,0.127
1986,0.159
1987,0.127
1988,0.155
1989,0.145
1990,0.143
1991,0.134
1992,0.147
1993,0.142
1994,0.142
1995,0.152
1996,0.167
1997,0.18
1998,0.191
1999,0.2
2000,0.215
2001,0.182
2002,0.169
2003,0.175
2004,0.198
2005,0.219
2006,0.228
2007,0.235
2008,0.209&quot;</span><span style="color: #080;">&#41;</span>, sep<span style="color: #080;">=</span><span style="color: #ff0000;">','</span>, colClasses<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/c.html"><span style="color: #0000FF; font-weight: bold;">c</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'numeric'</span>, <span style="color: #ff0000;">'numeric'</span><span style="color: #080;">&#41;</span>, header<span style="color: #080;">=</span>TRUE<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#trim recessions to meet minimum date of data</span>
recessions.<span style="">trim</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>recessions.<span style="">df</span>, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/format.html"><span style="color: #0000FF; font-weight: bold;">format</span></a><span style="color: #080;">&#40;</span>Peak,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/format.html"><span style="color: #0000FF; font-weight: bold;">format</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;%Y&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&gt;=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/min.html"><span style="color: #0000FF; font-weight: bold;">min</span></a><span style="color: #080;">&#40;</span>inequality.<span style="">df</span>$year<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#build a ggplot object with the inequality data, Note: X axis is &quot;date&quot;, not &quot;continuous</span>
g <span style="color: #080;">&lt;-</span> ggplot<span style="color: #080;">&#40;</span>inequality.<span style="">df</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> geom_line<span style="color: #080;">&#40;</span>aes<span style="color: #080;">&#40;</span>x<span style="color: #080;">=</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.Date.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">Date</span></span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/strptime.html"><span style="color: #0000FF; font-weight: bold;">strptime</span></a><span style="color: #080;">&#40;</span>year, <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/format.html"><span style="color: #0000FF; font-weight: bold;">format</span></a> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;%Y&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, y<span style="color: #080;">=</span>percent<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>theme_grey<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
g <span style="color: #080;">&lt;-</span> g <span style="color: #080;">+</span> scale_x_date<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Year'</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/format.html"><span style="color: #0000FF; font-weight: bold;">format</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;%Y&quot;</span>, major<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;5 years&quot;</span>, minor<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;1 year&quot;</span><span style="color: #080;">&#41;</span>
g <span style="color: #080;">&lt;-</span> g <span style="color: #080;">+</span> scale_y_continuous<span style="color: #080;">&#40;</span><span style="color: #ff0000;">'Percent of total income'</span>,formatter<span style="color: #080;">=</span><span style="color: #ff0000;">'percent'</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#add recession boxes</span>
g <span style="color: #080;">=</span> g <span style="color: #080;">+</span> geom_rect<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">data</span><span style="color: #080;">=</span>recessions.<span style="">trim</span>, aes<span style="color: #080;">&#40;</span>xmin<span style="color: #080;">=</span>Peak, xmax<span style="color: #080;">=</span>Trough, ymin<span style="color: #080;">=-</span>Inf, ymax<span style="color: #080;">=+</span>Inf<span style="color: #080;">&#41;</span>, fill<span style="color: #080;">=</span><span style="color: #ff0000;">'blue'</span>, alpha<span style="color: #080;">=</span><span style="color: #ff0000;">0.2</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#add title</span>
g <span style="color: #080;">&lt;-</span> g<span style="color: #080;">+</span> opts<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/title.html"><span style="color: #0000FF; font-weight: bold;">title</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Income share of top 1%<span style="color: #000099; font-weight: bold;">\n</span>1913-2008&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #228B22;">#print graph</span>
g
ggsave<span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/file.html"><span style="color: #0000FF; font-weight: bold;">file</span></a><span style="color: #080;">=</span><span style="color: #ff0000;">'output.png'</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Some lessons learned from this project mainly involve the grammar of ggplot2, but more importantly how to use data types and axis scaling.  Originally, I did this graph at 6am on Friday morning.  As a shortcut, I simply collapsed recession data into yearly data so that I had two vectors with numeric data.  In this more well-thought-out version, I use <code>scale_x_date()</code> instead of <code>scale_x_continuous()</code>.  This allowed me to control axis formatting better, at the expense of requiring me to figure out how to convert both <code>%Y</code> and <code>%Y-%M</code> dates to POSIX dates using <code>strptime(year, format = "%Y")</code>.  <code>as.Date()</code> and other functions would not allow a date without month/day.</p>
<p>In the end, instead of fussing with PNG graphics devices, which lack anti-aliasing under Windows, I used ggplot's built-in function <code>ggsave()</code>, and was very happy with the result:<br />
<div id="attachment_83" class="wp-caption aligncenter" style="width: 614px"><a href="http://www.perdomocore.com/wp-content/uploads/2012/02/output.png" rel="lightbox"><img src="http://www.perdomocore.com/wp-content/uploads/2012/02/output-1024x637.png" alt="Final graph of income share of the top 1%" title="output" width="604" height="375" class="size-large wp-image-83" /></a><p class="wp-caption-text">Final graph of income share of the top 1% with recession bars</p></div></p>
<div class="AWD_like_button "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.perdomocore.com%2F2012%2Fwill-inequality-continue-to-get-worse-r-to-the-rescue%2F&amp;send=false&amp;layout=standard&amp;width=&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=40" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:40px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.perdomocore.com/2012/will-inequality-continue-to-get-worse-r-to-the-rescue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calculate Aumann-Serrano riskiness with R</title>
		<link>http://www.perdomocore.com/2012/calculate-aumann-serrano-riskiness-with-r/</link>
		<comments>http://www.perdomocore.com/2012/calculate-aumann-serrano-riskiness-with-r/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 16:25:28 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Finance]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://www.perdomocore.com/?p=73</guid>
		<description><![CDATA[I completed a preliminary function to calculate Aumann-Serrano riskiness in R ?Download asRisk.r1 2 3 4 5 6 7 8 9 10 11 12 13 asRisk &#60;- function&#40;x&#41;&#123; if &#40;mean&#40;x&#41;&#60;0&#124;min&#40;x&#41;&#62;=0&#41;&#123; return&#40;0&#41; #If expected value is &#60; 0 or there are no negatives, return 0 &#125; else &#123; asNumber &#60;- 0.00001 total &#60;- 2 while &#40;total [...]]]></description>
			<content:encoded><![CDATA[<p>I completed a preliminary function to calculate Aumann-Serrano riskiness in R</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.perdomocore.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=73&amp;download=asRisk.r">asRisk.r</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7311"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p73code11"><pre class="rsplus" style="font-family:monospace;">asRisk <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/function.html"><span style="color: #0000FF; font-weight: bold;">function</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/if.html"><span style="color: #0000FF; font-weight: bold;">if</span></a> <span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/mean.html"><span style="color: #0000FF; font-weight: bold;">mean</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&lt;</span><span style="color: #ff0000;">0</span><span style="color: #080;">|</span>min<span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&gt;=</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span> <span style="color: #228B22;">#If expected value is  &lt; 0 or there are no negatives, return 0</span>
  <span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>
      asNumber <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">0.00001</span>
      total <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">2</span>
      <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/while.html"><span style="color: #0000FF; font-weight: bold;">while</span></a> <span style="color: #080;">&#40;</span>total <span style="color: #080;">&gt;</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
        total <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sum.html"><span style="color: #0000FF; font-weight: bold;">sum</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">/</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/exp.html"><span style="color: #0000FF; font-weight: bold;">exp</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">-</span>x<span style="color: #080;">/</span>asNumber<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
        asNumber <span style="color: #080;">&lt;-</span> asNumber <span style="color: #080;">+</span> .00001
      <span style="color: #080;">&#125;</span>
      <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/return.html"><span style="color: #0000FF; font-weight: bold;">return</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/sprintf.html"><span style="color: #0000FF; font-weight: bold;">sprintf</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;%.5f&quot;</span>,asNumber<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span>
<span style="color: #080;">&#125;</span></pre></td></tr></table></div>

<p>To use this function, input a vector of returns.  If AS risk cannot be calculated, the function will return "0".  If the gambles can be used, it will calculate AS riskiness to 5 decimal points.  If more or less are desired, you can change </p>
<p>Generally, to use this, I would recommend using one of the functions in <a href="http://www.quantmod.com/">quantmod</a> such as <code>weeklyReturn()</code> or <code>dailyReturn()</code>.  An example of this would be</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p73code12'); return false;">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7312"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p73code12"><pre class="rsplus" style="font-family:monospace;">asRisk<span style="color: #080;">&#40;</span>dailyReturn<span style="color: #080;">&#40;</span>AAPL<span style="color: #080;">&#91;</span><span style="color: #ff0000;">'2010'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>This example will return the AS risk of AAPL stock in 2010.  Quantmod uses the TTR package which allows a lot of quick and powerful date selection.</p>
<p>In the future I will add errors/warnings, and maybe make precision adjustable or switch to a solver package.  I also need to revise the function to meet a few more of my <a href="http://www.perdomocore.com/2012/prototyping-r-function-for-aumann-serrano-riskiness/" title="Prototyping an R function for Aumann-Serrano Riskiness">design parameters</a>.  This seems to be a good start and perfect for my research project next semester though!</p>
<div class="AWD_like_button "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.perdomocore.com%2F2012%2Fcalculate-aumann-serrano-riskiness-with-r%2F&amp;send=false&amp;layout=standard&amp;width=&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=40" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:40px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.perdomocore.com/2012/calculate-aumann-serrano-riskiness-with-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prototyping an R function for Aumann-Serrano Riskiness</title>
		<link>http://www.perdomocore.com/2012/prototyping-r-function-for-aumann-serrano-riskiness/</link>
		<comments>http://www.perdomocore.com/2012/prototyping-r-function-for-aumann-serrano-riskiness/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 13:00:21 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://www.perdomocore.com/?p=52</guid>
		<description><![CDATA[Currently, I am need of a function that solves for Aumann-Serrano riskiness. AS riskiness is a favorite academic paper of mine, and it establishes a new measure of riskiness developed in response to the financial crisis. AS Riskiness supposes for each gamble , there exists a unique positive number that satisfies There are a few [...]]]></description>
			<content:encoded><![CDATA[<p>Currently, I am need of a function that solves for <a href="http://www.ma.huji.ac.il/raumann/pdf/Economic%20Index%20of%20Riskiness.pdf">Aumann-Serrano riskiness</a>. AS riskiness is a favorite academic paper of mine, and it establishes a new measure of riskiness developed in response to the financial crisis.</p>
<p>AS Riskiness supposes for each gamble <span class='MathJax_Preview'><img src='http://www.perdomocore.com/wp-content/plugins/latex/cache/tex_b2f5ff47436671b6e533d8dc3614845d.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="g" /></span><script type='math/tex'>g</script>, there exists a unique positive number <span class='MathJax_Preview'><img src='http://www.perdomocore.com/wp-content/plugins/latex/cache/tex_3eedc635bf57691aee7de14df431dc30.gif' style='vertical-align: middle; border: none; ' class='tex' alt="R(g)" /></span><script type='math/tex'>R(g)</script> that satisfies <span class='MathJax_Preview'><img src='http://www.perdomocore.com/wp-content/plugins/latex/cache/tex_3560cb07858b6a96c3f162c42a211a41.gif' style='vertical-align: middle; border: none; ' class='tex' alt="{\rm{E}}{e^{ - g/R(g)}} = 1" /></span><script type='math/tex'>{\rm{E}}{e^{ - g/R(g)}} = 1</script></p>
<p>There are a few conditions though:</p>
<ul>
<li>Gambles must have a positive expected value (you can't use it at Vegas)</li>
<li>Gambles must include one negative outcome (you must have skin in the game)</li>
</ul>
<p><span id="more-52"></span>In exchange for these restrictions, one gets a measure of risk that is strictly monotone with respect to stochastic dominance. Additionally, a paper in 2011 by Homm and Pigorsch (<a href="http://www.eea-esem.com/files/papers/EEA-ESEM/2011/1166/EPM_ESEM.pdf">link</a>) established an economic performance measurement similar to the Sharpe Ratio, but one that excels at situations in which returns are not normally distributed (such as the 2008 GFC). In my classes, I did an initial exploration of the relation of returns and AS riskiness and found that the tendency of AS riskiness to rapidly respond to losses does have advantages for risk-conscious investors. See the shape of AS risk vs. Return below:</p>
<div id="attachment_67" class="wp-caption aligncenter" style="width: 624px"><a href="http://www.perdomocore.com/wp-content/uploads/2012/02/SP500-by-sectorzoom1.png" rel="lightbox"><img class=" wp-image-67 " title="SP500 by sectorzoom" src="http://www.perdomocore.com/wp-content/uploads/2012/02/SP500-by-sectorzoom1.png" alt="SP500 Component AS Risk vs Return" width="614" height="461" /></a><p class="wp-caption-text">SP500 Component AS Risk vs Return for 2010</p></div>
<p>For future investigation, I want to have an R function that calculates AS riskiness, so my prototype is as follows:</p>
<pre>-<strong>Name:</strong> asRisk()
-<strong>Input:</strong> vector of returns in percent
     -This makes it easy to use the function with quantmod
     -Also more versatile
-<strong>Dependencies/Requirements:</strong> none
     -One possible way to solve for AS risk is to use an R package with a solver, such as ROI
     -I would rather not have any dependencies, as my need for precision isn't very high
-<strong>Precision:</strong> 1/1000
-<strong>Method:</strong> Iterative from 0.0001 to 1000
     -The program will give up at 1000, as the function goes out to infinity as returns approach zero
     -In most cases, our risk scores are much much lower, so any sane situation should have no problem with this
     -If this method is too slow on modern computers (unlikely), I will consider moving to a solver package
-<strong>Error Checking:</strong> We will check for
     -Negative overall return (return Error)
     -No negative outcomes (return Error)
     -Failed sanity check, AS Score &gt; 1000 (return Warning)
-<strong>Output:</strong> AS risk score</pre>
<p>This will be my first R function other than "Hello, World!", so I am going to try to keep the solution as simple as possible.  Once I have a solid foundation with my current solver, I will attempt to work towards using option chains to counteract the negative overall return error, and potentially to use a solver.  The simple version will be a large improvement over Excel though, with its floating point errors that are a huge source of errors with large numbers of observations.</p>
<div class="AWD_like_button "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.perdomocore.com%2F2012%2Fprototyping-r-function-for-aumann-serrano-riskiness%2F&amp;send=false&amp;layout=standard&amp;width=&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=40" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:40px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.perdomocore.com/2012/prototyping-r-function-for-aumann-serrano-riskiness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Retrieving foreign stock tickers that start with numbers using quantmod</title>
		<link>http://www.perdomocore.com/2012/foreign-stock-tickers/</link>
		<comments>http://www.perdomocore.com/2012/foreign-stock-tickers/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 09:31:46 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[quantmod]]></category>

		<guid isPermaLink="false">http://www.perdomocore.com/?p=44</guid>
		<description><![CDATA[&#160; When doing research in foreign equities, I always use quantmod and R to get quotes. Google does not usually support CSV downloads of foreign quotes, but in most every case, Yahoo does. The "getSymbols()" function in quantmod is fully equipped for this, except for one crucial problem: foreign exchanges often use numbers rather than [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://www.perdomocore.com/wp-content/uploads/2012/02/Rplot01.png" rel="lightbox"><img class="aligncenter  wp-image-54" title="htc12-2011-to-2-2012" src="http://www.perdomocore.com/wp-content/uploads/2012/02/Rplot01.png" alt="HTC from 12-2011 to 2-2012" width="591" height="488" /></a></p>
<p>&nbsp;</p>
<p>When doing research in foreign equities, I always use <a href="http://www.quantmod.com/">quantmod</a> and R to get quotes. Google does not usually support CSV downloads of foreign quotes, but in most every case, Yahoo does. The "<code>getSymbols()</code>" function in quantmod is fully equipped for this, except for one crucial problem: foreign exchanges often use numbers rather than alphabetical identifiers for ticker symbols, especially in Asia. Examples of this are HTC in Taiwan(<a href="http://finance.yahoo.com/q?s=2498.TW&amp;ql=1">2498.TW</a>), NCSoft in Korea (<a href="http://finance.yahoo.com/q?s=036570.KS&amp;ql=0">036570.KS</a>), and Ping An in Hong Kong (<a href="http://finance.yahoo.com/q?s=2318.HK">2318.HK</a>).<span id="more-44"></span></p>
<p>This nomenclature has obvious implications for computer languages which will generally disallow variable names that begin with numbers. In R, this is also the case, and quantmod will attempt to create a dataframe with the name of the ticker symbol. This problem took a while for me to discover, since quantmod will not give you an error when you try to retrieve the quote. For instance, let me try to retrieve Apple (AAPL):<br />
<code><span style="color: #0000ff;">&gt; getSymbols("AAPL", src = "yahoo")</span><br />
[1] "AAPL"</code></p>
<p>I enter the command "<code>getSymbols()</code>" and when R has finished downloading the data, it echos the name of the dataframe created "AAPL".  Let's try HTC (2498.TW) now:<br />
<code><span style="color: #0000ff;">&gt; getSymbols("2498.tw", src = "yahoo")</span><br />
[1] "2498.TW"</code></p>
<p>It would appear as though the data has been downloaded correctly, and if you have automated this process, you wouldn't know the problem.  The only way the problem will present is as a frustrating error:<br />
<code><span style="color: #0000ff;">&gt; length(AAPL)</span><br />
[1] 4536<br />
<span style="color: #0000ff;">&gt; length(2498.TW)</span><br />
Error: unexpected symbol in "length(2498.TW"</code></p>
<p>The fix for the problem is very simple or very difficult, depending on your situation.  The solution to the problem is to set "<code>auto.assign=FALSE</code>" when calling <code>getSymbols()</code>. This will allow you to assign the data returned to a variable name of your choice. For example:<br />
<span style="color: #0000ff;"><code>&gt; HTC.TW &lt;- getSymbols("2498.TW", src = "yahoo", auto.assign=FALSE)</code></span></p>
<p>This code will not echo like the previous examples, since you are explicitly naming the variable yourself.  As I mentioned before, the solution is either simple or complex.  If one were running a long script with many equities from different countries, the lack of errors might cause big problems.  Correcting the names on a large scale would likely rely on using <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expressions</a> or a predefined list of alternate ticker symbols combined with the technique listed above.</p>
<div class="AWD_like_button "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.perdomocore.com%2F2012%2Fforeign-stock-tickers%2F&amp;send=false&amp;layout=standard&amp;width=&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=40" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:40px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.perdomocore.com/2012/foreign-stock-tickers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using R and quantmod to find cost of equity</title>
		<link>http://www.perdomocore.com/2012/using-r-and-quantmod-to-find-cost-of-equity/</link>
		<comments>http://www.perdomocore.com/2012/using-r-and-quantmod-to-find-cost-of-equity/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 08:16:56 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Finance]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[capm]]></category>
		<category><![CDATA[PerformanceAnalytics]]></category>
		<category><![CDATA[quantmod]]></category>

		<guid isPermaLink="false">http://www.perdomocore.com/?p=25</guid>
		<description><![CDATA[Recently, in my financial statements analysis class, I had to perform a valuation of Apple Inc. with a number of different valuation methods.  One of the things that made valuation simpler is the lack of long-term debt on Apple's balance sheet.  This simple fact means that Apple's WACC is equal to the cost of equity. To [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, in my financial statements analysis class, I had to perform a valuation of <a href="https://www.google.com/finance?client=ob&amp;q=NASDAQ:AAPL" target="_blank">Apple Inc.</a> with a number of different valuation methods.  One of the things that made valuation simpler is the lack of long-term debt on Apple's balance sheet.  This simple fact means that Apple's <a href="http://www.investopedia.com/terms/w/wacc.asp" target="_blank">WACC</a> is equal to the cost of equity.</p>
<p>To find the cost of equity, I use <a href="http://en.wikipedia.org/wiki/Capital_asset_pricing_model" target="_blank">CAPM</a>, which states</p>
<p><span class='MathJax_Preview'><img src='http://www.perdomocore.com/wp-content/plugins/latex/cache/tex_25daffdcf59ce7b6f10513e5ee4c452d.gif' style='vertical-align: middle; border: none; ' class='tex' alt="E(R_i) = R_f + \beta_{i}(E(R_m) - R_f)\," /></span><script type='math/tex'>E(R_i) = R_f + \beta_{i}(E(R_m) - R_f)\,</script></p>
<p>where <span class='MathJax_Preview'><img src='http://www.perdomocore.com/wp-content/plugins/latex/cache/tex_b228628da5e8a985db45fc37321cef4f.gif' style='vertical-align: middle; border: none; ' class='tex' alt="E(R_i)" /></span><script type='math/tex'>E(R_i)</script> is the expected return on capital, after accounting for the market risk premium.  To find the component pieces  <span class='MathJax_Preview'><img src='http://www.perdomocore.com/wp-content/plugins/latex/cache/tex_a8d50a42790b8cf303b70df396e5f1ee.gif' style='vertical-align: middle; border: none; ' class='tex' alt="R_f" /></span><script type='math/tex'>R_f</script>,   <span class='MathJax_Preview'><img src='http://www.perdomocore.com/wp-content/plugins/latex/cache/tex_245deed2b19c7bc7eee605f65d4778e3.gif' style='vertical-align: middle; border: none; ' class='tex' alt="R_m" /></span><script type='math/tex'>R_m</script>, and <span class='MathJax_Preview'><img src='http://www.perdomocore.com/wp-content/plugins/latex/cache/tex_f21255955e3fc1d80dd4328fedee714b.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\beta_{i}" /></span><script type='math/tex'>\beta_{i}</script>, I will use R with the quantmod package, and I will also use the PerformanceAnalytics Package, although I will show you how to avoid using it if you choose.</p>
<p>The sourcecode for the project:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.perdomocore.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=25&amp;download=betacalc.r">betacalc.r</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2515"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code" id="p25code15"><pre class="rsplus" style="font-family:monospace;"><span style="color: #228B22;">#Packages required</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>PerformanceAnalytics<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>quantmod<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/require.html"><span style="color: #0000FF; font-weight: bold;">require</span></a><span style="color: #080;">&#40;</span>car<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#Here we get the symbols for the SP500 (GSPC), AAPL, and 5yr Treasuries (GS5)</span>
getSymbols<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;^GSPC&quot;</span>, src <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;yahoo&quot;</span>, from <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.Date.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">Date</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;2008-01-01&quot;</span><span style="color: #080;">&#41;</span>, to <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.Date.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">Date</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;2011-12-31&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
getSymbols<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;AAPL&quot;</span>, src <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;yahoo&quot;</span>, from <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.Date.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">Date</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;2009-01-01&quot;</span><span style="color: #080;">&#41;</span>, to <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.Date.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">Date</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;2011-12-31&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
getSymbols<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;GS5&quot;</span>, src <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;FRED&quot;</span>, from <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.Date.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">Date</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;2008-12-01&quot;</span><span style="color: #080;">&#41;</span>, to <span style="color: #080;">=</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.Date.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">Date</span></span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;2011-12-31&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#Market risk R_m is the arithmetic mean of SP500 from 2009 through 2011</span>
<span style="color: #228B22;">#Riskfree rate is arithmetic mean of 5yr treasuries</span>
marketRisk<span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/mean.html"><span style="color: #0000FF; font-weight: bold;">mean</span></a><span style="color: #080;">&#40;</span>yearlyReturn<span style="color: #080;">&#40;</span>GSPC<span style="color: #080;">&#91;</span><span style="color: #ff0000;">'2009::2011'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
riskFree <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/mean.html"><span style="color: #0000FF; font-weight: bold;">mean</span></a><span style="color: #080;">&#40;</span>GS5<span style="color: #080;">&#91;</span><span style="color: #ff0000;">'2009::2011'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#My professor advised us to use weekly returns taken on wednesday</span>
<span style="color: #228B22;">#so I take a subset of wednesdays and use the quantmod function</span>
<span style="color: #228B22;">#weeklyReturn()</span>
AAPL.<span style="">weekly</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>AAPL,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/weekdays.html"><span style="color: #0000FF; font-weight: bold;">weekdays</span></a><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">time</span><span style="color: #080;">&#40;</span>AAPL<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">==</span><span style="color: #ff0000;">'Wednesday'</span><span style="color: #080;">&#41;</span>
AAPL.<span style="">weekly</span> <span style="color: #080;">&lt;-</span> weeklyReturn<span style="color: #080;">&#40;</span>AAPL<span style="color: #080;">&#91;</span><span style="color: #ff0000;">'2009::2011'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
GSPC.<span style="">weekly</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/subset.html"><span style="color: #0000FF; font-weight: bold;">subset</span></a><span style="color: #080;">&#40;</span>GSPC,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/weekdays.html"><span style="color: #0000FF; font-weight: bold;">weekdays</span></a><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">time</span><span style="color: #080;">&#40;</span>GSPC<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">==</span><span style="color: #ff0000;">'Wednesday'</span><span style="color: #080;">&#41;</span>
GSPC.<span style="">weekly</span> <span style="color: #080;">&lt;-</span> weeklyReturn<span style="color: #080;">&#40;</span>GSPC<span style="color: #080;">&#91;</span><span style="color: #ff0000;">'2009::2011'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#Here I use PerformanceAnalytics functions for alpha+beta</span>
<span style="color: #228B22;">#Then we calculate Cost of equity using our calculated figures</span>
AAPL.<span style="">beta</span> <span style="color: #080;">&lt;-</span> CAPM.<span style="">beta</span><span style="color: #080;">&#40;</span>AAPL.<span style="">weekly</span>,GSPC.<span style="">weekly</span><span style="color: #080;">&#41;</span>
AAPL.<span style="">alpha</span> <span style="color: #080;">&lt;-</span> CAPM.<span style="">alpha</span><span style="color: #080;">&#40;</span>AAPL.<span style="">weekly</span>,GSPC.<span style="">weekly</span><span style="color: #080;">&#41;</span>
AAPL.<span style="">expectedReturn</span> <span style="color: #080;">&lt;-</span> riskFree <span style="color: #080;">+</span> AAPL.<span style="">beta</span> <span style="color: #080;">*</span> <span style="color: #080;">&#40;</span>marketRisk<span style="color: #080;">-</span>riskFree<span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #228B22;">#For my graph, I want to show R^2, so we get it from the</span>
<span style="color: #228B22;">#lm object AAPL.reg</span>
AAPL.<span style="">reg</span><span style="color: #080;">&lt;-</span><span style="color: #0000FF; font-weight: bold;">lm</span><span style="color: #080;">&#40;</span>AAPL.<span style="">weekly</span>~GSPC.<span style="">weekly</span><span style="color: #080;">&#41;</span>
AAPL.<span style="">rsquared</span><span style="color: #080;">&lt;-</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/summary.html"><span style="color: #0000FF; font-weight: bold;">summary</span></a><span style="color: #080;">&#40;</span>AAPL.<span style="">reg</span><span style="color: #080;">&#41;</span>$r.<span style="">squared</span>
&nbsp;
<span style="color: #228B22;">#Lastly, we graph the returns and fit line, along with info</span>
scatterplot<span style="color: #080;">&#40;</span><span style="color: #ff0000;">100</span><span style="color: #080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">vector</span></span></a><span style="color: #080;">&#40;</span>GSPC.<span style="">weekly</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">100</span><span style="color: #080;">*</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">vector</span></span></a><span style="color: #080;">&#40;</span>AAPL.<span style="">weekly</span><span style="color: #080;">&#41;</span>, <span style="color: #0000FF; font-weight: bold;">smooth</span><span style="color: #080;">=</span>FALSE, main<span style="color: #080;">=</span><span style="color: #ff0000;">'Apple Inc. vs. S&amp;P 500 2009-2011'</span>,xlab<span style="color: #080;">=</span><span style="color: #ff0000;">'S&amp;P500 Returns'</span>, ylab<span style="color: #080;">=</span><span style="color: #ff0000;">'Apple Returns'</span>,boxplots<span style="color: #080;">=</span>FALSE<span style="color: #080;">&#41;</span>
<a href="http://astrostatistics.psu.edu/su07/R/html/stats/html/text.html"><span style="color: #0000FF; font-weight: bold;">text</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">5</span>,<span style="color: #080;">-</span><span style="color: #ff0000;">10</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/paste.html"><span style="color: #0000FF; font-weight: bold;">paste</span></a><span style="color: #080;">&#40;</span><span style="color: #ff0000;">'y = '</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/signif.html"><span style="color: #0000FF; font-weight: bold;">signif</span></a><span style="color: #080;">&#40;</span>AAPL.<span style="">alpha</span>,digits<span style="color: #080;">=</span><span style="color: #ff0000;">4</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">' + '</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/signif.html"><span style="color: #0000FF; font-weight: bold;">signif</span></a><span style="color: #080;">&#40;</span>AAPL.<span style="">beta</span>,digits<span style="color: #080;">=</span><span style="color: #ff0000;">5</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">'x <span style="color: #000099; font-weight: bold;">\n</span> R^2 = '</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/signif.html"><span style="color: #0000FF; font-weight: bold;">signif</span></a><span style="color: #080;">&#40;</span>AAPL.<span style="">rsquared</span>,digits<span style="color: #080;">=</span><span style="color: #ff0000;">6</span><span style="color: #080;">&#41;</span>,<span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>n='</span>,<a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/length.html"><span style="color: #0000FF; font-weight: bold;">length</span></a><span style="color: #080;">&#40;</span><a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/as.vector.html"><span style="color: #0000FF; font-weight: bold;">as.<span style="">vector</span></span></a><span style="color: #080;">&#40;</span>AAPL.<span style="">weekly</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>,sep<span style="color: #080;">=</span><span style="color: #ff0000;">''</span><span style="color: #080;">&#41;</span>,font<span style="color: #080;">=</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>The code is commented, but I will make some additional comments on specific sections to explain the process for those unsure.  I apologize for my unstandardized variable names as well!</p>
<p>First of all, I use the getQuotes() function, which has a few sources.  In this example, I use Yahoo data for equity data and FRED for information on 5yr Treasuries.  For reference, the ticker for retrieving the SP500 on Yahoo is "^GSPC", and the <a href="http://research.stlouisfed.org/fred2/" target="_blank">FRED</a> code for 5yr treasuries is "GS5".  Other symbols should be self explanatory.</p>
<p>Next is the issue of regression parameters.  To find alpha and beta, I use the capm functions of PerformanceAnalytics, but to find <span class='MathJax_Preview'><img src='http://www.perdomocore.com/wp-content/plugins/latex/cache/tex_e31b458b48dd58470b662e66b9742071.gif' style='vertical-align: middle; border: none; ' class='tex' alt="R^2" /></span><script type='math/tex'>R^2</script> I read it out of the the regression object using</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p25code16'); return false;">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2516"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p25code16"><pre class="rsplus" style="font-family:monospace;">AAPL.<span style="">reg</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">lm</span><span style="color: #080;">&#40;</span>AAPL.<span style="">weekly</span>~GSPC.<span style="">weekly</span><span style="color: #080;">&#41;</span>
AAPL.<span style="">rsquared</span> <span style="color: #080;">&lt;-</span> <a href="http://astrostatistics.psu.edu/su07/R/html/graphics/html/summary.html"><span style="color: #0000FF; font-weight: bold;">summary</span></a><span style="color: #080;">&#40;</span>AAPL.<span style="">reg</span><span style="color: #080;">&#41;</span>$r.<span style="">squared</span></pre></td></tr></table></div>

<p>It is possible to do this with beta and alpha, however, I did not do this because I did not originally did not start out to find <span class='MathJax_Preview'><img src='http://www.perdomocore.com/wp-content/plugins/latex/cache/tex_e31b458b48dd58470b662e66b9742071.gif' style='vertical-align: middle; border: none; ' class='tex' alt="R^2" /></span><script type='math/tex'>R^2</script>, and turned to PerformanceAnalytics out of convenience.</p>
<p>Finally, I graphed the results and regression line for the benefit of my teacher, the results of which can be seen here:<br />
<div id="attachment_39" class="wp-caption alignnone" style="width: 310px"><a href="http://www.perdomocore.com/wp-content/uploads/2012/02/sp500-vs-aapl.png" rel="lightbox"><img src="http://www.perdomocore.com/wp-content/uploads/2012/02/sp500-vs-aapl-300x233.png" alt="S&amp;P500 vs. Apple, 2009-2011" title="sp500-vs-aapl" width="300" height="233" class="size-medium wp-image-39" /></a><p class="wp-caption-text">S&#038;P500 vs. Apple, 2009-2011</p></div><br />
&nbsp;</p>
<div class="AWD_like_button "><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.perdomocore.com%2F2012%2Fusing-r-and-quantmod-to-find-cost-of-equity%2F&amp;send=false&amp;layout=standard&amp;width=&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=40" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:40px;" allowTransparency="true"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.perdomocore.com/2012/using-r-and-quantmod-to-find-cost-of-equity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

