<?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 &#187; mark</title>
	<atom:link href="http://www.perdomocore.com/author/mark/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.perdomocore.com</link>
	<description>Finance, Coding, Economics</description>
	<lastBuildDate>Wed, 08 Feb 2012 18:18:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<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="p733"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p73code3"><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('p73code4'); return false;">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p734"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p73code4"><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="p257"><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="p25code7"><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('p25code8'); return false;">View Code</a> RSPLUS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p258"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p25code8"><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>

