<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/in-search-of-an-incredible-posterior/"></div></div><div><a data-url="http://www.r-bloggers.com/in-search-of-an-incredible-posterior/" data-counturl="http://www.r-bloggers.com/in-search-of-an-incredible-posterior/" data-count="horizontal" data-text="In search of an incredible posterior" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://pirategrunt.com/r/2016/06/23/IncrediblePosterior/"> PirateGrunt</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>What is credibility?</h2>
<p>For over one hundred years <sup><a target="_blank" href="http://pirategrunt.com/r/2016/06/23/IncrediblePosterior/#fn:1">1</a></sup> actuaries have been wresting with the idea of “credibility”. This is the process whereby one may make a quantitative assessment of the predictive power of sample data. Where necessary, the researcher augments the sample with some exogeneous information – usually more data – to arrive at a final conclusion. In its simplest form, the first moment of the quantity being studied (pure premium, for example) is calculated as the weighted average of two numbers: the sample mean and the alternate estimate. The weight is what we (that is, actuaries) mean when we say credibility and it varies between zero and one.</p>
<p>This alternate estimate is generally referred to as the “complement of credibility”. There are thousands of pages written about how the complement may be derived. Boor <sup><a target="_blank" href="http://pirategrunt.com/r/2016/06/23/IncrediblePosterior/#fn:2">2</a></sup> is a particularly good paper. Suffice it to say that it should reflect the experience under study. This presents a slight conundrum: the sample data should be distinct enough that it must be separated and yet it’s not stable enough to stand on its own. For practitioners in other fields, this sounds a lot like a hierarchical model. In this case, certain “house effects” have enough signal to warrant deviation from the global experience, but are noisy enough that we should reflect a bit of the total sample data. Gelman and Hill <sup><a target="_blank" href="http://pirategrunt.com/r/2016/06/23/IncrediblePosterior/#fn:3">3</a></sup> is a fantastic treatment of the subject.</p>
<p>We may also view the complement of credibility in a Bayesian context. Here, the prior distribution may be regarded as the “complement” which we combine with data to arrive at a final estimate. However, here’s something interesting. We have a great deal of latitude in selecting the prior. Last week, I toyed with the idea of a prior which would be so powerful it would overwhelm whatever data entered into the analysis. Effectively, this was the opposite of an uninformed prior. I wanted to explore the idea of a pricing algorithm which would – by design – have very low credibility. Phrased differently, I wanted a posterior that reflected almost zero credibility- an incredible posterior, if you will.</p>
<p>The scenario I investigated was one where there were five years of claims data of very low volume, say an expected value of five claims per year. Most actuaries would regard that as sample with fairly low credibility. Against that I used a prior with an expected value of 100 claims. Surely, the data didn’t stand a chance. To keep things simple, I was using a Poisson frequency model, with parameter determined by a gamma distribution. <sup><a target="_blank" href="http://pirategrunt.com/r/2016/06/23/IncrediblePosterior/#fn:4">4</a></sup></p>
<p>First things first, I translated my prior assumptions about the Poisson parameter into parameters for the gamma distribution.</p>
<h2>Parameters</h2>
<p>In the code below, I’ll use the variable names <code>mu</code> and <code>sigma</code> to refer to the expected value and standard deviation. The variable prefixes “prior”, “posterior” and “sample”, should make it clear what I’m referring to. In formulae, I’ll use  and  for prior and posterior expected value and standard deviation. The posterior will carry a prime superscript.</p>
<h4>gamma</h4>
<p>To start, we’ll derive some fairly basic relationships between the moments of our claims and the parameters of the gamma.</p>
<table>
<tbody>
<tr>
<td>(1) </td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>(2) </td>
</tr>
</tbody>
</table>
<p>This then allows us to define  in terms of  and .</p>
<table>
<tbody>
<tr>
<td>(3) </td>
</tr>
</tbody>
</table>
<p>Because the gamma and poisson are conjugate pairs, there are simple closed form solutions for the posterior  and  as follows:</p>
<table>
<tbody>
<tr>
<td>(4) </td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>(5) </td>
</tr>
</tbody>
</table>
<p>Finally, we’ll show some convenience functions so that  and  may be derived by  and . Starting from equations (1) and (2),</p>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<h4>Credibility</h4>
<p>Z is generally used as the variable which denotes the credibility of the sample. The credibility equation is easily expressed as:</p>
<table>
<tbody>
<tr>
<td>(6) </td>
</tr>
</tbody>
</table>
<p>By rearranging terms, we can get an implied credibility which depends on the prior and posterior means and our sample average.</p>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>(7) </td>
</tr>
</tbody>
</table>
<p>But we can re-express  based on equations (1), (4) and (5) above. Then,</p>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>(8) </td>
</tr>
</tbody>
</table>
<p>We may now use equationn (8) to alter equation (7) as follows:</p>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
<p>This finally gives us a formula for the credibility based solely onn the mean and variance of our prior gamma.</p>
<table>
<tbody>
<tr>
<td>(9) </td>
</tr>
</tbody>
</table>
<p>It’s hard to describe just how happy I was when I worked this out. All these steps are the distillation of several pages of caveman scribbles on several sheets of paper. I’m not a mathematician, folks. When I get to this sort of result, it’s more fun than huffing paint.</p>
<p>Once I’m over that elation, note something very significant about this formula. It says <em>almost nothing</em> about my sample. My sample data could come from Mars and it would have no impact on how much credibility I assign it apart from N. What matters most is the relationship between the mean and variance of my prior. Weirdly, I’m trying to work out the <em>credibility</em> of my credibility complement.</p>
<h2>But does it work?</h2>
<p>With equation (9), we can easily write a function to compute credibility for a number of prior scenarios. I’ll assume a sample of five observations and a prior \mu of 100. I’ll construct a data frame with varying sigmas. (I like to think in terms of CV, so that I can easily switch up the mean and continue to get reasonable variances.)</p>
<figure>
<pre><code data-lang="r"><span>PriorCredibility</span> <span>&lt;-</span> <span>function</span><span>(</span><span>N</span><span>,</span> <span>mu</span><span>,</span> <span>sigma</span><span>){</span>
  <span>Z</span> <span>&lt;-</span> <span>N</span> <span>/</span> <span>(</span><span>N</span> <span>+</span> <span>mu</span> <span>/</span> <span>sigma</span><span>^</span><span>2</span><span>)</span>
  <span>Z</span>
<span>}</span>

<span>N</span> <span>&lt;-</span> <span>5</span>

<span>priorMu</span> <span>&lt;-</span> <span>100</span>
<span>priorCV</span> <span>&lt;-</span> <span>seq</span><span>(</span><span>.005</span><span>,</span> <span>.5</span><span>,</span> <span>length.out</span> <span>=</span> <span>100</span><span>)</span>
<span>priorSigma</span> <span>&lt;-</span> <span>priorMu</span> <span>*</span> <span>priorCV</span>
<span>priorZ</span> <span>&lt;-</span> <span>PriorCredibility</span><span>(</span><span>N</span><span>,</span> <span>priorMu</span><span>,</span> <span>priorSigma</span><span>)</span>

<span>df</span> <span>&lt;-</span> <span>data.frame</span><span>(</span><span>priorZ</span><span>,</span> <span>priorSigma</span><span>,</span> <span>priorMu</span><span>)</span></code></pre>
</figure>
<p>And here’s how the credibility would look based on sigma. Notice how quickly the credibility of the sample exceeds 50%.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>ggplot2</span><span>)</span>
<span>pltPrior</span> <span>&lt;-</span> <span>ggplot</span><span>(</span><span>df</span><span>,</span> <span>aes</span><span>(</span><span>priorSigma</span><span>,</span> <span>priorZ</span><span>))</span> <span>+</span> <span>geom_line</span><span>()</span> <span>+</span> <span>ylim</span><span>(</span><span>c</span><span>(</span><span>0</span><span>,</span><span>1</span><span>))</span>
<span>pltPrior</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-2" src="http://i2.wp.com/pirategrunt.com/images/posts/2016/06/23/IncrediblePosterior.Rmd/unnamed-chunk-2-1.png?w=456" data-recalc-dims="1"></p>
<p>Now let’s apply this to some sample data. I’ve got some convenience functions to translate my sample data into posterior gamma parameters and implied credibilities. I’ll do this with two samples: one has a mean of 5 and the other has a mean of 200. Both are a good distance away from my prior assumption.</p>
<figure>
<pre><code data-lang="r"><span>PosteriorMu</span> <span>&lt;-</span> <span>function</span><span>(</span><span>sampleMean</span><span>,</span> <span>N</span><span>,</span> <span>priorMu</span><span>,</span> <span>priorSigma</span><span>){</span>
  <span>numerator</span> <span>&lt;-</span> <span>priorMu</span><span>^</span><span>2</span> <span>+</span> <span>sampleMean</span> <span>*</span> <span>N</span> <span>*</span> <span>priorSigma</span><span>^</span><span>2</span>
  <span>denominator</span> <span>&lt;-</span> <span>priorMu</span> <span>+</span> <span>N</span> <span>*</span> <span>priorSigma</span><span>^</span><span>2</span>
  <span>postMu</span> <span>&lt;-</span> <span>numerator</span> <span>/</span> <span>denominator</span>
  <span>postMu</span>
<span>}</span>

<span>SampleCredibility</span> <span>&lt;-</span> <span>function</span><span>(</span><span>priorMu</span><span>,</span> <span>posteriorMu</span><span>,</span> <span>sampleMu</span><span>){</span>
  <span>Z</span> <span>&lt;-</span> <span>(</span><span>posteriorMu</span> <span>-</span> <span>priorMu</span><span>)</span> <span>/</span> <span>(</span><span>sampleMu</span> <span>-</span> <span>priorMu</span><span>)</span>
  <span>Z</span>
<span>}</span>

<span>set.seed</span><span>(</span><span>1234</span><span>)</span>
<span>df</span><span>$</span><span>sampleMu1</span> <span>&lt;-</span> <span>5</span>
<span>df</span><span>$</span><span>sampleMu2</span> <span>&lt;-</span> <span>200</span>

<span>sampleX_1</span> <span>&lt;-</span> <span>rpois</span><span>(</span><span>N</span><span>,</span> <span>df</span><span>$</span><span>sampleMu1</span><span>)</span>
<span>df</span><span>$</span><span>posteriorMu_1</span> <span>&lt;-</span> <span>PosteriorMu</span><span>(</span><span>mean</span><span>(</span><span>sampleX_1</span><span>),</span> <span>N</span><span>,</span> <span>df</span><span>$</span><span>priorMu</span><span>,</span> <span>df</span><span>$</span><span>priorSigma</span><span>)</span>
<span>df</span><span>$</span><span>postZ_1</span> <span>&lt;-</span> <span>SampleCredibility</span><span>(</span><span>df</span><span>$</span><span>priorMu</span><span>,</span> <span>df</span><span>$</span><span>posteriorMu_1</span><span>,</span> <span>mean</span><span>(</span><span>sampleX_1</span><span>))</span>

<span>sampleX_2</span> <span>&lt;-</span> <span>rpois</span><span>(</span><span>N</span><span>,</span> <span>df</span><span>$</span><span>sampleMu2</span><span>)</span>
<span>df</span><span>$</span><span>posteriorMu_2</span> <span>&lt;-</span> <span>PosteriorMu</span><span>(</span><span>mean</span><span>(</span><span>sampleX_2</span><span>),</span> <span>N</span><span>,</span> <span>df</span><span>$</span><span>priorMu</span><span>,</span> <span>df</span><span>$</span><span>priorSigma</span><span>)</span>
<span>df</span><span>$</span><span>postZ_2</span> <span>&lt;-</span> <span>SampleCredibility</span><span>(</span><span>df</span><span>$</span><span>priorMu</span><span>,</span> <span>df</span><span>$</span><span>posteriorMu_2</span><span>,</span> <span>mean</span><span>(</span><span>sampleX_2</span><span>))</span></code></pre>
</figure>
<p>We’ll plot this against our prior sigmas. Look familiar?</p>
<figure>
<pre><code data-lang="r"><span>pltSample1</span> <span>&lt;-</span> <span>ggplot</span><span>(</span><span>df</span><span>,</span> <span>aes</span><span>(</span><span>priorSigma</span><span>,</span> <span>postZ_1</span><span>))</span> <span>+</span> <span>geom_line</span><span>()</span> <span>+</span> <span>ylim</span><span>(</span><span>c</span><span>(</span><span>0</span><span>,</span><span>1</span><span>))</span>
<span>pltSample1</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-4" src="http://i0.wp.com/pirategrunt.com/images/posts/2016/06/23/IncrediblePosterior.Rmd/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<p>Just to hammer the point home, we’ll overlay the credibility as calculated by equation (9).</p>
<figure>
<pre><code data-lang="r"><span>pltSample1</span> <span>&lt;-</span> <span>pltSample1</span> <span>+</span> <span>geom_line</span><span>(</span><span>aes</span><span>(</span><span>priorSigma</span><span>,</span> <span>priorZ</span><span>),</span> <span>color</span> <span>=</span> <span>&quot;red&quot;</span><span>)</span>
<span>pltSample1</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-5" src="http://i2.wp.com/pirategrunt.com/images/posts/2016/06/23/IncrediblePosterior.Rmd/unnamed-chunk-5-1.png?w=456" data-recalc-dims="1"></p>
<p>Yep, they’re the same. And what happens if I use a very different sample?</p>
<figure>
<pre><code data-lang="r"><span>pltSample2</span> <span>&lt;-</span> <span>ggplot</span><span>(</span><span>df</span><span>,</span> <span>aes</span><span>(</span><span>priorSigma</span><span>,</span> <span>postZ_2</span><span>))</span> <span>+</span> <span>geom_line</span><span>()</span> <span>+</span> <span>ylim</span><span>(</span><span>c</span><span>(</span><span>0</span><span>,</span><span>1</span><span>))</span>
<span>pltSample2</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-6" src="http://i2.wp.com/pirategrunt.com/images/posts/2016/06/23/IncrediblePosterior.Rmd/unnamed-chunk-6-1.png?w=456" data-recalc-dims="1"></p>
<p>And there we have it: if we want an incredible posterior, we have to have a pretty tight estimate around our prior mean. This is actually sort of good news. Within the insurance industry, non-actuaries have a vast store of experience and an innate sense of the loss characteristics of their business. However, they’re often accustomed to thinking in terms of the first moment of the distribution, “poperties like this ought to cost XX”. If you haven’t had a chance to hang out with facultative underwriters, treat yourself. They’ve seen it all, they love to talk and they love to pick up a check. When you’re talking, push questions like “How often would it be above Y? What’s the 90th percentile?” You’ll start to get a <strong>quantitative</strong> sense for the range around our prior mean. And, in a Bayesian sense, that’s <em>just about all</em> that matters.</p>
<p>Big tip of the hat to my pals Avi and Dave for their very helpful comments. Obviously, anything silly or wrong is all my fault.</p>
<figure>
<pre><code data-lang="text">## R version 3.3.0 (2016-05-03)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 14.04.4 LTS
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  base     
## 
## other attached packages:
## [1] ggplot2_2.1.0
## 
## loaded via a namespace (and not attached):
##  [1] labeling_0.3     colorspace_1.2-6 scales_0.4.0     plyr_1.8.4      
##  [5] magrittr_1.5     formatR_1.4      tools_3.3.0      gtable_0.2.0    
##  [9] Rcpp_0.12.5      lubridate_1.5.6  stringi_1.1.1    grid_3.3.0      
## [13] knitr_1.13       methods_3.3.0    stringr_1.0.0    munsell_0.4.3   
## [17] evaluate_0.9</code></pre>
</figure>
<div>
<ol>
<li>
<p>It’s true! The Casualty Actuarial Society in the US was founded in 1914 to- among other things- address the problem of just how much data was needed to estimate premium in workers compensation insurance. <a target="_blank" href="http://pirategrunt.com/r/2016/06/23/IncrediblePosterior/#fnref:1">↩</a></p>
</li>
<li>
<p><a target="_blank" href="https://www.casact.org/pubs/proceed/proceed96/96001.pdf">Boor, Joseph, “The Complement of Credibility,” PCAS LXXXIII, Part 1, 1996, 1-32</a> <a target="_blank" href="http://pirategrunt.com/r/2016/06/23/IncrediblePosterior/#fnref:2">↩</a></p>
</li>
<li>
<p><a target="_blank" href="http://www.stat.columbia.edu/~gelman/arm/">Gelman &amp; Hill, “Data Analysis Using Regression and Multilevel/Hierarchical Models,” December 2006</a> <a target="_blank" href="http://pirategrunt.com/r/2016/06/23/IncrediblePosterior/#fnref:3">↩</a></p>
</li>
<li>
<p>In a realistic setting, we’d translate the Poisson parameter into a frequency that would be applied to exposure, like number of employees, number of vehicles, etc. To keep things simple, I presume that my prior and sample have identical exposures, so I can think in terms of claims rather than claim frequencies. <a target="_blank" href="http://pirategrunt.com/r/2016/06/23/IncrediblePosterior/#fnref:4">↩</a></p>
</li>
</ol>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/in-search-of-an-incredible-posterior/"></div></div><div><a data-url="http://www.r-bloggers.com/in-search-of-an-incredible-posterior/" data-counturl="http://www.r-bloggers.com/in-search-of-an-incredible-posterior/" data-count="vertical" data-text="In search of an incredible posterior" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://pirategrunt.com/r/2016/06/23/IncrediblePosterior/"> PirateGrunt</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/7BD_Q-KtsPY"></a></div>