<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Steve Martin</title>
<link>https://marberts.github.io/blog/</link>
<atom:link href="https://marberts.github.io/blog/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.10.18</generator>
<lastBuildDate>Fri, 07 Aug 2026 04:00:00 GMT</lastBuildDate>
<item>
  <title>Decomposing indexes with Euler weights</title>
  <dc:creator>Steve Martin</dc:creator>
  <link>https://marberts.github.io/blog/posts/2026/euler/</link>
  <description><![CDATA[ 





<p>I’ve always liked <span class="citation" data-cites="hallerbach2005">Hallerbach (2005)</span>’s decomposition of the Fisher index. It’s such a simple and straightforward way to decompose the Fisher index that generalizes to a wide range of index-number formulas. The latter has, to my knowledge, not been explored before, and Hallerbach’s approach of using Euler weights to decompose an index number is instead associated with the Fisher index. What I want to do here is explore this decomposition in more detail and show a couple properties that aren’t obvious when decomposing the Fisher index alone.</p>
<p>Let’s start with the family of generalized-mean indexes. For an index based on a generalized mean of order <img src="https://latex.codecogs.com/png.latex?%5Crho">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathfrak%7BM%7D_%7B%5Crho%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D)%20=%0A%5Cbegin%7Bcases%7D%0A%5Cleft(%5Csum_%7Bi%20=%201%7D%5E%7Bn%7D%20w_%7Bi%7D%20r_i%5E%7B%5Crho%7D%5Cright)%5E%7B1%20/%20%5Crho%7D%20&amp;%20%5Ctext%7Bif%20%7D%20%5Crho%20%5Cneq%200%20%5C%5C%0A%5Cprod_%7Bi%20=%201%7D%5E%7Bn%7D%20r_%7Bi%7D%5E%7Bw_%7Bi%7D%7D%20&amp;%20%5Ctext%7Bif%20%7D%20%5Crho%20=%200,%0A%5Cend%7Bcases%7D%0A"> the derivative with respect to <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D"> is <span id="eq-euler1"><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Cpartial%20%5Cmathfrak%7BM%7D_%7B%5Crho%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D)%7D%7B%5Cpartial%20r_%7Bi%7D%7D%20=%20w_%7Bi%7D%20%5Cleft(%5Cfrac%7Br_%7Bi%7D%7D%7B%5Cmathfrak%7BM%7D_%7B%5Crho%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D)%7D%5Cright)%5E%7B%5Crho%20-%201%7D.%0A%5Ctag%7B1%7D"></span> The generalized mean is a homogeneous function, so multiplying each of these terms by <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D"> and summing gives the value of <img src="https://latex.codecogs.com/png.latex?%5Cmathfrak%7BM%7D_%7B%5Crho%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D)"> by <a href="https://en.wikipedia.org/wiki/Homogeneous_function#Euler's_theorem">Euler’s theorem</a>.</p>
<p>Equation&nbsp;1 is similar in form to the generalized additive decomposition by <span class="citation" data-cites="martin2021">Martin (2021)</span> <span id="eq-logmean1"><img src="https://latex.codecogs.com/png.latex?%0Aw_%7Bi%7D%20%5Cmathfrak%7BL%7D_%7B%5Crho%7D(r_i,%20%5Cmathfrak%7BM%7D_%7B%5Crho%7D%20(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D))%5E%7B%5Crho%20-%201%7D%20%5CBigg/%20%5Csum_%7Bj=1%7D%5E%7Bn%7D%20w_%7Bj%7D%20%5Cmathfrak%7BL%7D_%7B%5Crho%7D(r_j,%20%5Cmathfrak%7BM%7D_%7B%5Crho%7D%20(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D))%5E%7B%5Crho%20-%201%7D,%0A%5Ctag%7B2%7D"></span> where <img src="https://latex.codecogs.com/png.latex?%5Cmathfrak%7BL%7D"> is the generalized logarithmic mean <span class="citation" data-cites="balk2008">(Balk 2008, chap. 4)</span>. As with Equation&nbsp;1, multiplying each of these terms by <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D"> and summing returns the values of the generalized mean. In both cases, these decompositions work by shifting weight to the larger <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D">s when <img src="https://latex.codecogs.com/png.latex?%5Crho%20%3E%201"> and shifting weight to the smaller <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D">s when <img src="https://latex.codecogs.com/png.latex?%5Crho%20%3C%201">. The break point is particularly clean for Equation&nbsp;1, as an <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D"> above the mean gets more (less) weight whenever <img src="https://latex.codecogs.com/png.latex?%5Crho%20%3E%201"> (<img src="https://latex.codecogs.com/png.latex?%5Crho%20%3C%201">), whereas this need not be true for Equation&nbsp;2.</p>
<p>Except when <img src="https://latex.codecogs.com/png.latex?%5Crho%20=%201"> (additive decomposition for an arithmetic mean) or all <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D"> are the same, Equation&nbsp;1 and Equation&nbsp;2 give different decompositions. In particular, the terms in Equation&nbsp;2 always sum to 1, whereas Equation&nbsp;1 is greater than 1 whenever <img src="https://latex.codecogs.com/png.latex?%5Crho%20%3C%201"> and less than 1 whenever <img src="https://latex.codecogs.com/png.latex?%5Crho%20%3E%201">. To see this, note that the sum of Equation&nbsp;1 is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Csum_%7Bi=1%7D%5E%7Bn%7D%20w_%7Bi%7Dr_%7Bi%7D%5E%7B%5Crho%20-%201%7D%7D%7B%5Cmathfrak%7BM%7D_%7B%5Crho%7D%20(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D)%5E%7B%5Crho%20-%201%7D%7D.%0A"> If <img src="https://latex.codecogs.com/png.latex?%5Crho%20%3E%201"> then this must be less than 1 because the generalized mean in an increasing function of <img src="https://latex.codecogs.com/png.latex?%5Crho">. If <img src="https://latex.codecogs.com/png.latex?%5Crho%20%3C%201"> then the inequality is reversed. This is in contrast to decomposition by <span class="citation" data-cites="hallerbach2005">Hallerbach (2005)</span> where the Euler weights for Fisher index are always greater than 1 (unless the Laspeyres and Paasche indexes are equal, in which case the weights sum to 1).</p>
<p>A stark example of the difference between the two decompositions is the additive decomposition of the geometric mean: each <img src="https://latex.codecogs.com/png.latex?i"> in Equation&nbsp;1 gets the same value when multiplied by <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D">, whereas Equation&nbsp;2 is increasing with respect to <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D">.<sup>1</sup> This seems like a counter-intuitive property for the decomposition of an index based on the geometric mean. Equation&nbsp;2 transfers weight from the larger <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D">s to the smaller ones—so that the arithmetic mean equals the geometric one—to determine the contribution of each <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D"> while preserving the overall ranking. Instead, Equation&nbsp;1 divides the value of the geometric mean into equal parts so that the sum equals the geometric mean, independent of the value of each <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D">.</p>
<p>Equation&nbsp;1 can be extended to the family indexes based on nested generalized means. For a pair of generalized means <img src="https://latex.codecogs.com/png.latex?%5Cmathfrak%7BN%7D_%7B%5Crho_%7B1%7D%5Crho_%7B2%7D%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D_%7B1%7D,%20%5Cmathbf%7Bw%7D_%7B2%7D)=%5Cleft(%5Cmathfrak%7BM%7D_%7B%5Crho_1%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D_1),%20%5Cmathfrak%7BM%7D_%7B%5Crho_2%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D_2)%5Cright)"> with weights <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B%5Comega%7D=(%5Comega_1,%20%5Comega_2)">, an index based on nested generalized means is written as <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathfrak%7BM%7D_%7B%5Crho%7D%5Cleft(%5Cmathfrak%7BN%7D_%7B%5Crho_%7B1%7D%5Crho_%7B2%7D%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D_%7B1%7D,%20%5Cmathbf%7Bw%7D_%7B2%7D),%20%5Cmathbf%7B%5Comega%7D%5Cright).%0A"> The Euler weights for this index are then</p>
<p><span id="eq-euler2"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign*%7D%0A%5Cfrac%7B%5Cpartial%20%5Cmathfrak%7BM%7D_%7B%5Crho%7D%5Cleft(%5Cmathfrak%7BN%7D_%7B%5Crho_%7B1%7D%5Crho_%7B2%7D%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D_%7B1%7D,%20%5Cmathbf%7Bw%7D_%7B2%7D),%20%5Cmathbf%7B%5Comega%7D%5Cright)%7D%7B%5Cpartial%20r_%7Bi%7D%7D%20&amp;=%20%5Comega_%7B1%7D%20%5Cleft(%5Cfrac%7B%5Cmathfrak%7BM%7D_%7B%5Crho_%7B1%7D%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw_%7B1%7D%7D)%7D%7B%5Cmathfrak%7BM%7D_%7B%5Crho%7D%5Cleft(%5Cmathfrak%7BN%7D_%7B%5Crho_%7B1%7D%5Crho_%7B2%7D%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D_%7B1%7D,%20%5Cmathbf%7Bw%7D_%7B2%7D),%20%5Cmathbf%7B%5Comega%7D%5Cright)%7D%5Cright)%5E%7B%5Crho%20-%201%7D%20w_%7Bi1%7D%20%5Cleft(%5Cfrac%7Br_%7Bi%7D%7D%7B%5Cmathfrak%7BM%7D_%7B%5Crho_%7B1%7D%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw_%7B1%7D%7D)%7D%5Cright)%5E%7B%5Crho_%7B1%7D%20-%201%7D%5C%5C%0A&amp;+%20%5Comega_%7B2%7D%20%5Cleft(%5Cfrac%7B%5Cmathfrak%7BM%7D_%7B%5Crho_%7B2%7D%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw_%7B2%7D%7D)%7D%7B%5Cmathfrak%7BM%7D_%7B%5Crho%7D%5Cleft(%5Cmathfrak%7BN%7D_%7B%5Crho_%7B1%7D%5Crho_%7B2%7D%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw%7D_%7B1%7D,%20%5Cmathbf%7Bw%7D_%7B2%7D),%20%5Cmathbf%7B%5Comega%7D%5Cright)%7D%5Cright)%5E%7B%5Crho%20-%201%7D%20w_%7Bi2%7D%20%5Cleft(%5Cfrac%7Br_%7Bi%7D%7D%7B%5Cmathfrak%7BM%7D_%7B%5Crho_%7B2%7D%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw_%7B2%7D%7D)%7D%5Cright)%5E%7B%5Crho_%7B2%7D%20-%201%7D.%0A%5Cend%7Balign*%7D%0A%5Ctag%7B3%7D"></span></p>
<p>For the Fisher index (<img src="https://latex.codecogs.com/png.latex?%5Crho%20=%200">, <img src="https://latex.codecogs.com/png.latex?%5Crho_%7B1%7D%20=%201">, <img src="https://latex.codecogs.com/png.latex?%5Crho_%7B2%7D%20=%20-1">, <img src="https://latex.codecogs.com/png.latex?%5Comega_%7B1%7D%20=%20%5Comega_%7B2%7D%20=%201%20/%202">), Equation&nbsp;3 simplifies to</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B1%7D%7B2%7D%5Csqrt%7B%5Cfrac%7B%5Cmathfrak%7BM%7D_%7B-1%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw_%7B2%7D%7D)%7D%7B%5Cmathfrak%7BM%7D_%7B1%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw_%7B1%7D)%7D%7D%7D%20w_%7Bi1%7D%20+%20%5Cfrac%7B1%7D%7B2%7D%5Csqrt%7B%5Cfrac%7B%5Cmathfrak%7BM%7D_%7B1%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw_%7B1%7D%7D)%7D%7B%5Cmathfrak%7BM%7D_%7B-1%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw_%7B2%7D)%7D%7D%7D%20w_%7Bi2%7D%20%5Cleft(%5Cfrac%7Br_%7Bi%7D%7D%7B%5Cmathfrak%7BM%7D_%7B-1%7D(%5Cmathbf%7Br%7D,%20%5Cmathbf%7Bw_%7B2%7D%7D)%7D%5Cright)%5E%7B-2%7D.%0A"></p>
<p>Note that the second term is not the same as in Equation 13 by <span class="citation" data-cites="hallerbach2005">Hallerbach (2005)</span>. Applying Equation&nbsp;2 to transmute the weights <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bw%7D_%7B2%7D"> to represent the harmonic mean as an arithmetic mean (i.e., hybrid weights for the Paasche index) yields the decomposition due to <span class="citation" data-cites="hallerbach2005">Hallerbach (2005)</span>. But this alternate form is interesting because it gives larger weights that are strictly greater than 1 even when the Laspeyres and Paasche indexes are the same (unless all <img src="https://latex.codecogs.com/png.latex?r_%7Bi%7D"> are the same). This suggests that Equation&nbsp;1 can be combined with Equation&nbsp;2 to produce even more decompositions based on Euler weights.</p>





<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-balk2008" class="csl-entry">
Balk, Bert M. 2008. <em>Price and Quantity Index Numbers: Models for Measuring Aggregate Change and Difference</em>. Cambridge University Press. <a href="https://doi.org/10.1017/CBO9780511720758">https://doi.org/10.1017/CBO9780511720758</a>.
</div>
<div id="ref-hallerbach2005" class="csl-entry">
Hallerbach, W. G. 2005. <span>“An Alternative Decomposition of the <span>F</span>isher Index.”</span> <em>Economics Letters</em> 86 (2): 147–52. <a href="https://doi.org/10.1016/j.econlet.2004.07.008">https://doi.org/10.1016/j.econlet.2004.07.008</a>.
</div>
<div id="ref-martin2021" class="csl-entry">
Martin, S. 2021. <em>A Note on Generalized Decompositions for Price Indexes</em>. Prices Analytical Series. Statistics Canada. <a href="https://www150.statcan.gc.ca/n1/pub/62f0014m/62f0014m2021012-eng.htm">https://www150.statcan.gc.ca/n1/pub/62f0014m/62f0014m2021012-eng.htm</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>To see this, note that <img src="https://latex.codecogs.com/png.latex?%5Cpartial%20(x%20/%20%5Cmathfrak%7BL%7D(x,%20y))/%20%5Cpartial%20x%20%3E%200"> if <img src="https://latex.codecogs.com/png.latex?x%20-%20y%20%3E%20y%20%5Clog(x%20/%20y)">. This is true whenever <img src="https://latex.codecogs.com/png.latex?x%20%5Cneq%20y"> because <img src="https://latex.codecogs.com/png.latex?%5Cmin(x,%20y)%20%3C%20%5Cmathfrak%7BL%7D(x%20,%20y)%20%3C%20%5Cmax(x,%20y)">; otherwise, <img src="https://latex.codecogs.com/png.latex?%5Cpartial%20(x%20/%20%5Cmathfrak%7BL%7D(x,%20y))/%20%5Cpartial%20x%20=%200">.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{martin2026,
  author = {Martin, Steve},
  title = {Decomposing Indexes with {Euler} Weights},
  date = {2026-08-07},
  url = {https://marberts.github.io/blog/posts/2026/euler/},
  doi = {10.59350/jwyjp-wqm17},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-martin2026" class="csl-entry quarto-appendix-citeas">
Martin, Steve. 2026. <span>“Decomposing Indexes with Euler
Weights.”</span> August 7. <a href="https://doi.org/10.59350/jwyjp-wqm17">https://doi.org/10.59350/jwyjp-wqm17</a>.
</div></div></section></div> ]]></description>
  <category>Index numbers</category>
  <guid>https://marberts.github.io/blog/posts/2026/euler/</guid>
  <pubDate>Fri, 07 Aug 2026 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Fun with anonymous functions in R</title>
  <dc:creator>Steve Martin</dc:creator>
  <link>https://marberts.github.io/blog/posts/2026/metaprogramming/</link>
  <description><![CDATA[ 





<p>Julia has an elegant syntax for making anonymous functions: <code>(x, y) -&gt; x^2 + x^2</code>. R’s <code>\(x, y) x^2 + y^2</code> syntax isn’t bad either, but I wanted to try and make something similar to Julia’s syntax as an excuse to play around with R’s metaprogramming facilities. It’s quite possible that there’s an R package out there that already does it—and I did look, briefly, without finding anything—but this is more of an excuse to do some metaprogramming than a serious need for a different way of writing out anonymous functions.</p>
<p>Let’s start by making a new, minimal infix operator.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">%-&gt;%</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(lhs, rhs) {</span>
<span id="cb1-2">  res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {}</span>
<span id="cb1-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">formals</span>(res) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.pairlist</span>(lhs)</span>
<span id="cb1-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">body</span>(res) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substitute</span>(rhs)</span>
<span id="cb1-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">environment</span>(res) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parent.frame</span>()</span>
<span id="cb1-6">  res</span>
<span id="cb1-7">}</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Map</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">alist</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%-&gt;%</span> {x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>}, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
[1] 2

[[2]]
[1] 5

[[3]]
[1] 10</code></pre>
</div>
</div>
<p>Not bad. The curly braces make it look a bit more like Rust’s syntax. But having <code>alist()</code> on the left-hand side seems ugly. What we can do instead is have something like <code>c(x, y = 1) %-&gt;% {x^2 + y^2}</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">%-&gt;%</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(lhs, rhs) {</span>
<span id="cb3-2">  res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>() {}</span>
<span id="cb3-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract the lhs args and set names.</span></span>
<span id="cb3-4">  args <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substitute</span>(lhs))[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-5">  unnamed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.null</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(args))) {</span>
<span id="cb3-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(args)</span>
<span id="cb3-7">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb3-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nzchar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(args)))</span>
<span id="cb3-9">  }</span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(args)[unnamed] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(</span>
<span id="cb3-11">    args[unnamed],</span>
<span id="cb3-12">    \(x) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.symbol</span>(x)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(x) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"invalid lhs"</span>)</span>
<span id="cb3-13">  )</span>
<span id="cb3-14">  args[unnamed] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quote</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expr =</span>))</span>
<span id="cb3-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same as before.</span></span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">formals</span>(res) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.pairlist</span>(args)</span>
<span id="cb3-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">body</span>(res) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substitute</span>(rhs)</span>
<span id="cb3-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">environment</span>(res) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parent.frame</span>()</span>
<span id="cb3-19">  res</span>
<span id="cb3-20">}</span>
<span id="cb3-21"></span>
<span id="cb3-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Map</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%-&gt;%</span> {x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>}, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
[1] 2

[[2]]
[1] 5

[[3]]
[1] 10</code></pre>
</div>
</div>
<p>That seems about as close as we’re going to get. Although this isn’t particularly useful, it’s neat to see how flexible R can be.</p>



<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{martin2026,
  author = {Martin, Steve},
  title = {Fun with Anonymous Functions in {R}},
  date = {2026-06-22},
  url = {https://marberts.github.io/blog/posts/2026/metaprogramming/},
  doi = {10.59350/swkwd-0zc10},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-martin2026" class="csl-entry quarto-appendix-citeas">
Martin, Steve. 2026. <span>“Fun with Anonymous Functions in R.”</span>
June 22. <a href="https://doi.org/10.59350/swkwd-0zc10">https://doi.org/10.59350/swkwd-0zc10</a>.
</div></div></section></div> ]]></description>
  <category>Metaprogramming</category>
  <category>R</category>
  <guid>https://marberts.github.io/blog/posts/2026/metaprogramming/</guid>
  <pubDate>Mon, 22 Jun 2026 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Consistency in aggregation of the Fisher index</title>
  <dc:creator>Steve Martin</dc:creator>
  <link>https://marberts.github.io/blog/posts/2026/consistency/</link>
  <description><![CDATA[ 





<p>I was recently re-reading <span class="citation" data-cites="vonauer2021">Auer and Wengenroth (2021)</span>’s paper outlining how superlative price indexes can be viewed as consistent in aggregation. This is a surprising result because the Fisher index, among others superlative indexes, is famously <em>not</em> consistent in aggregation. Here’s an example.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make some data.</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">74743</span>)</span>
<span id="cb1-3"></span>
<span id="cb1-4">prices <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p0 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb1-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb1-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q0 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rlnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb1-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rlnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb1-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb1-10">)</span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Two-step calculation.</span></span>
<span id="cb1-13">prices <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-14">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(group) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-15">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb1-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fisher =</span> gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fisher_mean</span>(p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p0, p0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q0, p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q1),</span>
<span id="cb1-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v0 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(p0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q0),</span>
<span id="cb1-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q1)</span>
<span id="cb1-19">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-20">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">two_step =</span> gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fisher_mean</span>(fisher, v0, v1)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-21">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(two_step)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.8643786</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># One-step calculation.</span></span>
<span id="cb3-2">prices <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-3">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb3-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">one_step =</span> gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fisher_mean</span>(p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p0, p0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q0, p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q1)</span>
<span id="cb3-5">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-6">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(one_step)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.8567247</code></pre>
</div>
</div>
<p>Unlike the Laspeyres and Paasche indexes that make up the Fisher index, and are generally the only two that are consistent in aggregation, the overall Fisher index cannot be represented as a Fisher index of Fisher indexes for separate groups of products. In many cases the Fisher index can be <em>approximately</em> consistent in aggregation <span class="citation" data-cites="cpitheory">(IMF et al. 2025, 27–28)</span>, but it’s certainly not exactly so.</p>
<p>So how do <span class="citation" data-cites="vonauer2021">Auer and Wengenroth (2021)</span> show that the Fisher index is consistent in aggregation? I’d read the paper before and got the basic idea, but I find this to be a sufficiently interesting result that I wanted to work through it. The trick is to represent the Fisher index in a way so that the aggregation rule is the same no matter how products are partitioned. What they do is write the Fisher index for <img src="https://latex.codecogs.com/png.latex?n"> products as a function of four variables</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A(z_1,%20z_2,%20z_3,%20z_4)%20%5Cmapsto%20%5Csqrt%7B%5Cfrac%7B%5Csum_%7Bi=1%7D%5E%7Bn%7Dz_%7Bi1%7D%7D%7B%5Csum_%7Bi=1%7D%5E%7Bn%7Dz_%7Bi2%7D%7D%5Cfrac%7B%20%5Csum_%7Bi=1%7D%5E%7Bn%7Dz_%7Bi3%7D%7D%7B%5Csum_%7Bi=1%7D%5E%7Bn%7Dz_%7Bi4%7D%7D%7D,%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?(z_1,%20z_2,%20z_3,%20z_4)%20=%20(p_1q_0,%20p_0q_0,%20p_1q_1,%20p_0q_1)">. This functions is clearly consistent in aggregation <em>with respect to <img src="https://latex.codecogs.com/png.latex?(z_1,%20z_2,%20z_3,%20z_4)"></em>: for each <img src="https://latex.codecogs.com/png.latex?z">, the sum is the same regardless of how each element of the vector is partitioned.</p>
<p>Here’s an example.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">fisher_agg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(z1, z2, z3, z4) {</span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(z1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(z2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(z3) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(z4))</span>
<span id="cb5-3">}</span>
<span id="cb5-4"></span>
<span id="cb5-5">prices <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prices <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-6">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(group) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-7">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb5-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z1 =</span> p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q0,</span>
<span id="cb5-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z2 =</span> p0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q0,</span>
<span id="cb5-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z3 =</span> p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q1,</span>
<span id="cb5-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z4 =</span> p0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q1</span>
<span id="cb5-12">  )</span>
<span id="cb5-13"></span>
<span id="cb5-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Group-wise indexes are the same as the regular calculation.</span></span>
<span id="cb5-15">prices <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-16">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb5-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">consistent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fisher_agg</span>(z1, z2, z3, z4),</span>
<span id="cb5-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">regular =</span> gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fisher_index</span>(p1, p0, q1, q0)</span>
<span id="cb5-19">  )</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 3
  group consistent regular
  &lt;fct&gt;      &lt;dbl&gt;   &lt;dbl&gt;
1 1          1.47    1.47 
2 2          0.624   0.624</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Overall index is the same as the regular calculation.</span></span>
<span id="cb7-2">prices <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-3">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-4">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb7-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">consistent =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fisher_agg</span>(z1, z2, z3, z4),</span>
<span id="cb7-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">regular =</span> gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fisher_index</span>(p1, p0, q1, q0)</span>
<span id="cb7-7">  )</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 2
  consistent regular
       &lt;dbl&gt;   &lt;dbl&gt;
1      0.857   0.857</code></pre>
</div>
</div>
<p>The alternative representation of the Fisher index is the same as the ordinary calculation for both groups <em>and</em> the overall Fisher index. But this aggregation rule is consistent in aggregation in the sense that pooling the products across separate partitions gives the correct result without needing to change the rule.</p>
<p>Overall I think this is a neat result, especially as the authors show how this can be used to represent many indexes in a way that is consistent in aggregation. But I usually think about consistency in aggregation as a way to preserve a value decomposition across a hierarchical classification of economic activity, rather than only stability of an aggregation rule. This is why the Laspeyres and Paasche indexes are the two key indexes for this purpose, as they are the only pair of price/quantity indexes that can be used to deflate a system of economic accounts.</p>




<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-vonauer2021" class="csl-entry">
Auer, Ludwig von, and Jochen Wengenroth. 2021. <span>“Consistent Aggregation with Superlative and Other Price Indices.”</span> <em>Journal of the Royal Statistical Society Series A: Statistics in Society</em> 184 (2): 589–615. <a href="https://doi.org/10.1111/rssa.12633">https://doi.org/10.1111/rssa.12633</a>.
</div>
<div id="ref-cpitheory" class="csl-entry">
IMF, ILO, Eurostat, UNECE, OECD, and World Bank. 2025. <em>Consumer Price Index Manual: Theory</em>. International Monetary Fund. <a href="https://doi.org/10.5089/9781513559605.069">https://doi.org/10.5089/9781513559605.069</a>.
</div>
</div></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{martin2026,
  author = {Martin, Steve},
  title = {Consistency in Aggregation of the {Fisher} Index},
  date = {2026-06-15},
  url = {https://marberts.github.io/blog/posts/2026/consistency/},
  doi = {10.59350/f0qs7-tfe09},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-martin2026" class="csl-entry quarto-appendix-citeas">
Martin, Steve. 2026. <span>“Consistency in Aggregation of the Fisher
Index.”</span> June 15. <a href="https://doi.org/10.59350/f0qs7-tfe09">https://doi.org/10.59350/f0qs7-tfe09</a>.
</div></div></section></div> ]]></description>
  <category>Index numbers</category>
  <category>R</category>
  <guid>https://marberts.github.io/blog/posts/2026/consistency/</guid>
  <pubDate>Mon, 15 Jun 2026 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Reproduction-resistant file formats</title>
  <dc:creator>Steve Martin</dc:creator>
  <link>https://marberts.github.io/blog/posts/2026/formats/</link>
  <description><![CDATA[ 





<p>I’ve been spending more time structuring projects as reproducible analytical pipelines and using tools like <code>dvc</code> <span class="citation" data-cites="dvc">(<span class="nocase">Kuprieiev et al.</span> 2026)</span> and <code>{targets}</code> <span class="citation" data-cites="targets">(Landau 2021)</span> to make analytical workflows reproducible. The idea is to represent the interaction between code and data as a directed acyclic graph (DAG) and keep track of the hashes of the data and code nodes in this graph. In this way, it is possible to know exactly the inputs, steps, and outputs of an analytical workflow, and to have a relatively automated way to reconstruct the graph that represents the pipeline.</p>
<div class="cell" data-layout-align="default">
<div class="cell-output-display">
<div>
<p></p><figure class="figure"><p></p>
<div>
<pre class="mermaid mermaid-js">graph TD
    df1["`data1.parquet
    _89d903b_`"]
    df2["`data2.parquet
    _ff9cf2d_`"]
    df3["`data3.parquet
    _79369f7_`"]
    
    df1 --&gt; step1["`step1.R
    _2e957eb_`"]
    df2 --&gt; step1
    df3 --&gt; step2["`step2.R
    _9b782e4_`"]
    step1 --&gt; df4["`data4.parquet
    _7321608_`"]
    step2 --&gt; df5["`data5.parquet
    _9bb597c_`"]
    df4 --&gt; step3["`step3.R
    _cd8e177_`"]
    df5 --&gt; step3
    step3 --&gt; res["`result.parquet
    _b4a8841_`"]
</pre>
</div>
<p></p></figure><p></p>
</div>
</div>
</div>
<p>Implicit in this formulation of a pipeline is the notion that the hash of an output for a step only depends on the hash of the code for that step and the hashes of its inputs. This implies that that knowing the hashes of all the data and code nodes in the DAG for a pipeline is sufficient to establish that you’ve successfully reproduced it.</p>
<p>In some cases using the hash is too sensitive a measure of change, at least for the purpose of reproducibility. For example, using a formatter like <code>air</code> or <code>black</code> can change the hash of a script even though the AST for that script, and therefore the output from that stage for the pipeline, remains the same. A tool like <code>dvc</code> would erroneously mark all stages of the pipeline that depend on this script as out of date, even though it will produce all the same outputs. It would be better to have a way to hash the AST of the script, but taking the hash of the file is simpler and will always detect a change in the AST of the script, even if it may falsely detect a change when the AST doesn’t.</p>
<p>What would, however, be problematic is if the same inputs and code produced an output with a variable hash due to the way the data are stored. I don’t mean doing something inherently non-reproducible like working with random numbers and not using a fixed seed, but rather data formats that resist hashing as a way to identify change. This would mean that knowing the hashes of all the inputs, code, and outputs would not be sufficient to reproduce the pipeline, at least from the point of view of a tool like <code>dvc</code> or <code>{targets}</code>, because executing the pipeline again can give a different collection of hashes—so much for reproducibility.</p>
<p>Unlike the code-formatter example, this is not a theoretical concern and I recently spent more time than I’d like to admit diagnosing why I couldn’t reproduce two separate pipelines despite the inputs and code remaining unchanged.</p>
<p>The first issue was with a pipeline that stored data as an excel spreadsheet. This shouldn’t be surprising, but I didn’t realize that writing a data frame as an xlsx, and then writing the same data frame as an xlsx again, produces two files with different hashes.<sup>1</sup></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">openxlsx2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_xlsx</span>(mtcars, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/mtcars1.xlsx"</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-3">openxlsx2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_xlsx</span>(mtcars, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/mtcars2.xlsx"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">md5sum</span> data/mtcars<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.xlsx</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>827d4ca8f14f39fa7862df53032890b5  data/mtcars1.xlsx
e3094e11d6ed1abcf9e767eeca72a0c1  data/mtcars2.xlsx</code></pre>
</div>
</div>
<p>I expect this is a well-known property of excel files, but it was new to me so I decided to explore why. Let’s start by unpacking the archives of both excel files containing the mtcars data frame.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unzip</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-qo</span> data/mtcars1.xlsx <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span> data/mtcars1</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unzip</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-qo</span> data/mtcars2.xlsx <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span> data/mtcars2</span>
<span id="cb4-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">tree</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-n</span> data/mtcars1</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>data/mtcars1
├── [Content_Types].xml
├── docProps
│&nbsp;&nbsp; ├── app.xml
│&nbsp;&nbsp; └── core.xml
├── _rels
└── xl
    ├── _rels
    │&nbsp;&nbsp; └── workbook.xml.rels
    ├── styles.xml
    ├── theme
    │&nbsp;&nbsp; └── theme1.xml
    ├── workbook.xml
    └── worksheets
        └── sheet1.xml

7 directories, 8 files</code></pre>
</div>
</div>
<p>And then finding which files have different hashes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">md5deep</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-r</span> data/mtcars1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> data/md5s</span>
<span id="cb6-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">md5deep</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-rl</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-x</span> data/md5s data/mtcars2<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>data/mtcars2/docProps/core.xml</code></pre>
</div>
</div>
<p>Except for <code>core.xml</code>, these two archives contain the same files. The reason <code>core.xml</code> differs is because it contains a timestamp. This makes excel a difficult format to work with for reproducibility because it depends on hidden global state (i.e., time) that is always mutating. Doing the same exercise with csv or parquet yields a stable hash that makes it possible to establish that the data frame we’re writing to disk is unchanged.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write.csv</span>(mtcars, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/mtcars1.csv"</span>)</span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write.csv</span>(mtcars, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/mtcars2.csv"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">md5sum</span> data/mtcars<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.csv</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>6463474bfe6973a81dc7cbc4a71e8dd1  data/mtcars1.csv
6463474bfe6973a81dc7cbc4a71e8dd1  data/mtcars2.csv</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">arrow<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_parquet</span>(mtcars, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/mtcars1.parquet"</span>)</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb11-3">arrow<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_parquet</span>(mtcars, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/mtcars2.parquet"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">md5sum</span> data/mtcars<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>.parquet</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>ed20929d8fd97c202c34fd600dad7403  data/mtcars1.parquet
ed20929d8fd97c202c34fd600dad7403  data/mtcars2.parquet</code></pre>
</div>
</div>
<p>The second issue was with a pipeline that was writing data as a partitioned parquet dataset. With sufficiently many rows, writing an arrow dataset doesn’t produce a stable hash for the individual partitions.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2e6</span>)</span>
<span id="cb14-2"></span>
<span id="cb14-3">arrow<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_dataset</span>(df, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/df1"</span>)</span>
<span id="cb14-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb14-5">arrow<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_dataset</span>(df, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/df2"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">md5sum</span> data/df<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span>/<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">*</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>f9829be365805a7bb34afcb97f3d510e  data/df1/part-0.parquet
173a94d10e8e00e9809ede831e3469d8  data/df2/part-0.parquet</code></pre>
</div>
</div>
<p>Unlike with excel, the issue here is that <code>{arrow}</code> is trying to speed things up by working in parallel and consequently the ordering of the rows is not guaranteed when writing the dataset (even with no partitions).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">df1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> arrow<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">open_dataset</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/df1"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span>
<span id="cb17-3"></span>
<span id="cb17-4">df2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> arrow<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">open_dataset</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/df2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-5">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">collect</span>()</span>
<span id="cb17-6"></span>
<span id="cb17-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setequal</span>(df1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setequal</span>(df2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>(df1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
</div>
<p>A new feature was added to the underlying arrow library last year to preserve the row ordering within partitions, and I recently contributed the bindings to this feature for the <code>{arrow}</code> R package (it’s already in <code>pyarrow</code>). With this feature enabled, the example of writing the partitioned dataset above results in identical hashes.</p>
<p>I suspect there are other file formats that also resistant reproducible workflows (maybe .RData or .Rds). In the case of arrow datasets, the fix was straightforward and it should soon go away; for excel, the format is inherently non-reproducible because it includes a timestamp.</p>





<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-dvc" class="csl-entry">
<span class="nocase">Kuprieiev, Ruslan, skshetry, (변기호)Peter Rowlands, et al.</span> 2026. <em>DVC: Data Version Control - Git for Data &amp; Models</em>. V. 3.67.0. Zenodo, released March. <a href="https://doi.org/10.5281/zenodo.19022483">https://doi.org/10.5281/zenodo.19022483</a>.
</div>
<div id="ref-targets" class="csl-entry">
Landau, William Michael. 2021. <span>“The Targets r Package: A Dynamic Make-Like Function-Oriented Pipeline Toolkit for Reproducibility and High-Performance Computing.”</span> <em>Journal of Open Source Software</em> 6 (57): 2959. <a href="https://doi.org/10.21105/joss.02959">https://doi.org/10.21105/joss.02959</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>I’m using <code>{openxlsx2}</code>, but the results are the same with both <code>{openxlsx}</code> and <code>pandas</code>.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{martin2026,
  author = {Martin, Steve},
  title = {Reproduction-Resistant File Formats},
  date = {2026-03-29},
  url = {https://marberts.github.io/blog/posts/2026/formats/},
  doi = {10.59350/mdxxv-ke330},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-martin2026" class="csl-entry quarto-appendix-citeas">
Martin, Steve. 2026. <span>“Reproduction-Resistant File Formats.”</span>
March 29. <a href="https://doi.org/10.59350/mdxxv-ke330">https://doi.org/10.59350/mdxxv-ke330</a>.
</div></div></section></div> ]]></description>
  <category>Reproducibility</category>
  <category>R</category>
  <guid>https://marberts.github.io/blog/posts/2026/formats/</guid>
  <pubDate>Sun, 29 Mar 2026 04:00:00 GMT</pubDate>
</item>
<item>
  <title>The price of morality</title>
  <dc:creator>Steve Martin</dc:creator>
  <link>https://marberts.github.io/blog/posts/2026/morality/</link>
  <description><![CDATA[ 





<p>What would happen to prices if firms started internalizing the social impact of their behavior? A few years ago I published a model of moral management in competitive markets <span class="citation" data-cites="martin2019">(Martin 2019)</span> and I thought it would be an interesting exercise to take the price and quantity functions from this model to make a price index. This is my first attempt to take a model where firms are not price takers and consumers aren’t standard utility maximizers and derive a price index as a function of how some deeper parameter changes over time.</p>
<section id="the-model" class="level2">
<h2 class="anchored" data-anchor-id="the-model">The model</h2>
<p>The model has two identical firms selling differentiated products to socially-conscientious consumers with heterogeneous preferences. Firms are able to engage in socially responsible behavior (e.g., voluntarily abating harmful emissions), with the twist that their motivation for doing so can be both for the usual strategic reasons and due to the preferences of the firm’s management—a moral manager. A greater degree of moral management means that a firm places more weight on pro-social behavior relative to profit, whereas a profit-maximizing manager places no weight on pro-social behavior and only considers profit. An increase in the degree of moral management for a firm affects the amount of pro-social behavior by both firms in the market, and consequently affects prices and expenditure/revenue shares for both products.</p>
<p>Part of the solution for the model is a pair of price and quantity functions that give the prices and market shares as a function of the degree of morality for each firm’s manager. These can be plugged into a price index to give a measure of the change in price from a change in managers’ preferences over time. In particular, comparing prices when managers are profit maximizers to prices with moral managers give a measure of the change in price due to moral managers.</p>
</section>
<section id="impact-of-morality-on-price" class="level2">
<h2 class="anchored" data-anchor-id="impact-of-morality-on-price">Impact of morality on price</h2>
<p>Let’s start with the simplest case: the two firms each have the same type of manager. In this case prices will be the same for both firms’ outputs (which do not change over time), so any price index that satisfies the proportionality axiom will simply be the change in price over time. Equations (2) and (3) in the paper can be used to establish that this change in price over time is a linear function of the degree to which managers value the social impact of their firm’s behavior, <img src="https://latex.codecogs.com/png.latex?1%20+%20%5Calpha%20%5Cmu">, where <img src="https://latex.codecogs.com/png.latex?%5Calpha"> is a complex function of the parameters of the model and <img src="https://latex.codecogs.com/png.latex?%5Cmu"> is the degree to which the manager values the social conduct of the their firm (relative to profits). Therefore, an increase in the morality of managers in the market will translate into an increase in any reasonable price index over time.</p>
<p>The more interesting case is when firms have different types of managers. In this case, an increase in the morality of one firm’s management will lead to an increase in price and quantity for that firm’s product and a (smaller) decrease in price and quantity for the competitor’s product. The increase in price for one product and decrease in price for the other work against each other to dampen the increase in price when firms are run by the same type of manager. Note that the expenditure/revenue share increases for the product with the increasing price, the opposite of what’s usually expected, and this means that the Paasche index is larger than the Laspeyres index.</p>
<p>Rather than try to get a closed-form expression for a price index as a function of the degree of moral management in the market, let’s work through some numerical examples to see how it compares to the case when both managers have the same type. We’ll start by making a class to generate price relatives and expenditure shares for a pair of types, relative to profit-maximizing managers.</p>
<div id="71403887" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> typing <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Callable</span>
<span id="cb1-4"></span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> ConstraintError(<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>):</span>
<span id="cb1-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb1-8"></span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> MoralManagers:</span>
<span id="cb1-11">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, t: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, v: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, c: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, k: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, F: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb1-12">        t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(t)</span>
<span id="cb1-13">        v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(v)</span>
<span id="cb1-14">        c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(c)</span>
<span id="cb1-15">        k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(k)</span>
<span id="cb1-16">        F <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(F)</span>
<span id="cb1-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> (v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> F):</span>
<span id="cb1-18">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> ConstraintError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t is too small"</span>)</span>
<span id="cb1-19"></span>
<span id="cb1-20">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> F <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> F <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-21">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.rho <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> F <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-22">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> t</span>
<span id="cb1-23">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v</span>
<span id="cb1-24">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c</span>
<span id="cb1-25">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> k</span>
<span id="cb1-26">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.F <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> F</span>
<span id="cb1-27"></span>
<span id="cb1-28">        base_s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.k) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.F)</span>
<span id="cb1-29">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.base_p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.p0(base_s, base_s)</span>
<span id="cb1-30"></span>
<span id="cb1-31">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> p0(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, s0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, s1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-32">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (</span>
<span id="cb1-33">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.t</span>
<span id="cb1-34">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.c</span>
<span id="cb1-35">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.k) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> s0</span>
<span id="cb1-36">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.k) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> s1</span>
<span id="cb1-37">        )</span>
<span id="cb1-38"></span>
<span id="cb1-39">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> p1(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, s0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, s1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-40">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.p0(s1, s0)</span>
<span id="cb1-41"></span>
<span id="cb1-42">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> q0(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, p0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, p1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, s0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, s1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-43">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (s0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> s1) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> p0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p1) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.t)</span>
<span id="cb1-44"></span>
<span id="cb1-45">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> q1(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, p0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, p1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, s0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, s1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-46">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q0(p1, p0, s1, s0)</span>
<span id="cb1-47"></span>
<span id="cb1-48">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> s0(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, mu0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, mu1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-49">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.k) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.F) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.phi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.F <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (</span>
<span id="cb1-50">            mu0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.rho <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> mu1</span>
<span id="cb1-51">        )</span>
<span id="cb1-52"></span>
<span id="cb1-53">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> s1(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, mu0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, mu1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-54">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s0(mu1, mu0)</span>
<span id="cb1-55"></span>
<span id="cb1-56">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pi(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, p: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, q: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, s: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-57">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.F <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> s<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-58"></span>
<span id="cb1-59">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> pqs(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, mu0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, mu1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>:</span>
<span id="cb1-60">        s0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s0(mu0, mu1)</span>
<span id="cb1-61">        s1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.s1(mu0, mu1)</span>
<span id="cb1-62">        p0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.p0(s0, s1)</span>
<span id="cb1-63">        p1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.p1(s0, s1)</span>
<span id="cb1-64">        q0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q0(p0, p1, s0, s1)</span>
<span id="cb1-65">        q1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.q1(p0, p1, s0, s1)</span>
<span id="cb1-66">        pi0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.pi(p0, q0, s0)</span>
<span id="cb1-67">        pi1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.pi(p1, q1, s1)</span>
<span id="cb1-68">        share0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q1)</span>
<span id="cb1-69">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> pi0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb1-70">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> ConstraintError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"negative profit for firm 0"</span>)</span>
<span id="cb1-71">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">elif</span> pi1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb1-72">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> ConstraintError(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"negative profit for firm 1"</span>)</span>
<span id="cb1-73">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb1-74">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> {</span>
<span id="cb1-75">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rel0"</span>: p0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.base_p,</span>
<span id="cb1-76">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rel1"</span>: p1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.base_p,</span>
<span id="cb1-77">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"share0"</span>: share0,</span>
<span id="cb1-78">            }</span>
<span id="cb1-79"></span>
<span id="cb1-80"></span>
<span id="cb1-81"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> laspeyres_index(rel0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, rel1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, share0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-82">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> rel0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rel1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb1-83"></span>
<span id="cb1-84"></span>
<span id="cb1-85"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> paasche_index(rel0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, rel1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, share0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-86">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (share0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> rel0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> share0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> rel1)</span>
<span id="cb1-87"></span>
<span id="cb1-88"></span>
<span id="cb1-89"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> fisher_index(rel0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, rel1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, share0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-90">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (laspeyres_index(rel0, rel1, share0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> paasche_index(rel0, rel1, share0))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span></span>
<span id="cb1-91"></span>
<span id="cb1-92"></span>
<span id="cb1-93"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> tornqvist_index(rel0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, rel1: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, share0: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-94">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> rel0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> share0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> rel1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> share0))</span>
<span id="cb1-95"></span>
<span id="cb1-96"></span>
<span id="cb1-97"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> make_index(data: pd.DataFrame, index: Callable) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pd.Series:</span>
<span id="cb1-98">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> data.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">apply</span>(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> x: index(x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rel0"</span>], x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rel1"</span>], x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"share0"</span>]), axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div></div>
</div>
<p>We can now simulate the impact of an increase in morality for one firm on a Fisher index under three scenarios: managers are entirely uncorrelated, so that the other firm is always a profit maximizes; managers are somewhat correlated; and managers are perfectly correlated and have the same type. In order to respect the constraint that firms must make positive profit, we’ll only consider a change in morality up to the point at which the manager places equal value on profitability and social conduct.</p>
<div id="1dccb4c5" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> simulate(corr: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>, model: MoralManagers) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> pd.DataFrame:</span>
<span id="cb2-2">    df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb2-3">        pd.DataFrame(model.pqs(m, corr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> m), index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[m]) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> m <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> np.arange(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)</span>
<span id="cb2-4">    ]</span>
<span id="cb2-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> pd.concat(df)</span>
<span id="cb2-6"></span>
<span id="cb2-7"></span>
<span id="cb2-8">mm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> MoralManagers(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-9"></span>
<span id="cb2-10">simulation <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {corr: simulate(corr, mm) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> corr <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]}</span>
<span id="cb2-11"></span>
<span id="cb2-12">fisher <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat(</span>
<span id="cb2-13">    {<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"corr=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>corr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>: make_index(s, fisher_index) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> corr, s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> simulation.items()},</span>
<span id="cb2-14">    axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb2-15">)</span>
<span id="cb2-16">fisher.plot(ylabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fisher index"</span>, xlabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Degree of morality"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://marberts.github.io/blog/posts/2026/morality/index_files/figure-html/cell-3-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>The top line shows the linear effect of morality on price when both firms have the same type of manager. Introducing heterogeneity in the types of managers results in a smaller increase in price, although the Fisher index is now a convex function of the degree of morality, with the curvature becoming more pronounced as managers’ types become less correlated. This is driven by the Paasche index, as the Laspeyres index is a linear function of the degree of morality.</p>
<div id="7954ed4a" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(</span>
<span id="cb3-2">    {</span>
<span id="cb3-3">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Laspeyres index"</span>: make_index(simulation[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], laspeyres_index),</span>
<span id="cb3-4">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paasche index"</span>: make_index(simulation[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], paasche_index),</span>
<span id="cb3-5">    }</span>
<span id="cb3-6">)</span>
<span id="cb3-7"></span>
<span id="cb3-8">df.plot(ylabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Index"</span>, xlabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Degree of morality"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://marberts.github.io/blog/posts/2026/morality/index_files/figure-html/cell-4-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Using a Törnqvist index instead of a Fisher index gives the same general result.</p>
<div id="08d7958a" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1">tornqvist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.concat(</span>
<span id="cb4-2">    {<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"corr=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>corr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>: make_index(s, tornqvist_index) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> corr, s <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> simulation.items()},</span>
<span id="cb4-3">    axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb4-4">)</span>
<span id="cb4-5">tornqvist.plot(ylabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Törnqvist index"</span>, xlabel<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Degree of morality"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://marberts.github.io/blog/posts/2026/morality/index_files/figure-html/cell-5-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>I find this to be an interesting result. Despite coming from a model in which both firms and consumers are making optimal decisions, an increase in the degree of morality from one firm in the market results in price indexes that behave quite differently than expected.</p>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-martin2019" class="csl-entry">
Martin, Steve. 2019. <span>“Moral Management in Competitive Markets.”</span> <em>Journal of Economics &amp; Management Strategy</em> 28 (3): 541–60. <a href="https://doi.org/10.1111/jems.12283">https://doi.org/10.1111/jems.12283</a>.
</div>
</div></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{martin2026,
  author = {Martin, Steve},
  title = {The Price of Morality},
  date = {2026-01-05},
  url = {https://marberts.github.io/blog/posts/2026/morality/},
  doi = {10.59350/dyzbr-nde66},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-martin2026" class="csl-entry quarto-appendix-citeas">
Martin, Steve. 2026. <span>“The Price of Morality.”</span> January 5. <a href="https://doi.org/10.59350/dyzbr-nde66">https://doi.org/10.59350/dyzbr-nde66</a>.
</div></div></section></div> ]]></description>
  <category>Corporate social responsibility</category>
  <category>Index numbers</category>
  <category>Python</category>
  <guid>https://marberts.github.io/blog/posts/2026/morality/</guid>
  <pubDate>Mon, 05 Jan 2026 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Making big multilateral indexes fast</title>
  <dc:creator>Steve Martin</dc:creator>
  <link>https://marberts.github.io/blog/posts/2025/multilaterals/</link>
  <description><![CDATA[ 





<p>Multilateral price indexes are often used to measure the evolution of prices over time when there are large volumes of transaction data, such as retail scanner data or housing data. The main challenge with computing multilateral indexes with large amounts of data is that these indexes often depend on a matrix where the dimensions are at least as large as the number of products. Lots of data means lots of different products, which in turns means making quite large matrices. Indeed, assuming these matrices are stored as 64-bit floats (more on that below), <em>at least</em> <img src="https://latex.codecogs.com/png.latex?8k%5E2"> bytes of memory are needed just to store one of these matrices with <img src="https://latex.codecogs.com/png.latex?k"> products—with, say, 100,000 products, that’s <em>at least</em> 80 GB of memory before doing any computations. Effectively computing these indexes requires a way to reduce the dimensions of these matrices.</p>
<section id="weighted-time-product-dummy" class="level2">
<h2 class="anchored" data-anchor-id="weighted-time-product-dummy">Weighted time product dummy</h2>
<p>Let’s start with the WTPD index. This index is recovered from the coefficient on a set of time-dummy variables in a weighted log-linear regression model with a dummy variable for each product <span class="citation" data-cites="diewert2022">(e.g., Diewert and Fox 2022, eq. 9)</span>. Fitting this model is impractical with many products—with <img src="https://latex.codecogs.com/png.latex?n"> observations for <img src="https://latex.codecogs.com/png.latex?k"> products over <img src="https://latex.codecogs.com/png.latex?t"> time periods, the design matrix is a <img src="https://latex.codecogs.com/png.latex?n%20%5Ctimes%20(k%20+%20t%20-%201)"> matrix and will consume at least <img src="https://latex.codecogs.com/png.latex?8(k%20+%20t%20-%201)%5E2"> bytes of memory. Fortunately, only the coefficients on the time-dummy variables are required to make the index and so the product-dummy variables can be removed by treating them as fixed effects and demeaning the regression equation <span class="citation" data-cites="wooldridge2002">(Wooldridge 2002, sec. 10.5)</span>. Now the design matrix has only <img src="https://latex.codecogs.com/png.latex?t%20-%201"> columns and, as <img src="https://latex.codecogs.com/png.latex?t"> is usually much smaller than <img src="https://latex.codecogs.com/png.latex?k">, will consume far less memory.</p>
<p>Let’s consider a example. We’ll start with a function to make (unbalanced) transaction data for a collection of products over time.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1552443</span>)</span>
<span id="cb1-2"></span>
<span id="cb1-3">make_prices <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(products, periods, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">frac =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>) {</span>
<span id="cb1-4">  res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">product =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(products)),</span>
<span id="cb1-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(periods), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> products)),</span>
<span id="cb1-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">price =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rlnorm</span>(products <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> periods),</span>
<span id="cb1-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quantity =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(products <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> periods)</span>
<span id="cb1-9">  )</span>
<span id="cb1-10">  res[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(res), frac <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(res)), ]</span>
<span id="cb1-11">}</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_prices</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  product time     price  quantity
2       2    1 1.4944618 0.2978414
3       3    1 0.6778284 0.9263743
5       5    1 1.2611687 0.3008513
7       7    1 0.7996127 0.9532272
8       8    1 0.2138277 0.4409094
9       9    1 1.9830544 0.9422246</code></pre>
</div>
</div>
<p>We’ll also make a function to take these data and construct the WTPD index from a fixed-effects model with the <code>{fixest}</code> package.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">wtpd_index <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(prices) {</span>
<span id="cb3-2">  prices <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prices <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-3">    dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(time) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-4">    dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">w =</span> gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_weights</span>(price <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> quantity))</span>
<span id="cb3-5"></span>
<span id="cb3-6">  mdl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fixest<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">feols</span>(</span>
<span id="cb3-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(price) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> product,</span>
<span id="cb3-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> prices,</span>
<span id="cb3-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>w</span>
<span id="cb3-10">  )</span>
<span id="cb3-11"></span>
<span id="cb3-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(mdl))</span>
<span id="cb3-13">}</span></code></pre></div></div>
</div>
<p>Now let’s build a WTPD index with data for 100,000 products over 25 time periods. Doing this with a regular linear model, where each product has its own dummy variable, would require 1.8 TB of memory to just store the design matrix, making it impractical to calculate. Using a fixed-effects model to recover the WTPD index can be done without issue on any decent laptop.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">price_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_prices</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span>
<span id="cb4-2"></span>
<span id="cb4-3">bench<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mark</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fixest =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wtpd_index</span>(price_data))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 6
  expression      min   median `itr/sec` mem_alloc `gc/sec`
  &lt;bch:expr&gt; &lt;bch:tm&gt; &lt;bch:tm&gt;     &lt;dbl&gt; &lt;bch:byt&gt;    &lt;dbl&gt;
1 fixest        4.17s    4.17s     0.240    2.77GB    0.960</code></pre>
</div>
</div>
<p>Note that this is generally much faster than the routine for making the WTPD index in the excellent <code>{IndexNumR}</code> package.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">wtpd_index2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(prices, window) {</span>
<span id="cb6-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># IndexNumR expects these to be integers.</span></span>
<span id="cb6-3">  prices[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"time"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"product"</span>)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(</span>
<span id="cb6-4">    prices[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"time"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"product"</span>)],</span>
<span id="cb6-5">    as.integer</span>
<span id="cb6-6">  )</span>
<span id="cb6-7">  res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> IndexNumR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">WTPDIndex</span>(</span>
<span id="cb6-8">    prices,</span>
<span id="cb6-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"price"</span>,</span>
<span id="cb6-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quantity"</span>,</span>
<span id="cb6-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"time"</span>,</span>
<span id="cb6-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"product"</span>,</span>
<span id="cb6-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> window</span>
<span id="cb6-14">  )</span>
<span id="cb6-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(res[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb6-16">}</span>
<span id="cb6-17"></span>
<span id="cb6-18">price_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_prices</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span>
<span id="cb6-19"></span>
<span id="cb6-20">bench<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mark</span>(</span>
<span id="cb6-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fixest =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wtpd_index</span>(price_data)),</span>
<span id="cb6-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">IndexNumR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wtpd_index2</span>(price_data, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span>
<span id="cb6-23">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 6
  expression      min   median `itr/sec` mem_alloc `gc/sec`
  &lt;bch:expr&gt; &lt;bch:tm&gt; &lt;bch:tm&gt;     &lt;dbl&gt; &lt;bch:byt&gt;    &lt;dbl&gt;
1 fixest       8.77ms   9.56ms   103.       1.51MB     3.98
2 IndexNumR     3.59s    3.59s     0.278  102.15MB     8.07</code></pre>
</div>
</div>
</section>
<section id="geary-khamis-index" class="level2">
<h2 class="anchored" data-anchor-id="geary-khamis-index">Geary-Khamis index</h2>
<p>The GK index is another multilateral index that’s often written as a function of a large matrix. Following <span class="citation" data-cites="diewert2022">Diewert and Fox (2022)</span>, the GK index can be written as a function of two <img src="https://latex.codecogs.com/png.latex?k%20%5Ctimes%20k"> matrices. One of these matrices is diagonal, so it can be handled efficiently, but the other is usually dense (unless product imbalanced is really bad) and is impractical to store in memory. Using the same data as with the WTPD index, it would require 80 GB of memory to just store this matrix.</p>
<p>An alternative approach <span class="citation" data-cites="balk2008">(Balk 2008, 245–46)</span> expresses the GK index as a function of three <img src="https://latex.codecogs.com/png.latex?t%20%5Ctimes%20t"> matrices. With the same data as before, this now requires only 15 KB of memory. Here is relatively fast function to implement this approach.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Intertemporal Geary-Khamis index</span></span>
<span id="cb8-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb8-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Make a multilateral Geary-Khamis price index.</span></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param p A numeric vector of prices.</span></span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param q A numeric vector of quantities.</span></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param period A factor, or something that can be coerced into one, that</span></span>
<span id="cb8-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'   gives the corresponding time period for each element in `p` and</span></span>
<span id="cb8-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'   `q`. The ordering of time periods follows the levels of `period`</span></span>
<span id="cb8-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'   to agree with [`cut()`][cut.Date].</span></span>
<span id="cb8-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param product A factor, or something that can be coerced into one, that</span></span>
<span id="cb8-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'   gives the corresponding product identifier for each element in `p` and</span></span>
<span id="cb8-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'   `q`.</span></span>
<span id="cb8-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param na.rm Should missing values be removed? By default they are not.</span></span>
<span id="cb8-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb8-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @returns</span></span>
<span id="cb8-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' A numeric vector of period-over-period price indexes.</span></span>
<span id="cb8-18"></span>
<span id="cb8-19">fast_gk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(p, q, period, product, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) {</span>
<span id="cb8-20">  period <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(period)</span>
<span id="cb8-21">  product <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(product)</span>
<span id="cb8-22"></span>
<span id="cb8-23">  qs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unsplit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(q, product), gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>scale_weights), product)</span>
<span id="cb8-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attributes</span>(product) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># faster to match on numeric codes</span></span>
<span id="cb8-25">  ux <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(product)</span>
<span id="cb8-26">  product <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(</span>
<span id="cb8-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(product, period),</span>
<span id="cb8-28">    \(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">match</span>(ux, x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">incomparables =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb8-29">  )</span>
<span id="cb8-30"></span>
<span id="cb8-31">  v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Map</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q, period), product)</span>
<span id="cb8-32">  qs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Map</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">[</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(qs, period), product)</span>
<span id="cb8-33"></span>
<span id="cb8-34">  n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nlevels</span>(period)</span>
<span id="cb8-35">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make the matrices from pp. 245-246 of Balk (2008) to build the quantity</span></span>
<span id="cb8-36">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># index, then deflate.</span></span>
<span id="cb8-37">  m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vector</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"list"</span>, n)</span>
<span id="cb8-38">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(m)) {</span>
<span id="cb8-39">    m[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(</span>
<span id="cb8-40">      qs,</span>
<span id="cb8-41">      \(qs) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(qs <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_weights</span>(v[[i]]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> na.rm),</span>
<span id="cb8-42">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L)</span>
<span id="cb8-43">    )</span>
<span id="cb8-44">  }</span>
<span id="cb8-45">  e <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(n)</span>
<span id="cb8-46">  r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep.int</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, n), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, n, n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb8-47">  q <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, m) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> e <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> r)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L, ]</span>
<span id="cb8-48">  v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(v, sum, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> na.rm)</span>
<span id="cb8-49">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Return the period-over-period index.</span></span>
<span id="cb8-50">  v[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> v[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(v)] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (q[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>L] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> q[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(q)])</span>
<span id="cb8-51">}</span></code></pre></div></div>
</div>
<p>As with the WTPD index, any decent laptop can compute this index.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">gk_index <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(prices) {</span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fast_gk</span>(</span>
<span id="cb9-3">    prices<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>price,</span>
<span id="cb9-4">    prices<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>quantity,</span>
<span id="cb9-5">    prices<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time,</span>
<span id="cb9-6">    prices<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>product,</span>
<span id="cb9-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb9-8">  )</span>
<span id="cb9-9">}</span>
<span id="cb9-10"></span>
<span id="cb9-11">price_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_prices</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span>
<span id="cb9-12"></span>
<span id="cb9-13">bench<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mark</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fast_gk =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gk_index</span>(price_data))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 6
  expression      min   median `itr/sec` mem_alloc `gc/sec`
  &lt;bch:expr&gt; &lt;bch:tm&gt; &lt;bch:tm&gt;     &lt;dbl&gt; &lt;bch:byt&gt;    &lt;dbl&gt;
1 fast_gk       1.37s    1.37s     0.729     819MB    0.729</code></pre>
</div>
</div>
<p>It is similarly non-trivial to make an index of this size with <code>{IndexNumR}</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">gk_index2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(prices, window) {</span>
<span id="cb11-2">  prices[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"time"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"product"</span>)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(</span>
<span id="cb11-3">    prices[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"time"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"product"</span>)],</span>
<span id="cb11-4">    as.integer</span>
<span id="cb11-5">  )</span>
<span id="cb11-6">  res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> IndexNumR<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">GKIndex</span>(</span>
<span id="cb11-7">    prices,</span>
<span id="cb11-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"price"</span>,</span>
<span id="cb11-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"quantity"</span>,</span>
<span id="cb11-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"time"</span>,</span>
<span id="cb11-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"product"</span>,</span>
<span id="cb11-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">window =</span> window</span>
<span id="cb11-13">  )</span>
<span id="cb11-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(res[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> res[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(res)])</span>
<span id="cb11-15">}</span>
<span id="cb11-16"></span>
<span id="cb11-17">price_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_prices</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span>
<span id="cb11-18"></span>
<span id="cb11-19">bench<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mark</span>(</span>
<span id="cb11-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fast_gk =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gk_index</span>(price_data)),</span>
<span id="cb11-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">IndexNumR =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gk_index2</span>(price_data, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)</span>
<span id="cb11-22">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 6
  expression      min   median `itr/sec` mem_alloc `gc/sec`
  &lt;bch:expr&gt; &lt;bch:tm&gt; &lt;bch:tm&gt;     &lt;dbl&gt; &lt;bch:byt&gt;    &lt;dbl&gt;
1 fast_gk      7.23ms   7.57ms   125.       8.15MB    1.99 
2 IndexNumR      1.4s     1.4s     0.713  332.21MB    0.713</code></pre>
</div>
</div>
</section>
<section id="repeat-sales" class="level2">
<h2 class="anchored" data-anchor-id="repeat-sales">Repeat sales</h2>
<p>The repeat-sales indexes listed by <span class="citation" data-cites="shiller1991">Shiller (1991)</span> are a type of multilateral index usually found in the housing economics literature. Like the WTPD index, they come from linear models and suffer from the same large design matrices. The geometric repeat-sales index is like the WTPD index, although usually with a much longer time horizon, except that it uses the first-difference transformation to sweep out the product fixed effects instead of the within-group transformation. The arithmetic repeat-sales index is more complicated as it comes from an IV estimator and its not obvious, at least to me, how the matrices for this index can be made smaller.</p>
<p>Instead of reducing the dimensionality of the repeat-sales matrices, these matrices are naturally sparse when products sell infrequently (like housing) and can be stored as sparse matrices. <sup>1</sup> Let’s adapt the vignette from my <code>{rsmatrix}</code> package to see this in action with some data for 5 million house sales over 30 years.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">periods <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2000-01-01"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2029-12-31"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"day"</span>)</span>
<span id="cb13-2"></span>
<span id="cb13-3">prices <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb13-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sale =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(periods, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5e6</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb13-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">property =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%06d"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e6</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5e6</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))),</span>
<span id="cb13-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">price =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rlnorm</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5e6</span>)</span>
<span id="cb13-7">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-8">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">period =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cut</span>(sale, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month"</span>))</span>
<span id="cb13-9"></span>
<span id="cb13-10">sales_pairs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rsmatrix<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rs_pairs</span>(prices<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>sale, prices<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>property)</span>
<span id="cb13-11">prices[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"price_prev"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"period_prev"</span>)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prices[</span>
<span id="cb13-12">  sales_pairs,</span>
<span id="cb13-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"price"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"period"</span>)</span>
<span id="cb13-14">]</span>
<span id="cb13-15"></span>
<span id="cb13-16">prices <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> prices <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-17">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb13-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">holding_period =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(period) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(period_prev)</span>
<span id="cb13-19">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-20">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(holding_period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb13-21"></span>
<span id="cb13-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(prices)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>        sale property     price     period price_prev period_prev
1 2016-05-11   627686 0.7705151 2016-05-01  0.1878927  2014-06-01
2 2029-07-22   992771 0.2366689 2029-07-01  0.4822262  2026-10-01
3 2011-07-17   925847 0.1712451 2011-07-01  0.5992709  2003-05-01
4 2013-03-28   337973 0.1540939 2013-03-01  0.9106525  2002-11-01
5 2008-04-22   295262 0.3274417 2008-04-01  0.2760829  2003-10-01
6 2015-12-27   038822 2.2174534 2015-12-01  1.7673652  2010-11-01
  holding_period
1             23
2             33
3             98
4            124
5             54
6             61</code></pre>
</div>
</div>
<p>We can now build a generator to make the sparse repeat-sales matrices. If these were dense matrices they would consume 22.1 GB of memory. This isn’t nearly as bad as the WTPD or GK indexes, but still enough to make computing these indexes cumbersome. With sparse matrices, however, it is easy to make the repeat-sales index.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">matrices <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(</span>
<span id="cb15-2">  prices,</span>
<span id="cb15-3">  rsmatrix<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rs_matrix</span>(period, period_prev, price, price_prev, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sparse =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb15-4">)</span>
<span id="cb15-5"></span>
<span id="cb15-6">Z <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrices</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Z"</span>)</span>
<span id="cb15-7">X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrices</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>)</span>
<span id="cb15-8">Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrices</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span>)</span>
<span id="cb15-9"></span>
<span id="cb15-10">bench<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mark</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ars =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> Matrix<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crossprod</span>(Z, X), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crossprod</span>(Z, Y)))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 6
  expression      min   median `itr/sec` mem_alloc `gc/sec`
  &lt;bch:expr&gt; &lt;bch:tm&gt; &lt;bch:tm&gt;     &lt;dbl&gt; &lt;bch:byt&gt;    &lt;dbl&gt;
1 ars           1.53s    1.53s     0.652       3MB        0</code></pre>
</div>
</div>
</section>
<section id="geks" class="level2">
<h2 class="anchored" data-anchor-id="geks">GEKS</h2>
<p>Let’s conclude by briefly mentioned the GEKS family of multilateral indexes. As with the others, the GEKS depends on a matrix, but it is only a <img src="https://latex.codecogs.com/png.latex?t%20%5Ctimes%20t"> matrix and will require very little memory. This makes GEKS indexes somewhat more straightforward to compute than the other popular multilateral indexes.</p>
<details>
<summary>
Session Info
</summary>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>R version 4.5.2 (2025-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.12.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0  LAPACK version 3.12.0

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       

time zone: America/Toronto
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Matrix_1.7-4        jsonlite_2.0.0      dplyr_1.1.4        
 [4] compiler_4.5.2      rsmatrix_0.2.9      tidyselect_1.2.1   
 [7] Rcpp_1.1.0          yaml_2.3.10         fastmap_1.2.0      
[10] lattice_0.22-7      R6_2.6.1            generics_0.1.4     
[13] Formula_1.2-5       knitr_1.50          htmlwidgets_1.6.4  
[16] tibble_3.3.0        pillar_1.11.1       rlang_1.1.6        
[19] utf8_1.2.6          xfun_0.54           stringmagic_1.2.0  
[22] fixest_0.13.2       cli_3.6.5           magrittr_2.0.4     
[25] digest_0.6.38       grid_4.5.2          IndexNumR_0.6.0    
[28] sandwich_3.1-1      gpindex_0.6.3       lifecycle_1.0.4    
[31] nlme_3.1-168        vctrs_0.6.5         bench_1.1.4        
[34] evaluate_1.0.5      glue_1.8.0          data.table_1.17.8  
[37] numDeriv_2016.8-1.1 zoo_1.8-14          profmem_0.7.0      
[40] dreamerr_1.5.0      rmarkdown_2.30      tools_4.5.2        
[43] pkgconfig_2.0.3     htmltools_0.5.8.1  </code></pre>
</div>
</div>
</details>



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-balk2008" class="csl-entry">
Balk, Bert M. 2008. <em>Price and Quantity Index Numbers: Models for Measuring Aggregate Change and Difference</em>. Cambridge University Press. <a href="https://doi.org/10.1017/CBO9780511720758">https://doi.org/10.1017/CBO9780511720758</a>.
</div>
<div id="ref-diewert2022" class="csl-entry">
Diewert, W Erwin, and Kevin J Fox. 2022. <span>“Substitution Bias in Multilateral Methods for Cpi Construction.”</span> <em>Journal of Business &amp; Economic Statistics</em> 40 (1): 355–69. <a href="https://doi.org/10.1080/07350015.2020.1816176">https://doi.org/10.1080/07350015.2020.1816176</a>.
</div>
<div id="ref-langsrud2023" class="csl-entry">
Langsrud, Øyvind. 2024. <span>“Sparse Model Matrices for Multidimensional Hierarchical Aggregation.”</span> <em>The R Journal</em> 15: 150–66. <a href="https://doi.org/10.32614/RJ-2023-088">https://doi.org/10.32614/RJ-2023-088</a>.
</div>
<div id="ref-shiller1991" class="csl-entry">
Shiller, Robert J. 1991. <span>“Arithmetic Repeat Sales Price Estimators.”</span> <em>Journal of Housing Economics</em> 1 (1): 110–26. <a href="https://doi.org/10.1016/S1051-1377(05)80028-2">https://doi.org/10.1016/S1051-1377(05)80028-2</a>.
</div>
<div id="ref-wooldridge2002" class="csl-entry">
Wooldridge, Jeffrey M. 2002. <em>Econometric Analysis of Cross Section and Panel Data</em>. MIT Press.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>See <span class="citation" data-cites="langsrud2023">Langsrud (2024)</span> for another example of using sparse matrices for aggregation.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{martin2025,
  author = {Martin, Steve},
  title = {Making Big Multilateral Indexes Fast},
  date = {2025-11-24},
  url = {https://marberts.github.io/blog/posts/2025/multilaterals/},
  doi = {10.59350/c2v2f-rnj27},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-martin2025" class="csl-entry quarto-appendix-citeas">
Martin, Steve. 2025. <span>“Making Big Multilateral Indexes
Fast.”</span> November 24. <a href="https://doi.org/10.59350/c2v2f-rnj27">https://doi.org/10.59350/c2v2f-rnj27</a>.
</div></div></section></div> ]]></description>
  <category>Performance</category>
  <category>Index numbers</category>
  <category>R</category>
  <guid>https://marberts.github.io/blog/posts/2025/multilaterals/</guid>
  <pubDate>Mon, 24 Nov 2025 05:00:00 GMT</pubDate>
</item>
<item>
  <title>How does the difference between the arithmetic and geometric mean depend on variance?</title>
  <dc:creator>Steve Martin</dc:creator>
  <link>https://marberts.github.io/blog/posts/2025/variance-inequality/</link>
  <description><![CDATA[ 





<p>That the arithmetic mean of a set of number is larger than the geometric mean is a foundational result in the theory of inequalities. This is a useful result for index numbers in particular as it gives that, for the same data, an index based on an arithmetic mean will always be larger than the corresponding index based on the geometric mean.</p>
<p>The latest edition of the CPI manual says the following about the difference between the arithmetic and geometric mean and how it relates to variance <span class="citation" data-cites="cpi2020">(IMF et al. 2020, 2)</span></p>
<blockquote class="blockquote">
<p>It should be noted that an arithmetic average is always greater than or equal to a geometric average and that the difference will be greater, the greater the variance in the price relatives.</p>
</blockquote>
<p>This seems like a great result that gives a way to easily think about the relationship between an index that uses an arithmetic mean to aggregate price relatives (Carli index) and one that uses a geometric mean (Jevons index). <sup>1</sup> Unfortunately it’s not true—see <span class="citation" data-cites="lord2002">Lord (2002)</span> for a neat way to generate counter examples. <sup>2</sup></p>
<p>The issue is that, for a vector of positive numbers <img src="https://latex.codecogs.com/png.latex?x%5Cin%5Cmathbf%7BR%7D_%7Bn%7D%5E%7B++%7D">, the difference between the arithmetic mean <img src="https://latex.codecogs.com/png.latex?%5Cmathfrak%7BA%7D(x)"> and geometric mean <img src="https://latex.codecogs.com/png.latex?%5Cmathfrak%7BG%7D(x)"> is only <em>bounded</em> by the variance <span class="citation" data-cites="bullen2003">(Bullen 2003, 156)</span></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cfrac%7B%5Ctext%7Bvar%7D(x)%7D%7B2%20%5Cmax(x)%7D%20%5Cleq%20%5Cmathfrak%7BA%7D(x)%20-%20%5Cmathfrak%7BG%7D(x)%20%5Cleq%20%5Cfrac%7B%5Ctext%7Bvar%7D(x)%7D%7B2%20%5Cmin(x)%7D.%0A"></p>
<p>For two vectors <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y">, we have that <img src="https://latex.codecogs.com/png.latex?%5Cmathfrak%7BA%7D(y)%20-%20%5Cmathfrak%7BG%7D(y)%20%3E%20%5Cmathfrak%7BA%7D(x)%20-%20%5Cmathfrak%7BG%7D(x)"> if</p>
<p><span id="eq-ineq1"><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Bvar%7D(y)%20%3E%20%5Cfrac%7B%5Cmax(y)%7D%7B%5Cmin(x)%7D%5Ctext%7Bvar%7D(x)%0A%5Ctag%7B1%7D"></span></p>
<p>and the opposite if</p>
<p><span id="eq-ineq2"><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Bvar%7D(y)%20%3C%20%5Cfrac%7B%5Cmin(y)%7D%7B%5Cmax(x)%7D%5Ctext%7Bvar%7D(x)%0A%5Ctag%7B2%7D"></span></p>
<p>If <img src="https://latex.codecogs.com/png.latex?%5Cmax(y)%20%3E%20%5Cmin(x)"> (what we should expect) then the difference between the arithmetic and geometric mean increases with a sufficiently large increase in variance. Similarly, if <img src="https://latex.codecogs.com/png.latex?%5Cmin(y)%20%3C%20%5Cmax(x)"> then the difference between the arithmetic and geometric mean decreases with a sufficiently large decrease in variance. If the support of the distribution is sufficiently narrow then the difference between the arithmetic and geometric mean will follow closely with the variance, and this is similar to the argument that generates the claim in the CPI manual <span class="citation" data-cites="cpitheory">(IMF et al. 2025, 131)</span>.</p>
<p>What I want to understand is how the difference between the arithmetic and geometric mean changes for <em>smaller</em> changes in variance without placing restrictions on the support of the distribution. Let’s consider drawing a collection of samples from a fixed probability distribution to see how the difference between the arithmetic and geometric mean relates to variance among samples from that distribution. We’ll make a few helper functions for this.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">var_ineq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb1-2">  m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x)</span>
<span id="cb1-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb1-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> m)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb1-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">diff =</span> m <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(x))),</span>
<span id="cb1-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(x),</span>
<span id="cb1-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(x)</span>
<span id="cb1-8">  )</span>
<span id="cb1-9">}</span>
<span id="cb1-10"></span>
<span id="cb1-11">simulate_var_ineq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n, sim, dist) {</span>
<span id="cb1-12">  res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dist</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sim), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> n), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, var_ineq)</span>
<span id="cb1-13">  res[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(res[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var"</span>, ])]</span>
<span id="cb1-14">}</span>
<span id="cb1-15"></span>
<span id="cb1-16">prob_decrease <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb1-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(</span>
<span id="cb1-18">    x,</span>
<span id="cb1-19">    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb1-20">    \(z) {</span>
<span id="cb1-21">      cond <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var"</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max"</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> z[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"min"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb1-22">        x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var"</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"min"</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> z[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var"</span>]</span>
<span id="cb1-23">      x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x[, cond]</span>
<span id="cb1-24">      decrease <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"diff"</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> z[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"diff"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var"</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> z[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var"</span>]</span>
<span id="cb1-25">      increase <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"diff"</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> z[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"diff"</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> x[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var"</span>, ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> z[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"var"</span>]</span>
<span id="cb1-26"></span>
<span id="cb1-27">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(decrease <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> increase) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb1-28">    }</span>
<span id="cb1-29">  )</span>
<span id="cb1-30">}</span></code></pre></div></div>
</div>
<p>The scatter plot of the difference between the arithmetic and geometric mean and variance for a sample of size 10 shows that, although the difference tends to increase with variance, there are several instances where the opposite occurs.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15243</span>)</span>
<span id="cb2-2"></span>
<span id="cb2-3">sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_var_ineq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, \(n) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rlnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sdlog =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>))</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(sim), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"arithmetic mean - geometric mean"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"variance"</span>)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">symbols</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.13</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">circles =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.015</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inches =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">symbols</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.065</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.03</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">circles =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inches =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">add =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://marberts.github.io/blog/posts/2025/variance-inequality/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>To better understand this, let’s simulate the probability that the difference between the arithmetic and geometric mean decreases with variance, conditional on Equation&nbsp;1 and Equation&nbsp;2 not holding.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_var_ineq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, \(n) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rlnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sdlog =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prob_decrease</span>()</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(res)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.000   4.400   6.340   6.562   8.020  35.160 </code></pre>
</div>
</div>
<p>Overall, the median of the conditional probability that the difference between the arithmetic and geometric mean decreases with variance is about 6.3%. This is a fairly small probability for the difference between the means to decrease when variance increases, although it depends on the underlying population from which samples are drawn. Drawing samples from a distribution with a wider support makes it more likely to see the difference between the arithmetic and geometric mean decreases with variance.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_var_ineq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, \(n) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(n, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prob_decrease</span>()</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(res)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.020   8.515  13.400  13.579  17.100  50.580 </code></pre>
</div>
</div>
<p>As the support of the distribution shrinks, the difference between the arithmetic and geometric mean follows the variance more closely and the probability that the difference in means decreases as variance increases becomes small.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">simulate_var_ineq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5000</span>, \(n) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(n, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prob_decrease</span>()</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(res)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  0.000   0.800   1.140   1.194   1.500   4.560 </code></pre>
</div>
</div>





<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-bullen2003" class="csl-entry">
Bullen, P. S. 2003. <em>Handbook of Means and Their Inequalities</em>. Springer Science+Business Media. <a href="https://doi.org/10.1007/978-94-017-0399-4">https://doi.org/10.1007/978-94-017-0399-4</a>.
</div>
<div id="ref-cpi2020" class="csl-entry">
IMF, ILO, Eurostat, UNECE, OECD, and World Bank. 2020. <em>Consumer Price Index Manual: Concepts and Methods</em>. International Monetary Fund. <a href="https://doi.org/10.5089/9781484354841.069 ">https://doi.org/10.5089/9781484354841.069 </a>.
</div>
<div id="ref-cpitheory" class="csl-entry">
IMF, ILO, Eurostat, UNECE, OECD, and World Bank. 2025. <em>Consumer Price Index Manual: Theory</em>. International Monetary Fund. <a href="https://doi.org/10.5089/9781513559605.069">https://doi.org/10.5089/9781513559605.069</a>.
</div>
<div id="ref-lord2002" class="csl-entry">
Lord, N. 2002. <span>“Does Smaller Spread Always Mean Larger Product?”</span> <em>The Mathematical Gazette</em>, nos. 506, 86: 273–74. <a href="https://doi.org/10.2307/3621857">https://doi.org/10.2307/3621857</a>.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>The CPI manual is more precise on page 179 and notes that the difference between the arithmetic and geometric mean <em>tends</em> to increase with variance.↩︎</p></li>
<li id="fn2"><p>This can also apply to the difference between the arithmetic mean and harmonic mean, where it is often suggested that the difference between these means increases with variance <span class="citation" data-cites="cpitheory">(IMF et al. 2025, 131)</span>.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{martin2025,
  author = {Martin, Steve},
  title = {How Does the Difference Between the Arithmetic and Geometric
    Mean Depend on Variance?},
  date = {2025-11-12},
  url = {https://marberts.github.io/blog/posts/2025/variance-inequality/},
  doi = {10.59350/rpvm0-57912},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-martin2025" class="csl-entry quarto-appendix-citeas">
Martin, Steve. 2025. <span>“How Does the Difference Between the
Arithmetic and Geometric Mean Depend on Variance?”</span> November 12.
<a href="https://doi.org/10.59350/rpvm0-57912">https://doi.org/10.59350/rpvm0-57912</a>.
</div></div></section></div> ]]></description>
  <category>Inequalities</category>
  <category>R</category>
  <guid>https://marberts.github.io/blog/posts/2025/variance-inequality/</guid>
  <pubDate>Wed, 12 Nov 2025 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Hedonic imputation with one regression, not two</title>
  <dc:creator>Steve Martin</dc:creator>
  <link>https://marberts.github.io/blog/posts/2025/hedonics/</link>
  <description><![CDATA[ 





<p>Comparing housing prices over time is difficult because of all the heterogeneity in housing that can both affect prices and vary over time. The typical solution to this problem is to use a hedonic price index that attempts to model the relationship between housing characteristics and price, and uses this to control the change in these characteristics over time.</p>
<p>Although there are several approaches to building a hedonic price index, the double imputation approach is usually seen as the best. Every presentation of this method that I have seen calculates the index by fitting two regression models and combining the fitted values from these models in a geometric index <span class="citation" data-cites="aizcorbe2014 rppi2013 rppi2020">(e.g., Aizcorbe 2014; ILO et al. 2013; IMF 2020)</span>. What I want to show here is that the double imputation index can be recovered from the coefficient in a linear regression. This makes it simpler to calculate the index and provides an easy strategy to estimate conventional standard errors.</p>
<section id="turning-two-regressions-into-one" class="level2">
<h2 class="anchored" data-anchor-id="turning-two-regressions-into-one">Turning two regressions into one</h2>
<p>The hedonic imputation model starts with two linear models, one for transactions in period 0,</p>
<p><span id="eq-laspeyres"><img src="https://latex.codecogs.com/png.latex?%0A%5Clog(p_%7Bi0%7D)%20=%20%5Calpha_%7B0%7D%20+%20x_%7Bi0%7D%20%5Cbeta_%7B0%7D%20+%20u_%7Bi0%7D,%0A%5Ctag%7B1%7D"></span></p>
<p>and one for transactions in period 1,</p>
<p><span id="eq-paasche"><img src="https://latex.codecogs.com/png.latex?%0A%5Clog(p_%7Bi1%7D)%20=%20%5Calpha_%7B1%7D%20+%20x_%7Bi1%7D%20%5Cbeta_%7B1%7D%20+%20e_%7Bi1%7D,%0A%5Ctag%7B2%7D"></span></p>
<p>where <img src="https://latex.codecogs.com/png.latex?x_%7Bit%7D"> is a vector of characteristics that confound a change in price over time with a change in the composition of housing.</p>
<p>The Laspeyres hedonic imputation index, <img src="https://latex.codecogs.com/png.latex?I">, compares the predicted prices from Equation&nbsp;2 against those from Equation&nbsp;1 over the distribution of characteristics in period 0</p>
<p><span id="eq-lhi"><img src="https://latex.codecogs.com/png.latex?%0A%5Clog(I)%20=%20%5Calpha_%7B1%7D%20-%20%5Calpha_%7B0%7D%20+%20%5Cbar%7Bx%7D_%7B0%7D%20(%5Cbeta_%7B1%7D%20-%20%5Cbeta_%7B0%7D),%0A%5Ctag%7B3%7D"></span></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D_%7B0%7D"> is the vector of average period 0 characteristics. This is equation 5.19 in <span class="citation" data-cites="rppi2013">ILO et al. (2013)</span>.</p>
<p>Equation&nbsp;1 and Equation&nbsp;2 can be nested to get a single linear model. Subtracting <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D_%7B0%7D"> from the interaction term then gives Equation&nbsp;3 as a coefficient on a time dummy variable</p>
<img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Balign%7D%0A%5Clog(p_%7Bit%7D)%20&amp;=%20%5Calpha_%7B0%7D%20+%20x_%7Bit%7D%20%5Cbeta_%7B0%7D%20+%20t%20(%5Calpha_%7B1%7D%20-%20%5Calpha_%7B0%7D%20+%20x_%7Bit%7D%20(%5Cbeta_%7B1%7D%20-%20%5Cbeta_%7B0%7D))%20+%20u_%7Bit%7D%20+%20t(e_%7Bit%7D%20-%20u_%7Bit%7D)%5C%5C%0A%0A&amp;=%20%5Calpha_%7B0%7D%20+%20t%20%5Clog(I)%20+%20x_%7Bit%7D%20%5Cbeta_%7B0%7D%20+%20t%20(x_%7Bit%7D%20-%20%5Cbar%7Bx%7D_%7B0%7D)%20(%5Cbeta_%7B1%7D%20-%20%5Cbeta_%7B0%7D)%20+%20%5Cvarepsilon_%7Bit%7D,%0A%5Cend%7Balign%7D">
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon_%7Bit%7D%20=%20u_%7Bit%7D%20+%20t(e_%7Bit%7D%20-%20u_%7Bit%7D)">. The Paasche hedonic imputation index follows along the same lines, replacing <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D_%7B0%7D"> with <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D_%7B1%7D">. The Fisher hedonic imputation index combines these two, which is the same as replacing <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D_%7B0%7D"> with <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D_%7B0%7D%20/%202%20+%20%5Cbar%7Bx%7D_%7B1%7D%20/%202">. Note that this reduces to the classic time-dummy hedonic index when <img src="https://latex.codecogs.com/png.latex?%5Cbeta_%7B1%7D%20=%20%5Cbeta_%7B0%7D">.<sup>1</sup></p>
</section>
<section id="an-example" class="level2">
<h2 class="anchored" data-anchor-id="an-example">An example</h2>
<p>Let’s go through an example to see this in action. As the goal is just to give an example, I’ll make up some typical-looking transaction data for property sales with a few characteristics over two periods.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15243</span>)</span>
<span id="cb1-2"></span>
<span id="cb1-3">sales <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">period =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>)),</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">price =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rlnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>),</span>
<span id="cb1-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">area =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2500</span>),</span>
<span id="cb1-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">amenity_score =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)</span>
<span id="cb1-9">)</span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(sales)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  period     price      area age amenity_score
1      0 1.8931980 2415.7592  70    0.01447159
2      0 0.9737266 2244.8349   5    0.59602963
3      0 0.9860096 1937.4438  80    0.39965375
4      0 1.4830738  989.7379  66    0.16038727
5      0 0.3651522 2324.3656  50    0.82509594
6      0 0.7955844 1930.3795  12    0.38836734</code></pre>
</div>
</div>
<p>We’ll work with the following basic model relating price with characteristics in each period</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Clog(%5Ctext%7Bprice%7D_%7Bit%7D)%20=%20%5Calpha_%7Bt%7D%20+%20%5Cbeta_%7B1t%7D%5Ctext%7Bage%7D_%7Bit%7D%20+%20%5Cbeta_%7B2t%7D%5Ctext%7Barea%7D_%7Bit%7D%20+%20%5Cbeta_%7Bi3%7D%5Ctext%7Barea%7D_%7Bit%7D%5E2%20+%20%5Cbeta_%7Bi4%7D%5Ctext%7Bneighbourhoodscore%7D_%7Bit%7D%20+%20%5Cvarepsilon_%7Bit%7D.%0A"></p>
<p>The two-regression approach for making the Laspeyres-imputation index fits two separate models for each time period and compares the average ratio of predicted prices over the distribution of housing characteristics for sales in period 0.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">sales2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(sales, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>period)</span>
<span id="cb3-2"></span>
<span id="cb3-3">mdl1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(price) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> area <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(area<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> amenity_score, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])</span>
<span id="cb3-4"></span>
<span id="cb3-5">mdl2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(price) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> area <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(area<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> amenity_score, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]])</span>
<span id="cb3-6"></span>
<span id="cb3-7">laspeyres <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mdl2, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mdl1, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])))</span>
<span id="cb3-8"></span>
<span id="cb3-9">laspeyres</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.9367763</code></pre>
</div>
</div>
<p>The Paasche-imputation index does the same thing, just over the distribution of characteristics for sales in period 1, and the Fisher-imputation index combines the Laspeyres and Paasche indexes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">paasche <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mdl2, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mdl1, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]])))</span>
<span id="cb5-2"></span>
<span id="cb5-3">paasche</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.9376688</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">fisher <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(laspeyres <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> paasche)</span>
<span id="cb7-2"></span>
<span id="cb7-3">fisher</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.9372224</code></pre>
</div>
</div>
<p>To make these indexes with one regression we need to remove the appropriate mean from the characteristics variables and interact them with the time dummy in the linear model. In each case the coefficient on the time dummy variable gives the same index as using two regressions. For simplicity, I’ll just show the case of the Fisher index.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">dm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb9-2">  m0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substitute</span>(period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parent.frame</span>())])</span>
<span id="cb9-3">  m1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substitute</span>(period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parent.frame</span>())])</span>
<span id="cb9-4">  x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(m0, m1))</span>
<span id="cb9-5">}</span>
<span id="cb9-6"></span>
<span id="cb9-7">mdl_fisher <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(</span>
<span id="cb9-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(price) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-9">    age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-10">    area <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(area<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-12">    amenity_score <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-13">    period<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm</span>(age) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm</span>(area) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm</span>(area<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm</span>(amenity_score)),</span>
<span id="cb9-14">  sales</span>
<span id="cb9-15">)</span>
<span id="cb9-16"></span>
<span id="cb9-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all.equal</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(mdl_fisher)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"period"</span>]), fisher, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">check.attributes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div>
<p>Representing the Fisher index as the coefficient in a linear model also makes it easy to get the (delta method) standard error for the index. <sup>2</sup></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(mdl_fisher)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"period"</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(sandwich<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vcovHC</span>(mdl_fisher)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   period 
0.1009944 </code></pre>
</div>
</div>
<p>Adding weights to both the regression model and the index-number formulas is a simple extension. Continuing with the above example, suppose we want to weight by house price so that more expensive houses received a larger weight.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">mdl1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(</span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(price) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> area <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(area<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> amenity_score,</span>
<span id="cb13-3">  sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]],</span>
<span id="cb13-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> price</span>
<span id="cb13-5">)</span>
<span id="cb13-6"></span>
<span id="cb13-7">mdl2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(</span>
<span id="cb13-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(price) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> area <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(area<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> amenity_score,</span>
<span id="cb13-9">  sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]],</span>
<span id="cb13-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> price</span>
<span id="cb13-11">)</span>
<span id="cb13-12"></span>
<span id="cb13-13">laspeyres <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(</span>
<span id="cb13-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">weighted.mean</span>(</span>
<span id="cb13-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mdl2, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mdl1, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]),</span>
<span id="cb13-16">    sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>price</span>
<span id="cb13-17">  )</span>
<span id="cb13-18">)</span>
<span id="cb13-19"></span>
<span id="cb13-20">paasche <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(</span>
<span id="cb13-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">weighted.mean</span>(</span>
<span id="cb13-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mdl2, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mdl1, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]]),</span>
<span id="cb13-23">    sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>price</span>
<span id="cb13-24">  )</span>
<span id="cb13-25">)</span>
<span id="cb13-26"></span>
<span id="cb13-27">fisher <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(laspeyres <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> paasche)</span></code></pre></div></div>
</div>
<p>Making the Fisher index with one regression just requires removing the weighted mean from the characteristics variables in the interaction term.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">dm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb14-2">  m0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eval</span>(</span>
<span id="cb14-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substitute</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">weighted.mean</span>(x[period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], price[period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])),</span>
<span id="cb14-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parent.frame</span>()</span>
<span id="cb14-5">  )</span>
<span id="cb14-6">  m1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eval</span>(</span>
<span id="cb14-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substitute</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">weighted.mean</span>(x[period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], price[period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])),</span>
<span id="cb14-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parent.frame</span>()</span>
<span id="cb14-9">  )</span>
<span id="cb14-10">  x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(m0, m1))</span>
<span id="cb14-11">}</span>
<span id="cb14-12"></span>
<span id="cb14-13">mdl_fisher <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(</span>
<span id="cb14-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(price) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> period <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-15">    age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-16">    area <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(area<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-18">    amenity_score <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-19">    period<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm</span>(age) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm</span>(area) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm</span>(area<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dm</span>(amenity_score)),</span>
<span id="cb14-20">  sales,</span>
<span id="cb14-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> price</span>
<span id="cb14-22">)</span>
<span id="cb14-23"></span>
<span id="cb14-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all.equal</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(mdl_fisher)[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"period"</span>]), fisher, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">check.attributes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div>
</section>
<section id="why-two-regressions-can-be-better" class="level2">
<h2 class="anchored" data-anchor-id="why-two-regressions-can-be-better">Why two regressions can be better</h2>
<p>As noted by <span class="citation" data-cites="aizcorbe2014">Aizcorbe (2014, chap. 3)</span>, constructing the hedonic-imputation index with two regressions is more flexible than recovering an index from a regression coefficient. The trick of doing it with one regression works because the model is linear; using a non-linear regression model (however rare) may require fitting two models separately. Explicitly using the fitted values from two models to make price relatives also allows for the use of other index-number formulas, not just geometric ones.</p>
<p>One reason that I’ve not seen for the two-regression approach is that it’s also more convenient to calculate product contributions when doing two regressions.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">laspeyres_relatives <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(</span>
<span id="cb16-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mdl2, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mdl1, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]])</span>
<span id="cb16-3">)</span>
<span id="cb16-4">contrib_laspeyres <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geometric_contributions</span>(laspeyres_relatives)</span>
<span id="cb16-5"></span>
<span id="cb16-6">paasche_relatives <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(</span>
<span id="cb16-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mdl2, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mdl1, sales2[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]])</span>
<span id="cb16-8">)</span>
<span id="cb16-9">contrib_paasche <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geometric_contributions</span>(paasche_relatives)</span>
<span id="cb16-10"></span>
<span id="cb16-11">contrib_fisher <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Map</span>(</span>
<span id="cb16-12">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">*</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,</span>
<span id="cb16-13">  gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transmute_weights</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(laspeyres, paasche)),</span>
<span id="cb16-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(contrib_laspeyres, contrib_paasche)</span>
<span id="cb16-15">)</span>
<span id="cb16-16"></span>
<span id="cb16-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(</span>
<span id="cb16-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">density</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(contrib_fisher)),</span>
<span id="cb16-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Distribution of sales contributions"</span></span>
<span id="cb16-20">)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://marberts.github.io/blog/posts/2025/hedonics/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>



</section>


<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-aizcorbe2014" class="csl-entry">
Aizcorbe, Ana M. 2014. <em>A Practical Guide to Price Index and Hedonic Techniques</em>. Oxford University Press. <a href="https://doi.org/10.1093/acprof:oso/9780198702429.001.0001">https://doi.org/10.1093/acprof:oso/9780198702429.001.0001</a>.
</div>
<div id="ref-rppi2013" class="csl-entry">
ILO, IMF, OECD, UNECE, and World Bank. 2013. <em>Handbook on Residential Property Prices Indices (RPPIs)</em>. Eurostat. <a href="https://doi.org/10.2785/34007">https://doi.org/10.2785/34007</a>.
</div>
<div id="ref-rppi2020" class="csl-entry">
IMF. 2020. <em>Residential Property Prices Index (RPPI) Practical Compilation Guide</em>. International Monetary Fund. <a href="https://www.imf.org/en/Data/Statistics/RPPI-guide">https://www.imf.org/en/Data/Statistics/RPPI-guide</a>.
</div>
<div id="ref-wooldridge2002" class="csl-entry">
Wooldridge, Jeffrey M. 2002. <em>Econometric Analysis of Cross Section and Panel Data</em>. MIT Press.
</div>
</div></section><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>This type of regression model comes up in the literature on program evaluation as a way to estimate average treatment effects; see, e.g., <span class="citation" data-cites="wooldridge2002">Wooldridge (2002, sec. 18.3.1)</span>.↩︎</p></li>
<li id="fn2"><p>These standard error are approximate when the mean of the characteristics is calculated from the sample of data used to the fit the model.↩︎</p></li>
</ol>
</section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{martin2025,
  author = {Martin, Steve},
  title = {Hedonic Imputation with One Regression, Not Two},
  date = {2025-10-27},
  url = {https://marberts.github.io/blog/posts/2025/hedonics/},
  doi = {10.59350/2f0dq-zvs49},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-martin2025" class="csl-entry quarto-appendix-citeas">
Martin, Steve. 2025. <span>“Hedonic Imputation with One Regression, Not
Two.”</span> October 27. <a href="https://doi.org/10.59350/2f0dq-zvs49">https://doi.org/10.59350/2f0dq-zvs49</a>.
</div></div></section></div> ]]></description>
  <category>Index numbers</category>
  <category>Econometrics</category>
  <category>R</category>
  <guid>https://marberts.github.io/blog/posts/2025/hedonics/</guid>
  <pubDate>Mon, 27 Oct 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>What causes inflation?</title>
  <dc:creator>Steve Martin</dc:creator>
  <link>https://marberts.github.io/blog/posts/2025/inflation/</link>
  <description><![CDATA[ 





<p>What causes inflation in prices over time? Increases in the money supply, of course. Although there are many reasons for increasing prices over time, money supply is one of the few things that can do so and grow without bound. Case closed, right? Not quite.</p>
<p>In practice we can’t perfectly observe how prices change over time and instead we must measure inflation with an index number. Ignoring all the details that arise in practice, the goal is usually to construct a chained Laspeyres or Fisher index. There’s a nice disclaimer in the CPI manual about potential issues with these formulas when prices bounce or oscillate, rather than grow monotonically <span class="citation" data-cites="cpitheory">(IMF et al. 2025, 24)</span>. What I want to show here is that this can be a source of perpetual inflation. This means that, for example, increases and decreases in market concentration over time, which would result in fluctuating prices, can result a price index measuring inflation, even when prices don’t cumulatively change over time.</p>
<section id="setup" class="level2">
<h2 class="anchored" data-anchor-id="setup">Setup</h2>
<p>Let’s start with a simple two-commodity model where a representative consumer has Cobb-Douglas preferences. We’ll make a small class and mixin to make a Laspeyres and Fisher index given prices based on these preferences.</p>
<div id="9ebff752" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-3"></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> IndexMixin:</span>
<span id="cb1-6">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> laspeyres(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, p1: np.ndarray, p0: np.ndarray) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-7">        q0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.demand(p0)</span>
<span id="cb1-8">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(p1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(p0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q0)</span>
<span id="cb1-9"></span>
<span id="cb1-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> paasche(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, p1: np.ndarray, p0: np.ndarray) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-11">        q1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.demand(p1)</span>
<span id="cb1-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(p1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q1) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(p0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q1)</span>
<span id="cb1-13"></span>
<span id="cb1-14">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> fisher(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, p1: np.ndarray, p0: np.ndarray) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>:</span>
<span id="cb1-15">        laspeyres <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.laspeyres(p1, p0)</span>
<span id="cb1-16">        paasche <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.paasche(p1, p0)</span>
<span id="cb1-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.sqrt(laspeyres <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> paasche)</span>
<span id="cb1-18"></span>
<span id="cb1-19"></span>
<span id="cb1-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> CobbDouglas(IndexMixin):</span>
<span id="cb1-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, alpha: np.ndarray, m: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>):</span>
<span id="cb1-22">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.asarray(alpha, dtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>np.float64)</span>
<span id="cb1-23">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._alpha)</span>
<span id="cb1-24">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(m)</span>
<span id="cb1-25"></span>
<span id="cb1-26">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> demand(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, p: np.ndarray) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> np.ndarray:</span>
<span id="cb1-27">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(p) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._alpha):</span>
<span id="cb1-28">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"must supply prices for </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._alpha)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> products"</span>)</span>
<span id="cb1-29">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p</span></code></pre></div></div>
</div>
<p>We’ll also make a function to oscillate prices. The idea is to form a chain of index values that starts with both products having a price of <code>[1, 1]</code>, switches prices between <code>[1, 1.1]</code> and <code>[1.1, 1]</code> some number of times, and ends with prices back at <code>[1, 1]</code>. As a chained index is just the cumulative product of these index values, we can decompose the chained index into the product of the first and last change in price, and the product of the oscillations. What’s necessary to drive inflation is that the product of the oscillations is greater than 1.</p>
<p>We’ll also make a second function that does a more complex oscillation that we’ll use later.</p>
<div id="a0c139e5" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> bounce(index) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">tuple</span>[np.float64]:</span>
<span id="cb2-2">    initial <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-3">    p1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>]</span>
<span id="cb2-4">    p2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-5">    i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> index(p1, initial) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> index(initial, p1)</span>
<span id="cb2-6">    b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> index(p2, p1) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> index(p1, p2)</span>
<span id="cb2-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> i, b</span>
<span id="cb2-8"></span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> bounce2(index) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">tuple</span>[np.float64]:</span>
<span id="cb2-11">    initial <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-12">    p1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>]</span>
<span id="cb2-13">    p2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-14">    p3 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-15">    i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> index(p1, initial) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> index(initial, p1)</span>
<span id="cb2-16">    b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> index(p2, p1) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> index(p3, p2) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> index(p1, p3)</span>
<span id="cb2-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> i, b</span></code></pre></div></div>
</div>
</section>
<section id="inflation-due-to-measurement" class="level2">
<h2 class="anchored" data-anchor-id="inflation-due-to-measurement">Inflation due to measurement</h2>
<p>Let’s start by generating some preferences and giving our representative consumer a fixed income, then simulating how the chained Laspeyres index evolves as prices bounce around.</p>
<div id="696c094c" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CobbDouglas([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb3-2"></span>
<span id="cb3-3">res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bounce(u.laspeyres)</span>
<span id="cb3-4"></span>
<span id="cb3-5">oscillate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [res[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> res[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span>n <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)]</span>
<span id="cb3-6"></span>
<span id="cb3-7">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots()</span>
<span id="cb3-8">ax.plot(oscillate)</span>
<span id="cb3-9">plt.xticks(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb3-10">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://marberts.github.io/blog/posts/2025/inflation/index_files/figure-html/cell-4-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Here we see that, despite prices starting and ending at the same level, the chained Laspeyres index shows that prices have increased over time. This happens because each oscillation registers an increase in prices (about 0.8%), and these compound to show that prices are increasing over time. Note that the culprit here is the well-known substitution bias in the Laspeyres index; if there was no substitution bias then the index would be transitive with Cobb-Douglas preferences and (correctly) show no change in prices over time. Instead, this happens with Leontief preferences.</p>
<div id="25f5a889" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Leontief(IndexMixin):</span>
<span id="cb4-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, m: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>):</span>
<span id="cb4-3">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(m)</span>
<span id="cb4-4"></span>
<span id="cb4-5">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> demand(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, p: np.ndarray) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-&gt;</span> np.ndarray:</span>
<span id="cb4-6">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> np.repeat(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>._m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(p), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(p))</span>
<span id="cb4-7"></span>
<span id="cb4-8">bounce(Leontief(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>).laspeyres)</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="4">
<pre><code>(np.float64(1.0), np.float64(1.0))</code></pre>
</div>
</div>
<p>Both components of the chained index—the first and last change in price and the part due to oscillations—show no change in price.</p>
<p>We can get the same behavior from the Fisher index, albeit with a more complex form of oscillation. (Using the simple oscillation would show no change in prices over time because the Fisher index satisfies the time-reversal property.)</p>
<div id="6f17e714" class="cell" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1">res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bounce2(u.fisher)</span>
<span id="cb6-2"></span>
<span id="cb6-3">oscillate <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [res[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> res[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> n <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> n <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)]</span>
<span id="cb6-4"></span>
<span id="cb6-5">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots()</span>
<span id="cb6-6">ax.plot(oscillate)</span>
<span id="cb6-7">plt.xticks(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb6-8">plt.show()</span></code></pre></div></div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://marberts.github.io/blog/posts/2025/inflation/index_files/figure-html/cell-6-output-1.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<p>Each oscillation registers a 0.02% increase in prices. Note that this behavior from the Fisher index is sensitive to the parameters in the Cobb-Douglas utility function and it’s also possible to get that prices decreases over time.</p>
<div id="d51659d7" class="cell" data-execution_count="6">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CobbDouglas([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb7-2">res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> bounce2(u.fisher)</span>
<span id="cb7-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(res)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>(np.float64(1.0), np.float64(0.9998308882582081))</code></pre>
</div>
</div>
<p>Overall, this example shows that more than just money supply can drive our measurement of inflation, even if increasing money supply is the only true cause of inflation.</p>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-cpitheory" class="csl-entry">
IMF, ILO, Eurostat, UNECE, OECD, and World Bank. 2025. <em>Consumer Price Index Manual: Theory</em>. International Monetary Fund. <a href="https://doi.org/10.5089/9781513559605.069">https://doi.org/10.5089/9781513559605.069</a>.
</div>
</div></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{martin2025,
  author = {Martin, Steve},
  title = {What Causes Inflation?},
  date = {2025-10-22},
  url = {https://marberts.github.io/blog/posts/2025/inflation/},
  doi = {10.59350/nag8p-v2n95},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-martin2025" class="csl-entry quarto-appendix-citeas">
Martin, Steve. 2025. <span>“What Causes Inflation?”</span> October 22.
<a href="https://doi.org/10.59350/nag8p-v2n95">https://doi.org/10.59350/nag8p-v2n95</a>.
</div></div></section></div> ]]></description>
  <category>Index numbers</category>
  <category>Python</category>
  <guid>https://marberts.github.io/blog/posts/2025/inflation/</guid>
  <pubDate>Wed, 22 Oct 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Decomposing diversity indexes</title>
  <dc:creator>Steve Martin</dc:creator>
  <link>https://marberts.github.io/blog/posts/2025/diversity/</link>
  <description><![CDATA[ 





<p>A diversity index is a way to measure the prevalence of different species in an ecosystem. Although these arise naturally in ecology, diversity indexes show up elsewhere as well. In economics, for example, we can think of diversity as related to the market concentration of firms (species) in an industry (ecosystem). What I want to show here is how we can use some of the machinery from the world of price and quantity indexes to decompose a diversity index into the contribution of each species towards overall diversity.</p>
<section id="what-is-diversity-anyway" class="level2">
<h2 class="anchored" data-anchor-id="what-is-diversity-anyway">What is diversity, anyway?</h2>
<p>Diversity is a tricky concept to formalize. Up until writing this post, I would have said it was just the number of species in an ecosystem. In his seminal paper, <span class="citation" data-cites="hill">Hill (1973)</span> considers a family diversity indexes that give a measure of the effective number of species in an ecosystem—that is, the number of species that would be present in an ecosystem if all species were equally prevalent. Mathemetically, if there are <img src="https://latex.codecogs.com/png.latex?n"> species and the <img src="https://latex.codecogs.com/png.latex?i">-th species appears with probability <img src="https://latex.codecogs.com/png.latex?p_%7Bi%7D">, then the diversity index of order <img src="https://latex.codecogs.com/png.latex?%5Calpha"> is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AN_%7B%5Calpha%7D(p_%7B1%7D,%20%5Cldots,%20p_%7Bn%7D)%20=%20%5Cleft(%5Csum_%7Bi=1%7D%5E%7Bn%7Dp_%7Bi%7D%5E%7B%5Calpha%7D%5Cright)%5E%7B%5Cfrac%7B1%7D%7B1%20-%20%5Calpha%7D%7D.%0A"></p>
<p>Different values for <img src="https://latex.codecogs.com/png.latex?%5Calpha"> yield different indexes; for example, <img src="https://latex.codecogs.com/png.latex?N_%7B0%7D"> measures diversity by the total number of species <img src="https://latex.codecogs.com/png.latex?n">, also known as the richness of the ecosystem. As a function of <img src="https://latex.codecogs.com/png.latex?%5Calpha">, <img src="https://latex.codecogs.com/png.latex?N_%7B%5Calpha%7D"> is a continuously decreasing function that maps values in <img src="https://latex.codecogs.com/png.latex?%5B0,%20%5Cinfty)"> onto <img src="https://latex.codecogs.com/png.latex?%5Bn,%201%20/%20%5Cmax(p_%7B1%7D,%20%5Cldots,%20p_%7Bn%7D))">. We can see this with an example of an ecosystem with 10 species.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">diversity_index <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, alpha) {</span>
<span id="cb1-2">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb1-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>alpha)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha))</span>
<span id="cb1-4">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb1-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(p)))</span>
<span id="cb1-6">  }</span>
<span id="cb1-7">}</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">54321</span>)</span>
<span id="cb1-10"></span>
<span id="cb1-11">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>(gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_weights</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rlnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)))</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Abundance of species in an ecosystem"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://marberts.github.io/blog/posts/2025/diversity/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">alphas <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb2-2">index <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(alphas, \(a) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diversity_index</span>(p, a))</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(</span>
<span id="cb2-5">  alphas,</span>
<span id="cb2-6">  index,</span>
<span id="cb2-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>),</span>
<span id="cb2-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"𝛼"</span>,</span>
<span id="cb2-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Index"</span>,</span>
<span id="cb2-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Diversity decreases with 𝛼"</span></span>
<span id="cb2-11">)</span>
<span id="cb2-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abline</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(p), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://marberts.github.io/blog/posts/2025/diversity/index_files/figure-html/unnamed-chunk-1-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>With a bit of rearranging, we can see that <img src="https://latex.codecogs.com/png.latex?N_%5Calpha"> is the reciprocal of the generalized mean of <img src="https://latex.codecogs.com/png.latex?(p_%7B1%7D,%20%5Cldots,%20p_%7Bn%7D)"> with these same values as weights</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AN_%7B%5Calpha%7D(p_%7B1%7D,%20%5Cldots,%20p_%7Bn%7D)%20=%201%20/%20%5Cleft(%5Csum_%7Bi=1%7D%5E%7Bn%7D%20p_%7Bi%7D%5E%7B%5Calpha%20-%201%7D%20p_%7Bi%7D%5Cright)%5E%7B%5Cfrac%7B1%7D%7B%5Calpha%20-%201%7D%7D.%0A"></p>
<p>Formulating <img src="https://latex.codecogs.com/png.latex?N_%7B%5Calpha%7D"> as a generalized mean shows a clear link between diversity indexes and concentration indexes, as <img src="https://latex.codecogs.com/png.latex?N_%7B2%7D"> is the reciprocal of the well-known Simpson index (or Herfindahl–Hirschman index if you’re an economist). (It also shows a link with measures of entropy, as <img src="https://latex.codecogs.com/png.latex?%5Clog(N_%7B%5Calpha%7D)"> is a generalization of Shannon entropy.) What makes a diversity index different from a measure of concentration (or entropy) is that it expresses diversity in terms of the effective size of the ecosystem that would give rise to a particular concentration of species if species were all equally abundant. Intuitively, <img src="https://latex.codecogs.com/png.latex?1%20/%20p_%7Bi%7D"> gives the effective size of the ecosystem if all species were as prevalent as species <img src="https://latex.codecogs.com/png.latex?i">. Rather than considering a single species, <img src="https://latex.codecogs.com/png.latex?N_%7Ba%7D"> uses the average abundance across all species to arrive at a measure of diversity.</p>
</section>
<section id="decomposing-diversity" class="level2">
<h2 class="anchored" data-anchor-id="decomposing-diversity">Decomposing diversity</h2>
<p><span class="citation" data-cites="hill">Hill (1973)</span> notes that different choices for <img src="https://latex.codecogs.com/png.latex?%5Calpha"> imply different sensitivities to rare versus abundant species in an ecosystem. Setting <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200"> means that diversity depends only on the number of species, no matter how rare some may be, whereas when <img src="https://latex.codecogs.com/png.latex?%5Calpha%20%5Crightarrow%20%5Cinfty"> then only the most prevalent species influences diversity. We can go one step further by decomposing <img src="https://latex.codecogs.com/png.latex?N_%7B%5Calpha%7D"> so that it’s represented as an arithmetic mean of the effective size of each species <img src="https://latex.codecogs.com/png.latex?1%20/%20p_%7Bi%7D"> and we can see the contribution of each species towards total diversity. We’ll do this by borrowing some of the machinery from price and quantity indexes to derive weights <img src="https://latex.codecogs.com/png.latex?(w_%7B1%7D,%20%5Cldots,%20w_%7Bn%7D)"> such that</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AN_%7B%5Calpha%7D(p_%7B1%7D,%20%5Cldots,%20p_%7Bn%7D)%20=%20%5Csum_%7Bi=1%7D%5E%7Bn%7D%20w_%7Bi%7D%20/%20p_%7Bi%7D.%0A"></p>
<p>The core tool to do this comes from my <a href="https://marberts.github.io/gpindex"><code>{gpindex}</code></a> package.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">diversity_weights <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, alpha) {</span>
<span id="cb3-2">  gpindex<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transmute_weights</span>(alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)(x, x)</span>
<span id="cb3-3">}</span></code></pre></div></div>
</div>
<p>When <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%200">, each species contributes the same amount to overall diversity.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diversity_weights</span>(p, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 1 1 1 1 1 1 1 1 1 1</code></pre>
</div>
</div>
<p>Increasing <img src="https://latex.codecogs.com/png.latex?%5Calpha"> to 2, the measure of diversity decreases and more weight is shifted towards more abundant species.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diversity_weights</span>(p, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 0.02204630 0.02298373 0.04003915 0.04733940 0.05467209 0.07962440
 [7] 0.10013170 0.14336078 0.48334916 1.48242257</code></pre>
</div>
</div>
<p>As alpha becomes larger, rare species get a small weight and contribute little towards overall diversity.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diversity_weights</span>(p, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 4.429620e-05 4.817851e-05 1.481826e-04 2.083463e-04 2.795183e-04
 [6] 6.049503e-04 9.729488e-04 2.068354e-03 3.261787e-02 9.630074e-01</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diversity_weights</span>(p, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 0.004974804 0.005190128 0.009163420 0.010897035 0.012658720 0.018811300
 [7] 0.024058228 0.035722328 0.167085953 1.608432485</code></pre>
</div>
</div>
<p>This gives a different way to view the prevalence of a species, not just by their abundance but by how their abundance contributes towards overall diversity in an ecosystem.</p>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-hill" class="csl-entry">
Hill, Mark O. 1973. <span>“Diversity and Evenness: A Unifying Notation and Its Consequences.”</span> <em>Ecology</em> 54 (2): 427–32. <a href="https://doi.org/10.2307/1934352">https://doi.org/10.2307/1934352</a>.
</div>
</div></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{martin2025,
  author = {Martin, Steve},
  title = {Decomposing Diversity Indexes},
  date = {2025-10-19},
  url = {https://marberts.github.io/blog/posts/2025/diversity/},
  doi = {10.59350/dfntc-yfd90},
  langid = {en}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-martin2025" class="csl-entry quarto-appendix-citeas">
Martin, Steve. 2025. <span>“Decomposing Diversity Indexes.”</span>
October 19. <a href="https://doi.org/10.59350/dfntc-yfd90">https://doi.org/10.59350/dfntc-yfd90</a>.
</div></div></section></div> ]]></description>
  <category>Ecology</category>
  <category>R</category>
  <guid>https://marberts.github.io/blog/posts/2025/diversity/</guid>
  <pubDate>Sun, 19 Oct 2025 04:00:00 GMT</pubDate>
</item>
</channel>
</rss>
