<?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>Pinkney&#39;s Sufficient Stats</title>
<link>https://spinkney.github.io/</link>
<atom:link href="https://spinkney.github.io/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.11.9</generator>
<lastBuildDate>Sun, 29 Nov 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>Frequentist’s are our Friends: A Bayesian’s Hierarchical Story</title>
  <dc:creator>Sean Pinkney</dc:creator>
  <link>https://spinkney.github.io/posts/post-13-s2z-fisher-geometry/s2z-fisher-geometry.html</link>
  <description><![CDATA[ 





<section id="two-communities-one-linear-predictor" class="level2">
<h2 class="anchored" data-anchor-id="two-communities-one-linear-predictor">1. Two Communities, One Linear Predictor</h2>
<p>Write a one-way varying-intercept model for observations <img src="https://latex.codecogs.com/png.latex?i%20=%201,%20%5Cdots,%20N"> across groups <img src="https://latex.codecogs.com/png.latex?j%20=%201,%20%5Cdots,%20J">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay_i%20%5Cmid%20%5Calpha,%20a,%20%5Csigma_y%20%5Csim%20%5Cmathcal%7BN%7D(%5Calpha%20+%20a_%7Bg%5Bi%5D%7D,%20%5Csigma_y%5E2).%0A"></p>
<p>Bayesians and frequentists complete this specification in different ways <span class="citation" data-cites="gelman2005 rouder2012">(Gelman 2005; Rouder et al. 2012)</span>. A Bayesian hierarchical model treats <img src="https://latex.codecogs.com/png.latex?%5Calpha%20%5Csim%20%5Cmathcal%7BN%7D(%5Cmu_0,%20s_%5Calpha%5E2)"> as a superpopulation mean and models the <img src="https://latex.codecogs.com/png.latex?J"> observed groups as exchangeable draws <img src="https://latex.codecogs.com/png.latex?a_j%20%5Cmid%20%5Ctau%20%5Coverset%7B%5Cmathrm%7Biid%7D%7D%7B%5Csim%7D%20%5Cmathcal%7BN%7D(0,%20%5Ctau%5E2)"> from that population. Because <img src="https://latex.codecogs.com/png.latex?J"> is finite, the realized sample average <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Ba%7D%20=%20J%5E%7B-1%7D%5Csum_%7Bj=1%7D%5EJ%20a_j%20%5Csim%20%5Cmathcal%7BN%7D(0,%20%5Ctau%5E2%20/%20J)"> is nonzero almost surely, so the average across the observed groups <img src="https://latex.codecogs.com/png.latex?%5Calpha%20+%20%5Cbar%7Ba%7D"> differs from the superpopulation parameter <img src="https://latex.codecogs.com/png.latex?%5Calpha">. That distinction lets the model predict a new <img src="https://latex.codecogs.com/png.latex?(J+1)">th group, and it creates a sampling bottleneck: every observation depends on <img src="https://latex.codecogs.com/png.latex?(%5Calpha,%20a)"> only through <img src="https://latex.codecogs.com/png.latex?%5Calpha%20+%20a_j">, so adding <img src="https://latex.codecogs.com/png.latex?d"> to <img src="https://latex.codecogs.com/png.latex?%5Calpha"> while subtracting <img src="https://latex.codecogs.com/png.latex?d"> from every <img src="https://latex.codecogs.com/png.latex?a_j"> leaves the likelihood unchanged. Whenever the data speak louder than the prior, Hamiltonian Monte Carlo (HMC) has to move <img src="https://latex.codecogs.com/png.latex?(%5Calpha,%20a)"> back and forth along a narrow <img src="https://latex.codecogs.com/png.latex?(1,%20-%5Cmathbf%7B1%7D_J%5E%5Ctop)"> shift ridge.</p>
<p>Frequentist effects coding avoids that ridge by treating the <img src="https://latex.codecogs.com/png.latex?J"> observed groups as the finite population of interest and imposing a hard sum-to-zero constraint,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctheta%20=%20%5Cfrac%7B1%7D%7BJ%7D%5Csum_%7Bj=1%7D%5EJ%20(%5Calpha%20+%20a_j)%20=%20%5Calpha%20+%20%5Cbar%7Ba%7D,%20%5Cqquad%20c_j%20=%20a_j%20-%20%5Cbar%7Ba%7D,%20%5Cqquad%20%5Csum_%7Bj=1%7D%5EJ%20c_j%20=%200.%0A"></p>
<p>Sweeping <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Ba%7D"> into the intercept removes the redundant shift direction <span class="citation" data-cites="vines1996 ogle2020">(Vines et al. 1996; Ogle and Barber 2020)</span>. As <span class="citation" data-cites="millar2009">Millar et al. (2009)</span> showed, however, placing the original prior <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BN%7D(%5Cmu_0,%20s_%5Calpha%5E2)"> directly on <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> changes the estimand from the superpopulation mean <img src="https://latex.codecogs.com/png.latex?%5Calpha"> to the finite-sample grand mean <img src="https://latex.codecogs.com/png.latex?%5Ctheta">.</p>
<p>In my <a href="https://github.com/spinkney/open-talks/tree/main/sum-to-zero-stancon26">StanCon 2026 talk</a> with Nikolai Vetr, we connected the two parameterizations so a Bayesian sampler can borrow the frequentist zero-sum geometry during HMC while keeping the exact superpopulation posterior.<sup>1</sup> Under the Gaussian prior, the realized group mean <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Ba%7D%20%5Cmid%20%5Ctau%20%5Csim%20%5Cmathcal%7BN%7D(0,%20%5Ctau%5E2%20/%20J)"> is independent of the centered deviations <img src="https://latex.codecogs.com/png.latex?c%20=%20P_0%20a">, where <img src="https://latex.codecogs.com/png.latex?P_0%20=%20I_J%20-%20J%5E%7B-1%7D%5Cmathbf%7B1%7D_J%5Cmathbf%7B1%7D_J%5E%5Ctop"> is the centering projector. Integrating <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Ba%7D"> out of the prior gives the exact marginal prior on the finite-population grand mean,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctheta%20%5Cmid%20%5Ctau%20%5Csim%20%5Cmathcal%7BN%7D%5C!%5Cleft(%5Cmu_0,%5C;%20V_%5Ctheta(%5Ctau)%5Cright),%20%5Cqquad%20V_%5Ctheta(%5Ctau)%20=%20s_%5Calpha%5E2%20+%20%5Cfrac%7B%5Ctau%5E2%7D%7BJ%7D.%0A"></p>
<p>To represent <img src="https://latex.codecogs.com/png.latex?c"> without constructing an explicit basis matrix in <code>transformed data</code>, we can use Stan’s built-in <code>sum_to_zero_constrain()</code> transform. Given an unconstrained vector <img src="https://latex.codecogs.com/png.latex?z_u%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BJ-1%7D">, <code>sum_to_zero_constrain(z_u)</code> applies the <img src="https://latex.codecogs.com/png.latex?O(J)"> isometric Helmert map <img src="https://latex.codecogs.com/png.latex?H:%20%5Cmathbb%7BR%7D%5E%7BJ-1%7D%20%5Cto%20%5C%7Bc%20%5Cin%20%5Cmathbb%7BR%7D%5EJ%20:%20%5Cmathbf%7B1%7D_J%5E%5Ctop%20c%20=%200%5C%7D"> <span class="citation" data-cites="egozcue2003">(Egozcue et al. 2003)</span>, which satisfies <img src="https://latex.codecogs.com/png.latex?H%5E%5Ctop%20H%20=%20I_%7BJ-1%7D">, <img src="https://latex.codecogs.com/png.latex?H%20H%5E%5Ctop%20=%20P_0">, and has unit Jacobian determinant. Putting a standard normal prior directly on the unconstrained <img src="https://latex.codecogs.com/png.latex?(J-1)">-vector <img src="https://latex.codecogs.com/png.latex?z_u%20%5Csim%20%5Cmathcal%7BN%7D(0,%20I_%7BJ-1%7D)"> therefore induces <img src="https://latex.codecogs.com/png.latex?c%20=%20%5Ctau%5C,%5Ctext%7Bsum%5C_to%5C_zero%5C_constrain%7D(z_u)%20%5Csim%20%5Cmathcal%7BN%7D(0,%20%5Ctau%5E2%20P_0)"> with no manual <code>H</code> matrix and no <code>target += log(tau)</code> degree-of-freedom correction. Because <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Ba%7D"> never enters the likelihood, a single conditional Gaussian draw in <code>generated quantities</code> recovers the superpopulation parameters <img src="https://latex.codecogs.com/png.latex?(%5Calpha,%20a)"> draw by draw:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb1-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">&gt;</span> N;</span>
<span id="cb1-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> J;</span>
<span id="cb1-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>J<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> group;</span>
<span id="cb1-5">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] y;</span>
<span id="cb1-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> mu_0;</span>
<span id="cb1-7">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> s_alpha;</span>
<span id="cb1-8">}</span>
<span id="cb1-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb1-10">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta;</span>
<span id="cb1-11">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J <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>] z_u;</span>
<span id="cb1-12">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> tau;</span>
<span id="cb1-13">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> sigma_y;</span>
<span id="cb1-14">}</span>
<span id="cb1-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb1-16">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tau <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;">sum_to_zero_constrain</span>(z_u);</span>
<span id="cb1-17">}</span>
<span id="cb1-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb1-19">  tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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>
<span id="cb1-20">  sigma_y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">2</span>);</span>
<span id="cb1-21">  theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(mu_0, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hypot</span>(s_alpha, tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(J)));</span>
<span id="cb1-22">  z_u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb1-23">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c[group], sigma_y);</span>
<span id="cb1-24">}</span>
<span id="cb1-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb1-26">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> mean_a;</span>
<span id="cb1-27">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> alpha;</span>
<span id="cb1-28">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] a;</span>
<span id="cb1-29">  {</span>
<span id="cb1-30">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> v_bar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(tau) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> J;</span>
<span id="cb1-31">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> v_tot <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(s_alpha) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v_bar;</span>
<span id="cb1-32">    mean_a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> normal_rng(</span>
<span id="cb1-33">      (v_bar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> v_tot) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu_0),</span>
<span id="cb1-34">      <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;">square</span>(s_alpha) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v_bar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> v_tot)</span>
<span id="cb1-35">    );</span>
<span id="cb1-36">  }</span>
<span id="cb1-37">  alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mean_a;</span>
<span id="cb1-38">  a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mean_a;</span>
<span id="cb1-39">}</span></code></pre></div></div>
<p>If we prefer to sample the contrasts on the physical predictor scale (<code>scaled</code> rather than <code>non_centered</code>), we declare <code>vector[J - 1] h_u</code>, set <code>vector[J] c = sum_to_zero_constrain(h_u)</code> without multiplying by <code>tau</code>, and still place the standard normal prior on the whitened unconstrained vector via <code>(h_u / tau) ~ std_normal(); target += -(J - 1) * log(tau);</code>.</p>
<p>In this post I ask what this frequentist-to-Bayesian bridge does to the <strong>Fisher information matrix</strong>: when does sum-to-zero (S2Z) make posterior correlations identically zero, how does the construction extend to richer hierarchical models, and where does correlation remain? Four results organize the rest of the post:</p>
<ol type="1">
<li><strong>Balanced designs give exact Cox–Reid parameter orthogonality; unbalanced designs still gain effective Fisher information in every direction.</strong> Under equal group sample sizes, S2Z diagonalizes the location precision matrix at every prior and variance scale. Under arbitrary unbalanced designs and multiple grouping factors, S2Z increases effective population Fisher information in the <strong>Loewner order</strong> <span class="citation" data-cites="loewner1934">(Löwner 1934)</span>—meaning <img src="https://latex.codecogs.com/png.latex?M_1%20-%20M_2"> is positive semidefinite, so precision is larger or equal along every linear combination of population parameters (Section 3.2)—with one boundary case when a dogmatic prior fixes <img src="https://latex.codecogs.com/png.latex?%5Calpha"> on sparse, unbalanced data (Section 2).</li>
<li><strong>The equivalence extends cleanly to correlated slopes, crossed factors, non-Gaussian priors, and varying group scales.</strong> Section 3 derives each extension in turn with a concrete example and a short Stan implementation built around <code>sum_to_zero_constrain()</code>.</li>
<li><strong>Group-varying standard deviations couple <img src="https://latex.codecogs.com/png.latex?(%5Ctheta,%20h)"> at order <img src="https://latex.codecogs.com/png.latex?O(J%5E%7B-1%7D)"> in the prior while diagonalizing the log-SD hierarchy at homogeneity via Louis’s identity.</strong> When <img src="https://latex.codecogs.com/png.latex?a_j%20%5Csim%20%5Cmathcal%7BN%7D(0,%20s_j%5E2)">, completing the square over <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Ba%7D"> leaves a small heterogeneous prior cross-term, and <strong>Louis’s missing-information identity</strong> <span class="citation" data-cites="louis1982">(Louis 1982)</span>—complete-data Fisher information minus the conditional score variance of the integrated-out mean (Section 3.4)—yields <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BI%7D_%7B%5Ctheta,h%7D(%5Cell)%20=%202I_J%20-%204%5Coperatorname%7Bdiag%7D(%5Cboldsymbol%7B%5Cpi%7D)%20+%202%5Cboldsymbol%7B%5Cpi%7D%5Cboldsymbol%7B%5Cpi%7D%5E%5Ctop">.</li>
<li><strong>Across the six Eight Schools parameterizations, rotating into the zero-sum basis matters as much as integrating out <img src="https://latex.codecogs.com/png.latex?%5Cmu">, strong data favor physical contrasts, and per-group partial centering handles mixed sample sizes.</strong> Section 4 derives all six Fisher matrices explicitly, showing why <code>centered_s2z</code> remains equicorrelated under balanced SEs and why physical S2Z (<code>scaled_s2z</code>) avoids a strong-data <img src="https://latex.codecogs.com/png.latex?(%5Clog%5Ctau,%20z)"> hyperbola that slows <code>non_centered_s2z</code>. Section 4.3 then combines per-group partial centering with <code>sum_to_zero_constrain()</code> using frequentist Fisher-reliability weights when weak and dense groups coexist in the same dataset.</li>
</ol>
<p>All 55 linear-algebraic, Fisher-information, Louis missing-information, and partial-centering theorems in Sections 2–4 are formalized in Lean 4 (<code>v4.34.0</code> with Mathlib <code>v4.34.0</code>), and every analytic Hessian below matches numerical second derivatives in R within <img src="https://latex.codecogs.com/png.latex?8%20%5Ctimes%2010%5E%7B-8%7D">.</p>
</section>
<section id="two-groups-one-ridge-and-the-dogmatic-prior-boundary" class="level2">
<h2 class="anchored" data-anchor-id="two-groups-one-ridge-and-the-dogmatic-prior-boundary">2. Two Groups, One Ridge, and the Dogmatic-Prior Boundary</h2>
<p>The <img src="https://latex.codecogs.com/png.latex?J%20=%202"> case with unit residual variance (<img src="https://latex.codecogs.com/png.latex?%5Csigma_y%20=%201">) shows the core geometry in <img src="https://latex.codecogs.com/png.latex?2%20%5Ctimes%202"> and <img src="https://latex.codecogs.com/png.latex?3%20%5Ctimes%203"> matrices. Suppose group <img src="https://latex.codecogs.com/png.latex?j%20%5Cin%20%5C%7B1,%202%5C%7D"> has <img src="https://latex.codecogs.com/png.latex?n_j"> observations (<img src="https://latex.codecogs.com/png.latex?N%20=%20n_1%20+%20n_2">), so its sample mean <img src="https://latex.codecogs.com/png.latex?%5Cbar%7By%7D_j%20%5Csim%20%5Cmathcal%7BN%7D(q_j,%20n_j%5E%7B-1%7D)"> contributes Fisher weight <img src="https://latex.codecogs.com/png.latex?n_j">. In conventional physical coordinates (<code>scaled_conv</code>), <img src="https://latex.codecogs.com/png.latex?q_j%20=%20%5Calpha%20+%20a_j"> with <img src="https://latex.codecogs.com/png.latex?%5Calpha%20%5Csim%20%5Cmathcal%7BN%7D(0,%20s_%5Calpha%5E2)"> and <img src="https://latex.codecogs.com/png.latex?a_j%20%5Csim%20%5Cmathcal%7BN%7D(0,%20%5Ctau%5E2)">. In physical S2Z (<code>scaled_s2z</code>), <code>sum_to_zero_constrain([h_1])</code> produces <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B1%7D%7B%5Csqrt%7B2%7D%7D(h_1,%20-h_1)%5E%5Ctop">, so <img src="https://latex.codecogs.com/png.latex?q_1%20=%20%5Ctheta%20+%20h_1%20/%20%5Csqrt%7B2%7D"> and <img src="https://latex.codecogs.com/png.latex?q_2%20=%20%5Ctheta%20-%20h_1%20/%20%5Csqrt%7B2%7D"> with <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20%5Csim%20%5Cmathcal%7BN%7D(0,%20s_%5Calpha%5E2%20+%20%5Ctau%5E2%20/%202)"> and <img src="https://latex.codecogs.com/png.latex?h_1%20%5Csim%20%5Cmathcal%7BN%7D(0,%20%5Ctau%5E2)">.</p>
<p>Conditional on <img src="https://latex.codecogs.com/png.latex?%5Ctau">, the two posterior precision matrices are</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_%7B%5Cmathrm%7Bconv%7D%7D(%5Calpha,%20a_1,%20a_2)%20=%0A%5Cbegin%7Bpmatrix%7D%0As_%5Calpha%5E%7B-2%7D%20+%20n_1%20+%20n_2%20&amp;%20n_1%20&amp;%20n_2%20%5C%5C%0An_1%20&amp;%20n_1%20+%20%5Ctau%5E%7B-2%7D%20&amp;%200%20%5C%5C%0An_2%20&amp;%200%20&amp;%20n_2%20+%20%5Ctau%5E%7B-2%7D%0A%5Cend%7Bpmatrix%7D,%0A%5Cqquad%0AP_%7B%5Cmathrm%7Bs2z%7D%7D(%5Ctheta,%20h_1)%20=%0A%5Cbegin%7Bpmatrix%7D%0A(s_%5Calpha%5E2%20+%20%5Ctau%5E2%20/%202)%5E%7B-1%7D%20+%20n_1%20+%20n_2%20&amp;%20%5Cdfrac%7Bn_1%20-%20n_2%7D%7B%5Csqrt%7B2%7D%7D%20%5C%5C%5B8pt%5D%0A%5Cdfrac%7Bn_1%20-%20n_2%7D%7B%5Csqrt%7B2%7D%7D%20&amp;%20%5Cdfrac%7Bn_1%20+%20n_2%7D%7B2%7D%20+%20%5Ctau%5E%7B-2%7D%0A%5Cend%7Bpmatrix%7D.%0A"></p>
<p>The off-diagonal entry of <img src="https://latex.codecogs.com/png.latex?P_%7B%5Cmathrm%7Bs2z%7D%7D"> is <img src="https://latex.codecogs.com/png.latex?(n_1%20-%20n_2)/%5Csqrt%7B2%7D">. Whenever the two groups have equal sample sizes (<img src="https://latex.codecogs.com/png.latex?n_1%20=%20n_2">, a 50% / 50% sample size split), <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> and <img src="https://latex.codecogs.com/png.latex?h_1"> are exact Cox–Reid orthogonal parameters <span class="citation" data-cites="cox1987">(Cox and Reid 1987)</span> at every <img src="https://latex.codecogs.com/png.latex?(%5Ctau,%20s_%5Calpha,%20N)">. In <img src="https://latex.codecogs.com/png.latex?P_%7B%5Cmathrm%7Bconv%7D%7D">, as <img src="https://latex.codecogs.com/png.latex?n_1,%20n_2"> grow, the likelihood block annihilates <img src="https://latex.codecogs.com/png.latex?(1,%20-1,%20-1)%5E%5Ctop"> and squeezes the <img src="https://latex.codecogs.com/png.latex?(%5Calpha,%20a_1)"> posterior ellipse onto the line <img src="https://latex.codecogs.com/png.latex?%5Calpha%20+%20a_1%20=%20%5Cbar%7By%7D_1">.</p>
<p>When <img src="https://latex.codecogs.com/png.latex?n_1%20%5Cne%20n_2">, integrating out the group effects reduces the intercept’s marginal precision from the top-left entry <img src="https://latex.codecogs.com/png.latex?a"> to the Schur complement <img src="https://latex.codecogs.com/png.latex?a%20-%20b%5E%5Ctop%20D%5E%7B-1%7D%20b">. Here <img src="https://latex.codecogs.com/png.latex?b%5E%5Ctop%20D%5E%7B-1%7D%20b"> is the scalar case of the cross-information penalty ordered by Loewner dominance in Section 3.2; for <img src="https://latex.codecogs.com/png.latex?J%20=%202">, their difference is nonnegative for all <img src="https://latex.codecogs.com/png.latex?n_1,%20n_2%20%5Cge%200"> and <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%3E%200">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cunderbrace%7B%5Cfrac%7Bn_1%5E2%7D%7Bn_1%20+%20%5Ctau%5E%7B-2%7D%7D%20+%20%5Cfrac%7Bn_2%5E2%7D%7Bn_2%20+%20%5Ctau%5E%7B-2%7D%7D%7D_%7Bb_%7B%5Cmathrm%7Bconv%7D%7D%5E%5Ctop%20D_%7B%5Cmathrm%7Bconv%7D%7D%5E%7B-1%7D%20b_%7B%5Cmathrm%7Bconv%7D%7D%7D%0A%5C;-%5C;%0A%5Cunderbrace%7B%5Cfrac%7B(n_1%20-%20n_2)%5E2%20/%202%7D%7B(n_1%20+%20n_2)/2%20+%20%5Ctau%5E%7B-2%7D%7D%7D_%7Bb_%7B%5Cmathrm%7Bs2z%7D%7D%5E%5Ctop%20D_%7B%5Cmathrm%7Bs2z%7D%7D%5E%7B-1%7D%20b_%7B%5Cmathrm%7Bs2z%7D%7D%7D%0A%5C;=%5C;%0A%5Cfrac%7B%5Ctau%5E%7B-4%7D(n_1%20+%20n_2)%20+%202%20n_1%20n_2%20(n_1%20+%20n_2%20+%20%5Ctau%5E%7B-2%7D)%7D%7B(n_1%20+%20%5Ctau%5E%7B-2%7D)(n_2%20+%20%5Ctau%5E%7B-2%7D)%5Cbig((n_1%20+%20n_2)/2%20+%20%5Ctau%5E%7B-2%7D%5Cbig)%7D%20%5Cge%200.%0A"></p>
<p>At the extreme split <img src="https://latex.codecogs.com/png.latex?n%20=%20(1,%200)"> and <img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%201">, <img src="https://latex.codecogs.com/png.latex?b_%7B%5Cmathrm%7Bconv%7D%7D%5E%5Ctop%20D_%7B%5Cmathrm%7Bconv%7D%7D%5E%7B-1%7D%20b_%7B%5Cmathrm%7Bconv%7D%7D%20=%20%5Cfrac%7B1%7D%7B2%7D"> while <img src="https://latex.codecogs.com/png.latex?b_%7B%5Cmathrm%7Bs2z%7D%7D%5E%5Ctop%20D_%7B%5Cmathrm%7Bs2z%7D%7D%5E%7B-1%7D%20b_%7B%5Cmathrm%7Bs2z%7D%7D%20=%20%5Cfrac%7B1%7D%7B3%7D">, a reduction of <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B1%7D%7B6%7D">. If we also place a dogmatic prior <img src="https://latex.codecogs.com/png.latex?s_%5Calpha%20=%200.1"> on <img src="https://latex.codecogs.com/png.latex?%5Calpha">, however, the leading diagonal entry changes from <img src="https://latex.codecogs.com/png.latex?a_%7B%5Cmathrm%7Bconv%7D%7D%20=%20100%20+%201%20=%20101"> to <img src="https://latex.codecogs.com/png.latex?a_%7B%5Cmathrm%7Bs2z%7D%7D%20=%20(0.01%20+%200.5)%5E%7B-1%7D%20+%201%20=%20%5Cfrac%7B151%7D%7B51%7D%20%5Capprox%202.96">. Dividing by <img src="https://latex.codecogs.com/png.latex?a"> reverses the normalized squared correlation: <img src="https://latex.codecogs.com/png.latex?R%5E2_%7B%5Cmathrm%7Bconv%7D%7D%20=%20%5Cfrac%7B1%7D%7B202%7D%20%5Capprox%200.005%20%3C%20%5Cfrac%7B51%7D%7B453%7D%20%5Capprox%200.113%20=%20R%5E2_%7B%5Cmathrm%7Bs2z%7D%7D">. Selecting <code>Dogmatic s_α = 0.1</code> in Figure 1 shows the mechanism: <img src="https://latex.codecogs.com/png.latex?s_%5Calpha%20=%200.1"> pins <img src="https://latex.codecogs.com/png.latex?%5Calpha%20%5Capprox%200"> without using the data, whereas <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20=%20%5Calpha%20+%20%5Cbar%7Ba%7D"> inherits prior variance <img src="https://latex.codecogs.com/png.latex?%5Ctau%5E2/2%20=%200.5"> from the unobserved second group <img src="https://latex.codecogs.com/png.latex?a_2">.</p>
<figure class="quarto-float quarto-float-fig figure" id="fig-ridge-geometry">
  <div class="s2z-fig" id="viz-ridge-geometry">
    <div class="s2z-fig-head">
      <div class="s2z-fig-title">Two-Group Posterior Contours: Shift Ridge vs Zero-Sum Subspace</div>
      <div class="s2z-fig-subtitle">50% and 95% conditional Gaussian contours at σ_y = 1; adjust sample sizes and prior scales or select a regime</div>
    </div>

    <div class="s2z-toolbar">
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Total sample size N = n₁ + n₂</span><span class="s2z-ctrl-val" id="rg-wsum-val">40.00</span></div>
        <input type="range" id="rg-wsum" min="-1.0" max="2.5" step="0.001" value="1.60206">
      </div>
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Sample size split n₁ / n₂</span><span class="s2z-ctrl-val" id="rg-imb-val">50% / 50%</span></div>
        <input type="range" id="rg-imb" min="0.0" max="1.0" step="0.01" value="0.50">
      </div>
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Group SD τ</span><span class="s2z-ctrl-val" id="rg-tau-val">2.00</span></div>
        <input type="range" id="rg-tau" min="-0.8" max="1.4" step="0.001" value="0.30103">
      </div>
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Prior SD s_α</span><span class="s2z-ctrl-val" id="rg-smu-val">5.00</span></div>
        <input type="range" id="rg-smu" min="-1.1" max="1.4" step="0.001" value="0.69897">
      </div>
      <div class="s2z-btn-group">
        <span class="s2z-btn-group-label">Preset:</span>
        <button type="button" class="s2z-btn active" data-rg-preset="balanced-strong">Balanced (N = 40)</button>
        <button type="button" class="s2z-btn" data-rg-preset="weak-balanced">Small sample</button>
        <button type="button" class="s2z-btn" data-rg-preset="unbalanced">88% / 12% split</button>
        <button type="button" class="s2z-btn" data-rg-preset="dogmatic">Dogmatic s_α = 0.1</button>
      </div>
    </div>

    <div class="s2z-facets-2">
      <div class="s2z-facet">
        <div class="s2z-strip"><span>Conventional: (α, a₁)</span><span class="s2z-strip-sub" id="rg-badge-conv"></span></div>
        <canvas class="s2z-canvas" id="rg-canvas-conv"></canvas>
      </div>
      <div class="s2z-facet">
        <div class="s2z-strip"><span>Sum-to-Zero: (θ, h₁)</span><span class="s2z-strip-sub" id="rg-badge-s2z"></span></div>
        <canvas class="s2z-canvas" id="rg-canvas-s2z"></canvas>
      </div>
    </div>

    <div class="s2z-footer-bar" id="rg-footer"></div>
  </div>
  <figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig">
    Figure&nbsp;1: Conditional posterior contours for conventional coordinates and zero-sum coordinates for \(J = 2\) groups with unit residual variance. Equal group sample sizes make \(\theta\) and \(h_1\) orthogonal; unbalanced splits still leave a nonnegative cross-information reduction \(b_{\mathrm{conv}}^\top D_{\mathrm{conv}}^{-1} b_{\mathrm{conv}} - b_{\mathrm{s2z}}^\top D_{\mathrm{s2z}}^{-1} b_{\mathrm{s2z}} \ge 0\).
  </figcaption>
</figure>
</section>
<section id="extending-the-equivalence-beyond-one-way-intercepts" class="level2">
<h2 class="anchored" data-anchor-id="extending-the-equivalence-beyond-one-way-intercepts">3. Extending the Equivalence Beyond One-Way Intercepts</h2>
<p>A one-way normal-intercept model is only the simplest hierarchical design. Applied models routinely include correlated varying slopes with centered covariates, multiple crossed grouping factors, non-Gaussian likelihoods or non-Gaussian intercept priors, and heavy-tailed or group-varying scales. Each extension follows the same recipe: identify the group-average shifts that leave the linear predictor unchanged, marginalize them out of the prior when conjugate (or isolate them from the likelihood when non-Gaussian), and constrain each factor’s <img src="https://latex.codecogs.com/png.latex?(J%20-%201)"> free coordinates with <code>sum_to_zero_constrain()</code>.</p>
<section id="correlated-varying-slopes-and-centered-covariates" class="level3">
<h3 class="anchored" data-anchor-id="correlated-varying-slopes-and-centered-covariates">3.1 Correlated Varying Slopes and Centered Covariates</h3>
<p>Suppose each observation has a covariate <img src="https://latex.codecogs.com/png.latex?x_i"> and each group <img src="https://latex.codecogs.com/png.latex?j%20%5Cin%20%5C%7B1,%20%5Cdots,%20J%5C%7D"> has a correlated intercept-and-slope pair <img src="https://latex.codecogs.com/png.latex?u_j%20=%20(a_j,%20b_j)%5E%5Ctop%20%5Csim%20%5Cmathcal%7BN%7D_2(0,%20%5CSigma)"> with Cholesky factor <img src="https://latex.codecogs.com/png.latex?%5CSigma%20=%20L_%5CSigma%20L_%5CSigma%5E%5Ctop">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay_i%20%5Csim%20%5Cmathcal%7BN%7D%5C!%5Cleft(%5Calpha%20+%20a_%7Bg%5Bi%5D%7D%20+%20(%5Cbeta%20+%20b_%7Bg%5Bi%5D%7D)%20x_i,%5C;%20%5Csigma_y%5E2%5Cright).%0A"></p>
<p>Regression implementations often center covariates around their sample mean <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D%20=%20N%5E%7B-1%7D%5Csum_%7Bi=1%7D%5EN%20x_i">, writing <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D_i%20=%20x_i%20-%20%5Cbar%7Bx%7D"> and parameterizing the population vector as <img src="https://latex.codecogs.com/png.latex?%5Cbeta_c%20=%20(%5Calpha_c,%20%5Cbeta)%5E%5Ctop%20%5Csim%20%5Cmathcal%7BN%7D_2(%5Cbeta_0,%20V_0)">, where <img src="https://latex.codecogs.com/png.latex?%5Calpha_c%20=%20%5Calpha%20+%20%5Cbeta%20%5Cbar%7Bx%7D"> is the expected response at <img src="https://latex.codecogs.com/png.latex?x%20=%20%5Cbar%7Bx%7D">. If the random slope still multiplies raw <img src="https://latex.codecogs.com/png.latex?x_i%20=%20%5Ctilde%7Bx%7D_i%20+%20%5Cbar%7Bx%7D"> (or if we want to match centered and uncentered columns cleanly), sweeping the realized group average <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bu%7D%20=%20(%5Cbar%7Ba%7D,%20%5Cbar%7Bb%7D)%5E%5Ctop%20=%20J%5E%7B-1%7D%5Csum_%7Bj=1%7D%5EJ%20u_j"> into the centered population vector shifts <img src="https://latex.codecogs.com/png.latex?%5Calpha_c"> by <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Ba%7D%20+%20%5Cbar%7Bx%7D%5Cbar%7Bb%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> by <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bb%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Calpha_c%20+%20%5Cbeta%20%5Ctilde%7Bx%7D_i%20+%20a_j%20+%20b_j%20x_i%0A=%20%5Cunderbrace%7B(%5Calpha_c%20+%20%5Cbar%7Ba%7D%20+%20%5Cbar%7Bx%7D%5Cbar%7Bb%7D)%7D_%7B%5Ctheta_1%7D%0A%20%20+%20c_%7Ba,j%7D%0A%20%20+%20%5Cunderbrace%7B(%5Cbeta%20+%20%5Cbar%7Bb%7D)%7D_%7B%5Ctheta_2%7D%20%5Ctilde%7Bx%7D_i%0A%20%20+%20c_%7Bb,j%7D%20%5Ctilde%7Bx%7D_i%20+%20c_%7Bb,j%7D%5Cbar%7Bx%7D.%0A"></p>
<p>When the random slope also multiplies centered <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7Bx%7D_i">, the matching matrix is the identity (<img src="https://latex.codecogs.com/png.latex?H_x%20=%20I_2">); when it multiplies raw <img src="https://latex.codecogs.com/png.latex?x_i"> while the fixed design is centered, <img src="https://latex.codecogs.com/png.latex?H_x%20=%20%5Cbegin%7Bpmatrix%7D%201%20&amp;%20%5Cbar%7Bx%7D%20%5C%5C%200%20&amp;%201%20%5Cend%7Bpmatrix%7D"> satisfies <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bpmatrix%7D%201%20&amp;%20%5Ctilde%7Bx%7D_i%20%5Cend%7Bpmatrix%7D%20H_x%20=%20%5Cbegin%7Bpmatrix%7D%201%20&amp;%20x_i%20%5Cend%7Bpmatrix%7D"> for every observation, so <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20=%20%5Cbeta_c%20+%20H_x%20%5Cbar%7Bu%7D%20%5Csim%20%5Cmathcal%7BN%7D_2(%5Cbeta_0,%20V_0%20+%20J%5E%7B-1%7D%20H_x%20%5CSigma%20H_x%5E%5Ctop)">.</p>
<p>To sample the <img src="https://latex.codecogs.com/png.latex?J"> zero-sum pairs <img src="https://latex.codecogs.com/png.latex?C%20=%20%5Bc_1,%20%5Cdots,%20c_J%5D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7B2%20%5Ctimes%20J%7D"> on the physical predictor scale while keeping a standard normal prior on the unconstrained coordinates, declare <code>matrix[2, J - 1] h_raw</code> and apply <code>sum_to_zero_constrain()</code> to each row. Because <img src="https://latex.codecogs.com/png.latex?C%20=%20h_%7B%5Cmathrm%7Braw%7D%7D%20H%5E%5Ctop"> and <img src="https://latex.codecogs.com/png.latex?H%5E%5Ctop%20H%20=%20I_%7BJ-1%7D">, whitening <img src="https://latex.codecogs.com/png.latex?C"> across coefficients is isometric to whitening <img src="https://latex.codecogs.com/png.latex?h_%7B%5Cmathrm%7Braw%7D%7D"> directly:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5C%7CL_%5CSigma%5E%7B-1%7D%20C%5C%7C_F%5E2%0A=%20%5Coperatorname%7Btr%7D%5C!%5Cleft(L_%5CSigma%5E%7B-1%7D%20h_%7B%5Cmathrm%7Braw%7D%7D%20H%5E%5Ctop%20H%20h_%7B%5Cmathrm%7Braw%7D%7D%5E%5Ctop%20L_%5CSigma%5E%7B-%5Ctop%7D%5Cright)%0A=%20%5C%7CL_%5CSigma%5E%7B-1%7D%20h_%7B%5Cmathrm%7Braw%7D%7D%5C%7C_F%5E2.%0A"></p>
<p>We therefore solve <img src="https://latex.codecogs.com/png.latex?L_%5CSigma"> against only <img src="https://latex.codecogs.com/png.latex?J%20-%201"> columns and put <code>std_normal()</code> directly on the <img src="https://latex.codecogs.com/png.latex?2(J%20-%201)"> unconstrained whitened entries:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb2-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</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>] L_Sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag_pre_multiply</span>(tau, L_Omega);</span>
<span id="cb2-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] c_int <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;">sum_to_zero_constrain</span>(h_raw[<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>);</span>
<span id="cb2-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] c_slp <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;">sum_to_zero_constrain</span>(h_raw[<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>
<span id="cb2-5">}</span>
<span id="cb2-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb2-7">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</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>] Sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Sigma);</span>
<span id="cb2-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, J <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>] z_u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mdivide_left_tri_low</span>(L_Sigma, h_raw);</span>
<span id="cb2-9"></span>
<span id="cb2-10">  theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">multi_normal</span>(beta_0, V_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> J);</span>
<span id="cb2-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">to_vector</span>(z_u) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb2-12">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(J <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diagonal</span>(L_Sigma)));</span>
<span id="cb2-13"></span>
<span id="cb2-14">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(</span>
<span id="cb2-15">    (theta[<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> c_int[group]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (theta[<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> c_slp[group]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.*</span> x_c,</span>
<span id="cb2-16">    sigma_y</span>
<span id="cb2-17">  );</span>
<span id="cb2-18">}</span></code></pre></div></div>
<p>In the Fisher information matrix, conventional coordinates <img src="https://latex.codecogs.com/png.latex?(%5Cbeta_c,%20u_1,%20%5Cdots,%20u_J)"> have a <img src="https://latex.codecogs.com/png.latex?2">-dimensional likelihood nullspace (one constant shift for the intercept and one for the slope). Mapping <img src="https://latex.codecogs.com/png.latex?u%20=%20(H%20%5Cotimes%20I_2)%5C,%5Cmathrm%7Bvec%7D(h_%7B%5Cmathrm%7Braw%7D%7D%5E%5Ctop)"> removes both null directions. Whenever every group has the same within-group design cross-product <img src="https://latex.codecogs.com/png.latex?X_j%5E%5Ctop%20X_j%20=%20M_0"> (for example, a balanced clinical trial or panel design), the off-diagonal Fisher block <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BI%7D_%7B%5Cmathrm%7Blik%7D%7D(%5Ctheta,%20h_%7B%5Cmathrm%7Braw%7D%7D)"> vanishes identically even when <img src="https://latex.codecogs.com/png.latex?%5CSigma"> has strong intercept–slope correlation.</p>
</section>
<section id="multiple-crossed-grouping-factors-and-loewner-dominance" class="level3">
<h3 class="anchored" data-anchor-id="multiple-crossed-grouping-factors-and-loewner-dominance">3.2 Multiple Crossed Grouping Factors and Loewner Dominance</h3>
<p>Now suppose each observation belongs to <img src="https://latex.codecogs.com/png.latex?F%20=%202"> crossed grouping factors—say <img src="https://latex.codecogs.com/png.latex?G_1"> participants and <img src="https://latex.codecogs.com/png.latex?G_2"> test items—with <img src="https://latex.codecogs.com/png.latex?u%5E%7B(1)%7D_g%20%5Csim%20%5Cmathcal%7BN%7D(0,%20%5Ctau_1%5E2)"> and <img src="https://latex.codecogs.com/png.latex?u%5E%7B(2)%7D_h%20%5Csim%20%5Cmathcal%7BN%7D(0,%20%5Ctau_2%5E2)">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay_i%20%5Csim%20%5Cmathcal%7BN%7D%5C!%5Cleft(%5Calpha%20+%20u%5E%7B(1)%7D_%7Bg_1%5Bi%5D%7D%20+%20u%5E%7B(2)%7D_%7Bg_2%5Bi%5D%7D,%5C;%20%5Csigma_y%5E2%5Cright),%20%5Cqquad%20%5Calpha%20%5Csim%20%5Cmathcal%7BN%7D(%5Cmu_0,%20s_%5Calpha%5E2).%0A"></p>
<p>Adding <img src="https://latex.codecogs.com/png.latex?d_1"> to every participant effect, <img src="https://latex.codecogs.com/png.latex?d_2"> to every item effect, and subtracting <img src="https://latex.codecogs.com/png.latex?d_1%20+%20d_2"> from <img src="https://latex.codecogs.com/png.latex?%5Calpha"> leaves every linear predictor unchanged, creating an <img src="https://latex.codecogs.com/png.latex?F">-dimensional shift ridge in the likelihood. Sweeping both realized means <img src="https://latex.codecogs.com/png.latex?m_1%20=%20G_1%5E%7B-1%7D%5Csum_%7Bg=1%7D%5E%7BG_1%7D%20u%5E%7B(1)%7D_g%20%5Csim%20%5Cmathcal%7BN%7D(0,%20v_1)"> (<img src="https://latex.codecogs.com/png.latex?v_1%20=%20%5Ctau_1%5E2%20/%20G_1">) and <img src="https://latex.codecogs.com/png.latex?m_2%20=%20G_2%5E%7B-1%7D%5Csum_%7Bh=1%7D%5E%7BG_2%7D%20u%5E%7B(2)%7D_h%20%5Csim%20%5Cmathcal%7BN%7D(0,%20v_2)"> (<img src="https://latex.codecogs.com/png.latex?v_2%20=%20%5Ctau_2%5E2%20/%20G_2">) into a single grand mean <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20=%20%5Calpha%20+%20m_1%20+%20m_2"> gives</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctheta%20%5Cmid%20%5Ctau_1,%20%5Ctau_2%20%5Csim%20%5Cmathcal%7BN%7D(%5Cmu_0,%20W),%20%5Cqquad%20W%20=%20s_%5Calpha%5E2%20+%20v_1%20+%20v_2.%0A"></p>
<p>How do we recover <em>both</em> <img src="https://latex.codecogs.com/png.latex?m_1"> and <img src="https://latex.codecogs.com/png.latex?m_2"> in <code>generated quantities</code> when only their sum enters <img src="https://latex.codecogs.com/png.latex?%5Ctheta">? Conditional on <img src="https://latex.codecogs.com/png.latex?%5Ctheta">, <img src="https://latex.codecogs.com/png.latex?m_1"> and <img src="https://latex.codecogs.com/png.latex?m_2"> are negatively correlated—if participant effects happened to average slightly high, item effects or <img src="https://latex.codecogs.com/png.latex?%5Calpha"> had to be lower to hit the same <img src="https://latex.codecogs.com/png.latex?%5Ctheta">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bpmatrix%7D%20m_1%20%5C%5C%20m_2%20%5Cend%7Bpmatrix%7D%20%5CBigm%7C%20%5Ctheta,%20%5Ctau_1,%20%5Ctau_2%0A%5Csim%20%5Cmathcal%7BN%7D_2%5C!%5Cleft(%0A%5Cfrac%7B%5Ctheta%20-%20%5Cmu_0%7D%7BW%7D%5Cbegin%7Bpmatrix%7D%20v_1%20%5C%5C%20v_2%20%5Cend%7Bpmatrix%7D,%5C;%0A%5Cbegin%7Bpmatrix%7D%20v_1%20&amp;%200%20%5C%5C%200%20&amp;%20v_2%20%5Cend%7Bpmatrix%7D%0A-%20%5Cfrac%7B1%7D%7BW%7D%5Cbegin%7Bpmatrix%7D%20v_1%20%5C%5C%20v_2%20%5Cend%7Bpmatrix%7D%5Cbegin%7Bpmatrix%7D%20v_1%20&amp;%20v_2%20%5Cend%7Bpmatrix%7D%0A%5Cright).%0A"></p>
<p>Rather than factoring an <img src="https://latex.codecogs.com/png.latex?(F%20%5Ctimes%20F)"> covariance matrix, Matheron’s conditional simulation rule draws <img src="https://latex.codecogs.com/png.latex?(%5Ctilde%7B%5Calpha%7D,%20%5Ctilde%7Bm%7D_1,%20%5Ctilde%7Bm%7D_2)"> from their independent priors and applies a rank-one linear update from the innovation <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20-%20(%5Ctilde%7B%5Calpha%7D%20+%20%5Ctilde%7Bm%7D_1%20+%20%5Ctilde%7Bm%7D_2)">. Using <code>sum_to_zero_constrain()</code> also avoids Stan’s array-length restriction (<code>array[F] sum_to_zero_vector</code> requires equal <img src="https://latex.codecogs.com/png.latex?G_f">), because we simply pass each factor’s <img src="https://latex.codecogs.com/png.latex?(G_f%20-%201)">-vector separately:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb3-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta;</span>
<span id="cb3-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[G1 <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>] h1_raw;</span>
<span id="cb3-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[G2 <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>] h2_raw;</span>
<span id="cb3-5">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" 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>] tau;</span>
<span id="cb3-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> sigma_y;</span>
<span id="cb3-7">}</span>
<span id="cb3-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb3-9">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[G1] c1 <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;">sum_to_zero_constrain</span>(h1_raw);</span>
<span id="cb3-10">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[G2] c2 <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;">sum_to_zero_constrain</span>(h2_raw);</span>
<span id="cb3-11">}</span>
<span id="cb3-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb3-13">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> W <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(s_alpha) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(tau[<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> G1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(tau[<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> G2;</span>
<span id="cb3-14">  theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(mu_0, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(W));</span>
<span id="cb3-15">  (h1_raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> tau[<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><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb3-16">  (h2_raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> tau[<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb3-17">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(G1 <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(tau[<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> (G2 <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(tau[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]);</span>
<span id="cb3-18">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c1[g1] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c2[g2], sigma_y);</span>
<span id="cb3-19">}</span>
<span id="cb3-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb3-21">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> alpha;</span>
<span id="cb3-22">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] m;</span>
<span id="cb3-23">  {</span>
<span id="cb3-24">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> v1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(tau[<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> G1;</span>
<span id="cb3-25">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> v2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(tau[<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> G2;</span>
<span id="cb3-26">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> W <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(s_alpha) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v2;</span>
<span id="cb3-27">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> a_sim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> normal_rng(mu_0, s_alpha);</span>
<span id="cb3-28">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> m1_sim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> normal_rng(<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;">sqrt</span>(v1));</span>
<span id="cb3-29">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> m2_sim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> normal_rng(<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;">sqrt</span>(v2));</span>
<span id="cb3-30">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> innov <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (a_sim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> m1_sim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> m2_sim);</span>
<span id="cb3-31">    alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a_sim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(s_alpha) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> W) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> innov;</span>
<span id="cb3-32">    m[<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> m1_sim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (v1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> W) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> innov;</span>
<span id="cb3-33">    m[<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> m2_sim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (v2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> W) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> innov;</span>
<span id="cb3-34">  }</span>
<span id="cb3-35">}</span></code></pre></div></div>
<p>When a multi-factor model also includes <img src="https://latex.codecogs.com/png.latex?q"> population regression coefficients (such as <code>(1 + x | g_1) + (1 | g_2)</code>), write the joint precision matrix of the population vector and the stacked group effects as <img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bpmatrix%7D%20A%20&amp;%20B%20%5C%5C%20B%5E%5Ctop%20&amp;%20D%20%5Cend%7Bpmatrix%7D">. Integrating out the group effects leaves the marginal population precision <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BI%7D_%7B%5Cmathrm%7Beff%7D%7D%20=%20A%20-%20B%20D%5E%7B-1%7D%20B%5E%5Ctop"> (the Schur complement of <img src="https://latex.codecogs.com/png.latex?D">), where <img src="https://latex.codecogs.com/png.latex?A"> is the precision the population parameters would have if all group effects were known and <img src="https://latex.codecogs.com/png.latex?B%20D%5E%7B-1%7D%20B%5E%5Ctop"> is the information lost from estimating the group effects alongside them.</p>
<p>To compare two <img src="https://latex.codecogs.com/png.latex?q%20%5Ctimes%20q"> information matrices <img src="https://latex.codecogs.com/png.latex?M_1"> and <img src="https://latex.codecogs.com/png.latex?M_2">, we use the <strong>Loewner order</strong> (or <strong>Loewner dominance</strong>, after <span class="citation" data-cites="loewner1934">Löwner (1934)</span>): writing <img src="https://latex.codecogs.com/png.latex?M_1%20%5Csucceq%20M_2"> means that <img src="https://latex.codecogs.com/png.latex?M_1%20-%20M_2"> is positive semidefinite—all eigenvalues of <img src="https://latex.codecogs.com/png.latex?M_1%20-%20M_2"> are nonnegative, or equivalently <img src="https://latex.codecogs.com/png.latex?v%5E%5Ctop%20M_1%20v%20%5Cge%20v%5E%5Ctop%20M_2%20v"> for every contrast vector <img src="https://latex.codecogs.com/png.latex?v%20%5Cin%20%5Cmathbb%7BR%7D%5Eq">. Unlike scalar summaries such as trace or determinant, Loewner dominance requires Fisher information to be at least as large along <em>every</em> linear combination of the population parameters simultaneously. When <img src="https://latex.codecogs.com/png.latex?M_1,%20M_2%20%5Csucc%200"> are precision matrices, <img src="https://latex.codecogs.com/png.latex?M_1%20%5Csucceq%20M_2"> is equivalent to <img src="https://latex.codecogs.com/png.latex?M_1%5E%7B-1%7D%20%5Cpreceq%20M_2%5E%7B-1%7D">, meaning every marginal variance <img src="https://latex.codecogs.com/png.latex?%5Coperatorname%7BVar%7D(v%5E%5Ctop%20%5Ctheta)"> shrinks or stays the same and the Gaussian credible ellipsoid of <img src="https://latex.codecogs.com/png.latex?M_1"> sits inside that of <img src="https://latex.codecogs.com/png.latex?M_2">.</p>
<p>Let <img src="https://latex.codecogs.com/png.latex?E%20=%20%5Coperatorname%7Bblockdiag%7D(E_1,%20%5Cdots,%20E_F)"> collect the factor-wise Helmert bases (<img src="https://latex.codecogs.com/png.latex?E_f%20=%20H_%7BG_f%7D%20%5Cotimes%20I_%7BM_f%7D">) and let <img src="https://latex.codecogs.com/png.latex?U_%7B%5Cmathrm%7Bmean%7D%7D%20=%20%5Coperatorname%7Bblockdiag%7D(%5Cmathbf%7B1%7D_%7BG_1%7D%20%5Cotimes%20I_%7BM_1%7D,%20%5Cdots,%20%5Cmathbf%7B1%7D_%7BG_F%7D%20%5Cotimes%20I_%7BM_F%7D)"> span the <img src="https://latex.codecogs.com/png.latex?F"> block-mean subspaces. Because <img src="https://latex.codecogs.com/png.latex?%5Coperatorname%7Bim%7D(E)"> and <img src="https://latex.codecogs.com/png.latex?%5Coperatorname%7Bim%7D(U_%7B%5Cmathrm%7Bmean%7D%7D)"> are orthogonal complements in <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BR%7D%5E%7B%5Csum_f%20G_f%20M_f%7D">, <img src="https://latex.codecogs.com/png.latex?B_%7B%5Cmathrm%7Bs2z%7D%7D%20=%20B_%7B%5Cmathrm%7Bconv%7D%7D%20E"> and <img src="https://latex.codecogs.com/png.latex?D_%7B%5Cmathrm%7Bs2z%7D%7D%20=%20E%5E%5Ctop%20D_%7B%5Cmathrm%7Bconv%7D%7D%20E"> satisfy an exact <img src="https://latex.codecogs.com/png.latex?D_%7B%5Cmathrm%7Bconv%7D%7D">-orthogonal projection identity for <em>any</em> unbalanced cell counts and any positive-definite group precision <img src="https://latex.codecogs.com/png.latex?D_%7B%5Cmathrm%7Bconv%7D%7D%20%5Csucc%200">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AB_%7B%5Cmathrm%7Bconv%7D%7D%20D_%7B%5Cmathrm%7Bconv%7D%7D%5E%7B-1%7D%20B_%7B%5Cmathrm%7Bconv%7D%7D%5E%5Ctop%20-%20B_%7B%5Cmathrm%7Bs2z%7D%7D%20D_%7B%5Cmathrm%7Bs2z%7D%7D%5E%7B-1%7D%20B_%7B%5Cmathrm%7Bs2z%7D%7D%5E%5Ctop%0A=%20%5Cleft(B_%7B%5Cmathrm%7Bconv%7D%7D%20D_%7B%5Cmathrm%7Bconv%7D%7D%5E%7B-1%7D%20U_%7B%5Cmathrm%7Bmean%7D%7D%5Cright)%0A%20%20%5Cleft(U_%7B%5Cmathrm%7Bmean%7D%7D%5E%5Ctop%20D_%7B%5Cmathrm%7Bconv%7D%7D%5E%7B-1%7D%20U_%7B%5Cmathrm%7Bmean%7D%7D%5Cright)%5E%7B-1%7D%0A%20%20%5Cleft(U_%7B%5Cmathrm%7Bmean%7D%7D%5E%5Ctop%20D_%7B%5Cmathrm%7Bconv%7D%7D%5E%7B-1%7D%20B_%7B%5Cmathrm%7Bconv%7D%7D%5E%5Ctop%5Cright)%20%5Csucceq%200.%0A"></p>
<p>Furthermore, whenever the cross-factor cell Fisher weights are additively separable across factors, <img src="https://latex.codecogs.com/png.latex?C(g_1,%20g_2)%20=%20A(g_1)%20+%20B(g_2)"> (including every balanced crossed design), the cross-factor Fisher block <img src="https://latex.codecogs.com/png.latex?E_1%5E%5Ctop%20Z_1%5E%5Ctop%20%5Cmathcal%7BW%7D%20Z_2%20E_2"> vanishes identically. Figure 2 lets you drag cell imbalance from <code>0.00</code> (exact Cox–Reid orthogonality across all blocks) up to <code>1.00</code> under both Gaussian and logistic GLM weights.</p>
<figure class="quarto-float quarto-float-fig figure" id="fig-brms-loewner">
  <div class="s2z-fig" id="viz-brms-loewner">
    <div class="s2z-fig-head">
      <div class="s2z-fig-title">Crossed Two-Factor Model: Loewner Information Gain and Canonical Correlations</div>
      <div class="s2z-fig-subtitle">Two-factor hierarchy (1 + x | g₁) + (1 | g₂) with G₁ = 5, G₂ = 4, and M₁ + M₂ = 3 shift dimensions</div>
    </div>

    <div class="s2z-toolbar">
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Cell &amp; covariate imbalance</span><span class="s2z-ctrl-val" id="bl-imb-val">0.00</span></div>
        <input type="range" id="bl-imb" min="0.0" max="1.0" step="0.02" value="0.0">
      </div>
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Population prior SD s₀</span><span class="s2z-ctrl-val" id="bl-prior-val">Flat (∞)</span></div>
        <input type="range" id="bl-prior" min="-1.0" max="1.8" step="0.04" value="1.8">
      </div>
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Sample size multiplier</span><span class="s2z-ctrl-val" id="bl-prec-val">4.00</span></div>
        <input type="range" id="bl-prec" min="-0.5" max="1.6" step="0.001" value="0.60206">
      </div>
      <div class="s2z-btn-group">
        <span class="s2z-btn-group-label">Weights:</span>
        <button type="button" class="s2z-btn active" data-bl-wmode="gaussian">Gaussian</button>
        <button type="button" class="s2z-btn" data-bl-wmode="bernoulli">Bernoulli p(1−p)</button>
      </div>
    </div>

    <div class="s2z-facets-2">
      <div class="s2z-facet">
        <div class="s2z-strip"><span>Eigenvalues of Loewner Gap ΔI_eff ⪰ 0</span></div>
        <canvas class="s2z-canvas" id="bl-canvas-eig"></canvas>
      </div>
      <div class="s2z-facet">
        <div class="s2z-strip"><span>Canonical Correlations ρ_k(pop; group)</span></div>
        <canvas class="s2z-canvas" id="bl-canvas-cancorr"></canvas>
      </div>
    </div>

    <div class="s2z-footer-bar" id="bl-footer"></div>
  </div>
  <figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig">
    Figure&nbsp;2: Eigenvalues of the Loewner cross-information reduction \(\Delta\mathcal{I}_{\mathrm{eff}}\) and population-to-group canonical correlations for a crossed two-factor hierarchy under Gaussian and Bernoulli observation weights.
  </figcaption>
</figure>
</section>
<section id="generalized-linear-models-and-non-gaussian-population-priors" class="level3">
<h3 class="anchored" data-anchor-id="generalized-linear-models-and-non-gaussian-population-priors">3.3 Generalized Linear Models and Non-Gaussian Population Priors</h3>
<p>Moving from Gaussian outcomes to a generalized linear model (GLM) changes only the observation weight matrix. In hierarchical logistic regression,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay_i%20%5Csim%20%5Coperatorname%7BBernoulli%7D%5C!%5Cleft(%5Coperatorname%7Blogit%7D%5E%7B-1%7D(%5Ceta_i)%5Cright),%20%5Cqquad%20%5Ceta_i%20=%20%5Calpha%20+%20a_%7Bg%5Bi%5D%7D%20+%20%5Cbeta%20x_i,%0A"></p>
<p>the log-likelihood Hessian with respect to <img src="https://latex.codecogs.com/png.latex?%5Ceta"> is diagonal with weights <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BW%7D(%5Ceta)_%7Bii%7D%20=%20p_i(1%20-%20p_i)">, where <img src="https://latex.codecogs.com/png.latex?p_i%20=%20%5Coperatorname%7Blogit%7D%5E%7B-1%7D(%5Ceta_i)">. Because the linear predictor <img src="https://latex.codecogs.com/png.latex?%5Ceta%20=%20%5Ctheta%5Cmathbf%7B1%7D_N%20+%20Z%20c%20+%20%5Cbeta%20x"> is algebraically identical in both parameterizations, every likelihood shift-nullspace and Loewner-dominance result from Section 3.2 carries over directly with <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BW%7D(%5Ceta)"> in place of <img src="https://latex.codecogs.com/png.latex?%5Csigma_y%5E%7B-2%7D%20I_N">.</p>
<p>A subtler question arises when the superpopulation intercept <img src="https://latex.codecogs.com/png.latex?%5Calpha"> carries a <strong>non-Gaussian prior</strong>—for example <img src="https://latex.codecogs.com/png.latex?%5Calpha%20%5Csim%20%5Coperatorname%7BLogistic%7D(0,%201)">, which induces a uniform prior on the baseline probability <img src="https://latex.codecogs.com/png.latex?%5Coperatorname%7Blogit%7D%5E%7B-1%7D(%5Calpha)%20%5Csim%20%5Coperatorname%7BUniform%7D(0,%201)">. Now <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20=%20%5Calpha%20+%20%5Cbar%7Ba%7D"> is the sum of a Logistic variable and a Normal variable <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Ba%7D%20%5Csim%20%5Cmathcal%7BN%7D(0,%20%5Ctau%5E2%20/%20J)">, which has no closed-form marginal density.</p>
<p>We do not need a closed-form convolution to remove the likelihood ridge. Keep the scalar standardized mean <img src="https://latex.codecogs.com/png.latex?z_%7B%5Cmathrm%7Bmean%7D%7D%20%5Csim%20%5Cmathcal%7BN%7D(0,%201)"> in <code>parameters</code>, reconstruct <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%20%5Ctheta%20-%20(%5Ctau%20/%20%5Csqrt%7BJ%7D)%20z_%7B%5Cmathrm%7Bmean%7D%7D"> to evaluate <code>alpha ~ logistic(0, 1)</code>, and pass only <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20+%20c_%7Bg%5Bi%5D%7D%20+%20%5Cbeta%20x_i"> into the Bernoulli likelihood:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb4-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta;</span>
<span id="cb4-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> z_mean;</span>
<span id="cb4-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>;</span>
<span id="cb4-5">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J <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>] h_raw;</span>
<span id="cb4-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> tau;</span>
<span id="cb4-7">}</span>
<span id="cb4-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb4-9">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] 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;">sum_to_zero_constrain</span>(h_raw);</span>
<span id="cb4-10">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> mean_a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(J)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z_mean;</span>
<span id="cb4-11">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mean_a;</span>
<span id="cb4-12">}</span>
<span id="cb4-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb4-14">  tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">2.5</span>);</span>
<span id="cb4-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">2.5</span>);</span>
<span id="cb4-16">  alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logistic</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>
<span id="cb4-17">  z_mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb4-18">  (h_raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> tau) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb4-19">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(J <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(tau);</span>
<span id="cb4-20">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bernoulli_logit</span>(theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c[group] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x);</span>
<span id="cb4-21">}</span></code></pre></div></div>
<p>Because <img src="https://latex.codecogs.com/png.latex?%5Cpartial%20%5Ceta%20/%20%5Cpartial%20z_%7B%5Cmathrm%7Bmean%7D%7D%20=%200">, the likelihood Fisher matrix on <img src="https://latex.codecogs.com/png.latex?(%5Ctheta,%20%5Cbeta,%20z_%7B%5Cmathrm%7Bmean%7D%7D,%20h_%7B%5Cmathrm%7Braw%7D%7D)"> has an identically zero row and column for <img src="https://latex.codecogs.com/png.latex?z_%7B%5Cmathrm%7Bmean%7D%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BI%7D_%7B%5Cmathrm%7Blik%7D%7D(%5Ctheta,%20%5Cbeta,%20z_%7B%5Cmathrm%7Bmean%7D%7D,%20h_%7B%5Cmathrm%7Braw%7D%7D)%20=%0A%5Cbegin%7Bpmatrix%7D%0A%5Cmathbf%7B1%7D%5E%5Ctop%20%5Cmathcal%7BW%7D%5Cmathbf%7B1%7D%20&amp;%20%5Cmathbf%7B1%7D%5E%5Ctop%20%5Cmathcal%7BW%7D%20x%20&amp;%200%20&amp;%20%5Cmathbf%7B1%7D%5E%5Ctop%20%5Cmathcal%7BW%7D%20Z%20H%20%5C%5C%0Ax%5E%5Ctop%20%5Cmathcal%7BW%7D%5Cmathbf%7B1%7D%20&amp;%20x%5E%5Ctop%20%5Cmathcal%7BW%7D%20x%20&amp;%200%20&amp;%20x%5E%5Ctop%20%5Cmathcal%7BW%7D%20Z%20H%20%5C%5C%0A0%20&amp;%200%20&amp;%200%20&amp;%20%5Cmathbf%7B0%7D%5E%5Ctop%20%5C%5C%0AH%5E%5Ctop%20Z%5E%5Ctop%20%5Cmathcal%7BW%7D%5Cmathbf%7B1%7D%20&amp;%20H%5E%5Ctop%20Z%5E%5Ctop%20%5Cmathcal%7BW%7D%20x%20&amp;%20%5Cmathbf%7B0%7D%20&amp;%20H%5E%5Ctop%20Z%5E%5Ctop%20%5Cmathcal%7BW%7D%20Z%20H%0A%5Cend%7Bpmatrix%7D.%0A"></p>
<p>Instead of coupling <img src="https://latex.codecogs.com/png.latex?%5Calpha"> to all <img src="https://latex.codecogs.com/png.latex?J"> group effects through a high-precision data ridge, <img src="https://latex.codecogs.com/png.latex?z_%7B%5Cmathrm%7Bmean%7D%7D"> interacts only with <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> through the smooth scalar logistic prior penalty <img src="https://latex.codecogs.com/png.latex?-%5Cpartial%5E2_%5Calpha%20%5Clog%20p(%5Calpha)%20=%20p_%5Calpha(1%20-%20p_%5Calpha)%20%5Cle%20%5Cfrac%7B1%7D%7B4%7D">.</p>
</section>
<section id="heavy-tailed-group-effects-varying-standard-deviations-and-louiss-identity" class="level3">
<h3 class="anchored" data-anchor-id="heavy-tailed-group-effects-varying-standard-deviations-and-louiss-identity">3.4 Heavy-Tailed Group Effects, Varying Standard Deviations, and Louis’s Identity</h3>
<p>What if groups do not share a single variance <img src="https://latex.codecogs.com/png.latex?%5Ctau%5E2">? Two common models give each group <img src="https://latex.codecogs.com/png.latex?j"> its own Gaussian scale <img src="https://latex.codecogs.com/png.latex?a_j%20%5Cmid%20s_j%20%5Csim%20%5Cmathcal%7BN%7D(0,%20s_j%5E2)">:</p>
<ul>
<li><strong>Heavy-tailed Student-<img src="https://latex.codecogs.com/png.latex?t_%5Cnu"> group effects</strong>: writing <img src="https://latex.codecogs.com/png.latex?a_j%20%5Cmid%20%5Comega_j,%20%5Ctau%20%5Csim%20%5Cmathcal%7BN%7D(0,%20%5Ctau%5E2%20%5Comega_j)"> with <img src="https://latex.codecogs.com/png.latex?%5Comega_j%20%5Csim%20%5Coperatorname%7BInv-Gamma%7D(%5Cnu/2,%20%5Cnu/2)"> gives <img src="https://latex.codecogs.com/png.latex?a_j%20%5Cmid%20%5Ctau%20%5Csim%20t_%5Cnu(0,%20%5Ctau)"> marginally while keeping the conditional prior Gaussian at every draw of <img src="https://latex.codecogs.com/png.latex?s_j%20=%20%5Ctau%5Csqrt%7B%5Comega_j%7D">.</li>
<li><strong>Partially pooled group standard deviations</strong>: modeling <img src="https://latex.codecogs.com/png.latex?%5Cell_j%20=%20%5Clog%20s_j%20=%20%5Cgamma%20+%20%5Clambda_%5Comega%20z_%7B%5Clog%20s,%20j%7D"> with <img src="https://latex.codecogs.com/png.latex?%5Cgamma%20%5Csim%20%5Cmathcal%7BN%7D(%5Cmu_%5Cgamma,%20s_%5Cgamma%5E2)">, <img src="https://latex.codecogs.com/png.latex?z_%7B%5Clog%20s%7D%20%5Csim%20%5Cmathcal%7BN%7D(0,%20I_J)">, and <img src="https://latex.codecogs.com/png.latex?%5Clambda_%5Comega%20%5Csim%20%5Cmathcal%7BN%7D%5E+(0,%20s_%5Clambda%5E2)"> lets group variability itself vary across groups.</li>
</ul>
<p>When <img src="https://latex.codecogs.com/png.latex?s_1,%20%5Cdots,%20s_J"> are unequal, <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Ba%7D"> and <img src="https://latex.codecogs.com/png.latex?c_j%20=%20a_j%20-%20%5Cbar%7Ba%7D"> are <strong>no longer independent in the prior</strong>: <img src="https://latex.codecogs.com/png.latex?%5Coperatorname%7BCov%7D(a_j,%20%5Cbar%7Ba%7D%20%5Cmid%20s)%20=%20s_j%5E2%20/%20J"> depends on <img src="https://latex.codecogs.com/png.latex?j">, so high-variance groups pull <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Ba%7D"> more than low-variance groups. Even so, the realized average <img src="https://latex.codecogs.com/png.latex?m%20=%20%5Cbar%7Ba%7D"> still enters <img src="https://latex.codecogs.com/png.latex?%5Calpha%20=%20%5Ctheta%20-%20m"> and <img src="https://latex.codecogs.com/png.latex?a_j%20=%20c_j%20+%20m"> quadratically in the log prior! Let <img src="https://latex.codecogs.com/png.latex?q_0%20=%20s_0%5E%7B-2%7D"> be the intercept prior precision, <img src="https://latex.codecogs.com/png.latex?q_j%20=%20s_j%5E%7B-2%7D%20=%20e%5E%7B-2%5Cell_j%7D"> be group <img src="https://latex.codecogs.com/png.latex?j">’s prior precision, and <img src="https://latex.codecogs.com/png.latex?P%20=%20q_0%20+%20%5Csum_%7Bj=1%7D%5EJ%20q_j%20=%20q_0%20+%20q_%5Cbullet">. Completing the square over <img src="https://latex.codecogs.com/png.latex?m"> gives</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Aq_0(%5Ctheta%20-%20m)%5E2%20+%20%5Csum_%7Bj=1%7D%5EJ%20q_j(c_j%20+%20m)%5E2%0A=%20P(m%20-%20%5Chat%7Bm%7D)%5E2%20+%20q_0(%5Ctheta%20-%20%5Chat%7Bm%7D)%5E2%20+%20%5Csum_%7Bj=1%7D%5EJ%20q_j(c_j%20+%20%5Chat%7Bm%7D)%5E2,%0A%5Cqquad%0A%5Chat%7Bm%7D%20=%20%5Cfrac%7Bq_0%20%5Ctheta%20-%20%5Cmathbf%7Bq%7D%5E%5Ctop%20c%7D%7BP%7D.%0A"></p>
<p>Integrating <img src="https://latex.codecogs.com/png.latex?m%20%5Cmid%20%5Ctheta,%20c,%20s%20%5Csim%20%5Cmathcal%7BN%7D(%5Chat%7Bm%7D,%20P%5E%7B-1%7D)"> out analytically adds <img src="https://latex.codecogs.com/png.latex?-%5Cfrac%7B1%7D%7B2%7D%5Clog%20P"> to the log-density and yields the conditional location precision matrix on <img src="https://latex.codecogs.com/png.latex?(%5Ctheta,%20h_%7B%5Cmathrm%7Braw%7D%7D)"> (<img src="https://latex.codecogs.com/png.latex?c%20=%20H%20h_%7B%5Cmathrm%7Braw%7D%7D">):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_%7B%5Cmathrm%7Bloc%7D%7D%5E%7B%5Cmathrm%7Bs2z%7D%7D(%5Ctheta,%20h_%7B%5Cmathrm%7Braw%7D%7D)%20=%0A%5Cbegin%7Bpmatrix%7D%0Aw_%5Cbullet%20+%20%5Cdfrac%7Bq_0%20q_%5Cbullet%7D%7BP%7D%20&amp;%20%5Cleft(%5Cmathbf%7Bw%7D%20+%20%5Cpi_0%20%5Cmathbf%7Bq%7D%5Cright)%5E%5Ctop%20H%20%5C%5C%5B8pt%5D%0AH%5E%5Ctop(%5Cmathbf%7Bw%7D%20+%20%5Cpi_0%20%5Cmathbf%7Bq%7D)%20&amp;%20H%5E%5Ctop%5C!%5Cleft(W%20+%20Q_a%20-%20%5Cdfrac%7B%5Cmathbf%7Bq%7D%5Cmathbf%7Bq%7D%5E%5Ctop%7D%7BP%7D%5Cright)%5C!H%0A%5Cend%7Bpmatrix%7D,%0A%5Cqquad%20%5Cpi_0%20=%20%5Cfrac%7Bq_0%7D%7BP%7D,%20%5Cquad%20%5Cboldsymbol%7B%5Cpi%7D%20=%20%5Cfrac%7B%5Cmathbf%7Bq%7D%7D%7BP%7D.%0A"></p>
<p>Notice the factor <img src="https://latex.codecogs.com/png.latex?%5Cpi_0%20=%20q_0%20/%20(q_0%20+%20%5Csum_%7Bj=1%7D%5EJ%20q_j)%20=%20O(J%5E%7B-1%7D)"> multiplying the heterogeneous prior coupling <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bq%7D%5E%5Ctop%20H">: as the number of groups <img src="https://latex.codecogs.com/png.latex?J"> grows (or as the intercept prior <img src="https://latex.codecogs.com/png.latex?s_0"> becomes wide), the prior cross-term between <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> and <img src="https://latex.codecogs.com/png.latex?h_%7B%5Cmathrm%7Braw%7D%7D"> vanishes.</p>
<p>When <img src="https://latex.codecogs.com/png.latex?%5Cell_j%20=%20%5Clog%20s_j"> is itself hierarchical, we can apply <code>sum_to_zero_constrain()</code> a second time on the log-SD layer, setting <img src="https://latex.codecogs.com/png.latex?%5Cell%20=%20%5Cgamma_s%20%5Cmathbf%7B1%7D_J%20+%20%5Clambda_%5Comega%5C,%5Ctext%7Bsum%5C_to%5C_zero%5C_constrain%7D(z_%7B%5Clog%20s,%5Cmathrm%7Braw%7D%7D)"> with <img src="https://latex.codecogs.com/png.latex?%5Cgamma_s%20%5Csim%20%5Cmathcal%7BN%7D(%5Cmu_%5Cgamma,%20%5Csqrt%7Bs_%5Cgamma%5E2%20+%20%5Clambda_%5Comega%5E2%20/%20J%7D)">:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb5-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] 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;">sum_to_zero_constrain</span>(h_raw);</span>
<span id="cb5-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] z_log_s <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;">sum_to_zero_constrain</span>(z_log_s_raw);</span>
<span id="cb5-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>[J] s_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(gamma_s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> lambda_w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z_log_s);</span>
<span id="cb5-5">}</span>
<span id="cb5-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb5-7">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] q_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_square</span>(s_j);</span>
<span id="cb5-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_square</span>(s_0);</span>
<span id="cb5-9">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> q_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(q_j);</span>
<span id="cb5-10">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> m_hat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (q_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(q_j, c)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> P;</span>
<span id="cb5-11"></span>
<span id="cb5-12">  gamma_s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(mu_gamma, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hypot</span>(s_gamma, lambda_w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(J)));</span>
<span id="cb5-13">  z_log_s_raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb5-14">  (theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> m_hat) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> s_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb5-15">  (c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> m_hat) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">./</span> s_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb5-16">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(s_0) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(s_j)) <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(P);</span>
<span id="cb5-17"></span>
<span id="cb5-18">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c[group], sigma_y);</span>
<span id="cb5-19">}</span></code></pre></div></div>
<p>How much Fisher information do the sampled location coordinates <img src="https://latex.codecogs.com/png.latex?w%20=%20(%5Ctheta,%20h_%7B%5Cmathrm%7Braw%7D%7D)"> carry about the log-SD vector <img src="https://latex.codecogs.com/png.latex?%5Cell%20=%20(%5Cell_1,%20%5Cdots,%20%5Cell_J)%5E%5Ctop"> once <img src="https://latex.codecogs.com/png.latex?m%20=%20%5Cbar%7Ba%7D"> has been integrated out? In conventional coordinates <img src="https://latex.codecogs.com/png.latex?(%5Calpha,%20a)">, each <img src="https://latex.codecogs.com/png.latex?a_j%20%5Csim%20%5Cmathcal%7BN%7D(0,%20e%5E%7B2%5Cell_j%7D)"> contributes <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5B-%5Cpartial%5E2_%7B%5Cell_j%7D%20%5Clog%20%5Cmathcal%7BN%7D(a_j%20%5Cmid%200,%20e%5E%7B2%5Cell_j%7D)%5D%20=%202">, so <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BI%7D_%7B%5Cmathrm%7Bcomplete%7D%7D(%5Cell)%20=%202I_J">—which induces a <code>non_centered_conv</code> coupling <img src="https://latex.codecogs.com/png.latex?2%5Clambda_%5Comega%20%5Cmathbf%7B1%7D_J"> between <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> and <img src="https://latex.codecogs.com/png.latex?z_%7B%5Clog%20s%7D">.</p>
<p>After marginalizing <img src="https://latex.codecogs.com/png.latex?m">, we obtain the exact Fisher matrix on <img src="https://latex.codecogs.com/png.latex?%5Cell"> via <strong>Louis’s missing-information identity</strong> <span class="citation" data-cites="louis1982">(Louis 1982)</span>. Originally derived to compute observed Fisher information in the EM algorithm when a latent variable <img src="https://latex.codecogs.com/png.latex?m"> is integrated out of <img src="https://latex.codecogs.com/png.latex?p(w,%20m%20%5Cmid%20%5Cell)%20=%20p(w%20%5Cmid%20%5Cell)%5C,p(m%20%5Cmid%20w,%20%5Cell)">, differentiating <img src="https://latex.codecogs.com/png.latex?%5Clog%20p(w%20%5Cmid%20%5Cell)%20=%20%5Clog%20%5Cint%20p(w,%20m%20%5Cmid%20%5Cell)%5C,dm"> twice under the integral sign gives</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A-%5Cnabla%5E2_%5Cell%20%5Clog%20p(w%20%5Cmid%20%5Cell)%0A=%20%5Cmathbb%7BE%7D_%7Bm%20%5Cmid%20w,%20%5Cell%7D%5C!%5Cleft%5B-%5Cnabla%5E2_%5Cell%20%5Clog%20p(w,%20m%20%5Cmid%20%5Cell)%5Cright%5D%0A%20%20-%20%5Coperatorname%7BVar%7D_%7Bm%20%5Cmid%20w,%20%5Cell%7D%5C!%5Cleft(%5Cnabla_%5Cell%20%5Clog%20p(w,%20m%20%5Cmid%20%5Cell)%5Cright).%0A"></p>
<p>Taking expectations over <img src="https://latex.codecogs.com/png.latex?w%20%5Cmid%20%5Cell"> gives the missing-information decomposition</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BI%7D_%7B%5Cmathrm%7Bmarginal%7D%7D(%5Cell)%0A=%20%5Cmathcal%7BI%7D_%7B%5Cmathrm%7Bcomplete%7D%7D(%5Cell)%0A%20%20-%20%5Cmathbb%7BE%7D_%7Bw%20%5Cmid%20%5Cell%7D%5C!%5Cleft%5B%5Coperatorname%7BVar%7D_%7Bm%20%5Cmid%20w,%20%5Cell%7D%5C!%5Cleft(%5Cnabla_%5Cell%20%5Clog%20p(w,%20m%20%5Cmid%20%5Cell)%5Cright)%5Cright%5D:%0A"></p>
<p>marginal Fisher information equals complete-data Fisher information (as if <img src="https://latex.codecogs.com/png.latex?m"> were observed) minus the expected conditional covariance of the complete-data score over the integrated-out variable <img src="https://latex.codecogs.com/png.latex?m">. Here the complete-data score with respect to <img src="https://latex.codecogs.com/png.latex?%5Cell_j"> is <img src="https://latex.codecogs.com/png.latex?%5Cpartial_%7B%5Cell_j%7D%20%5Clog%20p(%5Calpha,%20a%20%5Cmid%20%5Cell)%20=%20e%5E%7B-2%5Cell_j%7D%20a_j%5E2%20-%201">. Evaluating its conditional covariance under <img src="https://latex.codecogs.com/png.latex?m%20%5Cmid%20%5Ctheta,%20h_%7B%5Cmathrm%7Braw%7D%7D,%20%5Cell%20%5Csim%20%5Cmathcal%7BN%7D(%5Chat%7Bm%7D,%20P%5E%7B-1%7D)"> and averaging over <img src="https://latex.codecogs.com/png.latex?(%5Ctheta,%20h_%7B%5Cmathrm%7Braw%7D%7D)%20%5Cmid%20%5Cell"> gives missing information <img src="https://latex.codecogs.com/png.latex?4%5Coperatorname%7Bdiag%7D(%5Cboldsymbol%7B%5Cpi%7D)%20-%202%5Cboldsymbol%7B%5Cpi%7D%5Cboldsymbol%7B%5Cpi%7D%5E%5Ctop">, leaving</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BI%7D_%7B%5Ctheta,h%7D(%5Cell)%20=%20%5Cmathbb%7BE%7D_%7B%5Ctheta,h%20%5Cmid%20%5Cell%7D%5C!%5Cleft%5B-%5Cnabla%5E2_%5Cell%20%5Clog%20p(%5Ctheta,%20h_%7B%5Cmathrm%7Braw%7D%7D%20%5Cmid%20%5Cell)%5Cright%5D%0A=%202I_J%20-%204%5Coperatorname%7Bdiag%7D(%5Cboldsymbol%7B%5Cpi%7D)%20+%202%5Cboldsymbol%7B%5Cpi%7D%5Cboldsymbol%7B%5Cpi%7D%5E%5Ctop.%0A"></p>
<p>Both this conditional score-covariance subtraction and the direct Slepian–Bangs Gaussian trace formula <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7B1%7D%7B2%7D%5Coperatorname%7Btr%7D%5C!%5Cbig(P_%7B%5Cmathrm%7Bprior%7D%7D%5E%7B%5Cmathrm%7Bs2z%7D%7D(%5Cpartial_%7B%5Cell_j%7D%5CSigma_%7B%5Ctheta,h%7D)P_%7B%5Cmathrm%7Bprior%7D%7D%5E%7B%5Cmathrm%7Bs2z%7D%7D(%5Cpartial_%7B%5Cell_k%7D%5CSigma_%7B%5Ctheta,h%7D)%5Cbig)"> are proved equal in <code>S2ZFisher.VaryingSdsLouis</code>. At homogeneous variances (<img src="https://latex.codecogs.com/png.latex?s_j%20%5Cequiv%20%5Ctau">, <img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Cpi%7D%20=%20%5Cfrac%7B%5Ctau%5E%7B-2%7D%7D%7Bs_0%5E%7B-2%7D%20+%20J%5Ctau%5E%7B-2%7D%7D%5Cmathbf%7B1%7D_J">) along <img src="https://latex.codecogs.com/png.latex?%5Cpartial%5Cell/%5Cpartial%5Clog%5Ctau%20=%20%5Cmathbf%7B1%7D_J">, <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B1%7D_J%5E%5Ctop%20%5Cmathcal%7BI%7D_%7B%5Ctheta,h%7D(%5Cell)%5Cmathbf%7B1%7D_J%20=%202(J%20-%201)%20+%20%5Cfrac%7B2%5Ctau%5E4%7D%7B(J%20s_0%5E2%20+%20%5Ctau%5E2)%5E2%7D">. Setting group SD heterogeneity to <code>0.00</code> in Figure 3 shows that even with heavily unbalanced group sample sizes <img src="https://latex.codecogs.com/png.latex?n%20=%20(4,%207,%2012,%2018,%2025,%2034)">, the S2Z log-SD Fisher block is strictly diagonal (<code>max|ρ| = 0.000</code>, <code>MI = 0.000</code>), because the prior hierarchy contributes one latent draw <img src="https://latex.codecogs.com/png.latex?a_j"> per group regardless of <img src="https://latex.codecogs.com/png.latex?n_j">. (A parallel simplification holds for distributional residual SDs <img src="https://latex.codecogs.com/png.latex?%5Clog%5Csigma_i%20=%20%5Ctheta_%5Csigma%20+%20c_%7B%5Csigma,%20g%5Bi%5D%7D">: every Gaussian observation contributes constant Fisher weight <img src="https://latex.codecogs.com/png.latex?2"> to <img src="https://latex.codecogs.com/png.latex?%5Clog%5Csigma_i">, so equal group counts <img src="https://latex.codecogs.com/png.latex?n_g%20=%20n_0"> make <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BI%7D_%7B%5Cmathrm%7Blik%7D%7D(%5Ctheta_%5Csigma,%20h_%7B%5Csigma,%5Cmathrm%7Braw%7D%7D)%20=%200"> even when heteroskedasticity makes the mean submodel unbalanced.)</p>
<figure class="quarto-float quarto-float-fig figure" id="fig-varying-sds">
  <div class="s2z-fig" id="viz-varying-sds">
    <div class="s2z-fig-head">
      <div class="s2z-fig-title">Group-Varying Standard Deviations: Location and Log-SD Correlations</div>
      <div class="s2z-fig-subtitle">Unbalanced group counts n = (4, 7, 12, 18, 25, 34). Top row: conditional location precision. Bottom row: log-SD precision via Louis's identity</div>
    </div>

    <div class="s2z-toolbar">
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Log-SD hyper-scale λ_ω</span><span class="s2z-ctrl-val" id="vsd-lam-val">0.45</span></div>
        <input type="range" id="vsd-lam" min="0.0" max="0.9" step="0.02" value="0.45">
      </div>
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Group SD heterogeneity scale</span><span class="s2z-ctrl-val" id="vsd-het-val">0.00</span></div>
        <input type="range" id="vsd-het" min="0.0" max="1.4" step="0.05" value="0.00">
      </div>
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Residual SD σ_y</span><span class="s2z-ctrl-val" id="vsd-sig-val">0.25</span></div>
        <input type="range" id="vsd-sig" min="-1.0" max="0.4" step="0.001" value="-0.60206">
      </div>
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Intercept prior SD s₀</span><span class="s2z-ctrl-val" id="vsd-s0-val">2.50</span></div>
        <input type="range" id="vsd-s0" min="-0.5" max="1.2" step="0.001" value="0.39794">
      </div>
    </div>

    <div class="s2z-facets-2" style="margin-bottom:12px;">
      <div class="s2z-facet">
        <div class="s2z-strip"><span>Location: Conventional (α, a₁..a₆)</span><span class="s2z-strip-sub" id="vsd-b-loc-conv"></span></div>
        <canvas class="s2z-canvas" id="vsd-c-loc-conv"></canvas>
      </div>
      <div class="s2z-facet">
        <div class="s2z-strip"><span>Location: Marginalized S2Z (θ, h₁..h₅)</span><span class="s2z-strip-sub" id="vsd-b-loc-s2z"></span></div>
        <canvas class="s2z-canvas" id="vsd-c-loc-s2z"></canvas>
      </div>
    </div>

    <div class="s2z-facets-2">
      <div class="s2z-facet">
        <div class="s2z-strip"><span>Log-SD: Conventional (γ, z₁..z₆, λ_ω)</span><span class="s2z-strip-sub" id="vsd-b-sd-conv"></span></div>
        <canvas class="s2z-canvas" id="vsd-c-sd-conv"></canvas>
      </div>
      <div class="s2z-facet">
        <div class="s2z-strip"><span>Log-SD: S2Z + Louis (γ_s, ζ₁..ζ₅, λ_ω)</span><span class="s2z-strip-sub" id="vsd-b-sd-s2z"></span></div>
        <canvas class="s2z-canvas" id="vsd-c-sd-s2z"></canvas>
      </div>
    </div>

    <div class="s2z-footer-bar" id="vsd-inspector"></div>
  </div>
  <figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig">
    Figure&nbsp;3: Posterior correlation blocks for a six-group model with group-varying prior standard deviations and unbalanced sample sizes \(n = (4, 7, 12, 18, 25, 34)\). At zero SD heterogeneity (\(\zeta_{\log s} = 0\)), the S2Z log-SD Fisher matrix diagonalizes despite the unequal sample sizes.
  </figcaption>
</figure>
</section>
<section id="empirical-check-across-the-four-extensions" class="level3">
<h3 class="anchored" data-anchor-id="empirical-check-across-the-four-extensions">3.5 Empirical Check Across the Four Extensions</h3>
<p>To see how these Fisher reductions show up in NUTS sampling, Figure 4 compares conventional non-centered coordinates against physical S2Z coordinates across synthetic benchmarks for each of the four extensions in Sections 3.1–3.4 (4 chains × 1,500 post-warmup draws = 6,000 draws after 1,000 warmup, <code>adapt_delta = 0.95</code>, <code>max_treedepth = 12</code>):</p>
<ol type="1">
<li><strong>Correlated varying slopes</strong> (<img src="https://latex.codecogs.com/png.latex?J%20=%2010"> unbalanced groups, correlated intercepts and slopes with centered covariates plus a fixed-only predictor, <img src="https://latex.codecogs.com/png.latex?%5Csigma_y%20=%200.20">).</li>
<li><strong>Bernoulli GLM with a <code>logistic(0, 1)</code> intercept prior</strong> (<img src="https://latex.codecogs.com/png.latex?J%20=%2012"> groups, explicit standardized mean <img src="https://latex.codecogs.com/png.latex?z_%7B%5Cmathrm%7Bmean%7D%7D"> decoupled from the likelihood).</li>
<li><strong>Crossed two-factor hierarchy</strong> (<img src="https://latex.codecogs.com/png.latex?G_1%20=%208"> with random intercepts and slopes crossed with <img src="https://latex.codecogs.com/png.latex?G_2%20=%206"> random intercepts, removing <img src="https://latex.codecogs.com/png.latex?2%20+%201%20=%203"> shift dimensions via joint Matheron recovery).</li>
<li><strong>Heavy-tailed Student-<img src="https://latex.codecogs.com/png.latex?t"> group effects</strong> (<img src="https://latex.codecogs.com/png.latex?J%20=%2014"> groups, <img src="https://latex.codecogs.com/png.latex?%5Cnu%20=%204"> random intercepts and slopes via per-group inverse-gamma scales).</li>
</ol>
<figure class="quarto-float quarto-float-fig figure" id="fig-brms-bakeoff">
  <div class="s2z-fig" id="viz-brms-bakeoff">
    <div class="s2z-fig-head">
      <div class="s2z-fig-title">Empirical NUTS Efficiency Across the Four Hierarchical Extensions</div>
      <div class="s2z-fig-subtitle">4 chains × 1,500 post-warmup draws (6,000 transitions, adapt_delta = 0.95); click any row to inspect diagnostics</div>
    </div>

    <div class="s2z-toolbar">
      <div class="s2z-btn-group">
        <span class="s2z-btn-group-label">Metric:</span>
        <button type="button" class="s2z-btn active" data-bb-metric="b_ess">Median pop ESS / 1k grads</button>
        <button type="button" class="s2z-btn" data-bb-metric="min_ess">Min native ESS / 1k</button>
        <button type="button" class="s2z-btn" data-bb-metric="lf_per_dr">Leapfrogs / draw</button>
        <button type="button" class="s2z-btn" data-bb-metric="leapfrog">Total leapfrogs (×10³)</button>
        <button type="button" class="s2z-btn" data-bb-metric="mi">Sampled mutual info</button>
      </div>
    </div>

    <div class="s2z-facet">
      <div class="s2z-strip"><span>Conventional vs Sum-to-Zero Across Four Extensions</span></div>
      <canvas class="s2z-canvas" id="bb-canvas"></canvas>
    </div>

    <div class="s2z-footer-bar" id="bb-detail"></div>
  </div>
  <figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig">
    Figure&nbsp;4: Empirical NUTS diagnostics across paired fits of the four extension models. Every S2Z run finished with zero divergences, reduced leapfrog steps per draw by 1.4× to 25.4×, and matched conventional superpopulation posterior means within 1.43 to 2.71 combined MCSE.
  </figcaption>
</figure>
<p>Across all four models, S2Z finished with zero divergences, reproduced the superpopulation posteriors within <code>1.43</code>–<code>2.71</code> combined MCSE, cut leapfrog steps per draw from <code>502.3 → 46.6</code>, <code>17.6 → 12.5</code>, <code>401.7 → 15.8</code>, and <code>306.9 → 59.3</code>, and raised median population ESS per 1,000 gradients by <code>3.1×</code> to <code>54.9×</code>. (In Model 1, the single largest native pairwise correlation is <code>0.961</code> in S2Z versus <code>0.908</code> in conventional coordinates because conventional non-centering whitens each group’s local <img src="https://latex.codecogs.com/png.latex?2%20%5Ctimes%202"> correlation <img src="https://latex.codecogs.com/png.latex?%5Crho%20=%20-0.65"> inside <img src="https://latex.codecogs.com/png.latex?L_%5CSigma"> while concentrating global dependence into a rank-2 shift ridge of condition number <code>22,877</code>; S2Z keeps the harmless local <img src="https://latex.codecogs.com/png.latex?2%20%5Ctimes%202"> correlation on <code>(c_int[j], c_slp[j])</code> while dropping the global condition number to <code>1,524</code>.)</p>
</section>
</section>
<section id="the-eight-schools-on-their-own-six-fisher-information-matrices" class="level2">
<h2 class="anchored" data-anchor-id="the-eight-schools-on-their-own-six-fisher-information-matrices">4. The Eight Schools on Their Own: Six Fisher Information Matrices</h2>
<p>With the general extensions in hand, we can examine the classic <strong>Eight Schools</strong> benchmark <span class="citation" data-cites="rubin1981 papaspiliopoulos2007 betancourt2015">(Rubin 1981; Papaspiliopoulos et al. 2007; Betancourt and Girolami 2015)</span> in complete detail. Here <img src="https://latex.codecogs.com/png.latex?J%20=%208"> schools have observed treatment estimates <img src="https://latex.codecogs.com/png.latex?y%20%5Cmid%20%5Cphi%20%5Csim%20%5Cmathcal%7BN%7D(q(%5Cphi),%20W%5E%7B-1%7D)"> with known standard errors <img src="https://latex.codecogs.com/png.latex?%5Csigma_j">, <img src="https://latex.codecogs.com/png.latex?W%20=%20%5Coperatorname%7Bdiag%7D(w_1,%20%5Cdots,%20w_J)">, <img src="https://latex.codecogs.com/png.latex?w_j%20=%20%5Csigma_j%5E%7B-2%7D">, <img src="https://latex.codecogs.com/png.latex?w_%5Cbullet%20=%20%5Csum_%7Bj=1%7D%5EJ%20w_j">, <img src="https://latex.codecogs.com/png.latex?%5Cmu%20%5Csim%20%5Cmathcal%7BN%7D(0,%20s_%5Cmu%5E2)"> (<img src="https://latex.codecogs.com/png.latex?s_%5Cmu%20=%205">), and <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%5Csim%20%5Ctext%7BHalf-Cauchy%7D(0,%205)">.</p>
<p>Crossing the three standard location families (<code>centered</code>, <code>scaled</code>, <code>non_centered</code>) with <code>{conv, s2z}</code> produces six parameterizations of the exact same posterior. Using <code>sum_to_zero_constrain()</code>, the physical (<code>scaled_s2z</code>) and whitened (<code>non_centered_s2z</code>) S2Z models differ only in whether <code>tau</code> scales the prior or the linear predictor:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// scaled_s2z: physical contrasts in the predictor, whitened standard normal in the prior</span></span>
<span id="cb6-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb6-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> tau;</span>
<span id="cb6-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta;</span>
<span id="cb6-5">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J <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>] h_j;</span>
<span id="cb6-6">}</span>
<span id="cb6-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb6-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] q_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta <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;">sum_to_zero_constrain</span>(h_j);</span>
<span id="cb6-9">}</span>
<span id="cb6-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb6-11">  tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cauchy</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>
<span id="cb6-12">  theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(tau) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> J));</span>
<span id="cb6-13">  (h_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> tau) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb6-14">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(J <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(tau);</span>
<span id="cb6-15">  y_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(q_j, sigma_j);</span>
<span id="cb6-16">}</span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// non_centered_s2z: standard normal parameters scaled by tau in the predictor</span></span>
<span id="cb7-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb7-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> tau;</span>
<span id="cb7-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta;</span>
<span id="cb7-5">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J <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>] z_j;</span>
<span id="cb7-6">}</span>
<span id="cb7-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb7-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] q_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> tau <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;">sum_to_zero_constrain</span>(z_j);</span>
<span id="cb7-9">}</span>
<span id="cb7-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb7-11">  tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cauchy</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>
<span id="cb7-12">  theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(tau) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> J));</span>
<span id="cb7-13">  z_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb7-14">  y_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(q_j, sigma_j);</span>
<span id="cb7-15">}</span></code></pre></div></div>
<p>Meanwhile, <code>centered_s2z</code> keeps the <img src="https://latex.codecogs.com/png.latex?J"> school means <img src="https://latex.codecogs.com/png.latex?q%20%5Cin%20%5Cmathbb%7BR%7D%5EJ"> directly in <code>parameters</code> and integrates <img src="https://latex.codecogs.com/png.latex?%5Cmu"> out of <img src="https://latex.codecogs.com/png.latex?q%20%5Cmid%20%5Cmu,%20%5Ctau%20%5Csim%20%5Cmathcal%7BN%7D(%5Cmu%5Cmathbf%7B1%7D_J,%20%5Ctau%5E2%20I_J)"> by setting <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bq%7D%20=%20J%5E%7B-1%7D%5Cmathbf%7B1%7D_J%5E%5Ctop%20q%20%5Csim%20%5Cmathcal%7BN%7D(0,%20V_%5Ctheta(%5Ctau))"> and <img src="https://latex.codecogs.com/png.latex?q%20-%20%5Cbar%7Bq%7D%5Cmathbf%7B1%7D_J%20%5Csim%20%5Cmathcal%7BN%7D(0,%20%5Ctau%5E2%20P_0)">.</p>
<p>On Stan’s unconstrained coordinates <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5Cphi%7D%20=%20(%5Ceta,%20%5Cpsi)"> with <img src="https://latex.codecogs.com/png.latex?%5Ceta%20=%20%5Clog%5Ctau">, let <img src="https://latex.codecogs.com/png.latex?H%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BJ%20%5Ctimes%20(J-1)%7D"> denote the linear operator applied by <code>sum_to_zero_constrain()</code>, let <img src="https://latex.codecogs.com/png.latex?h_0(%5Ctau)%20=%20%5Cfrac%7B100%5Ctau%5E2%7D%7B(25%20+%20%5Ctau%5E2)%5E2%7D"> denote the half-Cauchy log-scale curvature, and define</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ckappa_%7B%5Ceta%5Ceta%7D(%5Ctheta,%20%5Ctau)%20=%20%5Cfrac%7B2%5Ctau%5E2%20s_%5Cmu%5E2%7D%7BJ%20V_%5Ctheta%5E2%7D%20-%20%5Cfrac%7B2%5Ctheta%5E2%20%5Ctau%5E2(s_%5Cmu%5E2%20-%20%5Ctau%5E2/J)%7D%7BJ%20V_%5Ctheta%5E3%7D,%0A%5Cqquad%0A%5Cbar%7B%5Ckappa%7D_%7B%5Ceta%5Ceta%7D(%5Ctau)%20=%20%5Cmathbb%7BE%7D_%7B%5Ctheta%20%5Cmid%20%5Ctau%7D%5B%5Ckappa_%7B%5Ceta%5Ceta%7D(%5Ctheta,%20%5Ctau)%5D%20=%20%5Cfrac%7B2%5Ctau%5E4%7D%7B(J%20s_%5Cmu%5E2%20+%20%5Ctau%5E2)%5E2%7D.%0A"></p>
<p>Writing <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BI%7D_%7B%5Cmathrm%7Blik%7D%7D(%5Ctilde%7B%5Cphi%7D)%20=%20J_q(%5Ctilde%7B%5Cphi%7D)%5E%5Ctop%20W%20J_q(%5Ctilde%7B%5Cphi%7D)"> for the likelihood Fisher matrix, <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BG%7D(%5Ctilde%7B%5Cphi%7D)%20=%20%5Cmathcal%7BI%7D_%7B%5Cmathrm%7Blik%7D%7D(%5Ctilde%7B%5Cphi%7D)%20-%20%5Cnabla%5E2_%7B%5Ctilde%7B%5Cphi%7D%7D%20%5Clog%20p(%5Ctilde%7B%5Cphi%7D)"> for the pointwise metric, and <img src="https://latex.codecogs.com/png.latex?%5Coverline%7B%5Cmathcal%7BG%7D%7D(%5Ctau)%20=%20%5Cmathbb%7BE%7D_%7B%5Cpsi%20%5Cmid%20%5Ctau%7D%5B%5Cmathcal%7BG%7D(%5Ceta,%20%5Cpsi)%5D"> for the prior-integrated Fisher matrix gives all six models in closed form:</p>
<ol type="1">
<li><p><strong><code>centered_conv</code></strong> (<img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5Cphi%7D%20=%20(%5Ceta,%20%5Cmu,%20q)%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BJ+2%7D">): <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BG%7D%5E%7B%5Cmathrm%7Bcent,conv%7D%7D%20=%0A%5Cbegin%7Bpmatrix%7D%0Ah_0(%5Ctau)%20+%20%5Cdfrac%7B2%5C%7Cq%20-%20%5Cmu%5Cmathbf%7B1%7D%5C%7C%5E2%7D%7B%5Ctau%5E2%7D%20&amp;%20%5Cdfrac%7B2%5Cmathbf%7B1%7D%5E%5Ctop(q%20-%20%5Cmu%5Cmathbf%7B1%7D)%7D%7B%5Ctau%5E2%7D%20&amp;%20-%5Cdfrac%7B2(q%20-%20%5Cmu%5Cmathbf%7B1%7D)%5E%5Ctop%7D%7B%5Ctau%5E2%7D%20%5C%5C%5B8pt%5D%0A%5Cdfrac%7B2%5Cmathbf%7B1%7D%5E%5Ctop(q%20-%20%5Cmu%5Cmathbf%7B1%7D)%7D%7B%5Ctau%5E2%7D%20&amp;%20s_%5Cmu%5E%7B-2%7D%20+%20J%5Ctau%5E%7B-2%7D%20&amp;%20-%5Ctau%5E%7B-2%7D%5Cmathbf%7B1%7D%5E%5Ctop%20%5C%5C%5B8pt%5D%0A-%5Cdfrac%7B2(q%20-%20%5Cmu%5Cmathbf%7B1%7D)%7D%7B%5Ctau%5E2%7D%20&amp;%20-%5Ctau%5E%7B-2%7D%5Cmathbf%7B1%7D%20&amp;%20W%20+%20%5Ctau%5E%7B-2%7DI_J%0A%5Cend%7Bpmatrix%7D,%0A"> <img src="https://latex.codecogs.com/png.latex?%0A%5Coverline%7B%5Cmathcal%7BG%7D%7D%5E%7B%5Cmathrm%7Bcent,conv%7D%7D(%5Ctau)%20=%0A%5Cbegin%7Bpmatrix%7D%0Ah_0(%5Ctau)%20+%202J%20&amp;%200%20&amp;%20%5Cmathbf%7B0%7D%5E%5Ctop%20%5C%5C%0A0%20&amp;%20s_%5Cmu%5E%7B-2%7D%20+%20J%5Ctau%5E%7B-2%7D%20&amp;%20-%5Ctau%5E%7B-2%7D%5Cmathbf%7B1%7D%5E%5Ctop%20%5C%5C%0A%5Cmathbf%7B0%7D%20&amp;%20-%5Ctau%5E%7B-2%7D%5Cmathbf%7B1%7D%20&amp;%20W%20+%20%5Ctau%5E%7B-2%7DI_J%0A%5Cend%7Bpmatrix%7D.%0A"></p></li>
<li><p><strong><code>centered_s2z</code></strong> (<img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5Cphi%7D%20=%20(%5Ceta,%20q)%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BJ+1%7D">, <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bq%7D%20=%20J%5E%7B-1%7D%5Cmathbf%7B1%7D%5E%5Ctop%20q">): <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BG%7D%5E%7B%5Cmathrm%7Bcent,s2z%7D%7D%20=%0A%5Cbegin%7Bpmatrix%7D%0Ah_0(%5Ctau)%20+%20%5Ckappa_%7B%5Ceta%5Ceta%7D(%5Cbar%7Bq%7D,%20%5Ctau)%20+%20%5Cdfrac%7B2%5C%7CP_0%20q%5C%7C%5E2%7D%7B%5Ctau%5E2%7D%20&amp;%20-%5Cdfrac%7B2(P_0%20q)%5E%5Ctop%7D%7B%5Ctau%5E2%7D%20-%20%5Cdfrac%7B2%5Cbar%7Bq%7D%5Ctau%5E2%7D%7BJ%5E2%20V_%5Ctheta%5E2%7D%5Cmathbf%7B1%7D%5E%5Ctop%20%5C%5C%5B8pt%5D%0A-%5Cdfrac%7B2%20P_0%20q%7D%7B%5Ctau%5E2%7D%20-%20%5Cdfrac%7B2%5Cbar%7Bq%7D%5Ctau%5E2%7D%7BJ%5E2%20V_%5Ctheta%5E2%7D%5Cmathbf%7B1%7D%20&amp;%20W%20+%20%5Ctau%5E%7B-2%7D%20P_0%20+%20%5Cdfrac%7B1%7D%7BJ%5E2%20V_%5Ctheta%7D%5Cmathbf%7B1%7D%5Cmathbf%7B1%7D%5E%5Ctop%0A%5Cend%7Bpmatrix%7D,%0A"> <img src="https://latex.codecogs.com/png.latex?%0A%5Coverline%7B%5Cmathcal%7BG%7D%7D%5E%7B%5Cmathrm%7Bcent,s2z%7D%7D(%5Ctau)%20=%0A%5Cbegin%7Bpmatrix%7D%0Ah_0(%5Ctau)%20+%202(J%20-%201)%20+%20%5Cbar%7B%5Ckappa%7D_%7B%5Ceta%5Ceta%7D(%5Ctau)%20&amp;%20%5Cmathbf%7B0%7D%5E%5Ctop%20%5C%5C%5B6pt%5D%0A%5Cmathbf%7B0%7D%20&amp;%20W%20+%20%5Ctau%5E%7B-2%7D%20P_0%20+%20%5Cdfrac%7B1%7D%7BJ%5E2%20V_%5Ctheta%7D%5Cmathbf%7B1%7D%5Cmathbf%7B1%7D%5E%5Ctop%0A%5Cend%7Bpmatrix%7D.%0A"></p></li>
<li><p><strong><code>scaled_conv</code></strong> (<img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5Cphi%7D%20=%20(%5Ceta,%20%5Cmu,%20r)%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BJ+2%7D">, <img src="https://latex.codecogs.com/png.latex?q%20=%20%5Cmu%5Cmathbf%7B1%7D%20+%20r">): <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BG%7D%5E%7B%5Cmathrm%7Bscal,conv%7D%7D%20=%0A%5Cbegin%7Bpmatrix%7D%0Ah_0(%5Ctau)%20+%20%5Cdfrac%7B2%5C%7Cr%5C%7C%5E2%7D%7B%5Ctau%5E2%7D%20&amp;%200%20&amp;%20-%5Cdfrac%7B2%20r%5E%5Ctop%7D%7B%5Ctau%5E2%7D%20%5C%5C%5B8pt%5D%0A0%20&amp;%20s_%5Cmu%5E%7B-2%7D%20+%20w_%5Cbullet%20&amp;%20%5Cmathbf%7Bw%7D%5E%5Ctop%20%5C%5C%5B8pt%5D%0A-%5Cdfrac%7B2%20r%7D%7B%5Ctau%5E2%7D%20&amp;%20%5Cmathbf%7Bw%7D%20&amp;%20W%20+%20%5Ctau%5E%7B-2%7DI_J%0A%5Cend%7Bpmatrix%7D,%0A"> <img src="https://latex.codecogs.com/png.latex?%0A%5Coverline%7B%5Cmathcal%7BG%7D%7D%5E%7B%5Cmathrm%7Bscal,conv%7D%7D(%5Ctau)%20=%0A%5Cbegin%7Bpmatrix%7D%0Ah_0(%5Ctau)%20+%202J%20&amp;%200%20&amp;%20%5Cmathbf%7B0%7D%5E%5Ctop%20%5C%5C%0A0%20&amp;%20s_%5Cmu%5E%7B-2%7D%20+%20w_%5Cbullet%20&amp;%20%5Cmathbf%7Bw%7D%5E%5Ctop%20%5C%5C%0A%5Cmathbf%7B0%7D%20&amp;%20%5Cmathbf%7Bw%7D%20&amp;%20W%20+%20%5Ctau%5E%7B-2%7DI_J%0A%5Cend%7Bpmatrix%7D.%0A"></p></li>
<li><p><strong><code>scaled_s2z</code></strong> (<img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5Cphi%7D%20=%20(%5Ceta,%20%5Ctheta,%20h)%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BJ+1%7D">, <img src="https://latex.codecogs.com/png.latex?q%20=%20%5Ctheta%5Cmathbf%7B1%7D%20+%20H%20h">): <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BG%7D%5E%7B%5Cmathrm%7Bscal,s2z%7D%7D%20=%0A%5Cbegin%7Bpmatrix%7D%0Ah_0(%5Ctau)%20+%20%5Ckappa_%7B%5Ceta%5Ceta%7D(%5Ctheta,%20%5Ctau)%20+%20%5Cdfrac%7B2%5C%7Ch%5C%7C%5E2%7D%7B%5Ctau%5E2%7D%20&amp;%20-%5Cdfrac%7B2%5Ctheta%5Ctau%5E2%7D%7BJ%20V_%5Ctheta%5E2%7D%20&amp;%20-%5Cdfrac%7B2%20h%5E%5Ctop%7D%7B%5Ctau%5E2%7D%20%5C%5C%5B8pt%5D%0A-%5Cdfrac%7B2%5Ctheta%5Ctau%5E2%7D%7BJ%20V_%5Ctheta%5E2%7D%20&amp;%20V_%5Ctheta%5E%7B-1%7D%20+%20w_%5Cbullet%20&amp;%20%5Cmathbf%7Bw%7D%5E%5Ctop%20H%20%5C%5C%5B8pt%5D%0A-%5Cdfrac%7B2%20h%7D%7B%5Ctau%5E2%7D%20&amp;%20H%5E%5Ctop%20%5Cmathbf%7Bw%7D%20&amp;%20H%5E%5Ctop%20W%20H%20+%20%5Ctau%5E%7B-2%7DI_%7BJ-1%7D%0A%5Cend%7Bpmatrix%7D,%0A"> <img src="https://latex.codecogs.com/png.latex?%0A%5Coverline%7B%5Cmathcal%7BG%7D%7D%5E%7B%5Cmathrm%7Bscal,s2z%7D%7D(%5Ctau)%20=%0A%5Cbegin%7Bpmatrix%7D%0Ah_0(%5Ctau)%20+%202(J%20-%201)%20+%20%5Cbar%7B%5Ckappa%7D_%7B%5Ceta%5Ceta%7D(%5Ctau)%20&amp;%200%20&amp;%20%5Cmathbf%7B0%7D%5E%5Ctop%20%5C%5C%5B6pt%5D%0A0%20&amp;%20V_%5Ctheta%5E%7B-1%7D%20+%20w_%5Cbullet%20&amp;%20%5Cmathbf%7Bw%7D%5E%5Ctop%20H%20%5C%5C%5B6pt%5D%0A%5Cmathbf%7B0%7D%20&amp;%20H%5E%5Ctop%20%5Cmathbf%7Bw%7D%20&amp;%20H%5E%5Ctop%20W%20H%20+%20%5Ctau%5E%7B-2%7DI_%7BJ-1%7D%0A%5Cend%7Bpmatrix%7D.%0A"></p></li>
<li><p><strong><code>non_centered_conv</code></strong> (<img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5Cphi%7D%20=%20(%5Ceta,%20%5Cmu,%20z)%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BJ+2%7D">, <img src="https://latex.codecogs.com/png.latex?q%20=%20%5Cmu%5Cmathbf%7B1%7D%20+%20%5Ctau%20z">): <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BG%7D%5E%7B%5Cmathrm%7Bnc,conv%7D%7D%20=%0A%5Cbegin%7Bpmatrix%7D%0Ah_0(%5Ctau)%20+%20%5Ctau%5E2%20z%5E%5Ctop%20W%20z%20&amp;%20%5Ctau%20%5Cmathbf%7Bw%7D%5E%5Ctop%20z%20&amp;%20%5Ctau%5E2%20z%5E%5Ctop%20W%20%5C%5C%0A%5Ctau%20%5Cmathbf%7Bw%7D%5E%5Ctop%20z%20&amp;%20s_%5Cmu%5E%7B-2%7D%20+%20w_%5Cbullet%20&amp;%20%5Ctau%20%5Cmathbf%7Bw%7D%5E%5Ctop%20%5C%5C%0A%5Ctau%5E2%20W%20z%20&amp;%20%5Ctau%20%5Cmathbf%7Bw%7D%20&amp;%20I_J%20+%20%5Ctau%5E2%20W%0A%5Cend%7Bpmatrix%7D,%0A"> <img src="https://latex.codecogs.com/png.latex?%0A%5Coverline%7B%5Cmathcal%7BG%7D%7D%5E%7B%5Cmathrm%7Bnc,conv%7D%7D(%5Ctau)%20=%0A%5Cbegin%7Bpmatrix%7D%0Ah_0(%5Ctau)%20+%20%5Ctau%5E2%20w_%5Cbullet%20&amp;%200%20&amp;%20%5Cmathbf%7B0%7D%5E%5Ctop%20%5C%5C%0A0%20&amp;%20s_%5Cmu%5E%7B-2%7D%20+%20w_%5Cbullet%20&amp;%20%5Ctau%20%5Cmathbf%7Bw%7D%5E%5Ctop%20%5C%5C%0A%5Cmathbf%7B0%7D%20&amp;%20%5Ctau%20%5Cmathbf%7Bw%7D%20&amp;%20I_J%20+%20%5Ctau%5E2%20W%0A%5Cend%7Bpmatrix%7D.%0A"></p></li>
<li><p><strong><code>non_centered_s2z</code></strong> (<img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5Cphi%7D%20=%20(%5Ceta,%20%5Ctheta,%20z)%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BJ+1%7D">, <img src="https://latex.codecogs.com/png.latex?q%20=%20%5Ctheta%5Cmathbf%7B1%7D%20+%20%5Ctau%20H%20z">): <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BG%7D%5E%7B%5Cmathrm%7Bnc,s2z%7D%7D%20=%0A%5Cbegin%7Bpmatrix%7D%0Ah_0(%5Ctau)%20+%20%5Ckappa_%7B%5Ceta%5Ceta%7D(%5Ctheta,%20%5Ctau)%20+%20%5Ctau%5E2%20z%5E%5Ctop%20H%5E%5Ctop%20W%20H%20z%20&amp;%20%5Ctau%20%5Cmathbf%7Bw%7D%5E%5Ctop%20H%20z%20-%20%5Cdfrac%7B2%5Ctheta%5Ctau%5E2%7D%7BJ%20V_%5Ctheta%5E2%7D%20&amp;%20%5Ctau%5E2%20z%5E%5Ctop%20H%5E%5Ctop%20W%20H%20%5C%5C%5B8pt%5D%0A%5Ctau%20%5Cmathbf%7Bw%7D%5E%5Ctop%20H%20z%20-%20%5Cdfrac%7B2%5Ctheta%5Ctau%5E2%7D%7BJ%20V_%5Ctheta%5E2%7D%20&amp;%20V_%5Ctheta%5E%7B-1%7D%20+%20w_%5Cbullet%20&amp;%20%5Ctau%20%5Cmathbf%7Bw%7D%5E%5Ctop%20H%20%5C%5C%5B8pt%5D%0A%5Ctau%5E2%20H%5E%5Ctop%20W%20H%20z%20&amp;%20%5Ctau%20H%5E%5Ctop%20%5Cmathbf%7Bw%7D%20&amp;%20I_%7BJ-1%7D%20+%20%5Ctau%5E2%20H%5E%5Ctop%20W%20H%0A%5Cend%7Bpmatrix%7D,%0A"> <img src="https://latex.codecogs.com/png.latex?%0A%5Coverline%7B%5Cmathcal%7BG%7D%7D%5E%7B%5Cmathrm%7Bnc,s2z%7D%7D(%5Ctau)%20=%0A%5Cbegin%7Bpmatrix%7D%0Ah_0(%5Ctau)%20+%20%5Cdfrac%7BJ%20-%201%7D%7BJ%7D%5Ctau%5E2%20w_%5Cbullet%20+%20%5Cbar%7B%5Ckappa%7D_%7B%5Ceta%5Ceta%7D(%5Ctau)%20&amp;%200%20&amp;%20%5Cmathbf%7B0%7D%5E%5Ctop%20%5C%5C%5B6pt%5D%0A0%20&amp;%20V_%5Ctheta%5E%7B-1%7D%20+%20w_%5Cbullet%20&amp;%20%5Ctau%20%5Cmathbf%7Bw%7D%5E%5Ctop%20H%20%5C%5C%5B6pt%5D%0A%5Cmathbf%7B0%7D%20&amp;%20%5Ctau%20H%5E%5Ctop%20%5Cmathbf%7Bw%7D%20&amp;%20I_%7BJ-1%7D%20+%20%5Ctau%5E2%20H%5E%5Ctop%20W%20H%0A%5Cend%7Bpmatrix%7D.%0A"></p></li>
</ol>
<section id="why-centered_s2z-stays-correlated-under-equal-school-ses" class="level3">
<h3 class="anchored" data-anchor-id="why-centered_s2z-stays-correlated-under-equal-school-ses">4.1 Why <code>centered_s2z</code> Stays Correlated Under Equal School SEs</h3>
<p>Switch Figure 5 to <code>Balanced (σ = 12.5)</code>. Both <code>scaled_s2z</code> and <code>non_centered_s2z</code> become strictly diagonal off the main diagonal because <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bw%7D%5E%5Ctop%20H%20=%20w_0%20%5Cmathbf%7B1%7D%5E%5Ctop%20H%20=%200"> and <img src="https://latex.codecogs.com/png.latex?H%5E%5Ctop%20W%20H%20=%20w_0%20I_%7BJ-1%7D">. In <code>centered_s2z</code>, however, the <img src="https://latex.codecogs.com/png.latex?(i,%20j)"> off-diagonal precision entry (<img src="https://latex.codecogs.com/png.latex?i%20%5Cne%20j">) is the Schur complement of <img src="https://latex.codecogs.com/png.latex?%5Cmu"> in <code>centered_conv</code>:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_%7B%5Cmathrm%7Bcent,s2z%7D%7D(%5Ctau)_%7Bij%7D%20=%20-%5Cfrac%7B1%7D%7BJ%5Ctau%5E2%7D%20+%20%5Cfrac%7B1%7D%7BJ%5E2(s_%5Cmu%5E2%20+%20%5Ctau%5E2/J)%7D%20=%20-%5Cfrac%7Bs_%5Cmu%5E2%7D%7B%5Ctau%5E2(J%20s_%5Cmu%5E2%20+%20%5Ctau%5E2)%7D%20%3C%200.%0A"></p>
<p>Marginalizing <img src="https://latex.codecogs.com/png.latex?%5Cmu"> removes the <img src="https://latex.codecogs.com/png.latex?(J+2)">th parameter and drops the <img src="https://latex.codecogs.com/png.latex?%5Ctau%5E%7B-3%7D"> grand-mean funnel direction, yet <img src="https://latex.codecogs.com/png.latex?q_1,%20%5Cdots,%20q_J"> remain unrotated school means. Whenever <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%5Cll%20s_%5Cmu">, all eight schools still move positively together around their shared sample average <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bq%7D">. Rotating into <code>sum_to_zero_constrain()</code> coordinates (<code>scaled_s2z</code> and <code>non_centered_s2z</code>) is what turns balanced weights into exact diagonalization.</p>
<figure class="quarto-float quarto-float-fig figure" id="fig-six-heatmaps">
  <div class="s2z-fig" id="viz-six-heatmaps">
    <div class="s2z-fig-head">
      <div class="s2z-fig-title">Eight Schools Fisher and Posterior Correlation Matrices</div>
      <div class="s2z-fig-subtitle">Hover any cell to inspect exact Posterior Correlation R, Prior-Integrated Fisher Ḡ, and Likelihood Fisher I_lik</div>
    </div>

    <div class="s2z-toolbar">
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Between-school SD τ</span><span class="s2z-ctrl-val" id="hm-tau-val">3.50</span></div>
        <input type="range" id="hm-tau" min="-0.82" max="1.45" step="0.001" value="0.54407">
      </div>
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>Prior SD s_μ</span><span class="s2z-ctrl-val" id="hm-smu-val">5.0</span></div>
        <input type="range" id="hm-smu" min="0.5" max="25.0" step="0.5" value="5.0">
      </div>
      <div class="s2z-ctrl">
        <div class="s2z-ctrl-label"><span>School SE divisor (σ_j / c)</span><span class="s2z-ctrl-val" id="hm-prec-val">1.00×</span></div>
        <input type="range" id="hm-prec" min="-0.7" max="1.2" step="0.02" value="0.0">
      </div>
      <div class="s2z-btn-group">
        <span class="s2z-btn-group-label">Data:</span>
        <button type="button" class="s2z-btn" data-hm-regime="balanced">Balanced (σ = 12.5)</button>
        <button type="button" class="s2z-btn active" data-hm-regime="standard">Standard</button>
        <button type="button" class="s2z-btn" data-hm-regime="precise">Precise (σ / 10)</button>
      </div>
      <div class="s2z-btn-group">
        <span class="s2z-btn-group-label">Matrix:</span>
        <button type="button" class="s2z-btn active" data-hm-mode="corr">Posterior Corr R</button>
        <button type="button" class="s2z-btn" data-hm-mode="joint">Joint Fisher Ḡ</button>
        <button type="button" class="s2z-btn" data-hm-mode="lik">Likelihood I_lik</button>
      </div>
    </div>

    <div class="s2z-facets-3">
      <div class="s2z-facet">
        <div class="s2z-strip"><span>centered_conv</span><span class="s2z-strip-sub" id="hm-b-centered_conv"></span></div>
        <canvas class="s2z-canvas" id="hm-c-centered_conv"></canvas>
      </div>
      <div class="s2z-facet">
        <div class="s2z-strip"><span>scaled_conv</span><span class="s2z-strip-sub" id="hm-b-scaled_conv"></span></div>
        <canvas class="s2z-canvas" id="hm-c-scaled_conv"></canvas>
      </div>
      <div class="s2z-facet">
        <div class="s2z-strip"><span>non_centered_conv</span><span class="s2z-strip-sub" id="hm-b-non_centered_conv"></span></div>
        <canvas class="s2z-canvas" id="hm-c-non_centered_conv"></canvas>
      </div>
      <div class="s2z-facet">
        <div class="s2z-strip"><span>centered_s2z</span><span class="s2z-strip-sub" id="hm-b-centered_s2z"></span></div>
        <canvas class="s2z-canvas" id="hm-c-centered_s2z"></canvas>
      </div>
      <div class="s2z-facet">
        <div class="s2z-strip"><span>scaled_s2z</span><span class="s2z-strip-sub" id="hm-b-scaled_s2z"></span></div>
        <canvas class="s2z-canvas" id="hm-c-scaled_s2z"></canvas>
      </div>
      <div class="s2z-facet">
        <div class="s2z-strip"><span>non_centered_s2z</span><span class="s2z-strip-sub" id="hm-b-non_centered_s2z"></span></div>
        <canvas class="s2z-canvas" id="hm-c-non_centered_s2z"></canvas>
      </div>
    </div>

    <div class="s2z-footer-bar" id="hm-inspector"><span>Hover any cell in the six panels above to inspect its exact correlation and Fisher entries.</span></div>
  </div>
  <figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig">
    Figure&nbsp;5: Exact correlation and Fisher matrices across all six Eight Schools parameterizations. Teal cells are positive, red cells are negative, and light neutral cells are zero. Thin navy rules separate \(\log\tau\) and the population location parameter from the school coordinates.
  </figcaption>
</figure>
</section>
<section id="weak-vs-strong-data-non_centered_s2z-vs-scaled_s2z" class="level3">
<h3 class="anchored" data-anchor-id="weak-vs-strong-data-non_centered_s2z-vs-scaled_s2z">4.2 Weak vs Strong Data: <code>non_centered_s2z</code> vs <code>scaled_s2z</code></h3>
<p>Conditional on <img src="https://latex.codecogs.com/png.latex?%5Ctau">, <img src="https://latex.codecogs.com/png.latex?%5Coperatorname%7Bdiag%7D(1,%20%5Ctau%20I_%7BJ-1%7D)"> rescales <img src="https://latex.codecogs.com/png.latex?h%20=%20%5Ctau%20z">, so <code>scaled_s2z</code> and <code>non_centered_s2z</code> have the exact same conditional location correlation matrix at every <img src="https://latex.codecogs.com/png.latex?%5Ctau">. Jointly with <img src="https://latex.codecogs.com/png.latex?%5Clog%5Ctau">, however, the two parameterizations split sharply across data regimes (Figure 6):</p>
<ul>
<li><strong>Weak data (<code>Standard</code> Eight Schools, <img src="https://latex.codecogs.com/png.latex?%5Csigma_j%20%5Cin%20%5B9,%2018%5D">)</strong>: the posterior supports <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%5Cto%200">. In <code>scaled_s2z</code>, the <img src="https://latex.codecogs.com/png.latex?(h,%20h)"> block <img src="https://latex.codecogs.com/png.latex?H%5E%5Ctop%20W%20H%20+%20%5Ctau%5E%7B-2%7D%20I_%7BJ-1%7D"> still diverges like <img src="https://latex.codecogs.com/png.latex?%5Ctau%5E%7B-2%7D"> along <img src="https://latex.codecogs.com/png.latex?J%20-%201%20=%207"> contrast directions (producing 13 divergences across 8,000 draws), whereas <code>non_centered_s2z</code> bounds <img src="https://latex.codecogs.com/png.latex?%5Coverline%7B%5Cmathcal%7BG%7D%7D_%7B%5Ceta,%5Ceta%7D(%5Ctau)%20=%20h_0(%5Ctau)%20+%20%5Cfrac%7BJ-1%7D%7BJ%7D%5Ctau%5E2%20w_%5Cbullet%20+%20%5Cbar%7B%5Ckappa%7D_%7B%5Ceta%5Ceta%7D(%5Ctau)"> as <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%5Cto%200"> and finishes with 0 divergences.</li>
<li><strong>Strong data (<code>Precise</code> Eight Schools, <img src="https://latex.codecogs.com/png.latex?%5Csigma_j%20/%2010">, <img src="https://latex.codecogs.com/png.latex?%5Csigma_j%20%5Cin%20%5B0.9,%201.8%5D">)</strong>: select <code>Pair: non_centered</code> and <code>Axes: (eff₁, log τ)</code> in Figure 6. Conventional <code>scaled_conv</code> and <code>non_centered_conv</code> are trapped on the <img src="https://latex.codecogs.com/png.latex?(%5Cmu,%20r)"> shift ridge (<code>full max|corr| = 0.964</code> and <code>0.924</code>; <code>μ</code> bulk ESS per 1,000 gradients is <code>3.41</code> and <code>3.65</code>). <code>non_centered_s2z</code> removes the <img src="https://latex.codecogs.com/png.latex?(%5Ctheta,%20z)"> ridge (<code>μ ESS/1k grad = 19.5</code>), yet its pointwise cross-block <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BG%7D%5E%7B%5Cmathrm%7Bnc,s2z%7D%7D_%7B%5Ceta,%20z%7D%20=%20%5Ctau%5E2%20z%5E%5Ctop%20H%5E%5Ctop%20W%20H"> grows with <img src="https://latex.codecogs.com/png.latex?W">: precise data pin <img src="https://latex.codecogs.com/png.latex?%5Ctau(Hz)_j%20%5Capprox%20y_j%20-%20%5Cbar%7By%7D">, locking <img src="https://latex.codecogs.com/png.latex?(%5Clog%5Ctau,%20z_k)"> onto a multiplicative hyperbola (<code>full max|corr| = 0.928</code>, <code>τ ESS/1k grad = 3.62</code>). Physical <code>scaled_s2z</code> avoids both ridges (<code>full max|corr| = 0.253</code>, <code>μ ESS/1k grad = 123.5</code>—a 36× speedup over <code>scaled_conv</code>).</li>
</ul>
<figure class="quarto-float quarto-float-fig figure" id="fig-regime-crossover">
  <div class="s2z-fig" id="viz-regime-crossover">
    <div class="s2z-fig-head">
      <div class="s2z-fig-title">Eight Schools Regime Curves and CmdStan Posterior Draws</div>
      <div class="s2z-fig-subtitle">Top: analytic Fisher metrics across τ (dashed = conv, solid = s2z). Bottom: post-warmup CmdStan draws (red points = divergent transitions)</div>
    </div>

    <div class="s2z-toolbar">
      <div class="s2z-btn-group">
        <span class="s2z-btn-group-label">Regime:</span>
        <button type="button" class="s2z-btn" data-rc-regime="balanced">Balanced</button>
        <button type="button" class="s2z-btn" data-rc-regime="standard">Standard</button>
        <button type="button" class="s2z-btn active" data-rc-regime="precise">Precise (σ / 10)</button>
      </div>
      <div class="s2z-btn-group">
        <span class="s2z-btn-group-label">Curve:</span>
        <button type="button" class="s2z-btn active" data-rc-metric="maxCorr">Max |Corr|</button>
        <button type="button" class="s2z-btn" data-rc-metric="mi">Mutual Info</button>
        <button type="button" class="s2z-btn" data-rc-metric="cond">Cond κ(P_loc)</button>
        <button type="button" class="s2z-btn" data-rc-metric="etaInfo">Ḡ_{log τ}</button>
      </div>
      <div class="s2z-btn-group">
        <span class="s2z-btn-group-label">Pair:</span>
        <button type="button" class="s2z-btn" data-rc-fam="centered">centered</button>
        <button type="button" class="s2z-btn active" data-rc-fam="scaled">scaled</button>
        <button type="button" class="s2z-btn" data-rc-fam="non_centered">non_centered</button>
      </div>
      <div class="s2z-btn-group">
        <span class="s2z-btn-group-label">Axes:</span>
        <button type="button" class="s2z-btn active" data-rc-axes="tau_loc">(μ / θ, log τ)</button>
        <button type="button" class="s2z-btn" data-rc-axes="loc_eff1">(μ / θ, eff₁)</button>
        <button type="button" class="s2z-btn" data-rc-axes="tau_eff1">(eff₁, log τ)</button>
      </div>
    </div>

    <div class="s2z-facet" style="margin-bottom:12px;">
      <div class="s2z-strip"><span>Analytic Fisher Metric vs Between-School SD τ</span></div>
      <canvas class="s2z-canvas" id="rc-canvas-curves"></canvas>
    </div>

    <div class="s2z-facets-2">
      <div class="s2z-facet">
        <div class="s2z-strip"><span id="rc-title-conv">scaled_conv</span><span class="s2z-strip-sub" id="rc-badge-conv"></span></div>
        <canvas class="s2z-canvas" id="rc-canvas-scat-conv"></canvas>
      </div>
      <div class="s2z-facet">
        <div class="s2z-strip"><span id="rc-title-s2z">scaled_s2z</span><span class="s2z-strip-sub" id="rc-badge-s2z"></span></div>
        <canvas class="s2z-canvas" id="rc-canvas-scat-s2z"></canvas>
      </div>
    </div>

    <div class="s2z-footer-bar" id="rc-inspector"></div>
  </div>
  <figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig">
    Figure&nbsp;6: Analytic conditional Fisher curves across \(\tau\) paired with thinned CmdStan draws across Balanced (\(\sigma_j = 12.5\)), Standard, and Precise (\(\sigma_j / 10\)) Eight Schools. Under Precise data, <code>scaled_s2z</code> avoids both the location shift ridge of <code>scaled_conv</code> and the \((\log\tau, z_1)\) hyperbolic ridge of <code>non_centered_s2z</code>.
  </figcaption>
</figure>
</section>
<section id="mixed-weak-and-strong-groups-partially-centered-sum-to-zero" class="level3">
<h3 class="anchored" data-anchor-id="mixed-weak-and-strong-groups-partially-centered-sum-to-zero">4.3 Mixed Weak and Strong Groups: Partially Centered Sum-to-Zero</h3>
<p>Section 4.2 treats weak data and strong data as separate global regimes, yet hierarchical datasets routinely contain sparse and dense groups at the same time. Consider the <img src="https://latex.codecogs.com/png.latex?J%20=%209"> mixed-sample-size design from <span class="citation" data-cites="betancourt2017hierarchical">Betancourt (2017b)</span> and my <a href="https://github.com/spinkney/hierarchical_model_tutorial/blob/main/slides/tutorial_presentation.qmd">StanCon 2024 tutorial</a> <span class="citation" data-cites="pinkney2024tutorial">(Pinkney 2024)</span>:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0An%20=%20(10,%5C;%205,%5C;%201000,%5C;%2010,%5C;%201,%5C;%205,%5C;%20100,%5C;%2010,%5C;%205),%20%5Cqquad%20%5Csigma%20=%2010.%0A"></p>
<p>Neither global S2Z endpoint fits all nine groups. Group 5 has a single observation (<img src="https://latex.codecogs.com/png.latex?n_5%20=%201">, SE <img src="https://latex.codecogs.com/png.latex?=%2010">), so <code>scaled_s2z</code> hits a <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%5Cto%200"> funnel along that sparse contrast; Group 3 has <img src="https://latex.codecogs.com/png.latex?n_3%20=%201000"> observations (SE <img src="https://latex.codecogs.com/png.latex?=%200.316">), so <code>non_centered_s2z</code> pins <img src="https://latex.codecogs.com/png.latex?%5Ctau%20z_3%20%5Capprox%20%5Cbar%7By%7D_3%20-%20%5Ctheta"> and stalls on a <img src="https://latex.codecogs.com/png.latex?(%5Clog%5Ctau,%20z_3)"> hyperbola.</p>
<p>In the <a href="https://github.com/spinkney/hierarchical_model_tutorial/blob/main/slides/tutorial_presentation.qmd">StanCon 2024 tutorial</a>, I introduced a per-group <strong>partially centered parameterization</strong> controlled by a continuous weight <img src="https://latex.codecogs.com/png.latex?w_i%20%5Cin%20%5B0,%201%5D">, where <img src="https://latex.codecogs.com/png.latex?w_i%20=%200"> is centered, <img src="https://latex.codecogs.com/png.latex?w_i%20=%201"> is non-centered, and <img src="https://latex.codecogs.com/png.latex?0%20%3C%20w_i%20%3C%201"> interpolates smoothly between them:<sup>2</sup></p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cchi_i%20%5Csim%20%5Cmathcal%7BN%7D%5C!%5Cbig%5B%5Cmu(1%20-%20w_i),%5C;%20%5Ctau(1%20-%20w_i)%20+%20w_i%5Cbig%5D,%0A%5Cqquad%0A%5Calpha_i%20=%20%5Cfrac%7B%5Cmu%20w_i%20+%20%5Ctau%20%5Cchi_i%7D%7B%5Ctau(1%20-%20w_i)%20+%20w_i%7D.%0A"></p>
<p>Checking the moments shows that <img src="https://latex.codecogs.com/png.latex?%5Calpha_i%20%5Csim%20%5Cmathcal%7BN%7D(%5Cmu,%20%5Ctau)"> for every <img src="https://latex.codecogs.com/png.latex?w_i%20%5Cin%20%5B0,%201%5D"> and <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%3E%200">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctau%20%5Cchi_i%20&amp;%5Csim%20%5Cmathcal%7BN%7D%5C!%5Cbig%5B%5Ctau%5Cmu(1%20-%20w_i),%5C;%20%5Ctau(%5Ctau(1%20-%20w_i)%20+%20w_i)%5Cbig%5D,%20%5C%5C%0A%5Cmu%20w_i%20+%20%5Ctau%20%5Cchi_i%20&amp;%5Csim%20%5Cmathcal%7BN%7D%5C!%5Cbig%5B%5Cmu(%5Ctau(1%20-%20w_i)%20+%20w_i),%5C;%20%5Ctau(%5Ctau(1%20-%20w_i)%20+%20w_i)%5Cbig%5D,%20%5C%5C%0A%5Cfrac%7B%5Cmu%20w_i%20+%20%5Ctau%20%5Cchi_i%7D%7B%5Ctau(1%20-%20w_i)%20+%20w_i%7D%20&amp;%5Csim%20%5Cmathcal%7BN%7D(%5Cmu,%20%5Ctau).%0A%5Cend%7Baligned%7D%0A"></p>
<p>Combining this denominator <img src="https://latex.codecogs.com/png.latex?d_i(%5Ctau)%20=%20%5Ctau(1%20-%20w_i)%20+%20w_i"> with <code>sum_to_zero_constrain()</code> requires one extra step. Sweeping the realized group mean into <img src="https://latex.codecogs.com/png.latex?%5Ctheta%20=%20%5Cmu%20+%20%5Cbar%7Ba%7D"> removes the <img src="https://latex.codecogs.com/png.latex?%5Cmu(1%20-%20w_i)"> location shift, leaving <img src="https://latex.codecogs.com/png.latex?u%20=%20%5Ctext%7Bsum%5C_to%5C_zero%5C_constrain%7D(z_%7B%5Cmathrm%7Braw%7D%7D)%20%5Cin%20%5Cmathbb%7BR%7D%5EJ"> for <img src="https://latex.codecogs.com/png.latex?z_%7B%5Cmathrm%7Braw%7D%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7BJ-1%7D">. When the weights <img src="https://latex.codecogs.com/png.latex?w_1,%20%5Cdots,%20w_J"> differ across groups, dividing <img src="https://latex.codecogs.com/png.latex?u"> componentwise by <img src="https://latex.codecogs.com/png.latex?d(%5Ctau)"> breaks the zero-sum condition because <img src="https://latex.codecogs.com/png.latex?%5Csum_%7Bi=1%7D%5EJ%20u_i%20/%20d_i(%5Ctau)%20%5Cne%200">. Subtracting the arithmetic mean of <img src="https://latex.codecogs.com/png.latex?u%20%5Coslash%20d(%5Ctau)"> restores exact zero sum while keeping <img src="https://latex.codecogs.com/png.latex?%5Ctau"> out of the denominator when warmup visits <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%5Cto%200">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Az_%7B%5Cmathrm%7Beff%7D,%20i%7D%20=%20%5Cfrac%7Bu_i%7D%7Bd_i(%5Ctau)%7D%20-%20%5Cfrac%7B1%7D%7BJ%7D%5Csum_%7Br=1%7D%5EJ%20%5Cfrac%7Bu_r%7D%7Bd_r(%5Ctau)%7D,%0A%5Cqquad%0Ac_i%20=%20%5Ctau%20z_%7B%5Cmathrm%7Beff%7D,%20i%7D,%0A%5Cqquad%0Ad_i(%5Ctau)%20=%20%5Ctau(1%20-%20w_i)%20+%20w_i.%0A"></p>
<p>When <img src="https://latex.codecogs.com/png.latex?w%20%5Cequiv%200">, <img src="https://latex.codecogs.com/png.latex?d_i(%5Ctau)%20=%20%5Ctau">, <img src="https://latex.codecogs.com/png.latex?%5Csum_r%20u_r%20/%20%5Ctau%20=%200">, <img src="https://latex.codecogs.com/png.latex?z_%7B%5Cmathrm%7Beff%7D%7D%20=%20u%20/%20%5Ctau">, and <img src="https://latex.codecogs.com/png.latex?c%20=%20u">, recovering <code>scaled_s2z</code>. When <img src="https://latex.codecogs.com/png.latex?w%20%5Cequiv%201">, <img src="https://latex.codecogs.com/png.latex?d_i(%5Ctau)%20=%201">, <img src="https://latex.codecogs.com/png.latex?%5Csum_r%20u_r%20=%200">, <img src="https://latex.codecogs.com/png.latex?z_%7B%5Cmathrm%7Beff%7D%7D%20=%20u">, and <img src="https://latex.codecogs.com/png.latex?c%20=%20%5Ctau%20u">, recovering <code>non_centered_s2z</code>.</p>
<p>To obtain the log-Jacobian on the <img src="https://latex.codecogs.com/png.latex?(J%20-%201)">-dimensional contrast space, write <img src="https://latex.codecogs.com/png.latex?h%20=%20H%5E%5Ctop%20c%20=%20(H%5E%5Ctop%20A%20H)%20z_%7B%5Cmathrm%7Braw%7D%7D"> where <img src="https://latex.codecogs.com/png.latex?A%20=%20%5Coperatorname%7Bdiag%7D(%5Ctau%20/%20d_1(%5Ctau),%20%5Cdots,%20%5Ctau%20/%20d_J(%5Ctau))">. Because <img src="https://latex.codecogs.com/png.latex?Q%20=%20%5BH,%5C;%20J%5E%7B-1/2%7D%5Cmathbf%7B1%7D_J%5D"> is orthogonal, Jacobi’s complementary-minor determinant identity <span class="citation" data-cites="jacobi1834">(Jacobi 1834)</span> expresses <img src="https://latex.codecogs.com/png.latex?%5Cdet(H%5E%5Ctop%20A%20H)"> in terms of <img src="https://latex.codecogs.com/png.latex?%5Cdet(A)"> and the complementary <img src="https://latex.codecogs.com/png.latex?1%20%5Ctimes%201"> block <img src="https://latex.codecogs.com/png.latex?(J%5E%7B-1/2%7D%5Cmathbf%7B1%7D_J)%5E%5Ctop%20A%5E%7B-1%7D(J%5E%7B-1/2%7D%5Cmathbf%7B1%7D_J)%20=%20%5Cfrac%7B1%7D%7BJ%7D%5Csum_%7Bi=1%7D%5EJ%20%5Cfrac%7Bd_i(%5Ctau)%7D%7B%5Ctau%7D%20=%20%5Cfrac%7B%5Cbar%7Bd%7D(%5Ctau)%7D%7B%5Ctau%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cdet(H%5E%5Ctop%20A%20H)%0A=%20%5Cdet(A)%5Ccdot%20%5Cfrac%7B%5Cbar%7Bd%7D(%5Ctau)%7D%7B%5Ctau%7D%0A=%20%5Cfrac%7B%5Ctau%5E%7BJ-1%7D%5C,%5Cbar%7Bd%7D(%5Ctau)%7D%7B%5Cprod_%7Bi=1%7D%5EJ%20d_i(%5Ctau)%7D,%0A%5Cqquad%0A%5Cbar%7Bd%7D(%5Ctau)%20=%20%5Cfrac%7B1%7D%7BJ%7D%5Csum_%7Bi=1%7D%5EJ%20d_i(%5Ctau).%0A"></p>
<p>Multiplying the <img src="https://latex.codecogs.com/png.latex?(J-1)">-dimensional Gaussian prior normalizer <img src="https://latex.codecogs.com/png.latex?%5Ctau%5E%7B-(J-1)%7D"> of <img src="https://latex.codecogs.com/png.latex?h%20%5Csim%20%5Cmathcal%7BN%7D(0,%20%5Ctau%5E2%20I_%7BJ-1%7D)"> by <img src="https://latex.codecogs.com/png.latex?%5Cdet(H%5E%5Ctop%20A%20H)"> cancels <img src="https://latex.codecogs.com/png.latex?%5Ctau%5E%7BJ-1%7D"> against <img src="https://latex.codecogs.com/png.latex?%5Ctau%5E%7B-(J-1)%7D">, and isometry of <img src="https://latex.codecogs.com/png.latex?H"> gives <img src="https://latex.codecogs.com/png.latex?%5C%7Ch/%5Ctau%5C%7C%5E2%20=%20%5C%7Cc/%5Ctau%5C%7C%5E2%20=%20%5C%7Cz_%7B%5Cmathrm%7Beff%7D%7D%5C%7C%5E2">. At <img src="https://latex.codecogs.com/png.latex?w%20%5Cequiv%200"> (<img src="https://latex.codecogs.com/png.latex?d_i%20=%20%5Cbar%7Bd%7D%20=%20%5Ctau">) the remaining volume term <img src="https://latex.codecogs.com/png.latex?-%5Csum_%7Bi=1%7D%5EJ%20%5Clog%20d_i(%5Ctau)%20+%20%5Clog%5Cbar%7Bd%7D(%5Ctau)"> reduces to <img src="https://latex.codecogs.com/png.latex?-(J%20-%201)%5Clog%5Ctau">; at <img src="https://latex.codecogs.com/png.latex?w%20%5Cequiv%201"> (<img src="https://latex.codecogs.com/png.latex?d_i%20=%20%5Cbar%7Bd%7D%20=%201">) it vanishes.</p>
<p>Frequentist Fisher information also supplies the group weights <img src="https://latex.codecogs.com/png.latex?w_i"> in closed form. Group <img src="https://latex.codecogs.com/png.latex?i">’s likelihood Fisher information is <img src="https://latex.codecogs.com/png.latex?j_i%20=%20n_i%20/%20%5Csigma%5E2"> (or <img src="https://latex.codecogs.com/png.latex?%5Csigma_i%5E%7B-2%7D"> in Eight Schools), so conditional on <img src="https://latex.codecogs.com/png.latex?%5Ctau">, the ratio of local posterior variance <img src="https://latex.codecogs.com/png.latex?(j_i%20+%20%5Ctau%5E%7B-2%7D)%5E%7B-1%7D"> to prior variance <img src="https://latex.codecogs.com/png.latex?%5Ctau%5E2"> is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Aw_i%20=%20%5Cfrac%7B1%7D%7B1%20+%20%5Chat%7B%5Ctau%7D%5E2%20j_i%7D%20%5Cin%20(0,%201),%0A"></p>
<p>which approaches <img src="https://latex.codecogs.com/png.latex?1"> (<code>non_centered</code>) when <img src="https://latex.codecogs.com/png.latex?j_i%20%5Cll%20%5Chat%7B%5Ctau%7D%5E%7B-2%7D"> and <img src="https://latex.codecogs.com/png.latex?0"> (<code>scaled</code>) when <img src="https://latex.codecogs.com/png.latex?j_i%20%5Cgg%20%5Chat%7B%5Ctau%7D%5E%7B-2%7D">. Evaluating <img src="https://latex.codecogs.com/png.latex?w_i"> at a fixed pilot <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D%5E2"> computed once from the data—here the closed-form DerSimonian–Laird moment statistic <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D%5E2_%7B%5Cmathrm%7BDL%7D%7D%20=%20%5Cmax%5C!%5Cbig(%5Ctau_%7B%5Cmin%7D%5E2,%20%5Cfrac%7BQ%20-%20(J%20-%201)%7D%7B%5Csum_i%20j_i%20-%20%5Csum_i%20j_i%5E2%20/%20%5Csum_i%20j_i%7D%5Cbig)"> with Cochran’s <img src="https://latex.codecogs.com/png.latex?Q%20=%20%5Csum_%7Bi=1%7D%5EJ%20j_i(%5Cbar%7By%7D_i%20-%20%5Cbar%7By%7D_w)%5E2">—keeps the coordinate map fixed during HMC, so no <img src="https://latex.codecogs.com/png.latex?%5Cpartial%20w_i%20/%20%5Cpartial%5Ctau"> correction arises and the target posterior remains exact:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb8-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb8-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] u <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;">sum_to_zero_constrain</span>(z_raw);</span>
<span id="cb8-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tau <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> w) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> w;</span>
<span id="cb8-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] u_div_d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">./</span> d;</span>
<span id="cb8-5">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] z_eff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> u_div_d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(u_div_d);</span>
<span id="cb8-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z_eff;</span>
<span id="cb8-7">}</span>
<span id="cb8-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb8-9">  theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">hypot</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span>, tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(J)));</span>
<span id="cb8-10">  tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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>
<span id="cb8-11">  z_eff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb8-12">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(d)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(d));</span>
<span id="cb8-13">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c[indiv_idx], sigma);</span>
<span id="cb8-14">}</span></code></pre></div></div>
<p>Running <code>scaled_s2z</code> (<img src="https://latex.codecogs.com/png.latex?w%20%5Cequiv%200">), <code>non_centered_s2z</code> (<img src="https://latex.codecogs.com/png.latex?w%20%5Cequiv%201">), and <code>partial_s2z</code> (<img src="https://latex.codecogs.com/png.latex?w%20=%20w_%7B%5Cmathrm%7BDL%7D%7D">) in CmdStan 2.40.0 on the <img src="https://latex.codecogs.com/png.latex?J%20=%209">, <img src="https://latex.codecogs.com/png.latex?n%20=%20(10,%205,%201000,%2010,%201,%205,%20100,%2010,%205)">, <img src="https://latex.codecogs.com/png.latex?%5Csigma%20=%2010"> design (4 chains <img src="https://latex.codecogs.com/png.latex?%5Ctimes"> 2,000 post-warmup draws, <code>adapt_delta = 0.80</code>) confirms the behavior across both small and moderate between-group variation:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 14%">
<col style="width: 14%">
<col style="width: 14%">
<col style="width: 14%">
<col style="width: 14%">
<col style="width: 14%">
<col style="width: 14%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">True <img src="https://latex.codecogs.com/png.latex?%5Ctau"> (pilot <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BDL%7D%7D">)</th>
<th style="text-align: left;">Model</th>
<th style="text-align: right;">Divergences</th>
<th style="text-align: right;">Leapfrogs / draw</th>
<th style="text-align: right;"><code>τ</code> ESS / 1k grads</th>
<th style="text-align: right;"><code>θ</code> ESS / 1k grads</th>
<th style="text-align: right;">Min ESS / 1k grads</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%200.50"> (<img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BDL%7D%7D%20=%200.50">)</td>
<td style="text-align: left;"><code>scaled_s2z</code> (<img src="https://latex.codecogs.com/png.latex?w%20%5Cequiv%200">)</td>
<td style="text-align: right;">57</td>
<td style="text-align: right;">30.0</td>
<td style="text-align: right;">0.43</td>
<td style="text-align: right;">10.31</td>
<td style="text-align: right;">0.43</td>
</tr>
<tr class="even">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%200.50"> (<img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BDL%7D%7D%20=%200.50">)</td>
<td style="text-align: left;"><code>non_centered_s2z</code> (<img src="https://latex.codecogs.com/png.latex?w%20%5Cequiv%201">)</td>
<td style="text-align: right;">29</td>
<td style="text-align: right;">26.5</td>
<td style="text-align: right;">11.29</td>
<td style="text-align: right;">11.24</td>
<td style="text-align: right;">11.24</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%200.50"> (<img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BDL%7D%7D%20=%200.50">)</td>
<td style="text-align: left;"><code>partial_s2z</code> (<img src="https://latex.codecogs.com/png.latex?w%20=%20w_%7B%5Cmathrm%7BDL%7D%7D">)</td>
<td style="text-align: right;"><strong>0</strong></td>
<td style="text-align: right;"><strong>15.6</strong></td>
<td style="text-align: right;"><strong>35.89</strong></td>
<td style="text-align: right;"><strong>29.36</strong></td>
<td style="text-align: right;"><strong>29.36</strong></td>
</tr>
<tr class="even">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%203.00"> (<img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BDL%7D%7D%20=%203.21">)</td>
<td style="text-align: left;"><code>scaled_s2z</code> (<img src="https://latex.codecogs.com/png.latex?w%20%5Cequiv%200">)</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">25.9</td>
<td style="text-align: right;">10.16</td>
<td style="text-align: right;">18.39</td>
<td style="text-align: right;">10.16</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%203.00"> (<img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BDL%7D%7D%20=%203.21">)</td>
<td style="text-align: left;"><code>non_centered_s2z</code> (<img src="https://latex.codecogs.com/png.latex?w%20%5Cequiv%201">)</td>
<td style="text-align: right;">36</td>
<td style="text-align: right;">28.7</td>
<td style="text-align: right;">8.82</td>
<td style="text-align: right;">21.70</td>
<td style="text-align: right;">8.82</td>
</tr>
<tr class="even">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%203.00"> (<img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BDL%7D%7D%20=%203.21">)</td>
<td style="text-align: left;"><code>partial_s2z</code> (<img src="https://latex.codecogs.com/png.latex?w%20=%20w_%7B%5Cmathrm%7BDL%7D%7D">)</td>
<td style="text-align: right;"><strong>0</strong></td>
<td style="text-align: right;"><strong>19.0</strong></td>
<td style="text-align: right;"><strong>28.07</strong></td>
<td style="text-align: right;"><strong>24.34</strong></td>
<td style="text-align: right;"><strong>24.34</strong></td>
</tr>
</tbody>
</table>
<p>At <img src="https://latex.codecogs.com/png.latex?%5Ctau_%7B%5Cmathrm%7Btrue%7D%7D%20=%200.50">, <img src="https://latex.codecogs.com/png.latex?w_%7B%5Cmathrm%7BDL%7D%7D"> assigns <img src="https://latex.codecogs.com/png.latex?w_3%20=%200.286"> to the <img src="https://latex.codecogs.com/png.latex?n_3%20=%201000"> group, <img src="https://latex.codecogs.com/png.latex?w_7%20=%200.800"> to the <img src="https://latex.codecogs.com/png.latex?n_7%20=%20100"> group, and <img src="https://latex.codecogs.com/png.latex?w_5%20=%200.998"> to the <img src="https://latex.codecogs.com/png.latex?n_5%20=%201"> group—eliminating the 57 funnel divergences of <code>scaled_s2z</code> and the 29 hyperbola divergences of <code>non_centered_s2z</code> while raising <code>τ</code> ESS per 1,000 gradients to <code>35.89</code> (versus <code>0.43</code> and <code>11.29</code>). At <img src="https://latex.codecogs.com/png.latex?%5Ctau_%7B%5Cmathrm%7Btrue%7D%7D%20=%203.00">, <img src="https://latex.codecogs.com/png.latex?w_%7B%5Cmathrm%7BDL%7D%7D"> shifts toward physical coordinates on dense groups (<img src="https://latex.codecogs.com/png.latex?w_3%20=%200.010">, <img src="https://latex.codecogs.com/png.latex?w_7%20=%200.089">) while keeping Group 5 nearly non-centered (<img src="https://latex.codecogs.com/png.latex?w_5%20=%200.907">), finishing with 0 divergences and 2.8× the <code>τ</code> ESS per gradient of <code>scaled_s2z</code>.</p>
</section>
</section>
<section id="machine-checked-proofs-in-lean-4-and-regime-summary" class="level2">
<h2 class="anchored" data-anchor-id="machine-checked-proofs-in-lean-4-and-regime-summary">5. Machine-Checked Proofs in Lean 4 and Regime Summary</h2>
<p>In <code>lean/S2ZFisher/</code>, 55 theorems formalize the algebraic, Fisher-information, Louis missing-information, and partial-centering identities in Lean <code>4.34.0</code> against Mathlib <code>v4.34.0</code>, checked with <code>check_proofs.py</code> to ensure zero compiler warnings, zero <code>sorry</code>s, and only Lean’s standard foundational axioms (<code>propext</code>, <code>Classical.choice</code>, <code>Quot.sound</code>):</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Lean 4 Module</th>
<th style="text-align: right;">Count</th>
<th style="text-align: left;">Verified Results</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>S2ZFisher.HelmertBasis</code></td>
<td style="text-align: right;">13</td>
<td style="text-align: left;">Centering projector <img src="https://latex.codecogs.com/png.latex?P_G%5E2%20=%20P_G">, <img src="https://latex.codecogs.com/png.latex?%5Coperatorname%7Btr%7D(P_G)%20=%20G-1">; isometric Helmert map <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B1%7D%5E%5Ctop%20H%20=%200">, <img src="https://latex.codecogs.com/png.latex?H%5E%5Ctop%20H%20=%20I">, <img src="https://latex.codecogs.com/png.latex?H%20H%5E%5Ctop%20=%20P_G">, <img src="https://latex.codecogs.com/png.latex?%5BH,%20G%5E%7B-1/2%7D%5Cmathbf%7B1%7D%5D%20%5Cin%20O(G)">; Kronecker <img src="https://latex.codecogs.com/png.latex?(H%20%5Cotimes%20I_K)"> mean/contrast split</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>S2ZFisher.EightSchools</code></td>
<td style="text-align: right;">11</td>
<td style="text-align: left;">Schur-complement identity <code>centered_conv</code> <img src="https://latex.codecogs.com/png.latex?%5Cto"> <code>centered_s2z</code>; <img src="https://latex.codecogs.com/png.latex?P_%7B%5Cmathrm%7Bcent,s2z%7D,ij%7D%20%3C%200"> under equal weights; shift nullvectors of <code>conv</code> vs positive definiteness and balanced diagonalization of <code>scaled_s2z</code> &amp; <code>non_centered_s2z</code>; <img src="https://latex.codecogs.com/png.latex?%5Ctau">-curvature reduction</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>S2ZFisher.BrmsGeneral</code></td>
<td style="text-align: right;">9</td>
<td style="text-align: left;">Centered design matching <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7BX%7D%20H_x%20=%20Z%20U">; multi-factor predictor invariance; <img src="https://latex.codecogs.com/png.latex?(%5Csum_f%20M_f)">-dim shift nullspace vs range preservation; balanced &amp; additively separable Cox–Reid orthogonality; explicit <img src="https://latex.codecogs.com/png.latex?z_%7B%5Cmathrm%7Bmean%7D%7D"> likelihood decoupling; Sherman–Morrison rank-one precision inverse</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>S2ZFisher.LoewnerReduction</code></td>
<td style="text-align: right;">8</td>
<td style="text-align: left;">Exact <img src="https://latex.codecogs.com/png.latex?D">-Pythagorean identity; unbalanced Loewner dominance <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BI%7D_%7B%5Cmathrm%7Beff%7D%7D%5E%7B%5Cmathrm%7Bs2z%7D%7D(%5Ctheta)%20%5Csucceq%20%5Cmathcal%7BI%7D_%7B%5Cmathrm%7Beff%7D%7D%5E%7B%5Cmathrm%7Bconv%7D%7D(%5Cbeta)">; complementary block-mean gap <img src="https://latex.codecogs.com/png.latex?D%5E%7B-1%7DU(U%5E%5Ctop%20D%5E%7B-1%7DU)%5E%7B-1%7DU%5E%5Ctop%20D%5E%7B-1%7D">; flat-prior canonical correlation bound; <img src="https://latex.codecogs.com/png.latex?G%20=%202"> dogmatic-prior counterexample</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>S2ZFisher.VaryingSdsLouis</code></td>
<td style="text-align: right;">14</td>
<td style="text-align: left;">Heterogeneous square-completion over <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Ba%7D">; Slepian–Bangs trace and Louis score-covariance proofs of <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BI%7D_%7B%5Ctheta,h%7D(%5Cell)%20=%202I_J%20-%204%5Coperatorname%7Bdiag%7D(%5Cboldsymbol%7B%5Cpi%7D)%20+%202%5Cboldsymbol%7B%5Cpi%7D%5Cboldsymbol%7B%5Cpi%7D%5E%5Ctop">; Eight Schools <img src="https://latex.codecogs.com/png.latex?%5Ctau">-curvature corollary &amp; unbalanced-count log-SD diagonalization; zero-sum, endpoints, explicit inverse, and injectivity of <code>partial_s2z</code></td>
</tr>
</tbody>
</table>
<p>Putting the analytic regimes and empirical benchmarks together gives a compact guide for choosing coordinates:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Regime / Model Structure</th>
<th style="text-align: left;">Parameterization</th>
<th style="text-align: left;">Fisher Geometry</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Weak group data (<img src="https://latex.codecogs.com/png.latex?%5Csigma_j%20%5Cgg%20%5Ctau"> or <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%5Cto%200">)</td>
<td style="text-align: left;"><code>non_centered_s2z</code> (<code>tau * sum_to_zero_constrain(z)</code>)</td>
<td style="text-align: left;">Bounded <img src="https://latex.codecogs.com/png.latex?%5Clog%5Ctau"> Fisher curvature <img src="https://latex.codecogs.com/png.latex?(1%20-%20J%5E%7B-1%7D)%5Ctau%5E2%20w_%5Cbullet"> avoids the <img src="https://latex.codecogs.com/png.latex?%5Ctau%5E%7B-2%7D"> funnel while reducing <img src="https://latex.codecogs.com/png.latex?(%5Ctheta,%20z)"> cross-information</td>
</tr>
<tr class="even">
<td style="text-align: left;">Moderate-to-strong group data (<img src="https://latex.codecogs.com/png.latex?%5Csigma_j%20%5Clesssim%20%5Ctau">)</td>
<td style="text-align: left;"><code>scaled_s2z</code> (<code>sum_to_zero_constrain(h)</code> in predictor, whitened in prior)</td>
<td style="text-align: left;">Removes the <img src="https://latex.codecogs.com/png.latex?(%5Cbeta,%20u)"> shift ridge without introducing <code>non_centered_s2z</code>’s strong-data <img src="https://latex.codecogs.com/png.latex?(%5Clog%5Ctau,%20z)"> hyperbola; diagonalizes when balanced</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Mixed sparse and dense groups (<img src="https://latex.codecogs.com/png.latex?n_j"> spanning orders of magnitude)</td>
<td style="text-align: left;"><code>partial_s2z</code> (<img src="https://latex.codecogs.com/png.latex?d_j%20=%20%5Ctau(1%20-%20w_j)%20+%20w_j">, <img src="https://latex.codecogs.com/png.latex?w_j%20=%20(1%20+%20%5Chat%7B%5Ctau%7D%5E2%20j_j)%5E%7B-1%7D">)</td>
<td style="text-align: left;">Interpolates per group between <code>non_centered_s2z</code> on sparse cells and <code>scaled_s2z</code> on dense cells with restricted Jacobian <img src="https://latex.codecogs.com/png.latex?-%5Csum_j%20%5Clog%20d_j%20+%20%5Clog%20%5Cbar%7Bd%7D"></td>
</tr>
<tr class="even">
<td style="text-align: left;">Very strong group data, 1-way intercepts only</td>
<td style="text-align: left;"><code>centered_s2z</code> or <code>scaled_s2z</code></td>
<td style="text-align: left;"><code>centered_s2z</code> decouples <img src="https://latex.codecogs.com/png.latex?q_j"> at large <img src="https://latex.codecogs.com/png.latex?W"> while remaining equicorrelated when <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%5Cll%20s_%5Cmu">; <code>scaled_s2z</code> extends to slopes and crossed factors</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Multi-factor crossed / varying-slope GLMs</td>
<td style="text-align: left;">Factor-wise <code>sum_to_zero_constrain()</code> + Matheron recovery</td>
<td style="text-align: left;">Removes all <img src="https://latex.codecogs.com/png.latex?%5Csum_%7Bf=1%7D%5EF%20M_f"> shift nullvectors and increases effective population Fisher information <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BI%7D_%7B%5Cmathrm%7Beff%7D%7D(%5Ctheta)"> in Loewner order</td>
</tr>
<tr class="even">
<td style="text-align: left;">Group-varying prior SDs (<img src="https://latex.codecogs.com/png.latex?%5Clog%20s_j%20=%20%5Cgamma%20+%20%5Clambda_%5Comega%20z_j">)</td>
<td style="text-align: left;">Dual-layer <code>sum_to_zero_constrain()</code> on location and log-SDs</td>
<td style="text-align: left;">Removes the location ridge and diagonalizes the log-SD block at homogeneity even under unbalanced group sample sizes <img src="https://latex.codecogs.com/png.latex?n_j"></td>
</tr>
<tr class="odd">
<td style="text-align: left;">Dogmatic prior on <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> (<img src="https://latex.codecogs.com/png.latex?V_0%20%5Cll%20%5CSigma_f%20/%20G_f">) with sparse, unbalanced groups</td>
<td style="text-align: left;">Conventional non-centered</td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?V_0"> pins <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20%5Capprox%20%5Cbeta_0"> directly; sweeping <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bu%7D"> into <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> inflates prior variance to <img src="https://latex.codecogs.com/png.latex?V_0%20+%20%5CSigma_f%20/%20G_f"> and increases normalized <img src="https://latex.codecogs.com/png.latex?R%5E2"></td>
</tr>
</tbody>
</table>
<p>Extending <code>partial_s2z</code> from scalar intercepts to correlated varying slopes replaces <img src="https://latex.codecogs.com/png.latex?d_j(%5Ctau)"> by a <img src="https://latex.codecogs.com/png.latex?K%20%5Ctimes%20K"> group preconditioner shaped by each group’s local design Fisher matrix <img src="https://latex.codecogs.com/png.latex?X_j%5E%5Ctop%20%5Cmathcal%7BW%7D_j%20X_j"> and the Cholesky factor <img src="https://latex.codecogs.com/png.latex?L_%5CSigma">, with a <img src="https://latex.codecogs.com/png.latex?(J-1)K">-dimensional block-minor Jacobian analogous to <img src="https://latex.codecogs.com/png.latex?-%5Csum_j%20%5Clog%20d_j%20+%20%5Clog%5Cbar%7Bd%7D">.</p>
</section>
<section id="conclusion-frequentist-coordinates-for-the-bayesian-typical-set" class="level2">
<h2 class="anchored" data-anchor-id="conclusion-frequentist-coordinates-for-the-bayesian-typical-set">6. Conclusion: Frequentist Coordinates for the Bayesian Typical Set</h2>
<p>Every sampling bottleneck we removed in Sections 2–4 was diagnosed and fixed with classical frequentist machinery: centered predictors, isometric Helmert contrasts (<code>sum_to_zero_constrain()</code>), Cox–Reid parameter orthogonality, Schur-complement Loewner dominance, Louis’s missing-information identity, and DerSimonian–Laird Fisher-reliability weights <img src="https://latex.codecogs.com/png.latex?w_i%20=%20(1%20+%20%5Chat%7B%5Ctau%7D%5E2_%7B%5Cmathrm%7BDL%7D%7D%20j_i)%5E%7B-1%7D">. Those tools describe local second-order curvature, which governs leapfrog step sizes and trajectory lengths in NUTS. Borrowing them to reparameterize a hierarchical model stops HMC from spending thousands of gradient evaluations sliding along non-identifiable <img src="https://latex.codecogs.com/png.latex?(%5Cbeta,%20u)"> ridges or fighting mismatched group sample sizes.</p>
<p>If frequentist Fisher geometry already tells us how to orthogonalize these models, why run HMC at all instead of stopping at restricted maximum likelihood (<code>lme4::lmer</code>), Laplace approximation (<code>lme4::glmer</code>), or plug-in empirical Bayes? Optimization-based estimators summarize a model around a mode plus local curvature, whereas posterior expectations and predictive intervals depend on probability mass—density multiplied by volume—which concentrates in the typical set <span class="citation" data-cites="betancourt2017conceptual">(Betancourt 2017a)</span>. Running <code>R 4.5.2</code> (<code>lme4 2.0.6</code>) alongside <code>CmdStan 2.40.0</code> on the four model families from this post shows directly where those frequentist estimates miss the typical set (Figure 7):</p>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Model &amp; Dataset</th>
<th style="text-align: left;">Frequentist Call (<code>R 4.5.2</code> / <code>lme4 2.0.6</code>)</th>
<th style="text-align: left;">Frequentist Estimate &amp; Diagnostic</th>
<th style="text-align: left;">Bayesian S2Z (<code>CmdStan 2.40.0</code>, <code>0</code> div)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Eight Schools (<img src="https://latex.codecogs.com/png.latex?J%20=%208">, Section 4)</td>
<td style="text-align: left;"><code>optimize()</code> on exact Gaussian ML &amp; REML log-likelihoods</td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BMLE%7D%7D%20=%200.000">, <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BREML%7D%7D%20=%200.000"> (<img src="https://latex.codecogs.com/png.latex?Q%20=%204.71">, <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Bdf%7D%20=%207">); School 1 EB <img src="https://latex.codecogs.com/png.latex?=%207.69"> <img src="https://latex.codecogs.com/png.latex?%5B0.99,%2014.38%5D"></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5B%5Ctau%20%5Cmid%20y%5D%20=%203.60">, <img src="https://latex.codecogs.com/png.latex?%5Clog%5Ctau"> mode <img src="https://latex.codecogs.com/png.latex?=%203.78">, <img src="https://latex.codecogs.com/png.latex?90%5C%25"> <img src="https://latex.codecogs.com/png.latex?%5B0.24,%209.84%5D">; School 1 <img src="https://latex.codecogs.com/png.latex?=%206.12"> <img src="https://latex.codecogs.com/png.latex?%5B-1.55,%2015.82%5D"></td>
</tr>
<tr class="even">
<td style="text-align: left;">Mixed Counts (<img src="https://latex.codecogs.com/png.latex?J%20=%209">, <img src="https://latex.codecogs.com/png.latex?n_5%20=%201">, <img src="https://latex.codecogs.com/png.latex?%5Ctau_%7B%5Cmathrm%7Btrue%7D%7D%20=%200.50">, Section 4.3)</td>
<td style="text-align: left;"><code>lmer(y ~ 1 + (1 | g))</code> (<code>ML</code> &amp; <code>REML</code>) + <code>ranef(condVar = TRUE)</code></td>
<td style="text-align: left;"><code>ML</code>: <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D%20=%200.000"> (<code>boundary (singular) fit</code>); <code>REML</code>: <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D%20=%200.374"> (<img src="https://latex.codecogs.com/png.latex?90%5C%25"> prof <img src="https://latex.codecogs.com/png.latex?%5B0,%201.19%5D">), <img src="https://latex.codecogs.com/png.latex?%5Calpha_5"> EB <img src="https://latex.codecogs.com/png.latex?=%202.62"> <img src="https://latex.codecogs.com/png.latex?%5B1.69,%203.54%5D"></td>
<td style="text-align: left;"><code>partial_s2z</code>: <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5B%5Ctau%20%5Cmid%20y%5D%20=%201.26"> <img src="https://latex.codecogs.com/png.latex?%5B0.10,%203.42%5D">; <img src="https://latex.codecogs.com/png.latex?%5Calpha_5%20=%202.45"> <img src="https://latex.codecogs.com/png.latex?%5B-0.64,%205.14%5D"> (<img src="https://latex.codecogs.com/png.latex?3.1%5Ctimes"> wider)</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Varying Slopes (<img src="https://latex.codecogs.com/png.latex?J%20=%2010">, <img src="https://latex.codecogs.com/png.latex?%5Crho_%7B%5Cmathrm%7Btrue%7D%7D%20=%20-0.50">, <img src="https://latex.codecogs.com/png.latex?%5Csigma_y%20=%201.5">, Section 3.1)</td>
<td style="text-align: left;"><code>lmer(y ~ 1 + x_c + (1 + x_c | g), REML = TRUE)</code></td>
<td style="text-align: left;"><code>isSingular == TRUE</code> in <strong>46 / 100</strong> replications; Seed 1002 hits <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Crho%7D_%7B%5Cmathrm%7BREML%7D%7D%20=%20-1.000">, <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7Bslp%7D%7D%20=%200.432"></td>
<td style="text-align: left;"><code>non_centered_s2z</code>: interior typical set <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5B%5Crho%20%5Cmid%20y%5D%20=%20-0.33">, <img src="https://latex.codecogs.com/png.latex?90%5C%25"> <img src="https://latex.codecogs.com/png.latex?%5Crho%20%5Cin%20%5B-0.87,%20+0.43%5D">, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5B%5Ctau_%7B%5Cmathrm%7Bslp%7D%7D%20%5Cmid%20y%5D%20=%200.44"></td>
</tr>
<tr class="even">
<td style="text-align: left;">Binary GLM (<img src="https://latex.codecogs.com/png.latex?J%20=%20100">, <img src="https://latex.codecogs.com/png.latex?n_j%20=%203">, <img src="https://latex.codecogs.com/png.latex?%5Ctau_%7B%5Cmathrm%7Btrue%7D%7D%20=%201.00">, Section 3.3)</td>
<td style="text-align: left;"><code>glmer(y ~ 1 + x + (1 | g), binomial, nAGQ = 1)</code> vs <code>nAGQ = 25</code></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BLaplace%7D%7D%20%3C%20%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BAGHQ25%7D%7D"> in <strong>28 / 30</strong> runs (median <img src="https://latex.codecogs.com/png.latex?0.916"> vs <img src="https://latex.codecogs.com/png.latex?1.039">); Seed 4001 <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BLaplace%7D%7D%20=%200.822"></td>
<td style="text-align: left;">S2Z Bernoulli: Seed 4001 <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5B%5Ctau%20%5Cmid%20y%5D%20=%200.929">, <img src="https://latex.codecogs.com/png.latex?90%5C%25"> <img src="https://latex.codecogs.com/png.latex?%5B0.30,%201.51%5D">; handles slopes &amp; crossed factors</td>
</tr>
</tbody>
</table>
<figure class="quarto-float quarto-float-fig figure" id="fig-typical-set-evidence">
  <div class="s2z-fig" id="viz-typical-set">
    <div class="s2z-fig-head">
      <div class="s2z-fig-title">Where Frequentist Modes and Laplace Curves Miss the Bayesian Typical Set</div>
      <div class="s2z-fig-subtitle">Empirical outputs from lme4 2.0.6 (lmer ML/REML, glmer nAGQ = 1 Laplace) and plug-in Empirical Bayes vs CmdStan 2.40.0 S2Z draws</div>
    </div>

    <div class="s2z-toolbar">
      <div class="s2z-btn-group">
        <span class="s2z-btn-group-label">Evidence Case:</span>
        <button type="button" class="s2z-btn active" data-ts-case="eight_schools">Eight Schools (REML = 0)</button>
        <button type="button" class="s2z-btn" data-ts-case="mixed_j9">Mixed Counts J = 9 (lmer EB)</button>
        <button type="button" class="s2z-btn" data-ts-case="varying_slopes">Varying Slopes J = 10 (lmer ρ̂ = −1)</button>
        <button type="button" class="s2z-btn" data-ts-case="binary_glm">Binary GLM (glmer Laplace)</button>
      </div>
    </div>

    <div class="s2z-facets-2">
      <div class="s2z-facet">
        <div class="s2z-strip"><span id="ts-strip-left">1D Scale: Density p(τ | y) vs Volume-Weighted p(log τ | y)</span></div>
        <canvas class="s2z-canvas" id="ts-canvas-left"></canvas>
      </div>
      <div class="s2z-facet">
        <div class="s2z-strip"><span id="ts-strip-right">School 1 (q₁, τ): CmdStan S2Z vs REML Plug-In EB</span></div>
        <canvas class="s2z-canvas" id="ts-canvas-right"></canvas>
      </div>
    </div>

    <div class="s2z-footer-bar" id="ts-detail"></div>
  </div>
  <figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig">
    Figure&nbsp;7: Four empirical comparisons between frequentist mixed-model point/Laplace estimators (red) and Bayesian S2Z HMC posterior draws from CmdStan 2.40.0 (teal). Switch tabs to inspect boundary variance collapse in Eight Schools, plug-in Empirical Bayes under-coverage on a single-observation group (\(n_5 = 1\)), singular \(\hat{\rho} = \pm 1\) walls in <code>lme4::lmer</code>, and first-order Laplace downward bias in <code>lme4::glmer</code>.
  </figcaption>
</figure>
<p>In the first two tabs of Figure 7, the likelihood and 1D marginal density peak near or on the boundary <img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%200">. On Eight Schools, <code>optimize()</code> returns <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BMLE%7D%7D%20=%20%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BREML%7D%7D%20=%200.000"> (<img src="https://latex.codecogs.com/png.latex?Q%20=%204.71"> on <img src="https://latex.codecogs.com/png.latex?7"> degrees of freedom), and even the Bayesian marginal density <img src="https://latex.codecogs.com/png.latex?p(%5Ctau%20%5Cmid%20y)"> has its mode at <img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%200">, while the centered joint density <img src="https://latex.codecogs.com/png.latex?p(%5Cmu,%20q,%20%5Ctau%20%5Cmid%20y)%20%5Cpropto%20%5Ctau%5E%7B-J%7D"> diverges there. Plugging <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D%20=%200"> into empirical Bayes pools all eight schools onto <img src="https://latex.codecogs.com/png.latex?7.69%20%5Cpm%204.07"> (<img src="https://latex.codecogs.com/png.latex?90%5C%25"> interval <img src="https://latex.codecogs.com/png.latex?%5B0.99,%2014.38%5D">) along the <img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%200"> floor. Yet only <img src="https://latex.codecogs.com/png.latex?2.1%5C%25"> of posterior probability mass lies below <img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%200.1">: multiplying <img src="https://latex.codecogs.com/png.latex?p(%5Ctau%20%5Cmid%20y)"> by the unconstrained volume factor <img src="https://latex.codecogs.com/png.latex?d%5Ctau%20=%20%5Ctau%5C,d%5Clog%5Ctau"> shifts the <img src="https://latex.codecogs.com/png.latex?%5Clog%5Ctau"> mode to <img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%203.78"> (<img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5B%5Ctau%20%5Cmid%20y%5D%20=%203.60">, <img src="https://latex.codecogs.com/png.latex?90%5C%25"> interval <img src="https://latex.codecogs.com/png.latex?%5B0.24,%209.84%5D">), fanning School 1’s <code>non_centered_s2z</code> draws across <img src="https://latex.codecogs.com/png.latex?%5B-1.55,%2015.82%5D">. On the exact <img src="https://latex.codecogs.com/png.latex?J%20=%209">, <img src="https://latex.codecogs.com/png.latex?%5Ctau_%7B%5Cmathrm%7Btrue%7D%7D%20=%200.50"> dataset from Section 4.3 (<code>Mixed Counts J = 9</code>), <code>lme4::lmer(..., REML = FALSE)</code> emits <code>boundary (singular) fit</code> at <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BML%7D%7D%20=%200.000">, and <code>REML = TRUE</code> plugs <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7BREML%7D%7D%20=%200.374"> into <code>ranef(fit, condVar = TRUE)</code> to report a narrow <img src="https://latex.codecogs.com/png.latex?90%5C%25"> interval <img src="https://latex.codecogs.com/png.latex?%5B1.69,%203.54%5D"> for single-observation Group 5 (<img src="https://latex.codecogs.com/png.latex?n_5%20=%201">, <img src="https://latex.codecogs.com/png.latex?y_5%20=%20-6.69">). Integrating over <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%5Cin%20%5B0.10,%203.42%5D"> in <code>partial_s2z</code> produces <img src="https://latex.codecogs.com/png.latex?%5B-0.64,%205.14%5D">—<img src="https://latex.codecogs.com/png.latex?3.1%5Ctimes"> wider—because draws with <img src="https://latex.codecogs.com/png.latex?%5Ctau%20%3E%201.5"> allow <img src="https://latex.codecogs.com/png.latex?y_5%20=%20-6.69"> to pull <img src="https://latex.codecogs.com/png.latex?%5Calpha_5"> downward.</p>
<p>The third and fourth tabs show covariance walls and non-Gaussian skewness. Simulating 100 datasets on the unbalanced <img src="https://latex.codecogs.com/png.latex?J%20=%2010"> varying-slope design of Section 3.1 (<img src="https://latex.codecogs.com/png.latex?%5Ctau_%7B%5Cmathrm%7Bint%7D%7D%20=%200.60,%20%5Ctau_%7B%5Cmathrm%7Bslp%7D%7D%20=%200.40,%20%5Crho_%7B%5Cmathrm%7Btrue%7D%7D%20=%20-0.50,%20%5Csigma_y%20=%201.5">), <code>lme4::lmer(y ~ 1 + x_c + (1 + x_c | g), REML = TRUE)</code> lands on a rank-deficient boundary (<code>isSingular(fit) == TRUE</code>) in 46 of 100 runs <span class="citation" data-cites="bates2015">(Bates et al. 2015)</span>; on Seed 1002 it collapses onto <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Crho%7D_%7B%5Cmathrm%7BREML%7D%7D%20=%20-1.000"> (<img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D_%7B%5Cmathrm%7Bslp%7D%7D%20=%200.432">), whereas <code>CmdStan</code> <code>non_centered_s2z</code> centers at <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5B%5Crho%20%5Cmid%20y%5D%20=%20-0.33"> (median <img src="https://latex.codecogs.com/png.latex?-0.39">, <img src="https://latex.codecogs.com/png.latex?90%5C%25"> <img src="https://latex.codecogs.com/png.latex?%5B-0.87,%20+0.43%5D">, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5B%5Ctau_%7B%5Cmathrm%7Bslp%7D%7D%20%5Cmid%20y%5D%20=%200.44">, regularized slightly toward zero by <code>lkj(2)</code> on <img src="https://latex.codecogs.com/png.latex?J%20-%201%20=%209"> degrees of freedom) and stays in the interior of the positive-definite cone because rank-deficient walls have zero volume. In the small-cluster binary GLM (<code>Binary GLM</code>, <img src="https://latex.codecogs.com/png.latex?J%20=%20100,%20n_j%20=%203,%20%5Ctau_%7B%5Cmathrm%7Btrue%7D%7D%20=%201.00">), <code>lme4::glmer(..., nAGQ = 1)</code> replaces each cluster’s skewed logistic-normal integrand with a symmetric Gaussian bell curve at <img src="https://latex.codecogs.com/png.latex?%5Chat%7Ba%7D_j">, underestimating exact 25-point Gauss–Hermite quadrature (<code>nAGQ = 25</code>) in 28 of 30 replications (median <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Ctau%7D%20=%200.916"> vs <img src="https://latex.codecogs.com/png.latex?1.039">) <span class="citation" data-cites="breslow1995">(Breslow and Lin 1995)</span>. As soon as a binary GLM adds varying slopes or a second crossed factor, <code>glmer</code> rejects <code>nAGQ &gt; 1</code> (<code>Error: nAGQ &gt; 1 is only available for models with a single, scalar random-effects term</code>), leaving first-order Laplace as its only option while S2Z HMC integrates over all latent dimensions jointly and recovers superpopulation parameters via Matheron’s rule.</p>
<p>That division of labor is why the two traditions work well together. Frequentist theory supplies the coordinate chart—orthogonal contrasts, Schur complements, Louis’s missing-information matrix, and Fisher-reliability weights—so the log-posterior has well-conditioned local curvature without altering the model. Bayesian computation then uses that chart to traverse the typical set and integrate over scale, correlation, and superpopulation uncertainty where boundary modes and Laplace approximations fall short.</p>
</section>
<section id="appendix-complete-stan-programs-and-lean-4-theorems" class="level2">
<h2 class="anchored" data-anchor-id="appendix-complete-stan-programs-and-lean-4-theorems">Appendix: Complete Stan Programs and Lean 4 Theorems</h2>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">Eight Schools <code>scaled_s2z</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Eight Schools <code>non_centered_s2z</code></a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">Correlated Varying Slopes S2Z</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">Partially Centered S2Z</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-5-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-5" aria-controls="tabset-1-5" aria-selected="false" href="">Lean 4: Loewner Dominance &amp; Exact Gap</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-6-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-6" aria-controls="tabset-1-6" aria-selected="false" href="">Lean 4: Louis’s Identity &amp; Partial Centering</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>scaled_s2z.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" data-filename="scaled_s2z.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb9-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb9-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">&gt;</span> J;</span>
<span id="cb9-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] y_j;</span>
<span id="cb9-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>[J] sigma_j;</span>
<span id="cb9-5">}</span>
<span id="cb9-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb9-7">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> tau;</span>
<span id="cb9-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta;</span>
<span id="cb9-9">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J <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>] h_j;</span>
<span id="cb9-10">}</span>
<span id="cb9-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb9-12">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] u_j <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;">sum_to_zero_constrain</span>(h_j);</span>
<span id="cb9-13">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] q_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> u_j;</span>
<span id="cb9-14">}</span>
<span id="cb9-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb9-16">  tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cauchy</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>
<span id="cb9-17">  theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(tau) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> J));</span>
<span id="cb9-18">  (h_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> tau) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb9-19">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(J <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(tau);</span>
<span id="cb9-20">  y_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(q_j, sigma_j);</span>
<span id="cb9-21">}</span>
<span id="cb9-22"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb9-23">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> mu;</span>
<span id="cb9-24">  {</span>
<span id="cb9-25">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(tau) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> J;</span>
<span id="cb9-26">    mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> normal_rng(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> theta <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;">25</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</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> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v)));</span>
<span id="cb9-27">  }</span>
<span id="cb9-28">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>non_centered_s2z.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" data-filename="non_centered_s2z.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb10-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb10-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">&gt;</span> J;</span>
<span id="cb10-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] y_j;</span>
<span id="cb10-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>[J] sigma_j;</span>
<span id="cb10-5">}</span>
<span id="cb10-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb10-7">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> tau;</span>
<span id="cb10-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta;</span>
<span id="cb10-9">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J <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>] z_j;</span>
<span id="cb10-10">}</span>
<span id="cb10-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb10-12">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] q_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> tau <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;">sum_to_zero_constrain</span>(z_j);</span>
<span id="cb10-13">}</span>
<span id="cb10-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb10-15">  tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cauchy</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>
<span id="cb10-16">  theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(tau) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> J));</span>
<span id="cb10-17">  z_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb10-18">  y_j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(q_j, sigma_j);</span>
<span id="cb10-19">}</span>
<span id="cb10-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb10-21">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> mu;</span>
<span id="cb10-22">  {</span>
<span id="cb10-23">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> v <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(tau) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> J;</span>
<span id="cb10-24">    mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> normal_rng(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> theta <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;">25</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</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> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v)));</span>
<span id="cb10-25">  }</span>
<span id="cb10-26">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>correlated_slopes_s2z.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" data-filename="correlated_slopes_s2z.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb11-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb11-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">&gt;</span> N;</span>
<span id="cb11-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> J;</span>
<span id="cb11-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>J<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> group;</span>
<span id="cb11-5">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] x;</span>
<span id="cb11-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] y;</span>
<span id="cb11-7">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] beta_0;</span>
<span id="cb11-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">cov_matrix</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] V_0;</span>
<span id="cb11-9">}</span>
<span id="cb11-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed data</span> {</span>
<span id="cb11-11">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> x_bar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x);</span>
<span id="cb11-12">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] x_c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x_bar;</span>
<span id="cb11-13">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</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>] H_x <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>, x_bar], [<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>
<span id="cb11-14">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</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>] L_V0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cholesky_decompose</span>(V_0);</span>
<span id="cb11-15">}</span>
<span id="cb11-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb11-17">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] theta;</span>
<span id="cb11-18">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, J <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>] h_raw;</span>
<span id="cb11-19">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" 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>] tau;</span>
<span id="cb11-20">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">cholesky_factor_corr</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] L_Omega;</span>
<span id="cb11-21">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> sigma_y;</span>
<span id="cb11-22">}</span>
<span id="cb11-23"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb11-24">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</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>] L_Sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag_pre_multiply</span>(tau, L_Omega);</span>
<span id="cb11-25">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] c_int <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;">sum_to_zero_constrain</span>(h_raw[<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>);</span>
<span id="cb11-26">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] c_slp <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;">sum_to_zero_constrain</span>(h_raw[<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>
<span id="cb11-27">}</span>
<span id="cb11-28"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb11-29">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</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>] Sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Sigma);</span>
<span id="cb11-30">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, J <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>] z_u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mdivide_left_tri_low</span>(L_Sigma, h_raw);</span>
<span id="cb11-31"></span>
<span id="cb11-32">  tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">2</span>);</span>
<span id="cb11-33">  L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lkj_corr_cholesky</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>);</span>
<span id="cb11-34">  sigma_y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">2</span>);</span>
<span id="cb11-35">  theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">multi_normal</span>(beta_0, V_0 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quad_form</span>(Sigma, H_x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> J);</span>
<span id="cb11-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">to_vector</span>(z_u) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb11-37">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(J <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diagonal</span>(L_Sigma)));</span>
<span id="cb11-38"></span>
<span id="cb11-39">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(</span>
<span id="cb11-40">    (theta[<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> c_int[group]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (theta[<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> c_slp[group]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.*</span> x_c,</span>
<span id="cb11-41">    sigma_y</span>
<span id="cb11-42">  );</span>
<span id="cb11-43">}</span>
<span id="cb11-44"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb11-45">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] beta_c;</span>
<span id="cb11-46">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] mean_u;</span>
<span id="cb11-47">  {</span>
<span id="cb11-48">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</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>] L_m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> H_x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> L_Sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> J);</span>
<span id="cb11-49">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</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>] A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mdivide_left_tri_low</span>(L_V0, L_m);</span>
<span id="cb11-50">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</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>] P_z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_diag</span>(A<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> A, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>);</span>
<span id="cb11-51">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</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>] L_Pz <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cholesky_decompose</span>(P_z);</span>
<span id="cb11-52">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] rhs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> A<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mdivide_left_tri_low</span>(L_V0, theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> beta_0);</span>
<span id="cb11-53">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] z_rng <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [std_normal_rng(), std_normal_rng()]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb11-54">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] z_draw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mdivide_left_spd</span>(P_z, rhs)</span>
<span id="cb11-55">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mdivide_right_tri_low</span>(z_rng<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>, L_Pz)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb11-56">    mean_u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (L_Sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> J)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z_draw;</span>
<span id="cb11-57">    beta_c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> H_x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> mean_u;</span>
<span id="cb11-58">  }</span>
<span id="cb11-59">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>partial_centered_s2z.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" data-filename="partial_centered_s2z.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb12-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb12-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">&gt;</span> N;</span>
<span id="cb12-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> J;</span>
<span id="cb12-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>J<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> indiv_idx;</span>
<span id="cb12-5">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] y;</span>
<span id="cb12-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> sigma;</span>
<span id="cb12-7">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</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;">&gt;</span>[J] w;</span>
<span id="cb12-8">}</span>
<span id="cb12-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb12-10">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta;</span>
<span id="cb12-11">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> tau;</span>
<span id="cb12-12">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J <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>] z_raw;</span>
<span id="cb12-13">}</span>
<span id="cb12-14"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb12-15">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] u <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;">sum_to_zero_constrain</span>(z_raw);</span>
<span id="cb12-16">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tau <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> w) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> w;</span>
<span id="cb12-17">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] u_div_d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">./</span> d;</span>
<span id="cb12-18">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] z_eff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> u_div_d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(u_div_d);</span>
<span id="cb12-19">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z_eff;</span>
<span id="cb12-20">}</span>
<span id="cb12-21"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb12-22">  theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">hypot</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span>, tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(J)));</span>
<span id="cb12-23">  tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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>
<span id="cb12-24">  z_eff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb12-25">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(d)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(d));</span>
<span id="cb12-26">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c[indiv_idx], sigma);</span>
<span id="cb12-27">}</span>
<span id="cb12-28"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb12-29">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> mu;</span>
<span id="cb12-30">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] alpha_indiv;</span>
<span id="cb12-31">  {</span>
<span id="cb12-32">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> v_bar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(tau) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> J;</span>
<span id="cb12-33">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> v_tot <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;">25.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> v_bar;</span>
<span id="cb12-34">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> normal_rng((v_bar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> v_tot) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> theta, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> v_bar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> v_tot));</span>
<span id="cb12-35">    mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> m;</span>
<span id="cb12-36">    alpha_indiv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c;</span>
<span id="cb12-37">  }</span>
<span id="cb12-38">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-5" class="tab-pane" aria-labelledby="tabset-1-5-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>lean/S2ZFisher/LoewnerReduction.lean</strong></pre>
</div>
<pre class="lean" data-filename="lean/S2ZFisher/LoewnerReduction.lean"><code>/-- Exact complementary block-mean gap identity: when `E` and `U` are orthogonal complements
(`Eᵀ * U = 0`, both injective, `∀ z, ∃ a b, z = E *ᵥ a + U *ᵥ b`),
`D⁻¹ - E * (Eᵀ * D * E)⁻¹ * Eᵀ = D⁻¹ * U * (Uᵀ * D⁻¹ * U)⁻¹ * Uᵀ * D⁻¹`. -/
theorem subspace_compression_exact_gap {Amb F M : Type*}
    [Fintype Amb] [DecidableEq Amb] [Fintype F] [DecidableEq F] [Fintype M] [DecidableEq M]
    (D : Matrix Amb Amb ℝ) (hD : D.PosDef)
    (E : Matrix Amb F ℝ) (U : Matrix Amb M ℝ)
    (hE : Function.Injective E.mulVec) (hU : Function.Injective U.mulVec)
    (horth : Eᵀ * U = 0)
    (hspan : ∀ z : Amb → ℝ, ∃ (a : F → ℝ) (b : M → ℝ), z = E.mulVec a + U.mulVec b) :
    D⁻¹ - E * (Eᵀ * D * E)⁻¹ * Eᵀ =
      D⁻¹ * U * (Uᵀ * D⁻¹ * U)⁻¹ * Uᵀ * D⁻¹

/-- Universal Loewner dominance of effective population Fisher information:
`x ⬝ᵥ I_eff^conv(β) x ≤ x ⬝ᵥ I_eff^s2z(θ) x` for all `x : Qcol → ℝ`. -/
theorem brms_s2z_effective_info_ge_conv
    {Qcol Bidx : Type*} [Fintype Qcol] [DecidableEq Bidx] [Fintype Bidx]
    (n : Bidx → ℕ) (M : Bidx → ℕ)
    (Alik : Matrix Qcol Qcol ℝ)
    (Bconv : Matrix Qcol (Σ b : Bidx, Fin (n b + 1) × Fin (M b)) ℝ)
    (Dconv : Matrix (Σ b : Bidx, Fin (n b + 1) × Fin (M b))
      (Σ b : Bidx, Fin (n b + 1) × Fin (M b)) ℝ)
    (hD : Dconv.PosDef) (x : Qcol → ℝ) :
    let E := brmsMultiContrast n M
    let Bs2z := Bconv * E
    let Ds2z := Eᵀ * Dconv * E
    let Ieff_conv := Alik - Bconv * Dconv⁻¹ * Bconvᵀ
    let Ieff_s2z := Alik - Bs2z * Ds2z⁻¹ * Bs2zᵀ
    x ⬝ᵥ Ieff_conv.mulVec x ≤ x ⬝ᵥ Ieff_s2z.mulVec x</code></pre>
</div>
</div>
<div id="tabset-1-6" class="tab-pane" aria-labelledby="tabset-1-6-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>lean/S2ZFisher/VaryingSdsLouis.lean</strong></pre>
</div>
<pre class="lean" data-filename="lean/S2ZFisher/VaryingSdsLouis.lean"><code>/-- Slepian–Bangs Gaussian Fisher information identity on `ℓ = log s` after marginalizing `m = ā`:
`(1/2) tr(P_{prior}^{s2z} (∂_{ℓ_j} Σ) P_{prior}^{s2z} (∂_{ℓ_k} Σ)) = 2 δ_{jk} - 4 δ_{jk} π_j + 2 π_j π_k`. -/
theorem slepian_bangs_s2z_log_sd_fisher_eq (n : ℕ) (q0 : ℝ) (q : Fin (n + 1) → ℝ)
    (hq : ∀ r : Fin (n + 1), q r ≠ 0)
    (hP : q0 + (∑ r : Fin (n + 1), q r) ≠ 0) (j k : Fin (n + 1)) :
    (1 / 2 : ℝ) * Matrix.trace
      ((varyingSdS2ZPrec n q0 q) * (varyingSdS2ZDCov n q j) *
        ((varyingSdS2ZPrec n q0 q) * (varyingSdS2ZDCov n q k))) =
      louisLogSdFisher n q0 q j k

/-- Louis's missing-information identity on `ℓ = log s`: complete Fisher information `2 δ_{jk}`
minus expected conditional score covariance `q_j q_k [4 E[r_j r_k] P⁻¹ + 2 P⁻²]` equals
`louisLogSdFisher n q0 q j k = 2 δ_{jk} - 4 δ_{jk} π_j + 2 π_j π_k`. -/
theorem louis_score_cov_log_sd_fisher_eq (n : ℕ) (q0 : ℝ) (q : Fin (n + 1) → ℝ)
    (hq : ∀ r : Fin (n + 1), q r ≠ 0)
    (hP : q0 + (∑ r : Fin (n + 1), q r) ≠ 0) (j k : Fin (n + 1)) :
    let P := q0 + ∑ r : Fin (n + 1), q r
    let cov_r := (if j = k then (q j)⁻¹ else 0) - P⁻¹
    let miss_jk := q j * q k * (4 * cov_r * P⁻¹ + 2 * (P⁻¹) ^ 2)
    2 * (if j = k then (1 : ℝ) else 0) - miss_jk =
      louisLogSdFisher n q0 q j k

/-- Explicit left inverse of `partialCenterS2Z`: for all `τ &gt; 0` and `w_i ∈ [0, 1]`,
`partialCenterS2ZInv` recovers `z_raw ∈ ℝ^n` identically. -/
theorem s2z_partial_centering_left_inv (n : ℕ) (tau : ℝ) (htau : 0 &lt; tau)
    (w : Fin (n + 1) → ℝ) (hw0 : ∀ i, 0 ≤ w i) (hw1 : ∀ i, w i ≤ 1) :
    Function.LeftInverse (partialCenterS2ZInv n tau w) (partialCenterS2Z n tau w)</code></pre>
</div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>The Fisher information analysis and ideas in this post are mine. The mathematics, proofs, and Lean 4 code were assisted by Gemini Neon.</p>
</div>
</div>
<script src="interactive_data.js"></script>
<script src="interactive_widgets.js"></script>



</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-bates2015" class="csl-entry">
Bates, Douglas, Martin Mächler, Ben Bolker, and Steve Walker. 2015. <span>“Fitting Linear Mixed-Effects Models Using <span class="nocase">lme4</span>.”</span> <em>Journal of Statistical Software</em> 67 (1): 1–48. <a href="https://doi.org/10.18637/jss.v067.i01">https://doi.org/10.18637/jss.v067.i01</a>.
</div>
<div id="ref-betancourt2017conceptual" class="csl-entry">
Betancourt, Michael. 2017a. <em>A Conceptual Introduction to <span>Hamiltonian Monte Carlo</span></em>. <a href="https://arxiv.org/abs/1701.02434">https://arxiv.org/abs/1701.02434</a>.
</div>
<div id="ref-betancourt2017hierarchical" class="csl-entry">
Betancourt, Michael. 2017b. <em>Hierarchical Modeling</em>. <a href="https://betanalpha.github.io/assets/case_studies/hierarchical_modeling.html">https://betanalpha.github.io/assets/case_studies/hierarchical_modeling.html</a>.
</div>
<div id="ref-betancourt2015" class="csl-entry">
Betancourt, Michael, and Mark Girolami. 2015. <span>“Hamiltonian <span>Monte Carlo</span> for Hierarchical Models.”</span> In <em>Current Trends in Bayesian Methodology with Applications</em>. Chapman; Hall/CRC.
</div>
<div id="ref-breslow1995" class="csl-entry">
Breslow, Norman E., and Xihong Lin. 1995. <span>“Bias Correction in Generalised Linear Mixed Models with a Single Component of Dispersion.”</span> <em>Biometrika</em> 82 (1): 81–91. <a href="https://doi.org/10.1093/biomet/82.1.81">https://doi.org/10.1093/biomet/82.1.81</a>.
</div>
<div id="ref-buerkner2017" class="csl-entry">
Bürkner, Paul-Christian. 2017. <span>“<span class="nocase">brms</span>: An <span>R</span> Package for <span>Bayesian</span> Multilevel Models Using <span>Stan</span>.”</span> <em>Journal of Statistical Software</em> 80 (1): 1–28. <a href="https://doi.org/10.18637/jss.v080.i01">https://doi.org/10.18637/jss.v080.i01</a>.
</div>
<div id="ref-cox1987" class="csl-entry">
Cox, D. R., and N. Reid. 1987. <span>“Parameter Orthogonality and Approximate Conditional Inference.”</span> <em>Journal of the Royal Statistical Society: Series B (Methodological)</em> 49 (1): 1–39.
</div>
<div id="ref-egozcue2003" class="csl-entry">
Egozcue, Juan Jose, Vera Pawlowsky-Glahn, Gloria Mateu-Figueras, and Carles Barceló-Vidal. 2003. <span>“Isometric Logratio Transformations for Compositional Data Analysis.”</span> <em>Mathematical Geology</em> 35 (3): 279–300. <a href="https://doi.org/10.1023/A:1023818214614">https://doi.org/10.1023/A:1023818214614</a>.
</div>
<div id="ref-gelman2005" class="csl-entry">
Gelman, Andrew. 2005. <span>“Analysis of Variance—Why It Is More Important Than Ever.”</span> <em>The Annals of Statistics</em> 33 (1): 1–53. <a href="https://doi.org/10.1214/009053604000001048">https://doi.org/10.1214/009053604000001048</a>.
</div>
<div id="ref-jacobi1834" class="csl-entry">
Jacobi, Carl Gustav Jacob. 1834. <span>“De Binis Quibuslibet Functionibus Homogeneis Secundi Ordinis Per Substitutiones Lineares in Alias Binas Tranformandis.”</span> <em>Journal f<span>ü</span>r Die Reine Und Angewandte Mathematik</em> 12: 1–69.
</div>
<div id="ref-louis1982" class="csl-entry">
Louis, Thomas A. 1982. <span>“Finding the Observed Information Matrix When Using the <span>EM</span> Algorithm.”</span> <em>Journal of the Royal Statistical Society: Series B (Methodological)</em> 44 (2): 226–33.
</div>
<div id="ref-loewner1934" class="csl-entry">
Löwner, Karl. 1934. <span>“<span>Ü</span>ber Monotone <span>M</span>atrixfunktionen.”</span> <em>Mathematische Zeitschrift</em> 38 (1): 177–216. <a href="https://doi.org/10.1007/BF01170633">https://doi.org/10.1007/BF01170633</a>.
</div>
<div id="ref-millar2009" class="csl-entry">
Millar, Colin P., Robert J. Fryer, and David Hirst. 2009. <em>Hierarchical Modelling of Scottish Fisheries Observer Data: The Underlying Model</em>. Collaborative Report No. 01/09. Fisheries Research Services.
</div>
<div id="ref-ogle2020" class="csl-entry">
Ogle, Kiona, and Jarrett J. Barber. 2020. <span>“Ensuring Identifiability in Hierarchical Mixed Effects <span>Bayesian</span> Models.”</span> <em>Ecological Applications</em> 30 (7): e02159. <a href="https://doi.org/10.1002/eap.2159">https://doi.org/10.1002/eap.2159</a>.
</div>
<div id="ref-papaspiliopoulos:2003" class="csl-entry">
Papaspiliopoulos, Omiros, and Gareth O. Roberts. 2003. <span>“Non-Centered Parameterisations for Hierarchical Models and Data Augmentation.”</span> <em>Bayesian Statistics</em> 7: 307–26.
</div>
<div id="ref-papaspiliopoulos2007" class="csl-entry">
Papaspiliopoulos, Omiros, Gareth O. Roberts, and Martin Sköld. 2007. <span>“A General Framework for the Parametrization of Hierarchical Models.”</span> <em>Statistical Science</em> 22 (1): 59–73. <a href="https://doi.org/10.1214/088342307000000014">https://doi.org/10.1214/088342307000000014</a>.
</div>
<div id="ref-pinkney2024tutorial" class="csl-entry">
Pinkney, Sean. 2024. <em>Hierarchical Modeling in <span>Stan</span>: <span>StanCon</span> 2024 Tutorial</em>. <a href="https://github.com/spinkney/hierarchical_model_tutorial">https://github.com/spinkney/hierarchical_model_tutorial</a>.
</div>
<div id="ref-rouder2012" class="csl-entry">
Rouder, Jeffrey N., Richard D. Morey, Paul L. Speckman, and Jordan M. Province. 2012. <span>“Default <span>Bayes</span> Factors for <span>ANOVA</span> Designs.”</span> <em>Journal of Mathematical Psychology</em> 56 (5): 356–74. <a href="https://doi.org/10.1016/j.jmp.2012.08.001">https://doi.org/10.1016/j.jmp.2012.08.001</a>.
</div>
<div id="ref-rubin1981" class="csl-entry">
Rubin, Donald B. 1981. <span>“Estimation in Parallel Randomized Experiments.”</span> <em>Journal of Educational Statistics</em> 6 (4): 377–401.
</div>
<div id="ref-vines1996" class="csl-entry">
Vines, S. K., W. R. Gilks, and P. Wild. 1996. <span>“Fitting <span>Bayesian</span> Multiple Random Effects Models.”</span> <em>Statistics and Computing</em> 6 (4): 337–46. <a href="https://doi.org/10.1007/BF00143554">https://doi.org/10.1007/BF00143554</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>General implementations of all the extensions from the talk—correlated varying slopes with centered covariates, multiple crossed factor sets, generalized linear models with non-Gaussian priors, and heavy-tailed or varying-scale group effects—are currently being worked on for <code>brms</code> <span class="citation" data-cites="buerkner2017">(Bürkner 2017)</span>.↩︎</p></li>
<li id="fn2"><p><span class="citation" data-cites="papaspiliopoulos:2003">Papaspiliopoulos and Roberts (2003)</span> introduced partially non-centered coordinates by weighting <img src="https://latex.codecogs.com/png.latex?%5Cmu"> alone, without rescaling the denominator by <img src="https://latex.codecogs.com/png.latex?%5Ctau(1%20-%20w_i)%20+%20w_i"> or deriving the implied latent scale needed in Stan; <span class="citation" data-cites="papaspiliopoulos2007">Papaspiliopoulos et al. (2007)</span> analyzed general linear partial non-centering on unconstrained group effects without a zero-sum subspace constraint.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>stan</category>
  <category>hierarchical-models</category>
  <category>parameterizations</category>
  <category>lean4</category>
  <guid>https://spinkney.github.io/posts/post-13-s2z-fisher-geometry/s2z-fisher-geometry.html</guid>
  <pubDate>Sun, 29 Nov 2026 00:00:00 GMT</pubDate>
  <media:content url="https://spinkney.github.io/posts/post-13-s2z-fisher-geometry/thumbnail.png" medium="image" type="image/png" height="75" width="144"/>
</item>
<item>
  <title>Bounded Correlation Matrices: Sign Constraints, Finite Boxes, and Exact Values</title>
  <dc:creator>Sean Pinkney</dc:creator>
  <link>https://spinkney.github.io/posts/post-12-corr-bounds/corr-bounds.html</link>
  <description><![CDATA[ 





<section id="from-structural-zeros-to-bounds" class="level2">
<h2 class="anchored" data-anchor-id="from-structural-zeros-to-bounds">1. From Structural Zeros to Bounds</h2>
<p>My <a href="../../posts/post-11-corr-zeros-bakeoff/corr-zeros-bakeoff.html">previous post</a> used a triangular basis to sample correlation matrices with exact structural zeros. Given the preceding Cholesky rows, each zero is a homogeneous linear constraint on the current row. Setting all off-diagonal entries in that row to zero always satisfies those constraints and preserves positive definiteness. Here I ask how much of that construction carries over to prescribed signs, finite cell-specific intervals, and exact nonzero values.</p>
<p>I ended up with three different cases.</p>
<ol type="1">
<li><p><strong>Prescribed signs admit a smooth triangular transform.</strong> Zeros define subspaces; signs define cones. Replacing the free coordinates in each Cholesky row by coordinates inside the requested sign cone gives a bijection onto the interior of the sign-constrained correlation space. Section 3 gives the row-wise inverse.</p></li>
<li><p><strong>Finite boxes do not fit the same homogeneous construction.</strong> A box such as <img src="https://latex.codecogs.com/png.latex?0.2%20%3C%20%5COmega_%7B21%7D%20%3C%200.5"> is not scale invariant, so the linear zero basis does not represent it. For this case, I use rays from a strictly feasible anchor. Section 4 shows that the resulting map covers the interior of the intersection between the box and the set of positive-definite correlation matrices, apart from the anchor itself. Its active boundary can change with direction and create gradient kinks.</p></li>
<li><p><strong>An exact nonzero value is an affine equality.</strong> A translated version of the zero-row construction exists, but earlier correlations determine how much Cholesky-row norm remains. They can leave no feasible completion even when the earlier rows are valid on their own. For general fixed-value patterns, I instead remove the fixed coordinates from the global anchor-ray map.</p></li>
</ol>
<p>Throughout, the LKJ shape <img src="https://latex.codecogs.com/png.latex?%5Ceta%3E0"> is fixed. The target is an LKJ density <span class="citation" data-cites="lewandowski2009">(Lewandowski et al. 2009)</span> restricted to the requested ordinary-correlation region,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cpi(%5COmega)%20%5Cpropto%20%5Cdet(%5COmega)%5E%7B%5Ceta-1%7D%0A%20%20%5Cmathbf%201%5C%7B%5Cell_%7Bij%7D%3C%5COmega_%7Bij%7D%3Cu_%7Bij%7D%5C%7D,"></p>
<p>with respect to Lebesgue measure on the free lower-triangular correlations. For positive-width free bounds, endpoint faces have measure zero, so open and closed interval notation induce the same continuous distribution.</p>
<p>When a correlation is fixed exactly, the target distribution lives on the lower-dimensional set where that equality holds, so I define its density with respect to the remaining free ordinary-correlation coordinates. Simply setting the lower and upper endpoints equal in the indicator above does not define this distribution: a zero-width interval has zero volume under the original full-dimensional measure. The fixed-value distribution can instead be obtained as a limit of <em>normalized</em> restrictions to progressively narrower intervals around the fixed value; the normalization changes with the interval width and must be retained when taking that limit.</p>
<p>The general look-ahead C-vine <a href="https://gist.github.com/spinkney/7d994fb03079c9edb8a0e83a40d2faad/d13345baceadce5062dc61ebb24cb9c08a87eba7">implementation</a> and my earlier entry-wise Cholesky construction <span class="citation" data-cites="pinkney2024">(Pinkney 2024)</span> provide sequential ways to target bounded densities, although choices made for earlier rows can leave no feasible value for a later correlation. Here I focus on transforms whose outputs satisfy all constraints globally.</p>
</section>
<section id="why-the-zero-basis-construction-does-not-extend-directly" class="level2">
<h2 class="anchored" data-anchor-id="why-the-zero-basis-construction-does-not-extend-directly">2. Why the Zero-Basis Construction Does Not Extend Directly</h2>
<p>Write <img src="https://latex.codecogs.com/png.latex?%5COmega=LL'"> and consider row <img src="https://latex.codecogs.com/png.latex?i">. Let</p>
<p><img src="https://latex.codecogs.com/png.latex?B=L_%7B1:(i-1),1:(i-1)%7D,%20%5Cqquad%0Av=L_%7Bi,1:(i-1)%7D',%20%5Cqquad%0Ac=%5COmega_%7Bi,1:(i-1)%7D'."></p>
<p>The ordinary correlations in the row satisfy</p>
<p><img src="https://latex.codecogs.com/png.latex?c=Bv,"></p>
<p>and positive definiteness requires <img src="https://latex.codecogs.com/png.latex?v'v%3C1"> so that <img src="https://latex.codecogs.com/png.latex?L_%7Bii%7D=%5Csqrt%7B1-v'v%7D%3E0">. Therefore the feasible set for <img src="https://latex.codecogs.com/png.latex?c"> given the previous rows is the Schur ellipsoid</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cmathcal%20E_B=%5C%7Bc:c'(BB')%5E%7B-1%7Dc%3C1%5C%7D."></p>
<p>An exact zero selects a linear slice through this ellipsoid. To see what this buys, let <img src="https://latex.codecogs.com/png.latex?d=i-1"> and suppose that the entries indexed by <img src="https://latex.codecogs.com/png.latex?Z=%5C%7Bj_1,%5Cldots,j_m%5C%7D%5Csubseteq%5C%7B1,%5Cldots,d%5C%7D"> are fixed to zero. Define the coordinate-selection matrix <img src="https://latex.codecogs.com/png.latex?P_Z%5Cin%5Cmathbb%20R%5E%7Bm%5Ctimes%20d%7D"> by taking its <img src="https://latex.codecogs.com/png.latex?r">th row to be the transpose of the <img src="https://latex.codecogs.com/png.latex?j_r">th standard basis vector, <img src="https://latex.codecogs.com/png.latex?e_%7Bj_r%7D'">. Thus, for any <img src="https://latex.codecogs.com/png.latex?x%5Cin%5Cmathbb%20R%5Ed">,</p>
<p><img src="https://latex.codecogs.com/png.latex?P_Zx=(x_%7Bj_1%7D,%5Cldots,x_%7Bj_m%7D)'."></p>
<p>Multiplication by <img src="https://latex.codecogs.com/png.latex?P_Z"> simply extracts the coordinates indexed by <img src="https://latex.codecogs.com/png.latex?Z">. The <img src="https://latex.codecogs.com/png.latex?m"> zero constraints can therefore be collected as</p>
<p><img src="https://latex.codecogs.com/png.latex?P_Zc=P_ZBv=0."></p>
<p>These are homogeneous linear equations in <img src="https://latex.codecogs.com/png.latex?v">. The leading Cholesky block <img src="https://latex.codecogs.com/png.latex?B"> is nonsingular, so the selected rows <img src="https://latex.codecogs.com/png.latex?P_ZB"> are linearly independent and have rank <img src="https://latex.codecogs.com/png.latex?m">. The constraints therefore remove exactly <img src="https://latex.codecogs.com/png.latex?m"> degrees of freedom and leave the <img src="https://latex.codecogs.com/png.latex?(d-m)">-dimensional subspace <img src="https://latex.codecogs.com/png.latex?%5Cker(P_ZB)">. The orthogonal-complement construction of <span class="citation" data-cites="axen2023">Axen (2023)</span> and the triangular construction from my previous post span this same nullspace, but they choose its basis differently. The former computes a complement to the constrained rows. The latter ties the basis to the free entries of the Cholesky row: for each free coordinate, it sets that coordinate to one and the other free coordinates to zero, solves the constrained coordinates by back-substitution, and then orthonormalizes the resulting basis.</p>
<p>If <img src="https://latex.codecogs.com/png.latex?Q"> denotes either orthonormal basis, write <img src="https://latex.codecogs.com/png.latex?v=Qz">, so that <img src="https://latex.codecogs.com/png.latex?v'v=z'z">. One can then sample <img src="https://latex.codecogs.com/png.latex?z"> in the open unit ball in <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%20R%5E%7Bd-m%7D"> and set</p>
<p><img src="https://latex.codecogs.com/png.latex?L_%7Bii%7D=%5Csqrt%7B1-z'z%7D."></p>
<p>The pair <img src="https://latex.codecogs.com/png.latex?(z,L_%7Bii%7D)"> lies on a <img src="https://latex.codecogs.com/png.latex?(d-m)">-dimensional unit hemisphere, and multiplication by <img src="https://latex.codecogs.com/png.latex?Q"> lifts <img src="https://latex.codecogs.com/png.latex?z"> into the original off-diagonal coordinates <img src="https://latex.codecogs.com/png.latex?v">. Every lifted row satisfies the zero constraints automatically, and every feasible row in the constrained subspace has such a representation. Thus the basis construction does more than enforce the zeros: it removes exactly the coordinates that the equalities have made redundant while retaining the usual hemisphere parameterization of a unit-norm Cholesky row.</p>
<p>Bounds instead produce</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cmathcal%20E_B%20%5Ccap%20%5C%7Bc:%5Cell%3Cc%3Cu%5C%7D,"></p>
<p>an ellipsoid intersected with slabs. Assume here that every interval has positive width. Both the Schur ellipsoid and the open box <img src="https://latex.codecogs.com/png.latex?%5C%7Bc:%5Cell%3Cc%3Cu%5C%7D"> are open subsets of <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%20R%5Ed">, so whenever their intersection is nonempty, every feasible point has a sufficiently small <img src="https://latex.codecogs.com/png.latex?d">-dimensional neighborhood that remains feasible. The bounds can truncate the allowable range along a direction, but they do not remove a local degree of freedom as an equality does.</p>
<p>They also change the boundary geometry. In the closure of the feasible set, a bound that is reached before the positive-definite boundary contributes a flat codimension-one face <img src="https://latex.codecogs.com/png.latex?c_j=%5Cell_j"> or <img src="https://latex.codecogs.com/png.latex?c_j=u_j">. Several active bounds can meet in edges or corners, while the condition <img src="https://latex.codecogs.com/png.latex?c'(BB')%5E%7B-1%7Dc=1"> contributes the curved ellipsoid boundary. Which piece is encountered first depends on the point and direction. There is therefore no fixed lower-dimensional subspace whose basis encodes the interval constraints: removing a basis column handles an equality, whereas an inequality must retain the coordinate and control how far it may move. The column-removal step used for exact zeros consequently does not impose these bounds by itself. Clipping correlations after the transform would change the induced density and can also break positive definiteness.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">library</span>(ggplot2)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../R/theme_blog.R"</span>)</span>
<span id="cb1-3"></span>
<span id="cb1-4">schur_constraint_plot <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;">local</span>({</span>
<span id="cb1-5">  previous_block <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;">matrix</span>(</span>
<span id="cb1-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb1-7">      <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb1-8">      <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>,</span>
<span id="cb1-9">      <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span></span>
<span id="cb1-10">    ),</span>
<span id="cb1-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb1-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb1-13">  )</span>
<span id="cb1-14">  previous_cholesky <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;">chol</span>(previous_block)</span>
<span id="cb1-15"></span>
<span id="cb1-16">  lower_bound <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.55</span></span>
<span id="cb1-17">  upper_bound <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span></span>
<span id="cb1-18">  fixed_nonzero <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.70</span></span>
<span id="cb1-19">  lower_free_radius <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>(<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> lower_bound<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-20">  upper_free_radius <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>(<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> upper_bound<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-21">  fixed_free_radius <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>(<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> fixed_nonzero<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-22"></span>
<span id="cb1-23">  zero_panel <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;">paste</span>(</span>
<span id="cb1-24">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Exact zero:  c₁ = 0"</span>,</span>
<span id="cb1-25">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2D linear section"</span>,</span>
<span id="cb1-26">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0² + free² &lt; 1;  free radius = 1.00"</span>,</span>
<span id="cb1-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb1-28">  )</span>
<span id="cb1-29">  bound_panel <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;">paste</span>(</span>
<span id="cb1-30">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Finite bound:  0.55 &lt; c₁ &lt; 0.85"</span>,</span>
<span id="cb1-31">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3D off-origin volume"</span>,</span>
<span id="cb1-32">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(</span>
<span id="cb1-33">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free radius varies: %.2f down to %.2f"</span>,</span>
<span id="cb1-34">      lower_free_radius,</span>
<span id="cb1-35">      upper_free_radius</span>
<span id="cb1-36">    ),</span>
<span id="cb1-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb1-38">  )</span>
<span id="cb1-39">  nonzero_panel <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;">paste</span>(</span>
<span id="cb1-40">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Exact nonzero:  c₁ = 0.70"</span>,</span>
<span id="cb1-41">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2D affine section"</span>,</span>
<span id="cb1-42">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(</span>
<span id="cb1-43">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0.70² + free² &lt; 1;  free radius = %.2f"</span>,</span>
<span id="cb1-44">      fixed_free_radius</span>
<span id="cb1-45">    ),</span>
<span id="cb1-46">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb1-47">  )</span>
<span id="cb1-48">  panel_levels <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;">c</span>(zero_panel, bound_panel, nonzero_panel)</span>
<span id="cb1-49"></span>
<span id="cb1-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(</span>
<span id="cb1-51">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eigen</span>(</span>
<span id="cb1-52">      previous_block,</span>
<span id="cb1-53">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">symmetric =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb1-54">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">only.values =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb1-55">    )<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>values) <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;">0</span>,</span>
<span id="cb1-56">    <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;">&lt;</span> lower_bound,</span>
<span id="cb1-57">    lower_bound <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> upper_bound,</span>
<span id="cb1-58">    upper_bound <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb1-59">    <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;">&lt;</span> fixed_nonzero,</span>
<span id="cb1-60">    fixed_nonzero <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-61">  )</span>
<span id="cb1-62"></span>
<span id="cb1-63">  azimuth <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">72</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi <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;">180</span></span>
<span id="cb1-64">  elevation <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi <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;">180</span></span>
<span id="cb1-65">  roll <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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;">14</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi <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;">180</span></span>
<span id="cb1-66">  right <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;">c</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;">sin</span>(azimuth), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>(azimuth), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb1-67">  up <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;">c</span>(</span>
<span id="cb1-68">    <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;">sin</span>(elevation) <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;">cos</span>(azimuth),</span>
<span id="cb1-69">    <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;">sin</span>(elevation) <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;">sin</span>(azimuth),</span>
<span id="cb1-70">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>(elevation)</span>
<span id="cb1-71">  )</span>
<span id="cb1-72"></span>
<span id="cb1-73">  project_3d <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>(points) {</span>
<span id="cb1-74">    projected <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.matrix</span>(points) <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;">cbind</span>(right, up)</span>
<span id="cb1-75">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-76">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>(roll) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> projected[, <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>
<span id="cb1-77">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(roll) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> projected[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb1-78">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(roll) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> projected[, <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>
<span id="cb1-79">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>(roll) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> projected[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb1-80">    )</span>
<span id="cb1-81">  }</span>
<span id="cb1-82"></span>
<span id="cb1-83">  ellipsoid_section <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>(value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">481</span>) {</span>
<span id="cb1-84">    angle <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;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> n)</span>
<span id="cb1-85">    radius <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>(<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> value<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-86">    sphere_section <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>
<span id="cb1-87">      value,</span>
<span id="cb1-88">      radius <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;">cos</span>(angle),</span>
<span id="cb1-89">      radius <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;">sin</span>(angle)</span>
<span id="cb1-90">    )</span>
<span id="cb1-91">    sphere_section <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> previous_cholesky</span>
<span id="cb1-92">  }</span>
<span id="cb1-93"></span>
<span id="cb1-94">  section_check <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;">ellipsoid_section</span>(fixed_nonzero)</span>
<span id="cb1-95">  previous_precision <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>(previous_block)</span>
<span id="cb1-96">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(</span>
<span id="cb1-97">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(section_check[, <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> fixed_nonzero)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-12</span>,</span>
<span id="cb1-98">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(</span>
<span id="cb1-99">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(</span>
<span id="cb1-100">        (section_check <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> previous_precision) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> section_check</span>
<span id="cb1-101">      ) <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-102">    )) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-10</span></span>
<span id="cb1-103">  )</span>
<span id="cb1-104"></span>
<span id="cb1-105">  ellipsoid_rail <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>(angle, from, to, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">241</span>) {</span>
<span id="cb1-106">    first <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>(from, to, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> n)</span>
<span id="cb1-107">    radius <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>(<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> first<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-108">    sphere_rail <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>
<span id="cb1-109">      first,</span>
<span id="cb1-110">      radius <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;">cos</span>(angle),</span>
<span id="cb1-111">      radius <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;">sin</span>(angle)</span>
<span id="cb1-112">    )</span>
<span id="cb1-113">    sphere_rail <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> previous_cholesky</span>
<span id="cb1-114">  }</span>
<span id="cb1-115"></span>
<span id="cb1-116">  with_panel <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>(data, panel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) {</span>
<span id="cb1-117">    data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>panel <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;">factor</span>(panel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> panel_levels)</span>
<span id="cb1-118">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">is.null</span>(group)) {</span>
<span id="cb1-119">      data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group <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;">paste</span>(panel, group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"::"</span>)</span>
<span id="cb1-120">    }</span>
<span id="cb1-121">    data</span>
<span id="cb1-122">  }</span>
<span id="cb1-123"></span>
<span id="cb1-124">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Common ellipsoid silhouette.</span></span>
<span id="cb1-125">  surface_grid <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;">expand.grid</span>(</span>
<span id="cb1-126">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">first =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</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.999</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.999</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">61</span>),</span>
<span id="cb1-127">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</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;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">121</span>)</span>
<span id="cb1-128">  )</span>
<span id="cb1-129">  surface_radius <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>(<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> surface_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>first<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-130">  surface_sphere <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>
<span id="cb1-131">    surface_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>first,</span>
<span id="cb1-132">    surface_radius <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;">cos</span>(surface_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>angle),</span>
<span id="cb1-133">    surface_radius <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;">sin</span>(surface_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>angle)</span>
<span id="cb1-134">  )</span>
<span id="cb1-135">  surface_projection <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;">project_3d</span>(</span>
<span id="cb1-136">    surface_sphere <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> previous_cholesky</span>
<span id="cb1-137">  )</span>
<span id="cb1-138">  silhouette_one <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> surface_projection[</span>
<span id="cb1-139">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chull</span>(surface_projection<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, surface_projection<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y),</span>
<span id="cb1-140">  ]</span>
<span id="cb1-141">  silhouette <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;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(panel_levels, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(panel) {</span>
<span id="cb1-142">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with_panel</span>(silhouette_one, panel, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ellipsoid"</span>)</span>
<span id="cb1-143">  }))</span>
<span id="cb1-144"></span>
<span id="cb1-145">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sparse common wireframe.</span></span>
<span id="cb1-146">  wire_parts <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>
<span id="cb1-147">  wire_id <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-148">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (value <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</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.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)) {</span>
<span id="cb1-149">    wire_parts[[wire_id]] <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>
<span id="cb1-150">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">project_3d</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ellipsoid_section</span>(value)),</span>
<span id="cb1-151">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wire_group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ring"</span>, wire_id)</span>
<span id="cb1-152">    )</span>
<span id="cb1-153">    wire_id <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> wire_id <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-154">  }</span>
<span id="cb1-155">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (angle <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</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;">330</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi <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;">180</span>) {</span>
<span id="cb1-156">    wire_parts[[wire_id]] <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>
<span id="cb1-157">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">project_3d</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ellipsoid_rail</span>(angle, <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.999</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.999</span>)),</span>
<span id="cb1-158">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wire_group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rail"</span>, wire_id)</span>
<span id="cb1-159">    )</span>
<span id="cb1-160">    wire_id <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> wire_id <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-161">  }</span>
<span id="cb1-162">  wire_one <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;">do.call</span>(rbind, wire_parts)</span>
<span id="cb1-163">  ellipsoid_wire <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;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(panel_levels, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(panel) {</span>
<span id="cb1-164">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with_panel</span>(wire_one, panel, wire_one<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>wire_group)</span>
<span id="cb1-165">  }))</span>
<span id="cb1-166"></span>
<span id="cb1-167">  zero_slice <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_panel</span>(</span>
<span id="cb1-168">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">project_3d</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ellipsoid_section</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)),</span>
<span id="cb1-169">    zero_panel,</span>
<span id="cb1-170">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zero"</span></span>
<span id="cb1-171">  )</span>
<span id="cb1-172">  nonzero_slice <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_panel</span>(</span>
<span id="cb1-173">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">project_3d</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ellipsoid_section</span>(fixed_nonzero)),</span>
<span id="cb1-174">    nonzero_panel,</span>
<span id="cb1-175">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nonzero"</span></span>
<span id="cb1-176">  )</span>
<span id="cb1-177"></span>
<span id="cb1-178">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The interval intersection: a projected silhouette, internal sections,</span></span>
<span id="cb1-179">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># curved boundary rails, and the two planar endpoint faces.</span></span>
<span id="cb1-180">  volume_grid <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;">expand.grid</span>(</span>
<span id="cb1-181">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">first =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(lower_bound, upper_bound, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">81</span>),</span>
<span id="cb1-182">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">angle =</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;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">181</span>)</span>
<span id="cb1-183">  )</span>
<span id="cb1-184">  volume_radius <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>(<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> volume_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>first<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-185">  volume_sphere <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>
<span id="cb1-186">    volume_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>first,</span>
<span id="cb1-187">    volume_radius <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;">cos</span>(volume_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>angle),</span>
<span id="cb1-188">    volume_radius <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;">sin</span>(volume_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>angle)</span>
<span id="cb1-189">  )</span>
<span id="cb1-190">  volume_projection <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;">project_3d</span>(</span>
<span id="cb1-191">    volume_sphere <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> previous_cholesky</span>
<span id="cb1-192">  )</span>
<span id="cb1-193">  volume_silhouette <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_panel</span>(</span>
<span id="cb1-194">    volume_projection[</span>
<span id="cb1-195">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chull</span>(volume_projection<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, volume_projection<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y),</span>
<span id="cb1-196">    ],</span>
<span id="cb1-197">    bound_panel,</span>
<span id="cb1-198">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"volume"</span></span>
<span id="cb1-199">  )</span>
<span id="cb1-200"></span>
<span id="cb1-201">  internal_values <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>
<span id="cb1-202">    lower_bound,</span>
<span id="cb1-203">    upper_bound,</span>
<span id="cb1-204">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span></span>
<span id="cb1-205">  )[<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>]</span>
<span id="cb1-206">  bound_sections <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;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(</span>
<span id="cb1-207">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(internal_values),</span>
<span id="cb1-208">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(index) {</span>
<span id="cb1-209">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with_panel</span>(</span>
<span id="cb1-210">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">project_3d</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ellipsoid_section</span>(internal_values[index])),</span>
<span id="cb1-211">        bound_panel,</span>
<span id="cb1-212">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"section"</span>, index)</span>
<span id="cb1-213">      )</span>
<span id="cb1-214">    }</span>
<span id="cb1-215">  ))</span>
<span id="cb1-216">  bound_rails <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;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(</span>
<span id="cb1-217">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</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;">330</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)),</span>
<span id="cb1-218">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(index) {</span>
<span id="cb1-219">      angle <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;">330</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)[index] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi <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;">180</span></span>
<span id="cb1-220">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with_panel</span>(</span>
<span id="cb1-221">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">project_3d</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ellipsoid_rail</span>(</span>
<span id="cb1-222">          angle,</span>
<span id="cb1-223">          lower_bound,</span>
<span id="cb1-224">          upper_bound</span>
<span id="cb1-225">        )),</span>
<span id="cb1-226">        bound_panel,</span>
<span id="cb1-227">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bound_rail"</span>, index)</span>
<span id="cb1-228">      )</span>
<span id="cb1-229">    }</span>
<span id="cb1-230">  ))</span>
<span id="cb1-231">  bound_faces <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;">rbind</span>(</span>
<span id="cb1-232">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with_panel</span>(</span>
<span id="cb1-233">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">project_3d</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ellipsoid_section</span>(lower_bound)),</span>
<span id="cb1-234">      bound_panel,</span>
<span id="cb1-235">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lower_face"</span></span>
<span id="cb1-236">    ),</span>
<span id="cb1-237">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with_panel</span>(</span>
<span id="cb1-238">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">project_3d</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ellipsoid_section</span>(upper_bound)),</span>
<span id="cb1-239">      bound_panel,</span>
<span id="cb1-240">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"upper_face"</span></span>
<span id="cb1-241">    )</span>
<span id="cb1-242">  )</span>
<span id="cb1-243"></span>
<span id="cb1-244">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A common c1 axis and origin make central versus affine obvious.</span></span>
<span id="cb1-245">  c1_axis_3d <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;">rbind</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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;">1.08</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;">0</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.08</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;">0</span>))</span>
<span id="cb1-246">  c1_axis_projected <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;">project_3d</span>(c1_axis_3d)</span>
<span id="cb1-247">  c1_axis <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;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(panel_levels, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(panel) {</span>
<span id="cb1-248">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-249">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> c1_axis_projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb1-250">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> c1_axis_projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb1-251">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> c1_axis_projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb1-252">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> c1_axis_projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb1-253">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(panel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> panel_levels)</span>
<span id="cb1-254">    )</span>
<span id="cb1-255">  }))</span>
<span id="cb1-256">  c1_label <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;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(panel_levels, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(panel) {</span>
<span id="cb1-257">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-258">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> c1_axis_projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb1-259">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> c1_axis_projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb1-260">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c[1]"</span>,</span>
<span id="cb1-261">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(panel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> panel_levels)</span>
<span id="cb1-262">    )</span>
<span id="cb1-263">  }))</span>
<span id="cb1-264">  origin_projection <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;">project_3d</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;">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;">0</span>, <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;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb1-265">  origins <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-266">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> origin_projection<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x,</span>
<span id="cb1-267">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> origin_projection<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y,</span>
<span id="cb1-268">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(panel_levels, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> panel_levels)</span>
<span id="cb1-269">  )</span>
<span id="cb1-270">  origin_labels <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;">transform</span>(</span>
<span id="cb1-271">    origins,</span>
<span id="cb1-272">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x <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.05</span>,</span>
<span id="cb1-273">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y <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.055</span>,</span>
<span id="cb1-274">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span></span>
<span id="cb1-275">  )</span>
<span id="cb1-276"></span>
<span id="cb1-277">  nonzero_center_3d <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;">c</span>(</span>
<span id="cb1-278">    fixed_nonzero,</span>
<span id="cb1-279">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> fixed_nonzero,</span>
<span id="cb1-280">    <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> fixed_nonzero</span>
<span id="cb1-281">  )</span>
<span id="cb1-282">  nonzero_center <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;">project_3d</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(nonzero_center_3d, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb1-283">  offset_segment <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-284">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> origin_projection<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x,</span>
<span id="cb1-285">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> origin_projection<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y,</span>
<span id="cb1-286">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> nonzero_center<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x,</span>
<span id="cb1-287">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> nonzero_center<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y,</span>
<span id="cb1-288">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(nonzero_panel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> panel_levels)</span>
<span id="cb1-289">  )</span>
<span id="cb1-290"></span>
<span id="cb1-291">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># In Cholesky coordinates, fixing the first coordinate to q leaves</span></span>
<span id="cb1-292">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># v_2^2 + v_3^2 &lt; 1 - q^2. Draw one transformed radius in each</span></span>
<span id="cb1-293">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># section so the spent and remaining parts of that budget are visible.</span></span>
<span id="cb1-294">  free_radius_segment <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>(value, panel) {</span>
<span id="cb1-295">    radius <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>(<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> value<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-296">    points <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;">rbind</span>(</span>
<span id="cb1-297">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(value, <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;">0</span>),</span>
<span id="cb1-298">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(value, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, radius)</span>
<span id="cb1-299">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> previous_cholesky</span>
<span id="cb1-300">    projected <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;">project_3d</span>(points)</span>
<span id="cb1-301">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-302">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb1-303">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb1-304">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb1-305">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> projected<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb1-306">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(panel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> panel_levels)</span>
<span id="cb1-307">    )</span>
<span id="cb1-308">  }</span>
<span id="cb1-309"></span>
<span id="cb1-310">  zero_radius_segment <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;">free_radius_segment</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, zero_panel)</span>
<span id="cb1-311">  bound_radius_segments <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;">rbind</span>(</span>
<span id="cb1-312">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transform</span>(</span>
<span id="cb1-313">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">free_radius_segment</span>(lower_bound, bound_panel),</span>
<span id="cb1-314">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lower"</span></span>
<span id="cb1-315">    ),</span>
<span id="cb1-316">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transform</span>(</span>
<span id="cb1-317">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">free_radius_segment</span>(upper_bound, bound_panel),</span>
<span id="cb1-318">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"upper"</span></span>
<span id="cb1-319">    )</span>
<span id="cb1-320">  )</span>
<span id="cb1-321">  nonzero_radius_segment <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;">free_radius_segment</span>(</span>
<span id="cb1-322">    fixed_nonzero,</span>
<span id="cb1-323">    nonzero_panel</span>
<span id="cb1-324">  )</span>
<span id="cb1-325"></span>
<span id="cb1-326">  radius_target <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>(segment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fraction =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.62</span>) {</span>
<span id="cb1-327">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb1-328">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> segment<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> fraction <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (segment<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>xend <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> segment<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x),</span>
<span id="cb1-329">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> segment<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> fraction <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (segment<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>yend <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> segment<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y)</span>
<span id="cb1-330">    )</span>
<span id="cb1-331">  }</span>
<span id="cb1-332">  callout_targets <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;">rbind</span>(</span>
<span id="cb1-333">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">radius_target</span>(zero_radius_segment),</span>
<span id="cb1-334">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">radius_target</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(bound_radius_segments, face <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lower"</span>)),</span>
<span id="cb1-335">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">radius_target</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(bound_radius_segments, face <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"upper"</span>)),</span>
<span id="cb1-336">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">radius_target</span>(nonzero_radius_segment)</span>
<span id="cb1-337">  )</span>
<span id="cb1-338">  free_radius_arrows <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-339">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow_x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.40</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.10</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.26</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.37</span>),</span>
<span id="cb1-340">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow_y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.84</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb1-341">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> callout_targets[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>],</span>
<span id="cb1-342">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> callout_targets[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>],</span>
<span id="cb1-343">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">constraint =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(</span>
<span id="cb1-344">      <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;">"zero"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bound"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bound"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nonzero"</span>),</span>
<span id="cb1-345">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <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;">"zero"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bound"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nonzero"</span>)</span>
<span id="cb1-346">    ),</span>
<span id="cb1-347">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(</span>
<span id="cb1-348">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(zero_panel, bound_panel, bound_panel, nonzero_panel),</span>
<span id="cb1-349">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> panel_levels</span>
<span id="cb1-350">    )</span>
<span id="cb1-351">  )</span>
<span id="cb1-352">  free_radius_labels <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-353">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label_x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.40</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.18</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.37</span>),</span>
<span id="cb1-354">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label_y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.91</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb1-355">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">constraint =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(</span>
<span id="cb1-356">      <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;">"zero"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bound"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nonzero"</span>),</span>
<span id="cb1-357">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <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;">"zero"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bound"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nonzero"</span>)</span>
<span id="cb1-358">    ),</span>
<span id="cb1-359">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(panel_levels, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> panel_levels),</span>
<span id="cb1-360">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free radius"</span></span>
<span id="cb1-361">  )</span>
<span id="cb1-362"></span>
<span id="cb1-363">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-364">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(</span>
<span id="cb1-365">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> silhouette,</span>
<span id="cb1-366">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group),</span>
<span id="cb1-367">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blue,</span>
<span id="cb1-368">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span id="cb1-369">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.055</span></span>
<span id="cb1-370">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-371">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(</span>
<span id="cb1-372">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ellipsoid_wire,</span>
<span id="cb1-373">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group),</span>
<span id="cb1-374">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blue,</span>
<span id="cb1-375">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.07</span>,</span>
<span id="cb1-376">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span></span>
<span id="cb1-377">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-378">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(</span>
<span id="cb1-379">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> c1_axis,</span>
<span id="cb1-380">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> xend, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> yend),</span>
<span id="cb1-381">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_light,</span>
<span id="cb1-382">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"22"</span>,</span>
<span id="cb1-383">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.55</span>,</span>
<span id="cb1-384">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> grid<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;">arrow</span>(</span>
<span id="cb1-385">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> grid<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;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>),</span>
<span id="cb1-386">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"closed"</span></span>
<span id="cb1-387">      )</span>
<span id="cb1-388">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-389">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(</span>
<span id="cb1-390">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> volume_silhouette,</span>
<span id="cb1-391">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group),</span>
<span id="cb1-392">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal,</span>
<span id="cb1-393">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span id="cb1-394">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.045</span></span>
<span id="cb1-395">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-396">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(</span>
<span id="cb1-397">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> bound_sections,</span>
<span id="cb1-398">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group),</span>
<span id="cb1-399">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal,</span>
<span id="cb1-400">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.24</span>,</span>
<span id="cb1-401">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span></span>
<span id="cb1-402">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-403">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(</span>
<span id="cb1-404">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> bound_rails,</span>
<span id="cb1-405">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group),</span>
<span id="cb1-406">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal,</span>
<span id="cb1-407">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.48</span>,</span>
<span id="cb1-408">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.45</span></span>
<span id="cb1-409">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-410">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(</span>
<span id="cb1-411">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(bound_faces, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lower_face"</span>, group)),</span>
<span id="cb1-412">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group),</span>
<span id="cb1-413">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal,</span>
<span id="cb1-414">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal,</span>
<span id="cb1-415">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.06</span>,</span>
<span id="cb1-416">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"22"</span>,</span>
<span id="cb1-417">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span></span>
<span id="cb1-418">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-419">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(</span>
<span id="cb1-420">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(bound_faces, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"upper_face"</span>, group)),</span>
<span id="cb1-421">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group),</span>
<span id="cb1-422">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal,</span>
<span id="cb1-423">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal,</span>
<span id="cb1-424">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.16</span>,</span>
<span id="cb1-425">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.05</span></span>
<span id="cb1-426">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-427">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(</span>
<span id="cb1-428">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> zero_slice,</span>
<span id="cb1-429">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group),</span>
<span id="cb1-430">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple,</span>
<span id="cb1-431">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple,</span>
<span id="cb1-432">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.27</span>,</span>
<span id="cb1-433">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span></span>
<span id="cb1-434">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-435">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(</span>
<span id="cb1-436">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> nonzero_slice,</span>
<span id="cb1-437">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group),</span>
<span id="cb1-438">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>gold,</span>
<span id="cb1-439">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>gold,</span>
<span id="cb1-440">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.27</span>,</span>
<span id="cb1-441">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span></span>
<span id="cb1-442">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-443">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(</span>
<span id="cb1-444">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ellipsoid_wire,</span>
<span id="cb1-445">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group),</span>
<span id="cb1-446">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blue,</span>
<span id="cb1-447">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.23</span>,</span>
<span id="cb1-448">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.30</span></span>
<span id="cb1-449">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-450">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(</span>
<span id="cb1-451">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> zero_radius_segment,</span>
<span id="cb1-452">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> xend, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> yend),</span>
<span id="cb1-453">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple,</span>
<span id="cb1-454">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.05</span>,</span>
<span id="cb1-455">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> grid<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;">arrow</span>(</span>
<span id="cb1-456">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> grid<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;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>),</span>
<span id="cb1-457">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"closed"</span></span>
<span id="cb1-458">      )</span>
<span id="cb1-459">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-460">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(</span>
<span id="cb1-461">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> bound_radius_segments,</span>
<span id="cb1-462">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> xend, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> yend),</span>
<span id="cb1-463">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal,</span>
<span id="cb1-464">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.05</span>,</span>
<span id="cb1-465">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> grid<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;">arrow</span>(</span>
<span id="cb1-466">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> grid<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;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>),</span>
<span id="cb1-467">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"closed"</span></span>
<span id="cb1-468">      )</span>
<span id="cb1-469">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-470">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(</span>
<span id="cb1-471">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> offset_segment,</span>
<span id="cb1-472">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> xend, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> yend),</span>
<span id="cb1-473">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>gold,</span>
<span id="cb1-474">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"22"</span>,</span>
<span id="cb1-475">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>,</span>
<span id="cb1-476">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> grid<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;">arrow</span>(</span>
<span id="cb1-477">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> grid<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;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>),</span>
<span id="cb1-478">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"closed"</span></span>
<span id="cb1-479">      )</span>
<span id="cb1-480">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-481">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>(</span>
<span id="cb1-482">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> nonzero_radius_segment,</span>
<span id="cb1-483">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> xend, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> yend),</span>
<span id="cb1-484">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>gold,</span>
<span id="cb1-485">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.05</span>,</span>
<span id="cb1-486">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> grid<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;">arrow</span>(</span>
<span id="cb1-487">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> grid<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;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>),</span>
<span id="cb1-488">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"closed"</span></span>
<span id="cb1-489">      )</span>
<span id="cb1-490">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-491">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(</span>
<span id="cb1-492">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> origins,</span>
<span id="cb1-493">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y),</span>
<span id="cb1-494">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>,</span>
<span id="cb1-495">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>background,</span>
<span id="cb1-496">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>navy,</span>
<span id="cb1-497">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.6</span>,</span>
<span id="cb1-498">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stroke =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span></span>
<span id="cb1-499">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-500">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(</span>
<span id="cb1-501">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> origin_labels,</span>
<span id="cb1-502">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> label),</span>
<span id="cb1-503">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>navy,</span>
<span id="cb1-504">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>,</span>
<span id="cb1-505">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb1-506">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb1-507">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-508">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(</span>
<span id="cb1-509">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transform</span>(</span>
<span id="cb1-510">        nonzero_center,</span>
<span id="cb1-511">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(nonzero_panel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> panel_levels)</span>
<span id="cb1-512">      ),</span>
<span id="cb1-513">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y),</span>
<span id="cb1-514">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>gold,</span>
<span id="cb1-515">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.4</span></span>
<span id="cb1-516">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-517">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_curve</span>(</span>
<span id="cb1-518">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> free_radius_arrows,</span>
<span id="cb1-519">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb1-520">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> arrow_x,</span>
<span id="cb1-521">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> arrow_y,</span>
<span id="cb1-522">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> xend,</span>
<span id="cb1-523">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> yend,</span>
<span id="cb1-524">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> constraint</span>
<span id="cb1-525">      ),</span>
<span id="cb1-526">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">curvature =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>,</span>
<span id="cb1-527">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.65</span>,</span>
<span id="cb1-528">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">arrow =</span> grid<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;">arrow</span>(</span>
<span id="cb1-529">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> grid<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;">unit</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cm"</span>),</span>
<span id="cb1-530">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"closed"</span></span>
<span id="cb1-531">      )</span>
<span id="cb1-532">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-533">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(</span>
<span id="cb1-534">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> free_radius_labels,</span>
<span id="cb1-535">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb1-536">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> label_x,</span>
<span id="cb1-537">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> label_y,</span>
<span id="cb1-538">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> label,</span>
<span id="cb1-539">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> constraint</span>
<span id="cb1-540">      ),</span>
<span id="cb1-541">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fontface =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>,</span>
<span id="cb1-542">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span></span>
<span id="cb1-543">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-544">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(</span>
<span id="cb1-545">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> c1_label,</span>
<span id="cb1-546">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> label),</span>
<span id="cb1-547">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parse =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb1-548">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text_light,</span>
<span id="cb1-549">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>,</span>
<span id="cb1-550">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>,</span>
<span id="cb1-551">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.3</span></span>
<span id="cb1-552">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-553">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(</span>
<span id="cb1-554">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb1-555">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">zero =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple,</span>
<span id="cb1-556">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bound =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal,</span>
<span id="cb1-557">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nonzero =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>gold</span>
<span id="cb1-558">      ),</span>
<span id="cb1-559">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span></span>
<span id="cb1-560">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-561">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>panel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</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>
<span id="cb1-562">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>(</span>
<span id="cb1-563">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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;">1.35</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.35</span>),</span>
<span id="cb1-564">      <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.20</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span>),</span>
<span id="cb1-565">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb1-566">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-567">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-568">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb1-569">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-570">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-571">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-572">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-573">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-574">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-575">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.line =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-576">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb1-577">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">9.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lineheight =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.12</span>)</span>
<span id="cb1-578">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-579">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb1-580">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Zero, Bound, and Nonzero Constraints Use the Row Budget Differently"</span>,</span>
<span id="cb1-581">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(</span>
<span id="cb1-582">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cholesky-row budget:  q² + free² &lt; 1."</span>,</span>
<span id="cb1-583">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fixing q leaves radius √(1 − q²); an interval makes that radius vary."</span></span>
<span id="cb1-584">      )</span>
<span id="cb1-585">    )</span>
<span id="cb1-586">})</span>
<span id="cb1-587"></span>
<span id="cb1-588">schur_constraint_plot</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-schur-ellipsoid-slabs" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-schur-ellipsoid-slabs-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-12-corr-bounds/corr-bounds_files/figure-html/fig-schur-ellipsoid-slabs-1.png" class="img-fluid figure-img" width="1152">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-schur-ellipsoid-slabs-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Each panel fixes the same leading <img src="https://latex.codecogs.com/png.latex?3%5Ctimes3"> correlation block and shows the feasible values of <img src="https://latex.codecogs.com/png.latex?c=(%5COmega_%7B4,1%7D,%5COmega_%7B4,2%7D,%5COmega_%7B4,3%7D)">. For this block, <img src="https://latex.codecogs.com/png.latex?c_1"> equals the first off-diagonal coordinate in the fourth Cholesky row, so fixing <img src="https://latex.codecogs.com/png.latex?c_1=q"> leaves the other two coordinates inside a disk of radius <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B1-q%5E2%7D">. Setting <img src="https://latex.codecogs.com/png.latex?c_1=0"> therefore gives a two-dimensional linear slice with radius <img src="https://latex.codecogs.com/png.latex?1">. Allowing <img src="https://latex.codecogs.com/png.latex?0.55%3Cc_1%3C0.85"> retains a three-dimensional region whose cross-sectional radius decreases from <img src="https://latex.codecogs.com/png.latex?0.84"> to <img src="https://latex.codecogs.com/png.latex?0.53">. Fixing <img src="https://latex.codecogs.com/png.latex?c_1=0.70"> gives a two-dimensional affine slice with radius <img src="https://latex.codecogs.com/png.latex?0.71">. Exact constraints remove one degree of freedom; a positive-width bound retains it while changing the available radius.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="the-smooth-triangular-sign-transform" class="level2">
<h2 class="anchored" data-anchor-id="the-smooth-triangular-sign-transform">3. The Smooth Triangular Sign Transform</h2>
<p>Sign constraints retain one useful property: they are homogeneous. If <img src="https://latex.codecogs.com/png.latex?c"> has the right signs, then <img src="https://latex.codecogs.com/png.latex?a%20c"> has the same signs for every <img src="https://latex.codecogs.com/png.latex?a%3E0">. Their directions form an orthant cone; the Schur ellipsoid truncates each ray at the positive-definite boundary.</p>
<p>Let <img src="https://latex.codecogs.com/png.latex?s_j%5Cin%5C%7B-1,+1%5C%7D"> be the requested sign of correlation <img src="https://latex.codecogs.com/png.latex?(i,j)">. Choose a smooth, strictly increasing bijection <img src="https://latex.codecogs.com/png.latex?g:%5Cmathbb%20R%5Cto(0,%5Cinfty)"> with <img src="https://latex.codecogs.com/png.latex?g'(y)%3E0">, and set</p>
<p><img src="https://latex.codecogs.com/png.latex?x_j=s_j%5C,g(y_j),%20%5Cqquad%20g(y)%3E0."></p>
<p>The implementation uses the numerically stable softplus divided by <img src="https://latex.codecogs.com/png.latex?2K">, which has these properties. The factor <img src="https://latex.codecogs.com/png.latex?2K"> changes the scale near the default initialization but not the image <img src="https://latex.codecogs.com/png.latex?(0,%5Cinfty)">. Solve through the previous Cholesky triangle,</p>
<p><img src="https://latex.codecogs.com/png.latex?w=B%5E%7B-1%7Dx,%20%5Cqquad%20t=w'w,"></p>
<p>then reuse the modified stereographic hemisphere map from <span class="citation" data-cites="pinkney2025">Pinkney (2025)</span>,</p>
<p><img src="https://latex.codecogs.com/png.latex?a(t)=%5Cfrac%7B%5Csqrt%7Bt+2%7D%7D%7Bt+1%7D,%20%5Cqquad%0Av=a(t)w,%20%5Cqquad%0AL_%7Bii%7D=%5Cfrac%7B1%7D%7Bt+1%7D."></p>
<p>The row has unit norm because</p>
<p><img src="https://latex.codecogs.com/png.latex?v'v+L_%7Bii%7D%5E2%0A=%5Cfrac%7Bt(t+2)+1%7D%7B(t+1)%5E2%7D=1."></p>
<p>The corresponding ordinary correlations are</p>
<p><img src="https://latex.codecogs.com/png.latex?c=Bv=a(t)Bw=a(t)x."></p>
<p>Since <img src="https://latex.codecogs.com/png.latex?a(t)%3E0">, every correlation has exactly the requested sign. In exact arithmetic, every finite raw vector therefore produces a positive-definite correlation matrix in the desired orthant without a feasibility check, projection, or rejection.</p>
<p>To check coverage conditional on the previous rows, take any <img src="https://latex.codecogs.com/png.latex?c"> with the prescribed strict signs and <img src="https://latex.codecogs.com/png.latex?q=c'(BB')%5E%7B-1%7Dc%3C1">. Set <img src="https://latex.codecogs.com/png.latex?v=B%5E%7B-1%7Dc">. The inverse radial coordinate is unique:</p>
<p><img src="https://latex.codecogs.com/png.latex?t=%5Cfrac%7B1%7D%7B%5Csqrt%7B1-q%7D%7D-1."></p>
<p>Then <img src="https://latex.codecogs.com/png.latex?w=v/a(t)"> and <img src="https://latex.codecogs.com/png.latex?x=Bw=c/a(t)">. Because <img src="https://latex.codecogs.com/png.latex?a(t)%3E0">, <img src="https://latex.codecogs.com/png.latex?x"> has the same signs as <img src="https://latex.codecogs.com/png.latex?c">, and <img src="https://latex.codecogs.com/png.latex?g"> can be inverted coordinate by coordinate. Thus each admissible row has one finite raw preimage. Applying this inverse successively from row 2 through row <img src="https://latex.codecogs.com/png.latex?K"> establishes the bijection for the complete matrix.</p>
<section id="jacobian" class="level3">
<h3 class="anchored" data-anchor-id="jacobian">3.1 Jacobian</h3>
<p>At fixed earlier rows, the determinants from <img src="https://latex.codecogs.com/png.latex?x%5Cmapsto%20w=B%5E%7B-1%7Dx"> and <img src="https://latex.codecogs.com/png.latex?v%5Cmapsto%20c=Bv"> cancel. For <img src="https://latex.codecogs.com/png.latex?d=i-1">, the row contribution with respect to the ordinary correlations is</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cdet%5Cleft%5C%7B%5Cfrac%7B%5Cpartial(a(t)w)%7D%7B%5Cpartial%20w%7D%5Cright%5C%7D%0A=a(t)%5Ed%5Cleft%5C%7B1+2t%5Cfrac%7Ba'(t)%7D%7Ba(t)%7D%5Cright%5C%7D%0A=%5Cfrac%7B2(t+2)%5E%7B(d-2)/2%7D%7D%7B(1+t)%5E%7Bd+1%7D%7D,"></p>
<p>where the first equality follows from the matrix determinant lemma. Including the coordinate-wise derivative of <img src="https://latex.codecogs.com/png.latex?g"> gives</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Clog%20J_i%0A=%5Csum_%7Bj=1%7D%5E%7Bd%7D%5Clog%20g'(y_j)%0A+%5Clog%202%0A+%5Cfrac%7Bd-2%7D%7B2%7D%5Clog(t+2)%0A-(d+1)%5Clog(1+t)."></p>
<p>The complete raw-to-free-correlation Jacobian is block triangular by rows, so the full log Jacobian is <img src="https://latex.codecogs.com/png.latex?%5Csum_i%5Clog%20J_i">. The Stan target is then</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Clog%20J+(%5Ceta-1)%5Clog%5Cdet(%5COmega)."></p>
<p>This ordinary-correlation formulation matters. Using <code>lkj_corr_cholesky_lpdf</code> would instead require the raw-to-free-Cholesky Jacobian; combining that density with the ordinary-correlation Jacobian above would count the Cholesky-to-correlation change of variables twice.</p>
<p>Any strict sign pattern is nonempty. Let <img src="https://latex.codecogs.com/png.latex?S"> be the symmetric zero-diagonal matrix whose off-diagonal entries are the requested signs. Then <img src="https://latex.codecogs.com/png.latex?I+%5Cepsilon%20S"> has that pattern and is positive definite whenever <img src="https://latex.codecogs.com/png.latex?0%3C%5Cepsilon%3C1/%5ClVert%20S%5CrVert_2">. For an all-negative matrix, <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%201'%5COmega%5Cmathbf%201%3E0"> also places the mean off-diagonal correlation in <img src="https://latex.codecogs.com/png.latex?(-1/(K-1),0)">. Equivalently, its mean absolute correlation is less than <img src="https://latex.codecogs.com/png.latex?1/(K-1)">.</p>
</section>
</section>
<section id="sec-global-ray" class="level2">
<h2 class="anchored" data-anchor-id="sec-global-ray">4. A Feasible Anchor for the Global-Ray Construction</h2>
<p>Individually sensible intervals need not admit a correlation matrix. For example, at <img src="https://latex.codecogs.com/png.latex?K=3"> the bounds</p>
<p><img src="https://latex.codecogs.com/png.latex?%5COmega_%7B12%7D,%5COmega_%7B13%7D%5Cin%5B0.8,0.9%5D,%5Cqquad%0A%5COmega_%7B23%7D%5Cin%5B-0.9,-0.8%5D"></p>
<p>are infeasible. Writing these entries as <img src="https://latex.codecogs.com/png.latex?a">, <img src="https://latex.codecogs.com/png.latex?b">, and <img src="https://latex.codecogs.com/png.latex?c">, respectively, gives</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cdet(%5COmega)=1+2abc-a%5E2-b%5E2-c%5E2%0A%5Cle%201-1.024-1.92=-1.944."></p>
<p>This example shows why a general-box construction needs a separate feasibility check.</p>
<p>For now, suppose every interval has positive width. Let <img src="https://latex.codecogs.com/png.latex?%5COmega_0"> be a strictly feasible anchor: it is positive definite and every off-diagonal entry lies strictly between its bounds. Vectorize the <img src="https://latex.codecogs.com/png.latex?D=K(K-1)/2"> lower-triangular entries, choose a unit direction <img src="https://latex.codecogs.com/png.latex?u%5Cin%5Cmathbb%20S%5E%7BD-1%7D">, and place it into a symmetric zero-diagonal matrix <img src="https://latex.codecogs.com/png.latex?U(u)">.</p>
<p>Along the ray</p>
<p><img src="https://latex.codecogs.com/png.latex?%5COmega(r)=%5COmega_0+rU,"></p>
<p>each box face supplies an upper limit on <img src="https://latex.codecogs.com/png.latex?r">. If <img src="https://latex.codecogs.com/png.latex?U_%7Bij%7D%3E0">, that limit is <img src="https://latex.codecogs.com/png.latex?(u_%7Bij%7D-%5COmega_%7B0,ij%7D)/U_%7Bij%7D">; if <img src="https://latex.codecogs.com/png.latex?U_%7Bij%7D%3C0">, it is <img src="https://latex.codecogs.com/png.latex?(%5Cell_%7Bij%7D-%5COmega_%7B0,ij%7D)/U_%7Bij%7D">. Let their minimum be <img src="https://latex.codecogs.com/png.latex?R_%7B%5Ctext%7Bbox%7D%7D(u)">.</p>
<p>The positive-definite boundary is also available exactly. With <img src="https://latex.codecogs.com/png.latex?%5COmega_0=L_0L_0'"> and</p>
<p><img src="https://latex.codecogs.com/png.latex?A(u)=L_0%5E%7B-1%7DU(u)L_0%5E%7B-T%7D,"></p>
<p>we have</p>
<p><img src="https://latex.codecogs.com/png.latex?%5COmega_0+rU=L_0%5C%7BI+rA(u)%5C%7DL_0'."></p>
<p>Because <img src="https://latex.codecogs.com/png.latex?U"> is nonzero, symmetric, and has trace zero, it has both positive and negative eigenvalues. Moreover, <img src="https://latex.codecogs.com/png.latex?A(u)=S%20U(u)S'"> with the nonsingular matrix <img src="https://latex.codecogs.com/png.latex?S=L_0%5E%7B-1%7D">, so <img src="https://latex.codecogs.com/png.latex?A(u)"> and <img src="https://latex.codecogs.com/png.latex?U(u)"> have the same inertia by Sylvester’s law of inertia <span class="citation" data-cites="sylvester1852 hornJohnson2013">(Sylvester 1852; Horn and Johnson 2013, Theorem 4.5.8, p.&nbsp;282)</span>. Hence <img src="https://latex.codecogs.com/png.latex?%5Clambda_%7B%5Cmin%7D%5C%7BA(u)%5C%7D%3C0">, and the first positive ray intersection with the semidefinite boundary is</p>
<p><img src="https://latex.codecogs.com/png.latex?R_%7B%5Ctext%7BPD%7D%7D(u)=-%5Cfrac%7B1%7D%7B%5Clambda_%7B%5Cmin%7D%5C%7BA(u)%5C%7D%7D."></p>
<p>Set <img src="https://latex.codecogs.com/png.latex?R(u)=%5Cmin%5C%7BR_%7B%5Ctext%7Bbox%7D%7D(u),R_%7B%5Ctext%7BPD%7D%7D(u)%5C%7D"> and</p>
<p><img src="https://latex.codecogs.com/png.latex?%5COmega=%5COmega_0+%5Crho%20R(u)U(u),%5Cqquad%200%3C%5Crho%3C1."></p>
<p>The intersection of the box, the affine unit-diagonal space, and the positive-definite cone is convex, so it is star-shaped about <img src="https://latex.codecogs.com/png.latex?%5COmega_0">. Conversely, any point in its interior other than <img src="https://latex.codecogs.com/png.latex?%5COmega_0"> determines a unique direction</p>
<p><img src="https://latex.codecogs.com/png.latex?u=%5Cfrac%7B%5Coperatorname%7Bvech%7D_%7B%5C!%5Cell%7D(%5COmega-%5COmega_0)%7D%0A%7B%5ClVert%5Coperatorname%7Bvech%7D_%7B%5C!%5Cell%7D(%5COmega-%5COmega_0)%5CrVert%7D"></p>
<p>and a unique <img src="https://latex.codecogs.com/png.latex?%5Crho=%5ClVert%5Coperatorname%7Bvech%7D_%7B%5C!%5Cell%7D(%5COmega-%5COmega_0)%5CrVert/R(u)">. Thus <img src="https://latex.codecogs.com/png.latex?(%5Crho,u)%5Cmapsto%5COmega"> is one-to-one and onto the feasible interior except for the anchor, a single omitted point. The implementation represents the unit direction as <img src="https://latex.codecogs.com/png.latex?u=z/%5ClVert%20z%5CrVert"> for an unconstrained vector <img src="https://latex.codecogs.com/png.latex?z">. The norm of <img src="https://latex.codecogs.com/png.latex?z"> is an auxiliary parameter, discussed below, rather than another correlation-matrix coordinate.</p>
<section id="jacobian-and-the-auxiliary-direction" class="level3">
<h3 class="anchored" data-anchor-id="jacobian-and-the-auxiliary-direction">4.1 Jacobian and the Auxiliary Direction</h3>
<p>Let <img src="https://latex.codecogs.com/png.latex?h=%5Coperatorname%7Bvech%7D_%7B%5C!%5Cell%7D(%5COmega-%5COmega_0)"> and let <img src="https://latex.codecogs.com/png.latex?d%5Csigma(u)"> denote surface measure on <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%20S%5E%7BD-1%7D">. Polar volume gives</p>
<p><img src="https://latex.codecogs.com/png.latex?dh=%5Crho%5E%7BD-1%7DR(u)%5ED%5C,d%5Crho%5C,d%5Csigma(u)."></p>
<p>Angular derivatives of <img src="https://latex.codecogs.com/png.latex?R(u)"> do not add another term. In local angular coordinates, the part involving <img src="https://latex.codecogs.com/png.latex?%5Cpartial%20R/%5Cpartial%5Ctheta"> is proportional to <img src="https://latex.codecogs.com/png.latex?u">, so it is collinear with the radial derivative and drops out of the determinant. The implementation obtains a uniform direction as <img src="https://latex.codecogs.com/png.latex?u=z/%5ClVert%20z%5CrVert"> with <img src="https://latex.codecogs.com/png.latex?z%5Csim%5Cmathcal%20N(0,I_D)">. Under the isotropic normal density, <img src="https://latex.codecogs.com/png.latex?u"> is uniform on the sphere and independent of the chi-distributed norm of <img src="https://latex.codecogs.com/png.latex?z">. Without that radial density, the target would be improper along rescalings of <img src="https://latex.codecogs.com/png.latex?z">.</p>
<p>In exact arithmetic, a strict anchor makes every finite raw state feasible, but the map is not globally smooth. The minimum changes identity where two box faces exchange roles or where the active boundary changes between a box face and the positive-definite cone. Differentiability can also fail when the smallest eigenvalue has multiplicity greater than one. The boundary-radius function is differentiable almost everywhere; ties and eigenvalue multiplicities are possible exceptions. They do not change the almost-everywhere Jacobian used for the continuous target, although an HMC trajectory can cross the resulting kinks. The auxiliary direction must be initialized away from zero. The supplied Stan implementation requires <img src="https://latex.codecogs.com/png.latex?K%5Cge3"> and at least two free correlations; the one-dimensional case should instead use a direct scalar transform over its feasible interval.</p>
</section>
</section>
<section id="exact-nonzero-correlations-are-affine-slices" class="level2">
<h2 class="anchored" data-anchor-id="exact-nonzero-correlations-are-affine-slices">5. Exact Nonzero Correlations Are Affine Slices</h2>
<p>Suppose selected correlations are fixed exactly rather than merely bounded. In row <img src="https://latex.codecogs.com/png.latex?i">, let the nonempty set <img src="https://latex.codecogs.com/png.latex?Z"> index the fixed correlations, let <img src="https://latex.codecogs.com/png.latex?q"> contain their specified values, and let <img src="https://latex.codecogs.com/png.latex?P_Z"> select those cells from <img src="https://latex.codecogs.com/png.latex?c=Bv">. Rows with <img src="https://latex.codecogs.com/png.latex?Z=%5Cvarnothing"> use the unconstrained row map. For <img src="https://latex.codecogs.com/png.latex?Z%5Cne%5Cvarnothing">, the row constraint is</p>
<p><img src="https://latex.codecogs.com/png.latex?Av=q,%5Cqquad%20A=P_ZB."></p>
<p>For a structural zero, <img src="https://latex.codecogs.com/png.latex?q=0"> and the solution set is the linear null space used by the triangular zero construction in Section 2. For <img src="https://latex.codecogs.com/png.latex?q%5Cne0">, the solution set is an affine translation that does not pass through the origin. This translation uses part of the available Cholesky row norm, so substituting a nonzero right-hand side into the zero basis while leaving the rest of the transform unchanged is not sufficient.</p>
<p>Let</p>
<p><img src="https://latex.codecogs.com/png.latex?G=AA'=%5COmega_%7BZ,Z%7D,%5Cqquad%0Av_0=A'G%5E%7B-1%7Dq,"></p>
<p>so that <img src="https://latex.codecogs.com/png.latex?v_0"> is the minimum-norm particular solution, and let <img src="https://latex.codecogs.com/png.latex?Q"> be an orthonormal basis for <img src="https://latex.codecogs.com/png.latex?%5Cker(A)">. Then <img src="https://latex.codecogs.com/png.latex?Q'v_0=0"> and every solution is <img src="https://latex.codecogs.com/png.latex?v=v_0+Qz">. A positive diagonal remains available exactly when</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Ckappa=q'G%5E%7B-1%7Dq%3C1."></p>
<p>Indeed, <img src="https://latex.codecogs.com/png.latex?v_0'v_0=%5Ckappa"> and <img src="https://latex.codecogs.com/png.latex?v'v=%5Ckappa+z'z">, so the remaining squared row radius is <img src="https://latex.codecogs.com/png.latex?1-%5Ckappa">.</p>
<p>Writing <img src="https://latex.codecogs.com/png.latex?r_0=%5Csqrt%7B1-%5Ckappa%7D">, <img src="https://latex.codecogs.com/png.latex?d=i-1-%7CZ%7C">, <img src="https://latex.codecogs.com/png.latex?t=y'y">, and</p>
<p><img src="https://latex.codecogs.com/png.latex?u(y)=%5Cfrac%7B%5Csqrt%7Bt+2%7D%7D%7Bt+1%7Dy,"></p>
<p>the affine row transform is</p>
<p><img src="https://latex.codecogs.com/png.latex?v=v_0+r_0Qu(y),%5Cqquad%20L_%7Bii%7D=%5Cfrac%7Br_0%7D%7Bt+1%7D."></p>
<p>It enforces <img src="https://latex.codecogs.com/png.latex?Av=q"> and satisfies <img src="https://latex.codecogs.com/png.latex?v'v+L_%7Bii%7D%5E2=1">. Because <img src="https://latex.codecogs.com/png.latex?u(y)"> is a bijection from <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%20R%5Ed"> to the open unit ball, this transform covers the current-row completions whenever <img src="https://latex.codecogs.com/png.latex?%5Ckappa%3C1">. With <img src="https://latex.codecogs.com/png.latex?B_F=P_FB">, its row log Jacobian with respect to the remaining ordinary correlations <img src="https://latex.codecogs.com/png.latex?c_F"> is</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Clog%20J_i%0A=%5Clog%20J_%7B%5Ctext%7Bstereo%7D%7D(y)+d%5Clog%20r_0%0A+%5Clog%5Cleft%7C%5Cdet(B_FQ)%5Cright%7C,"></p>
<p>where</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Clog%20J_%7B%5Ctext%7Bstereo%7D%7D(y)%0A=%5Clog%202+%5Cfrac%7Bd-2%7D%7B2%7D%5Clog(t+2)-(d+1)%5Clog(1+t)."></p>
<p>The remaining determinant follows by forming the orthogonal matrix</p>
<p><img src="https://latex.codecogs.com/png.latex?O=%5Cleft%5BQ,%5C%20A'G%5E%7B-1/2%7D%5Cright%5D."></p>
<p>After permuting the rows into <img src="https://latex.codecogs.com/png.latex?F,Z"> order, <img src="https://latex.codecogs.com/png.latex?BO"> is block upper triangular with diagonal blocks <img src="https://latex.codecogs.com/png.latex?B_FQ"> and <img src="https://latex.codecogs.com/png.latex?G%5E%7B1/2%7D">. Hence</p>
<p><img src="https://latex.codecogs.com/png.latex?%7C%5Cdet(B_FQ)%7C=%5Cfrac%7B%5Cdet(B)%7D%7B%5Csqrt%7B%5Cdet(G)%7D%7D."></p>
<p>The <img src="https://latex.codecogs.com/png.latex?d%5Clog%20r_0"> term comes from scaling <img src="https://latex.codecogs.com/png.latex?d"> free coordinates by <img src="https://latex.codecogs.com/png.latex?r_0">. It vanishes in the zero case because then <img src="https://latex.codecogs.com/png.latex?v_0=0"> and <img src="https://latex.codecogs.com/png.latex?r_0=1">. If <img src="https://latex.codecogs.com/png.latex?d=0">, the row has no free parameter and its empty-coordinate Jacobian is one.</p>
<p>This gives a transform for the <em>current</em> row whenever <img src="https://latex.codecogs.com/png.latex?%5Ckappa%3C1">, but not a global completion guarantee. At <img src="https://latex.codecogs.com/png.latex?K=3">, fix</p>
<p><img src="https://latex.codecogs.com/png.latex?%5COmega_%7B31%7D=0.8,%5Cqquad%20%5COmega_%7B32%7D=-0.8,"></p>
<p>and call the earlier free correlation <img src="https://latex.codecogs.com/png.latex?r=%5COmega_%7B21%7D">. Positive definiteness requires</p>
<p><img src="https://latex.codecogs.com/png.latex?1+2r(0.8)(-0.8)-r%5E2-0.8%5E2-(-0.8)%5E2%3E0,"></p>
<p>which reduces to <img src="https://latex.codecogs.com/png.latex?-1%3Cr%3C-0.28">. An earlier draw such as <img src="https://latex.codecogs.com/png.latex?r=0.5"> is a valid <img src="https://latex.codecogs.com/png.latex?2%5Ctimes2"> correlation matrix and would be allowed by a broad cell bound such as <img src="https://latex.codecogs.com/png.latex?(-1,1)">, but it leaves no possible third row. This prefix failure does not occur with zero equalities alone because <img src="https://latex.codecogs.com/png.latex?v=0"> is always a feasible row interior. A nonzero affine row retains a positive radius under additional structure—for example, if <img src="https://latex.codecogs.com/png.latex?G"> is fixed and the specified block</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Bpmatrix%7DG&amp;q%5C%5Cq'&amp;1%5Cend%7Bpmatrix%7D"></p>
<p>is positive definite, then its Schur complement gives <img src="https://latex.codecogs.com/png.latex?q'G%5E%7B-1%7Dq%3C1">.</p>
<p>The global construction avoids these prefix dead ends. Let <img src="https://latex.codecogs.com/png.latex?H"> denote all fixed cells, <img src="https://latex.codecogs.com/png.latex?F"> the <img src="https://latex.codecogs.com/png.latex?D_F"> remaining cells, and choose an anchor <img src="https://latex.codecogs.com/png.latex?%5COmega_0"> that is strictly positive definite, satisfies <img src="https://latex.codecogs.com/png.latex?%5COmega_%7B0,H%7D=q_H"> exactly, and lies strictly inside every free-cell bound. Draw directions only in the free coordinates and embed them as a symmetric matrix <img src="https://latex.codecogs.com/png.latex?U"> with</p>
<p><img src="https://latex.codecogs.com/png.latex?U_H=0."></p>
<p>The box and positive-definite ray limits from Section&nbsp;4 are otherwise unchanged. Consequently,</p>
<p><img src="https://latex.codecogs.com/png.latex?%5COmega=%5COmega_0+%5Crho%20R(U)U"></p>
<p>holds every specified correlation fixed for every raw input. Because the intersection of the positive-definite cone, the free-cell box, and the affine fixed-value space is convex, the same unique-ray argument shows that the reduced map covers its relative interior in the free-coordinate affine subspace, apart from the anchor. Its polar Jacobian is</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Crho%5E%7BD_F-1%7DR(U)%5E%7BD_F%7D"></p>
<p>with respect to the <img src="https://latex.codecogs.com/png.latex?D_F"> free ordinary correlations. There is no additional triangular Gram or delta term: the fixed coordinates have already been removed from the base measure.</p>
<p>The supplied global-ray Stan program implements this reduction. A fixed cell is encoded by setting its lower and upper bounds equal and including it in <code>N_fixed</code>; the anchor must match the value exactly. Prefer constructing or optimizing the anchor subject to those equalities. If fixed values are copied into an existing candidate, recheck positive definiteness and the strict margins for every free bound. The normalized-direction implementation requires at least two remaining free cells. With one free correlation, use a scalar logistic over its exact feasible interval; with none, the matrix is fixed data and only needs validation.</p>
<section id="numerical-check-against-iid-rejection" class="level3">
<h3 class="anchored" data-anchor-id="numerical-check-against-iid-rejection">5.1 Numerical Check Against IID Rejection</h3>
<p>As a check, I fixed <img src="https://latex.codecogs.com/png.latex?%5COmega_%7B31%7D=0.55"> and <img src="https://latex.codecogs.com/png.latex?%5COmega_%7B42%7D=-0.35"> in a <img src="https://latex.codecogs.com/png.latex?K=4">, <img src="https://latex.codecogs.com/png.latex?%5Ceta=2"> matrix and gave the other four correlations heterogeneous finite bounds. Thirteen of the box’s sixteen corners are not positive definite, and the positive-definite boundary supplied the active ray limit for 18.4% of the retained draws.</p>
<p>For an independent reference, I drew 500,000 points uniformly from the four-dimensional free box. Because <img src="https://latex.codecogs.com/png.latex?%5Ceta=2">, the target kernel is <img src="https://latex.codecogs.com/png.latex?%5Cdet(%5COmega)">; Hadamard’s inequality gives <img src="https://latex.codecogs.com/png.latex?0%3C%5Cdet(%5COmega)%5Cle1"> for a positive-definite correlation matrix. Retaining positive-definite proposals with probability <img src="https://latex.codecogs.com/png.latex?%5Cdet(%5COmega)"> therefore produced 108,553 IID draws from the same LKJ slice. The Stan fit retained 6,000 draws. No fixed-cell or bound violation was recorded, the smallest sampled eigenvalue was 0.010, and there were 0 divergences.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">fixed_comparison <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transmute</span>(</span>
<span id="cb2-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Correlation =</span> variable,</span>
<span id="cb2-4">    <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;">Stan mean</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(stan_mean, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb2-5">    <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;">IID mean</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(reference_mean, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb2-6">    <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;">Mean difference</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(mean_difference, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb2-7">    <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;">Largest quantile difference</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(max_quantile_difference, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb2-8">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-9">  knitr<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;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <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;">"l"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)))</span></code></pre></div></div>
</details>
<div id="tbl-fixed-values-validation" class="cell quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-fixed-values-validation-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: Reduced global-ray samples compared with an IID rejection reference for two exact nonzero correlations.
</figcaption>
<div aria-describedby="tbl-fixed-values-validation-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="cell caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 16%">
<col style="width: 13%">
<col style="width: 12%">
<col style="width: 21%">
<col style="width: 37%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Correlation</th>
<th style="text-align: right;">Stan mean</th>
<th style="text-align: right;">IID mean</th>
<th style="text-align: right;">Mean difference</th>
<th style="text-align: right;">Largest quantile difference</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Omega[2,1]</td>
<td style="text-align: right;">-0.077</td>
<td style="text-align: right;">-0.068</td>
<td style="text-align: right;">-0.009</td>
<td style="text-align: right;">0.011</td>
</tr>
<tr class="even">
<td style="text-align: left;">Omega[3,2]</td>
<td style="text-align: right;">-0.007</td>
<td style="text-align: right;">-0.002</td>
<td style="text-align: right;">-0.005</td>
<td style="text-align: right;">0.011</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Omega[4,1]</td>
<td style="text-align: right;">0.060</td>
<td style="text-align: right;">0.051</td>
<td style="text-align: right;">0.009</td>
<td style="text-align: right;">0.013</td>
</tr>
<tr class="even">
<td style="text-align: left;">Omega[4,3]</td>
<td style="text-align: right;">0.086</td>
<td style="text-align: right;">0.075</td>
<td style="text-align: right;">0.012</td>
<td style="text-align: right;">0.019</td>
</tr>
</tbody>
</table>
</div>
</div>
</figure>
</div>
</div>
<p>The largest absolute mean difference was 0.012, or 2.09 combined Monte Carlo standard errors. The largest difference among the 5th, 50th, and 95th percentiles was 0.019. This agreement is a numerical check of the reduced-coordinate implementation, including its Jacobian; it is not a proof. It also does not compare the sampling efficiency of the ray and triangular geometries.</p>
</section>
</section>
<section id="a-direct-logistic-transform-for-certified-boxes" class="level2">
<h2 class="anchored" data-anchor-id="a-direct-logistic-transform-for-certified-boxes">6. A Direct Logistic Transform for Certified Boxes</h2>
<p>For one heterogeneous example, I constructed a box certified to lie wholly inside the elliptope. Let <img src="https://latex.codecogs.com/png.latex?%5COmega_0"> be positive definite and write any matrix in the box as <img src="https://latex.codecogs.com/png.latex?%5COmega_0+E">. Define</p>
<p><img src="https://latex.codecogs.com/png.latex?m_%7Bij%7D=%5Cmax%5C%7B%7C%5Cell_%7Bij%7D-%5COmega_%7B0,ij%7D%7C,%0A%7Cu_%7Bij%7D-%5COmega_%7B0,ij%7D%7C%5C%7D."></p>
<p>If</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cmax_i%5Csum_%7Bj%5Cne%20i%7Dm_%7Bij%7D%3C%5Clambda_%7B%5Cmin%7D(%5COmega_0),"></p>
<p>then</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Clambda_%7B%5Cmin%7D(%5COmega_0+E)%0A%5Cge%20%5Clambda_%7B%5Cmin%7D(%5COmega_0)-%5ClVert%20E%5CrVert_2%0A%5Cge%20%5Clambda_%7B%5Cmin%7D(%5COmega_0)-%5ClVert%20E%5CrVert_%5Cinfty%3E0."></p>
<p>The second inequality uses symmetry: <img src="https://latex.codecogs.com/png.latex?%5ClVert%20E%5CrVert_2%5Cle%0A%5Csqrt%7B%5ClVert%20E%5CrVert_1%5ClVert%20E%5CrVert_%5Cinfty%7D%0A=%5ClVert%20E%5CrVert_%5Cinfty">, and <img src="https://latex.codecogs.com/png.latex?%5ClVert%20E%5CrVert_%5Cinfty%5Cle%5Cmax_i%5Csum_%7Bj%5Cne%20i%7Dm_%7Bij%7D">.</p>
<p>Every point in that box is therefore positive definite, and independent lower/upper logistic transforms are sufficient. This certificate is conservative: it covers only a subset of feasible boxes, but applies when bounds are generated around a known matrix or are scientifically narrow.</p>
<p>For arbitrary user-supplied boxes, finding a strict anchor can be written as the semidefinite program</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbegin%7Baligned%7D%0A%5Coperatorname*%7Bmaximize%7D_%7B%5COmega_0,%5Cdelta%7D%5Cquad%20&amp;%5Cdelta%5C%5C%0A%5Ctext%7Bsubject%20to%7D%5Cquad%0A&amp;%5COmega_0%5Csucceq%5Cdelta%20I,%5Cqquad%20%5Coperatorname%7Bdiag%7D(%5COmega_0)=%5Cmathbf%201,%5C%5C%0A&amp;%5Cell_%7Bij%7D+%5Cdelta%5Cle%5COmega_%7B0,ij%7D%5Cle%20u_%7Bij%7D-%5Cdelta.%0A%5Cend%7Baligned%7D"></p>
<p>A solution with <img src="https://latex.codecogs.com/png.latex?%5Cdelta%3E0"> supplies the required anchor. Conversely, any strict anchor makes some positive <img src="https://latex.codecogs.com/png.latex?%5Cdelta"> feasible, so for the exact program, <img src="https://latex.codecogs.com/png.latex?%5Cdelta%5E%5Cstar%3E0"> is equivalent to the existence of an anchor with both positive spectral margin and strict free-bound margins. A numerical optimum near zero still has to be interpreted using the solver’s feasibility residuals and tolerances. Endpoints lying in <img src="https://latex.codecogs.com/png.latex?%5B-1,1%5D"> alone do not establish joint feasibility. For fixed cells, replace the corresponding two inequalities by equalities and omit those cells from the bound-margin constraint.</p>
</section>
<section id="sampling-experiments" class="level2">
<h2 class="anchored" data-anchor-id="sampling-experiments">7. Sampling Experiments</h2>
<p>I tested five <img src="https://latex.codecogs.com/png.latex?K=5">, <img src="https://latex.codecogs.com/png.latex?%5Ceta=2"> prior-only cases in Stan <span class="citation" data-cites="carpenter2017">(Carpenter et al. 2017)</span>:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">scenario</th>
<th style="text-align: left;">constraints</th>
<th style="text-align: left;">transforms compared</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">all positive</td>
<td style="text-align: left;">every <img src="https://latex.codecogs.com/png.latex?%5COmega_%7Bij%7D%5Cin(0,1)"></td>
<td style="text-align: left;">triangular sign, global ray</td>
</tr>
<tr class="even">
<td style="text-align: left;">all negative</td>
<td style="text-align: left;">every <img src="https://latex.codecogs.com/png.latex?%5COmega_%7Bij%7D%5Cin(-1,0)"></td>
<td style="text-align: left;">triangular sign, global ray</td>
</tr>
<tr class="odd">
<td style="text-align: left;">mixed signs</td>
<td style="text-align: left;">random positive/negative sign per cell</td>
<td style="text-align: left;">triangular sign, global ray</td>
</tr>
<tr class="even">
<td style="text-align: left;">certified heterogeneous box</td>
<td style="text-align: left;">random asymmetric intervals wholly inside the elliptope</td>
<td style="text-align: left;">certified direct box, global ray</td>
</tr>
<tr class="odd">
<td style="text-align: left;">wide heterogeneous box</td>
<td style="text-align: left;">random asymmetric intervals with non-PD corners</td>
<td style="text-align: left;">global ray</td>
</tr>
</tbody>
</table>
<p>Each fit used four chains, 750 warmup iterations, 1,500 retained iterations, <code>adapt_delta = 0.90</code>, and <code>max_treedepth = 12</code>. The certified heterogeneous widths were scaled until the spectral bound in Section 6 gave a minimum eigenvalue of at least 0.22 throughout the box. The wide box has a strict feasible anchor but a minimum corner eigenvalue of -1.10; independent logits over its cells would therefore map some raw states to non-positive-definite matrices. Wall-clock times below describe this run and are not intended as portable hardware comparisons.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../R/theme_blog.R"</span>)</span>
<span id="cb3-6"></span>
<span id="cb3-7">diagnostics <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;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bounds-diagnostics.csv"</span>)</span>
<span id="cb3-8">cells <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;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bounds-cell-summary.csv"</span>)</span>
<span id="cb3-9">selected <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;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bounds-selected-draws.csv"</span>)</span>
<span id="cb3-10">bounds <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;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bounds-scenarios.csv"</span>)</span>
<span id="cb3-11">run_settings <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;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bounds-run-settings.csv"</span>)</span>
<span id="cb3-12"></span>
<span id="cb3-13">scenario_labels <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;">c</span>(</span>
<span id="cb3-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all_positive =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"All positive"</span>,</span>
<span id="cb3-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all_negative =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"All negative"</span>,</span>
<span id="cb3-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mixed_signs =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mixed signs"</span>,</span>
<span id="cb3-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heterogeneous_box =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Certified heterogeneous box"</span>,</span>
<span id="cb3-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">wide_heterogeneous_box =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wide heterogeneous box"</span></span>
<span id="cb3-19">)</span>
<span id="cb3-20">model_labels <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;">c</span>(</span>
<span id="cb3-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tri_sign =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Triangular sign"</span>,</span>
<span id="cb3-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">global_ray =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Global ray"</span>,</span>
<span id="cb3-23">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">direct_box =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Certified direct box"</span></span>
<span id="cb3-24">)</span>
<span id="cb3-25"></span>
<span id="cb3-26">diagnostics <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> diagnostics <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario_label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(scenario_labels[scenario],</span>
<span id="cb3-29">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(scenario_labels)),</span>
<span id="cb3-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model_label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(model_labels[model],</span>
<span id="cb3-31">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(model_labels))</span>
<span id="cb3-32">  )</span>
<span id="cb3-33">cells <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cells <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario_label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(scenario_labels[scenario],</span>
<span id="cb3-36">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(scenario_labels)),</span>
<span id="cb3-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model_label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(model_labels[model],</span>
<span id="cb3-38">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(model_labels))</span>
<span id="cb3-39">  )</span>
<span id="cb3-40">selected <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> selected <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-42">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario_label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(scenario_labels[scenario],</span>
<span id="cb3-43">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(scenario_labels)),</span>
<span id="cb3-44">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model_label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(model_labels[model],</span>
<span id="cb3-45">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(model_labels))</span>
<span id="cb3-46">  )</span>
<span id="cb3-47"></span>
<span id="cb3-48">reference_cells <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cells <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(model <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"global_ray"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-50">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(scenario, scenario_label, variable,</span>
<span id="cb3-51">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reference_model =</span> model_label,</span>
<span id="cb3-52">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reference_mean =</span> mean,</span>
<span id="cb3-53">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reference_mcse =</span> mcse_mean,</span>
<span id="cb3-54">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reference_q05 =</span> q05,</span>
<span id="cb3-55">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reference_q50 =</span> q50,</span>
<span id="cb3-56">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reference_q95 =</span> q95)</span>
<span id="cb3-57"></span>
<span id="cb3-58">parity <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cells <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-59">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(model <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"global_ray"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-60">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(scenario, variable,</span>
<span id="cb3-61">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ray_mean =</span> mean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ray_mcse =</span> mcse_mean,</span>
<span id="cb3-62">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ray_q05 =</span> q05, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ray_q50 =</span> q50, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ray_q95 =</span> q95) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inner_join</span>(reference_cells, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <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;">"scenario"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"variable"</span>))</span>
<span id="cb3-64"></span>
<span id="cb3-65">max_mean_difference <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;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(parity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>reference_mean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> parity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ray_mean))</span>
<span id="cb3-66">max_standardized_difference <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;">max</span>(</span>
<span id="cb3-67">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(parity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>reference_mean <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> parity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ray_mean)</span>
<span id="cb3-68">  <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>(parity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>reference_mcse<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> parity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ray_mcse<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="cb3-69">)</span>
<span id="cb3-70">max_quantile_difference <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;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-71">  parity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>reference_q05 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> parity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ray_q05,</span>
<span id="cb3-72">  parity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>reference_q50 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> parity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ray_q50,</span>
<span id="cb3-73">  parity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>reference_q95 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> parity<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>ray_q95</span>
<span id="cb3-74">)))</span></code></pre></div></div>
</details>
</div>
<section id="the-bounds-used" class="level3">
<h3 class="anchored" data-anchor-id="the-bounds-used">7.1 The Bounds Used</h3>
<p>The first three boxes impose signs only. The two heterogeneous boxes contain different lower and upper endpoints for all ten cells, including intervals that do not contain zero.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">bounds <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb4-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario_label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(scenario_labels[scenario],</span>
<span id="cb4-4">                            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(scenario_labels)),</span>
<span id="cb4-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cell =</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;">"(%d,%d)"</span>, row, col)</span>
<span id="cb4-6">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> cell)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_linerange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmin =</span> lower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> upper),</span>
<span id="cb4-9">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> anchor), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>red, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</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="cb4-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>scenario_label, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_x"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</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="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Correlation"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cell"</span>,</span>
<span id="cb4-14">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Elementwise Bounds"</span>,</span>
<span id="cb4-15">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Red points are strict feasible anchors"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-scenario-bounds" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-scenario-bounds-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-12-corr-bounds/corr-bounds_files/figure-html/fig-scenario-bounds-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-scenario-bounds-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Bounds and strict feasible anchors used in the five experiments. The certified heterogeneous intervals lie wholly inside the elliptope; the wide heterogeneous box has non-PD corners.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="a-numerical-cross-check" class="level3">
<h3 class="anchored" data-anchor-id="a-numerical-cross-check">7.2 A Numerical Cross-Check</h3>
<p>The derivations imply that the triangular sign map and global ray target the same bounded LKJ distribution in the first three scenarios. They likewise imply that the direct logistic and global ray have the same target for the certified heterogeneous box. The simulations provide a separate numerical check: across the 40 matched cell comparisons, the largest sample-mean difference is 0.010.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(parity, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> reference_mean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ray_mean)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slope =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">intercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb5-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>gold, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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;">+</span></span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> ray_mean <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> ray_mcse,</span>
<span id="cb5-5">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> ray_mean <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> ray_mcse),</span>
<span id="cb5-6">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <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;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blue, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmin =</span> reference_mean <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> reference_mcse,</span>
<span id="cb5-8">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> reference_mean <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> reference_mcse),</span>
<span id="cb5-9">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">orientation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb5-10">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blue, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</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="cb5-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>scenario_label) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Triangular sign or certified-box mean"</span>,</span>
<span id="cb5-16">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Global-ray mean"</span>,</span>
<span id="cb5-17">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cellwise Mean Comparison"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-mean-parity" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-mean-parity-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-12-corr-bounds/corr-bounds_files/figure-html/fig-mean-parity-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-mean-parity-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Sample means from independent parameterizations of the same bounded LKJ targets. Error bars are two Monte Carlo standard errors; the diagonal is the equality line.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The largest difference is 2.50 combined Monte Carlo standard errors. Across the 5th, 50th, and 95th percentiles, the largest absolute quantile difference is 0.020. These are marginal implementation checks; they do not test the complete joint distribution or prove either Jacobian.</p>
<p>The next figure compares selected-cell marginals.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">ggplot</span>(selected, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> model_label, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> model_label)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>scenario_label, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</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="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb6-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Triangular sign"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal,</span>
<span id="cb6-6">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Global ray"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple,</span>
<span id="cb6-7">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Certified direct box"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blue</span>
<span id="cb6-8">  )) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb6-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Triangular sign"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal,</span>
<span id="cb6-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Global ray"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple,</span>
<span id="cb6-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Certified direct box"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blue</span>
<span id="cb6-13">  )) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Correlation in cell (2, 1)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>,</span>
<span id="cb6-17">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb6-18">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bounded LKJ Marginals"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-selected-marginals" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-selected-marginals-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-12-corr-bounds/corr-bounds_files/figure-html/fig-selected-marginals-1.png" class="img-fluid figure-img" width="960">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-selected-marginals-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Marginal draws for the first lower-triangular cell, Omega[2,1], under each pair of transforms.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="feasibility-and-hmc-performance-in-these-runs" class="level3">
<h3 class="anchored" data-anchor-id="feasibility-and-hmc-performance-in-these-runs">7.3 Feasibility and HMC Performance in These Runs</h3>
<p>Across the 54,000 retained matrices, the checks recorded no bound violations. The smallest sampled eigenvalue was positive in every fit, and there were no maximum-treedepth hits. Every sampled raw coordinate, including <code>direction_raw</code> and <code>radius_raw</code>, and every reported off-diagonal correlation had <img src="https://latex.codecogs.com/png.latex?%5Cwidehat%20R%3C1.004">. The triangular sign map had no divergences in these runs. The global ray had one divergence in the mixed-sign case. A single divergent transition does not identify its cause; the piecewise boundary is one possible mechanism.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">diagnostics <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;">transmute</span>(</span>
<span id="cb7-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Scenario =</span> scenario_label,</span>
<span id="cb7-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Transform =</span> model_label,</span>
<span id="cb7-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Divergences =</span> divergences,</span>
<span id="cb7-6">    <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;">Min bulk ESS</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(min_bulk_ess, min_parameter_bulk_ess)),</span>
<span id="cb7-7">    <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;">Min corr ESS / 1k leapfrog steps</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(ess_per_1000_gradients, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb7-8">    <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;">Min E-BFMI</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(min_ebfmi, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb7-9">    <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;">Max R-hat</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(max_rhat, max_parameter_rhat), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb7-10">    <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;">PD boundary (%)</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(</span>
<span id="cb7-11">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(pd_boundary_fraction),</span>
<span id="cb7-12">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"--"</span>,</span>
<span id="cb7-13">      <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;">"%.1f"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pd_boundary_fraction)</span>
<span id="cb7-14">    ),</span>
<span id="cb7-15">    <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;">Time (s)</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(seconds, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb7-16">    <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;">Min eigenvalue</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">signif</span>(min_eigenvalue, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb7-17">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-18">  knitr<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;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <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;">"l"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"l"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)))</span></code></pre></div></div>
</details>
<div id="tbl-diagnostics" class="cell quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl quarto-uncaptioned" id="tbl-diagnostics-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;2
</figcaption>
<div aria-describedby="tbl-diagnostics-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 16%">
<col style="width: 12%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 19%">
<col style="width: 6%">
<col style="width: 5%">
<col style="width: 9%">
<col style="width: 5%">
<col style="width: 8%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Scenario</th>
<th style="text-align: left;">Transform</th>
<th style="text-align: right;">Divergences</th>
<th style="text-align: right;">Min bulk ESS</th>
<th style="text-align: right;">Min corr ESS / 1k leapfrog steps</th>
<th style="text-align: right;">Min E-BFMI</th>
<th style="text-align: right;">Max R-hat</th>
<th style="text-align: right;">PD boundary (%)</th>
<th style="text-align: right;">Time (s)</th>
<th style="text-align: right;">Min eigenvalue</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">All positive</td>
<td style="text-align: left;">Triangular sign</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3877</td>
<td style="text-align: right;">59.7</td>
<td style="text-align: right;">0.87</td>
<td style="text-align: right;">1.002</td>
<td style="text-align: right;">–</td>
<td style="text-align: right;">0.27</td>
<td style="text-align: right;">0.002950</td>
</tr>
<tr class="even">
<td style="text-align: left;">All positive</td>
<td style="text-align: left;">Global ray</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3049</td>
<td style="text-align: right;">5.7</td>
<td style="text-align: right;">0.78</td>
<td style="text-align: right;">1.001</td>
<td style="text-align: right;">59.4</td>
<td style="text-align: right;">1.19</td>
<td style="text-align: right;">0.000582</td>
</tr>
<tr class="odd">
<td style="text-align: left;">All negative</td>
<td style="text-align: left;">Triangular sign</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">5492</td>
<td style="text-align: right;">122.7</td>
<td style="text-align: right;">0.81</td>
<td style="text-align: right;">1.002</td>
<td style="text-align: right;">–</td>
<td style="text-align: right;">0.15</td>
<td style="text-align: right;">0.000375</td>
</tr>
<tr class="even">
<td style="text-align: left;">All negative</td>
<td style="text-align: left;">Global ray</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1877</td>
<td style="text-align: right;">1.3</td>
<td style="text-align: right;">0.78</td>
<td style="text-align: right;">1.003</td>
<td style="text-align: right;">42.9</td>
<td style="text-align: right;">3.17</td>
<td style="text-align: right;">0.001600</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Mixed signs</td>
<td style="text-align: left;">Triangular sign</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">4585</td>
<td style="text-align: right;">103.2</td>
<td style="text-align: right;">0.91</td>
<td style="text-align: right;">1.001</td>
<td style="text-align: right;">–</td>
<td style="text-align: right;">0.14</td>
<td style="text-align: right;">0.003020</td>
</tr>
<tr class="even">
<td style="text-align: left;">Mixed signs</td>
<td style="text-align: left;">Global ray</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1900</td>
<td style="text-align: right;">1.3</td>
<td style="text-align: right;">0.71</td>
<td style="text-align: right;">1.003</td>
<td style="text-align: right;">66.2</td>
<td style="text-align: right;">2.65</td>
<td style="text-align: right;">0.002780</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Certified heterogeneous box</td>
<td style="text-align: left;">Certified direct box</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">8040</td>
<td style="text-align: right;">187.0</td>
<td style="text-align: right;">0.83</td>
<td style="text-align: right;">1.004</td>
<td style="text-align: right;">–</td>
<td style="text-align: right;">0.15</td>
<td style="text-align: right;">0.417000</td>
</tr>
<tr class="even">
<td style="text-align: left;">Certified heterogeneous box</td>
<td style="text-align: left;">Global ray</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">2505</td>
<td style="text-align: right;">3.4</td>
<td style="text-align: right;">0.71</td>
<td style="text-align: right;">1.001</td>
<td style="text-align: right;">0.0</td>
<td style="text-align: right;">1.62</td>
<td style="text-align: right;">0.421000</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Wide heterogeneous box</td>
<td style="text-align: left;">Global ray</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3299</td>
<td style="text-align: right;">7.1</td>
<td style="text-align: right;">0.82</td>
<td style="text-align: right;">1.001</td>
<td style="text-align: right;">4.5</td>
<td style="text-align: right;">1.08</td>
<td style="text-align: right;">0.003540</td>
</tr>
</tbody>
</table>
</div>
</div>
</figure>
</div>
</div>
<p>The global construction was less efficient in these runs. It needs an eigendecomposition at every log-density evaluation, carries one auxiliary radial parameter, and uses the piecewise ray limit. Its minimum bulk ESS over the ten reported correlation cells per 1,000 recorded leapfrog steps was between 1.3 and 7.1, versus 59.7–122.7 for the triangular sign map and 187.0 for the certified direct box. The positive-definite boundary was active for 4.5% of the wide-box draws and was not active in the retained draws from the spectrally certified box.</p>
</section>
</section>
<section id="what-to-use" class="level2">
<h2 class="anchored" data-anchor-id="what-to-use">8. What to Use</h2>
<p>For an all-positive, all-negative, or prespecified mixed-sign pattern, I would start with the <strong>triangular sign transform</strong>. The formulas and row-wise inverse in Section 3 give a smooth bijection onto the interior of the signed elliptope. In exact arithmetic, every finite unconstrained state therefore produces a positive-definite correlation matrix with the requested signs, and every matrix in that constrained interior is represented. The calculation is triangular, with no eigendecomposition, auxiliary radial variable, or minimum over competing boundary distances.</p>
<p>The numerical comparison is consistent with that preference. In the three <img src="https://latex.codecogs.com/png.latex?K=5"> sign experiments, the triangular map produced no divergences and achieved 59.7–122.7 minimum bulk ESS over the ten correlation cells per 1,000 recorded leapfrog steps. The global ray achieved 1.3–5.7 on the same targets and had one divergence in the mixed-sign case. These runs do not establish a general performance ordering. Coverage instead follows from the inverse maps in Sections 3 and 4: both maps represent the same sign-constrained interiors. The triangular construction is therefore my default for sign-only models.</p>
<p>An exact fixed value specifies a magnitude, not merely a sign. The affine triangular transform handles an individual row whenever the Schur-complement condition in Section 5 holds; if the predecessor block is fixed, that condition can be checked in advance. For a general pattern, however, the sequential construction can encounter internal prefix boundaries. Given a strict anchor, the reduced global ray avoids those prefix failures because it holds the fixed cells constant while exploring the feasible slice.</p>
<p>The other constructions handle positive-width finite intervals that the sign transform does not express. A cell-specific cap such as <img src="https://latex.codecogs.com/png.latex?0%3C%5Crho_%7Bij%7D%3Cu_%7Bij%7D%3C1">, or a finite interval <img src="https://latex.codecogs.com/png.latex?%5Cell_%7Bij%7D%3C%5Crho_%7Bij%7D%3Cu_%7Bij%7D">, is more than a sign constraint. If the entire resulting box can be certified positive definite, independent logits give the simpler smooth parameterization. Otherwise, if a strict anchor is available, the global ray covers the interior of the box–elliptope intersection at the cost of piecewise geometry and an eigendecomposition at every density evaluation.</p>
<p>One final qualification concerns <img src="https://latex.codecogs.com/png.latex?%5Ceta">. Everything here treats the LKJ shape as fixed. For a bounded region or affine equality slice <img src="https://latex.codecogs.com/png.latex?%5Cmathcal%20A">, the relevant normalizing integral is</p>
<p><img src="https://latex.codecogs.com/png.latex?Z_%7B%5Cmathcal%20A%7D(%5Ceta)%0A=%5Cint_%7B%5Cmathcal%20A%7D%5Cdet(%5COmega)%5E%7B%5Ceta-1%7D%5C,d%5Cmu_%7B%5Cmathcal%20A%7D(%5COmega),"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cmu_%7B%5Cmathcal%20A%7D"> is Lebesgue measure on the remaining free correlations. If <img src="https://latex.codecogs.com/png.latex?%5Ceta"> is inferred, <img src="https://latex.codecogs.com/png.latex?Z_%7B%5Cmathcal%20A%7D(%5Ceta)"> generally depends on <img src="https://latex.codecogs.com/png.latex?%5Ceta"> and cannot be dropped unless it is shown to be constant for the particular slice. The determinant kernel and change-of-variables Jacobian alone are sufficient here because <img src="https://latex.codecogs.com/png.latex?%5Ceta"> is fixed.</p>
</section>
<section id="reproducibility" class="level2">
<h2 class="anchored" data-anchor-id="reproducibility">Reproducibility</h2>
<p>The main benchmark is in <a href="benchmark_bounds.R"><code>benchmark_bounds.R</code></a>; the exact-value rejection check is in <a href="benchmark_fixed_values.R"><code>benchmark_fixed_values.R</code></a>. The downloadable <a href="tri-sign-corr-bounds.stan"><code>tri-sign</code></a>, <a href="global-ray-corr-bounds.stan"><code>global-ray</code></a>, and <a href="direct-certified-box-corr.stan"><code>certified-box</code></a> Stan programs are in the same published directory. The global-ray program accepts both positive-width intervals and exact cells; the other two programs retain their narrower interfaces. Each script checks its applicable bounds or fixed values and sampled eigenvalues, and records the sampler diagnostics reported above.</p>
<p>The published results are the main <a href="bounds-diagnostics.csv"><code>diagnostics</code></a>, <a href="bounds-cell-summary.csv"><code>cell summaries</code></a>, <a href="bounds-scenarios.csv"><code>scenario bounds</code></a>, and <a href="bounds-selected-draws.csv"><code>plotted draw subset</code></a>, together with the fixed-value <a href="fixed-values-diagnostics.csv"><code>diagnostics</code></a> and <a href="fixed-values-summary.csv"><code>comparison</code></a>. Run settings and source checksums are recorded in <a href="bounds-run-settings.csv"><code>bounds-run-settings.csv</code></a>, <a href="bounds-source-hashes.csv"><code>bounds-source-hashes.csv</code></a>, <a href="fixed-values-run-settings.csv"><code>fixed-values-run-settings.csv</code></a>, and <a href="fixed-values-source-hashes.csv"><code>fixed-values-source-hashes.csv</code></a>.</p>
</section>
<section id="appendix-stan-implementations" class="level2">
<h2 class="anchored" data-anchor-id="appendix-stan-implementations">Appendix: Stan Implementations</h2>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">Triangular sign transform</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">Global anchor ray</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">Certified direct box</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>tri-sign-corr-bounds.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" data-filename="tri-sign-corr-bounds.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/*</span></span>
<span id="cb8-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  Smooth triangular transform for prescribed correlation signs.</span></span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  Every off-diagonal cell is either positive, with bounds [0, 1], or</span></span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  negative, with bounds [-1, 0].  In Cholesky row i, let B be the leading</span></span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  Cholesky block and c the ordinary correlations with earlier variables.</span></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  The transform creates a vector x in the requested orthant, solves</span></span>
<span id="cb8-8"></span>
<span id="cb8-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      w = B^{-1} x,</span></span>
<span id="cb8-10"></span>
<span id="cb8-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  and applies the modified stereographic hemisphere map</span></span>
<span id="cb8-12"></span>
<span id="cb8-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      z = sqrt(w'w + 2) / (w'w + 1) * w,</span></span>
<span id="cb8-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">      L[i,i] = 1 / (w'w + 1).</span></span>
<span id="cb8-15"></span>
<span id="cb8-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  Because c = B z is a positive scalar multiple of x, every requested sign</span></span>
<span id="cb8-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  is exact and every raw value produces a positive-definite correlation</span></span>
<span id="cb8-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  matrix.  The B and B^{-1} determinants cancel in the raw-to-c Jacobian.</span></span>
<span id="cb8-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*/</span></span>
<span id="cb8-20"></span>
<span id="cb8-21"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb8-22">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tuple</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>) <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">corr_cholesky_sign_jacobian</span>(</span>
<span id="cb8-23">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K,</span>
<span id="cb8-24">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb8-25">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> corr_lower,</span>
<span id="cb8-26">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> corr_upper) {</span>
<span id="cb8-27">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, K, K);</span>
<span id="cb8-28">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_pos <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="cb8-29">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_det <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.0</span>;</span>
<span id="cb8-30">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> input_scale <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;">2.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> K;</span>
<span id="cb8-31"></span>
<span id="cb8-32">    L[<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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>;</span>
<span id="cb8-33"></span>
<span id="cb8-34">    <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="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>K) {</span>
<span id="cb8-35">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i <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="cb8-36">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[d] x;</span>
<span id="cb8-37">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[d, d] previous_cholesky <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L[<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>d, <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>d];</span>
<span id="cb8-38"></span>
<span id="cb8-39">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>d) {</span>
<span id="cb8-40">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> raw_ij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> raw[raw_pos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> j <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="cb8-41">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> magnitude <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1p_exp</span>(raw_ij) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> input_scale;</span>
<span id="cb8-42">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> sign_ij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_lower[i, j] <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.0</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;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</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;">1.0</span>;</span>
<span id="cb8-43"></span>
<span id="cb8-44">        x[j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sign_ij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> magnitude;</span>
<span id="cb8-45">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log_inv_logit</span>(raw_ij) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(input_scale);</span>
<span id="cb8-46">      }</span>
<span id="cb8-47"></span>
<span id="cb8-48">      {</span>
<span id="cb8-49">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[d] w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mdivide_left_tri_low</span>(previous_cholesky, x);</span>
<span id="cb8-50">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_self</span>(w);</span>
<span id="cb8-51">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(t <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;">2.0</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> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>);</span>
<span id="cb8-52"></span>
<span id="cb8-53">        L[i, <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>d] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (scale <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> w)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb8-54">        L[i, i] <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.0</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> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>);</span>
<span id="cb8-55">        log_det <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;">2.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(L[i, i]);</span>
<span id="cb8-56"></span>
<span id="cb8-57">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Exact determinant of w -&gt; scale(t) * w.  The constant log(2)</span></span>
<span id="cb8-58">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// is retained even though it could be dropped from the target.</span></span>
<span id="cb8-59">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>)</span>
<span id="cb8-60">                    <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> (d <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;">2.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(t <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;">2.0</span>)</span>
<span id="cb8-61">                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (d <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1p</span>(t);</span>
<span id="cb8-62">      }</span>
<span id="cb8-63"></span>
<span id="cb8-64">      raw_pos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> d;</span>
<span id="cb8-65">    }</span>
<span id="cb8-66"></span>
<span id="cb8-67">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (raw_pos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(raw) <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="cb8-68">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sign transform consumed "</span>, raw_pos <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="cb8-69">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" raw values but received "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(raw), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb8-70">    }</span>
<span id="cb8-71"></span>
<span id="cb8-72">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (L, log_det);</span>
<span id="cb8-73">  }</span>
<span id="cb8-74">}</span>
<span id="cb8-75"></span>
<span id="cb8-76"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb8-77">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;</span>
<span id="cb8-78">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;</span>
<span id="cb8-79">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] corr_lower;</span>
<span id="cb8-80">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] corr_upper;</span>
<span id="cb8-81">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance;</span>
<span id="cb8-82">}</span>
<span id="cb8-83"></span>
<span id="cb8-84"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed data</span> {</span>
<span id="cb8-85">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(eta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>)) {</span>
<span id="cb8-86">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The LKJ shape eta must be strictly positive; received "</span>, eta,</span>
<span id="cb8-87">           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb8-88">  }</span>
<span id="cb8-89"></span>
<span id="cb8-90">  <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="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>K) {</span>
<span id="cb8-91">    <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;">abs</span>(corr_lower[i, i] <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance</span>
<span id="cb8-92">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(corr_upper[i, i] <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance) {</span>
<span id="cb8-93">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Both bound matrices must equal one on diagonal "</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb8-94">    }</span>
<span id="cb8-95"></span>
<span id="cb8-96">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb8-97">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb8-98">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_lower[i, j];</span>
<span id="cb8-99">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_upper[i, j];</span>
<span id="cb8-100">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> positive_sign <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lo <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.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> hi <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.0</span>;</span>
<span id="cb8-101">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> negative_sign <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</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;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> hi <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.0</span>;</span>
<span id="cb8-102"></span>
<span id="cb8-103">        <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;">abs</span>(lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> corr_lower[j, i]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance</span>
<span id="cb8-104">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> corr_upper[j, i]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance) {</span>
<span id="cb8-105">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bounds are not symmetric at pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")."</span>);</span>
<span id="cb8-106">        }</span>
<span id="cb8-107">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(positive_sign <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> negative_sign)) {</span>
<span id="cb8-108">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j,</span>
<span id="cb8-109">                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">") must request [0,1] or [-1,0]; received ["</span>,</span>
<span id="cb8-110">                 lo, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, hi, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]."</span>);</span>
<span id="cb8-111">        }</span>
<span id="cb8-112">      }</span>
<span id="cb8-113">    }</span>
<span id="cb8-114">  }</span>
<span id="cb8-115">}</span>
<span id="cb8-116"></span>
<span id="cb8-117"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb8-118">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)] raw;</span>
<span id="cb8-119">}</span>
<span id="cb8-120"></span>
<span id="cb8-121"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb8-122">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L_Omega;</span>
<span id="cb8-123">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">corr_matrix</span>[K] Omega;</span>
<span id="cb8-124">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_det_Omega;</span>
<span id="cb8-125"></span>
<span id="cb8-126">  {</span>
<span id="cb8-127">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tuple</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K], <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>) result <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;">corr_cholesky_sign_jacobian</span>(</span>
<span id="cb8-128">        K, raw, corr_lower, corr_upper);</span>
<span id="cb8-129">    L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> result<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>;</span>
<span id="cb8-130">    log_det_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> result<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>;</span>
<span id="cb8-131">    Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Omega);</span>
<span id="cb8-132">  }</span>
<span id="cb8-133">}</span>
<span id="cb8-134"></span>
<span id="cb8-135"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb8-136">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (eta <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> log_det_Omega;</span>
<span id="cb8-137">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>global-ray-corr-bounds.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" data-filename="global-ray-corr-bounds.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/*</span></span>
<span id="cb9-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  Globally feasible transform for arbitrary elementwise correlation bounds</span></span>
<span id="cb9-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  and exact fixed ordinary correlations.</span></span>
<span id="cb9-4"></span>
<span id="cb9-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  The feasible set is the intersection of the positive-definite correlation</span></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  cone and an ordinary-correlation box.  Given a strictly interior anchor,</span></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  every ray from that anchor remains feasible until it first reaches either a</span></span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  box face or the positive-definite boundary.  Cells whose lower and upper</span></span>
<span id="cb9-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  bounds are exactly equal are held fixed and omitted from the direction</span></span>
<span id="cb9-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  vector.  An auxiliary isotropic normal vector supplies a uniform direction</span></span>
<span id="cb9-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  on the sphere of the remaining free coordinates; its radius is independent</span></span>
<span id="cb9-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  of the resulting correlation matrix.</span></span>
<span id="cb9-13"></span>
<span id="cb9-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  The target is det(Omega)^(eta - 1) with respect to the remaining free</span></span>
<span id="cb9-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  ordinary off-diagonal correlations.</span></span>
<span id="cb9-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*/</span></span>
<span id="cb9-17"></span>
<span id="cb9-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb9-19">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tuple</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>) <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">corr_global_ray_jacobian</span>(</span>
<span id="cb9-20">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> direction_raw,</span>
<span id="cb9-21">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> radius_raw,</span>
<span id="cb9-22">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> corr_lower,</span>
<span id="cb9-23">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> corr_upper,</span>
<span id="cb9-24">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> anchor,</span>
<span id="cb9-25">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> anchor_cholesky) {</span>
<span id="cb9-26">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(anchor);</span>
<span id="cb9-27">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(direction_raw);</span>
<span id="cb9-28">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> direction_norm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <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;">dot_self</span>(direction_raw));</span>
<span id="cb9-29">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[D] direction;</span>
<span id="cb9-30">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] direction_matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, K, K);</span>
<span id="cb9-31">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> max_radius_box <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">positive_infinity</span>();</span>
<span id="cb9-32">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> max_radius_pd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">positive_infinity</span>();</span>
<span id="cb9-33">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> max_radius;</span>
<span id="cb9-34">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> radius_fraction <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_logit</span>(radius_raw);</span>
<span id="cb9-35">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> radius;</span>
<span id="cb9-36">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] Omega;</span>
<span id="cb9-37">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_det;</span>
<span id="cb9-38">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> pos <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="cb9-39"></span>
<span id="cb9-40">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(direction_norm <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>)) {</span>
<span id="cb9-41">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The auxiliary direction vector has zero norm."</span>);</span>
<span id="cb9-42">    }</span>
<span id="cb9-43">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(radius_fraction <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> radius_fraction <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>)) {</span>
<span id="cb9-44">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The radial inverse logit reached an endpoint; raw value="</span>,</span>
<span id="cb9-45">             radius_raw, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb9-46">    }</span>
<span id="cb9-47"></span>
<span id="cb9-48">    direction <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> direction_raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> direction_norm;</span>
<span id="cb9-49"></span>
<span id="cb9-50">    <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="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>K) {</span>
<span id="cb9-51">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb9-52">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (corr_lower[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> corr_upper[i, j]) {</span>
<span id="cb9-53">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> component <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> direction[pos];</span>
<span id="cb9-54">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> distance_to_face;</span>
<span id="cb9-55"></span>
<span id="cb9-56">          direction_matrix[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> component;</span>
<span id="cb9-57">          direction_matrix[j, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> component;</span>
<span id="cb9-58"></span>
<span id="cb9-59">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (component <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>) {</span>
<span id="cb9-60">            distance_to_face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (corr_upper[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> anchor[i, j])</span>
<span id="cb9-61">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> component;</span>
<span id="cb9-62">            max_radius_box <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(max_radius_box, distance_to_face);</span>
<span id="cb9-63">          } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (component <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>) {</span>
<span id="cb9-64">            distance_to_face <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (corr_lower[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> anchor[i, j])</span>
<span id="cb9-65">                               <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> component;</span>
<span id="cb9-66">            max_radius_box <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(max_radius_box, distance_to_face);</span>
<span id="cb9-67">          }</span>
<span id="cb9-68"></span>
<span id="cb9-69">          pos <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="cb9-70">        }</span>
<span id="cb9-71">      }</span>
<span id="cb9-72">    }</span>
<span id="cb9-73"></span>
<span id="cb9-74">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (pos <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> D <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="cb9-75">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Global-ray transform consumed "</span>, pos <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="cb9-76">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" free direction values but received "</span>, D, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb9-77">    }</span>
<span id="cb9-78"></span>
<span id="cb9-79">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// C(t) = C0 + t*S is positive definite exactly while</span></span>
<span id="cb9-80">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// I + t * L0^{-1} S L0^{-T} is positive definite.</span></span>
<span id="cb9-81">    {</span>
<span id="cb9-82">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] left_solve <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mdivide_left_tri_low</span>(</span>
<span id="cb9-83">          anchor_cholesky, direction_matrix);</span>
<span id="cb9-84">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] standardized_direction <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mdivide_left_tri_low</span>(</span>
<span id="cb9-85">          anchor_cholesky, left_solve<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb9-86">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] eigenvalues <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eigenvalues_sym</span>(</span>
<span id="cb9-87">          <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> (standardized_direction <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> standardized_direction<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>));</span>
<span id="cb9-88">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lambda_min <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eigenvalues[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>];</span>
<span id="cb9-89"></span>
<span id="cb9-90">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (lambda_min <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>) {</span>
<span id="cb9-91">        max_radius_pd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</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;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> lambda_min;</span>
<span id="cb9-92">      }</span>
<span id="cb9-93">      max_radius <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(max_radius_box, max_radius_pd);</span>
<span id="cb9-94">    }</span>
<span id="cb9-95"></span>
<span id="cb9-96">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(max_radius <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is_inf</span>(max_radius)) {</span>
<span id="cb9-97">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The supplied anchor has no finite positive ray in this "</span>,</span>
<span id="cb9-98">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"direction; max_radius="</span>, max_radius, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb9-99">    }</span>
<span id="cb9-100"></span>
<span id="cb9-101">    radius <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> radius_fraction <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> max_radius;</span>
<span id="cb9-102">    Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> anchor <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> radius <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> direction_matrix;</span>
<span id="cb9-103"></span>
<span id="cb9-104">    {</span>
<span id="cb9-105">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cholesky_decompose</span>(Omega);</span>
<span id="cb9-106">      log_det <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;">2.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diagonal</span>(L)));</span>
<span id="cb9-107">    }</span>
<span id="cb9-108"></span>
<span id="cb9-109">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// In D dimensions, dc = radius^(D-1) d(radius) d(surface).</span></span>
<span id="cb9-110">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Normalizing an isotropic standard-normal auxiliary vector supplies</span></span>
<span id="cb9-111">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// uniform surface measure, so only the ray and logistic terms remain.</span></span>
<span id="cb9-112">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (D <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(radius)</span>
<span id="cb9-113">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(max_radius)</span>
<span id="cb9-114">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log_inv_logit</span>(radius_raw)</span>
<span id="cb9-115">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m_inv_logit</span>(radius_raw);</span>
<span id="cb9-116"></span>
<span id="cb9-117">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (Omega, log_det, max_radius, max_radius_box, max_radius_pd);</span>
<span id="cb9-118">  }</span>
<span id="cb9-119">}</span>
<span id="cb9-120"></span>
<span id="cb9-121"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb9-122">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// With only one free cell, at any K, the unit direction is the</span></span>
<span id="cb9-123">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// discontinuous sign of one scalar; use a direct bounded logistic for</span></span>
<span id="cb9-124">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// that one-dimensional special case.</span></span>
<span id="cb9-125">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">&gt;</span> K;</span>
<span id="cb9-126">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;</span>
<span id="cb9-127">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_fixed;</span>
<span id="cb9-128">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] corr_lower;</span>
<span id="cb9-129">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] corr_upper;</span>
<span id="cb9-130">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">corr_matrix</span>[K] anchor;</span>
<span id="cb9-131">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance;</span>
<span id="cb9-132">}</span>
<span id="cb9-133"></span>
<span id="cb9-134"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed data</span> {</span>
<span id="cb9-135">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] anchor_cholesky <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cholesky_decompose</span>(anchor);</span>
<span id="cb9-136">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> fixed_count <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;">0</span>;</span>
<span id="cb9-137">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> D_free <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_fixed;</span>
<span id="cb9-138"></span>
<span id="cb9-139">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(eta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>)) {</span>
<span id="cb9-140">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The LKJ shape eta must be strictly positive; received "</span>, eta,</span>
<span id="cb9-141">           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb9-142">  }</span>
<span id="cb9-143">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (D_free <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;">2</span>) {</span>
<span id="cb9-144">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The normalized-direction global-ray transform requires at least "</span>,</span>
<span id="cb9-145">           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two free correlations; received "</span>, D_free, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb9-146">  }</span>
<span id="cb9-147"></span>
<span id="cb9-148">  <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="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>K) {</span>
<span id="cb9-149">    <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;">abs</span>(corr_lower[i, i] <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance</span>
<span id="cb9-150">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(corr_upper[i, i] <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance) {</span>
<span id="cb9-151">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Both bound matrices must equal one on diagonal "</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb9-152">    }</span>
<span id="cb9-153"></span>
<span id="cb9-154">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb9-155">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb9-156">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_lower[i, j];</span>
<span id="cb9-157">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_upper[i, j];</span>
<span id="cb9-158"></span>
<span id="cb9-159">        <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;">abs</span>(lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> corr_lower[j, i]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance</span>
<span id="cb9-160">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> corr_upper[j, i]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance) {</span>
<span id="cb9-161">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bounds are not symmetric at pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")."</span>);</span>
<span id="cb9-162">        }</span>
<span id="cb9-163">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</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;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> hi) {</span>
<span id="cb9-164">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j,</span>
<span id="cb9-165">                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">") must have ordered bounds inside [-1,1]; got ["</span>,</span>
<span id="cb9-166">                 lo, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, hi, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]."</span>);</span>
<span id="cb9-167">        }</span>
<span id="cb9-168"></span>
<span id="cb9-169">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> hi) {</span>
<span id="cb9-170">          fixed_count <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="cb9-171">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (anchor[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> lo) {</span>
<span id="cb9-172">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Anchor must equal the exact fixed value at pair ("</span>, i,</span>
<span id="cb9-173">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"): anchor="</span>, anchor[i, j],</span>
<span id="cb9-174">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", fixed value="</span>, lo, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb9-175">          }</span>
<span id="cb9-176">        } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb9-177">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(anchor[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> validation_tolerance</span>
<span id="cb9-178">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> anchor[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> validation_tolerance)) {</span>
<span id="cb9-179">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Anchor is not strictly inside the bounds at pair ("</span>, i,</span>
<span id="cb9-180">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"): anchor="</span>, anchor[i, j], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", bounds=["</span>,</span>
<span id="cb9-181">                   lo, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, hi, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]."</span>);</span>
<span id="cb9-182">          }</span>
<span id="cb9-183">        }</span>
<span id="cb9-184">      }</span>
<span id="cb9-185">    }</span>
<span id="cb9-186">  }</span>
<span id="cb9-187"></span>
<span id="cb9-188">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (fixed_count <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> N_fixed) {</span>
<span id="cb9-189">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N_fixed declares "</span>, N_fixed, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" exact cells, but the lower and "</span>,</span>
<span id="cb9-190">           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"upper bound matrices contain "</span>, fixed_count,</span>
<span id="cb9-191">           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" equal off-diagonal pairs."</span>);</span>
<span id="cb9-192">  }</span>
<span id="cb9-193">}</span>
<span id="cb9-194"></span>
<span id="cb9-195"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb9-196">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_fixed] direction_raw;</span>
<span id="cb9-197">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> radius_raw;</span>
<span id="cb9-198">}</span>
<span id="cb9-199"></span>
<span id="cb9-200"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb9-201">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">corr_matrix</span>[K] Omega;</span>
<span id="cb9-202">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_det_Omega;</span>
<span id="cb9-203">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> max_radius;</span>
<span id="cb9-204">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> max_radius_box;</span>
<span id="cb9-205">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> max_radius_pd;</span>
<span id="cb9-206">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> pd_boundary_active;</span>
<span id="cb9-207"></span>
<span id="cb9-208">  {</span>
<span id="cb9-209">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tuple</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K], <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>) result</span>
<span id="cb9-210">        <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;">corr_global_ray_jacobian</span>(</span>
<span id="cb9-211">            direction_raw, radius_raw,</span>
<span id="cb9-212">            corr_lower, corr_upper, anchor, anchor_cholesky);</span>
<span id="cb9-213">    Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> result<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>;</span>
<span id="cb9-214">    log_det_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> result<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>;</span>
<span id="cb9-215">    max_radius <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> result<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.3</span>;</span>
<span id="cb9-216">    max_radius_box <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> result<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.4</span>;</span>
<span id="cb9-217">    max_radius_pd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> result<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span>;</span>
<span id="cb9-218">    pd_boundary_active <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> max_radius_pd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> max_radius_box <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.0</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.0</span>;</span>
<span id="cb9-219">  }</span>
<span id="cb9-220">}</span>
<span id="cb9-221"></span>
<span id="cb9-222"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb9-223">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// The auxiliary radius integrates out independently; isotropy makes the</span></span>
<span id="cb9-224">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// normalized direction uniform on the sphere.</span></span>
<span id="cb9-225">  direction_raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">std_normal</span>();</span>
<span id="cb9-226">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (eta <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> log_det_Omega;</span>
<span id="cb9-227">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>direct-certified-box-corr.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" data-filename="direct-certified-box-corr.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/*</span></span>
<span id="cb10-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  Direct ordinary-correlation transform for a box certified to lie entirely</span></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  inside the positive-definite correlation cone.</span></span>
<span id="cb10-4"></span>
<span id="cb10-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  This model deliberately performs no feasibility look-ahead.  It is valid</span></span>
<span id="cb10-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  only when the supplied box has been certified externally (for example with</span></span>
<span id="cb10-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  a spectral-norm/Weyl bound around a positive-definite anchor).</span></span>
<span id="cb10-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*/</span></span>
<span id="cb10-9"></span>
<span id="cb10-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb10-11">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;</span>
<span id="cb10-12">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;</span>
<span id="cb10-13">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] corr_lower;</span>
<span id="cb10-14">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] corr_upper;</span>
<span id="cb10-15">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance;</span>
<span id="cb10-16">}</span>
<span id="cb10-17"></span>
<span id="cb10-18"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed data</span> {</span>
<span id="cb10-19">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(eta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>)) {</span>
<span id="cb10-20">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The LKJ shape eta must be strictly positive; received "</span>, eta,</span>
<span id="cb10-21">           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb10-22">  }</span>
<span id="cb10-23"></span>
<span id="cb10-24">  <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="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>K) {</span>
<span id="cb10-25">    <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;">abs</span>(corr_lower[i, i] <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance</span>
<span id="cb10-26">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(corr_upper[i, i] <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance) {</span>
<span id="cb10-27">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Both bound matrices must equal one on diagonal "</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb10-28">    }</span>
<span id="cb10-29"></span>
<span id="cb10-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb10-31">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb10-32">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_lower[i, j];</span>
<span id="cb10-33">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_upper[i, j];</span>
<span id="cb10-34"></span>
<span id="cb10-35">        <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;">abs</span>(lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> corr_lower[j, i]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance</span>
<span id="cb10-36">            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> corr_upper[j, i]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance) {</span>
<span id="cb10-37">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bounds are not symmetric at pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")."</span>);</span>
<span id="cb10-38">        }</span>
<span id="cb10-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</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;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> hi)) {</span>
<span id="cb10-40">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j,</span>
<span id="cb10-41">                 <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">") must have positive-width bounds inside [-1,1]; got ["</span>,</span>
<span id="cb10-42">                 lo, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, hi, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]."</span>);</span>
<span id="cb10-43">        }</span>
<span id="cb10-44">      }</span>
<span id="cb10-45">    }</span>
<span id="cb10-46">  }</span>
<span id="cb10-47">}</span>
<span id="cb10-48"></span>
<span id="cb10-49"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb10-50">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)] raw;</span>
<span id="cb10-51">}</span>
<span id="cb10-52"></span>
<span id="cb10-53"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb10-54">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">corr_matrix</span>[K] Omega;</span>
<span id="cb10-55">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_det_Omega;</span>
<span id="cb10-56"></span>
<span id="cb10-57">  {</span>
<span id="cb10-58">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] work <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">identity_matrix</span>(K);</span>
<span id="cb10-59">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> pos <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="cb10-60"></span>
<span id="cb10-61">    <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="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>K) {</span>
<span id="cb10-62">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb10-63">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> value <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;">lower_upper_bound_jacobian</span>(</span>
<span id="cb10-64">            raw[pos], corr_lower[i, j], corr_upper[i, j]);</span>
<span id="cb10-65">        work[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value;</span>
<span id="cb10-66">        work[j, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value;</span>
<span id="cb10-67">        pos <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="cb10-68">      }</span>
<span id="cb10-69">    }</span>
<span id="cb10-70"></span>
<span id="cb10-71">    Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> work;</span>
<span id="cb10-72">    log_det_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> log_determinant_spd(Omega);</span>
<span id="cb10-73">  }</span>
<span id="cb10-74">}</span>
<span id="cb10-75"></span>
<span id="cb10-76"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb10-77">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (eta <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> log_det_Omega;</span>
<span id="cb10-78">}</span></code></pre></div></div>
</div>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Revision note
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>August 10, 2026.</strong> I revised the explanation of exact fixed correlations and several related passages to remove context-dependent shorthand, explain the zero-width normalization issue directly, and clarify how correlations chosen in earlier Cholesky rows can affect the feasibility of later rows. This was an expository revision; the mathematical results, implementations, and numerical results were not changed.</p>
</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-axen2023" class="csl-entry">
Axen, Seth. 2023. <em>Comment on <span>“Covariance Conditioned on Undirected Graph”</span></em>. TuringLang/Turing.jl GitHub discussion #2067. <a href="https://github.com/TuringLang/Turing.jl/discussions/2067#discussioncomment-7023577">https://github.com/TuringLang/Turing.jl/discussions/2067#discussioncomment-7023577</a>.
</div>
<div id="ref-carpenter2017" class="csl-entry">
Carpenter, Bob, Andrew Gelman, Matthew D. Hoffman, et al. 2017. <span>“Stan: A Probabilistic Programming Language.”</span> <em>Journal of Statistical Software</em> 76 (1): 1–32. <a href="https://doi.org/10.18637/jss.v076.i01">https://doi.org/10.18637/jss.v076.i01</a>.
</div>
<div id="ref-hornJohnson2013" class="csl-entry">
Horn, Roger A., and Charles R. Johnson. 2013. <em>Matrix Analysis</em>. 2nd ed. Cambridge University Press. <a href="https://doi.org/10.1017/CBO9781139020411">https://doi.org/10.1017/CBO9781139020411</a>.
</div>
<div id="ref-lewandowski2009" class="csl-entry">
Lewandowski, Daniel, Dorota Kurowicka, and Harry Joe. 2009. <span>“Generating Random Correlation Matrices Based on Vines and Extended Onion Method.”</span> <em>Journal of Multivariate Analysis</em> 100 (9): 1989–2001. <a href="https://doi.org/10.1016/j.jmva.2009.04.008">https://doi.org/10.1016/j.jmva.2009.04.008</a>.
</div>
<div id="ref-pinkney2024" class="csl-entry">
Pinkney, Sean. 2024. <em>A Short Note on a Flexible Cholesky Parameterization of Correlation Matrices</em>. <a href="https://arxiv.org/abs/2405.07286">https://arxiv.org/abs/2405.07286</a>.
</div>
<div id="ref-pinkney2025" class="csl-entry">
Pinkney, Sean. 2025. <em>Updated Cholesky Corr Parameterization Testing</em>. Stan Discourse thread. <a href="https://discourse.mc-stan.org/t/updated-cholesky-corr-parameterization-testing/38827">https://discourse.mc-stan.org/t/updated-cholesky-corr-parameterization-testing/38827</a>.
</div>
<div id="ref-sylvester1852" class="csl-entry">
Sylvester, J. J. 1852. <span>“<span>XIX</span>. A Demonstration of the Theorem That Every Homogeneous Quadratic Polynomial Is Reducible by Real Orthogonal Substitutions to the Form of a Sum of Positive and Negative Squares.”</span> <em>The London, Edinburgh, and Dublin Philosophical Magazine and Journal of Science</em> 4 (23): 138–42. <a href="https://doi.org/10.1080/14786445208647087">https://doi.org/10.1080/14786445208647087</a>.
</div>
</div></section></div> ]]></description>
  <category>stan</category>
  <category>correlation</category>
  <category>parameterizations</category>
  <guid>https://spinkney.github.io/posts/post-12-corr-bounds/corr-bounds.html</guid>
  <pubDate>Sat, 08 Aug 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Structural Zeros in Correlation Matrices: A Thirteen-Parameterization Bake-Off</title>
  <dc:creator>Sean Pinkney</dc:creator>
  <link>https://spinkney.github.io/posts/post-11-corr-zeros-bakeoff/corr-zeros-bakeoff.html</link>
  <description><![CDATA[ 





<section id="the-problem-with-zeros-in-correlation-matrices" class="level2">
<h2 class="anchored" data-anchor-id="the-problem-with-zeros-in-correlation-matrices">1. The Problem with Zeros in Correlation Matrices</h2>
<p>Sometimes you know, before seeing any data, that certain correlations are exactly zero. For example, a priori knowledge that two variables are <em>marginally</em><sup>1</sup> unrelated occurs in graphical models as either an explicit assumption or by pre-existing knowledge that rules out the variable coupling. Building this structure directly through the correlation matrix parameterization is what this blog post is about.</p>
<p>Sampling from an LKJ prior and setting selected cells to zero does not work because the resulting matrix will generally not be positive definite. Instead, the zeros have to be part of the parameterization. In this post I compare thirteen Stan <span class="citation" data-cites="carpenter2017">(Carpenter et al. 2017)</span> implementations that target the same distribution. I check that they agree numerically, then compare divergences, effective sample size, gradient evaluations, and run time.</p>
<p>The implementations fall into two groups. The first imposes the zero equalities sequentially in Cholesky or C-vine coordinates. The second samples each row in the subspace satisfying all of its zero constraints. I also introduce a triangular basis for the second approach. In these examples it removes the divergences seen in the entry-wise construction without the loss of efficiency I found with a QR basis.</p>
</section>
<section id="family-1-forced-zeros" class="level2">
<h2 class="anchored" data-anchor-id="family-1-forced-zeros">2. Family 1: Forced Zeros</h2>
<p>Seven members of the first group use the constrained-Cholesky approach I introduced in <span class="citation" data-cites="pinkney2024">Pinkney (2024a)</span> and presented at StanCon <span class="citation" data-cites="pinkneytalk">(Pinkney 2024b)</span>. The original method also allows a correlation to be fixed at a nonzero value or restricted to an interval. Writing <img src="https://latex.codecogs.com/png.latex?%5COmega%20=%20L%20L'"> with unit-norm rows of <img src="https://latex.codecogs.com/png.latex?L">, the correlation in cell <img src="https://latex.codecogs.com/png.latex?(i,j)"> is <img src="https://latex.codecogs.com/png.latex?%0A%5COmega_%7Bij%7D%20=%20b_1%20+%20L_%7Bij%7D%20L_%7Bjj%7D,%20%5Cqquad%0Ab_1%20=%20%5Csum_%7Bk%3Cj%7D%20L_%7Bik%7D%20L_%7Bjk%7D,%0A"> This is affine in <img src="https://latex.codecogs.com/png.latex?L_%7Bij%7D">, so setting the correlation to zero gives <img src="https://latex.codecogs.com/png.latex?L_%7Bij%7D=-b_1/L_%7Bjj%7D"> and uses no parameter. A bound on <img src="https://latex.codecogs.com/png.latex?%5COmega_%7Bij%7D"> similarly becomes a bound on <img src="https://latex.codecogs.com/png.latex?L_%7Bij%7D">. Conditioning on <img src="https://latex.codecogs.com/png.latex?%5COmega_%7Bij%7D=0"> contributes <img src="https://latex.codecogs.com/png.latex?-%5Clog%20L_%7Bjj%7D"> to the log density for each zero. The <code>carpenter</code> variant below is Bob Carpenter’s compact formulation of this case from a <a href="https://discourse.mc-stan.org/t/new-users-guide-intro-sem-and-user-defined-constrained-parameters-chapters/41430">Stan Discourse discussion</a>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb1-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (is_zero(i, j, zeros, zero_idx)) {</span>
<span id="cb1-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(L[j, <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>(j <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>)], L[i, <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>(j <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="cb1-3">  L[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> L[j, j];   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// implies Omega[i, j] == 0</span></span>
<span id="cb1-4">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(L[j, j]);</span>
<span id="cb1-5">  zero_idx <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="cb1-6">}</span></code></pre></div></div>
<p>The free entries can be parameterized in several ways. I compare the following nine variants:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">label</th>
<th style="text-align: left;">free-cell parameterization</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>carpenter</code></td>
<td style="text-align: left;">direct Cholesky entries, bounded by the remaining stick</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>pinkney</code></td>
<td style="text-align: left;">correlation-scale value divided by <img src="https://latex.codecogs.com/png.latex?L_%7Bjj%7D">, multiplicative stick <span class="citation" data-cites="pinkney2024 pinkneytalk">(Pinkney 2024a, 2024b)</span></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>ldl</code> / <code>ldl2</code> / <code>ldl3</code></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?LDL'"> factorization <span class="citation" data-cites="pinkney2024">(Pinkney 2024a)</span>; stick tracked via <code>log_diff_exp</code>, a <code>log1m</code> ratio, or the natural scale</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>cvine</code></td>
<td style="text-align: left;">C-vine partial correlations with the LKJ Beta factorization <span class="citation" data-cites="joe2006 lewandowski2009">(Joe 2006; Lewandowski et al. 2009)</span></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>cvine_lookahead</code></td>
<td style="text-align: left;">exact equality coordinates, all-future 3-by-3 look-ahead, and a reduced free-cell Jacobian</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>rowscale</code></td>
<td style="text-align: left;">direct Cholesky entries, row-scaling stick</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>schur</code></td>
<td style="text-align: left;">canonical partial correlations, <img src="https://latex.codecogs.com/png.latex?L_%7Bij%7D%20=%20r%20%5Ctanh(x)"></td>
</tr>
</tbody>
</table>
<p>The seven Cholesky and <img src="https://latex.codecogs.com/png.latex?LDL'"> variants share the same constraint. The value <img src="https://latex.codecogs.com/png.latex?-b_1/L_%7Bjj%7D"> is computed without reference to the remaining row norm, <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7B1%20-%20%5Csum_%7Bk%3Cj%7D%20L_%7Bik%7D%5E2%7D">, and can exceed the available range. A later square root then receives a negative argument. In the <img src="https://latex.codecogs.com/png.latex?K=7"> benchmarks below, these variants have divergences in about 1% of post-warmup transitions.</p>
<p>The <a href="https://gist.github.com/spinkney/7d994fb03079c9edb8a0e83a40d2faad"><code>cvine_lookahead</code> model</a> instead represents every zero as an exact ordinary-correlation equality, omits those coordinates from the parameter vector, and uses exact <img src="https://latex.codecogs.com/png.latex?3%5Ctimes3"> elliptope projections to narrow each free partial-correlation interval before sampling it. Its Jacobian contains only the free ordinary-correlation coordinates, and the LKJ kernel is added through <img src="https://latex.codecogs.com/png.latex?(%5Ceta-1)%5Clog%5Cdet(%5COmega)">. The look-ahead substantially reduces infeasible proposals, although it does not remove every nonlocal feasibility boundary for dense patterns.</p>
<p>The three LDL variants are also useful for judging Monte Carlo variability. They define the same density and differ only in how some floating-point operations are arranged, but their total divergences over five patterns range from 356 to 466. Differences of that size should not be interpreted as evidence that one parameterization is better from a single set of fits.</p>
</section>
<section id="family-2-exact-constraint-subspaces" class="level2">
<h2 class="anchored" data-anchor-id="family-2-exact-constraint-subspaces">3. Family 2: Exact-Constraint Subspaces</h2>
<p>The second approach comes from Seth Axen’s Turing.jl discussion of positive definite matrices with structural zeros <span class="citation" data-cites="axen2023">(Axen 2023)</span>:</p>
<blockquote class="blockquote">
<p>For each column j, each column i for i &lt; j that is orthogonal to column j decreases the dimensionality of the hemisphere by 1. So we simply construct a point on a lower dimensional hemisphere and use the QR decomposition of the orthogonal columns to lift it to a higher dimensional hemisphere.</p>
</blockquote>
<p>Row <img src="https://latex.codecogs.com/png.latex?i"> of a correlation Cholesky factor is a unit vector with a positive diagonal element. Since row <img src="https://latex.codecogs.com/png.latex?j"> is zero after column <img src="https://latex.codecogs.com/png.latex?j">, the condition <img src="https://latex.codecogs.com/png.latex?%5COmega_%7Bij%7D=0"> is equivalent to requiring rows <img src="https://latex.codecogs.com/png.latex?i"> and <img src="https://latex.codecogs.com/png.latex?j"> to be orthogonal. If row <img src="https://latex.codecogs.com/png.latex?i"> has <img src="https://latex.codecogs.com/png.latex?n_z"> constrained correlations, it can be sampled on the unit hemisphere in the orthogonal complement of those <img src="https://latex.codecogs.com/png.latex?n_z"> rows. The dimension of this hemisphere is <img src="https://latex.codecogs.com/png.latex?d=i-1-n_z">. The joint conditioning term for the row is <img src="https://latex.codecogs.com/png.latex?-%5Ctfrac12%5Clog%5Cdet(A'A)">, where the columns of <img src="https://latex.codecogs.com/png.latex?A"> are the rows to which it must be orthogonal.</p>
<p>My first two implementations, <code>qr</code> and <code>qr2</code>, construct this complement with a Householder QR decomposition. They use either a gnomonic map or the hyper-tanh peel <span class="citation" data-cites="pinkney2026">(Pinkney 2026)</span> for the hemisphere coordinates. Both models have zero divergences in the <img src="https://latex.codecogs.com/png.latex?K=7"> benchmark, but their minimum ESS is about 40% of the entry-wise models. Changing the hemisphere map has little effect. The larger difference comes from the complement basis: the trailing columns of <img src="https://latex.codecogs.com/png.latex?Q"> change as the preceding rows change, so the interpretation of the unconstrained coordinates also changes during sampling.</p>
</section>
<section id="a-canonical-triangular-basis" class="level2">
<h2 class="anchored" data-anchor-id="a-canonical-triangular-basis">4. A Canonical Triangular Basis</h2>
<p>I instead construct a basis tied to the free matrix entries. For each free column <img src="https://latex.codecogs.com/png.latex?k"> of row <img src="https://latex.codecogs.com/png.latex?i">, I start with the coordinate vector <img src="https://latex.codecogs.com/png.latex?e_k"> and solve for the constrained columns by back-substitution. This is the entry-wise construction from the previous section applied to a basis vector rather than to the sampled row. If <img src="https://latex.codecogs.com/png.latex?Z"> and <img src="https://latex.codecogs.com/png.latex?F"> denote the constrained and free columns, the resulting basis matrix <img src="https://latex.codecogs.com/png.latex?T"> satisfies <img src="https://latex.codecogs.com/png.latex?T_F=I"> and <img src="https://latex.codecogs.com/png.latex?T_Z=-(A_Z')%5E%7B-1%7DA_F'">.</p>
<p>To orthonormalize this basis, let <img src="https://latex.codecogs.com/png.latex?S=%5Coperatorname%7Bchol%7D(T'T)">. Then <img src="https://latex.codecogs.com/png.latex?v=T(S')%5E%7B-1%7Dz"> preserves the norm of <img src="https://latex.codecogs.com/png.latex?z">. I parameterize <img src="https://latex.codecogs.com/png.latex?z"> using the row transformation from <span class="citation" data-cites="pinkney2025">Pinkney (2025)</span>. With <img src="https://latex.codecogs.com/png.latex?t=y'y">, <img src="https://latex.codecogs.com/png.latex?%0Az%20=%20%5Cfrac%7B%5Csqrt%7Bt+2%7D%7D%7Bt+1%7Dy,%20%5Cqquad%20L_%7Bii%7D=%5Cfrac%7B1%7D%7Bt+1%7D,%0A"> and the log Jacobian is <img src="https://latex.codecogs.com/png.latex?%5Ctfrac12(d-2)%5Clog(t+2)-(d+1)%5Clog(1+t)">.</p>
<p>Two identities reduce the amount of linear algebra required.</p>
<ol type="1">
<li><p>The zero-partner rows have triangular support, so <img src="https://latex.codecogs.com/png.latex?A_Z"> is triangular with the partners’ diagonals on its diagonal. Sylvester’s determinant identity gives <img src="https://latex.codecogs.com/png.latex?%0A%5Cdet(A'A)%20=%20%5CBig(%5Cprod_%7Bj%20%5Cin%20Z%7D%20L_%7Bjj%7D%5CBig)%5E%7B2%7D%20%5Cdet(T'T).%0A"> Thus <img src="https://latex.codecogs.com/png.latex?-%5Ctfrac12%5Clog%5Cdet(A'A)"> becomes <img src="https://latex.codecogs.com/png.latex?-%5Csum_j%5Clog%20L_%7Bjj%7D-%5Csum_k%5Clog%20S_%7Bkk%7D">. The second term is available from the Cholesky factor already used to orthonormalize <img src="https://latex.codecogs.com/png.latex?T">.</p></li>
<li><p>Since <img src="https://latex.codecogs.com/png.latex?T_F=I">, <img src="https://latex.codecogs.com/png.latex?T'T=I+W'W">, where <img src="https://latex.codecogs.com/png.latex?W"> contains only the <img src="https://latex.codecogs.com/png.latex?n_z"> constrained rows. The cross-product therefore uses vectors of length <img src="https://latex.codecogs.com/png.latex?n_z"> rather than <img src="https://latex.codecogs.com/png.latex?i-1">.</p></li>
</ol>
<p>Each constrained row requires one <img src="https://latex.codecogs.com/png.latex?d%5Ctimes%20d"> <code>cholesky_decompose</code> and several matrix-vector products. I call this model <code>tri</code>; the Stan code is in the appendix.</p>
<p>I also test an over-parameterized version, called <code>donut</code>, that uses one additional parameter per row. It normalizes a free vector, <img src="https://latex.codecogs.com/png.latex?u=y/%5ClVert%20y%5CrVert">, before mapping it into the constrained subspace. The normalization is rotationally symmetric but singular at the origin. Following <span class="citation" data-cites="axen2022">Axen (2022)</span>, I replace the usual radial term <img src="https://latex.codecogs.com/png.latex?-%5Ctfrac12%5ClVert%20y%5CrVert%5E2"> with <img src="https://latex.codecogs.com/png.latex?-%5Ctfrac12(%5ClVert%20y%5CrVert-m)%5E2"> so that the typical radius is away from zero. Since this term only changes the radius, it does not change the induced distribution on <img src="https://latex.codecogs.com/png.latex?%5COmega">. The largest difference in a free-cell posterior mean relative to <code>carpenter</code> is 0.0046. Across the five <img src="https://latex.codecogs.com/png.latex?K=7">, <img src="https://latex.codecogs.com/png.latex?%5Ceta=4"> patterns:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 27%">
<col style="width: 24%">
<col style="width: 24%">
<col style="width: 24%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">radial prior</th>
<th style="text-align: right;">total divergences</th>
<th style="text-align: right;">median min ESS</th>
<th style="text-align: right;">median ESS / 1k gradients</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?m%20=%200"> (implicit chi)</td>
<td style="text-align: right;">2,549</td>
<td style="text-align: right;">4,778</td>
<td style="text-align: right;">19</td>
</tr>
<tr class="even">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?m%20=%202"></td>
<td style="text-align: right;">349</td>
<td style="text-align: right;">8,106</td>
<td style="text-align: right;">80</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?m%20=%204"></td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">10,778</td>
<td style="text-align: right;">192</td>
</tr>
<tr class="even">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?m%20=%206"></td>
<td style="text-align: right;"><strong>0</strong></td>
<td style="text-align: right;">9,045</td>
<td style="text-align: right;">162</td>
</tr>
</tbody>
</table>
<p>Increasing <img src="https://latex.codecogs.com/png.latex?m"> removes the divergences and improves efficiency up to <img src="https://latex.codecogs.com/png.latex?m=4">; <img src="https://latex.codecogs.com/png.latex?m=6"> gives zero divergences in these runs. I use <img src="https://latex.codecogs.com/png.latex?m=6"> below. The <code>tri</code> model does not require this additional choice and uses <img src="https://latex.codecogs.com/png.latex?K-1"> fewer parameters.</p>
</section>
<section id="the-bake-off-at-k-7" class="level2">
<h2 class="anchored" data-anchor-id="the-bake-off-at-k-7">5. The Bake-Off at K = 7</h2>
<p>Setup: <img src="https://latex.codecogs.com/png.latex?K%20=%207">, <img src="https://latex.codecogs.com/png.latex?%5Ceta%20=%204">, prior-only, five random zero patterns with roughly half of the 21 lower-triangular cells zeroed, 4 chains <img src="https://latex.codecogs.com/png.latex?%5Ctimes"> 2000 draws after 1000 warmup, default <code>adapt_delta = 0.8</code>. Correctness was checked on a held-out pattern first: all thirteen posteriors match the <code>carpenter</code> baseline within Monte Carlo error, and the constrained cells come back as numerically exact zeros (<img src="https://latex.codecogs.com/png.latex?%5Csim%2010%5E%7B-19%7D">).</p>
<p><strong>Divergences</strong> out of 8,000 post-warmup transitions:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">pattern</th>
<th style="text-align: right;">carpenter</th>
<th style="text-align: right;">pinkney</th>
<th style="text-align: right;">ldl</th>
<th style="text-align: right;">ldl2</th>
<th style="text-align: right;">cvine</th>
<th style="text-align: right;">cvine_lookahead</th>
<th style="text-align: right;">rowscale</th>
<th style="text-align: right;">ldl3</th>
<th style="text-align: right;">schur</th>
<th style="text-align: right;">qr</th>
<th style="text-align: right;">qr2</th>
<th style="text-align: right;">tri</th>
<th style="text-align: right;">donut</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">seed 1</td>
<td style="text-align: right;">34</td>
<td style="text-align: right;">53</td>
<td style="text-align: right;">53</td>
<td style="text-align: right;">26</td>
<td style="text-align: right;">41</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">44</td>
<td style="text-align: right;">40</td>
<td style="text-align: right;">40</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;"><strong>0</strong></td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">seed 2</td>
<td style="text-align: right;">61</td>
<td style="text-align: right;">65</td>
<td style="text-align: right;">74</td>
<td style="text-align: right;">107</td>
<td style="text-align: right;">64</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">78</td>
<td style="text-align: right;">104</td>
<td style="text-align: right;">60</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;"><strong>0</strong></td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td style="text-align: left;">seed 3</td>
<td style="text-align: right;">77</td>
<td style="text-align: right;">44</td>
<td style="text-align: right;">67</td>
<td style="text-align: right;">57</td>
<td style="text-align: right;">69</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">44</td>
<td style="text-align: right;">68</td>
<td style="text-align: right;">59</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;"><strong>0</strong></td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;">seed 4</td>
<td style="text-align: right;">137</td>
<td style="text-align: right;">157</td>
<td style="text-align: right;">130</td>
<td style="text-align: right;">112</td>
<td style="text-align: right;">186</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">155</td>
<td style="text-align: right;">152</td>
<td style="text-align: right;">123</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;"><strong>0</strong></td>
<td style="text-align: right;">0</td>
</tr>
<tr class="odd">
<td style="text-align: left;">seed 5</td>
<td style="text-align: right;">110</td>
<td style="text-align: right;">94</td>
<td style="text-align: right;">114</td>
<td style="text-align: right;">54</td>
<td style="text-align: right;">141</td>
<td style="text-align: right;">7</td>
<td style="text-align: right;">114</td>
<td style="text-align: right;">102</td>
<td style="text-align: right;">29</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;"><strong>0</strong></td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>total</strong></td>
<td style="text-align: right;">419</td>
<td style="text-align: right;">413</td>
<td style="text-align: right;">438</td>
<td style="text-align: right;">356</td>
<td style="text-align: right;">501</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">435</td>
<td style="text-align: right;">466</td>
<td style="text-align: right;">311</td>
<td style="text-align: right;"><strong>0</strong></td>
<td style="text-align: right;"><strong>0</strong></td>
<td style="text-align: right;"><strong>0</strong></td>
<td style="text-align: right;"><strong>0</strong></td>
</tr>
</tbody>
</table>
<p><strong>Min bulk ESS</strong> over the free cells of <img src="https://latex.codecogs.com/png.latex?%5COmega"> (8,000 draws):</p>
<table class="caption-top table">
<colgroup>
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">pattern</th>
<th style="text-align: right;">carpenter</th>
<th style="text-align: right;">pinkney</th>
<th style="text-align: right;">ldl</th>
<th style="text-align: right;">ldl2</th>
<th style="text-align: right;">cvine</th>
<th style="text-align: right;">cvine_lookahead</th>
<th style="text-align: right;">rowscale</th>
<th style="text-align: right;">ldl3</th>
<th style="text-align: right;">schur</th>
<th style="text-align: right;">qr</th>
<th style="text-align: right;">qr2</th>
<th style="text-align: right;">tri</th>
<th style="text-align: right;">donut</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">seed 1</td>
<td style="text-align: right;">11639</td>
<td style="text-align: right;">10566</td>
<td style="text-align: right;">9979</td>
<td style="text-align: right;">10618</td>
<td style="text-align: right;">10219</td>
<td style="text-align: right;">11572</td>
<td style="text-align: right;">11319</td>
<td style="text-align: right;">9840</td>
<td style="text-align: right;">11036</td>
<td style="text-align: right;">3995</td>
<td style="text-align: right;">3362</td>
<td style="text-align: right;"><strong>11238</strong></td>
<td style="text-align: right;">10676</td>
</tr>
<tr class="even">
<td style="text-align: left;">seed 2</td>
<td style="text-align: right;">10817</td>
<td style="text-align: right;">9866</td>
<td style="text-align: right;">10826</td>
<td style="text-align: right;">9772</td>
<td style="text-align: right;">10188</td>
<td style="text-align: right;">12308</td>
<td style="text-align: right;">10153</td>
<td style="text-align: right;">10361</td>
<td style="text-align: right;">10040</td>
<td style="text-align: right;">4049</td>
<td style="text-align: right;">3728</td>
<td style="text-align: right;"><strong>11923</strong></td>
<td style="text-align: right;">9045</td>
</tr>
<tr class="odd">
<td style="text-align: left;">seed 3</td>
<td style="text-align: right;">9344</td>
<td style="text-align: right;">11014</td>
<td style="text-align: right;">10244</td>
<td style="text-align: right;">10314</td>
<td style="text-align: right;">10946</td>
<td style="text-align: right;">9535</td>
<td style="text-align: right;">9420</td>
<td style="text-align: right;">10625</td>
<td style="text-align: right;">10787</td>
<td style="text-align: right;">4334</td>
<td style="text-align: right;">4304</td>
<td style="text-align: right;">10655</td>
<td style="text-align: right;">9429</td>
</tr>
<tr class="even">
<td style="text-align: left;">seed 4</td>
<td style="text-align: right;">8395</td>
<td style="text-align: right;">9159</td>
<td style="text-align: right;">8326</td>
<td style="text-align: right;">9257</td>
<td style="text-align: right;">8771</td>
<td style="text-align: right;">9224</td>
<td style="text-align: right;">9463</td>
<td style="text-align: right;">8266</td>
<td style="text-align: right;">9458</td>
<td style="text-align: right;">3701</td>
<td style="text-align: right;">3815</td>
<td style="text-align: right;"><strong>9400</strong></td>
<td style="text-align: right;">8161</td>
</tr>
<tr class="odd">
<td style="text-align: left;">seed 5</td>
<td style="text-align: right;">10381</td>
<td style="text-align: right;">10010</td>
<td style="text-align: right;">9268</td>
<td style="text-align: right;">8760</td>
<td style="text-align: right;">10301</td>
<td style="text-align: right;">10454</td>
<td style="text-align: right;">9218</td>
<td style="text-align: right;">9896</td>
<td style="text-align: right;">4770</td>
<td style="text-align: right;">4267</td>
<td style="text-align: right;">4153</td>
<td style="text-align: right;"><strong>11845</strong></td>
<td style="text-align: right;">8619</td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>median</strong></td>
<td style="text-align: right;">10381</td>
<td style="text-align: right;">10010</td>
<td style="text-align: right;">9979</td>
<td style="text-align: right;">9772</td>
<td style="text-align: right;">10219</td>
<td style="text-align: right;">10454</td>
<td style="text-align: right;">9463</td>
<td style="text-align: right;">9896</td>
<td style="text-align: right;">10040</td>
<td style="text-align: right;">4049</td>
<td style="text-align: right;">3815</td>
<td style="text-align: right;"><strong>11238</strong></td>
<td style="text-align: right;">9045</td>
</tr>
</tbody>
</table>
<p><strong>Efficiency</strong> (medians over the five patterns):</p>
<table class="caption-top table">
<colgroup>
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">carpenter</th>
<th style="text-align: right;">pinkney</th>
<th style="text-align: right;">ldl</th>
<th style="text-align: right;">ldl2</th>
<th style="text-align: right;">cvine</th>
<th style="text-align: right;">cvine_lookahead</th>
<th style="text-align: right;">rowscale</th>
<th style="text-align: right;">ldl3</th>
<th style="text-align: right;">schur</th>
<th style="text-align: right;">qr</th>
<th style="text-align: right;">qr2</th>
<th style="text-align: right;">tri</th>
<th style="text-align: right;">donut</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">min ESS / 1000 gradients</td>
<td style="text-align: right;">188.5</td>
<td style="text-align: right;">179.6</td>
<td style="text-align: right;">185.9</td>
<td style="text-align: right;">182.2</td>
<td style="text-align: right;">188.9</td>
<td style="text-align: right;">194.1</td>
<td style="text-align: right;">176.3</td>
<td style="text-align: right;">182.1</td>
<td style="text-align: right;">182.0</td>
<td style="text-align: right;">75.0</td>
<td style="text-align: right;">72.5</td>
<td style="text-align: right;"><strong>212.3</strong></td>
<td style="text-align: right;">161.8</td>
</tr>
<tr class="even">
<td style="text-align: left;">total wall clock, 5 fits (s)</td>
<td style="text-align: right;">4.53</td>
<td style="text-align: right;">4.10</td>
<td style="text-align: right;">4.40</td>
<td style="text-align: right;">4.51</td>
<td style="text-align: right;">5.14</td>
<td style="text-align: right;">2.65</td>
<td style="text-align: right;">4.21</td>
<td style="text-align: right;">4.51</td>
<td style="text-align: right;">4.15</td>
<td style="text-align: right;">2.07</td>
<td style="text-align: right;">2.06</td>
<td style="text-align: right;"><strong>1.86</strong></td>
<td style="text-align: right;">2.39</td>
</tr>
</tbody>
</table>
<p>The <code>cvine_lookahead</code> model records 8 divergences across the five patterns, compared with 501 for <code>cvine</code>, and completes the five fits in 2.65 rather than 5.14 seconds. The <code>tri</code> model still has zero divergences for all five patterns, the highest median minimum ESS, the highest median ESS per 1,000 gradients, and the shortest total run time. The QR models also remove divergences, but their changing complement basis produces substantially lower ESS. The triangular basis avoids that loss in this benchmark.</p>
</section>
<section id="what-the-look-ahead-changes-cvine-vs-cvine_lookahead" class="level2">
<h2 class="anchored" data-anchor-id="what-the-look-ahead-changes-cvine-vs-cvine_lookahead">6. What the Look-Ahead Changes: <code>cvine</code> vs <code>cvine_lookahead</code></h2>
<p>The original <code>cvine</code> samples the unconstrained C-vine partial correlations from their LKJ Beta factors. When an ordinary correlation is fixed to zero, it solves the affine partial-correlation recursion for the required value. Nothing restricts earlier free partials to ensure that the solution will remain in <img src="https://latex.codecogs.com/png.latex?(-1,1)">, so the transform can meet a feasibility boundary late in the recursion.</p>
<p><code>cvine_lookahead</code> carries intervals for the unsampled conditional correlations. Before selecting the current partial correlation <img src="https://latex.codecogs.com/png.latex?x">, it uses the exact <img src="https://latex.codecogs.com/png.latex?3%5Ctimes3"> condition</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A1%20+%202xyr%20-%20x%5E2%20-%20y%5E2%20-%20r%5E2%20%5Cge%200%0A"></p>
<p>to project every later pair onto a feasible interval for <img src="https://latex.codecogs.com/png.latex?x">. After selecting <img src="https://latex.codecogs.com/png.latex?x">, it tightens the later intervals and continues. Fixed ordinary correlations are point intervals, so zeros take no raw parameter in either model. The new transform uses the reduced Jacobian from raw values to the free ordinary correlations and adds the LKJ kernel as <img src="https://latex.codecogs.com/png.latex?(%5Ceta-1)%5Clog%5Cdet(%5COmega)">; it therefore targets the same conditional LKJ distribution as <code>cvine</code>.</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?K=7">, <img src="https://latex.codecogs.com/png.latex?%5Ceta=4">, five patterns</th>
<th style="text-align: right;"><code>cvine</code></th>
<th style="text-align: right;"><code>cvine_lookahead</code></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">total divergences</td>
<td style="text-align: right;">501</td>
<td style="text-align: right;"><strong>8</strong></td>
</tr>
<tr class="even">
<td style="text-align: left;">median minimum bulk ESS</td>
<td style="text-align: right;">10,219</td>
<td style="text-align: right;"><strong>10,454</strong></td>
</tr>
<tr class="odd">
<td style="text-align: left;">median ESS / 1,000 gradients</td>
<td style="text-align: right;">188.9</td>
<td style="text-align: right;"><strong>194.1</strong></td>
</tr>
<tr class="even">
<td style="text-align: left;">total wall clock (s)</td>
<td style="text-align: right;">5.14</td>
<td style="text-align: right;"><strong>2.65</strong></td>
</tr>
</tbody>
</table>
<p>The scaling sweep tells the same story. The original initializes in 12 of 20 configurations and fails for every <img src="https://latex.codecogs.com/png.latex?K=15"> and <img src="https://latex.codecogs.com/png.latex?K=25"> fit. <code>cvine_lookahead</code> initializes in 16 of 20 and reaches <img src="https://latex.codecogs.com/png.latex?K=15">. On the 12 configurations where both initialize, divergences fall from 20,842 to 993, median minimum ESS rises from 6,447 to 7,876, and median ESS per 1,000 gradients rises from 84.3 to 152.4. It still fails under default random initialization at <img src="https://latex.codecogs.com/png.latex?K=25">: enforcing every local <img src="https://latex.codecogs.com/png.latex?3%5Ctimes3"> condition is necessary, but for <img src="https://latex.codecogs.com/png.latex?K%5Cge4"> it is not sufficient for a globally feasible completion satisfying all remaining zero constraints.</p>
</section>
<section id="how-far-can-3times3-look-ahead-propagate" class="level2">
<h2 class="anchored" data-anchor-id="how-far-can-3times3-look-ahead-propagate">7. How Far Can <img src="https://latex.codecogs.com/png.latex?3%5Ctimes3"> Look-Ahead Propagate?</h2>
<p>The gist implementation already has maximum <em>breadth</em>: at each C-vine pair <img src="https://latex.codecogs.com/png.latex?(i,j)"> it checks every future <img src="https://latex.codecogs.com/png.latex?k%3Ej">, not only <img src="https://latex.codecogs.com/png.latex?k=j+1">. What it does not do is repeatedly propagate every narrowing through other triangles. A bound narrowed in one triangle can narrow another edge in a second triangle, which can then feed a third. To test that depth, I added an experimental transform that sweeps all currently active triples symmetrically for a fixed number of passes before each sequential coordinate. Each synchronous pass reads the previous pass’s intervals, intersects all three projected edges, and never moves an equality or an already sampled coordinate.</p>
<p>The bounds still depend only on earlier raw coordinates and unsampled boxes. The transformation remains triangular, so the Jacobian needs only the current interval width, logistic derivative, and the existing C-vine scale factor; there is no additional “projection Jacobian.” Fixed pass counts also avoid a tolerance-dependent stopping rule inside HMC.</p>
<p>The next table uses the same five <img src="https://latex.codecogs.com/png.latex?K=7">, <img src="https://latex.codecogs.com/png.latex?%5Ceta=4"> patterns and sampler settings. Spans 1 through 5 limit both the pre-selection intersection and post-selection tightening to that many future indices. <code>all</code> reaches the maximum possible distance of 6 and is <code>cvine_lookahead</code>. The sweep rows retain all-future breadth and add synchronous all-triple propagation before every coordinate. For <code>rowscale</code>, pass 0 and pass 1 are the same executable with propagation disabled or enabled, which controls for floating-point changes between Stan programs.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 17%">
<col style="width: 15%">
<col style="width: 26%">
<col style="width: 15%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">experiment</th>
<th style="text-align: right;">total divergences</th>
<th style="text-align: right;">median min ESS</th>
<th style="text-align: right;">median ESS / 1k gradients</th>
<th style="text-align: right;">total time (s)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">C-vine, span 1</td>
<td style="text-align: right;">206</td>
<td style="text-align: right;">11,153</td>
<td style="text-align: right;">214.5</td>
<td style="text-align: right;">3.80</td>
</tr>
<tr class="even">
<td style="text-align: left;">C-vine, span 2</td>
<td style="text-align: right;">43</td>
<td style="text-align: right;">10,562</td>
<td style="text-align: right;">205.7</td>
<td style="text-align: right;">3.06</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C-vine, span 3</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">10,454</td>
<td style="text-align: right;">194.1</td>
<td style="text-align: right;">2.93</td>
</tr>
<tr class="even">
<td style="text-align: left;">C-vine, span 4</td>
<td style="text-align: right;"><strong>8</strong></td>
<td style="text-align: right;">10,454</td>
<td style="text-align: right;">194.1</td>
<td style="text-align: right;"><strong>2.84</strong></td>
</tr>
<tr class="odd">
<td style="text-align: left;">C-vine, span 5</td>
<td style="text-align: right;"><strong>8</strong></td>
<td style="text-align: right;">10,454</td>
<td style="text-align: right;">194.1</td>
<td style="text-align: right;">3.05</td>
</tr>
<tr class="even">
<td style="text-align: left;">C-vine, all future <img src="https://latex.codecogs.com/png.latex?k"></td>
<td style="text-align: right;"><strong>8</strong></td>
<td style="text-align: right;">10,454</td>
<td style="text-align: right;">194.1</td>
<td style="text-align: right;">2.99</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C-vine, all + 1 sweep</td>
<td style="text-align: right;"><strong>7</strong></td>
<td style="text-align: right;"><strong>11,255</strong></td>
<td style="text-align: right;"><strong>219.4</strong></td>
<td style="text-align: right;">19.89</td>
</tr>
<tr class="even">
<td style="text-align: left;">C-vine, all + 2 sweeps</td>
<td style="text-align: right;"><strong>7</strong></td>
<td style="text-align: right;"><strong>11,255</strong></td>
<td style="text-align: right;"><strong>219.4</strong></td>
<td style="text-align: right;">37.96</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C-vine, all + 4 sweeps</td>
<td style="text-align: right;"><strong>7</strong></td>
<td style="text-align: right;"><strong>11,255</strong></td>
<td style="text-align: right;"><strong>219.4</strong></td>
<td style="text-align: right;">74.42</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>rowscale</code>, 0 sweeps</td>
<td style="text-align: right;">362</td>
<td style="text-align: right;">9,567</td>
<td style="text-align: right;">174.8</td>
<td style="text-align: right;"><strong>4.54</strong></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>rowscale</code>, 1 sweep</td>
<td style="text-align: right;"><strong>207</strong></td>
<td style="text-align: right;"><strong>10,334</strong></td>
<td style="text-align: right;"><strong>188.6</strong></td>
<td style="text-align: right;">9.11</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>rowscale</code>, 2 sweeps</td>
<td style="text-align: right;"><strong>207</strong></td>
<td style="text-align: right;"><strong>10,334</strong></td>
<td style="text-align: right;"><strong>188.6</strong></td>
<td style="text-align: right;">16.67</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>rowscale</code>, 4 sweeps</td>
<td style="text-align: right;"><strong>207</strong></td>
<td style="text-align: right;"><strong>10,334</strong></td>
<td style="text-align: right;"><strong>188.6</strong></td>
<td style="text-align: right;">31.39</td>
</tr>
</tbody>
</table>
<p>Breadth is the useful part for the C-vine: nearest-neighbor look-ahead leaves 206 divergences, two-step leaves 43, three-step leaves 9, and spans 4, 5, and all future indices agree at 8. One synchronous all-triple pass before each coordinate changes the C-vine path slightly and removes one more divergence, but costs nearly seven times as much; passes 2 and 4 produce exactly the same diagnostics and ESS as pass 1 while only multiplying run time. The naive repeated sweep is <img src="https://latex.codecogs.com/png.latex?O(pK%5E5)"> for <img src="https://latex.codecogs.com/png.latex?p"> passes, so it is an experiment rather than a recommended default.</p>
<p>The projection also transfers to an entry-wise Cholesky transform, provided it is applied to ordinary correlations rather than to Cholesky entries. For <code>rowscale</code>, I maintain boxes for <img src="https://latex.codecogs.com/png.latex?%5COmega_%7Bij%7D">, sweep all same-row pairs before each coordinate against the already known <img src="https://latex.codecogs.com/png.latex?%5COmega_%7Bjk%7D">, then map the contracted interval back through <img src="https://latex.codecogs.com/png.latex?%5COmega_%7Bij%7D=b_1+L_%7Bij%7DL_%7Bjj%7D">. One pass cuts divergences from 362 to 207 and improves median efficiency; additional passes saturate immediately. It helps, but unlike the subspace construction it does not remove the nonlocal constrained-completion boundary.</p>
<p>I then ported the same one-pass ordinary-correlation sweep to six entry-wise transforms from the bake-off. At a common parity-check point, pass-zero mode reproduced each original log density and full raw-gradient vector to printed precision. The table compares pass zero with pass one in those same executables; this is a cleaner intervention than comparing separately compiled programs. The contraction happens on <img src="https://latex.codecogs.com/png.latex?%5COmega">, after which each transform retains its existing Cholesky or <img src="https://latex.codecogs.com/png.latex?LDL'"> scale and equality Jacobians.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 9%">
<col style="width: 27%">
<col style="width: 25%">
<col style="width: 37%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">model</th>
<th style="text-align: center;">successful patterns, 0 <img src="https://latex.codecogs.com/png.latex?%5Cto"> 1 pass</th>
<th style="text-align: right;">divergences, 0 <img src="https://latex.codecogs.com/png.latex?%5Cto"> 1 pass</th>
<th style="text-align: right;">median ESS / 1k gradients, 0 <img src="https://latex.codecogs.com/png.latex?%5Cto"> 1 pass</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>carpenter</code></td>
<td style="text-align: center;">5 <img src="https://latex.codecogs.com/png.latex?%5Cto"> 5</td>
<td style="text-align: right;">398 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>195</strong></td>
<td style="text-align: right;">199.3 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>202.6</strong></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>pinkney</code></td>
<td style="text-align: center;">5 <img src="https://latex.codecogs.com/png.latex?%5Cto"> 5</td>
<td style="text-align: right;">395 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>322</strong></td>
<td style="text-align: right;">192.3 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>199.1</strong></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>ldl2</code></td>
<td style="text-align: center;">5 <img src="https://latex.codecogs.com/png.latex?%5Cto"> 5</td>
<td style="text-align: right;">389 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>275</strong></td>
<td style="text-align: right;">187.7 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>190.3</strong></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>ldl3</code></td>
<td style="text-align: center;">5 <img src="https://latex.codecogs.com/png.latex?%5Cto"> 5</td>
<td style="text-align: right;">370 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>230</strong></td>
<td style="text-align: right;">182.1 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>190.9</strong></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>rowscale</code></td>
<td style="text-align: center;">5 <img src="https://latex.codecogs.com/png.latex?%5Cto"> 5</td>
<td style="text-align: right;">362 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>207</strong></td>
<td style="text-align: right;">174.8 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>188.6</strong></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>schur</code></td>
<td style="text-align: center;">4 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>5</strong></td>
<td style="text-align: right;">224 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>193</strong><sup>2</sup></td>
<td style="text-align: right;">191.9 <img src="https://latex.codecogs.com/png.latex?%5Cto"> <strong>202.2</strong></td>
</tr>
</tbody>
</table>
<p>Across the 29 matched model-pattern pairs, one sweep reduces divergences from 2,138 to 1,422, raises median minimum ESS from 10,222 to 10,334, and raises median ESS per 1,000 gradients from 189.7 to 190.9. Wall time doubles from 25.9 to 52.1 seconds. The direction is favorable for every model in aggregate, but the gain is much smaller than for <code>cvine_lookahead</code>, and individual fits can move the other way because HMC trajectories are chaotic near the boundary. Local <img src="https://latex.codecogs.com/png.latex?3%5Ctimes3"> pruning is therefore useful as a guardrail for the entry-wise family, not a replacement for a parameterization that is feasible by construction.</p>
<p>On a held-out <img src="https://latex.codecogs.com/png.latex?K=5"> pattern, the six one-pass posteriors also agree with the <code>carpenter</code> baseline: the largest free-cell mean difference is 0.016, the largest difference is 2.85 combined Monte Carlo standard errors, and the constrained cells remain zero to about <img src="https://latex.codecogs.com/png.latex?10%5E%7B-19%7D">. The experimental Stan sources, benchmark scripts, and raw result CSVs are stored alongside this post.</p>
</section>
<section id="does-it-hold-up-scaling-k-and-eta" class="level2">
<h2 class="anchored" data-anchor-id="does-it-hold-up-scaling-k-and-eta">8. Does It Hold Up? Scaling K and <img src="https://latex.codecogs.com/png.latex?%5Ceta"></h2>
<p>I next repeat the comparison for <img src="https://latex.codecogs.com/png.latex?K%5Cin%5C%7B5,7,9,15,25%5C%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Ceta%5Cin%5C%7B1,2,3,4%5C%7D">. This sweep includes five entry-wise models, both C-vine models, <code>qr2</code>, <code>tri</code>, and the donut model with <img src="https://latex.codecogs.com/png.latex?m=6">. For each <img src="https://latex.codecogs.com/png.latex?K">, I generate one Bernoulli(0.5) zero pattern and reuse it for all four values of <img src="https://latex.codecogs.com/png.latex?%5Ceta">. The realized number of zeros ranges from 6 of 10 cells at <img src="https://latex.codecogs.com/png.latex?K=5"> to 143 of 300 at <img src="https://latex.codecogs.com/png.latex?K=25">. Sampler settings are unchanged. There is one fit per configuration, so small differences should be interpreted with the variability from Section 2 in mind.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">library</span>(ggplot2)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../R/theme_blog.R"</span>)</span>
<span id="cb2-6"></span>
<span id="cb2-7">sweep <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;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sweep-results.csv"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb2-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb2-10">      model <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tri"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (triangular basis)"</span>,</span>
<span id="cb2-11">      model <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qr2"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (QR basis)"</span>,</span>
<span id="cb2-12">      model <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"donut"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (donut)"</span>,</span>
<span id="cb2-13">      model <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cvine_lookahead"</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C-vine (look-ahead)"</span>,</span>
<span id="cb2-14">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.default =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forced zeros"</span></span>
<span id="cb2-15">    ),</span>
<span id="cb2-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">div_pct =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> divergences <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;">8000</span>,</span>
<span id="cb2-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta_lab =</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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"eta == "</span>, eta),</span>
<span id="cb2-18">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"eta == "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(eta))))</span>
<span id="cb2-19">  )</span>
<span id="cb2-20"></span>
<span id="cb2-21">fam_colors <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;">c</span>(</span>
<span id="cb2-22">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forced zeros"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>gold,</span>
<span id="cb2-23">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C-vine (look-ahead)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>red,</span>
<span id="cb2-24">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (QR basis)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple,</span>
<span id="cb2-25">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (donut)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blue,</span>
<span id="cb2-26">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (triangular basis)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal</span>
<span id="cb2-27">)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(sweep <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>failed),</span>
<span id="cb3-2">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> div_pct, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> family)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> family, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> family, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> family)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>eta_lab, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labeller =</span> label_parsed) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> fam_colors, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_linewidth_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forced zeros"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C-vine (look-ahead)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (QR basis)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>,</span>
<span id="cb3-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (donut)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (triangular basis)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.3</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_alpha_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forced zeros"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.65</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C-vine (look-ahead)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (QR basis)"</span> <span class="ot" style="color: #003B4F;
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-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (donut)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (triangular basis)"</span> <span class="ot" style="color: #003B4F;
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>
<span id="cb3-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_linetype_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb3-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forced zeros"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C-vine (look-ahead)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (QR basis)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>,</span>
<span id="cb3-15">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (donut)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"22"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (triangular basis)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</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;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb3-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K (matrix dimension)"</span>,</span>
<span id="cb3-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Divergent transitions (%)"</span>,</span>
<span id="cb3-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Divergence Rate by Matrix Dimension"</span>,</span>
<span id="cb3-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"One Bernoulli(0.5) zero pattern per K and 8,000 post-warmup transitions per fit"</span></span>
<span id="cb3-23">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-divergence-scaling" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-divergence-scaling-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-11-corr-zeros-bakeoff/corr-zeros-bakeoff_files/figure-html/fig-divergence-scaling-1.png" class="img-fluid figure-img" style="width:100.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-divergence-scaling-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Divergence rate by matrix dimension and LKJ concentration. Thin gold lines are the original sequential transforms and the red line is cvine_lookahead; missing points indicate initialization failures. The tri model has zero divergences in every panel. For qr2 at K = 15 and eta = 2, one chain has 100% divergences. The donut model remains below 1.5% throughout.
</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">ggplot</span>(sweep <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>failed),</span>
<span id="cb4-2">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ess_per_1k_grad, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> family)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> family, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> family, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> family)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>eta_lab, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labeller =</span> label_parsed) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> fam_colors, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_linewidth_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb4-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forced zeros"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C-vine (look-ahead)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (QR basis)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>,</span>
<span id="cb4-9">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (donut)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (triangular basis)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.3</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_alpha_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb4-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forced zeros"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.65</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C-vine (look-ahead)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (QR basis)"</span> <span class="ot" style="color: #003B4F;
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-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (donut)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (triangular basis)"</span> <span class="ot" style="color: #003B4F;
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>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_linetype_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">guide =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb4-14">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forced zeros"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C-vine (look-ahead)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (QR basis)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>,</span>
<span id="cb4-15">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (donut)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"22"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Subspace (triangular basis)"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</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;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb4-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K (matrix dimension)"</span>,</span>
<span id="cb4-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"min ESS per 1,000 gradients (log)"</span>,</span>
<span id="cb4-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sampling Efficiency by Matrix Dimension"</span>,</span>
<span id="cb4-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Higher is better; missing points indicate initialization failures"</span></span>
<span id="cb4-24">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-efficiency-scaling" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-efficiency-scaling-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-11-corr-zeros-bakeoff/corr-zeros-bakeoff_files/figure-html/fig-efficiency-scaling-1.png" class="img-fluid figure-img" style="width:100.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-efficiency-scaling-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Sampling efficiency (min bulk ESS over free correlations per 1,000 gradient evaluations, log scale) by dimension and concentration.
</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Outright failures: initialization could not find a feasible point after 100 attempts per chain.</caption>
<thead>
<tr class="header">
<th style="text-align: left;">model</th>
<th style="text-align: left;">K = 25</th>
<th style="text-align: left;">K = 15</th>
<th style="text-align: left;">K = 7</th>
<th style="text-align: left;">K = 9</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">carpenter</td>
<td style="text-align: left;">4 of 4 etas</td>
<td style="text-align: left;">—</td>
<td style="text-align: left;">—</td>
<td style="text-align: left;">—</td>
</tr>
<tr class="even">
<td style="text-align: left;">cvine</td>
<td style="text-align: left;">4 of 4 etas</td>
<td style="text-align: left;">4 of 4 etas</td>
<td style="text-align: left;">—</td>
<td style="text-align: left;">—</td>
</tr>
<tr class="odd">
<td style="text-align: left;">cvine_lookahead</td>
<td style="text-align: left;">4 of 4 etas</td>
<td style="text-align: left;">—</td>
<td style="text-align: left;">—</td>
<td style="text-align: left;">—</td>
</tr>
<tr class="even">
<td style="text-align: left;">ldl2</td>
<td style="text-align: left;">4 of 4 etas</td>
<td style="text-align: left;">—</td>
<td style="text-align: left;">—</td>
<td style="text-align: left;">—</td>
</tr>
<tr class="odd">
<td style="text-align: left;">ldl3</td>
<td style="text-align: left;">4 of 4 etas</td>
<td style="text-align: left;">—</td>
<td style="text-align: left;">—</td>
<td style="text-align: left;">—</td>
</tr>
<tr class="even">
<td style="text-align: left;">pinkney</td>
<td style="text-align: left;">4 of 4 etas</td>
<td style="text-align: left;">—</td>
<td style="text-align: left;">—</td>
<td style="text-align: left;">—</td>
</tr>
<tr class="odd">
<td style="text-align: left;">schur</td>
<td style="text-align: left;">4 of 4 etas</td>
<td style="text-align: left;">4 of 4 etas</td>
<td style="text-align: left;">4 of 4 etas</td>
<td style="text-align: left;">4 of 4 etas</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>At <img src="https://latex.codecogs.com/png.latex?%5Ceta=1">, the entry-wise models have 15–18% divergences for <img src="https://latex.codecogs.com/png.latex?K=5"> and 85–94% for <img src="https://latex.codecogs.com/png.latex?K=7"> and <img src="https://latex.codecogs.com/png.latex?K=9">. The uniform LKJ prior places more mass near the part of the parameter space where a solved Cholesky entry can exceed the remaining row norm. Four curves coincide in the <img src="https://latex.codecogs.com/png.latex?%5Ceta=1"> panel because <code>carpenter</code>, <code>pinkney</code>, <code>ldl2</code>, and <code>ldl3</code> have the same density in their raw coordinates until floating-point differences separate their trajectories.</p>
<p>Initialization also becomes difficult for the sequential models. For these particular patterns, <code>schur</code> fails to initialize from <img src="https://latex.codecogs.com/png.latex?K=7"> onward and <code>cvine</code> fails from <img src="https://latex.codecogs.com/png.latex?K=15"> onward. The <code>cvine_lookahead</code> model initializes and samples through <img src="https://latex.codecogs.com/png.latex?K=15">, but still fails to find a feasible default initialization at <img src="https://latex.codecogs.com/png.latex?K=25">. The other entry-wise models also fail at <img src="https://latex.codecogs.com/png.latex?K=25">. This depends on the zero pattern—<code>schur</code> initialized for every pattern in Section 5—but the problem becomes more likely as the number of constraints increases. Every initial value in the subspace models satisfies the zero constraints by construction.</p>
<p>The <code>tri</code> model has zero divergences in all 20 configurations. The donut model remains below 1.5% divergences throughout, with small counts for <img src="https://latex.codecogs.com/png.latex?%5Ceta%5Cle2"> and none for <img src="https://latex.codecogs.com/png.latex?%5Ceta%5Cge3">. Through <img src="https://latex.codecogs.com/png.latex?K=15">, the minimum bulk ESS for <code>tri</code> remains near the number of posterior draws. At <img src="https://latex.codecogs.com/png.latex?K=25">, the minimum is taken over 157 free correlations and ranges from about 5,100 to 6,600 for <code>tri</code>. The donut model ranges from about 4,700 to 6,600. Both complete in seconds.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">sub_tab <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sweep <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;">filter</span>(model <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <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;">"tri"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"donut"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-3">  <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;">cell =</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;">"%s (%s)%s"</span>,</span>
<span id="cb5-4">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(min_ess), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">big.mark =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>),</span>
<span id="cb5-5">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(ess_per_1k_grad),</span>
<span id="cb5-6">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(divergences <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;">0</span>,</span>
<span id="cb5-7">                               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ["</span>, divergences, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" div]"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>)))</span>
<span id="cb5-8"></span>
<span id="cb5-9">sub_wide <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sub_tab <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(model, K, N_zero, eta, cell) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> eta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> cell,</span>
<span id="cb5-12">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_prefix =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"eta = "</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(</span>
<span id="cb5-14">    sub_tab <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-15">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(model, K) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-16">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</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;">wall clock (s)</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;">=</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;">"%.1f-%.1f"</span>,</span>
<span id="cb5-17">                                           <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(seconds), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(seconds)),</span>
<span id="cb5-18">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>),</span>
<span id="cb5-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <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;">"model"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"K"</span>)</span>
<span id="cb5-20">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</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;">zero / free cells</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(N_zero, <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;">choose</span>(K, <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> N_zero),</span>
<span id="cb5-22">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.after =</span> K) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>N_zero) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(K, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(model))</span>
<span id="cb5-25"></span>
<span id="cb5-26">knitr<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;">kable</span>(</span>
<span id="cb5-27">  sub_wide, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <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;">"l"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c"</span>),</span>
<span id="cb5-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tri and the donut (m_radius = 6) across the full sweep: min bulk ESS of 8,000 draws (ESS per 1,000 gradients in parentheses; divergent transitions in brackets where nonzero -- tri has none anywhere). Wall clock is the range across eta for 4 parallel chains."</span></span>
<span id="cb5-29">)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>tri and the donut (m_radius = 6) across the full sweep: min bulk ESS of 8,000 draws (ESS per 1,000 gradients in parentheses; divergent transitions in brackets where nonzero – tri has none anywhere). Wall clock is the range across eta for 4 parallel chains.</caption>
<colgroup>
<col style="width: 5%">
<col style="width: 2%">
<col style="width: 16%">
<col style="width: 18%">
<col style="width: 17%">
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 14%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">model</th>
<th style="text-align: right;">K</th>
<th style="text-align: center;">zero / free cells</th>
<th style="text-align: center;">eta = 1</th>
<th style="text-align: center;">eta = 2</th>
<th style="text-align: center;">eta = 3</th>
<th style="text-align: center;">eta = 4</th>
<th style="text-align: center;">wall clock (s)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">tri</td>
<td style="text-align: right;">5</td>
<td style="text-align: center;">6 / 4</td>
<td style="text-align: center;">6,839 (127)</td>
<td style="text-align: center;">8,619 (183)</td>
<td style="text-align: center;">8,129 (192)</td>
<td style="text-align: center;">8,530 (204)</td>
<td style="text-align: center;">0.1-0.3</td>
</tr>
<tr class="even">
<td style="text-align: left;">donut</td>
<td style="text-align: right;">5</td>
<td style="text-align: center;">6 / 4</td>
<td style="text-align: center;">2,364 (20) [23 div]</td>
<td style="text-align: center;">3,865 (66)</td>
<td style="text-align: center;">6,715 (120)</td>
<td style="text-align: center;">8,832 (172)</td>
<td style="text-align: center;">0.3-0.5</td>
</tr>
<tr class="odd">
<td style="text-align: left;">tri</td>
<td style="text-align: right;">7</td>
<td style="text-align: center;">7 / 14</td>
<td style="text-align: center;">6,922 (87)</td>
<td style="text-align: center;">7,572 (134)</td>
<td style="text-align: center;">10,480 (188)</td>
<td style="text-align: center;">10,832 (196)</td>
<td style="text-align: center;">0.4-0.5</td>
</tr>
<tr class="even">
<td style="text-align: left;">donut</td>
<td style="text-align: right;">7</td>
<td style="text-align: center;">7 / 14</td>
<td style="text-align: center;">2,031 (19) [42 div]</td>
<td style="text-align: center;">5,796 (70)</td>
<td style="text-align: center;">7,128 (117)</td>
<td style="text-align: center;">8,430 (151)</td>
<td style="text-align: center;">0.5-0.8</td>
</tr>
<tr class="odd">
<td style="text-align: left;">tri</td>
<td style="text-align: right;">9</td>
<td style="text-align: center;">13 / 23</td>
<td style="text-align: center;">6,419 (87)</td>
<td style="text-align: center;">6,707 (118)</td>
<td style="text-align: center;">7,424 (132)</td>
<td style="text-align: center;">8,221 (147)</td>
<td style="text-align: center;">0.6-0.7</td>
</tr>
<tr class="even">
<td style="text-align: left;">donut</td>
<td style="text-align: right;">9</td>
<td style="text-align: center;">13 / 23</td>
<td style="text-align: center;">5,315 (36) [54 div]</td>
<td style="text-align: center;">5,969 (74) [1 div]</td>
<td style="text-align: center;">6,977 (123)</td>
<td style="text-align: center;">6,834 (120)</td>
<td style="text-align: center;">0.7-1.5</td>
</tr>
<tr class="odd">
<td style="text-align: left;">tri</td>
<td style="text-align: right;">15</td>
<td style="text-align: center;">64 / 41</td>
<td style="text-align: center;">7,298 (67)</td>
<td style="text-align: center;">7,836 (134)</td>
<td style="text-align: center;">7,761 (127)</td>
<td style="text-align: center;">8,340 (149)</td>
<td style="text-align: center;">1.3-2.0</td>
</tr>
<tr class="even">
<td style="text-align: left;">donut</td>
<td style="text-align: right;">15</td>
<td style="text-align: center;">64 / 41</td>
<td style="text-align: center;">5,046 (41) [99 div]</td>
<td style="text-align: center;">7,532 (79)</td>
<td style="text-align: center;">6,515 (82)</td>
<td style="text-align: center;">7,523 (122)</td>
<td style="text-align: center;">1.8-3.1</td>
</tr>
<tr class="odd">
<td style="text-align: left;">tri</td>
<td style="text-align: right;">25</td>
<td style="text-align: center;">143 / 157</td>
<td style="text-align: center;">5,081 (42)</td>
<td style="text-align: center;">5,837 (51)</td>
<td style="text-align: center;">5,742 (50)</td>
<td style="text-align: center;">6,553 (55)</td>
<td style="text-align: center;">7.0-8.9</td>
</tr>
<tr class="even">
<td style="text-align: left;">donut</td>
<td style="text-align: right;">25</td>
<td style="text-align: center;">143 / 157</td>
<td style="text-align: center;">4,667 (29) [98 div]</td>
<td style="text-align: center;">5,547 (46) [2 div]</td>
<td style="text-align: center;">5,524 (46)</td>
<td style="text-align: center;">5,907 (50)</td>
<td style="text-align: center;">9.3-14.4</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The QR basis remains feasible when the entry-wise models fail, but it becomes expensive as <img src="https://latex.codecogs.com/png.latex?K"> grows. At <img src="https://latex.codecogs.com/png.latex?K=25">, <img src="https://latex.codecogs.com/png.latex?%5Ceta=1">, <code>qr2</code> takes 852 seconds compared with 8.9 seconds for <code>tri</code>. At <img src="https://latex.codecogs.com/png.latex?K=15">, <img src="https://latex.codecogs.com/png.latex?%5Ceta=2">, one <code>qr2</code> chain has 100% divergences and the minimum ESS is 7. The <code>ldl3</code> model has a similar isolated failure at <img src="https://latex.codecogs.com/png.latex?K=9">, <img src="https://latex.codecogs.com/png.latex?%5Ceta=2">, with a minimum ESS of 19. At <img src="https://latex.codecogs.com/png.latex?K=25">, the minimum ESS for <code>qr2</code> is about 3,500–4,100, compared with 5,100–6,600 for <code>tri</code>, and <code>qr2</code> requires many more gradient evaluations. <code>tri</code> has the highest ESS per gradient in 14 of the 20 configurations, <code>cvine_lookahead</code> in four, and <code>qr2</code> in two.</p>
</section>
<section id="when-to-use-which" class="level2">
<h2 class="anchored" data-anchor-id="when-to-use-which">9. When to Use Which</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Scenario</th>
<th style="text-align: left;">Recommendation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Structural zeros only</td>
<td style="text-align: left;">Use <code>tri</code></td>
</tr>
<tr class="even">
<td style="text-align: left;">Structural zeros at very large <img src="https://latex.codecogs.com/png.latex?K"></td>
<td style="text-align: left;">Use <code>tri</code>, but profile the per-row <img src="https://latex.codecogs.com/png.latex?d%5Ctimes%20d"> Cholesky factorizations</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Zeros plus other known correlation values</td>
<td style="text-align: left;">Use the entry-wise method <span class="citation" data-cites="pinkney2024 pinkneytalk">(Pinkney 2024a, 2024b)</span>; <code>tri</code> currently handles zeros only</td>
</tr>
<tr class="even">
<td style="text-align: left;">Zeros plus bounds on other correlations</td>
<td style="text-align: left;">Use the entry-wise method, which already supports bounded transforms <span class="citation" data-cites="pinkney2024 pinkneytalk">(Pinkney 2024a, 2024b)</span></td>
</tr>
</tbody>
</table>
<p>For structural zeros alone, I do not see a reason to prefer one of the entry-wise parameterizations. In these benchmarks <code>tri</code> is more reliable and at least as efficient. The entry-wise method remains the established option here for more general constraints. The underlying look-ahead C-vine also accepts nonzero equalities and bounded free correlations, but this zeros-only adapter does not benchmark those cases.</p>
</section>
<section id="discussion" class="level2">
<h2 class="anchored" data-anchor-id="discussion">10. Discussion</h2>
<p>The entry-wise construction solves each zero constraint but leaves the boundary of the feasible row norm in the parameter space. This produces divergences for the smaller examples and initialization failures for the larger ones. <code>cvine_lookahead</code> narrows sequential partial-correlation intervals using exact <img src="https://latex.codecogs.com/png.latex?3%5Ctimes3"> feasibility projections and performs much better, but dense patterns can still leave a nonlocal constrained-completion boundary. The subspace construction removes that boundary because every parameter value maps to a row satisfying the constraints.</p>
<p>The choice of basis is important. A complement obtained directly from QR is valid, but its coordinates change with the earlier rows and sampling becomes less efficient. The triangular basis fixes the free coordinates to matrix entries and only solves for the constrained coordinates. In these benchmarks, that gives zero divergences without sacrificing effective sample size or run time.</p>
<p>There are two limitations I have not addressed here. The general look-ahead C-vine also supports nonzero equalities and bounded free correlations, but this bake-off’s interface specializes it to exact zeros and does not compare those targets. Also, the small Cholesky factorization performed for each constrained row should be profiled for matrices with dimensions in the hundreds.</p>
</section>
<section id="acknowledgments" class="level2">
<h2 class="anchored" data-anchor-id="acknowledgments">Acknowledgments</h2>
<p>The exact-constraint subspace idea, zeros as orthogonality constraints with the sample lifted from a lower-dimensional hemisphere through the orthogonal complement, including the <img src="https://latex.codecogs.com/png.latex?%5Cdet%20R"> Jacobian factor, is due to Seth Axen <span class="citation" data-cites="axen2023">(Axen 2023)</span>, whose radial prior <span class="citation" data-cites="axen2022">(Axen 2022)</span> the donut variant relies on. The <code>tri</code> model is that idea with a canonical triangular choice of complement basis and the determinant factored analytically. Thanks to Bob Carpenter for the compact zeros-only formulation benchmarked here as <code>carpenter</code>, and for the Discourse conversation that prompted this bake-off. The C-vine construction rests on the vine and partial-correlation results of <span class="citation" data-cites="joe2006">Joe (2006)</span> and <span class="citation" data-cites="lewandowski2009">Lewandowski et al. (2009)</span>.</p>
</section>
<section id="appendix-stan-code-for-all-thirteen-parameterizations" class="level2">
<h2 class="anchored" data-anchor-id="appendix-stan-code-for-all-thirteen-parameterizations">Appendix: Stan Code for All Thirteen Parameterizations</h2>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true" href="">tri</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false" href="">donut</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false" href="">carpenter</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false" href="">pinkney</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-5-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-5" aria-controls="tabset-1-5" aria-selected="false" href="">ldl</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-6-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-6" aria-controls="tabset-1-6" aria-selected="false" href="">ldl2</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-7-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-7" aria-controls="tabset-1-7" aria-selected="false" href="">ldl3</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-8-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-8" aria-controls="tabset-1-8" aria-selected="false" href="">cvine</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-9-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-9" aria-controls="tabset-1-9" aria-selected="false" href="">cvine_lookahead</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-10-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-10" aria-controls="tabset-1-10" aria-selected="false" href="">rowscale</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-11-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-11" aria-controls="tabset-1-11" aria-selected="false" href="">schur</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-12-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-12" aria-controls="tabset-1-12" aria-selected="false" href="">qr</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-13-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-13" aria-controls="tabset-1-13" aria-selected="false" href="">qr2</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>unitvec-stereo-corr-zeros.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" data-filename="unitvec-stereo-corr-zeros.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb6-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cholesky_corr_constrain_stereo_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb6-3">                                              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros) {</span>
<span id="cb6-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Exact-constraint subspace geometry but with a canonical</span></span>
<span id="cb6-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// triangular basis instead of Householder QR: basis vector k is a unit</span></span>
<span id="cb6-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// at free column k with the row's forced columns filled in by</span></span>
<span id="cb6-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// back-substitution through the zero constraints, then orthonormalized</span></span>
<span id="cb6-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// by the Cholesky factor of its small Gram matrix. The basis is</span></span>
<span id="cb6-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// anchored to matrix entries and varies smoothly with earlier rows.</span></span>
<span id="cb6-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, K, K);</span>
<span id="cb6-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_idx <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="cb6-12">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx <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="cb6-13">    L[<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="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="cb6-14"></span>
<span id="cb6-15">    <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="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>K) {</span>
<span id="cb6-16">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n_z <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;">0</span>;</span>
<span id="cb6-17">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros)</span>
<span id="cb6-18">             <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_z, <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> i) {</span>
<span id="cb6-19">        n_z <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="cb6-20">      }</span>
<span id="cb6-21">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i <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> n_z;</span>
<span id="cb6-22"></span>
<span id="cb6-23">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// subsphere coordinates via the modified stereographic chart:</span></span>
<span id="cb6-24">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// z = sqrt(t + 2) / (t + 1) * y with t = y'y, diagonal 1 / (t + 1)</span></span>
<span id="cb6-25">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[d] z;</span>
<span id="cb6-26">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> r <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="cb6-27">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb6-28">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[d] y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> raw[raw_idx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(raw_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> d <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="cb6-29">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_self</span>(y);</span>
<span id="cb6-30">        z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(t <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> (t <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> y;</span>
<span id="cb6-31">        r <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> (t <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="cb6-32">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</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> (d <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(t <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> (d <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1p</span>(t);</span>
<span id="cb6-33">        raw_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> d;</span>
<span id="cb6-34">      }</span>
<span id="cb6-35"></span>
<span id="cb6-36">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (n_z <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;">0</span>) {</span>
<span id="cb6-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb6-38">          L[i, <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>d] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> z<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb6-39">        }</span>
<span id="cb6-40">        L[i, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r;</span>
<span id="cb6-41">      } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb6-42">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[n_z] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zc;</span>
<span id="cb6-43">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (m <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_z) {</span>
<span id="cb6-44">          zc[m] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> zeros[zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> m <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;">2</span>];</span>
<span id="cb6-45">        }</span>
<span id="cb6-46"></span>
<span id="cb6-47">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the joint-delta term -0.5 * log det(Gram of zero-partner rows)</span></span>
<span id="cb6-48">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// factors: splitting the partner matrix A by forced/free coords,</span></span>
<span id="cb6-49">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// A_Z is triangular with the partners' diagonals, and by Sylvester</span></span>
<span id="cb6-50">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// det(A'A) = prod(L[j,j])^2 * det(T'T)</span></span>
<span id="cb6-51">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (m <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_z) {</span>
<span id="cb6-52">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(L[zc[m], zc[m]]);</span>
<span id="cb6-53">        }</span>
<span id="cb6-54"></span>
<span id="cb6-55">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb6-56">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[d] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> free_cols;</span>
<span id="cb6-57">          {</span>
<span id="cb6-58">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> fc <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;">0</span>;</span>
<span id="cb6-59">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zp <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="cb6-60">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (c <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb6-61">              <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (zp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> n_z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zc[zp] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> c) {</span>
<span id="cb6-62">                zp <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="cb6-63">              } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb6-64">                fc <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="cb6-65">                free_cols[fc] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c;</span>
<span id="cb6-66">              }</span>
<span id="cb6-67">            }</span>
<span id="cb6-68">          }</span>
<span id="cb6-69">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// triangular basis: unit at each free column, forced columns</span></span>
<span id="cb6-70">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// filled by back-substitution so every column is orthogonal to</span></span>
<span id="cb6-71">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the zero-partner rows</span></span>
<span id="cb6-72">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[i <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>, d] T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, i <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>, d);</span>
<span id="cb6-73">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>d) {</span>
<span id="cb6-74">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">col</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> free_cols[k];</span>
<span id="cb6-75">            T[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">col</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;">1</span>;</span>
<span id="cb6-76">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (m <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_z) {</span>
<span id="cb6-77">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> zc[m];</span>
<span id="cb6-78">              <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">col</span>) {</span>
<span id="cb6-79">                T[j, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(L[j, <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>(j <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>)], T[<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>(j <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>), k])</span>
<span id="cb6-80">                          <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> L[j, j];</span>
<span id="cb6-81">              }</span>
<span id="cb6-82">            }</span>
<span id="cb6-83">          }</span>
<span id="cb6-84">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// T's free-column rows are the identity, so T'T = I + W'W with</span></span>
<span id="cb6-85">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// W the forced-column rows only</span></span>
<span id="cb6-86">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[n_z, d] W;</span>
<span id="cb6-87">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (m <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_z) {</span>
<span id="cb6-88">            W[m, ] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> T[zc[m], ];</span>
<span id="cb6-89">          }</span>
<span id="cb6-90">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// orthonormalize: v = T * S'^{-1} * z has ||v|| = ||z||;</span></span>
<span id="cb6-91">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// diagonal(S) doubles as the det(T'T) half of the delta term</span></span>
<span id="cb6-92">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[d, d] S <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cholesky_decompose</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_diag</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crossprod</span>(W), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>));</span>
<span id="cb6-93">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diagonal</span>(S)));</span>
<span id="cb6-94">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[d] t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mdivide_right_tri_low</span>(z<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>
<span id="cb6-95">          L[i, <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>(i <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> (T <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>;</span>
<span id="cb6-96">        }</span>
<span id="cb6-97">        L[i, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r;</span>
<span id="cb6-98">        zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> n_z;</span>
<span id="cb6-99">      }</span>
<span id="cb6-100">    }</span>
<span id="cb6-101">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> L;</span>
<span id="cb6-102">  }</span>
<span id="cb6-103">}</span>
<span id="cb6-104"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb6-105">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb6-106">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// concentration in LKJ Cholesky</span></span>
<span id="cb6-107">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb6-108">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular, row major order</span></span>
<span id="cb6-109">}</span>
<span id="cb6-110"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb6-111">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_zero] raw;</span>
<span id="cb6-112">}</span>
<span id="cb6-113"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb6-114">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L_Omega <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;">cholesky_corr_constrain_stereo_jacobian</span>(K, raw, zeros);</span>
<span id="cb6-115">}</span>
<span id="cb6-116"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb6-117">  L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lkj_corr_cholesky</span>(eta);</span>
<span id="cb6-118">}</span>
<span id="cb6-119"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb6-120">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Omega);</span>
<span id="cb6-121">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>unitvec-donut-corr-zeros.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" data-filename="unitvec-donut-corr-zeros.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb7-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb7-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cholesky_corr_constrain_donut_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb7-3">                                                <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros,</span>
<span id="cb7-4">                                                <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> m_radius) {</span>
<span id="cb7-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Exact-constraint subspace geometry with the triangular basis, but the</span></span>
<span id="cb7-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// subsphere is parameterized by normalizing a free vector ("donut"):</span></span>
<span id="cb7-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// one extra parameter per row, u = y / ||y||, with -0.5 * y'y keeping</span></span>
<span id="cb7-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the radius Gaussian and independent of the direction. The diagonal</span></span>
<span id="cb7-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// is |u_last| (the two mirror hemispheres fold onto the same L), and</span></span>
<span id="cb7-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the measure conversion dz = |u_last| dsigma contributes log|u_last|</span></span>
<span id="cb7-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// on top of the LKJ weight supplied by the model block.</span></span>
<span id="cb7-12">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, K, K);</span>
<span id="cb7-13">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_idx <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="cb7-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx <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="cb7-15">    L[<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="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="cb7-16"></span>
<span id="cb7-17">    <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="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>K) {</span>
<span id="cb7-18">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n_z <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;">0</span>;</span>
<span id="cb7-19">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros)</span>
<span id="cb7-20">             <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_z, <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> i) {</span>
<span id="cb7-21">        n_z <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="cb7-22">      }</span>
<span id="cb7-23">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i <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> n_z;</span>
<span id="cb7-24"></span>
<span id="cb7-25">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[d <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>] y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> raw[raw_idx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(raw_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> d)];</span>
<span id="cb7-26">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">norm2</span>(y);</span>
<span id="cb7-27">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[d <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>] u <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> s;</span>
<span id="cb7-28">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// radial prior centered at m_radius repels the norm from the</span></span>
<span id="cb7-29">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// origin singularity of the normalization (Axen, "A better unit</span></span>
<span id="cb7-30">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// vector", Stan Discourse post 30); m_radius = 0 recovers the</span></span>
<span id="cb7-31">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// implicit chi-distributed radius. The direction marginal, and</span></span>
<span id="cb7-32">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// hence the Omega posterior, is unchanged.</span></span>
<span id="cb7-33">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> m_radius) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(u[d <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="cb7-34">      L[i, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(u[d <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="cb7-35">      raw_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> d <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="cb7-36"></span>
<span id="cb7-37">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (n_z <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;">0</span>) {</span>
<span id="cb7-38">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb7-39">          L[i, <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>d] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> u[<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>d]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb7-40">        }</span>
<span id="cb7-41">      } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb7-42">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[n_z] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zc;</span>
<span id="cb7-43">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (m <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_z) {</span>
<span id="cb7-44">          zc[m] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> zeros[zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> m <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;">2</span>];</span>
<span id="cb7-45">        }</span>
<span id="cb7-46"></span>
<span id="cb7-47">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -0.5 * log det(Gram of zero-partner rows) from the joint delta</span></span>
<span id="cb7-48">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[i <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>, n_z] A;</span>
<span id="cb7-49">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (m <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_z) {</span>
<span id="cb7-50">          A[, m] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L[zc[m], <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>(i <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>;</span>
<span id="cb7-51">        }</span>
<span id="cb7-52">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diagonal</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cholesky_decompose</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crossprod</span>(A)))));</span>
<span id="cb7-53"></span>
<span id="cb7-54">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb7-55">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[d] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> free_cols;</span>
<span id="cb7-56">          {</span>
<span id="cb7-57">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> fc <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;">0</span>;</span>
<span id="cb7-58">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zp <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="cb7-59">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (c <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb7-60">              <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (zp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> n_z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zc[zp] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> c) {</span>
<span id="cb7-61">                zp <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="cb7-62">              } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb7-63">                fc <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="cb7-64">                free_cols[fc] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c;</span>
<span id="cb7-65">              }</span>
<span id="cb7-66">            }</span>
<span id="cb7-67">          }</span>
<span id="cb7-68">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// triangular basis: unit at each free column, forced columns</span></span>
<span id="cb7-69">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// filled by back-substitution so every column is orthogonal to</span></span>
<span id="cb7-70">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the zero-partner rows</span></span>
<span id="cb7-71">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[i <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>, d] T <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, i <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>, d);</span>
<span id="cb7-72">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>d) {</span>
<span id="cb7-73">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">col</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> free_cols[k];</span>
<span id="cb7-74">            T[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">col</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;">1</span>;</span>
<span id="cb7-75">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (m <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_z) {</span>
<span id="cb7-76">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> zc[m];</span>
<span id="cb7-77">              <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">col</span>) {</span>
<span id="cb7-78">                T[j, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(L[j, <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>(j <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>)], T[<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>(j <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>), k])</span>
<span id="cb7-79">                          <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> L[j, j];</span>
<span id="cb7-80">              }</span>
<span id="cb7-81">            }</span>
<span id="cb7-82">          }</span>
<span id="cb7-83">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// orthonormalize: v = T * S'^{-1} * u[1:d] has ||v|| = ||u[1:d]||</span></span>
<span id="cb7-84">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[d, d] S <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cholesky_decompose</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crossprod</span>(T));</span>
<span id="cb7-85">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[d] t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mdivide_right_tri_low</span>(u[<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>d]<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>
<span id="cb7-86">          L[i, <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>(i <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> (T <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>;</span>
<span id="cb7-87">        }</span>
<span id="cb7-88">        zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> n_z;</span>
<span id="cb7-89">      }</span>
<span id="cb7-90">    }</span>
<span id="cb7-91">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> L;</span>
<span id="cb7-92">  }</span>
<span id="cb7-93">}</span>
<span id="cb7-94"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb7-95">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb7-96">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// concentration in LKJ Cholesky</span></span>
<span id="cb7-97">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb7-98">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular, row major order</span></span>
<span id="cb7-99">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> m_radius;                     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// radial prior center; 0 = chi radius</span></span>
<span id="cb7-100">}</span>
<span id="cb7-101"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb7-102">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// one extra parameter per row: dim d + 1 per row sums to</span></span>
<span id="cb7-103">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// choose(K, 2) - N_zero + (K - 1)</span></span>
<span id="cb7-104">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_zero <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;">1</span>] raw;</span>
<span id="cb7-105">}</span>
<span id="cb7-106"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb7-107">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L_Omega <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;">cholesky_corr_constrain_donut_jacobian</span>(K, raw, zeros,</span>
<span id="cb7-108">                                                                m_radius);</span>
<span id="cb7-109">}</span>
<span id="cb7-110"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb7-111">  L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lkj_corr_cholesky</span>(eta);</span>
<span id="cb7-112">}</span>
<span id="cb7-113"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb7-114">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Omega);</span>
<span id="cb7-115">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>carpenter-corr-zeros.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" data-filename="carpenter-corr-zeros.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb8-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb8-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> is_zero(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> row_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> col_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx) {</span>
<span id="cb8-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros)</span>
<span id="cb8-4">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> row_idx</span>
<span id="cb8-5">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> col_idx;</span>
<span id="cb8-6">  }</span>
<span id="cb8-7">  </span>
<span id="cb8-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cholesky_corr_zeros_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> D,</span>
<span id="cb8-9">                                      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb8-10">                                      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros) {</span>
<span id="cb8-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_idx <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="cb8-12">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx <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="cb8-13">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[D, D] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, D, D);</span>
<span id="cb8-14">    <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="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>D) {</span>
<span id="cb8-15">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> stick <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="cb8-16">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb8-17">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (is_zero(i, j, zeros, zero_idx)) {</span>
<span id="cb8-18">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(L[j, <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>(j <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>)], L[i, <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>(j <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="cb8-19">          L[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> L[j, j];  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// implies Omega[i, j] == 0</span></span>
<span id="cb8-20">          zero_idx <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="cb8-21">        } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb8-22">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> sqrt_stick <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(stick);</span>
<span id="cb8-23">          L[i, j] <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;">lower_upper_bound_jacobian</span>(raw[raw_idx], <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sqrt_stick, sqrt_stick);  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// inside stick</span></span>
<span id="cb8-24">          raw_idx <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="cb8-25">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</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;">0</span>;</span>
<span id="cb8-26">        }</span>
<span id="cb8-27">    stick <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> L[i, j]<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="cb8-28">      }</span>
<span id="cb8-29">      L[i, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(stick);</span>
<span id="cb8-30">    }</span>
<span id="cb8-31">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> L;</span>
<span id="cb8-32">  }</span>
<span id="cb8-33"></span>
<span id="cb8-34">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lkj_cholesky_corr_zeros_lpdf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> L,</span>
<span id="cb8-35">                                    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> nu,</span>
<span id="cb8-36">                                    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros) {</span>
<span id="cb8-37">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lkj_corr_cholesky_lpdf(L | nu);  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// over-adjusts</span></span>
<span id="cb8-38">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N_zero <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros);</span>
<span id="cb8-39">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (n <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N_zero) {                      </span>
<span id="cb8-40">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> col_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> zeros[n, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>];</span>
<span id="cb8-41">      lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(L[col_idx, col_idx]);          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// correct over-adjustment</span></span>
<span id="cb8-42">    }</span>
<span id="cb8-43">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> lp;</span>
<span id="cb8-44">  }</span>
<span id="cb8-45">}</span>
<span id="cb8-46"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb8-47">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> D;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb8-48">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// concentration in LKJ Cholesky</span></span>
<span id="cb8-49">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(D, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb8-50">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular, row major order</span></span>
<span id="cb8-51">}</span>
<span id="cb8-52"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb8-53">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(D, <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> N_zero] raw;           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// raw parameters</span></span>
<span id="cb8-54">}</span>
<span id="cb8-55"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb8-56">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[D, D] L_Omega <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;">cholesky_corr_zeros_jacobian</span>(D, raw, zeros);</span>
<span id="cb8-57">}</span>
<span id="cb8-58"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb8-59">  L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span>lkj_cholesky_corr_zeros(eta, zeros);</span>
<span id="cb8-60">}</span>
<span id="cb8-61"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb8-62">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[D, D] Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Omega);</span>
<span id="cb8-63">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>pinkney-corr-zeros2.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" data-filename="pinkney-corr-zeros2.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb9-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb9-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> is_zero(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> row_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> col_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx) {</span>
<span id="cb9-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros)</span>
<span id="cb9-4">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> row_idx</span>
<span id="cb9-5">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> col_idx;</span>
<span id="cb9-6">  }</span>
<span id="cb9-7"></span>
<span id="cb9-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cholesky_corr_constrain_outer_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb9-9">                                                <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros) {</span>
<span id="cb9-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, K, K);</span>
<span id="cb9-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_idx <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="cb9-12">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx <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="cb9-13">    L[<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="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="cb9-14"></span>
<span id="cb9-15">    <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="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>K) {</span>
<span id="cb9-16">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// remaining radius of row i: sqrt(1 - sum of squares so far)</span></span>
<span id="cb9-17">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> l_ij_old <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="cb9-18">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb9-19">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> l_ij_old_x_l_jj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> l_ij_old <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> L[j, j];</span>
<span id="cb9-20">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> b1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(L[j, <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>(j <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>)], L[i, <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>(j <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="cb9-21">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// how to derive the bounds</span></span>
<span id="cb9-22">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// we know that the correlation value C is bound by</span></span>
<span id="cb9-23">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// b1 - Ljj * Lij_old &lt;= C &lt;= b1 + Ljj * Lij_old</span></span>
<span id="cb9-24">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Now we want our bounds to be enforced too so</span></span>
<span id="cb9-25">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// max(lb, b1 - Ljj * Lij_old) &lt;= C &lt;= min(ub, b1 + Ljj * Lij_old)</span></span>
<span id="cb9-26">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// We have Lij_new = (C - b1) / Ljj</span></span>
<span id="cb9-27">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> x;</span>
<span id="cb9-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (is_zero(i, j, zeros, zero_idx)) {</span>
<span id="cb9-29">          x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>b1;  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Omega[i, j] = b1 + Lij * Ljj = 0</span></span>
<span id="cb9-30">          zero_idx <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="cb9-31">        } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb9-32">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> low <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>({<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>l_ij_old_x_l_jj, <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> b1});</span>
<span id="cb9-33">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> up <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>({l_ij_old_x_l_jj, <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> b1});</span>
<span id="cb9-34">          x <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;">lower_upper_bound_jacobian</span>(raw[raw_idx], low, up);</span>
<span id="cb9-35">          raw_idx <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="cb9-36">        }</span>
<span id="cb9-37">        L[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> L[j, j];</span>
<span id="cb9-38">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// free entries: change of variables x -&gt; L[i, j]</span></span>
<span id="cb9-39">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// zero entries: Carpenter's correction for the LKJ over-adjustment</span></span>
<span id="cb9-40">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(L[j, j]);</span>
<span id="cb9-41">        l_ij_old <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> (L[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> l_ij_old)<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="cb9-42">      }</span>
<span id="cb9-43">      L[i, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> l_ij_old;</span>
<span id="cb9-44">    }</span>
<span id="cb9-45">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> L;</span>
<span id="cb9-46">  }</span>
<span id="cb9-47">}</span>
<span id="cb9-48"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb9-49">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb9-50">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// concentration in LKJ Cholesky</span></span>
<span id="cb9-51">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb9-52">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular, row major order</span></span>
<span id="cb9-53">}</span>
<span id="cb9-54"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb9-55">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_zero] raw;</span>
<span id="cb9-56">}</span>
<span id="cb9-57"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb9-58">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L_Omega <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;">cholesky_corr_constrain_outer_jacobian</span>(K, raw, zeros);</span>
<span id="cb9-59">}</span>
<span id="cb9-60"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb9-61">  L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lkj_corr_cholesky</span>(eta);</span>
<span id="cb9-62">}</span>
<span id="cb9-63"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb9-64">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Omega);</span>
<span id="cb9-65">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-5" class="tab-pane" aria-labelledby="tabset-1-5-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>pinkney-corr-zeros-ldl.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" data-filename="pinkney-corr-zeros-ldl.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb10-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb10-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> is_zero(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> row_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> col_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx) {</span>
<span id="cb10-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros)</span>
<span id="cb10-4">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> row_idx</span>
<span id="cb10-5">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> col_idx;</span>
<span id="cb10-6">  }</span>
<span id="cb10-7"></span>
<span id="cb10-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cholesky_corr_constrain_ldl_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb10-9">                                              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros) {</span>
<span id="cb10-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Omega = L * diag(D) * L' with unit lower-triangular L;</span></span>
<span id="cb10-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the Cholesky factor is diag_post_multiply(L, sqrt(D))</span></span>
<span id="cb10-12">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag_matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_vector</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, K));</span>
<span id="cb10-13">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] D;</span>
<span id="cb10-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] log_D;</span>
<span id="cb10-15">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_idx <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="cb10-16">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx <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="cb10-17">    D[<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> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb10-18">    log_D[<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> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>;</span>
<span id="cb10-19"></span>
<span id="cb10-20">    <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="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>K) {</span>
<span id="cb10-21">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// log remaining radius^2 of row i: log(1 - sum_{k&lt;j} L[i,k]^2 * D[k])</span></span>
<span id="cb10-22">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_r <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;">0</span>;</span>
<span id="cb10-23">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb10-24">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> b1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(D[<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>(j <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> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.*</span> L[j, <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>(j <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="cb10-25">                              L[i, <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>(j <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="cb10-26">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Omega[i, j] = b1 + L[i, j] * D[j] and the correlation C satisfies</span></span>
<span id="cb10-27">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// |C - b1| &lt;= sqrt(D[j] * r), intersected with (-1, 1)</span></span>
<span id="cb10-28">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> x;</span>
<span id="cb10-29">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (is_zero(i, j, zeros, zero_idx)) {</span>
<span id="cb10-30">          x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>b1;  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Omega[i, j] = b1 + Lij * Dj = 0</span></span>
<span id="cb10-31">          zero_idx <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="cb10-32">        } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb10-33">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> b2 <span class="op" 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="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> (log_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> log_D[j]));</span>
<span id="cb10-34">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> low <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>({<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>b2, <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> b1});</span>
<span id="cb10-35">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> up <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>({b2, <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> b1});</span>
<span id="cb10-36">          x <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;">lower_upper_bound_jacobian</span>(raw[raw_idx], low, up);</span>
<span id="cb10-37">          raw_idx <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="cb10-38">        }</span>
<span id="cb10-39">        L[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D[j];</span>
<span id="cb10-40">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// free entries: change of variables x -&gt; L_chol[i, j] = x / sqrt(D[j])</span></span>
<span id="cb10-41">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// zero entries: Carpenter's correction -log(L_chol[j, j])</span></span>
<span id="cb10-42">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</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> log_D[j];</span>
<span id="cb10-43">        log_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log_diff_exp</span>(log_r, log_D[j] <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(L[i, j])));</span>
<span id="cb10-44">      }</span>
<span id="cb10-45">      log_D[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> log_r;</span>
<span id="cb10-46">      D[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(log_r);</span>
<span id="cb10-47">    }</span>
<span id="cb10-48">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag_post_multiply</span>(L, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(D));</span>
<span id="cb10-49">  }</span>
<span id="cb10-50">}</span>
<span id="cb10-51"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb10-52">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb10-53">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// concentration in LKJ Cholesky</span></span>
<span id="cb10-54">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb10-55">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular, row major order</span></span>
<span id="cb10-56">}</span>
<span id="cb10-57"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb10-58">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_zero] raw;</span>
<span id="cb10-59">}</span>
<span id="cb10-60"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb10-61">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L_Omega <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;">cholesky_corr_constrain_ldl_jacobian</span>(K, raw, zeros);</span>
<span id="cb10-62">}</span>
<span id="cb10-63"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb10-64">  L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lkj_corr_cholesky</span>(eta);</span>
<span id="cb10-65">}</span>
<span id="cb10-66"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb10-67">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Omega);</span>
<span id="cb10-68">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-6" class="tab-pane" aria-labelledby="tabset-1-6-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>pinkney-corr-zeros-ldl2.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" data-filename="pinkney-corr-zeros-ldl2.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb11-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb11-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> is_zero(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> row_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> col_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx) {</span>
<span id="cb11-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros)</span>
<span id="cb11-4">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> row_idx</span>
<span id="cb11-5">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> col_idx;</span>
<span id="cb11-6">  }</span>
<span id="cb11-7"></span>
<span id="cb11-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cholesky_corr_constrain_ldl2_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb11-9">                                               <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros) {</span>
<span id="cb11-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Omega = L * diag(D) * L' with unit lower-triangular L;</span></span>
<span id="cb11-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the Cholesky factor is diag_post_multiply(L, sqrt(D)).</span></span>
<span id="cb11-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Same as ldl version except the remaining radius is updated with</span></span>
<span id="cb11-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// log1m of the ratio instead of log_diff_exp.</span></span>
<span id="cb11-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag_matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_vector</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, K));</span>
<span id="cb11-15">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] D;</span>
<span id="cb11-16">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] log_D;</span>
<span id="cb11-17">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_idx <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="cb11-18">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx <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="cb11-19">    D[<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> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb11-20">    log_D[<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> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>;</span>
<span id="cb11-21"></span>
<span id="cb11-22">    <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="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>K) {</span>
<span id="cb11-23">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// log remaining radius^2 of row i: log(1 - sum_{k&lt;j} L[i,k]^2 * D[k])</span></span>
<span id="cb11-24">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_r <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;">0</span>;</span>
<span id="cb11-25">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb11-26">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> b1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(D[<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>(j <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> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.*</span> L[j, <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>(j <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="cb11-27">                              L[i, <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>(j <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="cb11-28">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Omega[i, j] = b1 + L[i, j] * D[j] and the correlation C satisfies</span></span>
<span id="cb11-29">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// |C - b1| &lt;= sqrt(D[j] * r), intersected with (-1, 1)</span></span>
<span id="cb11-30">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> x;</span>
<span id="cb11-31">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (is_zero(i, j, zeros, zero_idx)) {</span>
<span id="cb11-32">          x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>b1;  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Omega[i, j] = b1 + Lij * Dj = 0</span></span>
<span id="cb11-33">          zero_idx <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="cb11-34">        } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb11-35">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> b2 <span class="op" 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="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> (log_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> log_D[j]));</span>
<span id="cb11-36">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> low <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>({<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>b2, <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> b1});</span>
<span id="cb11-37">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> up <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>({b2, <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> b1});</span>
<span id="cb11-38">          x <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;">lower_upper_bound_jacobian</span>(raw[raw_idx], low, up);</span>
<span id="cb11-39">          raw_idx <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="cb11-40">        }</span>
<span id="cb11-41">        L[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D[j];</span>
<span id="cb11-42">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// free entries: change of variables x -&gt; L_chol[i, j] = x / sqrt(D[j])</span></span>
<span id="cb11-43">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// zero entries: Carpenter's correction -log(L_chol[j, j])</span></span>
<span id="cb11-44">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</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> log_D[j];</span>
<span id="cb11-45">        log_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(D[j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> L[i, j]<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(log_r));</span>
<span id="cb11-46">      }</span>
<span id="cb11-47">      log_D[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> log_r;</span>
<span id="cb11-48">      D[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(log_r);</span>
<span id="cb11-49">    }</span>
<span id="cb11-50">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag_post_multiply</span>(L, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(D));</span>
<span id="cb11-51">  }</span>
<span id="cb11-52">}</span>
<span id="cb11-53"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb11-54">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb11-55">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// concentration in LKJ Cholesky</span></span>
<span id="cb11-56">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb11-57">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular, row major order</span></span>
<span id="cb11-58">}</span>
<span id="cb11-59"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb11-60">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_zero] raw;</span>
<span id="cb11-61">}</span>
<span id="cb11-62"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb11-63">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L_Omega <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;">cholesky_corr_constrain_ldl2_jacobian</span>(K, raw, zeros);</span>
<span id="cb11-64">}</span>
<span id="cb11-65"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb11-66">  L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lkj_corr_cholesky</span>(eta);</span>
<span id="cb11-67">}</span>
<span id="cb11-68"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb11-69">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Omega);</span>
<span id="cb11-70">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-7" class="tab-pane" aria-labelledby="tabset-1-7-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>pinkney-corr-zeros-ldl3.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" data-filename="pinkney-corr-zeros-ldl3.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb12-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb12-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> is_zero(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> row_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> col_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx) {</span>
<span id="cb12-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros)</span>
<span id="cb12-4">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> row_idx</span>
<span id="cb12-5">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> col_idx;</span>
<span id="cb12-6">  }</span>
<span id="cb12-7"></span>
<span id="cb12-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cholesky_corr_constrain_ldl3_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb12-9">                                               <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros) {</span>
<span id="cb12-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Omega = L * diag(D) * L' with unit lower-triangular L;</span></span>
<span id="cb12-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the Cholesky factor is diag_post_multiply(L, sqrt(D)).</span></span>
<span id="cb12-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Same as the ldl versions except the remaining radius is tracked on</span></span>
<span id="cb12-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the natural scale: r -= D[j] * L[i, j]^2.</span></span>
<span id="cb12-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag_matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_vector</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, K));</span>
<span id="cb12-15">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] D;</span>
<span id="cb12-16">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_idx <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="cb12-17">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx <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="cb12-18">    D[<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> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>;</span>
<span id="cb12-19"></span>
<span id="cb12-20">    <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="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>K) {</span>
<span id="cb12-21">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// remaining radius^2 of row i: 1 - sum_{k&lt;j} L[i,k]^2 * D[k]</span></span>
<span id="cb12-22">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> r <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="cb12-23">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb12-24">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> b1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(D[<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>(j <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> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.*</span> L[j, <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>(j <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="cb12-25">                              L[i, <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>(j <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="cb12-26">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Omega[i, j] = b1 + L[i, j] * D[j] and the correlation C satisfies</span></span>
<span id="cb12-27">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// |C - b1| &lt;= sqrt(D[j] * r), intersected with (-1, 1)</span></span>
<span id="cb12-28">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> x;</span>
<span id="cb12-29">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (is_zero(i, j, zeros, zero_idx)) {</span>
<span id="cb12-30">          x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>b1;  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Omega[i, j] = b1 + Lij * Dj = 0</span></span>
<span id="cb12-31">          zero_idx <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="cb12-32">        } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb12-33">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> b2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> D[j]);</span>
<span id="cb12-34">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> low <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>({<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>b2, <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> b1});</span>
<span id="cb12-35">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> up <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>({b2, <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> b1});</span>
<span id="cb12-36">          x <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;">lower_upper_bound_jacobian</span>(raw[raw_idx], low, up);</span>
<span id="cb12-37">          raw_idx <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="cb12-38">        }</span>
<span id="cb12-39">        L[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> D[j];</span>
<span id="cb12-40">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// free entries: change of variables x -&gt; L_chol[i, j] = x / sqrt(D[j])</span></span>
<span id="cb12-41">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// zero entries: Carpenter's correction -log(L_chol[j, j])</span></span>
<span id="cb12-42">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(D[j]);</span>
<span id="cb12-43">        r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> D[j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> L[i, j]<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="cb12-44">      }</span>
<span id="cb12-45">      D[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r;</span>
<span id="cb12-46">    }</span>
<span id="cb12-47">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag_post_multiply</span>(L, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(D));</span>
<span id="cb12-48">  }</span>
<span id="cb12-49">}</span>
<span id="cb12-50"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb12-51">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb12-52">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// concentration in LKJ Cholesky</span></span>
<span id="cb12-53">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb12-54">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular, row major order</span></span>
<span id="cb12-55">}</span>
<span id="cb12-56"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb12-57">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_zero] raw;</span>
<span id="cb12-58">}</span>
<span id="cb12-59"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb12-60">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L_Omega <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;">cholesky_corr_constrain_ldl3_jacobian</span>(K, raw, zeros);</span>
<span id="cb12-61">}</span>
<span id="cb12-62"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb12-63">  L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lkj_corr_cholesky</span>(eta);</span>
<span id="cb12-64">}</span>
<span id="cb12-65"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb12-66">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Omega);</span>
<span id="cb12-67">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-8" class="tab-pane" aria-labelledby="tabset-1-8-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>cvine-corr-zeros.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" data-filename="cvine-corr-zeros.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb13-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb13-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// traversal here is tree-by-tree (column major), so scan instead of the</span></span>
<span id="cb13-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// merge-join used by the row-major models</span></span>
<span id="cb13-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> is_zero_scan(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> row_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> col_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros) {</span>
<span id="cb13-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (n <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros)) {</span>
<span id="cb13-6">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (zeros[n, <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> row_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[n, <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> col_idx) {</span>
<span id="cb13-7">        <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>
<span id="cb13-8">      }</span>
<span id="cb13-9">    }</span>
<span id="cb13-10">    <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;">0</span>;</span>
<span id="cb13-11">  }</span>
<span id="cb13-12"></span>
<span id="cb13-13">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">corr_constrain_cvine_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb13-14">                                       <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> eta) {</span>
<span id="cb13-15">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] C <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">identity_matrix</span>(K);</span>
<span id="cb13-16">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">identity_matrix</span>(K);</span>
<span id="cb13-17">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// LKJ in C-vine form: partial corr in tree i ~ Beta(a - i/2 adjustments);</span></span>
<span id="cb13-18">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// constant base a with the -0.5 * i * log1m(p^2) correction below</span></span>
<span id="cb13-19">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eta <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> (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;">1</span>);</span>
<span id="cb13-20">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_idx <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="cb13-21"></span>
<span id="cb13-22">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// tree 1: marginal correlations with variable 1</span></span>
<span id="cb13-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>K) {</span>
<span id="cb13-24">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (is_zero_scan(k, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, zeros)) {</span>
<span id="cb13-25">        P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</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;">0</span>;  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// prior terms at p = 0 are constants</span></span>
<span id="cb13-26">      } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb13-27">        P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, 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;">lower_upper_bound_jacobian</span>(raw[raw_idx], <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="cb13-28">        raw_idx <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="cb13-29">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> beta_lpdf(<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> (P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</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;">1</span>) | a, a)</span>
<span id="cb13-30">                    <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</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="cb13-31">      }</span>
<span id="cb13-32">      C[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, k];</span>
<span id="cb13-33">      C[k, <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> P[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, k];</span>
<span id="cb13-34">    }</span>
<span id="cb13-35"></span>
<span id="cb13-36">    <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="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>(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;">1</span>)) {</span>
<span id="cb13-37">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (i <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>K) {</span>
<span id="cb13-38">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[i <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>] b1;</span>
<span id="cb13-39">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[i <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>] b2;</span>
<span id="cb13-40">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i;</span>
<span id="cb13-41">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb13-42">          m <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="cb13-43">          b1[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> P[m, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> P[m, j];</span>
<span id="cb13-44">          b2[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> P[m, i]<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;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> P[m, j]<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="cb13-45">        }</span>
<span id="cb13-46">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// unwinding the partial correlation recursion is affine:</span></span>
<span id="cb13-47">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// C[i, j] = A + B * P[i, j]</span></span>
<span id="cb13-48">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> A <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;">0</span>;</span>
<span id="cb13-49">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> B <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="cb13-50">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb13-51">          A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> b1[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> b2[k];</span>
<span id="cb13-52">          B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*=</span> b2[k];</span>
<span id="cb13-53">        }</span>
<span id="cb13-54">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> p_ij;</span>
<span id="cb13-55">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (is_zero_scan(j, i, zeros)) {</span>
<span id="cb13-56">          p_ij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> B;         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// C[i, j] = 0</span></span>
<span id="cb13-57">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(B);   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// delta in Omega coords -&gt; partial coords</span></span>
<span id="cb13-58">        } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb13-59">          p_ij <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;">lower_upper_bound_jacobian</span>(raw[raw_idx], <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="cb13-60">          raw_idx <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="cb13-61">        }</span>
<span id="cb13-62">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// LKJ prior on the tree-i partial (evaluated at p* for zeros)</span></span>
<span id="cb13-63">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> beta_lpdf(<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> (p_ij <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>) | a, a)</span>
<span id="cb13-64">                    <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> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(p_ij<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="cb13-65">        P[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> p_ij;</span>
<span id="cb13-66">        C[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> p_ij;</span>
<span id="cb13-67">        C[j, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> C[i, j];</span>
<span id="cb13-68">      }</span>
<span id="cb13-69">    }</span>
<span id="cb13-70">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> C;</span>
<span id="cb13-71">  }</span>
<span id="cb13-72">}</span>
<span id="cb13-73"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb13-74">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb13-75">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// concentration in LKJ</span></span>
<span id="cb13-76">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb13-77">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular index pairs</span></span>
<span id="cb13-78">}</span>
<span id="cb13-79"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb13-80">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_zero] raw;</span>
<span id="cb13-81">}</span>
<span id="cb13-82"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb13-83">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// prior is included via the beta terms in the constraint, so the</span></span>
<span id="cb13-84">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// model block stays empty</span></span>
<span id="cb13-85">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] Omega <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;">corr_constrain_cvine_jacobian</span>(K, raw, zeros, eta);</span>
<span id="cb13-86">}</span>
<span id="cb13-87"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb13-88">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-9" class="tab-pane" aria-labelledby="tabset-1-9-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>cvine-lookahead-corr-zeros.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" data-filename="cvine-lookahead-corr-zeros.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/*</span></span>
<span id="cb14-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  Look-ahead C-vine parameterization of a correlation matrix with exact structural</span></span>
<span id="cb14-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  zeros. This is the equality-specialized interface to the look-ahead C-vine</span></span>
<span id="cb14-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  transform in https://gist.github.com/spinkney/7d994fb03079c9edb8a0e83a40d2faad.</span></span>
<span id="cb14-5"></span>
<span id="cb14-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  Each structural zero is an exact ordinary-correlation equality and consumes</span></span>
<span id="cb14-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  no element of `raw`. All other ordinary correlations have bounds (-1, 1).</span></span>
<span id="cb14-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  The transform accumulates the reduced raw-to-free-correlation Jacobian, and</span></span>
<span id="cb14-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  the model adds the LKJ determinant kernel with respect to those free cells.</span></span>
<span id="cb14-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*/</span></span>
<span id="cb14-11"></span>
<span id="cb14-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb14-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// sqrt((1-a^2)(1-b^2)), evaluated stably on [-1,1]^2.</span></span>
<span id="cb14-14">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> unit_circle_root_product(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> a, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> b) {</span>
<span id="cb14-15">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q <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> a) <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> a) <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> b) <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> b);</span>
<span id="cb14-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <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;">fmax</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, q));</span>
<span id="cb14-17">  }</span>
<span id="cb14-18"></span>
<span id="cb14-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/*</span></span>
<span id="cb14-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Exact projection of the 3 x 3 correlation elliptope. Let x be the</span></span>
<span id="cb14-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    correlation to project, with y in [y_lo, y_hi] and r in [r_lo, r_hi].</span></span>
<span id="cb14-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    The result is [minimum feasible x, maximum feasible x]. Degenerate point</span></span>
<span id="cb14-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    intervals are retained for exact zero constraints.</span></span>
<span id="cb14-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  */</span></span>
<span id="cb14-25">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> corr3_project_first(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> y_lo_in, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> y_hi_in,</span>
<span id="cb14-26">                             <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> r_lo_in, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> r_hi_in) {</span>
<span id="cb14-27">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> y_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</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;">1.0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, y_lo_in));</span>
<span id="cb14-28">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> y_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</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;">1.0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, y_hi_in));</span>
<span id="cb14-29">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> r_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</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;">1.0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, r_lo_in));</span>
<span id="cb14-30">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> r_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</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;">1.0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, r_hi_in));</span>
<span id="cb14-31">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> x_lo;</span>
<span id="cb14-32">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> x_hi;</span>
<span id="cb14-33">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] out;</span>
<span id="cb14-34"></span>
<span id="cb14-35">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (y_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> y_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> r_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> r_hi) {</span>
<span id="cb14-36">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"corr3_project_first received a reversed interval: y=["</span>,</span>
<span id="cb14-37">             y_lo, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, y_hi, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"], r=["</span>, r_lo, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, r_hi, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]."</span>);</span>
<span id="cb14-38">    }</span>
<span id="cb14-39"></span>
<span id="cb14-40">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Maximum x.</span></span>
<span id="cb14-41">    <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;">fmax</span>(y_lo, r_lo) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(y_hi, r_hi)) {</span>
<span id="cb14-42">      x_hi <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.0</span>;</span>
<span id="cb14-43">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (y_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> r_hi) {</span>
<span id="cb14-44">      x_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> unit_circle_root_product(y_lo, r_hi);</span>
<span id="cb14-45">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb14-46">      x_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> unit_circle_root_product(y_hi, r_lo);</span>
<span id="cb14-47">    }</span>
<span id="cb14-48"></span>
<span id="cb14-49">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Minimum x.</span></span>
<span id="cb14-50">    <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;">fmax</span>(y_lo, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>r_hi) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(y_hi, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>r_lo)) {</span>
<span id="cb14-51">      x_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</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;">1.0</span>;</span>
<span id="cb14-52">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (y_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>r_lo) {</span>
<span id="cb14-53">      x_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> unit_circle_root_product(y_lo, r_lo);</span>
<span id="cb14-54">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb14-55">      x_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> r_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> unit_circle_root_product(y_hi, r_hi);</span>
<span id="cb14-56">    }</span>
<span id="cb14-57"></span>
<span id="cb14-58">    out[<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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</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;">1.0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, x_lo));</span>
<span id="cb14-59">    out[<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</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;">1.0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, x_hi));</span>
<span id="cb14-60">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> out;</span>
<span id="cb14-61">  }</span>
<span id="cb14-62"></span>
<span id="cb14-63">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/*</span></span>
<span id="cb14-64"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Map unconstrained free coordinates to a box-constrained correlation</span></span>
<span id="cb14-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    matrix while solving exact ordinary-correlation equalities. The complete</span></span>
<span id="cb14-66"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    reduced raw -&gt; free ordinary-correlation Jacobian is accumulated here.</span></span>
<span id="cb14-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">  */</span></span>
<span id="cb14-68">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tuple</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>) <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cvine_bound_equality_jacobian</span>(</span>
<span id="cb14-69">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb14-70">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> corr_lower,</span>
<span id="cb14-71">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> corr_upper,</span>
<span id="cb14-72">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> interval_tolerance,</span>
<span id="cb14-73">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> validation_tolerance) {</span>
<span id="cb14-74">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(corr_lower);</span>
<span id="cb14-75">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> M_free <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(raw);</span>
<span id="cb14-76">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> pos_free <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="cb14-77">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[K, K] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> pair_is_free <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_array</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, K, K);</span>
<span id="cb14-78">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] lo_work <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_lower;</span>
<span id="cb14-79">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] hi_work <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_upper;</span>
<span id="cb14-80">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] P_work <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">identity_matrix</span>(K);</span>
<span id="cb14-81">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] C_work <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">identity_matrix</span>(K);</span>
<span id="cb14-82">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> logJ <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.0</span>;</span>
<span id="cb14-83">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> logdet <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.0</span>;</span>
<span id="cb14-84"></span>
<span id="cb14-85">    <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;">cols</span>(corr_lower) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> K</span>
<span id="cb14-86">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(corr_upper) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> K</span>
<span id="cb14-87">        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols</span>(corr_upper) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> K) {</span>
<span id="cb14-88">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cvine_bound_equality_transform requires equally sized square "</span>,</span>
<span id="cb14-89">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bound matrices."</span>);</span>
<span id="cb14-90">    }</span>
<span id="cb14-91"></span>
<span id="cb14-92">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Classify coordinates and prepare the initial conditional bounds.</span></span>
<span id="cb14-93">    <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="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>K) {</span>
<span id="cb14-94">      lo_work[i, i] <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.0</span>;</span>
<span id="cb14-95">      hi_work[i, i] <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.0</span>;</span>
<span id="cb14-96"></span>
<span id="cb14-97">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb14-98">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb14-99">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_upper[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> corr_lower[i, j];</span>
<span id="cb14-100">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lo_ij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_lower[i, j];</span>
<span id="cb14-101">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> hi_ij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_upper[i, j];</span>
<span id="cb14-102"></span>
<span id="cb14-103">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>) {</span>
<span id="cb14-104">            pair_is_free[i, j] <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="cb14-105">            pair_is_free[j, i] <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="cb14-106">            lo_ij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</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;">1.0</span>, corr_lower[i, j]);</span>
<span id="cb14-107">            hi_ij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, corr_upper[i, j]);</span>
<span id="cb14-108">          } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (width <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.0</span>) {</span>
<span id="cb14-109">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Preserve a point equality exactly.</span></span>
<span id="cb14-110">            lo_ij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_lower[i, j];</span>
<span id="cb14-111">            hi_ij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr_lower[i, j];</span>
<span id="cb14-112">          } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb14-113">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cvine_bound_equality_transform received a reversed "</span>,</span>
<span id="cb14-114">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"interval at pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"): ["</span>,</span>
<span id="cb14-115">                   corr_lower[i, j], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, corr_upper[i, j], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]."</span>);</span>
<span id="cb14-116">          }</span>
<span id="cb14-117"></span>
<span id="cb14-118">          lo_work[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lo_ij;</span>
<span id="cb14-119">          lo_work[j, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lo_ij;</span>
<span id="cb14-120">          hi_work[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hi_ij;</span>
<span id="cb14-121">          hi_work[j, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hi_ij;</span>
<span id="cb14-122">        }</span>
<span id="cb14-123">      }</span>
<span id="cb14-124">    }</span>
<span id="cb14-125"></span>
<span id="cb14-126">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Sequential C-vine construction.</span></span>
<span id="cb14-127">    <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="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>(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;">1</span>)) {</span>
<span id="cb14-128">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (i <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>K) {</span>
<span id="cb14-129">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> is_free <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pair_is_free[i, j];</span>
<span id="cb14-130">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lo_direct <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lo_work[i, j];</span>
<span id="cb14-131">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> hi_direct <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hi_work[i, j];</span>
<span id="cb14-132">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</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;">1.0</span>, lo_direct);</span>
<span id="cb14-133">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, hi_direct);</span>
<span id="cb14-134">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> x;</span>
<span id="cb14-135">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_scale <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.0</span>;</span>
<span id="cb14-136"></span>
<span id="cb14-137">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// d C[i,j] / d P[i,j], holding earlier C-vine rows fixed.</span></span>
<span id="cb14-138">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb14-139">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (m <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb14-140">            log_scale <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>
<span id="cb14-141">                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(P_work[m, i]))</span>
<span id="cb14-142">                <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(P_work[m, j])));</span>
<span id="cb14-143">          }</span>
<span id="cb14-144">        }</span>
<span id="cb14-145"></span>
<span id="cb14-146">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (is_free <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="cb14-147">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> width;</span>
<span id="cb14-148"></span>
<span id="cb14-149">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Intersect the direct interval with every 3 x 3 projection.</span></span>
<span id="cb14-150">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> K) {</span>
<span id="cb14-151">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (j <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>K) {</span>
<span id="cb14-152">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] projected <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr3_project_first(</span>
<span id="cb14-153">                  lo_work[i, k], hi_work[i, k],</span>
<span id="cb14-154">                  lo_work[j, k], hi_work[j, k]);</span>
<span id="cb14-155">              lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</span>(lo, projected[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]);</span>
<span id="cb14-156">              hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(hi, projected[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]);</span>
<span id="cb14-157">            }</span>
<span id="cb14-158">          }</span>
<span id="cb14-159"></span>
<span id="cb14-160">          width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lo;</span>
<span id="cb14-161">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> interval_tolerance)) {</span>
<span id="cb14-162">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Empty free C-vine interval at partial pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j,</span>
<span id="cb14-163">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"). Interval after look-ahead=["</span>, lo, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, hi, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]."</span>);</span>
<span id="cb14-164">          }</span>
<span id="cb14-165">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (pos_free <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> M_free) {</span>
<span id="cb14-166">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cvine_bound_equality_transform received too few raw "</span>,</span>
<span id="cb14-167">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"coordinates; missing the coordinate for pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>,</span>
<span id="cb14-168">                   j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")."</span>);</span>
<span id="cb14-169">          }</span>
<span id="cb14-170"></span>
<span id="cb14-171">          {</span>
<span id="cb14-172">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_logit</span>(raw[pos_free]);</span>
<span id="cb14-173"></span>
<span id="cb14-174">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<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;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>)) {</span>
<span id="cb14-175">              <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inv_logit(raw["</span>, pos_free,</span>
<span id="cb14-176">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]) numerically reached an endpoint; raw="</span>,</span>
<span id="cb14-177">                     raw[pos_free], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb14-178">            }</span>
<span id="cb14-179"></span>
<span id="cb14-180">            x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q;</span>
<span id="cb14-181">            logJ <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(width)</span>
<span id="cb14-182">                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log_inv_logit</span>(raw[pos_free])</span>
<span id="cb14-183">                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m_inv_logit</span>(raw[pos_free])</span>
<span id="cb14-184">                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> log_scale;</span>
<span id="cb14-185">          }</span>
<span id="cb14-186"></span>
<span id="cb14-187">          pos_free <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="cb14-188">        } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb14-189">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> fixed_x <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> (lo_direct <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> hi_direct);</span>
<span id="cb14-190"></span>
<span id="cb14-191">          <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;">abs</span>(hi_direct <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lo_direct) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance) {</span>
<span id="cb14-192">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Internal equality interval acquired nonzero width at pair ("</span>,</span>
<span id="cb14-193">                   i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"): ["</span>, lo_direct, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, hi_direct, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]."</span>);</span>
<span id="cb14-194">          }</span>
<span id="cb14-195">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(fixed_x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</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;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> fixed_x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>)) {</span>
<span id="cb14-196">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Exact equality implies a non-interior partial correlation "</span>,</span>
<span id="cb14-197">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"at pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"): "</span>, fixed_x, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb14-198">          }</span>
<span id="cb14-199"></span>
<span id="cb14-200">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Validate rather than alter the exact point during look-ahead.</span></span>
<span id="cb14-201">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> K) {</span>
<span id="cb14-202">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (j <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>K) {</span>
<span id="cb14-203">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] projected <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr3_project_first(</span>
<span id="cb14-204">                  lo_work[i, k], hi_work[i, k],</span>
<span id="cb14-205">                  lo_work[j, k], hi_work[j, k]);</span>
<span id="cb14-206"></span>
<span id="cb14-207">              <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (fixed_x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> projected[<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> validation_tolerance</span>
<span id="cb14-208">                  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> fixed_x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> projected[<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> validation_tolerance) {</span>
<span id="cb14-209">                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Exact equality partial at pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j,</span>
<span id="cb14-210">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">") is incompatible with future pair "</span>, k,</span>
<span id="cb14-211">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">". Fixed partial="</span>, fixed_x,</span>
<span id="cb14-212">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", projected interval=["</span>, projected[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>,</span>
<span id="cb14-213">                       projected[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]."</span>);</span>
<span id="cb14-214">              }</span>
<span id="cb14-215">            }</span>
<span id="cb14-216">          }</span>
<span id="cb14-217"></span>
<span id="cb14-218">          x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fixed_x;</span>
<span id="cb14-219">        }</span>
<span id="cb14-220"></span>
<span id="cb14-221">        P_work[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x;</span>
<span id="cb14-222">        P_work[j, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x;</span>
<span id="cb14-223">        logdet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(x));</span>
<span id="cb14-224"></span>
<span id="cb14-225">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Update future intervals in the current C-vine row.</span></span>
<span id="cb14-226">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> K) {</span>
<span id="cb14-227">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (j <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>K) {</span>
<span id="cb14-228">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] future_projected <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> corr3_project_first(</span>
<span id="cb14-229">                x, x, lo_work[j, k], hi_work[j, k]);</span>
<span id="cb14-230"></span>
<span id="cb14-231">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (pair_is_free[i, 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;">1</span>) {</span>
<span id="cb14-232">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> next_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</span>(lo_work[i, k], future_projected[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]);</span>
<span id="cb14-233">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> next_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(hi_work[i, k], future_projected[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]);</span>
<span id="cb14-234"></span>
<span id="cb14-235">              <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(next_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> next_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> interval_tolerance)) {</span>
<span id="cb14-236">                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Future free C-vine interval became empty after partial ("</span>,</span>
<span id="cb14-237">                       i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"). Future pair="</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, k,</span>
<span id="cb14-238">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", interval=["</span>, next_lo, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, next_hi, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]."</span>);</span>
<span id="cb14-239">              }</span>
<span id="cb14-240"></span>
<span id="cb14-241">              lo_work[i, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> next_lo;</span>
<span id="cb14-242">              lo_work[k, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> next_lo;</span>
<span id="cb14-243">              hi_work[i, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> next_hi;</span>
<span id="cb14-244">              hi_work[k, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> next_hi;</span>
<span id="cb14-245">            } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb14-246">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> fixed_future <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>
<span id="cb14-247">                  lo_work[i, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> hi_work[i, k]);</span>
<span id="cb14-248"></span>
<span id="cb14-249">              <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;">abs</span>(hi_work[i, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lo_work[i, k])</span>
<span id="cb14-250">                  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance) {</span>
<span id="cb14-251">                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Internal future equality interval acquired width at "</span>,</span>
<span id="cb14-252">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, k, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")."</span>);</span>
<span id="cb14-253">              }</span>
<span id="cb14-254">              <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (fixed_future <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> future_projected[<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> validation_tolerance</span>
<span id="cb14-255">                  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> fixed_future <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> future_projected[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb14-256">                                      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> validation_tolerance) {</span>
<span id="cb14-257">                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sampling partial ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j,</span>
<span id="cb14-258">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">") made exact future equality pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, k,</span>
<span id="cb14-259">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">") infeasible. Fixed future partial="</span>, fixed_future,</span>
<span id="cb14-260">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", projected interval=["</span>, future_projected[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>,</span>
<span id="cb14-261">                       future_projected[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]."</span>);</span>
<span id="cb14-262">              }</span>
<span id="cb14-263"></span>
<span id="cb14-264">              lo_work[i, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fixed_future;</span>
<span id="cb14-265">              lo_work[k, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fixed_future;</span>
<span id="cb14-266">              hi_work[i, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fixed_future;</span>
<span id="cb14-267">              hi_work[k, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fixed_future;</span>
<span id="cb14-268">            }</span>
<span id="cb14-269">          }</span>
<span id="cb14-270">        }</span>
<span id="cb14-271">      }</span>
<span id="cb14-272"></span>
<span id="cb14-273">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Propagate remaining intervals to the next C-vine level.</span></span>
<span id="cb14-274">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</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;">1</span>) {</span>
<span id="cb14-275">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (i <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>(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;">1</span>)) {</span>
<span id="cb14-276">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (j <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>K) {</span>
<span id="cb14-277">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> pij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> P_work[i, j];</span>
<span id="cb14-278">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> pik <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> P_work[i, k];</span>
<span id="cb14-279">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> denom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(pij)) <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(pik)));</span>
<span id="cb14-280"></span>
<span id="cb14-281">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(denom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>)) {</span>
<span id="cb14-282">              <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Zero conditional-correlation denominator at level "</span>, i,</span>
<span id="cb14-283">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" for pair ("</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, k, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")."</span>);</span>
<span id="cb14-284">            }</span>
<span id="cb14-285"></span>
<span id="cb14-286">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (pair_is_free[j, 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;">1</span>) {</span>
<span id="cb14-287">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> next_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</span>(</span>
<span id="cb14-288">                  <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.0</span>, (lo_work[j, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pik) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denom);</span>
<span id="cb14-289">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> next_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(</span>
<span id="cb14-290">                  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, (hi_work[j, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pik) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denom);</span>
<span id="cb14-291"></span>
<span id="cb14-292">              <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(next_hi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> next_lo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> interval_tolerance)) {</span>
<span id="cb14-293">                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Conditional free box became empty after C-vine row "</span>,</span>
<span id="cb14-294">                       i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" for pair ("</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, k, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"). Interval=["</span>,</span>
<span id="cb14-295">                       next_lo, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, next_hi, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"]."</span>);</span>
<span id="cb14-296">              }</span>
<span id="cb14-297"></span>
<span id="cb14-298">              lo_work[j, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> next_lo;</span>
<span id="cb14-299">              lo_work[k, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> next_lo;</span>
<span id="cb14-300">              hi_work[j, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> next_hi;</span>
<span id="cb14-301">              hi_work[k, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> next_hi;</span>
<span id="cb14-302">            } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb14-303">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> fixed_current <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>
<span id="cb14-304">                  lo_work[j, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> hi_work[j, k]);</span>
<span id="cb14-305">              <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> fixed_next;</span>
<span id="cb14-306"></span>
<span id="cb14-307">              <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;">abs</span>(hi_work[j, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lo_work[j, k])</span>
<span id="cb14-308">                  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> validation_tolerance) {</span>
<span id="cb14-309">                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Internal equality interval acquired width before "</span>,</span>
<span id="cb14-310">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"conditioning at pair ("</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, k, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")."</span>);</span>
<span id="cb14-311">              }</span>
<span id="cb14-312"></span>
<span id="cb14-313">              fixed_next <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (fixed_current <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pij <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pik) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denom;</span>
<span id="cb14-314">              <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>(fixed_next <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</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;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> fixed_next <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>)) {</span>
<span id="cb14-315">                <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Exact ordinary equality implies a non-interior "</span>,</span>
<span id="cb14-316">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"conditional correlation after level "</span>, i,</span>
<span id="cb14-317">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" for pair ("</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, k, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"): "</span>, fixed_next, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb14-318">              }</span>
<span id="cb14-319"></span>
<span id="cb14-320">              lo_work[j, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fixed_next;</span>
<span id="cb14-321">              lo_work[k, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fixed_next;</span>
<span id="cb14-322">              hi_work[j, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fixed_next;</span>
<span id="cb14-323">              hi_work[k, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fixed_next;</span>
<span id="cb14-324">            }</span>
<span id="cb14-325">          }</span>
<span id="cb14-326">        }</span>
<span id="cb14-327">      }</span>
<span id="cb14-328">    }</span>
<span id="cb14-329"></span>
<span id="cb14-330">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (pos_free <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> M_free <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="cb14-331">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cvine_bound_equality_transform received "</span>, M_free,</span>
<span id="cb14-332">             <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" raw coordinates but consumed "</span>, pos_free <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>);</span>
<span id="cb14-333">    }</span>
<span id="cb14-334"></span>
<span id="cb14-335">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Convert C-vine partial correlations to ordinary correlations.</span></span>
<span id="cb14-336">    <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="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>(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;">1</span>)) {</span>
<span id="cb14-337">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (i <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>K) {</span>
<span id="cb14-338">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> P_work[i, j];</span>
<span id="cb14-339"></span>
<span id="cb14-340">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb14-341">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i;</span>
<span id="cb14-342">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step</span> <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb14-343">            m <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="cb14-344">            value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> P_work[m, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> P_work[m, j]</span>
<span id="cb14-345">                    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> value</span>
<span id="cb14-346">                      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(P_work[m, i]))</span>
<span id="cb14-347">                             <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(P_work[m, j])));</span>
<span id="cb14-348">          }</span>
<span id="cb14-349">        }</span>
<span id="cb14-350"></span>
<span id="cb14-351">        C_work[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value;</span>
<span id="cb14-352">        C_work[j, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value;</span>
<span id="cb14-353">      }</span>
<span id="cb14-354">    }</span>
<span id="cb14-355"></span>
<span id="cb14-356">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> logJ;</span>
<span id="cb14-357">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (C_work, logdet);</span>
<span id="cb14-358">  }</span>
<span id="cb14-359">}</span>
<span id="cb14-360"></span>
<span id="cb14-361"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb14-362">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb14-363">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// LKJ concentration</span></span>
<span id="cb14-364">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb14-365">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular index pairs</span></span>
<span id="cb14-366">}</span>
<span id="cb14-367"></span>
<span id="cb14-368"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed data</span> {</span>
<span id="cb14-369">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] corr_lower <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</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;">1.0</span>, K, K);</span>
<span id="cb14-370">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] corr_upper <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, K, K);</span>
<span id="cb14-371"></span>
<span id="cb14-372">  <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="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>K) {</span>
<span id="cb14-373">    corr_lower[i, i] <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.0</span>;</span>
<span id="cb14-374">    corr_upper[i, i] <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.0</span>;</span>
<span id="cb14-375">  }</span>
<span id="cb14-376"></span>
<span id="cb14-377">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (N_zero <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb14-378">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (n <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N_zero) {</span>
<span id="cb14-379">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> zeros[n, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>];</span>
<span id="cb14-380">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> zeros[n, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>];</span>
<span id="cb14-381"></span>
<span id="cb14-382">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (i <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;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> j <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;">1</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> i) {</span>
<span id="cb14-383">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"zeros["</span>, n, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"] must be a lower-triangular index pair; got ("</span>,</span>
<span id="cb14-384">               i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")."</span>);</span>
<span id="cb14-385">      }</span>
<span id="cb14-386">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb14-387">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (m <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(n <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="cb14-388">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (zeros[m, <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> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[m, <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> j) {</span>
<span id="cb14-389">            <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Duplicate structural-zero pair ("</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")."</span>);</span>
<span id="cb14-390">          }</span>
<span id="cb14-391">        }</span>
<span id="cb14-392">      }</span>
<span id="cb14-393"></span>
<span id="cb14-394">      corr_lower[i, j] <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.0</span>;</span>
<span id="cb14-395">      corr_lower[j, i] <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.0</span>;</span>
<span id="cb14-396">      corr_upper[i, j] <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.0</span>;</span>
<span id="cb14-397">      corr_upper[j, i] <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.0</span>;</span>
<span id="cb14-398">    }</span>
<span id="cb14-399">  }</span>
<span id="cb14-400">}</span>
<span id="cb14-401"></span>
<span id="cb14-402"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb14-403">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Exact zeros are omitted from the parameter vector.</span></span>
<span id="cb14-404">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_zero] raw;</span>
<span id="cb14-405">}</span>
<span id="cb14-406"></span>
<span id="cb14-407"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb14-408">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">corr_matrix</span>[K] Omega;</span>
<span id="cb14-409">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_det_Omega;</span>
<span id="cb14-410"></span>
<span id="cb14-411">  {</span>
<span id="cb14-412">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">tuple</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K], <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span>) transform <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;">cvine_bound_equality_jacobian</span>(</span>
<span id="cb14-413">        raw, corr_lower, corr_upper, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-12</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-10</span>);</span>
<span id="cb14-414">    Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> transform<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>;</span>
<span id="cb14-415">    log_det_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> transform<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.2</span>;</span>
<span id="cb14-416">  }</span>
<span id="cb14-417">}</span>
<span id="cb14-418"></span>
<span id="cb14-419"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb14-420">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// LKJ density with respect to the free ordinary-correlation coordinates.</span></span>
<span id="cb14-421">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (eta <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> log_det_Omega;</span>
<span id="cb14-422">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-10" class="tab-pane" aria-labelledby="tabset-1-10-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>pinkney-corr-zeros-rowscale.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" data-filename="pinkney-corr-zeros-rowscale.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb15-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb15-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> is_zero(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> row_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> col_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx) {</span>
<span id="cb15-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros)</span>
<span id="cb15-4">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> row_idx</span>
<span id="cb15-5">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> col_idx;</span>
<span id="cb15-6">  }</span>
<span id="cb15-7"></span>
<span id="cb15-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cholesky_corr_constrain_rowscale_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb15-9">                                                   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros) {</span>
<span id="cb15-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Samples L[i, j] directly (bounds pre-divided by L[j, j]) and keeps the</span></span>
<span id="cb15-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// remaining radius in the untouched tail of the row: each entry of</span></span>
<span id="cb15-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// L[i, j:i] holds the current radius until column j is sampled, then the</span></span>
<span id="cb15-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// tail is scaled down. L[i, i] ends up as the diagonal automatically.</span></span>
<span id="cb15-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, K, K);</span>
<span id="cb15-15">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_idx <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="cb15-16">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx <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="cb15-17">    L[<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="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="cb15-18"></span>
<span id="cb15-19">    <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="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>K) {</span>
<span id="cb15-20">      L[i, <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>i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_row_vector</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, i);</span>
<span id="cb15-21">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb15-22">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> l_ij_old <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L[i, j];  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// remaining radius of row i</span></span>
<span id="cb15-23">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> b1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(L[j, <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>(j <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>)], L[i, <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>(j <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="cb15-24">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (is_zero(i, j, zeros, zero_idx)) {</span>
<span id="cb15-25">          L[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>b1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> L[j, j];  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Omega[i, j] = b1 + Lij * Ljj = 0</span></span>
<span id="cb15-26">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(L[j, j]);  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Carpenter's correction</span></span>
<span id="cb15-27">          zero_idx <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="cb15-28">        } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb15-29">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// |L[i, j]| &lt;= radius and Omega[i, j] = b1 + Lij * Ljj in (-1, 1);</span></span>
<span id="cb15-30">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// sampling L directly, the 1 / L[j, j] factor is inside the</span></span>
<span id="cb15-31">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// bound width, so no separate jacobian term</span></span>
<span id="cb15-32">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> low <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>({<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>l_ij_old, (<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> b1) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> L[j, j]});</span>
<span id="cb15-33">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> up <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>({l_ij_old, (<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> b1) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> L[j, j]});</span>
<span id="cb15-34">          L[i, j] <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;">lower_upper_bound_jacobian</span>(raw[raw_idx], low, up);</span>
<span id="cb15-35">          raw_idx <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="cb15-36">        }</span>
<span id="cb15-37">        L[i, (j <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>i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> (L[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> l_ij_old)<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="cb15-38">      }</span>
<span id="cb15-39">    }</span>
<span id="cb15-40">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> L;</span>
<span id="cb15-41">  }</span>
<span id="cb15-42">}</span>
<span id="cb15-43"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb15-44">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb15-45">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// concentration in LKJ Cholesky</span></span>
<span id="cb15-46">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb15-47">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular, row major order</span></span>
<span id="cb15-48">}</span>
<span id="cb15-49"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb15-50">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_zero] raw;</span>
<span id="cb15-51">}</span>
<span id="cb15-52"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb15-53">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L_Omega <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;">cholesky_corr_constrain_rowscale_jacobian</span>(K, raw, zeros);</span>
<span id="cb15-54">}</span>
<span id="cb15-55"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb15-56">  L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lkj_corr_cholesky</span>(eta);</span>
<span id="cb15-57">}</span>
<span id="cb15-58"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb15-59">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Omega);</span>
<span id="cb15-60">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-11" class="tab-pane" aria-labelledby="tabset-1-11-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>pinkney-corr-zeros-schur.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" data-filename="pinkney-corr-zeros-schur.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb16-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb16-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> is_zero(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> row_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> col_idx, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx) {</span>
<span id="cb16-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros)</span>
<span id="cb16-4">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> row_idx</span>
<span id="cb16-5">      <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx, <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> col_idx;</span>
<span id="cb16-6">  }</span>
<span id="cb16-7"></span>
<span id="cb16-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cholesky_corr_constrain_schur_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb16-9">                                                <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros) {</span>
<span id="cb16-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Canonical partial correlation construction: each free entry is the</span></span>
<span id="cb16-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// remaining radius times tanh(raw), so |L[i, j]| &lt; radius automatically</span></span>
<span id="cb16-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// and no correlation bounds are needed. The untouched tail of each row</span></span>
<span id="cb16-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// holds the current radius (row-scaling trick).</span></span>
<span id="cb16-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, K, K);</span>
<span id="cb16-15">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_idx <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="cb16-16">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx <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="cb16-17">    L[<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="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="cb16-18"></span>
<span id="cb16-19">    <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="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>K) {</span>
<span id="cb16-20">      L[i, <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>i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_row_vector</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, i);</span>
<span id="cb16-21">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(i <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="cb16-22">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L[i, j];  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// remaining radius of row i</span></span>
<span id="cb16-23">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (is_zero(i, j, zeros, zero_idx)) {</span>
<span id="cb16-24">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> b1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(L[j, <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>(j <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>)], L[i, <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>(j <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="cb16-25">          L[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>b1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> L[j, j];  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Omega[i, j] = b1 + Lij * Ljj = 0</span></span>
<span id="cb16-26">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(L[j, j]);  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Carpenter's correction</span></span>
<span id="cb16-27">          zero_idx <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="cb16-28">          L[i, (j <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>i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> (L[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> r)<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="cb16-29">        } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb16-30">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> raw[raw_idx];</span>
<span id="cb16-31">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> cosh_x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cosh</span>(x);</span>
<span id="cb16-32">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// tanh jacobian and x -&gt; L[i, j] = r * tanh(x);</span></span>
<span id="cb16-33">          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the radius update sqrt(1 - tanh^2) is 1 / cosh</span></span>
<span id="cb16-34">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</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;">2</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(cosh_x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(r);</span>
<span id="cb16-35">          L[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tanh</span>(x);</span>
<span id="cb16-36">          raw_idx <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="cb16-37">          L[i, (j <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>i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/=</span> cosh_x;</span>
<span id="cb16-38">        }</span>
<span id="cb16-39">      }</span>
<span id="cb16-40">    }</span>
<span id="cb16-41">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> L;</span>
<span id="cb16-42">  }</span>
<span id="cb16-43">}</span>
<span id="cb16-44"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb16-45">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb16-46">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// concentration in LKJ Cholesky</span></span>
<span id="cb16-47">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb16-48">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular, row major order</span></span>
<span id="cb16-49">}</span>
<span id="cb16-50"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb16-51">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_zero] raw;</span>
<span id="cb16-52">}</span>
<span id="cb16-53"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb16-54">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L_Omega <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;">cholesky_corr_constrain_schur_jacobian</span>(K, raw, zeros);</span>
<span id="cb16-55">}</span>
<span id="cb16-56"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb16-57">  L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lkj_corr_cholesky</span>(eta);</span>
<span id="cb16-58">}</span>
<span id="cb16-59"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb16-60">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Omega);</span>
<span id="cb16-61">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-12" class="tab-pane" aria-labelledby="tabset-1-12-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>qr-corr-zeros.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" data-filename="qr-corr-zeros.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb17-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb17-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cholesky_corr_constrain_unitvec_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb17-3">                                                  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros) {</span>
<span id="cb17-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Row i of a Cholesky correlation factor is a unit vector in R^i with</span></span>
<span id="cb17-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// positive last coordinate. Omega[i, j] = 0 means row i is orthogonal</span></span>
<span id="cb17-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// to row j, so row i lives on the unit sphere in the orthogonal</span></span>
<span id="cb17-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// complement of its zero-partner rows: the constraint is satisfied</span></span>
<span id="cb17-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// exactly by construction and no proposal can ever be infeasible.</span></span>
<span id="cb17-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span></span>
<span id="cb17-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Chart per row: unit vector u = (C * y, 1)' / s with s = sqrt(1 + y'y),</span></span>
<span id="cb17-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// C an orthonormal basis of the feasible subspace. Jacobian onto the</span></span>
<span id="cb17-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// subsphere is -(d + 2) * log(s); the joint delta over the row's zeros</span></span>
<span id="cb17-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// contributes -0.5 * log det(Gram) = -sum(log|diag(R)|) from the QR.</span></span>
<span id="cb17-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, K, K);</span>
<span id="cb17-15">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_idx <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="cb17-16">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx <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="cb17-17">    L[<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="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="cb17-18"></span>
<span id="cb17-19">    <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="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>K) {</span>
<span id="cb17-20">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n_z <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;">0</span>;</span>
<span id="cb17-21">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros)</span>
<span id="cb17-22">             <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_z, <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> i) {</span>
<span id="cb17-23">        n_z <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="cb17-24">      }</span>
<span id="cb17-25">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i <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> n_z;</span>
<span id="cb17-26"></span>
<span id="cb17-27">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (n_z <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;">0</span>) {</span>
<span id="cb17-28">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[d] y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> raw[raw_idx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(raw_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> d <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="cb17-29">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_self</span>(y));</span>
<span id="cb17-30">        L[i, <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>(i <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> y<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> s;</span>
<span id="cb17-31">        L[i, i] <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> s;</span>
<span id="cb17-32">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(d <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(s);</span>
<span id="cb17-33">        raw_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> d;</span>
<span id="cb17-34">      } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb17-35">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// columns are the zero-partner rows restricted to 1:(i - 1)</span></span>
<span id="cb17-36">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[i <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>, n_z] A;</span>
<span id="cb17-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (n <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_z) {</span>
<span id="cb17-38">          A[, n] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L[zeros[zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n <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;">2</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>(i <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>;</span>
<span id="cb17-39">        }</span>
<span id="cb17-40">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// full qr: the complement basis lives in Q's last d columns and</span></span>
<span id="cb17-41">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// diagonal(R) supplies the Gram term from one decomposition</span></span>
<span id="cb17-42">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[i <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>, i <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>] Q;</span>
<span id="cb17-43">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[i <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>, n_z] R;</span>
<span id="cb17-44">        (Q, R) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qr</span>(A);</span>
<span id="cb17-45">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diagonal</span>(R))));</span>
<span id="cb17-46">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb17-47">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[d] y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> raw[raw_idx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(raw_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> d <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="cb17-48">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_self</span>(y));</span>
<span id="cb17-49">          L[i, <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>(i <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> (Q[, (n_z <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>(i <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> y)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> s;</span>
<span id="cb17-50">          L[i, i] <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> s;</span>
<span id="cb17-51">          <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(d <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(s);</span>
<span id="cb17-52">          raw_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> d;</span>
<span id="cb17-53">        } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb17-54">          L[i, i] <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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// row fully constrained: e_i</span></span>
<span id="cb17-55">        }</span>
<span id="cb17-56">        zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> n_z;</span>
<span id="cb17-57">      }</span>
<span id="cb17-58">    }</span>
<span id="cb17-59">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> L;</span>
<span id="cb17-60">  }</span>
<span id="cb17-61">}</span>
<span id="cb17-62"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb17-63">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb17-64">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// concentration in LKJ Cholesky</span></span>
<span id="cb17-65">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb17-66">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular, row major order</span></span>
<span id="cb17-67">}</span>
<span id="cb17-68"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb17-69">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_zero] raw;</span>
<span id="cb17-70">}</span>
<span id="cb17-71"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb17-72">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L_Omega <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;">cholesky_corr_constrain_unitvec_jacobian</span>(K, raw, zeros);</span>
<span id="cb17-73">}</span>
<span id="cb17-74"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb17-75">  L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lkj_corr_cholesky</span>(eta);</span>
<span id="cb17-76">}</span>
<span id="cb17-77"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb17-78">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Omega);</span>
<span id="cb17-79">}</span></code></pre></div></div>
</div>
</div>
<div id="tabset-1-13" class="tab-pane" aria-labelledby="tabset-1-13-tab">
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>qr2-corr-zeros.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" data-filename="qr2-corr-zeros.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb18-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb18-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cholesky_corr_constrain_unitvec2_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> raw,</span>
<span id="cb18-3">                                                   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[,] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros) {</span>
<span id="cb18-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Same exact-constraint geometry as the unitvec version: row i lives on</span></span>
<span id="cb18-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the unit sphere in the orthogonal complement of its zero-partner rows,</span></span>
<span id="cb18-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// so every raw value maps to a valid constrained matrix. The chart on</span></span>
<span id="cb18-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// the subsphere is the canonical-partial-correlation (tanh) stick</span></span>
<span id="cb18-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// construction instead of the gnomonic map, to lighten the tails.</span></span>
<span id="cb18-9">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, K, K);</span>
<span id="cb18-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> raw_idx <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="cb18-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zero_idx <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="cb18-12">    L[<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="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="cb18-13"></span>
<span id="cb18-14">    <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="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>K) {</span>
<span id="cb18-15">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n_z <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;">0</span>;</span>
<span id="cb18-16">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(zeros)</span>
<span id="cb18-17">             <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> zeros[zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n_z, <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> i) {</span>
<span id="cb18-18">        n_z <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="cb18-19">      }</span>
<span id="cb18-20">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i <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> n_z;</span>
<span id="cb18-21"></span>
<span id="cb18-22">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// subspace coordinates via tanh sticks: w_k = tanh(x_k) * radius,</span></span>
<span id="cb18-23">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// radius update sqrt(1 - tanh^2) = 1 / cosh, so the whole chart</span></span>
<span id="cb18-24">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// jacobian collapses to accumulated powers of log(cosh)</span></span>
<span id="cb18-25">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[d] w;</span>
<span id="cb18-26">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> r <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="cb18-27">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>d) {</span>
<span id="cb18-28">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> raw[raw_idx];</span>
<span id="cb18-29">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> cosh_x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cosh</span>(x);</span>
<span id="cb18-30">        w[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tanh</span>(x);</span>
<span id="cb18-31">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(d <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(cosh_x);</span>
<span id="cb18-32">        r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/=</span> cosh_x;</span>
<span id="cb18-33">        raw_idx <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="cb18-34">      }</span>
<span id="cb18-35"></span>
<span id="cb18-36">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (n_z <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;">0</span>) {</span>
<span id="cb18-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb18-38">          L[i, <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>d] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb18-39">        }</span>
<span id="cb18-40">        L[i, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r;</span>
<span id="cb18-41">      } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb18-42">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// columns are the zero-partner rows restricted to 1:(i - 1);</span></span>
<span id="cb18-43">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// -0.5 * log det(Gram) from the joint delta over this row's zeros</span></span>
<span id="cb18-44">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[i <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>, n_z] A;</span>
<span id="cb18-45">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (n <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n_z) {</span>
<span id="cb18-46">          A[, n] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> L[zeros[zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> n <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;">2</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>(i <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>;</span>
<span id="cb18-47">        }</span>
<span id="cb18-48">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// full qr: the complement basis lives in Q's last d columns and</span></span>
<span id="cb18-49">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// diagonal(R) supplies the Gram term from one decomposition</span></span>
<span id="cb18-50">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[i <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>, i <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>] Q;</span>
<span id="cb18-51">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[i <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>, n_z] R;</span>
<span id="cb18-52">        (Q, R) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qr</span>(A);</span>
<span id="cb18-53">        <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diagonal</span>(R))));</span>
<span id="cb18-54">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb18-55">          L[i, <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>(i <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> (Q[, (n_z <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>(i <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> w)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb18-56">        }</span>
<span id="cb18-57">        L[i, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r;</span>
<span id="cb18-58">        zero_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> n_z;</span>
<span id="cb18-59">      }</span>
<span id="cb18-60">    }</span>
<span id="cb18-61">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> L;</span>
<span id="cb18-62">  }</span>
<span id="cb18-63">}</span>
<span id="cb18-64"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb18-65">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// dimension of correlation matrix</span></span>
<span id="cb18-66">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> eta;                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// concentration in LKJ Cholesky</span></span>
<span id="cb18-67">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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;">&gt;</span> N_zero;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// # structural zero correlations</span></span>
<span id="cb18-68">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N_zero, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> zeros;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lower triangular, row major order</span></span>
<span id="cb18-69">}</span>
<span id="cb18-70"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb18-71">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">choose</span>(K, <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> N_zero] raw;</span>
<span id="cb18-72">}</span>
<span id="cb18-73"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb18-74">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] L_Omega <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;">cholesky_corr_constrain_unitvec2_jacobian</span>(K, raw, zeros);</span>
<span id="cb18-75">}</span>
<span id="cb18-76"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb18-77">  L_Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lkj_corr_cholesky</span>(eta);</span>
<span id="cb18-78">}</span>
<span id="cb18-79"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb18-80">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] Omega <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> multiply_lower_tri_self_transpose(L_Omega);</span>
<span id="cb18-81">}</span></code></pre></div></div>
</div>
</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-axen2022" class="csl-entry">
Axen, Seth. 2022. <em>A Better Unit Vector</em>. Stan Discourse thread, post 30. <a href="https://discourse.mc-stan.org/t/a-better-unit-vector/26989/30">https://discourse.mc-stan.org/t/a-better-unit-vector/26989/30</a>.
</div>
<div id="ref-axen2023" class="csl-entry">
Axen, Seth. 2023. <em>Working with Positive Definite Matrices with Structural Zeros</em>. TuringLang/Turing.jl GitHub discussion #2067. <a href="https://github.com/TuringLang/Turing.jl/discussions/2067">https://github.com/TuringLang/Turing.jl/discussions/2067</a>.
</div>
<div id="ref-carpenter2017" class="csl-entry">
Carpenter, Bob, Andrew Gelman, Matthew D. Hoffman, et al. 2017. <span>“Stan: A Probabilistic Programming Language.”</span> <em>Journal of Statistical Software</em> 76 (1): 1–32. <a href="https://doi.org/10.18637/jss.v076.i01">https://doi.org/10.18637/jss.v076.i01</a>.
</div>
<div id="ref-joe2006" class="csl-entry">
Joe, Harry. 2006. <span>“Generating Random Correlation Matrices Based on Partial Correlations.”</span> <em>Journal of Multivariate Analysis</em> 97 (10): 2177–89. <a href="https://doi.org/10.1016/j.jmva.2005.05.010">https://doi.org/10.1016/j.jmva.2005.05.010</a>.
</div>
<div id="ref-lewandowski2009" class="csl-entry">
Lewandowski, Daniel, Dorota Kurowicka, and Harry Joe. 2009. <span>“Generating Random Correlation Matrices Based on Vines and Extended Onion Method.”</span> <em>Journal of Multivariate Analysis</em> 100 (9): 1989–2001. <a href="https://doi.org/10.1016/j.jmva.2009.04.008">https://doi.org/10.1016/j.jmva.2009.04.008</a>.
</div>
<div id="ref-pinkney2024" class="csl-entry">
Pinkney, Sean. 2024a. <em>A Short Note on a Flexible Cholesky Parameterization of Correlation Matrices</em>. <a href="https://arxiv.org/abs/2405.07286">https://arxiv.org/abs/2405.07286</a>.
</div>
<div id="ref-pinkneytalk" class="csl-entry">
Pinkney, Sean. 2024b. <em>Structured Correlation Matrices</em>. StanCon 2024. <a href="https://discourse.mc-stan.org/t/new-users-guide-intro-sem-and-user-defined-constrained-parameters-chapters/41430/4">https://discourse.mc-stan.org/t/new-users-guide-intro-sem-and-user-defined-constrained-parameters-chapters/41430/4</a>.
</div>
<div id="ref-pinkney2025" class="csl-entry">
Pinkney, Sean. 2025. <em>Updated Cholesky Corr Parameterization Testing</em>. Stan Discourse thread. <a href="https://discourse.mc-stan.org/t/updated-cholesky-corr-parameterization-testing/38827">https://discourse.mc-stan.org/t/updated-cholesky-corr-parameterization-testing/38827</a>.
</div>
<div id="ref-pinkney2026" class="csl-entry">
Pinkney, Sean. 2026. <em>A Better-Er Unit Vector</em>. Stan Discourse thread. <a href="https://discourse.mc-stan.org/t/a-better-er-unit-vector/40830">https://discourse.mc-stan.org/t/a-better-er-unit-vector/40830</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><a href="https://bsky.app/profile/ethothepie.bsky.social">@ethothepie.bsky.social</a> corrected the original here where I incorrectly implied the zeros in the correlation matrix are conditionally unrelated when this is marginal↩︎</p></li>
<li id="fn2"><p>The pass-zero <code>schur</code> fit failed during initialization for seed 5, while the one-pass transform initialized all four chains. Its divergence and efficiency comparison therefore uses the four patterns that succeeded under both settings.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>stan</category>
  <category>correlation</category>
  <category>parameterizations</category>
  <guid>https://spinkney.github.io/posts/post-11-corr-zeros-bakeoff/corr-zeros-bakeoff.html</guid>
  <pubDate>Wed, 15 Jul 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Block-Level LKJ Priors for Structured Correlation Matrices</title>
  <dc:creator>Sean Pinkney</dc:creator>
  <link>https://spinkney.github.io/posts/post-5-compound-symmetric-block-toeplitz/compound-symmetric.html</link>
  <description><![CDATA[ 





<section id="the-problem-with-priors-on-large-correlation-matrices" class="level2">
<h2 class="anchored" data-anchor-id="the-problem-with-priors-on-large-correlation-matrices">1. The Problem with Priors on Large Correlation Matrices</h2>
<p>The LKJ distribution <span class="citation" data-cites="lewandowski2009">(Lewandowski et al. 2009)</span> is the default prior for correlation matrices in Stan <span class="citation" data-cites="carpenter2017">(Carpenter et al. 2017)</span>. For a <img src="https://latex.codecogs.com/png.latex?P%20%5Ctimes%20P"> correlation matrix <img src="https://latex.codecogs.com/png.latex?R">, it has density: <img src="https://latex.codecogs.com/png.latex?%0Ap(R%20%5Cmid%20%5Ceta)%20%5Cpropto%20%7CR%7C%5E%7B%5Ceta%20-%201%7D%0A"> When <img src="https://latex.codecogs.com/png.latex?%5Ceta%20=%201"> the prior is uniform over correlation matrices; when <img src="https://latex.codecogs.com/png.latex?%5Ceta%20%3E%201"> it concentrates toward the identity. This works well for small, unstructured problems.</p>
<p>But many multivariate datasets have <strong>known structure</strong>. Consider <img src="https://latex.codecogs.com/png.latex?N"> subjects each measured at <img src="https://latex.codecogs.com/png.latex?M"> time points, giving <img src="https://latex.codecogs.com/png.latex?P%20=%20NM"> total dimensions. The correlation matrix is not an arbitrary <img src="https://latex.codecogs.com/png.latex?P%20%5Ctimes%20P"> matrix as it has a natural decomposition into within-subject temporal correlations and between-subject cross-correlations. Slapping a single LKJ prior on the full matrix creates three problems:</p>
<ol type="1">
<li><p><strong>Uniform shrinkage</strong>: LKJ treats every off-diagonal entry the same. It penalizes strong within-subject temporal correlation just as hard as strong cross-subject correlation, even though these have completely different interpretations and plausible ranges.</p></li>
<li><p><strong>Dimension-dependent concentration</strong>: LKJ(<img src="https://latex.codecogs.com/png.latex?%5Ceta">) concentrates harder as <img src="https://latex.codecogs.com/png.latex?P"> grows. Adding more subjects (increasing <img src="https://latex.codecogs.com/png.latex?N">) makes the prior shrink all correlations toward zero.</p></li>
<li><p><strong>No elicitation lever</strong>: A practitioner might know that temporal autocorrelation is strong while cross-subject coupling is weak. With a single <img src="https://latex.codecogs.com/png.latex?%5Ceta"> parameter for the full matrix, there is no way to encode this.</p></li>
</ol>
<p>In this post I place LKJ priors on the <strong>building blocks</strong> of the correlation matrix instead of the assembled matrix. The compound-symmetric block-Toeplitz structure makes this decomposition tractable and the PACF parameterization makes it computationally clean.</p>
<p>This matters most in the regime where data are genuinely scarce. In longitudinal, multilevel, and panel settings, it is common to have fewer than 15 independent observational units: pilot studies, rare-disease cohorts, small classroom or clinic samples, intensive repeated-measures designs, and expensive laboratory experiments all produce this pattern. In those settings the number of correlation parameters implied by the full matrix can easily dwarf the amount of information in the data. That is exactly when we need to exploit structure rather than pretend the correlation matrix is unstructured.</p>
</section>
<section id="the-compound-symmetric-block-toeplitz-structure" class="level2">
<h2 class="anchored" data-anchor-id="the-compound-symmetric-block-toeplitz-structure">2. The Compound-Symmetric Block-Toeplitz Structure</h2>
<section id="definition" class="level3">
<h3 class="anchored" data-anchor-id="definition">2.1 Definition</h3>
<p>The model is easiest to define through two Toeplitz covariance components: <img src="https://latex.codecogs.com/png.latex?%0A%5CSigma_A%20=%20%5Csigma_A%5E2%20T_A,%20%5Cqquad%0A%5CSigma_B%20=%20%5Csigma_B%5E2%20T_B,%0A"> where <img src="https://latex.codecogs.com/png.latex?T_A"> and <img src="https://latex.codecogs.com/png.latex?T_B"> are <img src="https://latex.codecogs.com/png.latex?M%20%5Ctimes%20M"> Toeplitz correlation matrices and <img src="https://latex.codecogs.com/png.latex?%0A%5Csigma_A%5E2%20=%20s%20%5Ccdot%20%5Cfrac%7BN%7D%7BN%20-%201%7D,%20%5Cqquad%20%5Csigma_B%5E2%20=%20N(1-s),%0A"> with a single mixing parameter <img src="https://latex.codecogs.com/png.latex?s%20%5Cin%20(0,%201)">.</p>
<p>These two components induce the full <img src="https://latex.codecogs.com/png.latex?P%20%5Ctimes%20P"> block matrix through the mean/deviation decomposition used in the likelihood. The diagonal blocks are <img src="https://latex.codecogs.com/png.latex?%0AR_%7B%5Ctext%7Bon%7D%7D%20=%20%5Cfrac%7B(N-1)%5CSigma_A%20+%20%5CSigma_B%7D%7BN%7D%0A=%20s%20T_A%20+%20(1-s)%20T_B,%0A"> and the off-diagonal blocks are <img src="https://latex.codecogs.com/png.latex?%0AR_%7B%5Ctext%7Boff%7D%7D%20=%20%5Cfrac%7B%5CSigma_B%20-%20%5CSigma_A%7D%7BN%7D%0A=%20(1-s)%20T_B%20-%20%5Cfrac%7Bs%7D%7BN-1%7D%20T_A.%0A"> Equivalently, the full matrix can be written as <img src="https://latex.codecogs.com/png.latex?%0AR%20=%20I_N%20%5Cotimes%20R_%7B%5Ctext%7Bon%7D%7D%20+%20(J_N%20-%20I_N)%20%5Cotimes%20R_%7B%5Ctext%7Boff%7D%7D,%0A"> where <img src="https://latex.codecogs.com/png.latex?J_N%20=%20%5Cmathbf%7B1%7D_N%20%5Cmathbf%7B1%7D_N%5E%5Ctop">.</p>
<p>This is the exact structure implemented in the Stan model. The key simplification is that every off-diagonal block is determined by the same two Toeplitz pieces and the same scalar <img src="https://latex.codecogs.com/png.latex?s">, so we never parameterize each block separately.</p>
<p>Interpretation:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?T_A"> governs the <strong>block-specific</strong> temporal component</li>
<li><img src="https://latex.codecogs.com/png.latex?T_B"> governs the <strong>shared across-block</strong> temporal component</li>
<li><img src="https://latex.codecogs.com/png.latex?s"> controls the balance: high <img src="https://latex.codecogs.com/png.latex?s"> emphasizes block-specific structure and weakens cross-block dependence; low <img src="https://latex.codecogs.com/png.latex?s"> increases the shared component</li>
</ul>
</section>
<section id="pacf-parameterization" class="level3">
<h3 class="anchored" data-anchor-id="pacf-parameterization">2.2 PACF Parameterization</h3>
<p>A Toeplitz correlation matrix is positive definite if and only if all its partial autocorrelation (PACF) coefficients <img src="https://latex.codecogs.com/png.latex?%5Cpsi_1,%20%5Cldots,%20%5Cpsi_%7BM-1%7D%20%5Cin%20(-1,%201)"> <span class="citation" data-cites="barndorff1973">(Barndorff-Nielsen and Schou 1973)</span>. I map unconstrained parameters <img src="https://latex.codecogs.com/png.latex?%5Cpsi_%7B%5Ctext%7Braw%7D%7D%20%5Cin%20%5Cmathbb%7BR%7D%5ED"> to this interval via a scaled <img src="https://latex.codecogs.com/png.latex?%5Ctanh">: <img src="https://latex.codecogs.com/png.latex?%0A%5Cpsi_k%20=%20%5Ctanh%5C!%5Cleft(%5Cfrac%7B%5Cpsi_%7B%5Ctext%7Braw%7D,%20k%7D%7D%7B%5Csqrt%7BD%20-%201%7D%7D%5Cright)%0A"> then recover the autocorrelation function (ACF) using the Durbin-Levinson recursion <span class="citation" data-cites="durbinlevinson levinson1946">(Durbin 1960; Levinson 1946)</span>. This guarantees positive definiteness by construction.</p>
</section>
</section>
<section id="block-level-lkj-priors" class="level2">
<h2 class="anchored" data-anchor-id="block-level-lkj-priors">3. Block-Level LKJ Priors</h2>
<section id="the-standard-full-matrix-approach" class="level3">
<h3 class="anchored" data-anchor-id="the-standard-full-matrix-approach">3.1 The Standard (Full-Matrix) Approach</h3>
<p>The standard LKJ prior on the full compound-symmetric matrix uses the log-determinant of the assembled <img src="https://latex.codecogs.com/png.latex?P%20%5Ctimes%20P"> matrix: <img src="https://latex.codecogs.com/png.latex?%0A%5Clog%20p_%7B%5Ctext%7Bfull%7D%7D(%5Cpsi_A,%20%5Cpsi_B,%20s%20%5Cmid%20%5Ceta)%20=%20(%5Ceta%20-%201)%20%5Clog%7CR(%5Cpsi_A,%20%5Cpsi_B,%20s)%7C%20+%20%5Clog%7CJ_%7B%5Ctext%7BCS%7D%7D%7C%0A"> where <img src="https://latex.codecogs.com/png.latex?%7CJ_%7B%5Ctext%7BCS%7D%7D%7C"> is the Jacobian from the compound-symmetric parameterization. The full log-determinant decomposes as: <img src="https://latex.codecogs.com/png.latex?%0A%5Clog%7CR%7C%20=%20(N-1)%5CBig(M%20%5Clog%20%5Ctfrac%7BsN%7D%7BN-1%7D%20+%20%5Clog%7CT_A%7C%5CBig)%20+%20%5CBig(M%20%5Clog%20N(1-s)%20+%20%5Clog%7CT_B%7C%5CBig)%0A"></p>
<p>The problem is visible in this formula: the prior penalty on <img src="https://latex.codecogs.com/png.latex?T_A"> is amplified by <img src="https://latex.codecogs.com/png.latex?(N-1)(%5Ceta%20-%201)">. <strong>Adding subjects inflates the prior shrinkage on the block-specific temporal autocorrelation parameters in</strong> <img src="https://latex.codecogs.com/png.latex?T_A">, even though the underlying temporal structure hasn’t changed at all. By contrast, the <img src="https://latex.codecogs.com/png.latex?T_B"> term does not pick up the same <img src="https://latex.codecogs.com/png.latex?(N-1)"> amplification.</p>
</section>
<section id="the-innovation-priors-on-the-blocks" class="level3">
<h3 class="anchored" data-anchor-id="the-innovation-priors-on-the-blocks">3.2 The Innovation: Priors on the Blocks</h3>
<p>Instead of a single LKJ on <img src="https://latex.codecogs.com/png.latex?R">, we place <strong>independent LKJ priors directly on</strong> <img src="https://latex.codecogs.com/png.latex?T_A"> and <img src="https://latex.codecogs.com/png.latex?T_B">: <img src="https://latex.codecogs.com/png.latex?%0A%5Clog%20p_%7B%5Ctext%7Bblock%7D%7D(%5Cpsi_A,%20%5Cpsi_B%20%5Cmid%20%5Ceta_A,%20%5Ceta_B)%20=%20(%5Ceta_A%20-%201)%20%5Clog%7CT_A(%5Cpsi_A)%7C%20+%20%5Clog%7CJ_A%7C%20+%20(%5Ceta_B%20-%201)%20%5Clog%7CT_B(%5Cpsi_B)%7C%20+%20%5Clog%7CJ_B%7C%0A"> where <img src="https://latex.codecogs.com/png.latex?%7CJ_A%7C"> and <img src="https://latex.codecogs.com/png.latex?%7CJ_B%7C"> are the Jacobians for each Toeplitz block’s PACF-to-correlation transformation.</p>
<p>This gives us three advantages:</p>
<ol type="1">
<li><p><strong>Independent control</strong>: Set <img src="https://latex.codecogs.com/png.latex?%5Ceta_A%20=%204"> (stronger shrinkage on the block-specific component) while using <img src="https://latex.codecogs.com/png.latex?%5Ceta_B%20=%202"> (milder shrinkage on the shared component), or any other combination.</p></li>
<li><p><strong>Invariant to</strong> <img src="https://latex.codecogs.com/png.latex?N">: The prior on <img src="https://latex.codecogs.com/png.latex?T_A"> depends only on the <img src="https://latex.codecogs.com/png.latex?M%20%5Ctimes%20M"> block, not on how many subjects there are. Adding subjects brings more data, not more prior shrinkage on the block-specific Toeplitz component.</p></li>
<li><p><strong>Interpretable elicitation</strong>: “I believe the block-specific component is weak but the shared temporal component can persist” translates directly to <img src="https://latex.codecogs.com/png.latex?%5Ceta_A%20%3E%20%5Ceta_B">.</p></li>
</ol>
</section>
<section id="why-the-decomposition-works" class="level3">
<h3 class="anchored" data-anchor-id="why-the-decomposition-works">3.3 Why the Decomposition Works</h3>
<p>The PACF parameterization makes this possible because:</p>
<ul>
<li>Each Toeplitz block is parameterized by an <strong>independent</strong> PACF vector</li>
<li>The Jacobian of the PACF <img src="https://latex.codecogs.com/png.latex?%5Cto"> Toeplitz transformation factorizes across blocks</li>
<li>The log-determinant of the full matrix decomposes into per-block terms (as shown above)</li>
</ul>
<p>So the change-of-variables formula applies cleanly to each component, giving us a well-defined density on the building blocks without needing to reason about the assembled <img src="https://latex.codecogs.com/png.latex?P%20%5Ctimes%20P"> matrix at all.</p>
</section>
</section>
<section id="efficient-likelihood" class="level2">
<h2 class="anchored" data-anchor-id="efficient-likelihood">4. Efficient Likelihood</h2>
<p>The compound-symmetric structure also enables an efficient likelihood. The quadratic form decomposes into <img src="https://latex.codecogs.com/png.latex?M%20%5Ctimes%20M"> Toeplitz systems:</p>
<p>For each observation, define the block mean <img src="https://latex.codecogs.com/png.latex?%5Cbar%7B%5Cmathbf%7Bz%7D%7D%20=%20%5Cfrac%7B1%7D%7BN%7D%5Csum_n%20%5Cmathbf%7Bz%7D%5E%7B(n)%7D"> and deviations <img src="https://latex.codecogs.com/png.latex?%5Ctilde%7B%5Cmathbf%7Bz%7D%7D%5E%7B(n)%7D%20=%20%5Cmathbf%7Bz%7D%5E%7B(n)%7D%20-%20%5Cbar%7B%5Cmathbf%7Bz%7D%7D">. Then: <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7Bz%7D%5E%5Ctop%20%5CSigma%5E%7B-1%7D%20%5Cmathbf%7Bz%7D%20=%20N%5C,%5Cbar%7B%5Cmathbf%7Bz%7D%7D%5E%5Ctop%20%5CSigma_B%5E%7B-1%7D%20%5Cbar%7B%5Cmathbf%7Bz%7D%7D%20+%20%5Csum_%7Bn=1%7D%5E%7BN%7D%20%5Ctilde%7B%5Cmathbf%7Bz%7D%7D%5E%7B(n)%5Ctop%7D%20%5CSigma_A%5E%7B-1%7D%20%5Ctilde%7B%5Cmathbf%7Bz%7D%7D%5E%7B(n)%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?%5CSigma_A%20=%20%5Cfrac%7BsN%7D%7BN-1%7DT_A"> and <img src="https://latex.codecogs.com/png.latex?%5CSigma_B%20=%20N(1-s)T_B">. Each Toeplitz solve uses the innovations algorithm <span class="citation" data-cites="brockwell2002">(Brockwell and Davis 2002)</span> in <img src="https://latex.codecogs.com/png.latex?O(M%5E2)"> time. The total cost is <img src="https://latex.codecogs.com/png.latex?O(NM%5E2)"> per observation instead of <img src="https://latex.codecogs.com/png.latex?O(P%5E3)%20=%20O(N%5E3M%5E3)">.</p>
</section>
<section id="simulation-study" class="level2">
<h2 class="anchored" data-anchor-id="simulation-study">5. Simulation Study</h2>
<section id="setup" class="level3">
<h3 class="anchored" data-anchor-id="setup">5.1 Setup</h3>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">library</span>(cmdstanr)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mvtnorm)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(bayesplot)</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../R/theme_blog.R"</span>)</span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_blog_bayesplot</span>()</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">model <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;">cmdstan_model</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"compound-symmetric-block-toeplitz.stan"</span>)</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">squash_pacf <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>(psi_raw) {</span>
<span id="cb3-2">  D <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;">length</span>(psi_raw)</span>
<span id="cb3-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (D <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</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;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tanh</span>(psi_raw))</span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tanh</span>(psi_raw <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>(D <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">}</span>
<span id="cb3-6"></span>
<span id="cb3-7">pacf_to_acf <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>(psi_raw) {</span>
<span id="cb3-8">  D <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;">length</span>(psi_raw)</span>
<span id="cb3-9">  rho <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;">numeric</span>(D <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-10">  rho[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span></span>
<span id="cb3-11">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (D <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;">return</span>(rho)</span>
<span id="cb3-12"></span>
<span id="cb3-13">  psi <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;">squash_pacf</span>(psi_raw)</span>
<span id="cb3-14">  phi <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;">numeric</span>(D)</span>
<span id="cb3-15"></span>
<span id="cb3-16">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>D) {</span>
<span id="cb3-17">    psi_k <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> psi[k]</span>
<span id="cb3-18">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (k <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;">1</span>) {</span>
<span id="cb3-19">      phi_prev <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> phi[<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>(k <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-20">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(k <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-21">        phi[j] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> phi_prev[j] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> psi_k <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> phi_prev[k <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> j]</span>
<span id="cb3-22">      }</span>
<span id="cb3-23">    }</span>
<span id="cb3-24">    phi[k] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> psi_k</span>
<span id="cb3-25">    rho[k <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="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(phi[<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>k] <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;">rev</span>(rho[<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>k]))</span>
<span id="cb3-26">  }</span>
<span id="cb3-27"></span>
<span id="cb3-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(rho)</span>
<span id="cb3-29">}</span>
<span id="cb3-30"></span>
<span id="cb3-31">build_toeplitz <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>(r) {</span>
<span id="cb3-32">  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;">length</span>(r)</span>
<span id="cb3-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">outer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(n), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i, j) r[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> j) <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-34">}</span>
<span id="cb3-35"></span>
<span id="cb3-36">build_full_R <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>(psiA, psiB, s, M, N) {</span>
<span id="cb3-37">  rhoA <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;">pacf_to_acf</span>(psiA)</span>
<span id="cb3-38">  rhoB <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;">pacf_to_acf</span>(psiB)</span>
<span id="cb3-39"></span>
<span id="cb3-40">  sigmaA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (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="cb3-41">  sigmaB <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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> s) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N</span>
<span id="cb3-42">  a <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sigmaA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> rhoA</span>
<span id="cb3-43">  b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sigmaB <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> rhoB</span>
<span id="cb3-44"></span>
<span id="cb3-45">  r_on  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ((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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> a <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N</span>
<span id="cb3-46">  r_off <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (b <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> a) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N</span>
<span id="cb3-47"></span>
<span id="cb3-48">  T_on  <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;">build_toeplitz</span>(r_on)</span>
<span id="cb3-49">  T_off <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;">build_toeplitz</span>(r_off)</span>
<span id="cb3-50"></span>
<span id="cb3-51">  P <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> M <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N</span>
<span id="cb3-52">  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;">matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, P, P)</span>
<span id="cb3-53">  <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="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>(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="cb3-54">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>(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="cb3-55">      idx_i <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (i <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;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>((i <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> M)</span>
<span id="cb3-56">      idx_j <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (j <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;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>((j <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> M)</span>
<span id="cb3-57">      R[idx_i, idx_j] <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> (i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> j) T_on <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> T_off</span>
<span id="cb3-58">    }</span>
<span id="cb3-59">  R</span>
<span id="cb3-60">}</span></code></pre></div></div>
</details>
</div>
</section>
<section id="prior-predictive-comparison" class="level3">
<h3 class="anchored" data-anchor-id="prior-predictive-comparison">5.2 Prior Predictive Comparison</h3>
<p>Before fitting any data, let’s see what the two priors imply about the lag-1 autocorrelation in <img src="https://latex.codecogs.com/png.latex?T_A">. To do this we can sample from the Stan model directly with no observations and the posterior is the prior, so these draws come from the exact prior induced by the model we will fit later.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">sample_prior_lag1 <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_blocks, prior_on_full_corr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>) {</span>
<span id="cb4-2">  K <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> M <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N_blocks</span>
<span id="cb4-3"></span>
<span id="cb4-4">  fit_prior <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> model<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>
<span id="cb4-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb4-6">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">M_blocksize =</span> M,</span>
<span id="cb4-7">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N_blocks =</span> N_blocks,</span>
<span id="cb4-8">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta_lkj_a =</span> eta,</span>
<span id="cb4-9">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta_lkj_b =</span> eta,</span>
<span id="cb4-10">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb4-11">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> K,</span>
<span id="cb4-12">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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;">numeric</span>(<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;">nrow =</span> <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;">ncol =</span> K),</span>
<span id="cb4-13">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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>, K),</span>
<span id="cb4-14">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior_on_full_corr =</span> prior_on_full_corr</span>
<span id="cb4-15">    ),</span>
<span id="cb4-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb4-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb4-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb4-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,</span>
<span id="cb4-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> seed,</span>
<span id="cb4-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb4-22">  )</span>
<span id="cb4-23"></span>
<span id="cb4-24">  draws_A <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fit_prior<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;">draws</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"psi_A_raw"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"matrix"</span>)</span>
<span id="cb4-25">  lag1_A <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>(draws_A, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacf_to_acf</span>(x)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb4-26"></span>
<span id="cb4-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb4-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lag1 =</span> lag1_A,</span>
<span id="cb4-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Prior =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (prior_on_full_corr <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full-Matrix LKJ"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Block-Level LKJ"</span>,</span>
<span id="cb4-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N_blocks =</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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N[blocks] == "</span>, N_blocks), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N[blocks] == "</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;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>)))</span>
<span id="cb4-31">  )</span>
<span id="cb4-32">}</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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"><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;">42</span>)</span>
<span id="cb5-2">M <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb5-3">eta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb5-4"></span>
<span id="cb5-5">prior_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> 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;">bind_rows</span>(</span>
<span id="cb5-6">  <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;">c</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;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(n_blocks) {</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;">bind_rows</span>(</span>
<span id="cb5-8">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_prior_lag1</span>(n_blocks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior_on_full_corr =</span> <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;">eta =</span> eta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbinom</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>)),</span>
<span id="cb5-9">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample_prior_lag1</span>(n_blocks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior_on_full_corr =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta =</span> eta, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbinom</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>))</span>
<span id="cb5-10">    )</span>
<span id="cb5-11">  })</span>
<span id="cb5-12">)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Running MCMC with 1 chain...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Chain 1 finished in 0.1 seconds.
Running MCMC with 1 chain...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Chain 1 finished in 0.1 seconds.
Running MCMC with 1 chain...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Chain 1 finished in 0.1 seconds.
Running MCMC with 1 chain...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Chain 1 finished in 0.1 seconds.
Running MCMC with 1 chain...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Chain 1 finished in 0.2 seconds.
Running MCMC with 1 chain...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Chain 1 finished in 0.2 seconds.
Running MCMC with 1 chain...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Chain 1 finished in 0.8 seconds.
Running MCMC with 1 chain...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Chain 1 finished in 0.9 seconds.</code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(prior_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> lag1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Prior, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Prior)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>N_blocks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labeller =</span> label_parsed) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal, blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal, blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb15-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lag-1 ACF of "</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> T[A]),</span>
<span id="cb15-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>,</span>
<span id="cb15-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Prior on the Lag-1 ACF of "</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> T[A] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ("</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> eta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" = 4)"</span>),</span>
<span id="cb15-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Block-level prior is invariant to "</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N[blocks] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"; full-matrix prior shrinks toward zero as "</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N[blocks] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" grows"</span>)</span>
<span id="cb15-12">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-prior-predictive" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-prior-predictive-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-5-compound-symmetric-block-toeplitz/compound-symmetric_files/figure-html/fig-prior-predictive-1.png" class="img-fluid figure-img" style="width:100.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-prior-predictive-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Prior-implied distribution of the lag-1 ACF of T_A under block-level LKJ (stable across N_blocks) versus full-matrix LKJ (shrinks as N_blocks grows). Both use eta = 4 on T_A.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The pattern is clear: the block-level prior (teal) stays put regardless of how many blocks we add. The full-matrix prior (purple) collapses toward zero as <img src="https://latex.codecogs.com/png.latex?N_%7B%5Ctext%7Bblocks%7D%7D"> grows. We see that the LKJ prior increasingly regularizes against strong temporal autocorrelation, not because the data say so, but because the prior penalizes the determinant of a larger and larger matrix.</p>
</section>
<section id="data-generation" class="level3">
<h3 class="anchored" data-anchor-id="data-generation">5.3 Data Generation</h3>
<p>I simulate a deliberately prior-sensitive regime with 8 blocks of size 5 and only 10 observations. The block-specific Toeplitz component is weak, while the shared component is stronger and more structured, with <img src="https://latex.codecogs.com/png.latex?s%20=%200.30"> placing most of the within-block signal on <img src="https://latex.codecogs.com/png.latex?T_B">. This is exactly the setting where a practitioner would want to shrink <img src="https://latex.codecogs.com/png.latex?T_A"> harder than <img src="https://latex.codecogs.com/png.latex?T_B">.</p>
<p>This kind of sample size is not contrived. If the observational unit is a person, classroom, clinic, or experimental batch, it is easy to end up with single-digit or low-teens sample sizes while still recording multiple repeated measurements within each unit. The structure in the covariance is then the main source of statistical leverage: without it, estimation is driven too heavily by the geometry of a large generic correlation prior rather than by defensible scientific assumptions about how the measurements are related.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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"><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;">42</span>)</span>
<span id="cb16-2"></span>
<span id="cb16-3">M_blocksize <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb16-4">N_blocks    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span></span>
<span id="cb16-5">D           <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> M_blocksize <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="cb16-6">M_total     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> N_blocks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> M_blocksize</span>
<span id="cb16-7">N_obs       <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb16-8"></span>
<span id="cb16-9">true_s         <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.30</span></span>
<span id="cb16-10">true_psi_A_raw <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;">c</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.3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># weak block-specific component</span></span>
<span id="cb16-11">true_psi_B_raw <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;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</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;">1.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># strong shared temporal component</span></span>
<span id="cb16-12"></span>
<span id="cb16-13">R_true <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;">build_full_R</span>(true_psi_A_raw, true_psi_B_raw, true_s,</span>
<span id="cb16-14">                        M_blocksize, N_blocks)</span>
<span id="cb16-15"></span>
<span id="cb16-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Verify positive definiteness</span></span>
<span id="cb16-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eigen</span>(R_true, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">only.values =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>values <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;">0</span>))</span>
<span id="cb16-18"></span>
<span id="cb16-19">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;">rmvnorm</span>(N_obs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> R_true)</span>
<span id="cb16-20"></span>
<span id="cb16-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True psi_A (constrained):"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">squash_pacf</span>(true_psi_A_raw), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>True psi_A (constrained): -0.171 0.058 0 0 </code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True psi_B (constrained):"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">squash_pacf</span>(true_psi_B_raw), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>True psi_B (constrained): 0.227 -0.562 0.281 0 </code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True lag-1 ACF(T_A):"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacf_to_acf</span>(true_psi_A_raw)[<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;">3</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>True lag-1 ACF(T_A): -0.171 </code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True lag-1 ACF(T_B):"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacf_to_acf</span>(true_psi_B_raw)[<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;">3</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>True lag-1 ACF(T_B): 0.227 </code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True within-block lag-1 correlation:"</span>,</span>
<span id="cb24-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(true_s <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;">pacf_to_acf</span>(true_psi_A_raw)[<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="cb24-3">            (<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> true_s) <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;">pacf_to_acf</span>(true_psi_B_raw)[<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;">3</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>True within-block lag-1 correlation: 0.107 </code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True s:"</span>, true_s, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>True s: 0.3 </code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dimensions: N_obs ="</span>, N_obs, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", M ="</span>, M_blocksize,</span>
<span id="cb28-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", N_blocks ="</span>, N_blocks, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", P ="</span>, M_total, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Dimensions: N_obs = 10 , M = 5 , N_blocks = 8 , P = 40 </code></pre>
</div>
</div>
</section>
<section id="visualizing-the-true-correlation-matrix" class="level3">
<h3 class="anchored" data-anchor-id="visualizing-the-true-correlation-matrix">5.4 Visualizing the True Correlation Matrix</h3>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">corr_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;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Row =</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>M_total, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Col =</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>M_total) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb30-2">  <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;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(R_true))</span>
<span id="cb30-3"></span>
<span id="cb30-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(corr_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Col, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Row, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradient2</span>(</span>
<span id="cb30-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">low =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blue, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mid =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">high =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>red,</span>
<span id="cb30-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">midpoint =</span> <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;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Correlation"</span></span>
<span id="cb30-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_reverse</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Column"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Row"</span>,</span>
<span id="cb30-14">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True Correlation Matrix"</span>,</span>
<span id="cb30-15">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(N_blocks, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" blocks of size "</span>, M_blocksize, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", s = "</span>, true_s))</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-true-corr" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-true-corr-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-5-compound-symmetric-block-toeplitz/compound-symmetric_files/figure-html/fig-true-corr-1.png" class="img-fluid figure-img" style="width:80.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-true-corr-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: True compound-symmetric block-Toeplitz correlation matrix. The diagonal blocks mix a weak block-specific component with a strong shared temporal component.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="head-to-head-domain-informed-vs-full-matrix-priors" class="level3">
<h3 class="anchored" data-anchor-id="head-to-head-domain-informed-vs-full-matrix-priors">5.5 Head-to-Head: Domain-Informed vs Full-Matrix Priors</h3>
<p>The key advantage of block-level priors is <strong>independent control</strong>. A practitioner who believes the block-specific component should be tightly regularized but the shared component should remain more flexible can set <img src="https://latex.codecogs.com/png.latex?%5Ceta_A%20=%204"> and <img src="https://latex.codecogs.com/png.latex?%5Ceta_B%20=%202">. The full-matrix LKJ prior has a single <img src="https://latex.codecogs.com/png.latex?%5Ceta"> that applies everywhere.</p>
<p>I fit the same data with both approaches, but this is <strong>not</strong> a calibration-matched comparison in the sense of equal induced priors on <img src="https://latex.codecogs.com/png.latex?T_A"> and <img src="https://latex.codecogs.com/png.latex?T_B">. The block-level prior is applied directly to the component Toeplitz matrices, while the full-matrix LKJ prior is applied to the assembled <img src="https://latex.codecogs.com/png.latex?P%20%5Ctimes%20P"> correlation matrix. That means the induced shrinkage on <img src="https://latex.codecogs.com/png.latex?T_A">, <img src="https://latex.codecogs.com/png.latex?T_B">, and <img src="https://latex.codecogs.com/png.latex?s"> is different by construction and depends on the full block layout. The point of the comparison is exactly that the block-level formulation can target the two components separately, whereas the full-matrix LKJ cannot.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit 1: Block-level LKJ priors -- domain-informed</span></span>
<span id="cb31-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   eta_A = 4 (shrink the block-specific component)</span></span>
<span id="cb31-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   eta_B = 2 (leave the shared component more flexible)</span></span>
<span id="cb31-4">fit_block <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> model<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>
<span id="cb31-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb31-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">M_blocksize =</span> M_blocksize, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N_blocks =</span> N_blocks,</span>
<span id="cb31-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta_lkj_a =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta_lkj_b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb31-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> N_obs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> M_total, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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>, M_total),</span>
<span id="cb31-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior_on_full_corr =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb31-10">  ),</span>
<span id="cb31-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb31-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb31-13">)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Running MCMC with 4 parallel chains...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Chain 1 finished in 1.8 seconds.
Chain 2 finished in 1.8 seconds.
Chain 3 finished in 1.9 seconds.
Chain 4 finished in 1.8 seconds.

All 4 chains finished successfully.
Mean chain execution time: 1.8 seconds.
Total execution time: 2.0 seconds.</code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit 2: Single-eta full-matrix LKJ prior on the assembled correlation matrix</span></span>
<span id="cb34-2">fit_full <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> model<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>
<span id="cb34-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb34-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">M_blocksize =</span> M_blocksize, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N_blocks =</span> N_blocks,</span>
<span id="cb34-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta_lkj_a =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">eta_lkj_b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb34-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> N_obs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> M_total, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</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>, M_total),</span>
<span id="cb34-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prior_on_full_corr =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb34-8">  ),</span>
<span id="cb34-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb34-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb34-11">)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Running MCMC with 4 parallel chains...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Chain 1 finished in 1.7 seconds.
Chain 3 finished in 1.7 seconds.
Chain 4 finished in 1.7 seconds.
Chain 2 finished in 1.8 seconds.

All 4 chains finished successfully.
Mean chain execution time: 1.7 seconds.
Total execution time: 1.8 seconds.</code></pre>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1">fit_block<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;">diagnostic_summary</span>()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>$num_divergent
[1] 0 0 0 0

$num_max_treedepth
[1] 0 0 0 0

$ebfmi
[1] 1.198795 1.169541 0.944361 1.046228</code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1">fit_full<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;">diagnostic_summary</span>()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>$num_divergent
[1] 0 0 0 0

$num_max_treedepth
[1] 0 0 0 0

$ebfmi
[1] 1.214923 1.062168 1.090818 1.122228</code></pre>
</div>
</div>
</section>
<section id="recovery-of-interpretable-lag-1-correlations-and-the-mixing-weight" class="level3">
<h3 class="anchored" data-anchor-id="recovery-of-interpretable-lag-1-correlations-and-the-mixing-weight">5.6 Recovery of Interpretable Lag-1 Correlations and the Mixing Weight</h3>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1">extract_lag1_metrics <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>(fit, prior_label) {</span>
<span id="cb41-2">  draws_A <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fit<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;">draws</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"psi_A_raw"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"matrix"</span>)</span>
<span id="cb41-3">  draws_B <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fit<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;">draws</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"psi_B_raw"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"matrix"</span>)</span>
<span id="cb41-4">  draws_s <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.numeric</span>(fit<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;">draws</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"matrix"</span>)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb41-5"></span>
<span id="cb41-6">  lag1_A <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>(draws_A, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacf_to_acf</span>(x)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb41-7">  lag1_B <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>(draws_B, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacf_to_acf</span>(x)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb41-8">  lag1_within <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> draws_s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> lag1_A <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> draws_s) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> lag1_B</span>
<span id="cb41-9"></span>
<span id="cb41-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb41-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Prior =</span> prior_label,</span>
<span id="cb41-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;">Lag-1 ACF of T_A</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;">=</span> lag1_A,</span>
<span id="cb41-13">    <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;">Lag-1 ACF of T_B</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;">=</span> lag1_B,</span>
<span id="cb41-14">    <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;">Within-block lag-1 correlation</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;">=</span> lag1_within,</span>
<span id="cb41-15">    <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;">Mixing weight s</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;">=</span> draws_s,</span>
<span id="cb41-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">check.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb41-17">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb41-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Prior, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Quantity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"value"</span>)</span>
<span id="cb41-19">}</span>
<span id="cb41-20"></span>
<span id="cb41-21">lag1_metrics <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;">bind_rows</span>(</span>
<span id="cb41-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_lag1_metrics</span>(fit_block, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Block-Level LKJ"</span>),</span>
<span id="cb41-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_lag1_metrics</span>(fit_full, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full-Matrix LKJ"</span>)</span>
<span id="cb41-24">)</span>
<span id="cb41-25"></span>
<span id="cb41-26">quantity_levels <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;">rev</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb41-27">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mixing weight s"</span>,</span>
<span id="cb41-28">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lag-1 ACF of T_A"</span>,</span>
<span id="cb41-29">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lag-1 ACF of T_B"</span>,</span>
<span id="cb41-30">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Within-block lag-1 correlation"</span></span>
<span id="cb41-31">))</span>
<span id="cb41-32"></span>
<span id="cb41-33">lag1_summary <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lag1_metrics <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb41-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(Prior, Quantity) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb41-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb41-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q5 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(value, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>),</span>
<span id="cb41-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">median =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(value),</span>
<span id="cb41-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q95 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(value, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>),</span>
<span id="cb41-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span></span>
<span id="cb41-40">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb41-41">  <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;">Quantity =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Quantity, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> quantity_levels))</span>
<span id="cb41-42"></span>
<span id="cb41-43">true_lag1_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>
<span id="cb41-44">  <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;">factor</span>(</span>
<span id="cb41-45">    <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;">"Lag-1 ACF of T_A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lag-1 ACF of T_B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Within-block lag-1 correlation"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mixing weight s"</span>),</span>
<span id="cb41-46">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> quantity_levels</span>
<span id="cb41-47">  ),</span>
<span id="cb41-48">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">true_value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb41-49">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacf_to_acf</span>(true_psi_A_raw)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb41-50">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacf_to_acf</span>(true_psi_B_raw)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb41-51">    true_s <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;">pacf_to_acf</span>(true_psi_A_raw)[<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="cb41-52">      (<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> true_s) <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;">pacf_to_acf</span>(true_psi_B_raw)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb41-53">    true_s</span>
<span id="cb41-54">  )</span>
<span id="cb41-55">)</span>
<span id="cb41-56"></span>
<span id="cb41-57"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(lag1_summary, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> median, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Quantity, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Prior)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-58">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_linerange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmin =</span> q5, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xmax =</span> q95), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-59">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-60">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> true_lag1_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> true_value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Quantity),</span>
<span id="cb41-61">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb41-62">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>red, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stroke =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Prior) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-64">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal, blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-65">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-66">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb41-67">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value"</span>,</span>
<span id="cb41-68">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb41-69">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Recovery of Interpretable Lag-1 Correlation Summaries and s"</span>,</span>
<span id="cb41-70">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Block-level: LKJ("</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> eta[A] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" = 4, "</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> eta[B] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" = 2) versus full-matrix LKJ("</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> eta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" = 4)"</span>)</span>
<span id="cb41-71">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-72">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-recovery" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-recovery-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-5-compound-symmetric-block-toeplitz/compound-symmetric_files/figure-html/fig-recovery-1.png" class="img-fluid figure-img" style="width:100.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-recovery-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Posterior 90% intervals for interpretable lag-1 correlation summaries and the mixing weight under both prior approaches. Red crosses mark the true values.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="correlation-matrix-recovery" class="level3">
<h3 class="anchored" data-anchor-id="correlation-matrix-recovery">5.7 Correlation Matrix Recovery</h3>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1">R_block <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;">matrix</span>(fit_block<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;">summary</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean, M_total, M_total)</span>
<span id="cb42-2">R_full  <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;">matrix</span>(fit_full<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;">summary</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean, M_total, M_total)</span>
<span id="cb42-3"></span>
<span id="cb42-4">make_corr_df <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>(R, label) {</span>
<span id="cb42-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Row =</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>M_total, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Col =</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>M_total) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb42-6">    <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;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.vector</span>(R), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Matrix =</span> label)</span>
<span id="cb42-7">}</span>
<span id="cb42-8"></span>
<span id="cb42-9">lims <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;">c</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb42-10"></span>
<span id="cb42-11">p_true <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;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_corr_df</span>(R_true, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Col, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Row, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradient2</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">low =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blue, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mid =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">high =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>red,</span>
<span id="cb42-14">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">midpoint =</span> <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;">limits =</span> lims, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_reverse</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;">coord_equal</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;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb42-17"></span>
<span id="cb42-18">p_block_corr <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;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_corr_df</span>(R_block, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Block-Level"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Col, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Row, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradient2</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">low =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blue, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mid =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">high =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>red,</span>
<span id="cb42-21">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">midpoint =</span> <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;">limits =</span> lims, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_reverse</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;">coord_equal</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;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Block-Level: "</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> eta[A] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"=4, "</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> eta[B] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"=2"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb42-24"></span>
<span id="cb42-25">p_full_corr <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;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_corr_df</span>(R_full, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full-Matrix"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> Col, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Row, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_tile</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_gradient2</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">low =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>blue, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mid =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">high =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>red,</span>
<span id="cb42-28">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">midpoint =</span> <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;">limits =</span> lims, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"r"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_reverse</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;">coord_equal</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;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expression</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full-Matrix: "</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> eta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"=4"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb42-31"></span>
<span id="cb42-32">p_true <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_block_corr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_full_corr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</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>
<span id="cb42-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_annotation</span>(</span>
<span id="cb42-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Correlation Matrix Recovery"</span>,</span>
<span id="cb42-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theme =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>()</span>
<span id="cb42-37">  )</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-corr-comparison" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-corr-comparison-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-5-compound-symmetric-block-toeplitz/compound-symmetric_files/figure-html/fig-corr-comparison-1.png" class="img-fluid figure-img" style="width:100.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-corr-comparison-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Recovered correlation matrices under both prior approaches.
</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1">scatter_block <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="cb43-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">True =</span> R_true[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lower.tri</span>(R_true)],</span>
<span id="cb43-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Estimated =</span> R_block[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lower.tri</span>(R_block)],</span>
<span id="cb43-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Prior =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Block-Level LKJ"</span></span>
<span id="cb43-5">)</span>
<span id="cb43-6">scatter_full <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="cb43-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">True =</span> R_true[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lower.tri</span>(R_true)],</span>
<span id="cb43-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Estimated =</span> R_full[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lower.tri</span>(R_full)],</span>
<span id="cb43-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Prior =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full-Matrix LKJ"</span></span>
<span id="cb43-10">)</span>
<span id="cb43-11">scatter_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;">rbind</span>(scatter_block, scatter_full)</span>
<span id="cb43-12"></span>
<span id="cb43-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(scatter_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> True, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> Estimated)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb43-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_abline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slope =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">intercept =</span> <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;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>red,</span>
<span id="cb43-15">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb43-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Prior), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb43-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Prior) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb43-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal, blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb43-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <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="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="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>
<span id="cb43-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb43-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"True Correlation"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Estimated Correlation"</span>,</span>
<span id="cb43-22">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Element-wise Correlation Recovery"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb43-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-scatter-comparison" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-scatter-comparison-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-5-compound-symmetric-block-toeplitz/compound-symmetric_files/figure-html/fig-scatter-comparison-1.png" class="img-fluid figure-img" style="width:100.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-scatter-comparison-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: Element-wise recovery of the lower triangle under both prior approaches.
</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1">block_id <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;">rep</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>N_blocks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> M_blocksize)</span>
<span id="cb44-2">within_mask <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;">outer</span>(block_id, block_id, <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lower.tri</span>(R_true)</span>
<span id="cb44-3">between_mask <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;">outer</span>(block_id, block_id, <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lower.tri</span>(R_true)</span>
<span id="cb44-4"></span>
<span id="cb44-5">rmse_within_block <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((R_true[within_mask] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R_block[within_mask])<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="cb44-6">rmse_within_full  <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((R_true[within_mask] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R_full[within_mask])<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="cb44-7">rmse_between_block <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((R_true[between_mask] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R_block[between_mask])<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="cb44-8">rmse_between_full  <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((R_true[between_mask] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R_full[between_mask])<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="cb44-9">rmse_overall_block <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((R_true[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lower.tri</span>(R_true)] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R_block[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lower.tri</span>(R_block)])<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="cb44-10">rmse_overall_full  <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>((R_true[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lower.tri</span>(R_true)] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> R_full[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lower.tri</span>(R_full)])<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="cb44-11"></span>
<span id="cb44-12">rmse_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>
<span id="cb44-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Component =</span> <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;">"Within-block entries"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Between-block entries"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Overall"</span>),</span>
<span id="cb44-14">  <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;">Block-Level LKJ</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rmse_within_block, rmse_between_block, rmse_overall_block),</span>
<span id="cb44-15">  <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;">Full-Matrix LKJ</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;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(rmse_within_full, rmse_between_full, rmse_overall_full)</span>
<span id="cb44-16">)</span>
<span id="cb44-17"></span>
<span id="cb44-18">knitr<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;">kable</span>(rmse_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">Component</th>
<th style="text-align: right;">Block.Level.LKJ</th>
<th style="text-align: right;">Full.Matrix.LKJ</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Within-block entries</td>
<td style="text-align: right;">0.0323</td>
<td style="text-align: right;">0.1161</td>
</tr>
<tr class="even">
<td style="text-align: left;">Between-block entries</td>
<td style="text-align: right;">0.0289</td>
<td style="text-align: right;">0.1567</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Overall</td>
<td style="text-align: right;">0.0293</td>
<td style="text-align: right;">0.1530</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>The RMSE split shows exactly why block-level priors matter here: they let us regularize the weak block-specific component without over-shrinking the strong shared component. In this regime the block-level prior wins on the within-block entries, the between-block entries, and the overall matrix RMSE.</p>
</section>
<section id="posterior-on-the-within-block-lag-1-correlation-and-s" class="level3">
<h3 class="anchored" data-anchor-id="posterior-on-the-within-block-lag-1-correlation-and-s">5.8 Posterior on the Within-Block Lag-1 Correlation and s</h3>
<p>The most practically relevant quantity is the lag-1 correlation inside a diagonal block: <img src="https://latex.codecogs.com/png.latex?%0A%5Crho_%7B%5Ctext%7Bwithin%7D,%201%7D%20=%20s%20%5Crho_%7BA,%201%7D%20+%20(1%20-%20s)%20%5Crho_%7BB,%201%7D.%0A"> This is what determines how strongly adjacent time points within the same subject are correlated. The other key scalar is <img src="https://latex.codecogs.com/png.latex?s">, which controls how much of that within-block dependence is attributed to the block-specific component versus the shared component. With <img src="https://latex.codecogs.com/png.latex?%5Ceta_A%20=%204"> the block-level prior shrinks the weak block-specific component, while <img src="https://latex.codecogs.com/png.latex?%5Ceta_B%20=%202"> leaves the shared component flexible enough to match the data. The full-matrix LKJ(4) prior cannot separate these roles, so it ends up shrinking both the lag-1 summary and the implied decomposition differently.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1">draws_block_A <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fit_block<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;">draws</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"psi_A_raw"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"matrix"</span>)</span>
<span id="cb45-2">draws_block_B <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fit_block<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;">draws</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"psi_B_raw"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"matrix"</span>)</span>
<span id="cb45-3">draws_block_s <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.numeric</span>(fit_block<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;">draws</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"matrix"</span>)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb45-4"></span>
<span id="cb45-5">draws_full_A <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fit_full<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;">draws</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"psi_A_raw"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"matrix"</span>)</span>
<span id="cb45-6">draws_full_B <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fit_full<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;">draws</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"psi_B_raw"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"matrix"</span>)</span>
<span id="cb45-7">draws_full_s <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.numeric</span>(fit_full<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;">draws</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"s"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"matrix"</span>)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb45-8"></span>
<span id="cb45-9">lag1_A_block <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>(draws_block_A, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacf_to_acf</span>(x)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb45-10">lag1_B_block <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>(draws_block_B, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacf_to_acf</span>(x)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb45-11">lag1_A_full  <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>(draws_full_A, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacf_to_acf</span>(x)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb45-12">lag1_B_full  <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>(draws_full_B, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pacf_to_acf</span>(x)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])</span>
<span id="cb45-13"></span>
<span id="cb45-14">lag1_block  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> draws_block_s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> lag1_A_block <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> draws_block_s) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> lag1_B_block</span>
<span id="cb45-15">lag1_full   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> draws_full_s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> lag1_A_full  <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> draws_full_s) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> lag1_B_full</span>
<span id="cb45-16"></span>
<span id="cb45-17">true_lag1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> true_s <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;">pacf_to_acf</span>(true_psi_A_raw)[<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="cb45-18">  (<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> true_s) <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;">pacf_to_acf</span>(true_psi_B_raw)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb45-19"></span>
<span id="cb45-20">lag1_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>
<span id="cb45-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lag1_block, lag1_full, draws_block_s, draws_full_s),</span>
<span id="cb45-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Prior =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb45-23">    <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Block-Level LKJ"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full-Matrix LKJ"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(lag1_block)),</span>
<span id="cb45-24">    <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Block-Level LKJ"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Full-Matrix LKJ"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(draws_block_s))</span>
<span id="cb45-25">  ),</span>
<span id="cb45-26">  <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;">c</span>(</span>
<span id="cb45-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Within-block lag-1 correlation"</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;">length</span>(lag1_block)),</span>
<span id="cb45-28">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mixing weight s"</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;">length</span>(draws_block_s))</span>
<span id="cb45-29">  )</span>
<span id="cb45-30">)</span>
<span id="cb45-31"></span>
<span id="cb45-32">true_posterior_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>
<span id="cb45-33">  <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Within-block lag-1 correlation"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mixing weight s"</span>),</span>
<span id="cb45-34">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">true_value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(true_lag1, true_s)</span>
<span id="cb45-35">)</span>
<span id="cb45-36"></span>
<span id="cb45-37"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(lag1_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> Prior)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(density)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>,</span>
<span id="cb45-39">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> true_posterior_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> true_value),</span>
<span id="cb45-41">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>red, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb45-42">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal, blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(Quantity <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Prior, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free_x"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb45-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value"</span>,</span>
<span id="cb45-48">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>,</span>
<span id="cb45-49">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Posterior on the Within-Block Lag-1 Correlation and s"</span>,</span>
<span id="cb45-50">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Red line = true value"</span></span>
<span id="cb45-51">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div id="fig-lag1-s-posterior" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-lag1-s-posterior-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-5-compound-symmetric-block-toeplitz/compound-symmetric_files/figure-html/fig-lag1-s-posterior-1.png" class="img-fluid figure-img" style="width:90.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-lag1-s-posterior-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;6: Posterior distributions of the actual within-block lag-1 correlation and the mixing weight s under both priors.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="when-to-use-which-prior" class="level2">
<h2 class="anchored" data-anchor-id="when-to-use-which-prior">6. When to Use Which Prior</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Scenario</th>
<th style="text-align: left;">Recommended Prior</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Domain knowledge that within- and between-block correlation differ</td>
<td style="text-align: left;">Block-level: set <img src="https://latex.codecogs.com/png.latex?%5Ceta_A%20%5Cneq%20%5Ceta_B"></td>
</tr>
<tr class="even">
<td style="text-align: left;">Many blocks (<img src="https://latex.codecogs.com/png.latex?N"> large)</td>
<td style="text-align: left;">Block-level: avoids <img src="https://latex.codecogs.com/png.latex?N">-dependent shrinkage on <img src="https://latex.codecogs.com/png.latex?T_A"></td>
</tr>
<tr class="odd">
<td style="text-align: left;">Want a single “regularize toward identity” prior and <img src="https://latex.codecogs.com/png.latex?P"> is small</td>
<td style="text-align: left;">Full-matrix LKJ is fine</td>
</tr>
<tr class="even">
<td style="text-align: left;">No block structure at all</td>
<td style="text-align: left;">Standard LKJ (this model doesn’t apply)</td>
</tr>
</tbody>
</table>
<p>There is no free lunch, the full-matrix LKJ prior provides a single style of regularization, which can still be useful when there is little structural knowledge. The block-level prior’s advantage is not about always having lower error, it’s about <strong>encoding domain knowledge that the full-matrix prior cannot represent</strong>. When a practitioner believes the block-specific and shared components should be regularized differently, the block-level prior translates that directly into <img src="https://latex.codecogs.com/png.latex?%5Ceta_A%20%5Cneq%20%5Ceta_B">. The full-matrix prior has no mechanism for this asymmetry.</p>
</section>
<section id="discussion" class="level2">
<h2 class="anchored" data-anchor-id="discussion">7. Discussion</h2>
<p>The key insight is structural: <strong>when a correlation matrix decomposes into interpretable components, the prior should too</strong>. The PACF parameterization enables this by giving each Toeplitz block an independent parameter vector with a clean Jacobian. The compound-symmetric block-Toeplitz structure then provides the scaffold that turns two small priors into a valid prior on the full <img src="https://latex.codecogs.com/png.latex?P%20%5Ctimes%20P"> matrix.</p>
<p>The practical value is prior elicitation. In most panel or longitudinal settings, a researcher has different beliefs about temporal dependence within subjects versus cross-sectional dependence between subjects. Block-level priors make these beliefs expressible; the full-matrix LKJ prior forces a single <img src="https://latex.codecogs.com/png.latex?%5Ceta"> that governs everything.</p>
<p>An alternative route is to place a prior directly on the Toeplitz row through a spectral parameterization, such as a finite mixture of AR(1) components, and pair it with Trench-style Toeplitz linear algebra <span class="citation" data-cites="trench1985inverse trench1985eigen">(Trench 1985a, 1985b)</span>. Compared with that approach, the model in this post makes a different tradeoff. The spectral formulation is natural when the main object of interest is a single Toeplitz covariance and the prior should act directly on its spectrum. Here the goal is different: we want a block-specific Toeplitz component and a shared Toeplitz component, each with its own prior strength, combined through one scalar mixing parameter. The PACF parameterization makes each Toeplitz block valid by construction, while the compound-symmetric decomposition makes the full block matrix valid by construction. That is why this formulation is a better fit for prior elicitation on “within-block” versus “shared-across-block” correlation, even though a Trench-based implementation would be a strong alternative for pure Toeplitz models.</p>
<p>This principle generalizes beyond the specific structure shown here. Any time a large correlation matrix is built from smaller pieces – Kronecker products, block-diagonal structures, factor models – it’s worth asking whether priors on the pieces give better behavior than a single prior on the assembly.</p>
<p>The Stan implementation exposes both options via a single flag (<code>prior_on_full_corr</code>), making it easy to compare in practice. The efficient innovations-based likelihood handles the computation regardless of which prior is used.</p>
</section>
<section id="appendix-stan-code" class="level2">
<h2 class="anchored" data-anchor-id="appendix-stan-code">Appendix: Stan Code</h2>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>compound-symmetric-block-toeplitz.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb46" data-filename="compound-symmetric-block-toeplitz.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb46-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb46-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Squashes unconstrained partial autocorrelation (PACF) parameters to [-1, 1].</span></span>
<span id="cb46-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Uses tanh transformation with scaling to maintain numerical stability.</span></span>
<span id="cb46-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psi_raw A vector of unconstrained PACF parameters</span></span>
<span id="cb46-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return A vector of squashed PACF parameters in [-1, 1]</span></span>
<span id="cb46-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-9">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> squash_pacf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psi_raw) {</span>
<span id="cb46-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(psi_raw);</span>
<span id="cb46-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (D <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;">1</span>) {</span>
<span id="cb46-12">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tanh</span>(psi_raw);</span>
<span id="cb46-13">    }</span>
<span id="cb46-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tanh</span>(psi_raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(D <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.0</span>));</span>
<span id="cb46-15">  }</span>
<span id="cb46-16"></span>
<span id="cb46-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Converts PACF coefficients to ACF coefficients using the Durbin-Levinson</span></span>
<span id="cb46-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * algorithm. This is essential for constructing valid Toeplitz correlation</span></span>
<span id="cb46-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * matrices from PACF parameters.</span></span>
<span id="cb46-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psi_raw A vector of D unconstrained PACF parameters</span></span>
<span id="cb46-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return A vector of D+1 ACF coefficients [rho_0, rho_1, ..., rho_D],</span></span>
<span id="cb46-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *         where rho_0 = 1</span></span>
<span id="cb46-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-26">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> pacf_to_acf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psi_raw) {</span>
<span id="cb46-27">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(psi_raw);</span>
<span id="cb46-28">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[D <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>] rho;</span>
<span id="cb46-29">    rho[<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> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>;</span>
<span id="cb46-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (D <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;">0</span>) {</span>
<span id="cb46-31">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> rho;</span>
<span id="cb46-32">    }</span>
<span id="cb46-33">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[D] psi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> squash_pacf(psi_raw);</span>
<span id="cb46-34">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[D] phi;</span>
<span id="cb46-35">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>D) {</span>
<span id="cb46-36">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> psi_k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> psi[k];</span>
<span id="cb46-37">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> phi_k_k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> psi_k;</span>
<span id="cb46-38">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb46-39">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</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;">1</span>] phi_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(phi, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</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;">1</span>);</span>
<span id="cb46-40">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><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;">1</span>)) {</span>
<span id="cb46-41">          phi[j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> phi_prev[j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> psi_k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> phi_prev[k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> j];</span>
<span id="cb46-42">        }</span>
<span id="cb46-43">      }</span>
<span id="cb46-44">      phi[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> phi_k_k;</span>
<span id="cb46-45">      rho[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;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(phi, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, k),</span>
<span id="cb46-46">                               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reverse</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(rho, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, k)));</span>
<span id="cb46-47">    }</span>
<span id="cb46-48">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> rho;</span>
<span id="cb46-49">  }</span>
<span id="cb46-50"></span>
<span id="cb46-51">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Builds a compound symmetric block-Toeplitz correlation matrix.</span></span>
<span id="cb46-53"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * The correlation matrix has the form:</span></span>
<span id="cb46-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *   R = s * I_N (x) T_A + (1-s) * 1_N 1_N' (x) T_B</span></span>
<span id="cb46-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-57"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * where T_A and T_B are Toeplitz correlation matrices parametrized by</span></span>
<span id="cb46-58"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * their PACF coefficients, I_N is the NxN identity matrix, and 1_N is</span></span>
<span id="cb46-59"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * a vector of ones.</span></span>
<span id="cb46-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psiA_raw Unconstrained PACF parameters for within-block correlations</span></span>
<span id="cb46-62"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psiB_raw Unconstrained PACF parameters for between-block correlations</span></span>
<span id="cb46-63"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param s Mixing parameter in (0, 1)</span></span>
<span id="cb46-64"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param M_blocksize Dimension of each block</span></span>
<span id="cb46-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param N_blocks Number of blocks</span></span>
<span id="cb46-66"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return The M_total x M_total correlation matrix where M_total = M_blocksize * N_blocks</span></span>
<span id="cb46-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-68">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> build_cs_block_toeplitz_corr(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psiA_raw,</span>
<span id="cb46-69">                                      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psiB_raw,</span>
<span id="cb46-70">                                      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> s,</span>
<span id="cb46-71">                                      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> M_blocksize,</span>
<span id="cb46-72">                                      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N_blocks) {</span>
<span id="cb46-73">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> M_total <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> M_blocksize <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N_blocks;</span>
<span id="cb46-74">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[M_total, M_total] R;</span>
<span id="cb46-75"></span>
<span id="cb46-76">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[M_blocksize] rho_A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pacf_to_acf(psiA_raw);</span>
<span id="cb46-77">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[M_blocksize] rho_B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pacf_to_acf(psiB_raw);</span>
<span id="cb46-78"></span>
<span id="cb46-79">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[M_blocksize] rho_on_vec <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> rho_A <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.0</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> rho_B;</span>
<span id="cb46-80">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[M_blocksize] rho_off_vec <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.0</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> rho_B <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> (N_blocks <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.0</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> rho_A;</span>
<span id="cb46-81"></span>
<span id="cb46-82">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[M_blocksize, M_blocksize] rho_on;</span>
<span id="cb46-83">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[M_blocksize, M_blocksize] rho_off;</span>
<span id="cb46-84">    <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="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>M_blocksize) {</span>
<span id="cb46-85">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> i<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>M_blocksize) {</span>
<span id="cb46-86">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> val_on <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rho_on_vec[j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> i <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="cb46-87">        rho_on[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> val_on;</span>
<span id="cb46-88">        rho_on[j, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> val_on;</span>
<span id="cb46-89"></span>
<span id="cb46-90">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> val_off <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rho_off_vec[j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> i <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="cb46-91">        rho_off[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> val_off;</span>
<span id="cb46-92">        rho_off[j, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> val_off;</span>
<span id="cb46-93">      }</span>
<span id="cb46-94">    }</span>
<span id="cb46-95"></span>
<span id="cb46-96">    <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="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_blocks) {</span>
<span id="cb46-97">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> r1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (i <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> M_blocksize <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="cb46-98">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> r2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> M_blocksize;</span>
<span id="cb46-99"></span>
<span id="cb46-100">      R[r1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>r2, r1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>r2] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rho_on;</span>
<span id="cb46-101"></span>
<span id="cb46-102">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (i <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>N_blocks) {</span>
<span id="cb46-103">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> c1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (j <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> M_blocksize <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="cb46-104">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> c2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> M_blocksize;</span>
<span id="cb46-105">        R[r1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>r2, c1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>c2] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rho_off;</span>
<span id="cb46-106">        R[c1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>c2, r1<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>r2] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rho_off;</span>
<span id="cb46-107">      }</span>
<span id="cb46-108">    }</span>
<span id="cb46-109"></span>
<span id="cb46-110">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> R;</span>
<span id="cb46-111">  }</span>
<span id="cb46-112"></span>
<span id="cb46-113">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-114"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Computes the log-determinant of a Toeplitz covariance matrix</span></span>
<span id="cb46-115"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * using the innovations algorithm. Optimized for the case where</span></span>
<span id="cb46-116"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * no quadratic form is needed.</span></span>
<span id="cb46-117"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-118"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param c The autocovariance vector (first row of the covariance matrix).</span></span>
<span id="cb46-119"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return The log determinant.</span></span>
<span id="cb46-120"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-121">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> innovations_log_det_only(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> c) {</span>
<span id="cb46-122">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(c);</span>
<span id="cb46-123">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> EPS <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;">1e-12</span>;</span>
<span id="cb46-124"></span>
<span id="cb46-125">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (n <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;">0</span>) {</span>
<span id="cb46-126">      <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.0</span>;</span>
<span id="cb46-127">    }</span>
<span id="cb46-128">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (c[<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;">&lt;=</span> EPS) {</span>
<span id="cb46-129">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variance c[1] must be positive."</span>);</span>
<span id="cb46-130">    }</span>
<span id="cb46-131"></span>
<span id="cb46-132">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[n] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> rev_idx;</span>
<span id="cb46-133">    <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="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>
<span id="cb46-134">      rev_idx[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> i <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="cb46-135">    }</span>
<span id="cb46-136"></span>
<span id="cb46-137">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_det <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.0</span>;</span>
<span id="cb46-138">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[n <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>] phi_vec <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_vector</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, n <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="cb46-139">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> v_prev;</span>
<span id="cb46-140"></span>
<span id="cb46-141">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> v_curr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>];</span>
<span id="cb46-142">    log_det <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(v_curr);</span>
<span id="cb46-143">    v_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v_curr;</span>
<span id="cb46-144"></span>
<span id="cb46-145">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb46-146">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> ar_order <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;">1</span>;</span>
<span id="cb46-147"></span>
<span id="cb46-148">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> s <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.0</span>;</span>
<span id="cb46-149">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (ar_order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb46-150">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[ar_order <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="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> sub_rev_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rev_idx[(n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (ar_order <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> <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>
<span id="cb46-151">        s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(phi_vec, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ar_order <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="cb46-152">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(c, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, ar_order <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>)[sub_rev_idx]);</span>
<span id="cb46-153">      }</span>
<span id="cb46-154">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> reflection_coeff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</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;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> EPS, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> EPS, (c[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> v_prev));</span>
<span id="cb46-155"></span>
<span id="cb46-156">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (ar_order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb46-157">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[ar_order <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>] phi_temp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(phi_vec, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ar_order <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="cb46-158">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[ar_order <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="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> sub_rev_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rev_idx[(n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (ar_order <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> <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>
<span id="cb46-159">        phi_vec[<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>(ar_order <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> phi_temp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> reflection_coeff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> phi_temp[sub_rev_idx];</span>
<span id="cb46-160">      }</span>
<span id="cb46-161">      phi_vec[ar_order] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> reflection_coeff;</span>
<span id="cb46-162"></span>
<span id="cb46-163">      v_curr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v_prev <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.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(reflection_coeff));</span>
<span id="cb46-164"></span>
<span id="cb46-165">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (v_curr <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="cb46-166">        v_curr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EPS;</span>
<span id="cb46-167">      }</span>
<span id="cb46-168"></span>
<span id="cb46-169">      log_det <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(v_curr);</span>
<span id="cb46-170">      v_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v_curr;</span>
<span id="cb46-171">    }</span>
<span id="cb46-172"></span>
<span id="cb46-173">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> log_det;</span>
<span id="cb46-174">  }</span>
<span id="cb46-175"></span>
<span id="cb46-176">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-177"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Computes the quadratic form z' T^{-1} z for a Toeplitz covariance</span></span>
<span id="cb46-178"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * matrix T using the innovations algorithm.</span></span>
<span id="cb46-179"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-180"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param z The data vector of length n.</span></span>
<span id="cb46-181"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param c The autocovariance vector (first row of the covariance matrix).</span></span>
<span id="cb46-182"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return The quadratic form z' T^{-1} z.</span></span>
<span id="cb46-183"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-184">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> innovations_qf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> z, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> c) {</span>
<span id="cb46-185">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(z);</span>
<span id="cb46-186">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> EPS <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;">1e-12</span>;</span>
<span id="cb46-187"></span>
<span id="cb46-188">    <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;">rows</span>(c) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> n) {</span>
<span id="cb46-189">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"z and c must have the same dimension."</span>);</span>
<span id="cb46-190">    }</span>
<span id="cb46-191">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (n <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;">0</span>) {</span>
<span id="cb46-192">      <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.0</span>;</span>
<span id="cb46-193">    }</span>
<span id="cb46-194">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (c[<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;">&lt;=</span> EPS) {</span>
<span id="cb46-195">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Variance c[1] must be positive."</span>);</span>
<span id="cb46-196">    }</span>
<span id="cb46-197"></span>
<span id="cb46-198">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[n] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> rev_idx;</span>
<span id="cb46-199">    <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="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>
<span id="cb46-200">      rev_idx[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> i <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="cb46-201">    }</span>
<span id="cb46-202"></span>
<span id="cb46-203">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quad_form</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.0</span>;</span>
<span id="cb46-204">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[n <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>] phi_vec <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_vector</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, n <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="cb46-205">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> v_prev;</span>
<span id="cb46-206"></span>
<span id="cb46-207">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> v_curr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> c[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>];</span>
<span id="cb46-208">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quad_form</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(z[<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> v_curr;</span>
<span id="cb46-209">    v_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v_curr;</span>
<span id="cb46-210"></span>
<span id="cb46-211">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb46-212">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> ar_order <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;">1</span>;</span>
<span id="cb46-213">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> s <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.0</span>;</span>
<span id="cb46-214">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (ar_order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb46-215">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[ar_order <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="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> sub_rev_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rev_idx[(n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (ar_order <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> <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>
<span id="cb46-216">        s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(phi_vec, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ar_order <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="cb46-217">                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(c, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, ar_order <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>)[sub_rev_idx]);</span>
<span id="cb46-218">      }</span>
<span id="cb46-219">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> reflection_coeff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</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;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> EPS, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> EPS, (c[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> v_prev));</span>
<span id="cb46-220"></span>
<span id="cb46-221">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (ar_order <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb46-222">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[ar_order <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>] phi_temp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(phi_vec, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ar_order <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="cb46-223">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[ar_order <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="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> sub_rev_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rev_idx[(n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> (ar_order <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> <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>
<span id="cb46-224">        phi_vec[<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>(ar_order <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> phi_temp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> reflection_coeff <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> phi_temp[sub_rev_idx];</span>
<span id="cb46-225">      }</span>
<span id="cb46-226">      phi_vec[ar_order] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> reflection_coeff;</span>
<span id="cb46-227"></span>
<span id="cb46-228">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[ar_order] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> sub_rev_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rev_idx[(n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ar_order <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>n];</span>
<span id="cb46-229">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> pred <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_product</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(phi_vec, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ar_order),</span>
<span id="cb46-230">                              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(z, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ar_order)[sub_rev_idx]);</span>
<span id="cb46-231">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> error <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> z[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> pred;</span>
<span id="cb46-232"></span>
<span id="cb46-233">      v_curr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v_prev <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.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(reflection_coeff));</span>
<span id="cb46-234">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (v_curr <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="cb46-235">        v_curr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EPS;</span>
<span id="cb46-236">      }</span>
<span id="cb46-237">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quad_form</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(error) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> v_curr;</span>
<span id="cb46-238"></span>
<span id="cb46-239">      v_prev <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> v_curr;</span>
<span id="cb46-240">    }</span>
<span id="cb46-241">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quad_form</span>;</span>
<span id="cb46-242">  }</span>
<span id="cb46-243"></span>
<span id="cb46-244">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-245"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Vectorized log-determinant of Toeplitz correlation matrices</span></span>
<span id="cb46-246"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * parametrized by PACF coefficients.</span></span>
<span id="cb46-247"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-248"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psi_raw Array of unconstrained PACF parameter vectors</span></span>
<span id="cb46-249"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return Vector of log-determinants</span></span>
<span id="cb46-250"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-251">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> log_det_toeplitz_corr_array(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psi_raw) {</span>
<span id="cb46-252">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(psi_raw);</span>
<span id="cb46-253">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(psi_raw[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]);</span>
<span id="cb46-254">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (D <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;">0</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_vector</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, N);</span>
<span id="cb46-255">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> M <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> D <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="cb46-256">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> de <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(D <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.0</span>);</span>
<span id="cb46-257">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] ld <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (M <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tanh</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">to_vector</span>(psi_raw[<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> de)));</span>
<span id="cb46-258">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>D) {</span>
<span id="cb46-259">      ld <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (M <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tanh</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">to_vector</span>(psi_raw[<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> de)));</span>
<span id="cb46-260">    }</span>
<span id="cb46-261">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> ld;</span>
<span id="cb46-262">  }</span>
<span id="cb46-263"></span>
<span id="cb46-264">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-265"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Log determinant of Jacobian for PACF to covariance transformation.</span></span>
<span id="cb46-266"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-267"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psi_raw Unconstrained PACF parameters</span></span>
<span id="cb46-268"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return Log determinant of Jacobian</span></span>
<span id="cb46-269"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-270">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_det_block_jac(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psi_raw) {</span>
<span id="cb46-271">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(psi_raw);</span>
<span id="cb46-272">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (D <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;">0</span>) <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;">0</span>;</span>
<span id="cb46-273">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[D] psi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> squash_pacf(psi_raw);</span>
<span id="cb46-274">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> ld <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</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> <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> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(D <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> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>;</span>
<span id="cb46-275">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>D)</span>
<span id="cb46-276">      ld <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (D <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;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(psi[k]));</span>
<span id="cb46-277">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> ld;</span>
<span id="cb46-278">  }</span>
<span id="cb46-279"></span>
<span id="cb46-280">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-281"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Log determinant of Jacobian for compound symmetric transformation.</span></span>
<span id="cb46-282"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-283"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psiA_raw PACF parameters for within-block correlations</span></span>
<span id="cb46-284"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psiB_raw PACF parameters for between-block correlations</span></span>
<span id="cb46-285"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param s Mixing parameter in (0, 1)</span></span>
<span id="cb46-286"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param N_blocks Number of blocks</span></span>
<span id="cb46-287"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return Log determinant of Jacobian</span></span>
<span id="cb46-288"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-289">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_det_jac_cs(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psiA_raw,</span>
<span id="cb46-290">                      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psiB_raw,</span>
<span id="cb46-291">                      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> s,</span>
<span id="cb46-292">                      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N_blocks) {</span>
<span id="cb46-293">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(psiA_raw);</span>
<span id="cb46-294">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> ld <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> log_det_block_jac(psiA_raw) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> log_det_block_jac(psiB_raw);</span>
<span id="cb46-295">    ld <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(s));</span>
<span id="cb46-296">    ld <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (D <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> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(N_blocks) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(N_blocks <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="cb46-297">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> ld;</span>
<span id="cb46-298">  }</span>
<span id="cb46-299"></span>
<span id="cb46-300">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-301"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Computes the log-determinant of a compound symmetric block-Toeplitz</span></span>
<span id="cb46-302"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * correlation matrix.</span></span>
<span id="cb46-303"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-304"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psiA_raw PACF parameters for within-block correlations</span></span>
<span id="cb46-305"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psiB_raw PACF parameters for between-block correlations</span></span>
<span id="cb46-306"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param s Mixing parameter in (0, 1)</span></span>
<span id="cb46-307"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param M Dimension of each block</span></span>
<span id="cb46-308"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param N_blocks Number of blocks</span></span>
<span id="cb46-309"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return Log-determinant of the full correlation matrix</span></span>
<span id="cb46-310"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-311">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_det_corr_cs(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psiA_raw,</span>
<span id="cb46-312">                       <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psiB_raw,</span>
<span id="cb46-313">                       <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> s,</span>
<span id="cb46-314">                       <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> M,</span>
<span id="cb46-315">                       <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N_blocks) {</span>
<span id="cb46-316">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] log_dets <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> log_det_toeplitz_corr_array({psiA_raw, psiB_raw});</span>
<span id="cb46-317">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_sigmaA2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(N_blocks) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(N_blocks <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="cb46-318">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_sigmaB2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(N_blocks);</span>
<span id="cb46-319">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (N_blocks <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> (M <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> log_sigmaA2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> log_dets[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb46-320">         <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>                  (M <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> log_sigmaB2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> log_dets[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]);</span>
<span id="cb46-321">  }</span>
<span id="cb46-322"></span>
<span id="cb46-323">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-324"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Computes the log-PDF for an LKJ prior on a Toeplitz correlation matrix.</span></span>
<span id="cb46-325"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-326"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psi_raw A vector of D unconstrained PACF parameters.</span></span>
<span id="cb46-327"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param eta The shape parameter of the LKJ distribution (eta &gt; 0).</span></span>
<span id="cb46-328"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return The log-probability density, log p(T(psi_raw) | eta).</span></span>
<span id="cb46-329"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-330">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lkj_toeplitz_lpdf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psi_raw, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> eta) {</span>
<span id="cb46-331">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(psi_raw);</span>
<span id="cb46-332">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (D <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;">0</span>) {</span>
<span id="cb46-333">      <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.0</span>;</span>
<span id="cb46-334">    }</span>
<span id="cb46-335">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (eta <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="cb46-336">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LKJ shape parameter eta must be positive, but was "</span>, eta);</span>
<span id="cb46-337">    }</span>
<span id="cb46-338"></span>
<span id="cb46-339">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[D] psi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> squash_pacf(psi_raw);</span>
<span id="cb46-340"></span>
<span id="cb46-341">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Both log|T| and log|J| have weight (D-k+1) per PACF coefficient,</span></span>
<span id="cb46-342">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// so the combined density simplifies to eta * (D-k+1) * log(1 - psi_k^2).</span></span>
<span id="cb46-343">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// log|T| = sum_k (M-k) log(1-psi_k^2) where M = D+1, so M-k = D-k+1</span></span>
<span id="cb46-344">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// log|J| = sum_k (D-k+1) log(1-psi_k^2) - 0.5*D*log(D-1)</span></span>
<span id="cb46-345">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</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> <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> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(D <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.0</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;">0</span>;</span>
<span id="cb46-346">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>D) {</span>
<span id="cb46-347">      lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> eta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (D <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;">1</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(psi[k]));</span>
<span id="cb46-348">    }</span>
<span id="cb46-349"></span>
<span id="cb46-350">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> lp;</span>
<span id="cb46-351">  }</span>
<span id="cb46-352"></span>
<span id="cb46-353">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-354"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Places independent LKJ priors on the two component Toeplitz correlation</span></span>
<span id="cb46-355"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * matrices (T_A and T_B) of the compound-symmetric block-Toeplitz structure.</span></span>
<span id="cb46-356"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-357"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psiA_raw PACF parameters for the within-block correlation matrix T_A.</span></span>
<span id="cb46-358"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psiB_raw PACF parameters for the between-block correlation matrix T_B.</span></span>
<span id="cb46-359"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param eta_A LKJ shape parameter for T_A.</span></span>
<span id="cb46-360"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param eta_B LKJ shape parameter for T_B.</span></span>
<span id="cb46-361"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return The total log-probability density from both priors.</span></span>
<span id="cb46-362"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-363">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lkj_corr_cs_lpdf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psiA_raw,</span>
<span id="cb46-364">                        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psiB_raw,</span>
<span id="cb46-365">                        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> eta_A,</span>
<span id="cb46-366">                        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> eta_B) {</span>
<span id="cb46-367">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lkj_toeplitz_lpdf(psiA_raw | eta_A);</span>
<span id="cb46-368">    lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> lkj_toeplitz_lpdf(psiB_raw | eta_B);</span>
<span id="cb46-369">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> lp;</span>
<span id="cb46-370">  }</span>
<span id="cb46-371"></span>
<span id="cb46-372">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-373"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * LKJ prior for the full compound symmetric block-Toeplitz correlation matrix.</span></span>
<span id="cb46-374"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-375"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psiA_raw PACF parameters for within-block correlations</span></span>
<span id="cb46-376"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psiB_raw PACF parameters for between-block correlations</span></span>
<span id="cb46-377"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param s Mixing parameter in (0, 1)</span></span>
<span id="cb46-378"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param eta LKJ shape parameter</span></span>
<span id="cb46-379"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param M Dimension of each block</span></span>
<span id="cb46-380"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param N_blocks Number of blocks</span></span>
<span id="cb46-381"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return Log probability density</span></span>
<span id="cb46-382"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-383">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lkj_corr_compound_cs_lpdf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psiA_raw,</span>
<span id="cb46-384">                                 <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psiB_raw,</span>
<span id="cb46-385">                                 <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> s,</span>
<span id="cb46-386">                                 <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> eta,</span>
<span id="cb46-387">                                 <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> M,</span>
<span id="cb46-388">                                 <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N_blocks) {</span>
<span id="cb46-389">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> M <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N_blocks;</span>
<span id="cb46-390">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (eta <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> log_det_corr_cs(psiA_raw, psiB_raw, s,</span>
<span id="cb46-391">                                          M, N_blocks);</span>
<span id="cb46-392">    lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> log_det_jac_cs(psiA_raw, psiB_raw, s, N_blocks);</span>
<span id="cb46-393">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> lp;</span>
<span id="cb46-394">  }</span>
<span id="cb46-395"></span>
<span id="cb46-396">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb46-397"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Log-probability density for N i.i.d. observations from a multivariate normal</span></span>
<span id="cb46-398"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * with a compound-symmetric block-Toeplitz covariance structure.</span></span>
<span id="cb46-399"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-400"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Uses the innovations algorithm to avoid forming the full P x P matrix.</span></span>
<span id="cb46-401"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb46-402"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param y A matrix of observations (N_obs x P). Each row is one observation.</span></span>
<span id="cb46-403"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param mu A vector of means (P x 1).</span></span>
<span id="cb46-404"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psiA_raw Unconstrained PACF parameters for T_A (M-1 x 1).</span></span>
<span id="cb46-405"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param psiB_raw Unconstrained PACF parameters for T_B (M-1 x 1).</span></span>
<span id="cb46-406"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param s Mixing parameter in (0, 1).</span></span>
<span id="cb46-407"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param M Dimension of the inner Toeplitz blocks.</span></span>
<span id="cb46-408"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param N_blocks Number of blocks. P must equal M * N_blocks.</span></span>
<span id="cb46-409"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return The total log-probability density, log p(y | mu, Sigma).</span></span>
<span id="cb46-410"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb46-411">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> mvn_cs_block_toeplitz_dll_lpdf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> y,</span>
<span id="cb46-412">                                  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> mu,</span>
<span id="cb46-413">                                  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psiA_raw,</span>
<span id="cb46-414">                                  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> psiB_raw,</span>
<span id="cb46-415">                                  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> s,</span>
<span id="cb46-416">                                  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> M,</span>
<span id="cb46-417">                                  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N_blocks) {</span>
<span id="cb46-418">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N_obs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(y);</span>
<span id="cb46-419">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols</span>(y);</span>
<span id="cb46-420"></span>
<span id="cb46-421">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[M] rho_A <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pacf_to_acf(psiA_raw);</span>
<span id="cb46-422">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[M] rho_B <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pacf_to_acf(psiB_raw);</span>
<span id="cb46-423">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> sigA <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> N_blocks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (N_blocks <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.0</span>);</span>
<span id="cb46-424">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> sigB <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.0</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> N_blocks;</span>
<span id="cb46-425"></span>
<span id="cb46-426">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[M] a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigA <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> rho_A;</span>
<span id="cb46-427">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[M] b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigB <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> rho_B;</span>
<span id="cb46-428"></span>
<span id="cb46-429">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_det_a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> innovations_log_det_only(a);</span>
<span id="cb46-430">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> log_det_b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> innovations_log_det_only(b);</span>
<span id="cb46-431">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> total_log_det_constant <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N_obs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> ((N_blocks <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> log_det_a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> log_det_b);</span>
<span id="cb46-432"></span>
<span id="cb46-433">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> total_quad_form <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.0</span>;</span>
<span id="cb46-434">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> sqrtN_blocks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(N_blocks);</span>
<span id="cb46-435"></span>
<span id="cb46-436">    <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="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_obs) {</span>
<span id="cb46-437">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y[i]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mu;</span>
<span id="cb46-438"></span>
<span id="cb46-439">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[M] y_bar_sum <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_vector</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, M);</span>
<span id="cb46-440">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (n <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N_blocks) {</span>
<span id="cb46-441">        y_bar_sum <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(z, (n <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> M <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>, M);</span>
<span id="cb46-442">      }</span>
<span id="cb46-443">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[M] y_bar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> y_bar_sum <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N_blocks;</span>
<span id="cb46-444"></span>
<span id="cb46-445">      total_quad_form <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> innovations_qf(sqrtN_blocks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y_bar, b);</span>
<span id="cb46-446"></span>
<span id="cb46-447">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (n <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N_blocks) {</span>
<span id="cb46-448">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[M] z_tilde <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">segment</span>(z, (n <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> M <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>, M) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y_bar;</span>
<span id="cb46-449">        total_quad_form <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> innovations_qf(z_tilde, a);</span>
<span id="cb46-450">      }</span>
<span id="cb46-451">    }</span>
<span id="cb46-452"></span>
<span id="cb46-453">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</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> (N_obs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pi</span>()) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> total_log_det_constant <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> total_quad_form);</span>
<span id="cb46-454">  }</span>
<span id="cb46-455">}</span>
<span id="cb46-456"></span>
<span id="cb46-457"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb46-458">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">&gt;</span> M_blocksize;</span>
<span id="cb46-459">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">&gt;</span> N_blocks;</span>
<span id="cb46-460">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> eta_lkj_a;</span>
<span id="cb46-461">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> eta_lkj_b;</span>
<span id="cb46-462">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N;</span>
<span id="cb46-463">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K;</span>
<span id="cb46-464">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[N, K] y;</span>
<span id="cb46-465">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] mu;</span>
<span id="cb46-466">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</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;">&gt;</span> prior_on_full_corr;</span>
<span id="cb46-467">}</span>
<span id="cb46-468"></span>
<span id="cb46-469"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed data</span> {</span>
<span id="cb46-470">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> D <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> M_blocksize <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="cb46-471">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> M_total <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> N_blocks <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> M_blocksize;</span>
<span id="cb46-472">}</span>
<span id="cb46-473"></span>
<span id="cb46-474"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb46-475">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[D] psi_A_raw;</span>
<span id="cb46-476">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[D] psi_B_raw;</span>
<span id="cb46-477">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</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;">&gt;</span> s;</span>
<span id="cb46-478">}</span>
<span id="cb46-479"></span>
<span id="cb46-480"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb46-481">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (prior_on_full_corr <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="cb46-482">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> lkj_corr_compound_cs_lpdf(psi_A_raw | psi_B_raw, s,</span>
<span id="cb46-483">                                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>([eta_lkj_a, eta_lkj_b]), M_blocksize, N_blocks);</span>
<span id="cb46-484">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb46-485">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> lkj_corr_cs_lpdf(psi_A_raw | psi_B_raw, eta_lkj_a, eta_lkj_b);</span>
<span id="cb46-486">  }</span>
<span id="cb46-487"></span>
<span id="cb46-488">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> mvn_cs_block_toeplitz_dll_lpdf(y | mu,</span>
<span id="cb46-489">                        psi_A_raw, psi_B_raw,</span>
<span id="cb46-490">                        s, M_blocksize, N_blocks);</span>
<span id="cb46-491">}</span>
<span id="cb46-492"></span>
<span id="cb46-493"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb46-494">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[M_total, M_total] R <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> build_cs_block_toeplitz_corr(psi_A_raw, psi_B_raw, s, M_blocksize, N_blocks);</span>
<span id="cb46-495">}</span></code></pre></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-barndorff1973" class="csl-entry">
Barndorff-Nielsen, O., and G. Schou. 1973. <span>“On the Parametrization of Autoregressive Models by Partial Autocorrelations.”</span> <em>Journal of Multivariate Analysis</em> 3 (4): 408–19. <a href="https://doi.org/10.1016/0047-259X(73)90030-4">https://doi.org/10.1016/0047-259X(73)90030-4</a>.
</div>
<div id="ref-brockwell2002" class="csl-entry">
Brockwell, Peter J., and Richard A. Davis. 2002. <em>Introduction to Time Series and Forecasting</em>. 2nd ed. Springer. <a href="https://doi.org/10.1007/b97391">https://doi.org/10.1007/b97391</a>.
</div>
<div id="ref-carpenter2017" class="csl-entry">
Carpenter, Bob, Andrew Gelman, Matthew D. Hoffman, et al. 2017. <em>Stan: A Probabilistic Programming Language</em>. In <em>Journal of Statistical Software</em>, No. 1, vol. 76. <a href="https://doi.org/10.18637/jss.v076.i01">https://doi.org/10.18637/jss.v076.i01</a>.
</div>
<div id="ref-durbinlevinson" class="csl-entry">
Durbin, J. 1960. <span>“The Fitting of Time-Series Models.”</span> <em>Revue de l’Institut International de Statistique / Review of the International Statistical Institute</em> 28 (3): 233–44. <a href="https://doi.org/10.2307/1401322">https://doi.org/10.2307/1401322</a>.
</div>
<div id="ref-levinson1946" class="csl-entry">
Levinson, Norman. 1946. <span>“The <span>Wiener</span> (<span>Root</span> <span>Mean</span> <span>Square</span>) Error Criterion in Filter Design and Prediction.”</span> <em>Journal of Mathematics and Physics</em> 25 (1–4): 261–78. <a href="https://doi.org/10.1002/sapm1946251261">https://doi.org/10.1002/sapm1946251261</a>.
</div>
<div id="ref-lewandowski2009" class="csl-entry">
Lewandowski, Daniel, Dorota Kurowicka, and Harry Joe. 2009. <span>“Generating Random Correlation Matrices Based on Vines and Extended Onion Method.”</span> <em>Journal of Multivariate Analysis</em> 100 (9): 1989–2001. <a href="https://doi.org/10.1016/j.jmva.2009.04.008">https://doi.org/10.1016/j.jmva.2009.04.008</a>.
</div>
<div id="ref-trench1985inverse" class="csl-entry">
Trench, William F. 1985a. <span>“Explicit Inversion Formulas for Toeplitz Band Matrices.”</span> <em>SIAM Journal on Algebraic and Discrete Methods</em> 6 (4): 546–54. <a href="https://doi.org/10.1137/0606054">https://doi.org/10.1137/0606054</a>.
</div>
<div id="ref-trench1985eigen" class="csl-entry">
Trench, William F. 1985b. <span>“On the Eigenvalue Problem for Toeplitz Band Matrices.”</span> <em>Linear Algebra and Its Applications</em> 64: 199–214. <a href="https://doi.org/10.1016/0024-3795(85)90277-0">https://doi.org/10.1016/0024-3795(85)90277-0</a>.
</div>
</div></section></div> ]]></description>
  <category>stan</category>
  <category>correlation</category>
  <guid>https://spinkney.github.io/posts/post-5-compound-symmetric-block-toeplitz/compound-symmetric.html</guid>
  <pubDate>Tue, 31 Mar 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Latent Class Analysis on a Restricted Poincaré Disk</title>
  <dc:creator>Sean Pinkney</dc:creator>
  <link>https://spinkney.github.io/posts/post-4-poincare-lca/poincare-lca.html</link>
  <description><![CDATA[ 





<div id="setup" class="cell" data-execution_count="1">
<details class="code-fold">
<summary>Show the code</summary>
<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> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> logging</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> collections <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Counter</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.special <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> expit <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> sigmoid</span>
<span id="cb1-6"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> cmdstanpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> CmdStanModel</span>
<span id="cb1-7"><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-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> matplotlib.gridspec <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> gridspec</span>
<span id="cb1-9"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> matplotlib.colors <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> LinearSegmentedColormap</span>
<span id="cb1-10"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> subprocess</span>
<span id="cb1-11"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> textwrap</span>
<span id="cb1-12"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> shutil</span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Silence CmdStanPy INFO logs in rendered output.</span></span>
<span id="cb1-15">logging.getLogger(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmdstanpy"</span>).setLevel(logging.WARNING)</span>
<span id="cb1-16">logging.getLogger(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cmdstanpy"</span>).propagate <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;">False</span></span>
<span id="cb1-17"></span>
<span id="cb1-18">np.random.seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb1-19">blog <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb1-20">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"background"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#f0f2f5"</span>,</span>
<span id="cb1-21">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#24292e"</span>,</span>
<span id="cb1-22">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#6a737d"</span>,</span>
<span id="cb1-23">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"teal"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0d7d6c"</span>,</span>
<span id="cb1-24">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0366d6"</span>,</span>
<span id="cb1-25">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"purple"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#6f42c1"</span>,</span>
<span id="cb1-26">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gold"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#b08800"</span>,</span>
<span id="cb1-27">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#cb2431"</span>,</span>
<span id="cb1-28">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"navy"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#032f62"</span>,</span>
<span id="cb1-29">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grid"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e1e4e8"</span>,</span>
<span id="cb1-30">}</span>
<span id="cb1-31">colors <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"teal"</span>], blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>], blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"purple"</span>], blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gold"</span>], blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>], blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"navy"</span>]]</span>
<span id="cb1-32"></span>
<span id="cb1-33">plt.rcParams.update({</span>
<span id="cb1-34">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"figure.facecolor"</span>: blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"background"</span>],</span>
<span id="cb1-35">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"axes.facecolor"</span>: blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"background"</span>],</span>
<span id="cb1-36">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"axes.edgecolor"</span>: blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>],</span>
<span id="cb1-37">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"axes.labelcolor"</span>: blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>],</span>
<span id="cb1-38">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"axes.titlecolor"</span>: blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>],</span>
<span id="cb1-39">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text.color"</span>: blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>],</span>
<span id="cb1-40">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xtick.color"</span>: blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>],</span>
<span id="cb1-41">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ytick.color"</span>: blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>],</span>
<span id="cb1-42">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grid.color"</span>: blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grid"</span>],</span>
<span id="cb1-43">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grid.linestyle"</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>,</span>
<span id="cb1-44">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grid.linewidth"</span>: <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>,</span>
<span id="cb1-45">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"axes.grid"</span>: <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>,</span>
<span id="cb1-46">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"legend.facecolor"</span>: blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"background"</span>],</span>
<span id="cb1-47">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"legend.edgecolor"</span>: blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grid"</span>],</span>
<span id="cb1-48">})</span>
<span id="cb1-49"></span>
<span id="cb1-50"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> style_axes(ax, grid<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;">True</span>):</span>
<span id="cb1-51">    ax.set_facecolor(blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"background"</span>])</span>
<span id="cb1-52">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> grid:</span>
<span id="cb1-53">        ax.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grid"</span>], linewidth<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.6</span>, alpha<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.9</span>)</span>
<span id="cb1-54">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb1-55">        ax.grid(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb1-56">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> spine <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> ax.spines.values():</span>
<span id="cb1-57">        spine.set_color(blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>])</span>
<span id="cb1-58">        spine.set_linewidth(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>)</span></code></pre></div></div>
</details>
</div>
<section id="why-this-model" class="level2">
<h2 class="anchored" data-anchor-id="why-this-model">1. Why This Model</h2>
<p>This post fits a 3-class latent class model where each class is a point on a restricted Poincare disk. The geometry gives a direct interpretation of class differences, and an ordering constraint on radii gives identification without post-hoc relabeling.</p>
<p>The goals are:</p>
<ol type="1">
<li>Fit an identified Bayesian LCA with interpretable latent coordinates.</li>
<li>Use a fast collapsed likelihood on sufficient statistics.</li>
<li>Verify that the geometric model recovers clear class separation.</li>
<li>Compare the fitted profiles to a standard <code>poLCA</code> baseline.</li>
</ol>
</section>
<section id="data" class="level2">
<h2 class="anchored" data-anchor-id="data">2. Data</h2>
<p>I use the synthetic drinking-behavior data from the <a href="https://stats.oarc.ucla.edu/sas/dae/latent-class-analysis/">UCLA OARC LCA tutorial</a> <span class="citation" data-cites="ucla_oarc_lca">(UCLA Statistical Consulting Group 2026)</span>. The dataset has <img src="https://latex.codecogs.com/png.latex?N%20=%201%7B,%7D000"> respondents and <img src="https://latex.codecogs.com/png.latex?J%20=%209"> binary items:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;">Item</th>
<th style="text-align: left;">Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">j1</td>
<td style="text-align: left;">I like to drink</td>
</tr>
<tr class="even">
<td style="text-align: left;">j2</td>
<td style="text-align: left;">I drink hard liquor</td>
</tr>
<tr class="odd">
<td style="text-align: left;">j3</td>
<td style="text-align: left;">I have drank in the morning</td>
</tr>
<tr class="even">
<td style="text-align: left;">j4</td>
<td style="text-align: left;">I have drank at work</td>
</tr>
<tr class="odd">
<td style="text-align: left;">j5</td>
<td style="text-align: left;">I drink to get drunk</td>
</tr>
<tr class="even">
<td style="text-align: left;">j6</td>
<td style="text-align: left;">I like the taste of alcohol</td>
</tr>
<tr class="odd">
<td style="text-align: left;">j7</td>
<td style="text-align: left;">I drink to help me sleep</td>
</tr>
<tr class="even">
<td style="text-align: left;">j8</td>
<td style="text-align: left;">Drinking interferes with my relationships</td>
</tr>
<tr class="odd">
<td style="text-align: left;">j9</td>
<td style="text-align: left;">I frequently visit bars</td>
</tr>
</tbody>
</table>
<div id="load-data" class="cell" data-execution_count="2">
<details class="code-fold">
<summary>Show the code</summary>
<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">item_labels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb2-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Like to drink"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hard liquor"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Morning drinking"</span>,</span>
<span id="cb2-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drinking at work"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drink to get drunk"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Like taste"</span>,</span>
<span id="cb2-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drink to sleep"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Interferes w/ relationships"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Visit bars"</span></span>
<span id="cb2-5">]</span>
<span id="cb2-6"></span>
<span id="cb2-7">raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.loadtxt(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/lca1.csv"</span>, delimiter<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;">","</span>, dtype<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;">int</span>)</span>
<span id="cb2-8">Y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> raw[:, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># drop row ID</span></span>
<span id="cb2-9">N, J <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Y.shape</span>
<span id="cb2-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"N = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N<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;">, J = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>J<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>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>N = 1000, J = 9</code></pre>
</div>
</div>
<section id="descriptive-statistics" class="level3">
<h3 class="anchored" data-anchor-id="descriptive-statistics">2.1 Descriptive Statistics</h3>
<div id="cell-descriptives" class="cell" data-execution_count="3">
<details class="code-fold">
<summary>Show the code</summary>
<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">means <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Y.mean(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;">0</span>)</span>
<span id="cb4-2">desc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb4-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Item"</span>: [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"j</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)],</span>
<span id="cb4-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Description"</span>: item_labels,</span>
<span id="cb4-5">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Endorsement Rate"</span>: [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><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;">:.1%}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <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> means]</span>
<span id="cb4-6">})</span>
<span id="cb4-7">desc</span></code></pre></div></div>
</details>
<div id="descriptives" class="cell-output cell-output-display" data-execution_count="3">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Item</th>
<th data-quarto-table-cell-role="th">Description</th>
<th data-quarto-table-cell-role="th">Endorsement Rate</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<th data-quarto-table-cell-role="th">0</th>
<td>j1</td>
<td>Like to drink</td>
<td>69.3%</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">1</th>
<td>j2</td>
<td>Hard liquor</td>
<td>29.1%</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">2</th>
<td>j3</td>
<td>Morning drinking</td>
<td>8.3%</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">3</th>
<td>j4</td>
<td>Drinking at work</td>
<td>9.0%</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">4</th>
<td>j5</td>
<td>Drink to get drunk</td>
<td>19.9%</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">5</th>
<td>j6</td>
<td>Like taste</td>
<td>28.2%</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">6</th>
<td>j7</td>
<td>Drink to sleep</td>
<td>13.9%</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">7</th>
<td>j8</td>
<td>Interferes w/ relationships</td>
<td>16.7%</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">8</th>
<td>j9</td>
<td>Visit bars</td>
<td>27.7%</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>Endorsement rates range from under 10% (morning drinking, drinking at work) to almost 70% (liking to drink). So the item set spans lower-risk and higher-risk behavior.</p>
</section>
<section id="sufficient-statistics" class="level3">
<h3 class="anchored" data-anchor-id="sufficient-statistics">2.2 Sufficient Statistics</h3>
<p>For computational speed, I collapse the <img src="https://latex.codecogs.com/png.latex?N=1%7B,%7D000"> response vectors into <img src="https://latex.codecogs.com/png.latex?P"> unique patterns with counts. This keeps the likelihood exact but avoids repeated evaluations of identical rows:</p>
<div id="suff-stats" class="cell" data-execution_count="4">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">patterns <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;">tuple</span>(row) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> Y]</span>
<span id="cb5-2">counts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Counter(patterns)</span>
<span id="cb5-3">unique <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;">sorted</span>(counts.keys())</span>
<span id="cb5-4">pat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array(unique, dtype<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;">int</span>)</span>
<span id="cb5-5">cnt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([counts[p] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> unique], dtype<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;">int</span>)</span>
<span id="cb5-6">P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pat.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb5-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>N<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;"> observations → </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>P<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;"> unique patterns"</span>)</span>
<span id="cb5-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Most common: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cnt<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;">max</span>()<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;"> occurrences, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>(cnt <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>()<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;"> singletons"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>1000 observations → 180 unique patterns
Most common: 126 occurrences, 87 singletons</code></pre>
</div>
</div>
</section>
</section>
<section id="model" class="level2">
<h2 class="anchored" data-anchor-id="model">3. Model</h2>
<section id="identification-problem" class="level3">
<h3 class="anchored" data-anchor-id="identification-problem">3.1 Identification Problem</h3>
<p>Finite mixtures have label symmetry: with <img src="https://latex.codecogs.com/png.latex?K"> classes there are <img src="https://latex.codecogs.com/png.latex?K!"> equivalent posterior modes <span class="citation" data-cites="stephens2000 jasra2005">(Stephens 2000; Jasra et al. 2005)</span>. Without identifying structure, MCMC can jump across class permutations and make class summaries hard to interpret.</p>
<p>I use a geometric identification strategy: classes are embedded on a restricted Poincare disk <span class="citation" data-cites="nickel2017">(Nickel and Kiela 2017)</span>, and class labels are fixed by ordering radii.</p>
</section>
<section id="geometric-class-coordinates" class="level3">
<h3 class="anchored" data-anchor-id="geometric-class-coordinates">3.2 Geometric Class Coordinates</h3>
<p>Each class is a point on the open unit disk with polar coordinates <img src="https://latex.codecogs.com/png.latex?(r_k,%5Calpha_k)">, where <img src="https://latex.codecogs.com/png.latex?r_k%5Cin(0,1)"> and <img src="https://latex.codecogs.com/png.latex?%5Calpha_k%5Cin(0,%5Cpi/2)">. Item directions are fixed and evenly spaced: <img src="https://latex.codecogs.com/png.latex?%0A%5Cvarphi_j%20=%20%5Cfrac%7B%5Cpi%7D%7B2%7D%20%5Ccdot%20%5Cfrac%7Bj-1%7D%7BJ-1%7D,%20%5Cqquad%20j%20=%201,%20%5Cldots,%20J%0A"> so all items lie in the same quadrant.</p>
</section>
<section id="item-response-probabilities" class="level3">
<h3 class="anchored" data-anchor-id="item-response-probabilities">3.3 Item-Response Probabilities</h3>
<p>The class-<img src="https://latex.codecogs.com/png.latex?k"> endorsement probability for item <img src="https://latex.codecogs.com/png.latex?j"> is <img src="https://latex.codecogs.com/png.latex?%0A%5Ctheta_%7Bkj%7D%20=%20%5Ctext%7Blogit%7D%5E%7B-1%7D%5C!%5CBig(%5Calpha_j%20+%20%5Clambda_j%20%5C,%20r_k%20%5Ccos(%5Calpha_k%20-%20%5Cvarphi_j)%5CBig)%0A"> where:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?%5Calpha_j%20%5Cin%20%5Cmathbb%7BR%7D"> is the item baseline,</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Clambda_j%20%3E%200"> is item discrimination,</li>
<li><img src="https://latex.codecogs.com/png.latex?r_k%20%5Ccos(%5Calpha_k%20-%20%5Cvarphi_j)"> is the projection of class position onto item direction.</li>
</ul>
<p>Because both angles are restricted to <img src="https://latex.codecogs.com/png.latex?%5B0,%5Cpi/2%5D">, we have <img src="https://latex.codecogs.com/png.latex?%5Ccos(%5Calpha_k-%5Cvarphi_j)%5Cge%200">. Increasing radius therefore cannot lower an item below its baseline <img src="https://latex.codecogs.com/png.latex?%5Ctext%7Blogit%7D%5E%7B-1%7D(%5Calpha_j)">, which removes one common source of multimodality.</p>
<p>The two coordinates have a direct interpretation:</p>
<ul>
<li>Radius <img src="https://latex.codecogs.com/png.latex?r_k">: global severity (closer to 1 means higher overall propensity to endorse).</li>
<li>Angle <img src="https://latex.codecogs.com/png.latex?%5Calpha_k">: profile direction (which subset of items gets the strongest lift).</li>
</ul>
</section>
<section id="mixture-likelihood-and-ordering-constraint" class="level3">
<h3 class="anchored" data-anchor-id="mixture-likelihood-and-ordering-constraint">3.4 Mixture Likelihood and Ordering Constraint</h3>
<p>The collapsed-pattern likelihood is: <img src="https://latex.codecogs.com/png.latex?%0Ap(%5Cmathbf%7By%7D_p)%20=%20%5Csum_%7Bk=1%7D%5EK%20%5Cpi_k%20%5Cprod_%7Bj=1%7D%5EJ%20%5Ctheta_%7Bkj%7D%5E%7By_%7Bpj%7D%7D%20(1%20-%20%5Ctheta_%7Bkj%7D)%5E%7B1%20-%20y_%7Bpj%7D%7D%0A"></p>
<p>I break label symmetry with ordered radii: <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Blogit%7D(r_1)%20%3C%20%5Ctext%7Blogit%7D(r_2)%20%3C%20%5Ccdots%20%3C%20%5Ctext%7Blogit%7D(r_K)%0A"> implemented using Stan’s <code>ordered[K]</code> type <span class="citation" data-cites="carpenter2017">(Carpenter et al. 2017)</span>. Class 1 is always most central and class <img src="https://latex.codecogs.com/png.latex?K"> is always most extreme.</p>
</section>
<section id="blas-accelerated-likelihood-evaluation" class="level3">
<h3 class="anchored" data-anchor-id="blas-accelerated-likelihood-evaluation">3.5 BLAS-Accelerated Likelihood Evaluation</h3>
<p>For each class, the Bernoulli log-probability for pattern <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7By%7D_p"> can be written as: <img src="https://latex.codecogs.com/png.latex?%0A%5Clog%20p(%5Cmathbf%7By%7D_p%20%5Cmid%20k)%20=%20%5Cmathbf%7By%7D_p%5E%5Ctop%20%5Cboldsymbol%7B%5Ceta%7D_k%20-%20%5Csum_%7Bj=1%7D%5EJ%20%5Clog(1%20+%20e%5E%7B%5Ceta_%7Bkj%7D%7D)%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Ceta_%7Bkj%7D%20=%20%5Calpha_j%20+%20%5Clambda_j%20r_k%20%5Ccos(%5Calpha_k%20-%20%5Cvarphi_j)">. This gives a fast three-step computation:</p>
<ol type="1">
<li><p><strong>Precompute class offsets</strong> (once per leapfrog step): <img src="https://latex.codecogs.com/png.latex?%0Ac_k%20=%20-%5Csum_%7Bj=1%7D%5EJ%20%5Clog(1%20+%20e%5E%7B%5Ceta_%7Bkj%7D%7D)%0A"></p></li>
<li><p><strong>Single BLAS matrix multiply</strong> (<img src="https://latex.codecogs.com/png.latex?O(PJK)">): <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7BS%7D%20=%20%5Cmathbf%7BY%7D%5C,%5Cboldsymbol%7B%5Ceta%7D%5E%5Ctop%20%5Cqquad%20(P%20%5Ctimes%20K)%0A"></p></li>
<li><p><strong>Pattern-wise log-sum-exp</strong>: <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Btarget%7D%20%5Cmathrel%7B+%7D=%20%5Csum_%7Bp=1%7D%5EP%20n_p%20%5Clog%5C!%5Csum_%7Bk=1%7D%5EK%20%5Cexp%5C!%5Cbig(%5Clog%20%5Cpi_k%20+%20S_%7Bpk%7D%20+%20c_k%5Cbig)%0A"></p></li>
</ol>
<p>The key speed gain comes from replacing the inner item loop with one <code>dgemm</code> call for <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BY%7D%5Ceta%5E%5Ctop">.</p>
</section>
<section id="priors" class="level3">
<h3 class="anchored" data-anchor-id="priors">3.6 Priors</h3>
<table class="caption-top table">
<colgroup>
<col style="width: 41%">
<col style="width: 29%">
<col style="width: 29%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Parameter</th>
<th style="text-align: left;">Prior</th>
<th style="text-align: left;">Notes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Ctext%7Blogit%7D(r_k)"></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BN%7D(0,%202)"></td>
<td style="text-align: left;">Ordered in Stan for identification</td>
</tr>
<tr class="even">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Calpha_k%5E%7B%5Ctext%7Braw%7D%7D"></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BN%7D(0,%202)"></td>
<td style="text-align: left;">Mapped to <img src="https://latex.codecogs.com/png.latex?(0,%20%5Cpi/2)"> via <img src="https://latex.codecogs.com/png.latex?(%5Cpi/2)%5C,%5Csigma(%5Ccdot)"></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Calpha_j"></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BN%7D(0,%203)"></td>
<td style="text-align: left;">Weakly informative item baselines</td>
</tr>
<tr class="even">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Clambda_j"></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BN%7D%5E+(0,%203)"></td>
<td style="text-align: left;">Half-normal via <code>&lt;lower=0&gt;</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Cpi%7D"></td>
<td style="text-align: left;"><img src="https://latex.codecogs.com/png.latex?%5Ctext%7BDir%7D(2,%20%5Cldots,%202)"></td>
<td style="text-align: left;">Mild regularization toward balanced class weights</td>
</tr>
</tbody>
</table>
</section>
</section>
<section id="inference" class="level2">
<h2 class="anchored" data-anchor-id="inference">4. Inference</h2>
<section id="compile-and-optimize" class="level3">
<h3 class="anchored" data-anchor-id="compile-and-optimize">4.1 Compile and Optimize</h3>
<p>I use a two-stage workflow: first multi-start optimization to locate a good mode, then NUTS initialized near that mode.</p>
<div id="compile" class="cell" data-execution_count="5">
<details class="code-fold">
<summary>Show the code</summary>
<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">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>
<span id="cb7-2">model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CmdStanModel(stan_file<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;">"lca_poincare.stan"</span>)</span></code></pre></div></div>
</details>
</div>
<div id="helpers" class="cell" data-execution_count="6">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> random_init(K, J, rng):</span>
<span id="cb8-2">    logit_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sort(rng.randn(K) <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.8</span>)</span>
<span id="cb8-3">    angle_raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rng.randn(K) <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.0</span></span>
<span id="cb8-4">    alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rng.randn(J) <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="cb8-5">    lam <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;">abs</span>(rng.randn(J)) <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="cb8-6">    pi_w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rng.dirichlet(np.ones(K) <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;">2.0</span>)</span>
<span id="cb8-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(logit_r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>logit_r.tolist(), angle_raw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>angle_raw.tolist(),</span>
<span id="cb8-8">                alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha.tolist(), lam<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lam.tolist(), pi_w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pi_w.tolist())</span>
<span id="cb8-9"></span>
<span id="cb8-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> find_best_optimum(model, stan_data, n_starts<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;">20</span>, seed<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;">42</span>):</span>
<span id="cb8-11">    best_opt, best_lp <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;">None</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.inf</span>
<span id="cb8-12">    rng <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.RandomState(seed)</span>
<span id="cb8-13">    K, J <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stan_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'K'</span>], stan_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>]</span>
<span id="cb8-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <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>(n_starts):</span>
<span id="cb8-15">        init <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> random_init(K, J, rng)</span>
<span id="cb8-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb8-17">            opt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model.optimize(data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>stan_data, inits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>init, jacobian<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;">True</span>,</span>
<span id="cb8-18">                                 seed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>seed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> i, show_console<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;">False</span>)</span>
<span id="cb8-19">            lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> opt.optimized_params_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lp__'</span>]</span>
<span id="cb8-20">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> best_lp:</span>
<span id="cb8-21">                best_lp, best_opt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lp, opt</span>
<span id="cb8-22">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>:</span>
<span id="cb8-23">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb8-24">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> best_opt</span>
<span id="cb8-25"></span>
<span id="cb8-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> opt_to_nuts_init(opt, K, J, jitter<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.02</span>, rng<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;">None</span>):</span>
<span id="cb8-27">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> rng <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb8-28">        rng <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.RandomState()</span>
<span id="cb8-29">    d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> opt.optimized_params_dict</span>
<span id="cb8-30">    logit_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sort(np.array([d[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'logit_r[</span><span class="sc" 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;">1</span><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>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K)]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn(K) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter)</span>
<span id="cb8-31">    angle_raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([d[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'angle_raw[</span><span class="sc" 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;">1</span><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>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K)]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn(K) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter</span>
<span id="cb8-32">    alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([d[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'alpha[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]'</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn(J) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter</span>
<span id="cb8-33">    lam <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(np.array([d[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'lam[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]'</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn(J) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb8-34">    pi_w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(np.array([d[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'pi_w[</span><span class="sc" 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;">1</span><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>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K)]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn(K) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter <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.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb8-35">    pi_w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/=</span> pi_w.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb8-36">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(logit_r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>logit_r.tolist(), angle_raw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>angle_raw.tolist(),</span>
<span id="cb8-37">                alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha.tolist(), lam<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lam.tolist(), pi_w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pi_w.tolist())</span></code></pre></div></div>
</details>
</div>
<div id="optimize" class="cell" data-execution_count="7">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">stan_data <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;">'P'</span>: P, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'K'</span>: K, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>: J, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pattern'</span>: pat.tolist(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'count'</span>: cnt.tolist()}</span>
<span id="cb9-2"></span>
<span id="cb9-3">best_opt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> find_best_optimum(model, stan_data, n_starts<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;">20</span>, seed<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;">42</span>)</span>
<span id="cb9-4">d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> best_opt.optimized_params_dict</span>
<span id="cb9-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Best lp__: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lp__'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb9-6"></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># MAP estimates</span></span>
<span id="cb9-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">MAP θ[k,j]:"</span>)</span>
<span id="cb9-9">header <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;">"         "</span> <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;">""</span>.join(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'j'</span><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;">str</span>(j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;6}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J))</span>
<span id="cb9-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(header)</span>
<span id="cb9-11"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K):</span>
<span id="cb9-12">    theta_row <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [sigmoid(d[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'logit_theta[</span><span class="sc" 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;">1</span><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><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]'</span>]) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)]</span>
<span id="cb9-13">    pi_k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> d[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'pi_w[</span><span class="sc" 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;">1</span><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>]</span>
<span id="cb9-14">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"    C</span><span class="sc" 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;">1</span><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> <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;">""</span>.join(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>t<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;6.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> t <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> theta_row) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  π=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_k<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [1] done processing</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Best lp__: -4246.63

MAP θ[k,j]:
             j1    j2    j3    j4    j5    j6    j7    j8    j9
    C1:   0.49  0.17  0.02  0.04  0.07  0.22  0.08  0.10  0.26  π=0.483
    C2:   0.87  0.37  0.10  0.10  0.24  0.31  0.14  0.16  0.28  π=0.441
    C3:   0.96  0.58  0.37  0.36  0.81  0.54  0.49  0.61  0.43  π=0.076</code></pre>
</div>
</div>
</section>
<section id="nuts-sampling" class="level3">
<h3 class="anchored" data-anchor-id="nuts-sampling">4.2 NUTS Sampling</h3>
<div id="sample" class="cell" data-execution_count="8">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1">chains <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;">4</span></span>
<span id="cb12-2">inits <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [opt_to_nuts_init(best_opt, K, J, jitter<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.02</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.01</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>c,</span>
<span id="cb12-3">                           rng<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>np.random.RandomState(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c <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;">100</span>))</span>
<span id="cb12-4">         <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> c <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>(chains)]</span>
<span id="cb12-5"></span>
<span id="cb12-6">fit <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model.sample(</span>
<span id="cb12-7">    data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>stan_data, chains<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>chains,</span>
<span id="cb12-8">    iter_warmup<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;">2000</span>, iter_sampling<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;">2000</span>,</span>
<span id="cb12-9">    seed<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;">42</span>, inits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>inits,</span>
<span id="cb12-10">    adapt_delta<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.95</span>, max_treedepth<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;">10</span>,</span>
<span id="cb12-11">    show_progress<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;">False</span>,</span>
<span id="cb12-12">)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>10:19:33 - cmdstanpy - INFO - CmdStan start processing
10:19:33 - cmdstanpy - INFO - Chain [1] start processing
10:19:33 - cmdstanpy - INFO - Chain [2] start processing
10:19:33 - cmdstanpy - INFO - Chain [3] start processing
10:19:33 - cmdstanpy - INFO - Chain [4] start processing
10:19:52 - cmdstanpy - INFO - Chain [1] done processing
10:19:53 - cmdstanpy - INFO - Chain [4] done processing
10:19:53 - cmdstanpy - INFO - Chain [2] done processing
10:19:54 - cmdstanpy - INFO - Chain [3] done processing</code></pre>
</div>
</div>
</section>
<section id="diagnostics" class="level3">
<h3 class="anchored" data-anchor-id="diagnostics">4.3 Diagnostics</h3>
<div id="diagnostics" class="cell" data-execution_count="9">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1">diag <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit.diagnose()</span>
<span id="cb14-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> line <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> diag.split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>):</span>
<span id="cb14-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> line.strip():</span>
<span id="cb14-4">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(line)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Checking sampler transitions treedepth.
Treedepth satisfactory for all transitions.
Checking sampler transitions for divergences.
No divergent transitions found.
Checking E-BFMI - sampler transitions HMC potential energy.
E-BFMI satisfactory.
Rank-normalized split effective sample size satisfactory for all parameters.
Rank-normalized split R-hat values satisfactory for all parameters.
Processing complete, no problems detected.</code></pre>
</div>
</div>
<div id="cell-convergence" class="cell" data-execution_count="10">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1">summary <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit.summary()</span>
<span id="cb16-2">targets <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ([<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'logit_r[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>k<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> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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;">1</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;">1</span>)]</span>
<span id="cb16-3">         <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'angle_raw[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>k<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> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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;">1</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;">1</span>)]</span>
<span id="cb16-4">         <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'alpha[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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;">1</span>, J<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="cb16-5">         <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'lam[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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;">1</span>, J<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="cb16-6">         <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'pi_w[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>k<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> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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;">1</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;">1</span>)])</span>
<span id="cb16-7"></span>
<span id="cb16-8">conv_rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb16-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> targets:</span>
<span id="cb16-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> summary.index:</span>
<span id="cb16-11">        row <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> summary.loc[p]</span>
<span id="cb16-12">        rhat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> row.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'R_hat'</span>, row.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Rhat'</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'nan'</span>)))</span>
<span id="cb16-13">        ess <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> row.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'N_Eff'</span>, row.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ESS_bulk'</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'nan'</span>)))</span>
<span id="cb16-14">        conv_rows.append({<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Parameter"</span>: p, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R-hat"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>rhat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ESS"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ess<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.0f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>})</span>
<span id="cb16-15"></span>
<span id="cb16-16">pd.DataFrame(conv_rows)</span></code></pre></div></div>
</details>
<div id="convergence" class="cell-output cell-output-display" data-execution_count="10">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Parameter</th>
<th data-quarto-table-cell-role="th">R-hat</th>
<th data-quarto-table-cell-role="th">ESS</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<th data-quarto-table-cell-role="th">0</th>
<td>logit_r[1]</td>
<td>1.001</td>
<td>2795</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">1</th>
<td>logit_r[2]</td>
<td>1.001</td>
<td>3790</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">2</th>
<td>logit_r[3]</td>
<td>1.000</td>
<td>6969</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">3</th>
<td>angle_raw[1]</td>
<td>1.000</td>
<td>6867</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">4</th>
<td>angle_raw[2]</td>
<td>1.000</td>
<td>3700</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">5</th>
<td>angle_raw[3]</td>
<td>1.001</td>
<td>2964</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">6</th>
<td>alpha[1]</td>
<td>1.002</td>
<td>2174</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">7</th>
<td>alpha[2]</td>
<td>1.002</td>
<td>2578</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">8</th>
<td>alpha[3]</td>
<td>1.001</td>
<td>2992</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">9</th>
<td>alpha[4]</td>
<td>1.002</td>
<td>2811</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">10</th>
<td>alpha[5]</td>
<td>1.002</td>
<td>2372</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">11</th>
<td>alpha[6]</td>
<td>1.003</td>
<td>2699</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">12</th>
<td>alpha[7]</td>
<td>1.001</td>
<td>2695</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">13</th>
<td>alpha[8]</td>
<td>1.001</td>
<td>2777</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">14</th>
<td>alpha[9]</td>
<td>1.000</td>
<td>3950</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">15</th>
<td>lam[1]</td>
<td>1.000</td>
<td>4966</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">16</th>
<td>lam[2]</td>
<td>1.000</td>
<td>4260</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">17</th>
<td>lam[3]</td>
<td>1.001</td>
<td>3942</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">18</th>
<td>lam[4]</td>
<td>1.001</td>
<td>4262</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">19</th>
<td>lam[5]</td>
<td>1.000</td>
<td>4213</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">20</th>
<td>lam[6]</td>
<td>1.001</td>
<td>4376</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">21</th>
<td>lam[7]</td>
<td>1.001</td>
<td>4270</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">22</th>
<td>lam[8]</td>
<td>1.000</td>
<td>4400</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">23</th>
<td>lam[9]</td>
<td>1.001</td>
<td>2862</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">24</th>
<td>pi_w[1]</td>
<td>1.004</td>
<td>2529</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">25</th>
<td>pi_w[2]</td>
<td>1.003</td>
<td>2457</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">26</th>
<td>pi_w[3]</td>
<td>1.000</td>
<td>4338</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>In this run (seed = 42), diagnostics are clean: no divergences, no treedepth saturations, and rank-normalized <img src="https://latex.codecogs.com/png.latex?%5Chat%7BR%7D"> values near 1 (maximum about 1.004 on class weights). Bulk ESS values are in the thousands.</p>
</section>
</section>
<section id="posterior-results" class="level2">
<h2 class="anchored" data-anchor-id="posterior-results">5. Posterior Results</h2>
<section id="posterior-item-probabilities" class="level3">
<h3 class="anchored" data-anchor-id="posterior-item-probabilities">5.1 Posterior Item Probabilities</h3>
<div id="cell-theta-table" class="cell" data-execution_count="11">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1">theta_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sigmoid(fit.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'logit_theta'</span>))</span>
<span id="cb17-2">theta_mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta_post.mean(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;">0</span>)</span>
<span id="cb17-3">pi_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pi_w'</span>)</span>
<span id="cb17-4">pi_mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pi_post.mean(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;">0</span>)</span>
<span id="cb17-5"></span>
<span id="cb17-6">rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb17-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K):</span>
<span id="cb17-8">    row <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;">"Class"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"C</span><span class="sc" 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;">1</span><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><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_mean[k]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span>}</span>
<span id="cb17-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J):</span>
<span id="cb17-10">        q05, q95 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_post[:, k, j], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>])</span>
<span id="cb17-11">        row[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"j</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>theta_mean[k,j]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb17-12">    rows.append(row)</span>
<span id="cb17-13"></span>
<span id="cb17-14">pd.DataFrame(rows).set_index(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Class"</span>)</span></code></pre></div></div>
</details>
<div id="theta-table" class="cell-output cell-output-display" data-execution_count="11">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">j1</th>
<th data-quarto-table-cell-role="th">j2</th>
<th data-quarto-table-cell-role="th">j3</th>
<th data-quarto-table-cell-role="th">j4</th>
<th data-quarto-table-cell-role="th">j5</th>
<th data-quarto-table-cell-role="th">j6</th>
<th data-quarto-table-cell-role="th">j7</th>
<th data-quarto-table-cell-role="th">j8</th>
<th data-quarto-table-cell-role="th">j9</th>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">Class</th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<th data-quarto-table-cell-role="th">C1 (π=0.51)</th>
<td>0.501</td>
<td>0.184</td>
<td>0.028</td>
<td>0.040</td>
<td>0.072</td>
<td>0.227</td>
<td>0.085</td>
<td>0.108</td>
<td>0.261</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">C2 (π=0.41)</th>
<td>0.865</td>
<td>0.372</td>
<td>0.105</td>
<td>0.107</td>
<td>0.250</td>
<td>0.305</td>
<td>0.143</td>
<td>0.162</td>
<td>0.273</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">C3 (π=0.09)</th>
<td>0.949</td>
<td>0.565</td>
<td>0.341</td>
<td>0.332</td>
<td>0.775</td>
<td>0.509</td>
<td>0.467</td>
<td>0.578</td>
<td>0.393</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
</section>
<section id="geometric-parameters" class="level3">
<h3 class="anchored" data-anchor-id="geometric-parameters">5.2 Geometric Parameters</h3>
<div id="cell-geometry" class="cell" data-execution_count="12">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1">radii_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'radii'</span>)</span>
<span id="cb18-2">angles_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'angles'</span>)</span>
<span id="cb18-3">angles_deg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> angles_post <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;">180</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.pi</span>
<span id="cb18-4"></span>
<span id="cb18-5">geo_rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb18-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K):</span>
<span id="cb18-7">    r_q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(radii_post[:, k], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">95</span>])</span>
<span id="cb18-8">    a_q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(angles_deg[:, k], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">95</span>])</span>
<span id="cb18-9">    geo_rows.append({</span>
<span id="cb18-10">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Class"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"C</span><span class="sc" 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;">1</span><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>,</span>
<span id="cb18-11">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"π"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_mean[k]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb18-12">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Radius"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>r_q[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>r_q[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>r_q[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]"</span>,</span>
<span id="cb18-13">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Angle (°)"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>a_q[<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;">:.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>a_q[<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;">:.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>a_q[<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;">:.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]"</span>,</span>
<span id="cb18-14">    })</span>
<span id="cb18-15"></span>
<span id="cb18-16">pd.DataFrame(geo_rows).set_index(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Class"</span>)</span></code></pre></div></div>
</details>
<div id="geometry" class="cell-output cell-output-display" data-execution_count="12">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">π</th>
<th data-quarto-table-cell-role="th">Radius</th>
<th data-quarto-table-cell-role="th">Angle (°)</th>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">Class</th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<th data-quarto-table-cell-role="th">C1</th>
<td>0.505</td>
<td>0.050 [0.006, 0.176]</td>
<td>50.2 [3.3, 87.6]</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">C2</th>
<td>0.407</td>
<td>0.432 [0.293, 0.594]</td>
<td>10.8 [1.2, 35.5]</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">C3</th>
<td>0.088</td>
<td>0.927 [0.745, 0.991]</td>
<td>43.5 [21.7, 63.8]</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p>The fitted classes are radially ordered with clear gaps: median radii are about <img src="https://latex.codecogs.com/png.latex?r_1=0.05">, <img src="https://latex.codecogs.com/png.latex?r_2=0.43">, and <img src="https://latex.codecogs.com/png.latex?r_3=0.93"> (see the 90% intervals in the table). Class 3 is near the boundary, while Classes 1 and 2 are well separated low/moderate groups.</p>
</section>
<section id="class-locations-on-the-disk" class="level3">
<h3 class="anchored" data-anchor-id="class-locations-on-the-disk">5.3 Class Locations on the Disk</h3>
<div id="cell-fig-disk" class="cell" data-execution_count="13">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<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;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>))</span>
<span id="cb19-2">style_axes(ax, grid<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;">False</span>)</span>
<span id="cb19-3"></span>
<span id="cb19-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unit circle arc</span></span>
<span id="cb19-5">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.linspace(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, np.pi<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb19-6">ax.plot(np.cos(t), np.sin(t), color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>], lw<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.5</span>)</span>
<span id="cb19-7">ax.plot([<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>], lw<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="cb19-8">ax.plot([<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;">0</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>], color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>], lw<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="cb19-9"></span>
<span id="cb19-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Item directions</span></span>
<span id="cb19-11">phi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([(np.pi<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> j <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (J<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="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)])</span>
<span id="cb19-12"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J):</span>
<span id="cb19-13">    ax.plot([<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.95</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np.cos(phi[j])], [<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.95</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np.sin(phi[j])],</span>
<span id="cb19-14">            color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>], lw<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>, ls<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;">'--'</span>, alpha<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="cb19-15">    ax.text(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.07</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np.cos(phi[j]), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.07</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>np.sin(phi[j]), <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"j</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb19-16">            ha<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;">"center"</span>, va<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;">"center"</span>, fontsize<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;">8</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>])</span>
<span id="cb19-17"></span>
<span id="cb19-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Posterior samples</span></span>
<span id="cb19-19">n_draw <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;">min</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, radii_post.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>])</span>
<span id="cb19-20">idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.choice(radii_post.shape[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>], n_draw, replace<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;">False</span>)</span>
<span id="cb19-21"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K):</span>
<span id="cb19-22">    x_pts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> radii_post[idx, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.cos(angles_post[idx, k])</span>
<span id="cb19-23">    y_pts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> radii_post[idx, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sin(angles_post[idx, k])</span>
<span id="cb19-24">    ax.scatter(x_pts, y_pts, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>colors[k], alpha<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.15</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;">8</span>)</span>
<span id="cb19-25">    r_m, a_m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> radii_post[:, k].mean(), angles_post[:, k].mean()</span>
<span id="cb19-26">    ax.scatter(r_m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.cos(a_m), r_m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> np.sin(a_m),</span>
<span id="cb19-27">               color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>colors[k], 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;">100</span>, marker<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;">"*"</span>, edgecolors<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>],</span>
<span id="cb19-28">               linewidths<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>, zorder<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;">5</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"C</span><span class="sc" 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;">1</span><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><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_mean[k]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span>)</span>
<span id="cb19-29"></span>
<span id="cb19-30">ax.set_xlim(<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.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>)</span>
<span id="cb19-31">ax.set_ylim(<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.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>)</span>
<span id="cb19-32">ax.set_aspect(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"equal"</span>)</span>
<span id="cb19-33">ax.legend(fontsize<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>, loc<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;">"upper right"</span>)</span>
<span id="cb19-34">ax.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x = r cos(α)"</span>)</span>
<span id="cb19-35">ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y = r sin(α)"</span>)</span>
<span id="cb19-36">plt.tight_layout()</span>
<span id="cb19-37">plt.show()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display">
<div id="fig-disk" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-disk-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-4-poincare-lca/poincare-lca_files/figure-html/fig-disk-output-1.png" width="569" height="555" class="figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-disk-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Posterior draws of latent class positions on the restricted Poincaré disk. Stars mark posterior means. Dashed lines show fixed item directions.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="item-probability-profiles" class="level3">
<h3 class="anchored" data-anchor-id="item-probability-profiles">5.4 Item Probability Profiles</h3>
<div id="cell-fig-profiles" class="cell" data-execution_count="14">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb20-1">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, K, figsize<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;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>K, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), sharey<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;">True</span>)</span>
<span id="cb20-2">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(J)</span>
<span id="cb20-3"></span>
<span id="cb20-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K):</span>
<span id="cb20-5">    ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> axes[k]</span>
<span id="cb20-6">    style_axes(ax)</span>
<span id="cb20-7">    means <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta_post[:, k, :].mean(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;">0</span>)</span>
<span id="cb20-8">    q05 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_post[:, k, :], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">0</span>)</span>
<span id="cb20-9">    q95 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_post[:, k, :], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</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;">0</span>)</span>
<span id="cb20-10">    ax.bar(x, means, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>colors[k], alpha<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="cb20-11">    ax.errorbar(x, means, yerr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[means <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> q05, q95 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> means],</span>
<span id="cb20-12">                fmt<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;">'none'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>colors[k], capsize<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>, lw<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.5</span>)</span>
<span id="cb20-13">    ax.set_xticks(x)</span>
<span id="cb20-14">    ax.set_xticklabels([<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"j</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)], fontsize<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;">8</span>, rotation<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;">45</span>)</span>
<span id="cb20-15">    ax.set_ylim(<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>
<span id="cb20-16">    ax.set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Class </span><span class="sc" 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;">1</span><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><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_mean[k]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span>, fontweight<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;">"bold"</span>)</span>
<span id="cb20-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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;">0</span>:</span>
<span id="cb20-18">        ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P(endorse)"</span>)</span>
<span id="cb20-19"></span>
<span id="cb20-20">plt.tight_layout()</span>
<span id="cb20-21">plt.show()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display">
<div id="fig-profiles" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-profiles-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-4-poincare-lca/poincare-lca_files/figure-html/fig-profiles-output-1.png" width="1430" height="374" class="figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-profiles-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Posterior mean item probabilities by class with 90% credible intervals.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="item-parameters" class="level3">
<h3 class="anchored" data-anchor-id="item-parameters">5.5 Item Parameters</h3>
<div id="cell-fig-item-params" class="cell" data-execution_count="15">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1">alpha_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'alpha'</span>)</span>
<span id="cb21-2">lam_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lam'</span>)</span>
<span id="cb21-3"></span>
<span id="cb21-4">fig, (ax1, ax2) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<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>, figsize<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;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb21-5">style_axes(ax1)</span>
<span id="cb21-6">style_axes(ax2)</span>
<span id="cb21-7"></span>
<span id="cb21-8">bp1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ax1.boxplot([alpha_post[:, j] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)], patch_artist<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;">True</span>, widths<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="cb21-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> patch <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> bp1[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'boxes'</span>]:</span>
<span id="cb21-10">    patch.set_facecolor(blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>])</span>
<span id="cb21-11">    patch.set_alpha(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>)</span>
<span id="cb21-12">ax1.set_xticklabels([<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"j</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)], fontsize<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;">8</span>)</span>
<span id="cb21-13">ax1.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"α (baseline)"</span>)</span>
<span id="cb21-14">ax1.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Item Baselines"</span>, fontweight<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;">"bold"</span>)</span>
<span id="cb21-15">ax1.axhline(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>], ls<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;">"--"</span>, lw<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="cb21-16"></span>
<span id="cb21-17">bp2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ax2.boxplot([lam_post[:, j] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)], patch_artist<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;">True</span>, widths<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="cb21-18"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> patch <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> bp2[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'boxes'</span>]:</span>
<span id="cb21-19">    patch.set_facecolor(blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>])</span>
<span id="cb21-20">    patch.set_alpha(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>)</span>
<span id="cb21-21">ax2.set_xticklabels([<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"j</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)], fontsize<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;">8</span>)</span>
<span id="cb21-22">ax2.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"λ (discrimination)"</span>)</span>
<span id="cb21-23">ax2.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Item Discriminations"</span>, fontweight<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;">"bold"</span>)</span>
<span id="cb21-24"></span>
<span id="cb21-25">plt.tight_layout()</span>
<span id="cb21-26">plt.show()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display">
<div id="fig-item-params" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-item-params-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-4-poincare-lca/poincare-lca_files/figure-html/fig-item-params-output-1.png" width="950" height="374" class="figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-item-params-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Posterior distributions of item baselines (α) and discriminations (λ).
</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="separation-diagnostics-in-hyperbolic-space" class="level2">
<h2 class="anchored" data-anchor-id="separation-diagnostics-in-hyperbolic-space">6. Separation Diagnostics in Hyperbolic Space</h2>
<p>The hyperbolic distance between two classes <img src="https://latex.codecogs.com/png.latex?a"> and <img src="https://latex.codecogs.com/png.latex?b"> on the Poincaré disk is <span class="citation" data-cites="nickel2017">(Nickel and Kiela 2017)</span>: <img src="https://latex.codecogs.com/png.latex?%0Ad_%7B%5Cmathbb%7BH%7D%7D(z_a,%20z_b)%20=%202%5C,%5Coperatorname%7Barctanh%7D%5C!%5Cleft(%5Cfrac%7B%7Cz_a%20-%20z_b%7C%7D%7B%7C1%20-%20%5Cbar%7Bz%7D_a%20z_b%7C%7D%5Cright)%0A"> where <img src="https://latex.codecogs.com/png.latex?z_k%20=%20r_k%20e%5E%7Bi%5Calpha_k%7D"> is the complex representation of the class position.</p>
<section id="why-this-metric-is-useful" class="level3">
<h3 class="anchored" data-anchor-id="why-this-metric-is-useful">6.1 Why This Metric Is Useful</h3>
<p>Unlike Euclidean distance, hyperbolic distance grows quickly near the boundary (<img src="https://latex.codecogs.com/png.latex?r%20%5Cto%201">). That gives three practical diagnostics:</p>
<ol type="1">
<li><p><strong>Boundary classes can be strongly distinct.</strong> Large <img src="https://latex.codecogs.com/png.latex?d_%7B%5Cmathbb%7BH%7D%7D"> values near the rim indicate genuinely different high-severity profiles.</p></li>
<li><p><strong>Central classes are naturally close to all others.</strong> A class near <img src="https://latex.codecogs.com/png.latex?r%5Capprox0"> tends to have small distance to every class because its profile is close to baseline.</p></li>
<li><p><strong>The minimum pairwise distance is an adequacy check.</strong> Small <img src="https://latex.codecogs.com/png.latex?%5Cmin%20d_%7B%5Cmathbb%7BH%7D%7D"> suggests redundant classes; larger values support distinct classes for the chosen <img src="https://latex.codecogs.com/png.latex?K">.</p></li>
</ol>
</section>
<section id="hyperbolic-vs-profile-distance" class="level3">
<h3 class="anchored" data-anchor-id="hyperbolic-vs-profile-distance">6.2 Hyperbolic vs Profile Distance</h3>
<p>Profile distance (<img src="https://latex.codecogs.com/png.latex?%5C%7C%5Cboldsymbol%7B%5Ctheta%7D_a%20-%20%5Cboldsymbol%7B%5Ctheta%7D_b%5C%7C_2">) measures separation in observable item-probability space. Hyperbolic distance measures structural separation in latent space.</p>
<p>The two can disagree. For example, weak discrimination can make geometrically distinct classes look similar in profile space. Using both gives a better separation diagnosis than either metric alone.</p>
<div id="hyp-dist" class="cell" data-execution_count="16">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb22-1">hyp_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hyp_dist'</span>)</span>
<span id="cb22-2">mpd_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'min_profile_dist'</span>)</span>
<span id="cb22-3">min_hyp_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'min_hyp_dist'</span>)</span>
<span id="cb22-4"></span>
<span id="cb22-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pairwise Hyperbolic Distances (posterior mean):"</span>)</span>
<span id="cb22-6">hyp_mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hyp_post.mean(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;">0</span>)</span>
<span id="cb22-7">labels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"C</span><span class="sc" 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;">1</span><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> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K)]</span>
<span id="cb22-8">hyp_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(hyp_mean, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>labels, columns<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>labels).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb22-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(hyp_df.to_string())</span>
<span id="cb22-10"></span>
<span id="cb22-11">q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(min_hyp_post, [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">95</span>])</span>
<span id="cb22-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">Min hyperbolic distance: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  90% CI = [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]"</span>)</span>
<span id="cb22-13"></span>
<span id="cb22-14">q2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(mpd_post, [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">95</span>])</span>
<span id="cb22-15"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Min profile distance:   </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q2[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  90% CI = [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q2[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q2[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Pairwise Hyperbolic Distances (posterior mean):
       C1     C2     C3
C1  0.000  0.868  3.305
C2  0.868  0.000  2.842
C3  3.305  2.842  0.000

Min hyperbolic distance: 0.846  90% CI = [0.537, 1.253]
Min profile distance:   0.491  90% CI = [0.326, 0.605]</code></pre>
</div>
</div>
<p>In this run, separation is strong: the minimum pairwise hyperbolic distance has posterior median 0.85 (90% interval [0.54, 1.25]), and the minimum profile distance has median 0.49 (90% interval [0.33, 0.61]).</p>
<div id="cell-fig-hyp-dist" class="cell" data-execution_count="17">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb24-1">pairs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(a, b) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> a <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>(K) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> b <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>(a<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>, K)]</span>
<span id="cb24-2"></span>
<span id="cb24-3">fig, (ax1, ax2) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<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>, figsize<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;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))</span>
<span id="cb24-4">style_axes(ax1)</span>
<span id="cb24-5">style_axes(ax2)</span>
<span id="cb24-6"></span>
<span id="cb24-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> a, b <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> pairs:</span>
<span id="cb24-8">    ax1.hist(hyp_post[:, a, b], bins<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;">50</span>, alpha<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>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"C</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>a<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">–C</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>b<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, density<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;">True</span>)</span>
<span id="cb24-9">ax1.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hyperbolic distance"</span>)</span>
<span id="cb24-10">ax1.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>)</span>
<span id="cb24-11">ax1.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pairwise Hyperbolic Distances"</span>, fontweight<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;">"bold"</span>)</span>
<span id="cb24-12">ax1.legend()</span>
<span id="cb24-13"></span>
<span id="cb24-14">ax2.hist(mpd_post, bins<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;">50</span>, alpha<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>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"teal"</span>], density<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;">True</span>)</span>
<span id="cb24-15">ax2.set_xlabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Profile distance (L2)"</span>)</span>
<span id="cb24-16">ax2.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>)</span>
<span id="cb24-17">ax2.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Min Profile Distance"</span>, fontweight<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;">"bold"</span>)</span>
<span id="cb24-18"></span>
<span id="cb24-19">plt.tight_layout()</span>
<span id="cb24-20">plt.show()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display">
<div id="fig-hyp-dist" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-hyp-dist-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-4-poincare-lca/poincare-lca_files/figure-html/fig-hyp-dist-output-1.png" width="950" height="374" class="figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-hyp-dist-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Posterior distributions of pairwise hyperbolic distances and minimum profile distance.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="frequentist-cross-check-with-polca" class="level2">
<h2 class="anchored" data-anchor-id="frequentist-cross-check-with-polca">7. Frequentist Cross-Check with poLCA</h2>
<p>As a baseline, I fit the same <img src="https://latex.codecogs.com/png.latex?K=3"> model with <a href="https://cran.r-project.org/package=poLCA"><code>poLCA</code></a> in R using many random starts <span class="citation" data-cites="linzer2011">(Linzer and Lewis 2011)</span>.</p>
<div id="freq-comparison" class="cell" data-execution_count="18">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> shutil.which(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rscript"</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb25-2">    <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;">RuntimeError</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rscript is required for poLCA comparison but was not found on PATH."</span>)</span>
<span id="cb25-3"></span>
<span id="cb25-4">r_code <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> textwrap.dedent(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"""</span></span>
<span id="cb25-5"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">suppressPackageStartupMessages(library(poLCA))</span></span>
<span id="cb25-6"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">set.seed(1234)</span></span>
<span id="cb25-7"></span>
<span id="cb25-8"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">raw &lt;- read.csv("data/lca1.csv", header=FALSE)</span></span>
<span id="cb25-9"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">Y &lt;- raw[, -1]</span></span>
<span id="cb25-10"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">colnames(Y) &lt;- paste0("j", 1:ncol(Y))</span></span>
<span id="cb25-11"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">for (j in seq_along(Y)) Y[[j]] &lt;- Y[[j]] + 1L</span></span>
<span id="cb25-12"></span>
<span id="cb25-13"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f &lt;- as.formula(paste0("cbind(", paste(colnames(Y), collapse=","), ") ~ 1"))</span></span>
<span id="cb25-14"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">fit &lt;- poLCA(f, data=Y, nclass=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>K<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;">, nrep=100, maxiter=5000, tol=1e-10,</span></span>
<span id="cb25-15"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">             verbose=FALSE, graphs=FALSE)</span></span>
<span id="cb25-16"></span>
<span id="cb25-17"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">cat(sprintf("LLIK %.12f</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">n", fit$llik))</span></span>
<span id="cb25-18"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">cat(paste("PI", paste(fit$P, collapse=" "), sep=" "), "</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">n")</span></span>
<span id="cb25-19"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">for (j in seq_len(ncol(Y))) </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">{{</span></span>
<span id="cb25-20"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  p &lt;- fit$probs[[paste0("j", j)]][, 2]</span></span>
<span id="cb25-21"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  cat(paste0("J", j, " ", paste(p, collapse=" "), "</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\\</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">n"))</span></span>
<span id="cb25-22"><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">}}</span></span>
<span id="cb25-23"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span>)</span>
<span id="cb25-24"></span>
<span id="cb25-25">res <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> subprocess.run([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rscript"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-e"</span>, r_code], check<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;">True</span>, capture_output<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;">True</span>, text<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;">True</span>)</span>
<span id="cb25-26">lines <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [ln.strip() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ln <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> res.stdout.splitlines() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ln.strip()]</span>
<span id="cb25-27">lines <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [ln <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ln <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> lines <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> ln.startswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LLIK"</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> ln.startswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PI"</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">or</span> ln.startswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"J"</span>)]</span>
<span id="cb25-28"></span>
<span id="cb25-29">polca_llik <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>(lines[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].split()[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb25-30">freq_pi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(x) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> lines[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>].split()[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]], dtype<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>)</span>
<span id="cb25-31">freq_theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((K, J))</span>
<span id="cb25-32"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> ln <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> lines[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>:]:</span>
<span id="cb25-33">    parts <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ln.split()</span>
<span id="cb25-34">    j <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;">int</span>(parts[<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="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="cb25-35">    freq_theta[:, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>(x) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> parts[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]], dtype<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>)</span>
<span id="cb25-36"></span>
<span id="cb25-37">freq_labels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"poLCA class </span><span class="sc" 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;">1</span><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> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K)]</span>
<span id="cb25-38"></span>
<span id="cb25-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Match classes: Bayes classes are ordered by radius (C1 = smallest r = most baseline)</span></span>
<span id="cb25-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We align classes by solving a global assignment problem (Hungarian algorithm)</span></span>
<span id="cb25-41"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># on profile distance with a small class-proportion penalty.</span></span>
<span id="cb25-42"></span>
<span id="cb25-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build comparison table</span></span>
<span id="cb25-44"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</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;">80</span>)</span>
<span id="cb25-45"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Frequentist (poLCA) vs Bayesian Geometric"</span>)</span>
<span id="cb25-46"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</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;">80</span>)</span>
<span id="cb25-47"></span>
<span id="cb25-48"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Global class matching</span></span>
<span id="cb25-49"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.spatial.distance <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> cdist</span>
<span id="cb25-50"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> scipy.optimize <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> linear_sum_assignment</span>
<span id="cb25-51"></span>
<span id="cb25-52">match_weight_pi <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.25</span></span>
<span id="cb25-53">geo_cost <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cdist(theta_mean, freq_theta) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> match_weight_pi <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;">abs</span>(pi_mean[:, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> freq_pi[<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, :])</span>
<span id="cb25-54">geo_idx, freq_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> linear_sum_assignment(geo_cost)</span>
<span id="cb25-55">geo_for_freq <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {f: g <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> g, f <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;">zip</span>(geo_idx, freq_idx)}</span>
<span id="cb25-56">matched <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(geo_for_freq[f], f) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <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>(K)]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ordered by poLCA class index</span></span>
<span id="cb25-57"></span>
<span id="cb25-58"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;25}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <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;">""</span>.join(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'j'</span><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;">str</span>(j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;7}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)) <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;">"      π"</span>)</span>
<span id="cb25-59"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</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;">80</span>)</span>
<span id="cb25-60"></span>
<span id="cb25-61"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> fk <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>(K):</span>
<span id="cb25-62">    bk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> geo_for_freq[fk]</span>
<span id="cb25-63">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  poLCA </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>freq_labels[fk]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;18}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <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;">""</span>.join(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>freq_theta[fk,j]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;7.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J))</span>
<span id="cb25-64">          <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>freq_pi[fk]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb25-65">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  Geometric C</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>bk<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;12}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <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;">""</span>.join(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>theta_mean[bk,j]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;7.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J))</span>
<span id="cb25-66">          <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_mean[bk]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb25-67">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>()</span>
<span id="cb25-68"></span>
<span id="cb25-69"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"poLCA log-likelihood: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>polca_llik<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb25-70">abs_diff <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;">abs</span>(theta_mean[[geo_for_freq[f] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> f <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>(K)], :] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> freq_theta)</span>
<span id="cb25-71"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Mean absolute item-probability gap (matched classes): </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>abs_diff<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>mean()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb25-72"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Max absolute item-probability gap (matched classes):  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>abs_diff<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;">max</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb25-73"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"="</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;">80</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>================================================================================
Frequentist (poLCA) vs Bayesian Geometric
================================================================================

                              j1     j2     j3     j4     j5     j6     j7     j8     j9      π
--------------------------------------------------------------------------------
  poLCA      poLCA class 1  0.909  0.338  0.065  0.066  0.219  0.321  0.113  0.141  0.326  0.557
  Geometric C           2  0.865  0.372  0.105  0.107  0.250  0.305  0.143  0.162  0.273  0.407

  poLCA      poLCA class 2  0.313  0.164  0.036  0.056  0.045  0.183  0.098  0.110  0.188  0.365
  Geometric C           1  0.501  0.184  0.028  0.040  0.072  0.227  0.085  0.108  0.261  0.505

  poLCA      poLCA class 3  0.923  0.549  0.428  0.421  0.775  0.468  0.517  0.619  0.345  0.078
  Geometric C           3  0.949  0.565  0.341  0.332  0.775  0.509  0.467  0.578  0.393  0.088

poLCA log-likelihood: -4229.31
Mean absolute item-probability gap (matched classes): 0.041
Max absolute item-probability gap (matched classes):  0.188
================================================================================</code></pre>
</div>
</div>
<div id="cell-fig-comparison" class="cell" data-execution_count="19">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, K, figsize<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;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>K, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>), sharey<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;">True</span>)</span>
<span id="cb27-2">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(J)</span>
<span id="cb27-3">w <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.35</span></span>
<span id="cb27-4"></span>
<span id="cb27-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, fk <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;">enumerate</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(K)):</span>
<span id="cb27-6">    bk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> geo_for_freq[fk]</span>
<span id="cb27-7">    ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> axes[i]</span>
<span id="cb27-8">    style_axes(ax)</span>
<span id="cb27-9">    ax.bar(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> w<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>, freq_theta[fk], w, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>], alpha<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.6</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"poLCA: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>freq_labels[fk]<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>)</span>
<span id="cb27-10">    geo_means <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta_mean[bk]</span>
<span id="cb27-11">    q05 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_post[:, bk, :], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">0</span>)</span>
<span id="cb27-12">    q95 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_post[:, bk, :], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</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;">0</span>)</span>
<span id="cb27-13">    ax.bar(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> w<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>, geo_means, w, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>colors[bk], alpha<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.7</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Geometric C</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>bk<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb27-14">    ax.errorbar(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> w<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>, geo_means, yerr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[geo_means <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> q05, q95 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> geo_means],</span>
<span id="cb27-15">                fmt<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;">'none'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>], capsize<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>, lw<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.8</span>)</span>
<span id="cb27-16">    ax.set_xticks(x)</span>
<span id="cb27-17">    ax.set_xticklabels([<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"j</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)], fontsize<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;">7</span>, rotation<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;">45</span>)</span>
<span id="cb27-18">    ax.set_ylim(<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>
<span id="cb27-19">    ax.legend(fontsize<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;">7</span>, loc<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;">"upper right"</span>)</span>
<span id="cb27-20">    ax.set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"π: poLCA=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>freq_pi[fk]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> vs NUTS=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_mean[bk]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, fontweight<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;">"bold"</span>)</span>
<span id="cb27-21">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> i <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;">0</span>:</span>
<span id="cb27-22">        ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P(endorse)"</span>)</span>
<span id="cb27-23"></span>
<span id="cb27-24">plt.tight_layout()</span>
<span id="cb27-25">plt.show()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display">
<div id="fig-comparison" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-comparison-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-4-poincare-lca/poincare-lca_files/figure-html/fig-comparison-output-1.png" width="1430" height="423" class="figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-comparison-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: Frequentist poLCA (gray) vs Bayesian geometric (color) item probabilities with 90% credible intervals.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="what-this-run-shows" class="level2">
<h2 class="anchored" data-anchor-id="what-this-run-shows">8. What This Run Shows</h2>
<ul>
<li>NUTS diagnostics are strong (no divergences, <img src="https://latex.codecogs.com/png.latex?%5Chat%7BR%7D"> near 1), so posterior summaries are stable.</li>
<li>Class separation is now clear in both latent space and profile space.</li>
<li>Matched class proportions are close to poLCA (Bayes about 0.51, 0.41, 0.09 vs poLCA about 0.56, 0.37, 0.08).</li>
<li>Remaining mismatch to poLCA is modest (mean absolute item-probability gap about 0.04; maximum about 0.19).</li>
</ul>
<p>The practical result is that the restricted Poincare specification is now doing what it should: identifiable classes, interpretable geometry, and competitive fit on this dataset.</p>
</section>
<section id="optional-extension-geometric-mean-with-sparse-departures" class="level2">
<h2 class="anchored" data-anchor-id="optional-extension-geometric-mean-with-sparse-departures">9. Optional Extension: Geometric Mean with Sparse Departures</h2>
<p>The base model constrains all item-class logits to the cosine surface <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Blogit%7D(%5Ctheta_%7Bkj%7D)%20=%20%5Calpha_j%20+%20%5Clambda_j%20r_k%20%5Ccos(%5Calpha_k%20-%20%5Cvarphi_j).%0A"> This is interpretable, but it can be too rigid in some datasets.</p>
<p>The extension keeps the geometric mean structure and adds sparse deviations with a regularized horseshoe prior <span class="citation" data-cites="piironen2017">(Piironen and Vehtari 2017)</span>: <img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Blogit%7D(%5Ctheta_%7Bkj%7D)%20=%20%5Cmu_%7Bkj%7D%20+%20%5Cdelta_%7Bkj%7D,%20%5Cqquad%0A%5Cdelta_%7Bkj%7D%20=%20z_%7Bkj%7D%5C,%5Ctau%5C,%5Ctilde%7B%5Clambda%7D_%7Bkj%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cmu_%7Bkj%7D%20=%20%5Calpha_j%20+%20%5Clambda_j%20r_k%20%5Ccos(%5Calpha_k%20-%20%5Cvarphi_j)"> is the geometric prediction, <img src="https://latex.codecogs.com/png.latex?%0Az_%7Bkj%7D%5Csim%5Cmathcal%7BN%7D(0,1),%5Cquad%0A%5Clambda_%7Bkj%7D%5Csim%5Cmathcal%7BC%7D%5E+(0,1),%5Cquad%0A%5Ctau%5Csim%5Cmathcal%7BC%7D%5E+(0,%5Ctau_0),%0A"> and the regularized local scale is <img src="https://latex.codecogs.com/png.latex?%0A%5Ctilde%7B%5Clambda%7D_%7Bkj%7D%0A=%5Csqrt%7B%5Cfrac%7Bc%5E2%5Clambda_%7Bkj%7D%5E2%7D%7Bc%5E2+%5Ctau%5E2%5Clambda_%7Bkj%7D%5E2%7D%7D,%0A%5Cqquad%20c%5E2%5Csim%5Ctext%7BInv-Gamma%7D%5C!%5Cleft(%5Cfrac%7B%5Cnu%7D%7B2%7D,%5Cfrac%7B%5Cnu%7D%7B2%7D%5Cright).%0A"> This prior shrinks most <img src="https://latex.codecogs.com/png.latex?%5Cdelta_%7Bkj%7D"> values toward zero and only allows larger departures where the pure geometry misses local structure.</p>
<p>Class identification still comes from ordered radii. Departures then act as diagnostics for item-class cells that need extra flexibility.</p>
<div id="departure-compile" class="cell" data-execution_count="20">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1">model_dep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> CmdStanModel(stan_file<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;">"lca_poincare_departure.stan"</span>)</span></code></pre></div></div>
</details>
</div>
<div id="departure-helpers" class="cell" data-execution_count="21">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> random_init_departure(K, J, rng):</span>
<span id="cb29-2">    init <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> random_init(K, J, rng)</span>
<span id="cb29-3">    init[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'z_delta'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (rng.randn(K, J) <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.1</span>).tolist()</span>
<span id="cb29-4">    init[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hs_lambda'</span>] <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;">abs</span>(rng.randn(K, J)) <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.3</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.1</span>).tolist()</span>
<span id="cb29-5">    init[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hs_tau'</span>] <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>(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(rng.randn()) <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.1</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.1</span>)</span>
<span id="cb29-6">    init[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hs_c2_aux'</span>] <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>(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(rng.randn()) <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.0</span>)</span>
<span id="cb29-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> init</span>
<span id="cb29-8"></span>
<span id="cb29-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> find_best_optimum_departure(model, stan_data, n_starts<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;">20</span>, seed<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;">42</span>):</span>
<span id="cb29-10">    best_opt, best_lp <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;">None</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.inf</span>
<span id="cb29-11">    rng <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.RandomState(seed)</span>
<span id="cb29-12">    K, J <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> stan_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'K'</span>], stan_data[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'J'</span>]</span>
<span id="cb29-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <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>(n_starts):</span>
<span id="cb29-14">        init <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> random_init_departure(K, J, rng)</span>
<span id="cb29-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb29-16">            opt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model.optimize(data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>stan_data, inits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>init, jacobian<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;">True</span>,</span>
<span id="cb29-17">                                 seed<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>seed <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> i, show_console<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;">False</span>)</span>
<span id="cb29-18">            lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> opt.optimized_params_dict[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lp__'</span>]</span>
<span id="cb29-19">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> best_lp:</span>
<span id="cb29-20">                best_lp, best_opt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lp, opt</span>
<span id="cb29-21">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>:</span>
<span id="cb29-22">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb29-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> best_opt</span>
<span id="cb29-24"></span>
<span id="cb29-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> opt_to_nuts_init_departure(opt, K, J, jitter<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.02</span>, rng<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;">None</span>):</span>
<span id="cb29-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> rng <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb29-27">        rng <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.RandomState()</span>
<span id="cb29-28">    dd <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> opt.optimized_params_dict</span>
<span id="cb29-29">    logit_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sort(np.array([dd[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'logit_r[</span><span class="sc" 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;">1</span><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>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K)]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn(K) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter)</span>
<span id="cb29-30">    angle_raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([dd[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'angle_raw[</span><span class="sc" 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;">1</span><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>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K)]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn(K) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter</span>
<span id="cb29-31">    alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([dd[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'alpha[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]'</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn(J) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter</span>
<span id="cb29-32">    lam <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(np.array([dd[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'lam[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]'</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn(J) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb29-33">    pi_w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.maximum(np.array([dd[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'pi_w[</span><span class="sc" 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;">1</span><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>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K)]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn(K) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter <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.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>)</span>
<span id="cb29-34">    pi_w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/=</span> pi_w.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>()</span>
<span id="cb29-35">    z_delta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([[dd[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'z_delta[</span><span class="sc" 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;">1</span><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><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]'</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K)])</span>
<span id="cb29-36">    hs_lambda <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([[dd[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'hs_lambda[</span><span class="sc" 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;">1</span><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><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]'</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K)])</span>
<span id="cb29-37">    hs_tau <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;">max</span>(dd[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hs_tau'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter <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.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-3</span>)</span>
<span id="cb29-38">    hs_c2_aux <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;">max</span>(dd[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hs_c2_aux'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn() <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter <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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-3</span>)</span>
<span id="cb29-39">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(logit_r<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>logit_r.tolist(), angle_raw<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>angle_raw.tolist(),</span>
<span id="cb29-40">                alpha<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>alpha.tolist(), lam<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>lam.tolist(), pi_w<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>pi_w.tolist(),</span>
<span id="cb29-41">                z_delta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(z_delta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn(K, J) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter).tolist(),</span>
<span id="cb29-42">                hs_lambda<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>np.maximum(hs_lambda <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> rng.randn(K, J) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> jitter, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>).tolist(),</span>
<span id="cb29-43">                hs_tau<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>(hs_tau), hs_c2_aux<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>(hs_c2_aux))</span></code></pre></div></div>
</details>
</div>
<div id="departure-fit" class="cell" data-execution_count="22">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb30-1">best_opt_dep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> find_best_optimum_departure(model_dep, stan_data, n_starts<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;">20</span>, seed<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;">42</span>)</span>
<span id="cb30-2">d_dep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> best_opt_dep.optimized_params_dict</span>
<span id="cb30-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Geometric model MAP lp__:  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lp__'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb30-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Departure model MAP lp__:  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>d_dep[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lp__'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb30-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Difference:                </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>d_dep[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lp__'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> d[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'lp__'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb30-6">sd_total <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;">sum</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">abs</span>(d_dep[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'sigma_delta[</span><span class="sc" 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;">1</span><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><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]'</span>]) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J))</span>
<span id="cb30-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">MAP total |δ|:            </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>sd_total<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb30-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"MAP horseshoe τ:           </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>d_dep[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hs_tau'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb30-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"MAP horseshoe slab aux:    </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>d_dep[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hs_c2_aux'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb30-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">MAP departures δ[k,j] (regularized horseshoe):"</span>)</span>
<span id="cb30-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;30s}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <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;">""</span>.join(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'j'</span><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;">str</span>(j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;8}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)))</span>
<span id="cb30-12"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K):</span>
<span id="cb30-13">    vals <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [d_dep[<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'sigma_delta[</span><span class="sc" 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;">1</span><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><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]'</span>] <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)]</span>
<span id="cb30-14">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'C'</span><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;">str</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;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;30s}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <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;">""</span>.join(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><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;">:&gt;8.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> v <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> vals))</span>
<span id="cb30-15"></span>
<span id="cb30-16">chains_dep <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;">4</span></span>
<span id="cb30-17">inits_dep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [opt_to_nuts_init_departure(best_opt_dep, K, J, jitter<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.02</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.01</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>c,</span>
<span id="cb30-18">                                         rng<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>np.random.RandomState(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c <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;">300</span>))</span>
<span id="cb30-19">             <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> c <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>(chains_dep)]</span>
<span id="cb30-20"></span>
<span id="cb30-21">fit_dep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model_dep.sample(</span>
<span id="cb30-22">    data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>stan_data, chains<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>chains_dep,</span>
<span id="cb30-23">    iter_warmup<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;">3000</span>, iter_sampling<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;">2000</span>,</span>
<span id="cb30-24">    seed<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;">42</span>, inits<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>inits_dep,</span>
<span id="cb30-25">    adapt_delta<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.98</span>, max_treedepth<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;">12</span>,</span>
<span id="cb30-26">    show_progress<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;">False</span>,</span>
<span id="cb30-27">)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [1] done processing
10:20:36 - cmdstanpy - INFO - CmdStan start processing
10:20:36 - cmdstanpy - INFO - Chain [1] start processing
10:20:36 - cmdstanpy - INFO - Chain [2] start processing
10:20:36 - cmdstanpy - INFO - Chain [3] start processing
10:20:36 - cmdstanpy - INFO - Chain [4] start processing</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Geometric model MAP lp__:  -4246.63
Departure model MAP lp__:  -4274.26
Difference:                -27.63

MAP total |δ|:            1.3205
MAP horseshoe τ:           0.2048
MAP horseshoe slab aux:    1.0727

MAP departures δ[k,j] (regularized horseshoe):
                                      j1      j2      j3      j4      j5      j6      j7      j8      j9
                              C1 -0.0441 -0.0028  0.0204  0.0558 -0.0334 -0.0588  0.0366  0.0136 -0.1448
                              C2  0.0673  0.0049 -0.0581 -0.1044  0.0332  0.1001 -0.0513 -0.0229  0.2180
                              C3 -0.0232 -0.0021  0.0376  0.0486  0.0002 -0.0413  0.0147  0.0092 -0.0731</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>10:20:57 - cmdstanpy - INFO - Chain [2] done processing
10:20:58 - cmdstanpy - INFO - Chain [3] done processing
10:21:03 - cmdstanpy - INFO - Chain [1] done processing
10:21:09 - cmdstanpy - INFO - Chain [4] done processing</code></pre>
</div>
</div>
<div id="departure-diagnostics" class="cell" data-execution_count="23">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb34-1">diag_dep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_dep.diagnose()</span>
<span id="cb34-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> line <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> diag_dep.split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>):</span>
<span id="cb34-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> line.strip():</span>
<span id="cb34-4">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(line)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Checking sampler transitions treedepth.
Treedepth satisfactory for all transitions.
Checking sampler transitions for divergences.
No divergent transitions found.
Checking E-BFMI - sampler transitions HMC potential energy.
E-BFMI satisfactory.
Rank-normalized split effective sample size satisfactory for all parameters.
Rank-normalized split R-hat values satisfactory for all parameters.
Processing complete, no problems detected.</code></pre>
</div>
</div>
<section id="horseshoe-sparsity-diagnostics" class="level3">
<h3 class="anchored" data-anchor-id="horseshoe-sparsity-diagnostics">Horseshoe Sparsity Diagnostics</h3>
<p>The regularized horseshoe does not hard-threshold coefficients, but shrinks most departures toward zero and allows a few larger effects. Though this extension isn’t helpful in this dataset, it might be useful for more complex data scenarios.</p>
<div id="l1-budget" class="cell" data-execution_count="24">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb36-1">delta_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_dep.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'sigma_delta'</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># K×J departure (= delta)</span></span>
<span id="cb36-2">l1_norm <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;">abs</span>(delta_post).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># per-draw L1 norm</span></span>
<span id="cb36-3">q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(l1_norm, [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">95</span>])</span>
<span id="cb36-4">tau_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_dep.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hs_tau'</span>)</span>
<span id="cb36-5">q_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(tau_post, [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">95</span>])</span>
<span id="cb36-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Posterior horseshoe τ: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q_tau[<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;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q_tau[<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;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q_tau[<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;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]"</span>)</span>
<span id="cb36-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Posterior total |δ|:   </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q[<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;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q[<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;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q[<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;">:.4f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]"</span>)</span>
<span id="cb36-8">n_material <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;">abs</span>(delta_post) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb36-9">q_mat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(n_material, [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">95</span>])</span>
<span id="cb36-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Entries with |δ| &gt; 0.05 (of </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>K<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>J<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><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q_mat[<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;">:.0f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q_mat[<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;">:.0f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>q_mat[<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;">:.0f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Posterior horseshoe τ: 0.0912 [0.0097, 0.2326]
Posterior total |δ|:   1.2429 [0.1282, 3.3400]
Entries with |δ| &gt; 0.05 (of 27): 9 [0, 20]</code></pre>
</div>
</div>
</section>
<section id="departure-heatmap" class="level3">
<h3 class="anchored" data-anchor-id="departure-heatmap">Departure Heatmap</h3>
<div id="cell-fig-delta-heatmap" class="cell" data-execution_count="25">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb38-1">delta_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_dep.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'delta'</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># = sigma_delta</span></span>
<span id="cb38-2">delta_mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> delta_post.mean(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;">0</span>)</span>
<span id="cb38-3"></span>
<span id="cb38-4">fig, ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(figsize<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;">10</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>))</span>
<span id="cb38-5">style_axes(ax, grid<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;">False</span>)</span>
<span id="cb38-6">vmax <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;">abs</span>(delta_mean).<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">max</span>()</span>
<span id="cb38-7">blog_div <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> LinearSegmentedColormap.from_list(</span>
<span id="cb38-8">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blog_div"</span>,</span>
<span id="cb38-9">    [blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>], blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"background"</span>], blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>]],</span>
<span id="cb38-10">)</span>
<span id="cb38-11">im <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ax.imshow(delta_mean, cmap<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog_div, vmin<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=-</span>vmax, vmax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vmax, aspect<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;">'auto'</span>)</span>
<span id="cb38-12">ax.set_xticks(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(J))</span>
<span id="cb38-13">ax.set_xticklabels([<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"j</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>item_labels[j]<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> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)], fontsize<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;">7</span>)</span>
<span id="cb38-14">ax.set_yticks(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(K))</span>
<span id="cb38-15">ax.set_yticklabels([<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"C</span><span class="sc" 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;">1</span><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> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K)])</span>
<span id="cb38-16"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <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>(K):</span>
<span id="cb38-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J):</span>
<span id="cb38-18">        ax.text(j, k, <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>delta_mean[k,j]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:+.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, ha<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;">'center'</span>, va<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;">'center'</span>, fontsize<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;">8</span>,</span>
<span id="cb38-19">                color<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;">'white'</span> <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;">abs</span>(delta_mean[k,j]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> vmax<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.6</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>])</span>
<span id="cb38-20">cbar <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.colorbar(im, ax<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>ax, label<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;">"δ (logit scale)"</span>)</span>
<span id="cb38-21">cbar.outline.set_edgecolor(blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>])</span>
<span id="cb38-22">cbar.ax.yaxis.label.set_color(blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>])</span>
<span id="cb38-23">cbar.ax.tick_params(color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>], labelcolor<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>])</span>
<span id="cb38-24">cbar.ax.set_facecolor(blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"background"</span>])</span>
<span id="cb38-25">ax.set_title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Departures from Geometric Prediction"</span>, fontweight<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;">"bold"</span>)</span>
<span id="cb38-26">plt.tight_layout()</span>
<span id="cb38-27">plt.show()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display">
<div id="fig-delta-heatmap" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-delta-heatmap-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-4-poincare-lca/poincare-lca_files/figure-html/fig-delta-heatmap-output-1.png" width="896" height="328" class="figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-delta-heatmap-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;6: Posterior mean departures δ[k,j] = logit(θ) − geometric prediction. Red = item-class pair endorses more than geometry predicts; blue = less.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="model-comparison-via-loo" class="level3">
<h3 class="anchored" data-anchor-id="model-comparison-via-loo">Model Comparison via LOO</h3>
<div id="loo-comparison" class="cell" data-execution_count="26">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb39-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> importlib.util</span>
<span id="cb39-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> warnings</span>
<span id="cb39-3">warnings.filterwarnings(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ignore"</span>)</span>
<span id="cb39-4"></span>
<span id="cb39-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> importlib.util.find_spec(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"arviz"</span>) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb39-6">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Skipping LOO comparison: 'arviz' is not installed in the active Python environment."</span>)</span>
<span id="cb39-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb39-8">    <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> arviz <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> az</span>
<span id="cb39-9"></span>
<span id="cb39-10">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> make_loo_idata(fit_obj, cnt):</span>
<span id="cb39-11">        ll_pattern <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_obj.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'log_lik'</span>)</span>
<span id="cb39-12">        ll_obs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.repeat(ll_pattern, cnt, 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="cb39-13">        n_chains <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_obj.chains</span>
<span id="cb39-14">        n_draws <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_obj.num_draws_sampling</span>
<span id="cb39-15">        ll_3d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ll_obs.reshape(n_chains, n_draws, <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="cb39-16">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> az.from_dict(</span>
<span id="cb39-17">            log_likelihood<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;">"y"</span>: ll_3d},</span>
<span id="cb39-18">            posterior<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;">"mu"</span>: np.zeros((n_chains, n_draws))}</span>
<span id="cb39-19">        )</span>
<span id="cb39-20"></span>
<span id="cb39-21">    idata_geo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_loo_idata(fit, cnt)</span>
<span id="cb39-22">    idata_dep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_loo_idata(fit_dep, cnt)</span>
<span id="cb39-23"></span>
<span id="cb39-24">    loo_geo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> az.loo(idata_geo)</span>
<span id="cb39-25">    loo_dep <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> az.loo(idata_dep)</span>
<span id="cb39-26"></span>
<span id="cb39-27">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Geometric model ELPD (LOO): </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>loo_geo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>elpd_loo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> ± </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>loo_geo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>se<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb39-28">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Departure model ELPD (LOO): </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>loo_dep<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>elpd_loo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> ± </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>loo_dep<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>se<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb39-29">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Difference:                 </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>loo_dep<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>elpd_loo <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> loo_geo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>elpd_loo<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.1f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Geometric model ELPD (LOO): -4261.6 ± 56.6
Departure model ELPD (LOO): -4260.4 ± 56.6
Difference:                 1.2</code></pre>
</div>
</div>
</section>
<section id="three-way-comparison-class-proportions" class="level3">
<h3 class="anchored" data-anchor-id="three-way-comparison-class-proportions">Three-Way Comparison: Class Proportions</h3>
<div id="cell-class-comparison" class="cell" data-execution_count="27">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb41-1">theta_dep_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_dep.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'theta'</span>)</span>
<span id="cb41-2">theta_dep_mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta_dep_post.mean(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;">0</span>)</span>
<span id="cb41-3">pi_dep_post <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> fit_dep.stan_variable(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pi_w'</span>)</span>
<span id="cb41-4">pi_dep_mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pi_dep_post.mean(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;">0</span>)</span>
<span id="cb41-5"></span>
<span id="cb41-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Match departure classes directly to poLCA classes using the same cost function</span></span>
<span id="cb41-7">dep_cost <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> cdist(theta_dep_mean, freq_theta) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> match_weight_pi <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;">abs</span>(pi_dep_mean[:, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> freq_pi[<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, :])</span>
<span id="cb41-8">dep_idx, dep_freq_idx <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> linear_sum_assignment(dep_cost)</span>
<span id="cb41-9">dep_for_freq <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {f: d <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> d, f <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;">zip</span>(dep_idx, dep_freq_idx)}</span>
<span id="cb41-10"></span>
<span id="cb41-11">class_rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb41-12"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> fk <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>(K):</span>
<span id="cb41-13">    bk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> geo_for_freq[fk]</span>
<span id="cb41-14">    depk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dep_for_freq[fk]</span>
<span id="cb41-15"></span>
<span id="cb41-16">    pi_geo_q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(pi_post[:, bk], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">95</span>])</span>
<span id="cb41-17">    pi_dep_q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(pi_dep_post[:, depk], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">95</span>])</span>
<span id="cb41-18"></span>
<span id="cb41-19">    class_rows.append({</span>
<span id="cb41-20">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Class"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>freq_labels[fk]<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>,</span>
<span id="cb41-21">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"poLCA π"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>freq_pi[fk]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb41-22">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Geometric class"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"C</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>bk<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb41-23">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Geometric π"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_geo_q[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_geo_q[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_geo_q[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]"</span>,</span>
<span id="cb41-24">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Departure class"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"C</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>depk<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb41-25">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Departure π"</span>: <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_dep_q[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> [</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_dep_q[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_dep_q[<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;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">]"</span>,</span>
<span id="cb41-26">    })</span>
<span id="cb41-27"></span>
<span id="cb41-28">pd.DataFrame(class_rows).set_index(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Class"</span>)</span></code></pre></div></div>
</details>
<div id="class-comparison" class="cell-output cell-output-display" data-execution_count="27">
<div>


<table class="dataframe caption-top table table-sm table-striped small" data-border="1">
<thead>
<tr class="header">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">poLCA π</th>
<th data-quarto-table-cell-role="th">Geometric class</th>
<th data-quarto-table-cell-role="th">Geometric π</th>
<th data-quarto-table-cell-role="th">Departure class</th>
<th data-quarto-table-cell-role="th">Departure π</th>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">Class</th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<th data-quarto-table-cell-role="th">poLCA class 1</th>
<td>0.557</td>
<td>C2</td>
<td>0.403 [0.206, 0.617]</td>
<td>C2</td>
<td>0.426 [0.225, 0.626]</td>
</tr>
<tr class="even">
<th data-quarto-table-cell-role="th">poLCA class 2</th>
<td>0.365</td>
<td>C1</td>
<td>0.510 [0.281, 0.710]</td>
<td>C1</td>
<td>0.488 [0.277, 0.690]</td>
</tr>
<tr class="odd">
<th data-quarto-table-cell-role="th">poLCA class 3</th>
<td>0.078</td>
<td>C3</td>
<td>0.084 [0.044, 0.151]</td>
<td>C3</td>
<td>0.082 [0.046, 0.144]</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
</section>
<section id="three-way-comparison-item-probabilities" class="level3">
<h3 class="anchored" data-anchor-id="three-way-comparison-item-probabilities">Three-Way Comparison: Item Probabilities</h3>
<div id="item-comparison" class="cell" data-execution_count="28">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb42-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> fk <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>(K):</span>
<span id="cb42-2">    bk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> geo_for_freq[fk]</span>
<span id="cb42-3">    depk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dep_for_freq[fk]</span>
<span id="cb42-4">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'='</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;">95</span><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>)</span>
<span id="cb42-5">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>freq_labels[fk]<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;">  (Geo=C</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>bk<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, Dep=C</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>depk<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)"</span>)</span>
<span id="cb42-6">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'='</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;">95</span><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>)</span>
<span id="cb42-7">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;12}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <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;">""</span>.join(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'j'</span><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;">str</span>(j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)))</span>
<span id="cb42-8">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</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;">93</span><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>)</span>
<span id="cb42-9"></span>
<span id="cb42-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># poLCA</span></span>
<span id="cb42-11">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'poLCA'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;12}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <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;">""</span>.join(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>freq_theta[fk,j]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)))</span>
<span id="cb42-12"></span>
<span id="cb42-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Geometric with CI</span></span>
<span id="cb42-14">    geo_q05 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_post[:, bk, :], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">0</span>)</span>
<span id="cb42-15">    geo_q95 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_post[:, bk, :], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</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;">0</span>)</span>
<span id="cb42-16">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Geometric'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;12}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <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;">""</span>.join(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>theta_mean[bk,j]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)))</span>
<span id="cb42-17">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'90% CI'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;12}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <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;">""</span>.join(</span>
<span id="cb42-18">        <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'['</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>geo_q05[j]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><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;">','</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>geo_q95[j]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><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;">']'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)))</span>
<span id="cb42-19"></span>
<span id="cb42-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Departure with CI</span></span>
<span id="cb42-21">    dep_q05 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_dep_post[:, depk, :], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">0</span>)</span>
<span id="cb42-22">    dep_q95 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_dep_post[:, depk, :], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</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;">0</span>)</span>
<span id="cb42-23">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Departure'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;12}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <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;">""</span>.join(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>theta_dep_mean[depk,j]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)))</span>
<span id="cb42-24">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'90% CI'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;12}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <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;">""</span>.join(</span>
<span id="cb42-25">        <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'['</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>dep_q05[j]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><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;">','</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>dep_q95[j]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><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;">']'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:&gt;10}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)))</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>
===============================================================================================
  poLCA class 1  (Geo=C2, Dep=C2)
===============================================================================================
                      j1        j2        j3        j4        j5        j6        j7        j8        j9
  ---------------------------------------------------------------------------------------------
         poLCA     0.909     0.338     0.065     0.066     0.219     0.321     0.113     0.141     0.326
     Geometric     0.865     0.372     0.105     0.107     0.250     0.305     0.143     0.162     0.273
        90% CI[0.77,0.95][0.29,0.47][0.06,0.17][0.07,0.15][0.16,0.38][0.26,0.35][0.11,0.19][0.12,0.22][0.24,0.30]
     Departure     0.866     0.368     0.101     0.103     0.245     0.308     0.140     0.159     0.283
        90% CI[0.78,0.94][0.29,0.46][0.06,0.15][0.07,0.15][0.16,0.36][0.26,0.36][0.11,0.18][0.12,0.21][0.25,0.33]

===============================================================================================
  poLCA class 2  (Geo=C1, Dep=C1)
===============================================================================================
                      j1        j2        j3        j4        j5        j6        j7        j8        j9
  ---------------------------------------------------------------------------------------------
         poLCA     0.313     0.164     0.036     0.056     0.045     0.183     0.098     0.110     0.188
     Geometric     0.501     0.184     0.028     0.040     0.072     0.227     0.085     0.108     0.261
        90% CI[0.35,0.61][0.13,0.23][0.01,0.05][0.02,0.06][0.04,0.11][0.19,0.26][0.06,0.11][0.08,0.13][0.23,0.29]
     Departure     0.485     0.180     0.028     0.040     0.069     0.221     0.086     0.108     0.252
        90% CI[0.33,0.60][0.13,0.23][0.01,0.04][0.02,0.06][0.04,0.10][0.18,0.26][0.06,0.11][0.08,0.13][0.21,0.29]

===============================================================================================
  poLCA class 3  (Geo=C3, Dep=C3)
===============================================================================================
                      j1        j2        j3        j4        j5        j6        j7        j8        j9
  ---------------------------------------------------------------------------------------------
         poLCA     0.923     0.549     0.428     0.421     0.775     0.468     0.517     0.619     0.345
     Geometric     0.949     0.565     0.341     0.332     0.775     0.509     0.467     0.578     0.393
        90% CI[0.90,0.98][0.46,0.67][0.23,0.47][0.23,0.46][0.60,0.92][0.40,0.62][0.32,0.64][0.40,0.78][0.29,0.52]
     Departure     0.948     0.563     0.345     0.338     0.777     0.505     0.472     0.582     0.389
        90% CI[0.90,0.98][0.46,0.67][0.23,0.48][0.23,0.46][0.62,0.92][0.39,0.62][0.34,0.64][0.41,0.77][0.29,0.51]</code></pre>
</div>
</div>
<div id="cell-fig-three-way" class="cell" data-execution_count="29">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb44-1">fig, axes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> plt.subplots(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, K, figsize<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;">5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>K, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>), sharey<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;">True</span>)</span>
<span id="cb44-2">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.arange(J)</span>
<span id="cb44-3">w <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.27</span></span>
<span id="cb44-4"></span>
<span id="cb44-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, fk <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;">enumerate</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(K)):</span>
<span id="cb44-6">    bk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> geo_for_freq[fk]</span>
<span id="cb44-7">    depk <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> dep_for_freq[fk]</span>
<span id="cb44-8">    ax <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> axes[i]</span>
<span id="cb44-9">    style_axes(ax)</span>
<span id="cb44-10">    ax.bar(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> w, freq_theta[fk], w, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text_light"</span>], alpha<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.6</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"poLCA: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>freq_labels[fk]<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>)</span>
<span id="cb44-11"></span>
<span id="cb44-12">    geo_means <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta_mean[bk]</span>
<span id="cb44-13">    geo_q05 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_post[:, bk, :], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">0</span>)</span>
<span id="cb44-14">    geo_q95 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_post[:, bk, :], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</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;">0</span>)</span>
<span id="cb44-15">    ax.bar(x, geo_means, w, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>colors[bk], alpha<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.35</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Geometric C</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>bk<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb44-16">    ax.errorbar(x, geo_means, yerr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[geo_means <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> geo_q05, geo_q95 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> geo_means],</span>
<span id="cb44-17">                fmt<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;">'none'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>], capsize<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>, lw<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.8</span>)</span>
<span id="cb44-18"></span>
<span id="cb44-19">    dep_means <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> theta_dep_mean[depk]</span>
<span id="cb44-20">    dep_q05 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_dep_post[:, depk, :], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">0</span>)</span>
<span id="cb44-21">    dep_q95 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.percentile(theta_dep_post[:, depk, :], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</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;">0</span>)</span>
<span id="cb44-22">    ax.bar(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> w, dep_means, w, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>colors[bk], alpha<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.8</span>, label<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Departure C</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>depk<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb44-23">    ax.errorbar(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> w, dep_means, yerr<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>[dep_means <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> dep_q05, dep_q95 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> dep_means],</span>
<span id="cb44-24">                fmt<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;">'none'</span>, color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>blog[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>], capsize<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>, lw<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.8</span>)</span>
<span id="cb44-25"></span>
<span id="cb44-26">    ax.set_xticks(x)</span>
<span id="cb44-27">    ax.set_xticklabels([<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"j</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>j<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j <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>(J)], fontsize<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;">7</span>, rotation<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;">45</span>)</span>
<span id="cb44-28">    ax.set_ylim(<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>
<span id="cb44-29">    ax.legend(fontsize<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;">6</span>, loc<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;">"upper right"</span>)</span>
<span id="cb44-30">    ax.set_title(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"π: poLCA=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>freq_pi[fk]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, Geo=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_mean[bk]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, Dep=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pi_dep_mean[depk]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.3f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>,</span>
<span id="cb44-31">                 fontweight<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;">"bold"</span>, fontsize<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>
<span id="cb44-32">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> i <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;">0</span>:</span>
<span id="cb44-33">        ax.set_ylabel(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"P(endorse)"</span>)</span>
<span id="cb44-34"></span>
<span id="cb44-35">plt.tight_layout()</span>
<span id="cb44-36">plt.show()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-display">
<div id="fig-three-way" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-three-way-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://spinkney.github.io/posts/post-4-poincare-lca/poincare-lca_files/figure-html/fig-three-way-output-1.png" width="1430" height="422" class="figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-three-way-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;7: Three-way comparison: poLCA (gray), geometric (light), and departure model (saturated) item probabilities with 90% credible intervals.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="appendix-stan-code" class="level2">
<h2 class="anchored" data-anchor-id="appendix-stan-code">Appendix: Stan Code</h2>
<div class="code-with-filename">
<div class="code-with-filename-file">
<pre><strong>lca_poincare.stan</strong></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb45" data-filename="lca_poincare.stan" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb45-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// lca_poincare.stan</span></span>
<span id="cb45-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// LCA with Restricted Cosine Poincaré Disk + BLAS matmul likelihood</span></span>
<span id="cb45-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// =================================================================</span></span>
<span id="cb45-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span></span>
<span id="cb45-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// theta[k,j] = inv_logit(alpha[j] + lam[j] * r[k] * cos(angle[k] - phi[j]))</span></span>
<span id="cb45-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span></span>
<span id="cb45-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Items spread evenly at phi[j] = (π/2)(j-1)/(J-1) in [0, π/2].</span></span>
<span id="cb45-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Classes identified by ordered radii.</span></span>
<span id="cb45-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span></span>
<span id="cb45-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Likelihood uses BLAS matmul: S = Y * η' avoids inner loop over items.</span></span>
<span id="cb45-11"></span>
<span id="cb45-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb45-13">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">&gt;</span> P;                              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// unique response patterns</span></span>
<span id="cb45-14">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;                              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// latent classes</span></span>
<span id="cb45-15">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> J;                              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// items (≥2 for phi spacing)</span></span>
<span id="cb45-16">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[P, J] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</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;">&gt;</span> pattern;  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// unique binary patterns</span></span>
<span id="cb45-17">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[P] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">&gt;</span> count;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// count per pattern</span></span>
<span id="cb45-18">}</span>
<span id="cb45-19"></span>
<span id="cb45-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed data</span> {</span>
<span id="cb45-21">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(count);</span>
<span id="cb45-22">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] phi;</span>
<span id="cb45-23">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>J)</span>
<span id="cb45-24">    phi[j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pi</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;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (j <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.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (J <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.0</span>);</span>
<span id="cb45-25"></span>
<span id="cb45-26">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Real matrix version of pattern for BLAS matmul</span></span>
<span id="cb45-27">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[P, J] Y;</span>
<span id="cb45-28">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (p <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>P)</span>
<span id="cb45-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>J)</span>
<span id="cb45-30">      Y[p, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pattern[p, j];</span>
<span id="cb45-31"></span>
<span id="cb45-32">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">row_vector</span>[J] rv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_row_vector</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, J);</span>
<span id="cb45-33">}</span>
<span id="cb45-34"></span>
<span id="cb45-35"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb45-36">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ordered</span>[K] logit_r;           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// ordered logit-radii → identifiability</span></span>
<span id="cb45-37">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] angle_raw;          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// mapped to (0, pi/2) via scaled logistic</span></span>
<span id="cb45-38">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[J] alpha;              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// item baselines</span></span>
<span id="cb45-39">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>[J] lam;      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// item discriminations</span></span>
<span id="cb45-40">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">simplex</span>[K] pi_w;             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// mixing weights</span></span>
<span id="cb45-41">}</span>
<span id="cb45-42"></span>
<span id="cb45-43"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb45-44">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</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;">&gt;</span>[K] radii;</span>
<span id="cb45-45">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>[K] angles;</span>
<span id="cb45-46">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, J] logit_theta;    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// η[k,j] = alpha[j] + lam[j] * r[k] * cos(a[k] - phi[j])</span></span>
<span id="cb45-47">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// matrix[K, J] theta;</span></span>
<span id="cb45-48"></span>
<span id="cb45-49">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>K) {</span>
<span id="cb45-50">    radii[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_logit</span>(logit_r[k]);</span>
<span id="cb45-51">    angles[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pi</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;">2</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_logit</span>(angle_raw[k]);</span>
<span id="cb45-52">  }</span>
<span id="cb45-53"></span>
<span id="cb45-54">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>K)</span>
<span id="cb45-55">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>J) {</span>
<span id="cb45-56">      logit_theta[k, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> alpha[j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> lam[j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> radii[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>(angles[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> phi[j]);</span>
<span id="cb45-57">     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// theta[k, j] = inv_logit(logit_theta[k, j]);</span></span>
<span id="cb45-58">    }</span>
<span id="cb45-59">}</span>
<span id="cb45-60"></span>
<span id="cb45-61"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb45-62">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Priors</span></span>
<span id="cb45-63">  logit_r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">2</span>);</span>
<span id="cb45-64">  angle_raw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">2</span>);</span>
<span id="cb45-65">  alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">3</span>);</span>
<span id="cb45-66">  lam <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">3</span>);           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// half-normal via &lt;lower=0&gt;</span></span>
<span id="cb45-67">  pi_w <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dirichlet</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep_vector</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, K));</span>
<span id="cb45-68"></span>
<span id="cb45-69">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// --- fast marginalized mixture likelihood ---</span></span>
<span id="cb45-70">  {</span>
<span id="cb45-71">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// For each class k:</span></span>
<span id="cb45-72">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   Σ_j log p(y_j | η_j,k)</span></span>
<span id="cb45-73">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// = (y^T η_:,k) - Σ_j log(1+exp η_j,k)</span></span>
<span id="cb45-74">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">row_vector</span>[K] sum_log1p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1p_exp</span>(logit_theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>); <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// length K</span></span>
<span id="cb45-75">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sum_log1p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb45-76">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] log_nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(pi_w);</span>
<span id="cb45-77"></span>
<span id="cb45-78">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// S[p,k] = Σ_j Y[p,j] * η[k,j]  (BLAS matmul)</span></span>
<span id="cb45-79">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[P, K] S <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> logit_theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb45-80"></span>
<span id="cb45-81">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (p <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>P)</span>
<span id="cb45-82">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> count[p] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log_sum_exp</span>(log_nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (S[p]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c));</span>
<span id="cb45-83">  }</span>
<span id="cb45-84"></span>
<span id="cb45-85">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// --- sufficient-statistics likelihood (loop version) ---</span></span>
<span id="cb45-86">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// {</span></span>
<span id="cb45-87">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   matrix[K, J] lt = log(theta);</span></span>
<span id="cb45-88">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   matrix[K, J] lt1m = log1m(theta);</span></span>
<span id="cb45-89">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span></span>
<span id="cb45-90">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   for (p in 1:P) {</span></span>
<span id="cb45-91">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//     vector[K] lps;</span></span>
<span id="cb45-92">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//     for (k in 1:K) {</span></span>
<span id="cb45-93">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//       lps[k] = log(pi_w[k]);</span></span>
<span id="cb45-94">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//       for (j in 1:J)</span></span>
<span id="cb45-95">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//         lps[k] += pattern[p, j] == 1 ? lt[k, j] : lt1m[k, j];</span></span>
<span id="cb45-96">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//     }</span></span>
<span id="cb45-97">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//     target += count[p] * log_sum_exp(lps);</span></span>
<span id="cb45-98">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//   }</span></span>
<span id="cb45-99">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// }</span></span>
<span id="cb45-100">}</span>
<span id="cb45-101"></span>
<span id="cb45-102"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">generated quantities</span> {</span>
<span id="cb45-103">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] log_lik;</span>
<span id="cb45-104">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, J] theta_free;     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// relaxed (unconstrained) item probs</span></span>
<span id="cb45-105">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> min_profile_dist;</span>
<span id="cb45-106">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, K] hyp_dist;</span>
<span id="cb45-107">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> min_hyp_dist;</span>
<span id="cb45-108"></span>
<span id="cb45-109">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Per-pattern log-lik (BLAS version) + responsibilities for theta_free</span></span>
<span id="cb45-110">  {</span>
<span id="cb45-111">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">row_vector</span>[K] sum_log1p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1p_exp</span>(logit_theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>);</span>
<span id="cb45-112">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sum_log1p<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb45-113">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] log_nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(pi_w);</span>
<span id="cb45-114">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[P, K] S <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> logit_theta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb45-115"></span>
<span id="cb45-116">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// log-lik</span></span>
<span id="cb45-117">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (p <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>P)</span>
<span id="cb45-118">      log_lik[p] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log_sum_exp</span>(log_nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (S[p]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> c));</span>
<span id="cb45-119"></span>
<span id="cb45-120">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Relaxed theta via iterated EM (seeded from geometric model)</span></span>
<span id="cb45-121">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Initial E-step uses geometric logit_theta; then iterate M→E→M...</span></span>
<span id="cb45-122">    {</span>
<span id="cb45-123">      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Seed theta_free from geometric model</span></span>
<span id="cb45-124">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, J] eta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> logit_theta;</span>
<span id="cb45-125"></span>
<span id="cb45-126">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (em_iter <span class="cf" 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;">1</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;">200</span>) {</span>
<span id="cb45-127">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// E-step: responsibilities from current eta</span></span>
<span id="cb45-128">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[P, K] W;</span>
<span id="cb45-129">        {</span>
<span id="cb45-130">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">row_vector</span>[K] slp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> rv <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1p_exp</span>(eta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>);</span>
<span id="cb45-131">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] cc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>slp<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb45-132">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[P, K] SS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> eta<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span>;</span>
<span id="cb45-133"></span>
<span id="cb45-134">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (p <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>P) {</span>
<span id="cb45-135">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] lp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> log_nu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> SS[p]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> cc;</span>
<span id="cb45-136">            <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lse <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log_sum_exp</span>(lp);</span>
<span id="cb45-137">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>K)</span>
<span id="cb45-138">              W[p, k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> count[p] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(lp[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lse);</span>
<span id="cb45-139">          }</span>
<span id="cb45-140">        }</span>
<span id="cb45-141"></span>
<span id="cb45-142">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// M-step: theta_free = weighted sample means</span></span>
<span id="cb45-143">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, J] numer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> W<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">'</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> Y;</span>
<span id="cb45-144">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>K) {</span>
<span id="cb45-145">          <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> denom <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(W[, k]);</span>
<span id="cb45-146">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>J) {</span>
<span id="cb45-147">            theta_free[k, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numer[k, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denom;</span>
<span id="cb45-148">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Update eta for next E-step (logit scale, clamp to avoid ±inf)</span></span>
<span id="cb45-149">            eta[k, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</span>(theta_free[k, j], <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>))</span>
<span id="cb45-150">                       <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmax</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> theta_free[k, j], <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>));</span>
<span id="cb45-151">          }</span>
<span id="cb45-152">        }</span>
<span id="cb45-153">      }</span>
<span id="cb45-154">    }</span>
<span id="cb45-155">  }</span>
<span id="cb45-156"></span>
<span id="cb45-157">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Min pairwise profile distance</span></span>
<span id="cb45-158">  min_profile_dist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">positive_infinity</span>();</span>
<span id="cb45-159">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (a <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>K)</span>
<span id="cb45-160">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (b <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (a <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>K) {</span>
<span id="cb45-161">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> d <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;">0</span>;</span>
<span id="cb45-162">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>J)</span>
<span id="cb45-163">        d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_logit</span>(logit_theta[a, j]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_logit</span>(logit_theta[b, j]));</span>
<span id="cb45-164">      <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;">sqrt</span>(d) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> min_profile_dist)</span>
<span id="cb45-165">        min_profile_dist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(d);</span>
<span id="cb45-166">    }</span>
<span id="cb45-167"></span>
<span id="cb45-168">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Pairwise hyperbolic distances on Poincaré disk</span></span>
<span id="cb45-169">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (a <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>K) {</span>
<span id="cb45-170">    hyp_dist[a, a] <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;">0</span>;</span>
<span id="cb45-171">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (b <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (a <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>K) {</span>
<span id="cb45-172">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> xa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> radii[a] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>(angles[a]);</span>
<span id="cb45-173">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> ya <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> radii[a] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(angles[a]);</span>
<span id="cb45-174">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> xb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> radii[b] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>(angles[b]);</span>
<span id="cb45-175">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> yb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> radii[b] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(angles[b]);</span>
<span id="cb45-176">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> num <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <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;">square</span>(xa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> xb) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(ya <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> yb));</span>
<span id="cb45-177">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> re <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> (xa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> xb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ya <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> yb);</span>
<span id="cb45-178">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> im <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>(xa <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> yb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ya <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> xb);</span>
<span id="cb45-179">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> den <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <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;">square</span>(re) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(im));</span>
<span id="cb45-180">      hyp_dist[a, b] <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">atanh</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmin</span>(num <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> den, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.999</span>));</span>
<span id="cb45-181">      hyp_dist[b, a] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hyp_dist[a, b];</span>
<span id="cb45-182">    }</span>
<span id="cb45-183">  }</span>
<span id="cb45-184"></span>
<span id="cb45-185">  min_hyp_dist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">positive_infinity</span>();</span>
<span id="cb45-186">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (a <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>K)</span>
<span id="cb45-187">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (b <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> (a <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>K)</span>
<span id="cb45-188">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (hyp_dist[a, b] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> min_hyp_dist)</span>
<span id="cb45-189">        min_hyp_dist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> hyp_dist[a, b];</span>
<span id="cb45-190">}</span></code></pre></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-carpenter2017" class="csl-entry">
Carpenter, Bob, Andrew Gelman, Matthew D. Hoffman, et al. 2017. <span>“Stan: A Probabilistic Programming Language.”</span> <em>Journal of Statistical Software</em> 76 (1): 1–32. <a href="https://doi.org/10.18637/jss.v076.i01">https://doi.org/10.18637/jss.v076.i01</a>.
</div>
<div id="ref-jasra2005" class="csl-entry">
Jasra, Ajay, Christopher C. Holmes, and David A. Stephens. 2005. <span>“Markov Chain Monte Carlo Methods and the Label Switching Problem in Bayesian Mixture Modeling.”</span> <em>Statistical Science</em> 20 (1): 50–67. <a href="https://doi.org/10.1214/088342305000000016">https://doi.org/10.1214/088342305000000016</a>.
</div>
<div id="ref-linzer2011" class="csl-entry">
Linzer, Drew A., and Jeffrey B. Lewis. 2011. <span>“poLCA: An r Package for Polytomous Variable Latent Class Analysis.”</span> <em>Journal of Statistical Software</em> 42 (10): 1–29. <a href="https://doi.org/10.18637/jss.v042.i10">https://doi.org/10.18637/jss.v042.i10</a>.
</div>
<div id="ref-nickel2017" class="csl-entry">
Nickel, Maximilian, and Douwe Kiela. 2017. <span>“Poincare Embeddings for Learning Hierarchical Representations.”</span> <em>Advances in Neural Information Processing Systems 30</em>.
</div>
<div id="ref-piironen2017" class="csl-entry">
Piironen, Juho, and Aki Vehtari. 2017. <span>“Sparsity Information and Regularization in the Horseshoe and Other Shrinkage Priors.”</span> <em>Electronic Journal of Statistics</em> 11 (2): 5018–51. <a href="https://doi.org/10.1214/17-EJS1337SI">https://doi.org/10.1214/17-EJS1337SI</a>.
</div>
<div id="ref-stephens2000" class="csl-entry">
Stephens, Matthew. 2000. <span>“Dealing with Label Switching in Mixture Models.”</span> <em>Journal of the Royal Statistical Society Series B</em> 62 (4): 795–809. <a href="https://doi.org/10.1111/1467-9868.00265">https://doi.org/10.1111/1467-9868.00265</a>.
</div>
<div id="ref-ucla_oarc_lca" class="csl-entry">
UCLA Statistical Consulting Group. 2026. <em>Latent Class Analysis</em>. <a href="https://stats.oarc.ucla.edu/sas/dae/latent-class-analysis/">https://stats.oarc.ucla.edu/sas/dae/latent-class-analysis/</a>.
</div>
</div></section></div> ]]></description>
  <category>stan</category>
  <category>model</category>
  <guid>https://spinkney.github.io/posts/post-4-poincare-lca/poincare-lca.html</guid>
  <pubDate>Sat, 28 Feb 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>The Hyper-Tanh Peel: A Novel Parameterization for Unit Vectors</title>
  <dc:creator>Sean Pinkney</dc:creator>
  <link>https://spinkney.github.io/posts/post-3-better-unit-vector/a-better-unit-vector.html</link>
  <description><![CDATA[ 





<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">1. Introduction</h2>
<p>Parameterizing unit vectors (points on a hypersphere <img src="https://latex.codecogs.com/png.latex?S%5E%7BK-1%7D">) in Bayesian models presents unique challenges.</p>
<ol type="1">
<li>Hyperspherical coordinates (angles) involve boundaries (<img src="https://latex.codecogs.com/png.latex?0,%20%5Cpi">) that hinder Hamiltonian Monte Carlo (HMC) sampling.</li>
<li><span class="citation" data-cites="muller1959">Muller (1959)</span>’s method (normalizing standard Gaussians, how Stan parameterizes <code>unit_vector</code>) is isotropic but over-parameterized (<img src="https://latex.codecogs.com/png.latex?K"> parameters for <img src="https://latex.codecogs.com/png.latex?K-1"> dimensions) and suffers from a gradient singularity at the origin.</li>
</ol>
<p>I’m calling this the <strong>hyper-tanh peel</strong> bijective parameterization. It is a mapping <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BR%7D%5E%7BK-1%7D%20%5Cto%20S%5E%7BK-1%7D">. It uses “Logistic Geometry” to create a smooth, unconstrained manifold that is numerically stable and avoids the singularities of traditional methods.</p>
</section>
<section id="mathematical-formulation" class="level2">
<h2 class="anchored" data-anchor-id="mathematical-formulation">2. Mathematical Formulation</h2>
<section id="the-transformation" class="level3">
<h3 class="anchored" data-anchor-id="the-transformation">The Transformation</h3>
<p>The method “peels” dimensions one by one using the hyperbolic tangent function (<img src="https://latex.codecogs.com/png.latex?%5Ctanh">), which maps <img src="https://latex.codecogs.com/png.latex?(-%5Cinfty,%20%5Cinfty)%20%5Cto%20(-1,%201)">, and closes the remaining 2D subspace with a stereographic projection.</p>
<p><strong>Algorithm:</strong></p>
<p>Let <img src="https://latex.codecogs.com/png.latex?r_1%20=%201">. For <img src="https://latex.codecogs.com/png.latex?i%20=%201,%20%5Cdots,%20K-2">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Ay_i%20&amp;=%20r_i%20%5Ccdot%20%5Ctanh(x_i)%20%5C%5C%0Ar_%7Bi+1%7D%20&amp;=%20r_i%20%5Ccdot%20%5Ctext%7Bsech%7D(x_i)%20%5Cquad%20%5Ctext%7Bwhere%20%7D%20%5Ctext%7Bsech%7D(x)%20=%20%5Cfrac%7B1%7D%7B%5Ccosh(x)%7D%20=%20%5Csqrt%7B1%20-%20%5Ctanh%5E2(x)%7D%0A%5Cend%7Baligned%7D%0A"></p>
<p>The final two dimensions are parameterized as a stereographic coordinate:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctheta%20&amp;=%202%20%5Carctan(x_%7BK-1%7D)%20%5C%5C%0Ay_%7BK-1%7D%20&amp;=%20r_%7BK-1%7D%20%5Ccos(%5Ctheta)%20=%20r_%7BK-1%7D%20%5Cfrac%7B1%20-%20x_%7BK-1%7D%5E2%7D%7B1%20+%20x_%7BK-1%7D%5E2%7D%20%5C%5C%0Ay_%7BK%7D%20&amp;=%20r_%7BK-1%7D%20%5Csin(%5Ctheta)%20=%20r_%7BK-1%7D%20%5Cfrac%7B2%20x_%7BK-1%7D%7D%7B1%20+%20x_%7BK-1%7D%5E2%7D%0A%5Cend%7Baligned%7D%0A"></p>
</section>
<section id="the-jacobian-adjustment" class="level3">
<h3 class="anchored" data-anchor-id="the-jacobian-adjustment">The Jacobian Adjustment</h3>
<p>The log-determinant of the Jacobian is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Clog%20%7CJ%7C%20=%20%5Csum_%7Bi=1%7D%5E%7BK-2%7D%20-(K-i)%20%5Clog(%5Ccosh(x_i))%20+%20%5Clog(2)%20-%20%5Clog(1%20+%20x_%7BK-1%7D%5E2)%0A"></p>
<p>This implies that: 1. The peeling parameters follow a sech distribution: <img src="https://latex.codecogs.com/png.latex?p(x_i)%20%5Cpropto%20%5Ctext%7Bsech%7D%5E%7BK-i%7D(x_i)">. 2. The core parameter follows a cauchy distribution: <img src="https://latex.codecogs.com/png.latex?p(x_%7BK-1%7D)%20%5Cpropto%20(1+x_%7BK-1%7D%5E2)%5E%7B-1%7D">.</p>
</section>
</section>
<section id="logistic-geometry-gradient-stability" class="level2">
<h2 class="anchored" data-anchor-id="logistic-geometry-gradient-stability">3. Logistic Geometry &amp; Gradient Stability</h2>
<p>A problem with <span class="citation" data-cites="muller1959">Muller (1959)</span>’s method when used with HMC is the singularity at the origin. In Muller’s parameterization, <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7By%7D%20=%20%5Cmathbf%7Bz%7D%20/%20%5C%7C%5Cmathbf%7Bz%7D%5C%7C">. As <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bz%7D%20%5Cto%200">, the gradient <img src="https://latex.codecogs.com/png.latex?%5Cnabla_%5Cmathbf%7Bz%7D%20%5Cmathbf%7By%7D"> explodes to infinity. This creates a “funnel” that traps HMC samplers.</p>
<p>Advantages of this parameterization: The Hyper-Tanh Peel maps the origin of the parameter space <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bx%7D=%5Cmathbf%7B0%7D"> to the “North Pole” of the sphere <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7By%7D=(1,%200,%20%5Cdots,%200)">.</p>
<p>The derivative of the mapping is governed by <img src="https://latex.codecogs.com/png.latex?%5Cfrac%7Bd%7D%7Bdx%7D%20%5Ctanh(x)%20=%20%5Ctext%7Bsech%7D%5E2(x)">. * At <img src="https://latex.codecogs.com/png.latex?x=0">, <img src="https://latex.codecogs.com/png.latex?%5Ctext%7Bsech%7D%5E2(0)%20=%201">. * The gradient is linear and bounded. There is no singularity.</p>
</section>
<section id="visual-verification" class="level2">
<h2 class="anchored" data-anchor-id="visual-verification">4. Visual Verification</h2>
<p>This demonstrates two key properties using R: 1. Uniformity: With Jacobian correction, Hyper-Tanh matches <span class="citation" data-cites="muller1959">Muller (1959)</span>’s isotropy. 2. Stability: Hyper-Tanh has bounded gradients where Muller explodes.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">library</span>(ggplot2)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load blog theme</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../R/theme_blog.R"</span>)</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- Define Methods ---</span></span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Muller's Method (Standard)</span></span>
<span id="cb1-12">generate_muller <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, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) {</span>
<span id="cb1-13">  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;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> K), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol=</span>K)</span>
<span id="cb1-14">  y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> z <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(z<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-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Muller (1959)"</span>))</span>
<span id="cb1-16">}</span>
<span id="cb1-17"></span>
<span id="cb1-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. Hyper-Tanh (Uniform via Inverse Jacobian Sampling)</span></span>
<span id="cb1-19">generate_hypertanh_uniform <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, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) {</span>
<span id="cb1-20">  y_out <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;">matrix</span>(<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;">nrow=</span>N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol=</span>K)</span>
<span id="cb1-21">  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;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, N)</span>
<span id="cb1-22">  </span>
<span id="cb1-23">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Peel Phase (Dim 1 to K-2)</span></span>
<span id="cb1-24">  <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="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>(K<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-2</span>)) {</span>
<span id="cb1-25">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Inverse sample from p(x) ~ sech^(K-i)(x)</span></span>
<span id="cb1-26">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For K=3, i=1: p(x) ~ sech^2(x) -&gt; CDF = (tanh(x)+1)/2</span></span>
<span id="cb1-27">    u <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;">runif</span>(N)</span>
<span id="cb1-28">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clip for stability</span></span>
<span id="cb1-29">    u <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;">pmax</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(u, <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;">1e-7</span>), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-7</span>)</span>
<span id="cb1-30">    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;">atanh</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>u <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-31">    </span>
<span id="cb1-32">    y_out[,i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> r <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;">tanh</span>(x)</span>
<span id="cb1-33">    r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> r <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cosh</span>(x))</span>
<span id="cb1-34">  }</span>
<span id="cb1-35">  </span>
<span id="cb1-36">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Core Phase (Stereographic)</span></span>
<span id="cb1-37">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># p(x) ~ 1/(1+x^2) -&gt; Cauchy(0,1)</span></span>
<span id="cb1-38">  x_last <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;">rcauchy</span>(N)</span>
<span id="cb1-39">  denom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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> x_last<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-40">  </span>
<span id="cb1-41">  y_out[,K<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> r <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> x_last<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> denom</span>
<span id="cb1-42">  y_out[,K]   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> r <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> x_last) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denom</span>
<span id="cb1-43">  </span>
<span id="cb1-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(y_out, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Method=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hyper-Tanh"</span>))</span>
<span id="cb1-45">}</span></code></pre></div></div>
</details>
</div>
<section id="experiment-a-uniformity-check" class="level3">
<h3 class="anchored" data-anchor-id="experiment-a-uniformity-check">Experiment A: Uniformity Check</h3>
<p>I generate 2,000 points on a 3D sphere (<img src="https://latex.codecogs.com/png.latex?S%5E2">) using both methods and verify that they cover the projected disk uniformly.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">42</span>)</span>
<span id="cb2-2">N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span></span>
<span id="cb2-3">df_muller <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;">generate_muller</span>(N)</span>
<span id="cb2-4">df_ht <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;">generate_hypertanh_uniform</span>(N)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(df_muller)[<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;">2</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y2"</span>)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(df_ht)[<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;">2</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y2"</span>)</span>
<span id="cb2-7"></span>
<span id="cb2-8">df_all <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;">rbind</span>(df_muller, df_ht)</span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># X-axis limits: sphere diameter (2) should be 80% of total width</span></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># So total width = 2 / 0.8 = 2.5, meaning xlim = c(-1.25, 1.25)</span></span>
<span id="cb2-12">xlims <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;">c</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;">1.25</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span>)</span>
<span id="cb2-13"></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Split data by method for individual plots</span></span>
<span id="cb2-15">df_ht_only <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_all[df_all<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Method <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hyper-Tanh"</span>, ]</span>
<span id="cb2-16">df_muller_only <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_all[df_all<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Method <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Muller (1959)"</span>, ]</span>
<span id="cb2-17"></span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use facet_grid for proper alignment</span></span>
<span id="cb2-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reshape data for combined plot</span></span>
<span id="cb2-20">df_scatter <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_all <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-21">  <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;">plot_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Isotropy"</span>)</span>
<span id="cb2-22"></span>
<span id="cb2-23">df_hist <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_all <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-24">  <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;">plot_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Marginal"</span>)</span>
<span id="cb2-25"></span>
<span id="cb2-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Scatter plot with facets</span></span>
<span id="cb2-27">p_scatter <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;">ggplot</span>(df_all, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>y1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y2)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"path"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</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;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi,<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;">y=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</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;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi,<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;">color=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey50"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span>Method), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Method, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</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>
<span id="cb2-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal, blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits=</span>xlims, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits=</span>xlims, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y1"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb2-37">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aspect.ratio=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb2-38">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.spacing =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>))</span>
<span id="cb2-39"></span>
<span id="cb2-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Histogram with facets</span></span>
<span id="cb2-41">p_hist <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;">ggplot</span>(df_all, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>y1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>Method)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">after_stat</span>(density)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Method, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</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>
<span id="cb2-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal, blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits=</span>xlims, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y1"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Density"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-48">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>,</span>
<span id="cb2-49">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-50">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.spacing =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lines"</span>))</span>
<span id="cb2-51"></span>
<span id="cb2-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Stack with patchwork</span></span>
<span id="cb2-53">p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p_hist <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;">plot_layout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">heights =</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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>))</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://spinkney.github.io/posts/post-3-better-unit-vector/a-better-unit-vector_files/figure-html/uniformity-plot-1.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
<p>Result: The Hyper-Tanh parameterization, when sampled with the correct Jacobian prior, is indistinguishable from Muller’s method. It is perfectly uniform.</p>
</section>
<section id="experiment-b-gradient-stability-the-y0-singularity" class="level3">
<h3 class="anchored" data-anchor-id="experiment-b-gradient-stability-the-y0-singularity">Experiment B: Gradient Stability (The “y=0” Singularity)</h3>
<p>Here I calculate the magnitude of the gradient <img src="https://latex.codecogs.com/png.latex?%5Cleft%5C%7C%20%5Cfrac%7Bd%5Cmathbf%7By%7D%7D%7Bdp%7D%20%5Cright%5C%7C"> as the parameter <img src="https://latex.codecogs.com/png.latex?p"> passes through the origin.</p>
<ol type="1">
<li>Muller: <img src="https://latex.codecogs.com/png.latex?p=z">. <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7By%7D%20=%20z/%7Cz%7C">. Gradient <img src="https://latex.codecogs.com/png.latex?%5Cpropto%201/%7Cz%7C">.</li>
<li>Hyper-Tanh: <img src="https://latex.codecogs.com/png.latex?p=x">. <img src="https://latex.codecogs.com/png.latex?y%20=%20%5Ctanh(x)">. Gradient <img src="https://latex.codecogs.com/png.latex?%5Cpropto%20%5Ctext%7Bsech%7D%5E2(x)">.</li>
</ol>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;"># Define parameter sequence crossing zero</span></span>
<span id="cb3-2">p_seq <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="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;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>)</span>
<span id="cb3-3">epsilon <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-9</span></span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Muller Gradient Norm approx (1/|p|)</span></span>
<span id="cb3-6">grad_muller <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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;">sqrt</span>(p_seq<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> epsilon)</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hyper-Tanh Gradient Norm approx (sech^2(p))</span></span>
<span id="cb3-9">grad_ht <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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;">cosh</span>(p_seq))<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="cb3-10"></span>
<span id="cb3-11">df_grad <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="cb3-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Parameter =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(p_seq, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb3-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Gradient =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(grad_muller, grad_ht),</span>
<span id="cb3-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Method =</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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Muller (z -&gt; z/|z|)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hyper-Tanh (x -&gt; y)"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>)</span>
<span id="cb3-15">)</span>
<span id="cb3-16"></span>
<span id="cb3-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(df_grad, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>Parameter, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Gradient, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span>Method)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal, blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gradient Stability at the Origin"</span>,</span>
<span id="cb3-23">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Muller explodes (Singularity). Hyper-Tanh is stable (Linear)."</span>,</span>
<span id="cb3-24">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gradient Magnitude (Log Scale)"</span>,</span>
<span id="cb3-25">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Parameter Value"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <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;">linetype=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://spinkney.github.io/posts/post-3-better-unit-vector/a-better-unit-vector_files/figure-html/gradient-plot-1.png" class="img-fluid figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
<p>Result: The Hyper-Tanh Peel eliminates the topological singularity. The sampler can pass through the origin without experiencing infinite forces.</p>
</section>
</section>
<section id="stan-implementation" class="level2">
<h2 class="anchored" data-anchor-id="stan-implementation">5. Stan Implementation</h2>
<p>Copy this block directly into your Stan program.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb4-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/**</span></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * Maps unconstrained R^(K-1) vector x to Unit Vector S^(K-1).</span></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   *</span></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param x Unconstrained vector of length K-1</span></span>
<span id="cb4-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @param K Dimension of the embedding space (output vector size)</span></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   * @return Unit vector of length K</span></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">   */</span></span>
<span id="cb4-9">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">hyper_tanh_to_unit_jacobian</span>(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> x, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K) {</span>
<span id="cb4-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] y;</span>
<span id="cb4-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> r <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.0</span>;</span>
<span id="cb4-12">    </span>
<span id="cb4-13">    <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="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>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="cb4-14">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> val <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x[i];</span>
<span id="cb4-15">      y[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tanh</span>(val);</span>
<span id="cb4-16">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> cosh_val <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cosh</span>(val);</span>
<span id="cb4-17">      r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv</span>(cosh_val);</span>
<span id="cb4-18">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> power <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> i; </span>
<span id="cb4-19">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>power <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(cosh_val);</span>
<span id="cb4-20">    }</span>
<span id="cb4-21"></span>
<span id="cb4-22">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> last_x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x[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;">1</span>];</span>
<span id="cb4-23">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">jacobian</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1p</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(last_x));</span>
<span id="cb4-24">  </span>
<span id="cb4-25">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> denom <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.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(last_x);</span>
<span id="cb4-26">    </span>
<span id="cb4-27">    y[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;">1</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r <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.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square</span>(last_x)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denom;</span>
<span id="cb4-28">    y[K] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> r <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;">2.0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> last_x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> denom;</span>
<span id="cb4-29">    </span>
<span id="cb4-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> y;</span>
<span id="cb4-31">  }</span>
<span id="cb4-32">}</span>
<span id="cb4-33"></span>
<span id="cb4-34"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb4-35">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> K;</span>
<span id="cb4-36">}</span>
<span id="cb4-37"></span>
<span id="cb4-38"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb4-39">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</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;">1</span>] x_raw; <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Unconstrained</span></span>
<span id="cb4-40">}</span>
<span id="cb4-41"></span>
<span id="cb4-42"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb4-43">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] mu <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;">hyper_tanh_to_unit_jacobian</span>(x_raw, K);</span>
<span id="cb4-44">}</span>
<span id="cb4-45"></span>
<span id="cb4-46"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb4-47">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// uniform on the sphere</span></span>
<span id="cb4-48">}</span></code></pre></div></div>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">6. Conclusion</h2>
<p>The hyper-tanh peel parameterization offers a robust alternative to <span class="citation" data-cites="muller1959">Muller (1959)</span>’s method for Bayesian modeling. It uses exactly <img src="https://latex.codecogs.com/png.latex?K-1"> parameters, avoids the singularity at the origin by mapping from the pole, and might offer some interesting priors on the pullback.</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-muller1959" class="csl-entry">
Muller, Mervin E. 1959. <span>“A Note on a Method for Generating Points Uniformly on n-Dimensional Spheres.”</span> <em>Commun. ACM</em> 2 (4): 19–20. <a href="https://doi.org/10.1145/377939.377946">https://doi.org/10.1145/377939.377946</a>.
</div>
</div></section></div> ]]></description>
  <category>stan</category>
  <category>parameterizations</category>
  <guid>https://spinkney.github.io/posts/post-3-better-unit-vector/a-better-unit-vector.html</guid>
  <pubDate>Thu, 15 Jan 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Quantile Regressions in Stan: Part III</title>
  <dc:creator>Sean Pinkney</dc:creator>
  <link>https://spinkney.github.io/posts/post-2-quantile-reg-series/post-2-quantile-reg-part-III/quantile-reg-part-III.html</link>
  <description><![CDATA[ 





<p>This is part III of the quantile regression series. <a href="../../../posts/post-2-quantile-reg-series/post-2-quantile-reg-part-I/quantile-reg.html">Part I</a> and <a href="../../../posts/post-2-quantile-reg-series/post-2-quantile-reg-part-II/quantile-reg-part-II.html">part II</a> show the Bayesian quantile regression using the asymmetric Laplace distribution, augmented scheme, and score. In this post I’m going to show the multiple quantile regression given in <span class="citation" data-cites="score">Wu and Narisetty (2021)</span>, which is a follow up to part II.</p>
<section id="multiple-quantile-regression-with-score" class="level1">
<h1>Multiple quantile regression with score</h1>
<p>The score method includes the regressors into the objective function as <img src="https://latex.codecogs.com/png.latex?%0As_%5Ctau(%5Cbeta)%20=%20%5Csum_%7Bi=1%7D%5E%7Bn%7D%20x_i%20%5Cpsi_%5Ctau(y_i%20-%20x_i%5E%7B%5Ctop%7D%5Cbeta).%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cpsi_%5Ctau(u)%20=%20%5Ctau%20-%20I(u%20%3C%200)">.</p>
<p><span class="citation" data-cites="score">Wu and Narisetty (2021)</span> consider the following “working” likelihood <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BL%7D(y%20%5Cmid%20x,%5C,%20%5Cbeta)%20=%20C%20%5Cexp%5Cbigg(-%5Cfrac%7B1%7D%7B2n%7D%20s_%5Ctau(%5Cbeta)%5E%7B%5Ctop%7D%20W%20s_%5Ctau(%5Cbeta)%20%20%5Cbigg),%0A"> where <img src="https://latex.codecogs.com/png.latex?W"> is a <img src="https://latex.codecogs.com/png.latex?p%20%5Ctimes%20p"> positive definite weight matrix, and C is a constant free of <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. The quadratic form and the given objective function implies that the quantile estimator is maximized at the typical quantile estimate.</p>
<p>The <img src="https://latex.codecogs.com/png.latex?W"> matrix for multiple quantiles is given as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AW%20=%20(Q%20%5Cotimes%20G%20)%5E%7B-1%7D,%20%5Ctext%7B%20where%20%7D%20Q%20=%20(%5Cmin(%5Ctau_i,%20%5Ctau_j)%20-%20%5Ctau_i%20%5Ctau_j)_%7Bij%7D,%20%5C;%20G%20=%20%5Cfrac%7B1%7D%7Bn%7D%5Csum_%7Bi=1%7D%5En%20x_i%20x_i%5E%7B%5Ctop%7D.%0A"></p>
<p>A nice property of <img src="https://latex.codecogs.com/png.latex?W"> is that it depends all on user input data so can be constructed outside of the MCMC iterations.</p>
<section id="score-function" class="level2">
<h2 class="anchored" data-anchor-id="score-function">Score function</h2>
<p>The following Stan model implements the multiple <img src="https://latex.codecogs.com/png.latex?%5Ctau"> code.</p>
<div class="cell" data-output.var="multiple_score_qr">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode stan fold-show code-with-copy"><code class="sourceCode stan"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span>{</span>
<span id="cb1-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> kronecker(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> A, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> B) {</span>
<span id="cb1-3">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(A) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(B), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols</span>(A) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols</span>(B)] C;</span>
<span id="cb1-4">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(A);</span>
<span id="cb1-5">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols</span>(A);</span>
<span id="cb1-6">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(B);</span>
<span id="cb1-7">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols</span>(B);</span>
<span id="cb1-8">    <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="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>m) {</span>
<span id="cb1-9">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>n) {</span>
<span id="cb1-10">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> row_start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (i <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> p <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="cb1-11">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> row_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (i <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> p <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p;</span>
<span id="cb1-12">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> col_start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (j <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> q <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="cb1-13">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> col_end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (j <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> q <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> q;</span>
<span id="cb1-14">        C[row_start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>row_end, col_start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>col_end] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> A[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> B;</span>
<span id="cb1-15">      }</span>
<span id="cb1-16">    }</span>
<span id="cb1-17">   <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> C;</span>
<span id="cb1-18">}</span>
<span id="cb1-19">  </span>
<span id="cb1-20">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> q_loss(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> u){</span>
<span id="cb1-21">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(u) <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> q <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> u);</span>
<span id="cb1-22"> }</span>
<span id="cb1-23">    </span>
<span id="cb1-24">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> score(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> q, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> y, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> x, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>) {</span>
<span id="cb1-25">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(y);</span>
<span id="cb1-26">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>[ , <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]);</span>
<span id="cb1-27">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(q);</span>
<span id="cb1-28">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K, P] out;</span>
<span id="cb1-29">      </span>
<span id="cb1-30">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (k <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>K) {</span>
<span id="cb1-31">      out[k] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> transpose(transpose(x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q_loss(q[k], y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">to_vector</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>[ ,k])));</span>
<span id="cb1-32">    }</span>
<span id="cb1-33">      </span>
<span id="cb1-34">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">to_vector</span>(out);</span>
<span id="cb1-35">  }</span>
<span id="cb1-36">    </span>
<span id="cb1-37">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> make_w (<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> x, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> q) {</span>
<span id="cb1-38">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(x);</span>
<span id="cb1-39">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols</span>(x);</span>
<span id="cb1-40">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(q);</span>
<span id="cb1-41">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> P, m <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> P] out;</span>
<span id="cb1-42">      </span>
<span id="cb1-43">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[m, m] Q;</span>
<span id="cb1-44">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[P, P] G <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crossprod</span>(x) ;</span>
<span id="cb1-45">    </span>
<span id="cb1-46">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//  G[1:P, 1] /= log(N);</span></span>
<span id="cb1-47">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//  G[1, 2:P] = transpose(G[2:P, 1]);</span></span>
<span id="cb1-48">      </span>
<span id="cb1-49">    <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="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>m) {</span>
<span id="cb1-50">      Q[i, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> q[i] <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> q[i]);</span>
<span id="cb1-51">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" 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;">1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>i <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="cb1-52">        Q[i, j] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>([q[i], q[j]]) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> q[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q[j];</span>
<span id="cb1-53">        Q[j, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Q[i, j];</span>
<span id="cb1-54">      }</span>
<span id="cb1-55">    }</span>
<span id="cb1-56">  </span>
<span id="cb1-57">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> kronecker(N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inverse</span>(G), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inverse</span>(Q));</span>
<span id="cb1-58">  }</span>
<span id="cb1-59">}</span>
<span id="cb1-60"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb1-61">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N;               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of observation</span></span>
<span id="cb1-62">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P;               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of predictors</span></span>
<span id="cb1-63">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> K;               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of quantiles</span></span>
<span id="cb1-64">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] q;</span>
<span id="cb1-65">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] y;         </span>
<span id="cb1-66">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[N, P] x;</span>
<span id="cb1-67">}</span>
<span id="cb1-68"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed data</span> {</span>
<span id="cb1-69">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> P, K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> P] W <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_w(x, q);</span>
<span id="cb1-70">}</span>
<span id="cb1-71"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb1-72">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// can add ordered constraint here</span></span>
<span id="cb1-73">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[P] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>;</span>
<span id="cb1-74">   <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> sigma;</span>
<span id="cb1-75">}</span>
<span id="cb1-76"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb1-77">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> P] score_vec <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> score(q, y, x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sigma;</span>
<span id="cb1-78">  </span>
<span id="cb1-79">  <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="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>K) </span>
<span id="cb1-80">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>[, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">4</span>);</span>
<span id="cb1-81">  </span>
<span id="cb1-82">  sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>);</span>
<span id="cb1-83"> </span>
<span id="cb1-84">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quad_form</span>(W, score_vec) <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> N <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> K <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(sigma);</span>
<span id="cb1-85"></span>
<span id="cb1-86">}</span></code></pre></div></div>
</details>
</div>
<p>I’ll simulate the same data as in part II:</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Show the code</summary>
<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;">12312</span>)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(quantreg)</span>
<span id="cb2-3">N     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb2-4">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;">runif</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb2-5">alpha <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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>
<span id="cb2-6">beta  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb2-7">y     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> beta <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> .<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> x)</span>
<span id="cb2-8">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;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>)</span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># frequentist estimate</span></span>
<span id="cb2-11">out_freq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> quantreg<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;">rq</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> q)</span>
<span id="cb2-12"></span>
<span id="cb2-13">out_score <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> multiple_score_qr<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>
<span id="cb2-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> N,</span>
<span id="cb2-15">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb2-16">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(q),</span>
<span id="cb2-17">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q =</span> q,</span>
<span id="cb2-18">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y,</span>
<span id="cb2-19">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</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;">alpha =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x))),</span>
<span id="cb2-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12123123</span>, </span>
<span id="cb2-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb2-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb2-23">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb2-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb2-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb2-26">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb2-27">)</span>
<span id="cb2-28"></span>
<span id="cb2-29">out_score<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;">summary</span>()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 8 × 10
  variable       mean    median     sd    mad       q5       q95  rhat ess_bulk
  &lt;chr&gt;         &lt;dbl&gt;     &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;
1 lp__      -3083.    -3083.    1.83   1.67   -3087.   -3081.     1.01     762.
2 beta[1,1]    -1.39     -1.38  0.266  0.279     -1.83    -0.973  1.00     870.
3 beta[2,1]     1.30      1.30  0.0417 0.0434     1.23     1.37   1.00     863.
4 beta[1,2]    -0.862    -0.861 0.122  0.123     -1.06    -0.660  1.01    1135.
5 beta[2,2]     1.97      1.97  0.0204 0.0202     1.94     2.00   1.00    1095.
6 beta[1,3]    -0.903    -0.934 0.220  0.213     -1.20    -0.502  1.00    1046.
7 beta[2,3]     2.83      2.83  0.0462 0.0450     2.75     2.90   1.00    1110.
8 sigma         1.69      1.69  0.0220 0.0205     1.66     1.73   1.00    1548.
# ℹ 1 more variable: ess_tail &lt;dbl&gt;</code></pre>
</div>
</div>
</section>
</section>
<section id="test-on-immunogg-data" class="level1">
<h1>Test on ImmunogG data</h1>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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;">library</span>(Brq)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ImmunogG"</span>)</span>
<span id="cb4-3">dat <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;">y =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>IgG, </span>
<span id="cb4-4">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb4-5">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Age, </span>
<span id="cb4-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xsq =</span> ImmunogG<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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb4-7"></span>
<span id="cb4-8">out_score_mod <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> multiple_score_qr<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>
<span id="cb4-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ImmunogG),</span>
<span id="cb4-10">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb4-11">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb4-12">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>),</span>
<span id="cb4-13">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>IgG,</span>
<span id="cb4-14">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(dat[, <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])),</span>
<span id="cb4-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12123123</span>, </span>
<span id="cb4-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb4-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_treedepth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>,</span>
<span id="cb4-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb4-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb4-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb4-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb4-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb4-23">)</span>
<span id="cb4-24"></span>
<span id="cb4-25">out_freq_1 <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;">rq</span>(y <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> xsq, </span>
<span id="cb4-26">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat,</span>
<span id="cb4-27">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)</span>
<span id="cb4-28">out_freq_2 <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;">rq</span>(y <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> xsq, </span>
<span id="cb4-29">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat,</span>
<span id="cb4-30">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb4-31">out_freq_3 <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;">rq</span>(y <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> xsq, </span>
<span id="cb4-32">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat,</span>
<span id="cb4-33">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>)</span></code></pre></div></div>
</details>
</div>
<section id="results" class="level2">
<h2 class="anchored" data-anchor-id="results">Results</h2>
<section id="modified-score" class="level3">
<h3 class="anchored" data-anchor-id="modified-score">Modified score:</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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">out_score_mod<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;">summary</span>()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 11 × 10
   variable       mean    median     sd    mad        q5      q95  rhat ess_bulk
   &lt;chr&gt;         &lt;dbl&gt;     &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;
 1 lp__      -605.     -605.     2.31   2.12   -609.     -6.02e+2  1.01     632.
 2 beta[1,1]    0.668     0.688  0.401  0.418    -0.0334  1.26e+0  1.00     683.
 3 beta[2,1]    1.19      1.19   0.346  0.329     0.623   1.76e+0  1.00     596.
 4 beta[3,1]   -0.137    -0.137  0.0580 0.0551   -0.235  -4.26e-2  1.00     648.
 5 beta[1,2]    2.72      2.70   0.318  0.324     2.23    3.26e+0  1.00     706.
 6 beta[2,2]    1.18      1.19   0.238  0.227     0.779   1.55e+0  1.01     690.
 7 beta[3,2]   -0.0732   -0.0760 0.0377 0.0361   -0.132  -8.66e-3  1.01     747.
 8 beta[1,3]    7.20      7.21   0.626  0.613     6.14    8.25e+0  1.00     630.
 9 beta[2,3]   -0.444    -0.461  0.503  0.478    -1.21    4.21e-1  1.01     597.
10 beta[3,3]    0.246     0.248  0.0806 0.0752    0.106   3.71e-1  1.00     611.
11 sigma        1.19      1.19   0.0267 0.0267    1.15    1.23e+0  1.00    1463.
# ℹ 1 more variable: ess_tail &lt;dbl&gt;</code></pre>
</div>
</div>
<p>Quantreg</p>
</section>
<section id="quantreg" class="level3">
<h3 class="anchored" data-anchor-id="quantreg">Quantreg:</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(out_freq_1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iid"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>
Call: rq(formula = y ~ x + xsq, tau = 0.05, data = dat)

tau: [1] 0.05

Coefficients:
            Value    Std. Error t value  Pr(&gt;|t|)
(Intercept)  0.76514  0.30477    2.51058  0.01259
x            1.19143  0.24792    4.80577  0.00000
xsq         -0.13371  0.04037   -3.31187  0.00104</code></pre>
</div>
<details class="code-fold">
<summary>Show the code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(out_freq_2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iid"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>
Call: rq(formula = y ~ x + xsq, tau = 0.5, data = dat)

tau: [1] 0.5

Coefficients:
            Value    Std. Error t value  Pr(&gt;|t|)
(Intercept)  2.80112  0.59354    4.71936  0.00000
x            1.15865  0.48282    2.39976  0.01703
xsq         -0.07523  0.07863   -0.95677  0.33947</code></pre>
</div>
<details class="code-fold">
<summary>Show the code</summary>
<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;">summary</span>(out_freq_3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iid"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>
Call: rq(formula = y ~ x + xsq, tau = 0.95, data = dat)

tau: [1] 0.95

Coefficients:
            Value    Std. Error t value  Pr(&gt;|t|)
(Intercept)  7.23563  0.67142   10.77659  0.00000
x           -0.48165  0.54617   -0.88186  0.37857
xsq          0.24602  0.08895    2.76597  0.00603</code></pre>
</div>
</div>



</section>
</section>
</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-score" class="csl-entry">
Wu, Teng, and Naveen N. Narisetty. 2021. <span>“<span class="nocase">Bayesian Multiple Quantile Regression for Linear Models Using a Score Likelihood</span>.”</span> <em>Bayesian Analysis</em> 16 (3): 875–903. <a href="https://doi.org/10.1214/20-BA1217">https://doi.org/10.1214/20-BA1217</a>.
</div>
</div></section></div> ]]></description>
  <category>stan</category>
  <category>quantile</category>
  <guid>https://spinkney.github.io/posts/post-2-quantile-reg-series/post-2-quantile-reg-part-III/quantile-reg-part-III.html</guid>
  <pubDate>Sun, 23 Oct 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Quantile Regressions in Stan: Part II</title>
  <dc:creator>Sean Pinkney</dc:creator>
  <link>https://spinkney.github.io/posts/post-2-quantile-reg-series/post-2-quantile-reg-part-II/quantile-reg-part-II.html</link>
  <description><![CDATA[ 





<p><a href="../../../posts/post-2-quantile-reg-series/post-2-quantile-reg-part-I/quantile-reg.html">Part I</a> showed the basic Bayesian quantile regression using the asymmetric Laplace distribution and augmented scheme. In this post I’m going to show an approximate likelihood, score method proposed in <span class="citation" data-cites="score">Wu and Narisetty (2021)</span>.</p>
<section id="first-why-not-use-the-asymmetric-laplace" class="level1">
<h1>First, why not use the asymmetric Laplace?</h1>
<p>The ALD (asymmetric Laplace distribution) is great. It’s fast and gives point results comparable to the frequentist estimator. However, <span class="citation" data-cites="yang2016">Yang et al. (2016)</span> showed that the asymptotic variance of the posterior distribution is “incorrect”. It does not correspond to the frequentist interval. A reason is that the ALD and pinball loss function, <img src="https://latex.codecogs.com/png.latex?%5Crho_%5Ctau(u)%20=%20%5Cfrac%7B%7Cu%7C%20+%20(2%5Ctau%20-%201)u%7D%7B2%7D">, is maximized at the standard frequentist quantile but does not correspond to a true data generating likelihood. In the aforementioned paper, they propose a correction to the interval after sampling. I might show this in another post but post-processing is annoying. The score method asymptotically approaches the correct estimates with the correct interval.</p>
</section>
<section id="quantile-regression-with-score" class="level1">
<h1>Quantile regression with score</h1>
<p>The score method includes the regressors into the objective function as <img src="https://latex.codecogs.com/png.latex?%0As_%5Ctau(%5Cbeta)%20=%20%5Csum_%7Bi=1%7D%5E%7Bn%7D%20x_i%20%5Cpsi_%5Ctau(y_i%20-%20x_i%5E%7B%5Ctop%7D%5Cbeta).%0A"> where <img src="https://latex.codecogs.com/png.latex?%5Cpsi_%5Ctau(u)%20=%20%5Ctau%20-%20I(u%20%3C%200)">.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Warning! It seems that <img src="https://latex.codecogs.com/png.latex?%5Cpsi"> doesn’t work for me! What does work is using <img src="https://latex.codecogs.com/png.latex?%5Crho_%5Ctau%20=%20%5Cfrac%7B%7Cu%7C%20+%20(2%5Ctau%20-%201)u%7D%7B2%7D"> as before. They mention that the loss is the same but then use new notation indicating that it’s different. Anyway, this may be what they intended.</p>
</div>
</div>
<p><span class="citation" data-cites="score">Wu and Narisetty (2021)</span> consider the following “working” likelihood <img src="https://latex.codecogs.com/png.latex?%0A%5Cmathcal%7BL%7D(y%20%5Cmid%20x,%5C,%20%5Cbeta)%20=%20C%20%5Cexp%5Cbigg(-%5Cfrac%7B1%7D%7B2n%7D%20s_%5Ctau(%5Cbeta)%5E%7B%5Ctop%7D%20W%20s_%5Ctau(%5Cbeta)%20%20%5Cbigg),%0A"> where <img src="https://latex.codecogs.com/png.latex?W"> is a <img src="https://latex.codecogs.com/png.latex?p%20%5Ctimes%20p"> positive definite weight matrix, and C is a constant free of <img src="https://latex.codecogs.com/png.latex?%5Cbeta">. The quadratic form and the given objective function implies that the quantile estimator is maximized at the typical quantile estimate.</p>
<p>They then choose a weight matrix which gives the correct posterior covariance as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AW%20=%20%5Cfrac%7Bn%7D%7B%5Ctau%20(1%20-%20%5Ctau)%7D%5Cbigg(%5Csum_%7Bi=1%7D%5En%20x_i%20x_i%5E%7B%5Ctop%7D%20%5Cbigg).%0A"> In fact, they also show how this can incorporate multiple <img src="https://latex.codecogs.com/png.latex?%5Ctau">’s (I’ll show this in part III) which allows more flexible modeling options, such as a squared exponential kernel that shares information across <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> quantile estimates which are near each other. The <img src="https://latex.codecogs.com/png.latex?W"> matrix for multiple quantiles is given as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AW%20=%20(Q%20%5Cotimes%20G%20)%5E%7B-1%7D,%20%5Ctext%7B%20where%20%7D%20Q%20=%20(%5Cmin(%5Ctau_i,%20%5Ctau_j)%20-%20%5Ctau_i%20%5Ctau_j)_%7Bij%7D,%20%5C;%20G%20=%20%5Cfrac%7B1%7D%7Bn%7D%5Csum_%7Bi=1%7D%5En%20x_i%20x_i%5E%7B%5Ctop%7D.%0A"> A nice property of <img src="https://latex.codecogs.com/png.latex?W"> is that it depends all on user input data so can be constructed outside of the MCMC iterations.</p>
<section id="score-function" class="level2">
<h2 class="anchored" data-anchor-id="score-function">Score function</h2>
<p>The following Stan model implements the single <img src="https://latex.codecogs.com/png.latex?%5Ctau"> code. I’ll simulate some data and compare to the asymmetric Laplace:</p>
<div class="cell" data-output.var="score_qr">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode stan fold-show code-with-copy"><code class="sourceCode stan"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span>{</span>
<span id="cb1-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> q_loss(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> u){</span>
<span id="cb1-3">    <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(u) <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> q <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> u);</span>
<span id="cb1-4">  }</span>
<span id="cb1-5">    </span>
<span id="cb1-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> score(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> y, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> x, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>) {</span>
<span id="cb1-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> transpose(x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q_loss(q, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> );</span>
<span id="cb1-8">  }</span>
<span id="cb1-9">    </span>
<span id="cb1-10">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> make_w (<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> x, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q) {</span>
<span id="cb1-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(x);</span>
<span id="cb1-12">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols</span>(x);</span>
<span id="cb1-13">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> alpha <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> (q <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> q));</span>
<span id="cb1-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[P, P] x_out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inverse_spd</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crossprod</span>(x));</span>
<span id="cb1-15"></span>
<span id="cb1-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x_out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N;</span>
<span id="cb1-17">  }</span>
<span id="cb1-18">}</span>
<span id="cb1-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb1-20">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of observation</span></span>
<span id="cb1-21">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of predictors</span></span>
<span id="cb1-22">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q;</span>
<span id="cb1-23">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] y;           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Response variable sorted</span></span>
<span id="cb1-24">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[N, P] x;</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;">transformed data</span> {</span>
<span id="cb1-27">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[P, P] W <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_w(x, q);</span>
<span id="cb1-28">}</span>
<span id="cb1-29"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb1-30">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>;</span>
<span id="cb1-31">}</span>
<span id="cb1-32"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb1-33">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] score_vec <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> score(q, y, x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>);</span>
<span id="cb1-34"></span>
<span id="cb1-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">4</span>);</span>
<span id="cb1-36"></span>
<span id="cb1-37">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quad_form</span>(W, score_vec) <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> N);</span>
<span id="cb1-38">}</span></code></pre></div></div>
</details>
</div>
<p>Let’s simulate some data and see how well it performs.</p>
<div class="cell">
<details open="" class="code-fold">
<summary>Show the code</summary>
<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;">12312</span>)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(quantreg)</span>
<span id="cb2-3">N     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb2-4">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;">runif</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb2-5">alpha <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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>
<span id="cb2-6">beta  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb2-7">y     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> beta <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> .<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> x)</span>
<span id="cb2-8">q     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span></span>
<span id="cb2-9"></span>
<span id="cb2-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># frequentist estimate</span></span>
<span id="cb2-11">out_freq <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> quantreg<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;">rq</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> q)</span>
<span id="cb2-12"></span>
<span id="cb2-13">out_score <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> score_qr<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>
<span id="cb2-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> N,</span>
<span id="cb2-15">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb2-16">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q =</span> q,</span>
<span id="cb2-17">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y,</span>
<span id="cb2-18">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</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;">alpha =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x))),</span>
<span id="cb2-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12123123</span>, </span>
<span id="cb2-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb2-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb2-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb2-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb2-24">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb2-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb2-26">)</span>
<span id="cb2-27"></span>
<span id="cb2-28">out_asym_laplace <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> asym_laplace<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>
<span id="cb2-29">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> N,</span>
<span id="cb2-30">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb2-31">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q =</span> q,</span>
<span id="cb2-32">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y,</span>
<span id="cb2-33">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</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;">alpha =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x))),</span>
<span id="cb2-34">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12123123</span>, </span>
<span id="cb2-35">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb2-36">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb2-37">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb2-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb2-39">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb2-40">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb2-41">)</span></code></pre></div></div>
</details>
</div>
<section id="score" class="level3">
<h3 class="anchored" data-anchor-id="score">Score</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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">out_score<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;">summary</span>()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 10
  variable    mean  median     sd    mad      q5     q95  rhat ess_bulk ess_tail
  &lt;chr&gt;      &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;
1 lp__     -832.   -831.   1.00   0.671  -834.   -831.    1.01     452.     568.
2 beta[1]    -1.41   -1.41 0.171  0.181    -1.68   -1.12  1.02     393.     486.
3 beta[2]     1.30    1.30 0.0271 0.0288    1.25    1.34  1.02     384.     468.</code></pre>
</div>
</div>
</section>
<section id="asymmetric-laplace" class="level3">
<h3 class="anchored" data-anchor-id="asymmetric-laplace">Asymmetric Laplace</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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">out_asym_laplace<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;">summary</span>(<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;">"beta"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span>))</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 10
  variable   mean median      sd     mad     q5    q95  rhat ess_bulk ess_tail
  &lt;chr&gt;     &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;
1 beta[1]  -1.08  -1.07  0.0855  0.0882  -1.23  -0.961  1.00     747.     793.
2 beta[2]   1.24   1.24  0.0187  0.0193   1.22   1.28   1.00     751.     945.
3 sigma     0.248  0.248 0.00789 0.00797  0.236  0.261  1.00    1087.    1009.</code></pre>
</div>
</div>
</section>
<section id="quantreg" class="level3">
<h3 class="anchored" data-anchor-id="quantreg">Quantreg</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(out_freq, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"boot"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>
Call: quantreg::rq(formula = y ~ x, tau = q)

tau: [1] 0.05

Coefficients:
            Value    Std. Error t value  Pr(&gt;|t|)
(Intercept) -1.03935  0.12013   -8.65201  0.00000
x            1.23539  0.03651   33.84091  0.00000</code></pre>
</div>
</div>
</section>
<section id="modified-score" class="level3">
<h3 class="anchored" data-anchor-id="modified-score">Modified Score</h3>
<p>We see that the frequentist estimate from <code>quantreg</code> and the asymmetric Laplace outputs are similar. The score method under-estimates the intercept where the credible interval doesn’t even include -1 and the coefficient for <img src="https://latex.codecogs.com/png.latex?x"> seems to be over-estimated. Noticing this behavior, let’s see about adding <code>sigma</code> the standard deviation to the estimation.</p>
<p>Let’s re-estimate the above with the modification.</p>
<div class="cell" data-output.var="score_qr_modified">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode stan foldable code-with-copy"><code class="sourceCode stan"><span id="cb9-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span>{</span>
<span id="cb9-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> q_loss(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> u){</span>
<span id="cb9-3">    <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(u) <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> q <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> u);</span>
<span id="cb9-4">  }</span>
<span id="cb9-5">    </span>
<span id="cb9-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> score(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> y, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> x, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>) {</span>
<span id="cb9-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> transpose(x) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> q_loss(q, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>);</span>
<span id="cb9-8">  }</span>
<span id="cb9-9"></span>
<span id="cb9-10">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> make_w (<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span> x, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q) {</span>
<span id="cb9-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rows</span>(x);</span>
<span id="cb9-12">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols</span>(x);</span>
<span id="cb9-13">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> alpha <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> (q <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> q));</span>
<span id="cb9-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[P, P] x_out <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crossprod</span>(x);</span>
<span id="cb9-15"></span>
<span id="cb9-16">   <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inverse_spd</span>(x_out) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> N;</span>
<span id="cb9-17"></span>
<span id="cb9-18">  }</span>
<span id="cb9-19">}</span>
<span id="cb9-20"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb9-21">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of observation</span></span>
<span id="cb9-22">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P;                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of predictors</span></span>
<span id="cb9-23">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q;</span>
<span id="cb9-24">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] y;           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Response variable sorted</span></span>
<span id="cb9-25">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[N, P] x;</span>
<span id="cb9-26">}</span>
<span id="cb9-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed data</span> {</span>
<span id="cb9-28">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[P, P] W <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> make_w(x, q);</span>
<span id="cb9-29">}</span>
<span id="cb9-30"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb9-31">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>;</span>
<span id="cb9-32">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> sigma;</span>
<span id="cb9-33">}</span>
<span id="cb9-34"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb9-35">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] score_vec <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> score( q, y, x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> ) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sigma ;</span>
<span id="cb9-36"></span>
<span id="cb9-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">4</span>);</span>
<span id="cb9-38"></span>
<span id="cb9-39">  <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">target</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quad_form</span>(W, score_vec) <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> N) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(sigma);</span>
<span id="cb9-40">}</span></code></pre></div></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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">out_score_modified <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> score_qr_modified<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>
<span id="cb10-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> N,</span>
<span id="cb10-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb10-4">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q =</span> q,</span>
<span id="cb10-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y,</span>
<span id="cb10-6">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</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;">alpha =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x))),</span>
<span id="cb10-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12123123</span>, </span>
<span id="cb10-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb10-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb10-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb10-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb10-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb10-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb10-14">)</span>
<span id="cb10-15">out_score_modified<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;">summary</span>() </span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 10
  variable    mean  median     sd    mad      q5     q95  rhat ess_bulk ess_tail
  &lt;chr&gt;      &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;
1 lp__     -755.   -755.   1.22   1.01   -757.   -754.    1.00     751.     946.
2 beta[1]    -1.41   -1.40 0.206  0.200    -1.76   -1.06  1.00     544.     841.
3 beta[2]     1.30    1.30 0.0322 0.0319    1.25    1.35  1.00     551.     838.
4 sigma       1.29    1.29 0.0293 0.0293    1.24    1.34  1.00    1007.    1080.</code></pre>
</div>
</div>
<p>The estimates are basically the same but the standard errors increased. We’ll see that the increased in standard errors makes more reasonable posterior intervals.</p>
</section>
<section id="simulated-data-with-n-10000" class="level3">
<h3 class="anchored" data-anchor-id="simulated-data-with-n-10000">Simulated data with N = 10000</h3>
</section>
<section id="score-1" class="level3">
<h3 class="anchored" data-anchor-id="score-1">Score</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">out_score<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;">summary</span>()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 10
  variable      mean    median      sd     mad        q5      q95  rhat ess_bulk
  &lt;chr&gt;        &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;
1 lp__     -9621.    -9620.    0.938   0.760   -9623.    -9.62e+3  1.00     852.
2 beta[1]     -0.913    -0.911 0.0339  0.0330     -0.972 -8.60e-1  1.01     545.
3 beta[2]      1.15      1.15  0.00633 0.00615     1.14   1.16e+0  1.01     617.
# ℹ 1 more variable: ess_tail &lt;dbl&gt;</code></pre>
</div>
</div>
</section>
<section id="modified-score-1" class="level3">
<h3 class="anchored" data-anchor-id="modified-score-1">Modified Score</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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">out_score_modified<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;">summary</span>()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 10
  variable      mean    median      sd     mad       q5       q95  rhat ess_bulk
  &lt;chr&gt;        &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;
1 lp__     -8273.    -8273.    1.21    1.01    -8275.   -8272.     1.00     949.
2 beta[1]     -0.920    -0.916 0.0511  0.0504     -1.01    -0.844  1.00     900.
3 beta[2]      1.15      1.15  0.00932 0.00900     1.14     1.17   1.00     934.
4 sigma        1.39      1.39  0.00990 0.00985     1.37     1.40   1.00    1162.
# ℹ 1 more variable: ess_tail &lt;dbl&gt;</code></pre>
</div>
</div>
</section>
<section id="quantreg-1" class="level3">
<h3 class="anchored" data-anchor-id="quantreg-1">Quantreg</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(out_freq, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iid"</span>))</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>
Call: quantreg::rq(formula = y ~ x, tau = q)

tau: [1] 0.05

Coefficients:
            Value     Std. Error t value   Pr(&gt;|t|) 
(Intercept)  -0.99120   0.02167  -45.74656   0.00000
x             1.16937   0.00374  312.48513   0.00000</code></pre>
</div>
</div>
</section>
<section id="asymmetric-laplace-1" class="level3">
<h3 class="anchored" data-anchor-id="asymmetric-laplace-1">Asymmetric Laplace</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">out_asym_laplace<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;">summary</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"beta"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 10
  variable   mean median      sd     mad    q5    q95  rhat ess_bulk ess_tail
  &lt;chr&gt;     &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;
1 beta[1]  -0.997 -0.995 0.0152  0.0140  -1.02 -0.976  1.00    1193.     898.
2 beta[2]   1.17   1.17  0.00605 0.00605  1.16  1.18   1.00    1577.    1471.</code></pre>
</div>
</div>
<p>It does appear to be better. Let’s see how this does on the ImmunogG data.</p>
</section>
</section>
</section>
<section id="test-on-immunogg-data" class="level1">
<h1>Test on ImmunogG data</h1>
<p>We’ll use the same quantile of <img src="https://latex.codecogs.com/png.latex?5%5C%25"> to test.</p>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(Brq)</span>
<span id="cb20-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ImmunogG"</span>)</span>
<span id="cb20-3">q <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span></span>
<span id="cb20-4">dat <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;">y =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>IgG, </span>
<span id="cb20-5">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb20-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Age, </span>
<span id="cb20-7">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xsq =</span> ImmunogG<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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb20-8"></span>
<span id="cb20-9">out_score <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> score_qr<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>
<span id="cb20-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ImmunogG),</span>
<span id="cb20-11">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb20-12">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q =</span> q,</span>
<span id="cb20-13">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>IgG,</span>
<span id="cb20-14">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(dat[, <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])),</span>
<span id="cb20-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12123123</span>, </span>
<span id="cb20-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb20-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb20-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb20-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb20-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb20-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb20-22">)</span>
<span id="cb20-23"></span>
<span id="cb20-24">out_score_mod <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> score_qr_modified<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>
<span id="cb20-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ImmunogG),</span>
<span id="cb20-26">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb20-27">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q =</span> q,</span>
<span id="cb20-28">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>IgG,</span>
<span id="cb20-29">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(dat[, <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])),</span>
<span id="cb20-30">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12123123</span>, </span>
<span id="cb20-31">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb20-32">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb20-33">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb20-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb20-35">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb20-36">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb20-37">)</span>
<span id="cb20-38"></span>
<span id="cb20-39">out_al <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> asym_laplace<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>
<span id="cb20-40">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ImmunogG),</span>
<span id="cb20-41">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb20-42">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q =</span> q,</span>
<span id="cb20-43">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>IgG,</span>
<span id="cb20-44">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(dat[, <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])),</span>
<span id="cb20-45">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12123123</span>, </span>
<span id="cb20-46">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb20-47">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb20-48">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb20-49">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb20-50">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb20-51">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb20-52">)</span>
<span id="cb20-53"></span>
<span id="cb20-54">out_freq <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;">rq</span>(y <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> xsq, </span>
<span id="cb20-55">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dat,</span>
<span id="cb20-56">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> q)</span></code></pre></div></div>
</details>
</div>
<section id="results" class="level3">
<h3 class="anchored" data-anchor-id="results">Results</h3>
</section>
<section id="orginal-score" class="level3">
<h3 class="anchored" data-anchor-id="orginal-score">Orginal score:</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">out_score<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;">summary</span>()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 10
  variable    mean  median     sd    mad      q5      q95  rhat ess_bulk
  &lt;chr&gt;      &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt;    &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;
1 lp__     -89.5   -89.2   1.28   1.15   -91.8   -87.9     1.00     555.
2 beta[1]    0.678   0.708 0.317  0.341    0.121   1.15    1.01     262.
3 beta[2]    1.20    1.20  0.266  0.267    0.749   1.63    1.01     268.
4 beta[3]   -0.136  -0.136 0.0433 0.0429  -0.206  -0.0662  1.01     287.
# ℹ 1 more variable: ess_tail &lt;dbl&gt;</code></pre>
</div>
</div>
</section>
<section id="modified-score-2" class="level3">
<h3 class="anchored" data-anchor-id="modified-score-2">Modified score:</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">out_score_mod<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;">summary</span>()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 10
  variable    mean  median     sd    mad      q5      q95  rhat ess_bulk
  &lt;chr&gt;      &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt;    &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;
1 lp__     -72.6   -72.2   1.47   1.32   -75.4   -70.8     1.01     557.
2 beta[1]    0.676   0.686 0.239  0.259    0.260   1.03    1.01     511.
3 beta[2]    1.21    1.22  0.184  0.188    0.921   1.53    1.01     500.
4 beta[3]   -0.138  -0.138 0.0291 0.0293  -0.186  -0.0920  1.00     546.
5 sigma      0.773   0.771 0.0321 0.0322   0.723   0.825   1.01     738.
# ℹ 1 more variable: ess_tail &lt;dbl&gt;</code></pre>
</div>
</div>
</section>
<section id="quantreg-2" class="level3">
<h3 class="anchored" data-anchor-id="quantreg-2">Quantreg:</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(out_freq, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"iid"</span>)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>
Call: rq(formula = y ~ x + xsq, tau = q, data = dat)

tau: [1] 0.05

Coefficients:
            Value    Std. Error t value  Pr(&gt;|t|)
(Intercept)  0.76514  0.30477    2.51058  0.01259
x            1.19143  0.24792    4.80577  0.00000
xsq         -0.13371  0.04037   -3.31187  0.00104</code></pre>
</div>
</div>
</section>
<section id="asymmetric-laplace-2" class="level3">
<h3 class="anchored" data-anchor-id="asymmetric-laplace-2">Asymmetric Laplace:</h3>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">out_al<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;">summary</span>()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 10
  variable     mean   median     sd    mad       q5       q95  rhat ess_bulk
  &lt;chr&gt;       &lt;dbl&gt;    &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;
1 lp__     -671.    -670.    1.54   1.28   -673.    -669.      1.00     606.
2 beta[1]     0.599    0.617 0.255  0.278     0.152    0.985   1.00     380.
3 beta[2]     1.31     1.30  0.217  0.207     0.970    1.67    1.01     360.
4 beta[3]    -0.155   -0.153 0.0369 0.0329   -0.217   -0.0993  1.01     391.
5 sigma       0.165    0.164 0.0100 0.0100    0.150    0.182   1.01     738.
# ℹ 1 more variable: ess_tail &lt;dbl&gt;</code></pre>
</div>
</div>
<p>Interestingly, the results of the score method lie between the frequentist and asymmetric Laplace methods.</p>
</section>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>The score method is promising but it doesn’t seem like one would use the asymmetric Laplace due to the ad-hoc nature of the intercept. One thing the score method has is incorporating multipled quantiles in a principled way that allows you to put priors on the estimates that correspond to linking them. It also doesn’t need to re-run the data through each quantile. In the next post, part III, I’ll show this. If you have a bunch of different quantiles, lots of data, and want to understand the correlations across parameters then the score method may be right for you.</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-score" class="csl-entry">
Wu, Teng, and Naveen N. Narisetty. 2021. <span>“<span class="nocase">Bayesian Multiple Quantile Regression for Linear Models Using a Score Likelihood</span>.”</span> <em>Bayesian Analysis</em> 16 (3): 875–903. <a href="https://doi.org/10.1214/20-BA1217">https://doi.org/10.1214/20-BA1217</a>.
</div>
<div id="ref-yang2016" class="csl-entry">
Yang, Yunwen, Huixia Judy Wang, and Xuming He. 2016. <span>“Posterior Inference in Bayesian Quantile Regression with Asymmetric Laplace Likelihood.”</span> <em>International Statistical Review</em> 84 (3): 327–44. https://doi.org/<a href="https://doi.org/10.1111/insr.12114">https://doi.org/10.1111/insr.12114</a>.
</div>
</div></section></div> ]]></description>
  <category>stan</category>
  <category>quantile</category>
  <guid>https://spinkney.github.io/posts/post-2-quantile-reg-series/post-2-quantile-reg-part-II/quantile-reg-part-II.html</guid>
  <pubDate>Tue, 18 Oct 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Quantile Regressions in Stan: Part I</title>
  <dc:creator>Sean Pinkney</dc:creator>
  <link>https://spinkney.github.io/posts/post-2-quantile-reg-series/post-2-quantile-reg-part-I/quantile-reg.html</link>
  <description><![CDATA[ 





<p>Quantile regressions are relatively new in the Bayesian literature first appearing in <span class="citation" data-cites="yu">Yu and Moyeed (2001)</span> who described an asymmetric Laplace distribution to estimate the conditional quantiles. This post is going to show the asymmetric Laplace and the augmented version. In part II I will show the score likelihood method.</p>
<p>The asymmetric Laplace distribution is given as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Coperatorname%7BALD%7D(Y%20%5Cmid%20x,%20%5Cbeta)%20=%20%5Cfrac%7B%5Ctau%5En%20(1%20-%20%5Ctau)%5En%7D%7B%5Csigma%5En%7D%5Cexp%20%5Cbigg(%20-%5Cfrac%7B%5Csum_%7Bi=1%7D%5En%20%5Crho_%5Ctau%20(y_i%20-%20x_i%5E%7B%5Ctop%7D%5Cbeta)%7D%7B%5Csigma%7D%20%5Cbigg)%0A"></p>
<p>where</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Crho_%5Ctau(u)%20=%20%5Cfrac%7B%7Cu%7C%20+%20(2%5Ctau%20-%201)u%7D%7B2%7D%0A"></p>
<p>We may write this in Stan as</p>
<div class="cell" data-output.var="asymetric_laplace">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb1-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span>{</span>
<span id="cb1-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q_loss(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> u){</span>
<span id="cb1-3">  <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(u) <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> q <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> u);</span>
<span id="cb1-4">}</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> ald_lpdf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> y, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> sigma, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> q_est){</span>
<span id="cb1-7">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(y);</span>
<span id="cb1-8">  </span>
<span id="cb1-9">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(q) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(q) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(sigma)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> q_loss(q, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> q_est) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sigma;</span>
<span id="cb1-10">}</span>
<span id="cb1-11">}</span>
<span id="cb1-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb1-13">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N;                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of observation</span></span>
<span id="cb1-14">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P;                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of predictors</span></span>
<span id="cb1-15">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</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;">&gt;</span> q;</span>
<span id="cb1-16">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] y;             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Response variable sorted</span></span>
<span id="cb1-17">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[N, P] x;</span>
<span id="cb1-18">}</span>
<span id="cb1-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb1-20">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>;</span>
<span id="cb1-21">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> sigma;</span>
<span id="cb1-22">}</span>
<span id="cb1-23"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb1-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">4</span>);</span>
<span id="cb1-25">  sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>);</span>
<span id="cb1-26">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span>ald(q, sigma, x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>);</span>
<span id="cb1-27">}</span></code></pre></div></div>
</details>
</div>
<p>Let’s compare the 5th percentile to the <code>Brq</code> R package</p>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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;">library</span>(Brq)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ImmunogG"</span>)</span>
<span id="cb2-3">out_asym_laplace <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> asymetric_laplace<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>
<span id="cb2-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ImmunogG),</span>
<span id="cb2-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb2-6">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>,</span>
<span id="cb2-7">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>IgG,</span>
<span id="cb2-8">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</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;">alpha =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Age, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xsq =</span> ImmunogG<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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))),</span>
<span id="cb2-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12123123</span>,</span>
<span id="cb2-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb2-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb2-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb2-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb2-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb2-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb2-16">)</span>
<span id="cb2-17">out_asym_laplace<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;">summary</span>()</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 10
  variable     mean   median     sd    mad       q5       q95  rhat ess_bulk
  &lt;chr&gt;       &lt;dbl&gt;    &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;
1 lp__     -671.    -670.    1.54   1.28   -673.    -669.      1.00     606.
2 beta[1]     0.599    0.617 0.255  0.278     0.152    0.985   1.00     380.
3 beta[2]     1.31     1.30  0.217  0.207     0.970    1.67    1.01     360.
4 beta[3]    -0.155   -0.153 0.0369 0.0329   -0.217   -0.0993  1.01     391.
5 sigma       0.165    0.164 0.0100 0.0100    0.150    0.182   1.01     738.
# ℹ 1 more variable: ess_tail &lt;dbl&gt;</code></pre>
</div>
<details class="code-fold">
<summary>Show the code</summary>
<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">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>IgG</span>
<span id="cb4-2">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Age</span>
<span id="cb4-3">X<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(x, 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>
<span id="cb4-4">fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Brq</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> X , <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau=</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;">runs=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">burn=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb4-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit))</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Call:
Brq.formula(formula = y ~ X, tau = 0.05, runs = 2000, burn = 1000)

tau:[1] 0.05

            Estimate L.CredIntv  U.CredIntv
Intercept  0.6201064  0.1113146  1.03475824
Xx         1.2991424  0.8962901  1.68272470
X         -0.1550870 -0.2317421 -0.08612944</code></pre>
</div>
</div>
<section id="using-the-covariates-for-extra-information" class="level3">
<h3 class="anchored" data-anchor-id="using-the-covariates-for-extra-information">Using the covariates for extra information</h3>
<p>In <span class="citation" data-cites="Lee2022">Lee et al. (2022)</span> they detail a Bayesian approach to the envelope quantile regression. In all honesty, I briefly skimmed the paper and gathered that by adding a regression on the covariates and using this information in the asymmetric Laplace can help identification. This isn’t a recreation of their method but a quick detour to see if a simplified version of what they do increases ESS.</p>
<p>The idea is that we use the variability in the covariance of the regressors to help identify the coefficients on the ALD regression. I now have to separate out the intercept into <img src="https://latex.codecogs.com/png.latex?%5Calpha">.</p>
<div class="cell" data-output.var="asymetric_laplace_envelope">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span>{</span>
<span id="cb6-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q_loss(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> u){</span>
<span id="cb6-3">  <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(u) <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> q <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> u);</span>
<span id="cb6-4">}</span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> ald_lpdf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> y, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> q, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> sigma, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> q_est){</span>
<span id="cb6-7">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(y);</span>
<span id="cb6-8">  </span>
<span id="cb6-9">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(q) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(q) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(sigma)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> q_loss(q, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> q_est) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> sigma;</span>
<span id="cb6-10">}</span>
<span id="cb6-11">}</span>
<span id="cb6-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb6-13">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N;                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of observation</span></span>
<span id="cb6-14">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P;                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of predictors</span></span>
<span id="cb6-15">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</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;">&gt;</span> q;</span>
<span id="cb6-16">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] y;             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Response variable sorted</span></span>
<span id="cb6-17">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[N, P] x;</span>
<span id="cb6-18">}</span>
<span id="cb6-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed data</span> {</span>
<span id="cb6-20">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">array</span>[N] <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] X;</span>
<span id="cb6-21">  </span>
<span id="cb6-22">  <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="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>P) {</span>
<span id="cb6-23">    X[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>, i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">to_array_1d</span>(x[<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>, i]);</span>
<span id="cb6-24">  }</span>
<span id="cb6-25">}</span>
<span id="cb6-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb6-27">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] eta;</span>
<span id="cb6-28">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> sigma;</span>
<span id="cb6-29">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] mu;</span>
<span id="cb6-30">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">cholesky_factor_corr</span>[P] L;</span>
<span id="cb6-31">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>[P] <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gamma</span>;</span>
<span id="cb6-32">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> alpha;</span>
<span id="cb6-33">}</span>
<span id="cb6-34"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed parameters</span> {</span>
<span id="cb6-35">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> eta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gamma</span>;</span>
<span id="cb6-36">}</span>
<span id="cb6-37"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb6-38">  eta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">4</span>);</span>
<span id="cb6-39">  sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>);</span>
<span id="cb6-40">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span>ald(q, sigma, alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>);</span>
<span id="cb6-41">  alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">4</span>);</span>
<span id="cb6-42">  </span>
<span id="cb6-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gamma</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>);</span>
<span id="cb6-44">  mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">4</span>);</span>
<span id="cb6-45"></span>
<span id="cb6-46">  X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">multi_normal_cholesky</span>(mu, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag_pre_multiply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gamma</span>, L));</span>
<span id="cb6-47">}</span></code></pre></div></div>
</details>
</div>
<p>Let’s fit the model.</p>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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">out_envelope <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> asymetric_laplace_envelope<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>
<span id="cb7-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ImmunogG),</span>
<span id="cb7-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb7-4">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>,</span>
<span id="cb7-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>IgG,</span>
<span id="cb7-6">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</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> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Age, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xsq =</span> ImmunogG<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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))),</span>
<span id="cb7-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12123123</span>,</span>
<span id="cb7-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb7-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb7-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb7-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb7-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb7-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb7-14">)</span>
<span id="cb7-15"></span>
<span id="cb7-16">out_envelope<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;">summary</span>(<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;">"alpha"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"beta"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span>))</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 10
  variable   mean median      sd     mad     q5    q95  rhat ess_bulk ess_tail
  &lt;chr&gt;     &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;
1 alpha     0.614  0.637 0.244   0.252    0.204  0.981  1.00     913.    1012.
2 beta[1]   1.29   1.29  0.203   0.201    0.982  1.62   1.00     863.     966.
3 beta[2]  -0.152 -0.151 0.0345  0.0329  -0.213 -0.102  1.00     965.     946.
4 sigma     0.166  0.165 0.00976 0.00983  0.151  0.182  1.00    1211.    1075.</code></pre>
</div>
</div>
<p>Slightly different estimates, higher ESS, slower to fit, but lower standard deviations! Neat.</p>
</section>
<section id="scale-mixture-representation" class="level2">
<h2 class="anchored" data-anchor-id="scale-mixture-representation">Scale-mixture representation</h2>
<p>The above may also be written as a mixture of exponential and normal distributions. Letting</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0Az_i%20&amp;%5Csim%20%5Coperatorname%7BExp%7D(1)%20%5C%5C%0A%5Csigma%20&amp;%5Csim%20%5Coperatorname%7BExp%7D(1)%20%5C%5C%0Ay_i%20&amp;%5Csim%20%5Cmathcal%7BN%7D(x_i%5E%7B%5Ctop%7D%5Cbeta_p%20+%20%5Csigma%20%5Ctheta%20z_i,%5C,%20%5Ctau%20%5Csigma%20%5Csqrt%7Bz_i%7D)%0A%5Cend%7Baligned%7D%0A"> where</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctheta%20&amp;=%20%5Cfrac%7B1%20-%202%20q%7D%7Bq%20(1%20-%20q)%7D%20%5C%5C%0A%5Ctau%20&amp;=%20%5Csqrt%7B%5Cfrac%7B2%7D%7Bq(1%20-q)%7D%7D%0A%5Cend%7Baligned%7D%0A"></p>
<p>The following Stan model implements the augmented approach.</p>
<div class="cell" data-output.var="augmented_laplace">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb9-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb9-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N;                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of observation</span></span>
<span id="cb9-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P;                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of predictors</span></span>
<span id="cb9-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</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;">&gt;</span> q;</span>
<span id="cb9-5">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] y;             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Response variable sorted</span></span>
<span id="cb9-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[N, P] x;</span>
<span id="cb9-7">}</span>
<span id="cb9-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed data</span> {</span>
<span id="cb9-9">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta <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> <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> q) <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="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> q));</span>
<span id="cb9-10">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> (q <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> q)));</span>
<span id="cb9-11">}</span>
<span id="cb9-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb9-13">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>;</span>
<span id="cb9-14">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>[N] z;</span>
<span id="cb9-15">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> sigma;</span>
<span id="cb9-16">}</span>
<span id="cb9-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb9-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">2</span>);</span>
<span id="cb9-19">  sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>);</span>
<span id="cb9-20">  </span>
<span id="cb9-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Data Augmentation</span></span>
<span id="cb9-22">  z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>);</span>
<span id="cb9-23">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</span>(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z, tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(z) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sigma);</span>
<span id="cb9-24">}</span></code></pre></div></div>
</details>
</div>
<p>Let’s fit the model</p>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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">out_aug_laplace <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> augmented_laplace<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>
<span id="cb10-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ImmunogG),</span>
<span id="cb10-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb10-4">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>,</span>
<span id="cb10-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>IgG,</span>
<span id="cb10-6">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</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;">alpha =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Age, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xsq =</span> ImmunogG<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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))),</span>
<span id="cb10-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12123123</span>,</span>
<span id="cb10-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb10-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb10-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb10-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb10-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb10-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb10-14">)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: 57 of 2000 (3.0%) transitions ended with a divergence.
See https://mc-stan.org/misc/warnings for details.</code></pre>
</div>
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">out_aug_laplace<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;">summary</span>(<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;">"beta"</span>))</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 10
  variable   mean median     sd    mad     q5     q95  rhat ess_bulk ess_tail
  &lt;chr&gt;     &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;
1 beta[1]   0.606  0.619 0.243  0.260   0.193  0.990   1.01     303.     547.
2 beta[2]   1.30   1.30  0.206  0.198   0.968  1.62    1.01     335.     492.
3 beta[3]  -0.154 -0.154 0.0350 0.0325 -0.211 -0.0979  1.01     390.     669.</code></pre>
</div>
</div>
<p>Ouch! Divergences. Although the parameter estimates look comparable. Let’s inspect the pairs plots between the augmented version and the asymmetric Laplace.</p>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(bayesplot)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>This is bayesplot version 1.15.0</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>- Online documentation and vignettes at mc-stan.org/bayesplot</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>- bayesplot theme set to bayesplot::theme_default()</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>   * Does _not_ affect other ggplot2 plots</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>   * See ?bayesplot_theme_set for details on theme setting</code></pre>
</div>
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../../R/theme_blog.R"</span>)</span>
<span id="cb20-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_blog_bayesplot</span>()</span>
<span id="cb20-3">pairs_np_style <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> bayesplot<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;">pairs_style_np</span>(</span>
<span id="cb20-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">div_color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>red,</span>
<span id="cb20-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">td_color =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>gold</span>
<span id="cb20-6">)</span>
<span id="cb20-7"></span>
<span id="cb20-8">np_aug_laplace <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;">nuts_params</span>(out_aug_laplace)</span>
<span id="cb20-9">np_asym_laplace <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;">nuts_params</span>(out_asym_laplace)</span>
<span id="cb20-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mcmc_pairs</span>(out_aug_laplace<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;">draws</span>(<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;">"beta"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span>)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">np =</span> np_aug_laplace, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">np_style =</span> pairs_np_style)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://spinkney.github.io/posts/post-2-quantile-reg-series/post-2-quantile-reg-part-I/quantile-reg_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mcmc_pairs</span>(out_asym_laplace<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;">draws</span>(<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;">"beta"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sigma"</span>)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">np =</span> np_asym_laplace, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">np_style =</span> pairs_np_style)</span></code></pre></div></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://spinkney.github.io/posts/post-2-quantile-reg-series/post-2-quantile-reg-part-I/quantile-reg_files/figure-html/unnamed-chunk-8-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The pairs plots don’t appear to show the divergence originating be due to <code>beta</code> or <code>sigma</code>. It’s most likely the augmentation variables <code>z</code>. As I’m up for time on this, I’d be curious to test out alternative specifications or see if anyone has encountered these issues with the augmented version and found a solution.</p>
<section id="try-student-t" class="level3">
<h3 class="anchored" data-anchor-id="try-student-t">Try Student T</h3>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>The Student T won’t result in the same posterior and is likely not ALD. However, it approaches a normal distribution with increasing <img src="https://latex.codecogs.com/png.latex?%5Cnu">.</p>
</div>
</div>
<p>Let’s use the <code>student_t</code> with <img src="https://latex.codecogs.com/png.latex?%5Cnu%20=%2030"></p>
<div class="cell" data-output.var="student_augmented_laplace">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb22-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb22-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N;                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of observation</span></span>
<span id="cb22-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> P;                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Number of predictors</span></span>
<span id="cb22-4">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</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;">&gt;</span> q;</span>
<span id="cb22-5">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] y;             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Response variable sorted</span></span>
<span id="cb22-6">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">matrix</span>[N, P] x;</span>
<span id="cb22-7">}</span>
<span id="cb22-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">transformed data</span> {</span>
<span id="cb22-9">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta <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> <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> q) <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="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> q));</span>
<span id="cb22-10">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> (q <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> q)));</span>
<span id="cb22-11">}</span>
<span id="cb22-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb22-13">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[P] <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>;</span>
<span id="cb22-14">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>[N] z;</span>
<span id="cb22-15">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> sigma;</span>
<span id="cb22-16">}</span>
<span id="cb22-17"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb22-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">2</span>);</span>
<span id="cb22-19">  sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>);</span>
<span id="cb22-20">  </span>
<span id="cb22-21">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">// Data Augmentation</span></span>
<span id="cb22-22">  z <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exponential</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>);</span>
<span id="cb22-23">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">student_t</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> z, tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(z) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sigma);</span>
<span id="cb22-24">}</span></code></pre></div></div>
</details>
</div>
<p>Let’s fit the same model</p>
<div class="cell">
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">out_student_aug_laplace <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> student_augmented_laplace<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>
<span id="cb23-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(ImmunogG),</span>
<span id="cb23-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">P =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb23-4">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>,</span>
<span id="cb23-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>IgG,</span>
<span id="cb23-6">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</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;">alpha =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> ImmunogG<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Age, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xsq =</span> ImmunogG<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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))),</span>
<span id="cb23-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12123123</span>,</span>
<span id="cb23-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb23-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_warmup =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb23-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter_sampling =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>,</span>
<span id="cb23-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb23-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb23-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb23-14">)</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: 1 of 2000 (0.0%) transitions ended with a divergence.
See https://mc-stan.org/misc/warnings for details.</code></pre>
</div>
<details class="code-fold">
<summary>Show the code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">out_student_aug_laplace<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;">summary</span>(<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;">"beta"</span>))</span></code></pre></div></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 10
  variable   mean median     sd    mad     q5    q95  rhat ess_bulk ess_tail
  &lt;chr&gt;     &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;
1 beta[1]   0.604  0.647 0.229  0.214   0.178  0.927  1.01     358.     462.
2 beta[2]   1.34   1.33  0.164  0.153   1.10   1.63   1.01     371.     733.
3 beta[3]  -0.156 -0.154 0.0267 0.0252 -0.202 -0.116  1.01     388.     715.</code></pre>
</div>
</div>
<p>Great no divergences!</p>
</section>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>For the next post, we’ll take a look at an approximate likelihood using the data which asymptotically approaches the true likelihood with more data.</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-Lee2022" class="csl-entry">
Lee, Minji, Saptarshi Chakraborty, and Zhihua Su. 2022. <span>“A Bayesian Approach to Envelope Quantile Regression.”</span> <em>Statistica Sinica</em>, ahead of print, January. <a href="https://doi.org/10.5705/ss.202020.0109">https://doi.org/10.5705/ss.202020.0109</a>.
</div>
<div id="ref-yu" class="csl-entry">
Yu, Keming, and Rana A. Moyeed. 2001. <span>“Bayesian Quantile Regression.”</span> <em>Statistics &amp; Probability Letters</em> 54 (4): 437–47. https://doi.org/<a href="https://doi.org/10.1016/S0167-7152(01)00124-9">https://doi.org/10.1016/S0167-7152(01)00124-9</a>.
</div>
</div></section></div> ]]></description>
  <category>stan</category>
  <category>quantile</category>
  <guid>https://spinkney.github.io/posts/post-2-quantile-reg-series/post-2-quantile-reg-part-I/quantile-reg.html</guid>
  <pubDate>Sat, 15 Oct 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Vasicek Quantile Regression in Stan</title>
  <dc:creator>Sean Pinkney</dc:creator>
  <link>https://spinkney.github.io/posts/post-1-vasciek/vasciek_post.html</link>
  <description><![CDATA[ 





<p>In this post I’ll show a way to do quantile regression on a bounded unit interval. The Vasicek distribution is an alternative distribution for beta regressions and is described in <span class="citation" data-cites="vasicek">Mazucheli et al. (2022)</span>. The Vasicek distribution is given as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%20%20%5Cbegin%7Baligned%7D%0Af(y%20%5Cmid%20%5Calpha,%20%5Ctheta)%20&amp;=%20%5Csqrt%7B%5Cfrac%7B1%20-%20%5Ctheta%7D%7B%5Ctheta%7D%7D%20%5Cexp%20%5Cbigg%5C%7B%5Cfrac%7B1%7D%7B2%7D%20%5Cbigg%5B%5CPhi%5E%7B-1%7D(y)%5E2%20-%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Cbigg(%5Cfrac%7B%5CPhi%5E%7B-1%7D(y)%20%5Csqrt%7B1%20-%20%5Ctheta%7D%20-%20%5CPhi%5E%7B-1%7D(%5Calpha)%7D%7B%5Csqrt%7B%5Ctheta%7D%7D%20%5Cbigg)%5E2%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5Cbigg%5C%7D%20%5C%5C%0AF(y%20%5Cmid%20%5Calpha,%20%5Ctheta)%20&amp;=%20%5CPhi%20%5Cbigg(%20%5Cfrac%7B%5CPhi%5E%7B-1%7D(y)%20%5Csqrt%7B1%20-%20%5Ctheta%7D%20-%20%5CPhi%5E%7B-1%7D(%5Calpha)%7D%7B%5Csqrt%7B%5Ctheta%7D%7D%20%5Cbigg)%20%5C%5C%0AQ(%5Ctau%20%5Cmid%20%5Calpha,%20%5Ctheta)%20&amp;=%20F%5E%7B-1%7D(%5Ctau%20%5Cmid%20%5Calpha,%20%5Ctheta)%20=%20%5CPhi%20%5Cbigg(%20%5Cfrac%7B%5CPhi%5E%7B-1%7D(%5Calpha)%20+%20%5CPhi%5E%7B-1%7D(%5Ctau)%20%5Csqrt%7B%5Ctheta%7D%7D%7B%5Csqrt%7B1%20-%20%5Ctheta%7D%7D%20%5Cbigg)%0A%5Cend%7Baligned%7D%0A"> where <img src="https://latex.codecogs.com/png.latex?0%20%3C%20(y,%20%5Calpha,%20%5Ctheta,%20%5Ctau)%20%3C%201"> and <img src="https://latex.codecogs.com/png.latex?%5CPhi"> and <img src="https://latex.codecogs.com/png.latex?%5CPhi%5E%7B-1%7D"> are the standard normal CDF and QF respectively. The mean of the distribution is <img src="https://latex.codecogs.com/png.latex?%5Coperatorname%7BE%7D(Y)%20=%20%5Calpha"> and <img src="https://latex.codecogs.com/png.latex?%5Coperatorname%7BVar%7D(Y)%20=%20%5CPhi_2(%5CPhi%5E%7B-1%7D(%5Calpha),%20%5CPhi%5E%7B-1%7D(%5Calpha),%20%5Ctheta)"> where <img src="https://latex.codecogs.com/png.latex?%5CPhi_2"> is the bivariate standard normal CDF.</p>
<p>In the paper they reparameterize the mean in terms of the <img src="https://latex.codecogs.com/png.latex?%5Ctau">-th quantile as <img src="https://latex.codecogs.com/png.latex?%0A%5Calpha%20=%20h%5E%7B-1%7D(%5Cmu)%20=%20%5CPhi(%5CPhi%5E%7B-1%7D(%5Cmu)%5Csqrt%7B1%20-%20%5Ctheta%7D%20-%20%5CPhi%5E%7B-1%7D(%5Ctau)%5Csqrt%7B%5Ctheta%7D).%0A"> Given this reparmeterization the log-likelihood is <img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cell(%20%5Cmu,%20%5Ctheta%20%5Cmid%20y,%20%5Ctau)%20=%20%5C;&amp;%20%5Cfrac%7Bn%7D%7B2%7D%5Clog%5Cbigg(%5Cfrac%7B1%20-%20%5Ctheta%7D%7B%5Ctheta%7D%20%5Cbigg)%20-%20%5Cfrac%7B%5CPhi%5E%7B-1%7D(y)%5E%5Ctop%20%5CPhi%5E%7B-1%7D(y)%7D%7B2%7D%20-%20%5C%5C%0A&amp;%20%5Cfrac%7B1%7D%7B2%5Ctheta%7D%20%5Csum_%7Bi=1%7D%5E%7Bn%7D%20%5Cbigg(%5Csqrt%7B1%20-%20%5Ctheta%7D%5Cbigg%5B%5CPhi%5E%7B-1%7D(y_i)%20-%20%5CPhi%5E%7B-1%7D(%5Cmu)%20%5Cbigg%5D%20+%20%5Csqrt%7B%5Ctheta%7D%5CPhi%5E%7B-1%7D(%5Ctau)%20%5Cbigg)%5E2%0A%5Cend%7Baligned%7D%0A"> The log-likelihood may be written in Stan as:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb1-1">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> vasicek_quantile_lpdf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> y, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> mu, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> tau) {</span>
<span id="cb1-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (tau <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) </span>
<span id="cb1-3">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tau must be between 0 and 1 found tau = "</span>, tau);</span>
<span id="cb1-4">    </span>
<span id="cb1-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (theta <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) </span>
<span id="cb1-6">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theta must be between 0 and 1 found theta = "</span>, theta);</span>
<span id="cb1-7">    </span>
<span id="cb1-8">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(y);</span>
<span id="cb1-9">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lpdf <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> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(theta) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(theta));</span>
<span id="cb1-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] qnorm_mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_Phi</span>(mu);</span>
<span id="cb1-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> qnorm_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_Phi</span>(tau);</span>
<span id="cb1-12">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] qnorm_y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_Phi</span>(y);</span>
<span id="cb1-13">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] qnorm_alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(theta) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> qnorm_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-14">                              qnorm_mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> theta);</span>
<span id="cb1-15">    </span>
<span id="cb1-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> lpdf <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_self</span>(qnorm_y) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span></span>
<span id="cb1-17">            <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>( (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> theta) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> qnorm_y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> qnorm_alpha)<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> theta);</span>
<span id="cb1-18">  }</span></code></pre></div></div>
<p>The authors provided an R package <code>vasicekreg</code> where we can check if it results in the same log-likelihood,</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;">library</span>(cmdstanr)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(vasicekreg)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"expose_cmdstanr_funs.R"</span>)</span>
<span id="cb2-4">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;">rVASIQ</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.50</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.69</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.50</span>)</span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expose_cmdstanr_functions</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vasicek.stan"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expose_to_global_env =</span> T)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in readLines(temp_cpp_file): incomplete final line found on
'/var/folders/dm/p14hjty57hndjp4f4n_y1dz00000gn/T//RtmpX7Wede/model-305333dd1948.cpp'</code></pre>
</div>
<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">stan_lpdf <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;">vasicek_quantile_lpdf</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.69</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb4-2">r_lpdf <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;">dVASIQ</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.69</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log =</span> T)</span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all.equal</span>(stan_lpdf, r_lpdf)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
</div>
<p>To expose the Stan function to my R environment I have used the <code>expose_cmdstanr_funs.R</code> <a href="https://github.com/rok-cesnovar/misc/blob/master/expose-cmdstanr-functions/expose_cmdstanr_functions.R">script from Rok Češnovar’s repo</a>.</p>
<p>We can test this out aginst the R package fit. The following Stan code performs the regression.</p>
<p>Now, let’s see how the package and Stan compare.</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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb6-2">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb6-3">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;">rbinom</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb6-4">eta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</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> x;</span>
<span id="cb6-5">mu <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</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> <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>eta));</span>
<span id="cb6-6">sigma <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>;</span>
<span id="cb6-7">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;">rVASIQ</span>(n, mu, sigma, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb6-8">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;">data.frame</span>(y, x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb6-9">tau <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>;</span>
<span id="cb6-10">fit_gamlss <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gamlss<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;">gamlss</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> VASIQ)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>GAMLSS-RS iteration 1: Global Deviance = -55.4043 
GAMLSS-RS iteration 2: Global Deviance = -55.4042 </code></pre>
</div>
<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;">summary</span>(fit_gamlss)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>******************************************************************
Family:  c("VASIQ", "VasicekQ") 

Call:  gamlss::gamlss(formula = y ~ x, family = VASIQ, data = data) 

Fitting method: RS() 

------------------------------------------------------------------
Mu link function:  logit
Mu Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)   
(Intercept)   0.6554     0.1936   3.385  0.00103 **
x             0.9284     0.2975   3.120  0.00238 **
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

------------------------------------------------------------------
Sigma link function:  logit
Sigma Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)  
(Intercept)  -0.2929     0.1414  -2.071    0.041 *
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

------------------------------------------------------------------
No. of observations in the fit:  100 
Degrees of Freedom for the fit:  3
      Residual Deg. of Freedom:  97 
                      at cycle:  2 
 
Global Deviance:     -55.40424 
            AIC:     -49.40424 
            SBC:     -41.58873 
******************************************************************</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">mod <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;">cmdstan_model</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vasicek.stan"</span>)</span>
<span id="cb10-2">fit_stan <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mod<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>
<span id="cb10-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> n,</span>
<span id="cb10-4">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y,</span>
<span id="cb10-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x,</span>
<span id="cb10-6">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb10-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb10-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb10-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb10-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb10-11">)</span>
<span id="cb10-12">fit_stan<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;">summary</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 10
  variable   mean median    sd   mad     q5     q95  rhat ess_bulk ess_tail
  &lt;chr&gt;     &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;
1 lp__     26.1   26.5   1.26  1.04  23.7   27.5    1.00     1813.    2268.
2 alpha     0.667  0.668 0.197 0.196  0.341  0.987  1.000    2420.    2396.
3 beta      0.931  0.925 0.302 0.303  0.440  1.43   1.00     2487.    2601.
4 sigma    -0.260 -0.263 0.143 0.147 -0.490 -0.0119 1.00     2495.    2712.</code></pre>
</div>
</div>
<p>Similar results!</p>
<p>We can update the code to run multiple quantiles which is the same as running the model for each quantile. In Stan, we can run the same model multiple times or we do the loop, as I show below, inside the program and pass a vector of <img src="https://latex.codecogs.com/png.latex?%5Ctau">’s in as data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">fittaus <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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</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.50</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.90</span>), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(Tau)</span>
<span id="cb12-2"> {</span>
<span id="cb12-3">  tau <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;&lt;-</span> Tau; </span>
<span id="cb12-4">  gamlss<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;">gamlss</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> VASIQ)</span>
<span id="cb12-5"> })</span>
<span id="cb12-6"></span>
<span id="cb12-7">gamlss_mtau <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>(fittaus, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(x)[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb12-8"></span>
<span id="cb12-9"></span>
<span id="cb12-10">mod_vector <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;">cmdstan_model</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vasciek_vector_tau.stan"</span>)</span>
<span id="cb12-11">fit_stan_vector <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mod_vector<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>
<span id="cb12-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> n,</span>
<span id="cb12-13">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y,</span>
<span id="cb12-14">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x,</span>
<span id="cb12-15">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">K =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb12-16">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tau =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</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.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>)),</span>
<span id="cb12-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">refresh =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb12-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_messages =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb12-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_exceptions =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb12-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel_chains =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb12-21">)</span></code></pre></div></div>
</div>
<p>The parameter results</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"><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;">stan =</span> fit_stan_vector<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;">summary</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="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;">2</span>], </span>
<span id="cb13-2">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gamlss_estimates =</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(gamlss_mtau)))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   stan.variable  stan.mean gamlss_estimates
1       alpha[1] -1.1831888       -1.1439249
2       alpha[2] -0.2989731       -0.2798790
3       alpha[3]  0.6617247        0.6553778
4       alpha[4]  1.6824475        1.6508456
5       alpha[5]  2.7219139        2.6707318
6        beta[1]  0.8916370        0.8953157
7        beta[2]  0.8772167        0.8746433
8        beta[3]  0.9313420        0.9284406
9        beta[4]  1.0622834        1.0609846
10       beta[5]  1.2615722        1.2410915
11      sigma[1] -0.2576663       -0.2913405
12      sigma[2] -0.2609139       -0.2927525
13      sigma[3] -0.2599794       -0.2928563
14      sigma[4] -0.2593018       -0.2927494
15      sigma[5] -0.2580766       -0.2914501</code></pre>
</div>
</div>
<p><a href="https://twitter.com/avehtari/status/1580841358713323521">Aki Veharti</a> correctly noted that the increase in the number of parameters will reduce the step size and increase the number of steps. If you have lots of data then it may make more sense to run each <img src="https://latex.codecogs.com/png.latex?%5Ctau"> independently.</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">individual_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fit_stan<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;">sampler_diagnostics</span>()[,,<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;">"stepsize__"</span>, </span>
<span id="cb15-2">                                                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_leapfrog__"</span>)]</span>
<span id="cb15-3">vector_fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fit_stan_vector<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;">sampler_diagnostics</span>()[,,<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;">"stepsize__"</span>,</span>
<span id="cb15-4">                                                        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_leapfrog__"</span>)]</span>
<span id="cb15-5"></span>
<span id="cb15-6"><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;">stepsize_avg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(individual_fit[, , <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]),</span>
<span id="cb15-7">                                  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(vector_fit[, , <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>
<span id="cb15-8">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_leapfrog_avg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(individual_fit[, , <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]), </span>
<span id="cb15-9">                                    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(vector_fit[, , <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="cb15-10">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <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;">"individual_fit"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vector_fit"</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>               stepsize_avg n_leapfrog_avg
individual_fit         0.55           5.91
vector_fit             0.36          13.29</code></pre>
</div>
</div>
<p>We see that the sampler must take smaller steps and increase the number of leapfrog integrations.</p>
<p>Lastly, we can plot the effects. Here are the intercepts and beta coefficients</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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(posterior)</span>
<span id="cb17-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidybayes)</span>
<span id="cb17-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb17-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"../../R/theme_blog.R"</span>)</span>
<span id="cb17-5"></span>
<span id="cb17-6">fit_stan_vector <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spread_draws</span>(alpha[K]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> alpha))  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabh</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>teal) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Posterior distribution of alpha by quantile"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://spinkney.github.io/posts/post-1-vasciek/vasciek_post_files/figure-html/unnamed-chunk-6-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="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">fit_stan_vector <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">spread_draws</span>(beta[K]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> K, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> beta))  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_slabh</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> blog_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>purple) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Posterior distribution of beta by quantile"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://spinkney.github.io/posts/post-1-vasciek/vasciek_post_files/figure-html/unnamed-chunk-6-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>In my next post, <a href="../../posts/post-2-quantile-reg-series/post-2-quantile-reg-part-I/quantile-reg.html">quantile regression post</a> I’ll show various ways to represent a quantile regression in Stan where the outcome, however, is continuous on the real line.</p>
<section id="appendix" class="level2">
<h2 class="anchored" data-anchor-id="appendix">Appendix</h2>
<p>Single tau</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb19-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb19-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> vasicek_quantile_lpdf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> y, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> mu, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> tau) {</span>
<span id="cb19-3">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(y);</span>
<span id="cb19-4">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lpdf <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> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(theta) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(theta));</span>
<span id="cb19-5">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] qnorm_mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_Phi</span>(mu);</span>
<span id="cb19-6">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> qnorm_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_Phi</span>(tau);</span>
<span id="cb19-7">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] qnorm_y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_Phi</span>(y);</span>
<span id="cb19-8">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] qnorm_alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(theta) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> qnorm_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb19-9">                              qnorm_mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> theta);</span>
<span id="cb19-10">    </span>
<span id="cb19-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> lpdf <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_self</span>(qnorm_y) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> </span>
<span id="cb19-12">            <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>( (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> theta) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> qnorm_y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> qnorm_alpha)<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> theta);</span>
<span id="cb19-13">  }</span>
<span id="cb19-14">}</span>
<span id="cb19-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb19-16">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> N;</span>
<span id="cb19-17">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] y;</span>
<span id="cb19-18">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] x;</span>
<span id="cb19-19">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</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;">&gt;</span> tau;</span>
<span id="cb19-20">} </span>
<span id="cb19-21"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb19-22">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> alpha;</span>
<span id="cb19-23">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>;</span>
<span id="cb19-24">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> sigma;</span>
<span id="cb19-25">} </span>
<span id="cb19-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb19-27">  alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">4</span>);</span>
<span id="cb19-28">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">4</span>);</span>
<span id="cb19-29">  sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">2</span>);</span>
<span id="cb19-30">  y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span>vasicek_quantile(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_logit</span>(sigma), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_logit</span>(alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x), tau);</span>
<span id="cb19-31">}</span></code></pre></div></div>
<p>Multiple tau</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode stan code-with-copy"><code class="sourceCode stan"><span id="cb20-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">functions</span> {</span>
<span id="cb20-2">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> vasicek_quantile_lpdf(<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> y, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> theta, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span> mu, <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> tau) {</span>
<span id="cb20-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (tau <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) </span>
<span id="cb20-4">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tau must be between 0 and 1 found tau = "</span>, tau);</span>
<span id="cb20-5">    </span>
<span id="cb20-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (theta <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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">||</span> theta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) </span>
<span id="cb20-7">      <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">reject</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theta must be between 0 and 1 found theta = "</span>, theta);</span>
<span id="cb20-8">    </span>
<span id="cb20-9">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">num_elements</span>(y);</span>
<span id="cb20-10">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> lpdf <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> N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log1m</span>(theta) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(theta));</span>
<span id="cb20-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] qnorm_mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_Phi</span>(mu);</span>
<span id="cb20-12">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">real</span> qnorm_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_Phi</span>(tau);</span>
<span id="cb20-13">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] qnorm_y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_Phi</span>(y);</span>
<span id="cb20-14">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] qnorm_alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(theta) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> qnorm_tau <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>  </span>
<span id="cb20-15">                              qnorm_mu <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> theta);</span>
<span id="cb20-16">    </span>
<span id="cb20-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> lpdf <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb20-18">            <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dot_self</span>(qnorm_y) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> </span>
<span id="cb20-19">            <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>( (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</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> theta) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> qnorm_y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> qnorm_alpha)<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> theta);</span>
<span id="cb20-20">  }</span>
<span id="cb20-21">}</span>
<span id="cb20-22"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">data</span> {</span>
<span id="cb20-23">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> N;</span>
<span id="cb20-24">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] y;</span>
<span id="cb20-25">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[N] x;</span>
<span id="cb20-26">  </span>
<span id="cb20-27">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">&gt;</span> K;</span>
<span id="cb20-28">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lower</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;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">upper</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;">&gt;</span>[K] tau;</span>
<span id="cb20-29">} </span>
<span id="cb20-30"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">parameters</span> {</span>
<span id="cb20-31">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] alpha;</span>
<span id="cb20-32">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>;</span>
<span id="cb20-33">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">vector</span>[K] sigma;</span>
<span id="cb20-34">} </span>
<span id="cb20-35"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">model</span> {</span>
<span id="cb20-36">  alpha <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">4</span>);</span>
<span id="cb20-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">4</span>);</span>
<span id="cb20-38">  sigma <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normal</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;">2</span>);</span>
<span id="cb20-39">  </span>
<span id="cb20-40">  <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="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>K)</span>
<span id="cb20-41">    y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~ </span>vasicek_quantile(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_logit</span>(sigma[i]), </span>
<span id="cb20-42">                         <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inv_logit</span>(alpha[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">beta</span>[i] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> x), tau[i]);</span>
<span id="cb20-43">}</span></code></pre></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-vasicek" class="csl-entry">
Mazucheli, Josmar, Bruna Alves, Mustafa Ç. Korkmaz, and Víctor Leiva. 2022. <span>“Vasicek Quantile and Mean Regression Models for Bounded Data: New Formulation, Mathematical Derivations, and Numerical Applications.”</span> <em>Mathematics</em> 10 (9). <a href="https://doi.org/10.3390/math10091389">https://doi.org/10.3390/math10091389</a>.
</div>
</div></section></div> ]]></description>
  <category>stan</category>
  <category>quantile</category>
  <guid>https://spinkney.github.io/posts/post-1-vasciek/vasciek_post.html</guid>
  <pubDate>Fri, 14 Oct 2022 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
