<?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>Angelo Maria Sabatini</title>
<link>https://amsabatini.netlify.app/blog.html</link>
<atom:link href="https://amsabatini.netlify.app/blog.xml" rel="self" type="application/rss+xml"/>
<description>The personal blog of Angelo Maria Sabatini</description>
<generator>quarto-1.6.40</generator>
<lastBuildDate>Tue, 02 Jun 2026 22:00:00 GMT</lastBuildDate>
<item>
  <title>Is the sample mean a Kalman filter in disguise?</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/post_1/</link>
  <description><![CDATA[ 




<blockquote class="blockquote">
<p>💾 <strong>Prefer reading offline?</strong> You can <a href="post_pdf.pdf">download a PDF version of this post</a> for printing or future reference.</p>
</blockquote>
<section id="about-the-sample-mean" class="level2">
<h2 class="anchored" data-anchor-id="about-the-sample-mean">About the sample mean</h2>
<p>The experimental activity considered in this post consists of acquiring a dataset of <img src="https://latex.codecogs.com/png.latex?n"> measurements of some quantity at discrete instants of time that, without loss of generality, can be assumed equally spaced by an interval <img src="https://latex.codecogs.com/png.latex?%5CDelta%20T">.</p>
<p>The measurand, whose physical nature is immaterial for the present discussion, is taken to be constant and equal to <img src="https://latex.codecogs.com/png.latex?%5Cmu">. Observations are affected by additive noise modeled as a random variable <img src="https://latex.codecogs.com/png.latex?V"> with zero mean and constant variance <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2">.</p>
<p>The resulting sequence can therefore be written as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax_k=%5Cmu+v_k,%5Cqquad%20k=1,%5Cdots,n,%0A"></p>
<p>where the explicit time dependence has been omitted for the sake of clarity.</p>
<p>If measurement errors are furthermore assumed to be independent, a basic probabilistic model consists of <img src="https://latex.codecogs.com/png.latex?n"> independent identically distributed (IID) random variables <img src="https://latex.codecogs.com/png.latex?%5C%7BX_k%5C%7D_%7Bk=1%7D%5En">, whose realizations are <img src="https://latex.codecogs.com/png.latex?%5C%7Bx_k%5C%7D_%7Bk=1%7D%5En">. No assumption has yet been made about the probability distribution of <img src="https://latex.codecogs.com/png.latex?V">.</p>
<p>A central result of estimation theory is that the unknown quantity <img src="https://latex.codecogs.com/png.latex?%5Cmu"> can be estimated using the <em>sample mean</em>, defined as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbar%20X_n=%5Cfrac%7B1%7D%7Bn%7D%5Csum_%7Bk=1%7D%5En%20X_k.%0A"></p>
<p>Since <img src="https://latex.codecogs.com/png.latex?%5Cbar%20X_n"> is itself a random variable, the available dataset yields the numerical estimate</p>
<p><span id="eq-3"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbar%20x_n=%5Cfrac%7B1%7D%7Bn%7D%5Csum_%7Bk=1%7D%5En%20x_k.%0A%5Ctag%7B1%7D"></span></p>
<p>The expectation of <img src="https://latex.codecogs.com/png.latex?%5Cbar%20X_n"> is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D%5B%5Cbar%20X_n%5D=%5Cfrac%7B1%7D%7Bn%7D%5Csum_%7Bk=1%7D%5En%5Cmathbb%7BE%7D%5BX_k%5D=%5Cmu.%0A"></p>
<p>This holds for every value of <img src="https://latex.codecogs.com/png.latex?n">, meaning that the estimator is unbiased.</p>
<p>The variance of <img src="https://latex.codecogs.com/png.latex?%5Cbar%20X_n"> is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathrm%7BVar%7D(%5Cbar%20X_n)=%5Cfrac%7B1%7D%7Bn%5E2%7D%5Csum_%7Bk=1%7D%5En%5Cmathrm%7BVar%7D(X_k)=%5Cfrac%7B%5Csigma%5E2%7D%7Bn%7D,%0A"></p>
<p>showing the decrease of uncertainty as the number of observations increases. This result ultimately relies on the fact that the variance of a sum is equal to the sum of the variances if the added terms are independent (or at least pairwise uncorrelated).</p>
<p>Moreover, provided that <img src="https://latex.codecogs.com/png.latex?%5Csigma"> is finite, the central limit theorem implies that, for sufficiently large <img src="https://latex.codecogs.com/png.latex?n">, the distribution of <img src="https://latex.codecogs.com/png.latex?%5Cbar%20X_n"> becomes approximately Gaussian even when the original noise distribution is not.</p>
<p>All these results are mathematically elegant, but they also hide an important practical issue: in real experimental settings, collecting a “large” number of genuinely independent observations may be much more difficult than it first appears.</p>
<p>Under the additional assumption of Gaussian noise, the sample mean enjoys several important optimality properties. In particular, it coincides with:</p>
<ul>
<li>the least-squares (LS) solution,</li>
<li>the maximum-likelihood (ML) estimator,</li>
<li>the minimum mean-square error (MMSE) estimator.</li>
</ul>
<p>From an LS perspective, the problem of estimating a constant quantity can also be interpreted as a particularly simple regression model. Since data are acquired sequentially, the acquisition index (or time itself) may be formally regarded as a predictor variable, while the observations play the role of the response.</p>
<p>In regression terms, computing the sample mean corresponds to fitting an intercept-only model, or equivalently a linear model with zero slope. Importantly, the LS solution does not require Gaussian assumptions. In fact, the regression coefficients are obtained simply by minimizing a quadratic cost function, which leads to a linear system of equations when its gradient is set to zero. This procedure produces a straight line that always passes through the centroid of the observed data. When the slope is explicitly constrained to zero, the LS solution coincides with the sample mean. Probabilistic assumptions on <img src="https://latex.codecogs.com/png.latex?V"> instead determine the statistical properties and optimality characteristics of the estimator. In particular, under Gaussian assumptions, the sample mean is also the MMSE estimator.</p>
<p>Before leaving this section, one further observation is worth making: the sample mean admits a recursive formulation. If <img src="https://latex.codecogs.com/png.latex?%5Cbar%20x_%7Bk-1%7D"> denotes the estimate computed from the first <img src="https://latex.codecogs.com/png.latex?k-1"> observations, then the arrival of a new observation <img src="https://latex.codecogs.com/png.latex?x_k"> produces the updated estimate</p>
<p><span id="eq-5"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbar%20x_k=%5Cleft(1-%5Cfrac%7B1%7D%7Bk%7D%5Cright)%5Cbar%20x_%7Bk-1%7D+%5Cfrac%7B1%7D%7Bk%7Dx_k,%5Cqquad%20k=1,%5Cdots,n.%0A%5Ctag%7B2%7D"></span></p>
<p>This recursive expression is algebraically equivalent to the batch computation of the sample mean in Equation&nbsp;1, but it highlights a key idea: the estimate can be updated sequentially using the previous estimate and each newly acquired observation. As <img src="https://latex.codecogs.com/png.latex?k"> increases, the weight assigned to new data points progressively decreases.</p>
</section>
<section id="the-kalman-filter-in-a-nutshell" class="level2">
<h2 class="anchored" data-anchor-id="the-kalman-filter-in-a-nutshell">The Kalman filter in a nutshell</h2>
<p>When Rudolf E. Kálmán (1930–2016) introduced his recursive filtering framework in the early 1960s, the problem was not merely statistical. Modern dynamical systems — aircraft, missiles, spacecraft — were generating streams of noisy observations that needed to be processed sequentially and in real time. Batch estimation techniques already existed, but recomputing the entire estimate from scratch as new data arrived was computationally unattractive, especially on the constrained onboard computers of that era.</p>
<p>Kalman’s key insight was that, under linear Gaussian assumptions, the estimate could instead be updated recursively: each newly acquired observation could be combined with the current state estimate and its uncertainty to produce an improved estimate, without storing or reprocessing the entire measurement history. Equally important, if not revolutionary, was the adoption of a state-space approach to estimation, in which both system dynamics and the measurement process could be modeled explicitly and treated within a unified probabilistic framework.</p>
<p>Over the decades, Kalman’s original linear framework has been extended in many directions, including nonlinear filtering approaches such as the Extended Kalman Filter (EKF), the Unscented Kalman Filter (UKF), and particle filters. Despite their substantial algorithmic differences, all these approaches share the same Bayesian genealogical roots: sequentially updating probabilistic beliefs as new information becomes available.</p>
<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">
Linear Kalman filter
</div>
</div>
<div class="callout-body-container callout-body">
<p>In its simplest linear form, the Kalman filter (KF) can be regarded as a recursive MMSE estimator for dynamical systems observed through noisy measurements.</p>
<p>The KF alternates between two complementary operations: a prediction step based on the dynamical model, and a correction step driven by incoming measurements, as schematically illustrated in Figure&nbsp;1.</p>
<div class="cell">
<div class="cell-output-display">
<div id="fig-kf-diagram" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-kf-diagram-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/post_1/kf_block_diagram.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-kf-diagram-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Conceptual structure of the Kalman filter recursion. Model-based prediction and measurement-based correction interact continuously to update the state estimate and its uncertainty.
</figcaption>
</figure>
</div>
</div>
</div>
<p>For a linear discrete-time dynamical system observed through noisy measurements,</p>
<p><span id="eq-6"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0A%5Cmathbf%7Bx%7D_%7Bk+1%7D&amp;=A_k%5Cmathbf%7Bx%7D_k+%5Cmathbf%7Bw%7D_k%5C%5C%0A%5Cmathbf%7Bz%7D_k&amp;=H_k%5Cmathbf%7Bx%7D_k+%5Cmathbf%7Bv%7D_k%0A%5Cend%7Bcases%7D%0A%5Ctag%7B3%7D"></span></p>
<p>where the state vector is <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bx%7D_k%5Cin%5Cmathbb%7BR%7D%5En">, the state transition matrix <img src="https://latex.codecogs.com/png.latex?A_k"> is square <img src="https://latex.codecogs.com/png.latex?n%5Ctimes%20n">, the measurement vector is <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bz%7D_k%20%5Cin%20%5Cmathbb%7BR%7D%5Em">, and <img src="https://latex.codecogs.com/png.latex?H_k"> is an <img src="https://latex.codecogs.com/png.latex?m%5Ctimes%20n"> measurement matrix.</p>
<p>The <em>process noise</em> <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bw%7D_k"> and the <em>measurement noise</em> <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bv%7D_k"> are modeled as zero-mean Gaussian random vectors with covariance matrices</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0A%5Cmathbf%7Bw%7D_k&amp;%5Csim%5Cmathcal%7BN%7D(0,Q_k)%20%5C%5C%0A%5Cmathbf%7Bv%7D_k&amp;%5Csim%5Cmathcal%7BN%7D(0,R_k)%0A%5Cend%7Bcases%7D%0A"></p>
<p>The covariance matrices <img src="https://latex.codecogs.com/png.latex?Q_k"> and <img src="https://latex.codecogs.com/png.latex?R_k"> are associated with the assumption that the process and measurement noise components are temporally uncorrelated. Because the noise processes are Gaussian, uncorrelated components are also statistically independent. <img src="https://latex.codecogs.com/png.latex?Q_k"> and <img src="https://latex.codecogs.com/png.latex?R_k"> fully characterize the second-order statistical structure of the process and measurement noise.</p>
<p>In Kalman filtering notation, the symbols <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cmathbf%7Bx%7D%7D_k%5E-"> and <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cmathbf%7Bx%7D%7D_k%5E+"> denote the predicted (a priori) and updated (a posteriori) state estimates at time step <img src="https://latex.codecogs.com/png.latex?k">, respectively. The corresponding estimation errors are</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0A%5Cmathbf%7Be%7D_k%5E-&amp;=%5Cmathbf%7Bx%7D_k-%5Chat%7B%5Cmathbf%7Bx%7D%7D_k%5E-%5C%5C%0A%5Cmathbf%7Be%7D_k%5E+&amp;=%5Cmathbf%7Bx%7D_k-%5Chat%7B%5Cmathbf%7Bx%7D%7D_k%5E+,%0A%5Cend%7Bcases%7D%0A"></p>
<p>with covariance matrices</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0AP_k%5E-&amp;=%5Cmathbb%7BE%7D%5Cleft%5B%5Cmathbf%7Be%7D_k%5E%7B-%7D(%5Cmathbf%7Be%7D_k%5E%7B-%7D)%5E%5Ctop%5Cright%5D%20%5C%5C%0AP_k%5E+&amp;=%5Cmathbb%7BE%7D%5Cleft%5B%5Cmathbf%7Be%7D_k%5E%7B+%7D(%5Cmathbf%7Be%7D_k%5E%7B+%7D)%5E%5Ctop%5Cright%5D.%0A%5Cend%7Bcases%7D%0A"></p>
</div>
</div>
<section id="estimating-the-value-of-a-constant-measurand" class="level3">
<h3 class="anchored" data-anchor-id="estimating-the-value-of-a-constant-measurand">Estimating the value of a constant measurand</h3>
<p>Let us now return to the experimental scenario introduced above. The measurand is assumed constant and observations are affected by additive Gaussian noise. In Kalman-filter notation, this corresponds to the scalar model</p>
<p><span id="eq-7"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0Ax_%7Bk+1%7D&amp;=x_k%5C%5C%0Az_k&amp;=x_k+v_k,%0A%5Cend%7Bcases%7D%0A%5Ctag%7B4%7D"></span></p>
<p>where</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Av_k%5Csim%5Cmathcal%7BN%7D(0,R),%5Cqquad%20R=%5Csigma%5E2.%0A"></p>
<p>We also have:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AA=1,%5Cqquad%20H=1,%5Cqquad%20Q=0.%0A"></p>
<p>The condition <img src="https://latex.codecogs.com/png.latex?Q=0"> is particularly important. The filter is explicitly told that the state is constant: measurements are noisy, but the measurand is assumed not to evolve in time. There is therefore uncertainty in the observations, but no uncertainty in the state dynamics: the filter assumes that the true value remains constant and that any fluctuations arise solely from measurement noise.</p>
<p>For the scalar model considered here, the KF equations simplify considerably. Since <img src="https://latex.codecogs.com/png.latex?A=1"> and <img src="https://latex.codecogs.com/png.latex?Q=0">, the prediction step leaves both the estimate and its uncertainty unchanged:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Chat%20x_k%5E-=%5Chat%20x_%7Bk-1%7D%5E+,%5Cqquad%20P_k%5E-=P_%7Bk-1%7D%5E+.%0A"></p>
<p>The Kalman gain is therefore</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AK_k=%5Cfrac%7BP_k%5E-%7D%7BP_k%5E-+R%7D.%0A"></p>
<p>Suppose the filter is initialized with no prior knowledge about the value of the state. In practice, this means that the initial estimate is regarded as extremely uncertain. This corresponds to assigning a very large initial uncertainty to the estimate, formally <img src="https://latex.codecogs.com/png.latex?P_1%5E-%20%5Cto%20%5Cinfty">. As a consequence,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AK_1=%5Cfrac%7BP_1%5E-%7D%7BP_1%5E-+R%7D%5Cto%201,%0A"></p>
<p>so the first measurement is trusted completely:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Chat%20x_1%5E+=z_1.%0A"></p>
<p>The updated covariance is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_1%5E+=%5Cleft(1-%5Cfrac%7BP_1%5E-%7D%7BP_1%5E-+R%7D%5Cright)P_1%5E-=%5Cfrac%7BRP_1%5E-%7D%7BP_1%5E-+R%7D%5Cto%20R.%0A"></p>
<p>Because <img src="https://latex.codecogs.com/png.latex?Q=0">, the prediction step does not reinject uncertainty into the model:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_2%5E-=P_1%5E+=R.%0A"></p>
<p>At the second iteration,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AK_2=%5Cfrac%7BR%7D%7BR+R%7D=%5Cfrac12.%0A"></p>
<p>The updated estimate is therefore</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Chat%20x_2%5E+=%5Chat%20x_1%5E+%20+%5Cfrac12(z_2-%5Chat%20x_1%5E+)=%5Cfrac%7Bz_1+z_2%7D%7B2%7D,%0A"></p>
<p>and the corresponding covariance becomes</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_2%5E+=%5Cfrac%7BR%7D%7B2%7D.%0A"></p>
<p>Continuing the recursion, one obtains</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Chat%20x_k%5E+=%5Cleft(1-%5Cfrac%7B1%7D%7Bk%7D%5Cright)%5Chat%20x_%7Bk-1%7D%5E+%20+%5Cfrac%7B1%7D%7Bk%7Dz_k,%0A"></p>
<p>which is exactly the formula for the sample mean introduced in Equation&nbsp;2. In this special case, the Kalman gain becomes</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AK_k=%5Cfrac%7B1%7D%7Bk%7D.%0A"></p>
<p>The KF therefore progressively stops reacting to new measurements: each additional observation receives less weight as confidence in the estimated value gradually increases.</p>
</section>
</section>
<section id="is-a-physical-quantity-ever-really-constant" class="level2">
<h2 class="anchored" data-anchor-id="is-a-physical-quantity-ever-really-constant">Is a physical quantity ever really constant?</h2>
<p>In the previous section, the KF state was assumed perfectly constant, corresponding to the choice <img src="https://latex.codecogs.com/png.latex?Q=0">. Let us now relax this assumption and consider the more realistic situation in which the state may slowly evolve over time.</p>
<p>The simplest stochastic model for a slowly varying quantity consists of describing the scalar state as a random walk driven by continuous-time white noise with strength <img src="https://latex.codecogs.com/png.latex?q">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cdot%20x(t)%20=%20w(t),%0A"></p>
<p>where</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbb%7BE%7D%5Bw(t)w(t+%5Ctau)%5D%20=%20q%5C,%5Cdelta(%5Ctau).%0A"></p>
<p>If observations are acquired every <img src="https://latex.codecogs.com/png.latex?%5CDelta%20T"> seconds, the corresponding discrete-time model becomes</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax_%7Bk+1%7D=x_k+%5CDelta%20w_k,%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5CDelta%20w_k"> represents the stochastic increment accumulated over the sampling interval <img src="https://latex.codecogs.com/png.latex?%5CDelta%20T">. The variance of this discrete-time process noise is therefore</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AQ=q%5C,%5CDelta%20T.%0A"></p>
<p>This simple relation already contains an important physical idea: uncertainty accumulates over time. The longer the interval between consecutive observations, the larger the uncertainty injected during the prediction step.</p>
<p>For the scalar random-walk model,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bcases%7D%0Ax_%7Bk+1%7D&amp;=x_k+%5CDelta%20w_k%20%5C%5C%0Az_k&amp;=x_k+v_k,%0A%5Cend%7Bcases%7D%0A"></p>
<p>the KF prediction step is particularly simple. Since <img src="https://latex.codecogs.com/png.latex?A=1"> and <img src="https://latex.codecogs.com/png.latex?H=1">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_k%5E-=P_%7Bk-1%7D%5E++Q.%0A"></p>
<p>The Kalman gain is therefore</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AK_k=%5Cfrac%7BP_k%5E-%7D%7BP_k%5E-+R%7D=%5Cfrac%7BP_%7Bk-1%7D%5E++Q%7D%7BP_%7Bk-1%7D%5E++Q+R%7D.%0A"></p>
<p>After the measurement update,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_k%5E+=(1-K_k)P_k%5E-.%0A"></p>
<p>Substituting the expression of <img src="https://latex.codecogs.com/png.latex?K_k">,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_k%5E+=%5Cleft(1-%5Cfrac%7BP_%7Bk-1%7D%5E++Q%7D%7BP_%7Bk-1%7D%5E++Q+R%7D%5Cright)(P_%7Bk-1%7D%5E++Q),%0A"></p>
<p>and therefore</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_k%5E+=%5Cfrac%7B(P_%7Bk-1%7D%5E++Q)R%7D%7BP_%7Bk-1%7D%5E++Q+R%7D.%0A"></p>
<p>This recursion is the scalar Riccati equation for the random-walk model.</p>
<p>The most interesting behavior emerges when the recursion reaches steady state:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_k%5E+=P_%7Bk-1%7D%5E+=P_%5Cinfty,%0A"></p>
<p>then</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_%5Cinfty=%5Cfrac%7B(P_%5Cinfty+Q)R%7D%7BP_%5Cinfty+Q+R%7D.%0A"></p>
<p>Unlike the previous case with <img src="https://latex.codecogs.com/png.latex?Q=0">, the covariance no longer collapses toward zero because uncertainty is continuously reinjected into the dynamics.</p>
<p>Solving this quadratic equation gives</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AP_%5Cinfty=%5Cfrac%7B-Q+%5Csqrt%7BQ%5E2+4QR%7D%7D%7B2%7D.%0A"></p>
<p>As soon as <img src="https://latex.codecogs.com/png.latex?Q%3E0">, the steady-state covariance remains strictly positive. Consequently, the steady-state Kalman gain is also strictly positive:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AK_%5Cinfty=%5Cfrac%7BP_%5Cinfty+Q%7D%7BP_%5Cinfty+Q+R%7D.%0A"></p>
<p>The KF therefore never stops listening to new measurements.</p>
</section>
<section id="simulations" class="level2">
<h2 class="anchored" data-anchor-id="simulations">Simulations</h2>
<p>The following simulation compares two KFs observing the same random-walk process. The first filter uses the correct stochastic model (<img src="https://latex.codecogs.com/png.latex?Q=q%5CDelta%20T">), so that uncertainty is continuously reinjected during prediction. The second incorrectly assumes a perfectly constant state (<img src="https://latex.codecogs.com/png.latex?Q=0">), producing the <em>sleepy</em> behavior discussed below.</p>
<div class="cell">
<details class="code-fold">
<summary>A filter that never forgets — and one that does</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode matlab code-with-copy"><code class="sourceCode matlab"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% ********************************</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Random-walk process simulation</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% ********************************</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dt</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</span><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;">% sampling interval, s</span></span>
<span id="cb1-6"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">T</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;">50</span><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;">% observation time, s</span></span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">q</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</span><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;">% continuous-time noise strength, [a.u.]^2/s</span></span>
<span id="cb1-9"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">std_q</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">q</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dt</span>)<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;">% discrete-time process noise std, [a.u.]</span></span>
<span id="cb1-10"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">std_v</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 class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% measurement noise standard deviation, [a.u.]</span></span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% ***************</span></span>
<span id="cb1-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Path generation</span></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% ***************</span></span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rng</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span>)</span>
<span id="cb1-17"></span>
<span id="cb1-18"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">T</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dt</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-19"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xi</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zeros</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-20"></span>
<span id="cb1-21"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</span></span>
<span id="cb1-22">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xi</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xi</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</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</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">q</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dt</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">randn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-23"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb1-24"></span>
<span id="cb1-25"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xi</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-26"></span>
<span id="cb1-27"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">z_meas</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">randn</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">std_v</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-28"></span>
<span id="cb1-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% ******</span></span>
<span id="cb1-30"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Models</span></span>
<span id="cb1-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% ******</span></span>
<span id="cb1-32"></span>
<span id="cb1-33"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">F</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-34"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">H</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-35"></span>
<span id="cb1-36"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">std_v</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-37"></span>
<span id="cb1-38"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Q_true</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">std_q</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-39"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Q_sleepy</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-40"></span>
<span id="cb1-41"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Q_true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-42"></span>
<span id="cb1-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% **************</span></span>
<span id="cb1-44"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Initialization</span></span>
<span id="cb1-45"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% **************</span></span>
<span id="cb1-46"></span>
<span id="cb1-47"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_true</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zeros</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-48"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_true</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zeros</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-49"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">K_true</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zeros</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-50"></span>
<span id="cb1-51"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_sleepy</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zeros</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-52"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_sleepy</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zeros</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-53"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">K_sleepy</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zeros</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-54"></span>
<span id="cb1-55"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_pred_true</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-56"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_pred_true</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-57"></span>
<span id="cb1-58"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_pred_sleepy</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">randn</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-59"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_pred_sleepy</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-60"></span>
<span id="cb1-61"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</span></span>
<span id="cb1-62"></span>
<span id="cb1-63">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% *******************</span></span>
<span id="cb1-64">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% compute Kalman gain</span></span>
<span id="cb1-65">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% *******************</span></span>
<span id="cb1-66"></span>
<span id="cb1-67">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">K_true</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>)   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_pred_true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">H</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">H</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_pred_true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">H</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-68">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">K_sleepy</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_pred_sleepy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">H</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">H</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_pred_sleepy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">H</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-69">  </span>
<span id="cb1-70">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% ******</span></span>
<span id="cb1-71">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% update</span></span>
<span id="cb1-72">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% ******</span></span>
<span id="cb1-73"></span>
<span id="cb1-74">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_true</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_pred_true</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>
<span id="cb1-75">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">K_true</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">z_meas</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">H</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_pred_true</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-76"></span>
<span id="cb1-77">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_true</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</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</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">K_true</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">H</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_pred_true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-78"></span>
<span id="cb1-79">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_sleepy</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_pred_sleepy</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>
<span id="cb1-80">                <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">K_sleepy</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">z_meas</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">H</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_pred_sleepy</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-81"></span>
<span id="cb1-82">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_sleepy</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</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</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">K_sleepy</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">H</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_pred_sleepy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-83"></span>
<span id="cb1-84">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% *************</span></span>
<span id="cb1-85">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% project-ahead</span></span>
<span id="cb1-86">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% *************</span></span>
<span id="cb1-87"></span>
<span id="cb1-88">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_pred_true</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">F</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_true</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-89">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_pred_true</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">F</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_true</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">F</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Q_true</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-90"></span>
<span id="cb1-91">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_pred_sleepy</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">F</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_sleepy</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-92">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_pred_sleepy</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">F</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">P_sleepy</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">i</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">F</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Q_sleepy</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-93">    </span>
<span id="cb1-94"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb1-95"></span>
<span id="cb1-96"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% ************************</span></span>
<span id="cb1-97"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% State estimate evolution</span></span>
<span id="cb1-98"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% ************************</span></span>
<span id="cb1-99"></span>
<span id="cb1-100"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot</span>((<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</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;">'k'</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;">'LineWidth'</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;">2</span>)</span>
<span id="cb1-101"></span>
<span id="cb1-102"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hold</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">on</span></span>
<span id="cb1-103"></span>
<span id="cb1-104"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot</span>((<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_true</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;">'b.'</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>
<span id="cb1-105">     (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x_sleepy</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;">'r.'</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>
<span id="cb1-106">     <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'LineWidth'</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</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>
<span id="cb1-107">     <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'MarkerSize'</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;">18</span>)</span>
<span id="cb1-108"></span>
<span id="cb1-109"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"process"</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>
<span id="cb1-110">       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"matched KF"</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>
<span id="cb1-111">       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sleepy KF"</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>
<span id="cb1-112">       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Location"</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;">"northwest"</span>)</span>
<span id="cb1-113"></span>
<span id="cb1-114"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xlabel</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Steps'</span>)</span>
<span id="cb1-115"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ylabel</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'State [a.u.]'</span>)</span>
<span id="cb1-116"></span>
<span id="cb1-117"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">set</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gca</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;">'FontSize'</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;">14</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;">'LineWidth'</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;">2</span>)</span>
<span id="cb1-118"></span>
<span id="cb1-119"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% ***********************</span></span>
<span id="cb1-120"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Kalman gain evolution</span></span>
<span id="cb1-121"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% ***********************</span></span>
<span id="cb1-122"></span>
<span id="cb1-123"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">figure</span></span>
<span id="cb1-124"></span>
<span id="cb1-125"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot</span>((<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">K_true</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;">'b'</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>
<span id="cb1-126">     (<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">K_sleepy</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;">'r'</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>
<span id="cb1-127">     <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'LineWidth'</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;">2</span>)</span>
<span id="cb1-128"></span>
<span id="cb1-129"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"matched KF"</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>
<span id="cb1-130">       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sleepy KF"</span>)</span>
<span id="cb1-131"></span>
<span id="cb1-132"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ylim</span>([<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.25</span>])</span>
<span id="cb1-133"></span>
<span id="cb1-134"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xlabel</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Steps'</span>)</span>
<span id="cb1-135"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ylabel</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Kalman gain'</span>)</span>
<span id="cb1-136"></span>
<span id="cb1-137"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">set</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gca</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;">'FontSize'</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;">14</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;">'LineWidth'</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;">2</span>)</span></code></pre></div>
</details>
</div>
<p>Figure&nbsp;2 compares the state estimates produced by the two KFs. While the KF using the correct process model continues to track the evolving random walk, the KF with <img src="https://latex.codecogs.com/png.latex?Q=0"> progressively loses responsiveness.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-kf-path" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-kf-path-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/post_1/traj.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-kf-path-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: State estimates produced by the two filters.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The consequences become evident in Figure&nbsp;3. When <img src="https://latex.codecogs.com/png.latex?Q=0">, the Kalman gain progressively collapses toward zero and the KF gradually stops incorporating new information. By contrast, when <img src="https://latex.codecogs.com/png.latex?Q=q%5CDelta%20T">, the gain converges to a finite steady-state value, allowing the KF to remain responsive to the evolving dynamics. In other words, the KF never becomes completely certain about the system.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-kf-gain" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-kf-gain-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/post_1/gain.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-kf-gain-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Evolution of the Kalman gain.
</figcaption>
</figure>
</div>
</div>
</div>
<p>To better visualize the effect of the process noise variance <img src="https://latex.codecogs.com/png.latex?Q">, a second simulation is performed using three different process models obtained by perturbing a nominal value of Q by one order of magnitude in both directions. More precisely, the three KFs use</p>
<p><img src="https://latex.codecogs.com/png.latex?Q=%5B0.1,%5C%201,%5C%2010%5D%5Ctimes%20q%5CDelta%20T,"></p>
<p>with <img src="https://latex.codecogs.com/png.latex?q=0.1">, while the measurement noise standard deviation is kept fixed at <img src="https://latex.codecogs.com/png.latex?%5Csigma=0.3">.</p>
<p>The first KF intentionally underestimates the process uncertainty and behaves as a <em>sleepy</em> filter, characterized by long memory and slow response to incoming measurements. The second KF uses the correct stochastic model and represents a balanced compromise between prediction and measurement update. The third KF largely overestimates the process uncertainty and behaves as an <em>awake</em> filter, rapidly incorporating incoming measurements into the state estimate.</p>
<p>Figure&nbsp;4 compares the resulting state estimates.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-kf-path-3q" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-kf-path-3q-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/post_1/traj_3q.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:75.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-kf-path-3q-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: State estimates obtained using different values of the process noise variance.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The effect of the different values of <img src="https://latex.codecogs.com/png.latex?Q"> becomes even more evident in the corresponding Kalman gains shown in Figure&nbsp;5.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-kf-gain-3q" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-kf-gain-3q-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/post_1/gain_3q.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:75.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-kf-gain-3q-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: Kalman gain evolution for the three filters.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The process noise variance <img src="https://latex.codecogs.com/png.latex?Q"> effectively acts as a tuning knob. Small values of <img src="https://latex.codecogs.com/png.latex?Q"> produce KFs with slow response, while large values of <img src="https://latex.codecogs.com/png.latex?Q"> produce highly responsive filters that rapidly follow incoming measurements — sometimes too closely.</p>
<p>This behavior is simply another manifestation of the classical trade-off between promptness of response and noise sensitivity encountered in virtually every filtering problem. In practice, mildly overestimating <img src="https://latex.codecogs.com/png.latex?Q"> is often less dangerous than underestimating it: an awake filter may become noisy, but a sleepy filter may eventually stop reacting altogether.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Throughout this post, we explored how different assumptions about the measurand lead to different KF behaviors: from the recursive averaging of a perfectly static constant (<img src="https://latex.codecogs.com/png.latex?Q=0">) to a drifting random walk with independent increments (<img src="https://latex.codecogs.com/png.latex?Q%3E0">). We saw how the process noise covariance <img src="https://latex.codecogs.com/png.latex?Q"> acts as a tuning knob balancing promptness of response and noise sensitivity.</p>
<p>Many physical quantities, however, possess a more structured temporal behavior than a simple random walk. In the next post, we will move one step further by modeling the measurand as a first-order Gauss–Markov process: we will see how the KF manages systems with intrinsic temporal memory.</p>


</section>

 ]]></description>
  <category>stochastic modeling</category>
  <category>signal processing</category>
  <category>measurement</category>
  <guid>https://amsabatini.netlify.app/posts/post_1/</guid>
  <pubDate>Tue, 02 Jun 2026 22:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/post_1/knob.png" medium="image" type="image/png" height="152" width="144"/>
</item>
<item>
  <title>What does a discrete-time noise generator really simulate?</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/stochastic_model_1/</link>
  <description><![CDATA[ 




<blockquote class="blockquote">
<p>💾 <strong>Prefer reading offline?</strong> You can <a href="post_pdf.pdf">download a PDF version of this post</a> for printing or future reference.</p>
</blockquote>
<section id="the-physical-system" class="level2">
<h2 class="anchored" data-anchor-id="the-physical-system">The physical system</h2>
<p>Suppose we have a low-pass RC filter composed of a resistor <img src="https://latex.codecogs.com/png.latex?R"> in series with a capacitor <img src="https://latex.codecogs.com/png.latex?C">, with the output voltage available across the capacitor plates. If the output terminals are connected to, e.g., an oscilloscope, a noise-like waveform can be observed on the screen, even in the absence of any externally applied input voltage (Figure&nbsp;1).</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-rc-schematic" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-rc-schematic-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/stochastic_model_1/RC_circuit.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-rc-schematic-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Thévenin equivalent model of resistor thermal noise driving the RC low-pass filter. The externally applied input voltage is assumed to be zero in this post, while <img src="https://latex.codecogs.com/png.latex?y(t)"> is the output voltage across the capacitor. While this post adopts a rigorous double-sided PSD convention (<img src="https://latex.codecogs.com/png.latex?W_%7BNN%7D%20=%202k_BT%20R">), electronics textbooks and instruments typically quote the single-sided noise amplitude spectral density, <img src="https://latex.codecogs.com/png.latex?e_n%20=%20%5Csqrt%7B4k_BT%20R%7D">, measured in <img src="https://latex.codecogs.com/png.latex?%5Cmu">V<img src="https://latex.codecogs.com/png.latex?/%5Csqrt%7B%5Cmathrm%7BHz%7D%7D">.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Where does this voltage come from? It is well known that thermal noise is generated by the erratic motion of free electrons in the metallic conductor from which the resistor is made. If the resistor is at absolute temperature <img src="https://latex.codecogs.com/png.latex?T">, the classical Nyquist–Johnson model states that, to an excellent approximation, this thermal noise can be represented as a zero-mean, stationary, Gaussian white process, with flat power spectral density (PSD)</p>
<p><span id="eq-eq1"><img src="https://latex.codecogs.com/png.latex?%0AW_%7BNN%7D(%5Comega)=2k_BT%20R,%0A%5Ctag%7B1%7D"></span></p>
<p>where <img src="https://latex.codecogs.com/png.latex?k_B"> is Boltzmann’s constant (<img src="https://latex.codecogs.com/png.latex?k_B=1.380649%5Ctimes%2010%5E%7B-23%7D"> J/K).</p>
<p>Using standard circuit theory, the output noise voltage—the one we actually observe—is still Gaussian and stationary, but no longer white. It is colored by the RC low-pass filter, and its PSD is</p>
<p><span id="eq-eq2"><img src="https://latex.codecogs.com/png.latex?%0AW_%7BYY%7D(%5Comega)=%5Cfrac%7B1%7D%7B1+(%5Comega%20RC)%5E2%7DW_%7BNN%7D(%5Comega)=%5Cfrac%7B2k_BTR%7D%7B1+(%5Comega%20RC)%5E2%7D.%0A%5Ctag%7B2%7D"></span></p>
<p>The variance of the output voltage, equal to its mean-square value since the mean is zero, is therefore</p>
<p><span id="eq-eq3"><img src="https://latex.codecogs.com/png.latex?%0A%5Csigma_Y%5E2=%5Cfrac%7B1%7D%7B2%5Cpi%7D%5Cint_%7B-%5Cinfty%7D%5E%7B+%5Cinfty%7D%20W_%7BYY%7D(%5Comega)%5C,d%5Comega=%5Cfrac%7Bk_BT%7D%7BC%7D.%0A%5Ctag%7B3%7D"></span></p>
<p>It can also be shown using the Wiener–Khinchin theorem that the autocorrelation function <img src="https://latex.codecogs.com/png.latex?R_%7BYY%7D(%5Ctau)"> of the output voltage <img src="https://latex.codecogs.com/png.latex?y(t)"> is the inverse Fourier transform of the PSD:</p>
<p><span id="eq-eq4"><img src="https://latex.codecogs.com/png.latex?%0AR_%7BYY%7D(%5Ctau)=%5Cfrac%7Bk_BT%7D%7BC%7De%5E%7B-%7C%5Ctau%7C/RC%7D.%0A%5Ctag%7B4%7D"></span></p>
<p>Here, <img src="https://latex.codecogs.com/png.latex?RC"> is the time constant of the exponential memory kernel that characterizes the first-order Gauss–Markov process <img src="https://latex.codecogs.com/png.latex?Y(t)">.</p>
<p>So far, everything looks clear. But this standard analytical treatment—familiar from systems theory and signal processing—hides an important fact from view. The approach, rooted in Wiener’s theory, is extremely effective for solving linear ordinary differential equations with constant coefficients and stochastic forcing, almost without giving the impression that one is solving a stochastic differential equation at all.</p>
<p>The crucial step is the formal introduction of white noise: a stationary random process with infinite bandwidth, formally infinite pointwise variance, and impulsive autocorrelation:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AR_%7BNN%7D(%5Ctau)=2k_BTR%5C,%5Cdelta(%5Ctau),%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cdelta(%5Ctau)"> is Dirac’s delta.</p>
<p>Since ideal white noise has infinite bandwidth and infinite power, it should be regarded more as a mathematical idealization than as a literal physical signal—even when it provides an excellent model for phenomena such as thermal noise over the chosen frequency range of interest.</p>
<p>This is where the central question of the post arises: how can we simulate the simple physical system described above in discrete time? And how can we write, in MATLAB, a simulation code that allows us to verify empirically whether the statistical properties of the output are those predicted by the theory of Gauss–Markov processes?</p>
</section>
<section id="the-pieces-of-the-puzzle" class="level2">
<h2 class="anchored" data-anchor-id="the-pieces-of-the-puzzle">The pieces of the puzzle</h2>
<section id="sampling-random-signals" class="level3">
<h3 class="anchored" data-anchor-id="sampling-random-signals">Sampling random signals</h3>
<p>It is well known that the Nyquist–Shannon sampling theorem—usually formulated for deterministic signals—states that a continuous-time signal <img src="https://latex.codecogs.com/png.latex?x_a(t)">, whose bandwidth is strictly limited to <img src="https://latex.codecogs.com/png.latex?B">, can be perfectly reconstructed from the sequence of its samples <img src="https://latex.codecogs.com/png.latex?x_a(nT_s)">, provided that the sampling frequency <img src="https://latex.codecogs.com/png.latex?f_s=1/T_s"> satisfies the Nyquist condition <img src="https://latex.codecogs.com/png.latex?f_s%20%5Cgeq%202B">. The minimum admissible sampling frequency <img src="https://latex.codecogs.com/png.latex?f_%7Bs,%5Cmin%7D=2B"> is called the <em>Nyquist rate</em>, while <img src="https://latex.codecogs.com/png.latex?f_N=%5Cfrac%7Bf_s%7D%7B2%7D"> is called the <em>Nyquist frequency</em>, that is, the highest frequency that can be correctly represented for a given sampling frequency.</p>
<p>Whenever we sample too slowly, aliasing is the price to pay. Because of the well-known folding mechanism in the baseband, high-frequency components are mapped back into lower frequencies, producing distortion that may become so severe that the original spectral content is no longer recognizable.</p>
<p>This picture is familiar and well understood when the signal is deterministic. But what happens when the signal is stochastic rather than deterministic? Can the same theorem still be applied? And, more importantly, what exactly does it mean to sample a random process? Are we sampling individual realizations of the process, its autocorrelation function, or both? And what criteria of accuracy can be stated for the reconstruction?</p>
<div class="callout callout-style-default callout-important 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">
Review: sampling wide-sense stationary random processes
</div>
</div>
<div class="callout-body-container callout-body">
<p>Let <img src="https://latex.codecogs.com/png.latex?X_a(t)"> be a continuous-time wide-sense stationary random process with autocorrelation function <img src="https://latex.codecogs.com/png.latex?R_%7BXX%7D(%5Ctau)"> and PSD <img src="https://latex.codecogs.com/png.latex?W_%7BXX%7D(%5Comega)">. If each realization of the process is sampled every <img src="https://latex.codecogs.com/png.latex?T_s"> seconds, the resulting discrete-time process</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax%5Bn%5D=x_a(nT_s)%0A"></p>
<p>is also wide-sense stationary, with autocorrelation sequence</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ar_%7BXX%7D%5Bk%5D=R_%7BXX%7D(kT_s).%0A"></p>
<p>That is, sampling the process induces sampling of its autocorrelation function.</p>
<p>Moreover, if the process is band-limited and the Nyquist condition is satisfied, each realization can be reconstructed from its samples in the <em>mean-square sense</em>, that is, with vanishing mean-square reconstruction error.</p>
<p>Equivalently, in the frequency domain, the discrete-time PSD is obtained through periodic replication of the continuous-time spectrum:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AW_d(e%5E%7Bj%5Ctheta%7D)=%5Cfrac%7B1%7D%7BT_s%7D%5Csum_%7Bm=-%5Cinfty%7D%5E%7B+%5Cinfty%7DW_%7BXX%7D%5Cleft(%5Cfrac%7B%5Ctheta%20-%202%5Cpi%20m%7D%7BT_s%7D%5Cright).%0A"></p>
<p>If <img src="https://latex.codecogs.com/png.latex?W_%7BXX%7D(%5Comega)"> is strictly band-limited to <img src="https://latex.codecogs.com/png.latex?B"> and the Nyquist condition is satisfied, these replicas do not overlap and aliasing is avoided.</p>
<p>This is the stochastic analogue of the classical sampling theorem. The crucial point is that the theorem applies to the second-order structure of the process—autocorrelation and PSD—and the reconstruction of sample paths must be understood in the mean-square sense, not necessarily pointwise as for ordinary deterministic signals.</p>
</div>
</div>
</section>
<section id="white-noise-and-its-trap" class="level3">
<h3 class="anchored" data-anchor-id="white-noise-and-its-trap">White noise and its trap</h3>
<p>The problem with sampling ideal white noise, as introduced above, is that its PSD is flat over the entire frequency axis. Therefore, no finite band limit can be found, and no matter how fast we sample, aliasing cannot be avoided. At first sight, this leads to a paradox. Since sampling produces periodic replicas of the continuous-time spectrum, an infinite number of copies of a flat PSD would fold back into the baseband. One might then be tempted to conclude that the resulting discrete-time sequence should have infinite pointwise variance.</p>
<p>That conclusion is, of course, nonsense. But the fact that it sounds plausible is exactly the conceptual trap. Now let us see how the picture changes if we assume that the noise is white only within a finite physical bandwidth <img src="https://latex.codecogs.com/png.latex?B_N"> (in Hz).</p>
<p>Let us choose <img src="https://latex.codecogs.com/png.latex?B_N"> sufficiently larger than the bandwidth of the physical system receiving the stochastic forcing—in the present case, the RC low-pass filter, namely:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AB_N%20%5Cgg%20%5Cdfrac%7B1%7D%7B2%5Cpi%20RC%7D.%0A"></p>
<p>The system then behaves, for all practical purposes, as if it were driven by ideal white noise. The input noise is no longer an ideal mathematical object with infinite bandwidth, but a physically meaningful band-limited random process whose sampling can be treated consistently within the ordinary framework of sampling theory. Its PSD can be written as</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AW_%7BNN%7D(%5Comega)=%0A%5Cbegin%7Bcases%7D%0AN_0,%20&amp;%20%7C%5Comega%7C%20%5Cleq%20%5Comega_N%20%5C%5C%0A0,%20&amp;%20%5Ctext%7Botherwise%7D.%0A%5Cend%7Bcases%7D%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Comega_N%20=%202%5Cpi%20B_N">, and</p>
<p><span id="eq-eq6"><img src="https://latex.codecogs.com/png.latex?%0AN_0%20=%202k_BTR.%0A%5Ctag%7B5%7D"></span></p>
<p>By the Wiener–Khinchin theorem, the corresponding continuous-time autocorrelation function is the inverse Fourier transform of the rectangular spectrum:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AR_%7BNN%7D(%5Ctau)=%5Cfrac%7B1%7D%7B2%5Cpi%7D%5Cint_%7B-%5Comega_N%7D%5E%7B+%5Comega_N%7D%20N_0%20e%5E%7Bj%5Comega%5Ctau%7D%5C,d%5Comega%20=%20%5Cfrac%7BN_0%7D%7B%5Cpi%5Ctau%7D%5Csin(%5Comega_N%5Ctau)=2B_NN_0%5C,%5Cmathrm%7Bsinc%7D(2B_N%5Ctau),%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Bsinc%7D(x)%20=%20%5Csin(%5Cpi%20x)/(%5Cpi%20x)"> denotes the normalized sinc function standard in signal processing and MATLAB.</p>
<p>Here lies the elegant resolution of the paradox, matching the simulation strategy: the noise bandwidth <img src="https://latex.codecogs.com/png.latex?B_N"> is fixed large enough to cover the filter physics, and the system is sampled <em>exactly at the Nyquist rate of the noise</em>:</p>
<p><span id="eq-eq7"><img src="https://latex.codecogs.com/png.latex?%0Af_s%20=%202B_N.%0A%5Ctag%7B6%7D"></span></p>
<p>Under this specific condition, the sampled input autocorrelation sequence at discrete lags <img src="https://latex.codecogs.com/png.latex?%5Ctau%20=%20k%20T_s%20=%20k/(2B_N)"> evaluates to:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ar_%7BNN%7D%5Bk%5D=R_%7BNN%7D%5Cleft(%5Cfrac%7Bk%7D%7B2B_N%7D%5Cright)%20=%202B_NN_0%5C,%5Cmathrm%7Bsinc%7D(k)%20=%0A%5Cbegin%7Bcases%7D%0A2B_NN_0,%20&amp;%20k=0%20%5C%5C%0A0,%20&amp;%20k%5Cneq%200.%0A%5Cend%7Bcases%7D%0A"></p>
<p>Because <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Bsinc%7D(k)%20=%200"> for all non-zero integers <img src="https://latex.codecogs.com/png.latex?k">, the samples become completely uncorrelated. This is exactly the defining property of discrete-time white noise.</p>
<p>This is the point where the apparent paradox disappears: a discrete-time white-noise sequence generated numerically (e.g., via <code>randn</code> in MATLAB) is not the impossible sampling of ideal continuous-time white noise, but the perfectly legitimate Nyquist-rate sampling of a band-limited white process.</p>
<p>Using (Equation&nbsp;5) and (Equation&nbsp;6), the variance of the discrete-time noise sequence driving the digital filter evaluates to:</p>
<p><span id="eq-eq8"><img src="https://latex.codecogs.com/png.latex?%0A%5Csigma_X%5E2%20=%202B_N%20N_0%20=%20%5Cfrac%7B2k_BTR%7D%7BT_s%7D.%0A%5Ctag%7B7%7D"></span></p>
<p>This is the hidden assumption behind every practical simulation of thermal noise. When a discrete-time noise generator produces a sequence of independent Gaussian samples, it is not simulating ideal white noise in the strict mathematical sense. It is simulating the Nyquist-rate sampling of a band-limited white process whose effective bandwidth has been placed—explicitly or implicitly—by physics.</p>
<p>In other words, the real question is never</p>
<p><em>how do we sample white noise?</em></p>
<p>but rather</p>
<p><em>where does physics place bandwidth?</em></p>
<div class="callout callout-style-default callout-important 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">
Why the output variance does not depend on the sampling rate
</div>
</div>
<div class="callout-body-container callout-body">
<p>For a discrete-time linear time-invariant filter driven by white noise with variance <img src="https://latex.codecogs.com/png.latex?%5Csigma_X%5E2">, the output variance is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csigma_Y%5E2=%5Csigma_X%5E2%5Csum_%7Bn=0%7D%5E%7B%5Cinfty%7D%20h%5E2%5Bn%5D,%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?h%5Bn%5D"> is the discrete-time impulse response.</p>
<p>The quantity</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AG_N=%5Cfrac%7B%5Csigma_Y%5E2%7D%7B%5Csigma_X%5E2%7D=%5Csum_%7Bn=0%7D%5E%7B%5Cinfty%7D%20h%5E2%5Bn%5D%0A"></p>
<p>is called the <em>discrete-time noise gain</em>.</p>
<p>For the Tustin (bilinear) discretization of the first-order RC filter, the transfer function is</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AH(z)=%5Cfrac%7Bb_0+b_1z%5E%7B-1%7D%7D%7B1+a_1z%5E%7B-1%7D%7D,%0A"></p>
<p>with</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ab_0=b_1=%5Cfrac%7B1%7D%7B1+%5Calpha%7D,%0A%5Cqquad%0Aa_1=%5Cfrac%7B1-%5Calpha%7D%7B1+%5Calpha%7D,%0A%5Cqquad%0A%5Calpha=%5Cfrac%7B2RC%7D%7BT_s%7D.%0A"></p>
<p>The resulting impulse response evaluates to <img src="https://latex.codecogs.com/png.latex?h%5B0%5D=b_0"> and, for <img src="https://latex.codecogs.com/png.latex?n%5Cgeq1">, to <img src="https://latex.codecogs.com/png.latex?h%5Bn%5D=b_0(1-a_1)(-a_1)%5E%7Bn-1%7D">. Summing the squares of this sequence yields a clean geometric series that simplifies to:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AG_N%20=%20b_0%5E2%20%5Cleft(%201%20+%20%5Cfrac%7B1-a_1%7D%7B1+a_1%7D%20%5Cright)=%5Cfrac%7B1%7D%7B1+%5Calpha%7D=%5Cfrac%7B1%7D%7B1+2RC/T_s%7D.%0A"></p>
<p>For a sufficiently small sampling interval (<img src="https://latex.codecogs.com/png.latex?T_s%20%5Cll%20RC">), the parameter <img src="https://latex.codecogs.com/png.latex?%5Calpha"> becomes very large, and the discrete-time noise gain can be approximated as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AG_N%5Capprox%5Cfrac%7BT_s%7D%7B2RC%7D.%0A"></p>
<p>As the sampling interval <img src="https://latex.codecogs.com/png.latex?T_s"> decreases, the variance of the discrete-time white-noise input must increase (<img src="https://latex.codecogs.com/png.latex?%5Csigma_X%5E2%20=%202B_N%20N_0%20=%20N_0/T_s">) in order to represent the same continuous-time thermal-noise PSD. At the same time, the discrete-time noise gain <img src="https://latex.codecogs.com/png.latex?G_N"> decreases by exactly the same factor, perfectly compensating for the input scaling and recovering the continuous-time result in Equation&nbsp;3:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csigma_Y%5E2%20=%20%5Csigma_X%5E2%20%5Ccdot%20G_N%20%5Capprox%20%5Cleft(%5Cfrac%7B2k_BTR%7D%7BT_s%7D%5Cright)%20%5Ccdot%20%5Cleft(%5Cfrac%7BT_s%7D%7B2RC%7D%5Cright)%20=%20%5Cfrac%7Bk_BT%7D%7BC%7D.%0A"></p>
</div>
</div>
</section>
</section>
<section id="matlab-code" class="level2">
<h2 class="anchored" data-anchor-id="matlab-code">MATLAB code</h2>
<p>At this stage, the conceptual picture is now in place: the input noise can be modeled as a discrete-time white Gaussian sequence, provided that we remember what this really means.</p>
<p>We are not sampling ideal continuous-time white noise, but generating the Nyquist-rate representation of a band-limited white process whose effective bandwidth is placed beyond the frequency range relevant for the RC filter. This is the correct interpretation of a standard command such as randn() in MATLAB: it produces independent Gaussian samples with finite variance and impulsive autocorrelation in the discrete-time sense.</p>
<p>That variance, however, must be scaled consistently with the sampling interval so that the equivalent continuous-time PSD matches the physical Nyquist–Johnson thermal-noise level. The following code implements a single realization of the RC output voltage using the Tustin discretization discussed above. The input noise is calibrated directly in physical units, so that the output variance emerges naturally from the model and converges to the theoretical value of (Equation&nbsp;3), rather than being imposed afterward by artificial normalization.</p>
<div class="cell">
<details class="code-fold">
<summary>Single realization of the RC thermal-noise output</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode matlab code-with-copy"><code class="sourceCode matlab"><span id="cb1-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">clear</span></span>
<span id="cb1-2"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">clc</span></span>
<span id="cb1-3"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">close</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">all</span></span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rng</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span>)           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Reproducibility</span></span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Physical constants and RC parameters</span></span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">kB</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.380649e-23</span><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;">% Boltzmann constant [J/K]</span></span>
<span id="cb1-10"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">T</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;">300</span><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;">% Absolute temperature [K]</span></span>
<span id="cb1-11"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</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;">10e3</span><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;">% Resistance [Ohm]</span></span>
<span id="cb1-12"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">C</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;">100e-9</span><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;">% Capacitance [F]</span></span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tau</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">C</span><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;">% Theoretical time constant [s]</span></span>
<span id="cb1-15"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma2_y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">kB</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">T</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">C</span><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;">% Theoretical output variance [V^2]</span></span>
<span id="cb1-16"></span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Sampling settings</span></span>
<span id="cb1-18"></span>
<span id="cb1-19"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">oversampling</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;">50</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-20"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Ts</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tau</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">oversampling</span><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;">% Sampling interval [s]</span></span>
<span id="cb1-21"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Fs</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Ts</span><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;">% Sampling frequency [Hz]</span></span>
<span id="cb1-22"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">BN</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Fs</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;">2</span><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;">% Effective white-noise bandwidth [Hz]</span></span>
<span id="cb1-23"></span>
<span id="cb1-24"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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;">2</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;">14</span><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;">% Number of samples</span></span>
<span id="cb1-25"></span>
<span id="cb1-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Tustin discretization</span></span>
<span id="cb1-27"></span>
<span id="cb1-28"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">alpha</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">C</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Ts</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-29"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b0</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</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</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">alpha</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-30"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b1</span>    <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-31"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">a1</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</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">alpha</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</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">alpha</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-32"></span>
<span id="cb1-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Input white-noise calibration</span></span>
<span id="cb1-34"></span>
<span id="cb1-35"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N0</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">kB</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">T</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-36"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma2_x</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">BN</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-37"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma_x</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma2_x</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-38"></span>
<span id="cb1-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Generate calibrated discrete-time white Gaussian noise</span></span>
<span id="cb1-40"></span>
<span id="cb1-41"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma_x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">randn</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-42"></span>
<span id="cb1-43"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Filter input noise through the discrete-time RC filter</span></span>
<span id="cb1-44"></span>
<span id="cb1-45"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zeros</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-46"></span>
<span id="cb1-47"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</span></span>
<span id="cb1-48">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</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</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">a1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-49"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb1-50"></span>
<span id="cb1-51"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Empirical variance check</span></span>
<span id="cb1-52"></span>
<span id="cb1-53"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fprintf</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Theoretical output variance = %.4e V^2\n'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma2_y</span>)</span>
<span id="cb1-54"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fprintf</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Empirical output variance   = %.4e V^2\n'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>))</span>
<span id="cb1-55"></span>
<span id="cb1-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Time-domain visualization</span></span>
<span id="cb1-57"></span>
<span id="cb1-58"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">t</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Ts</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-59"></span>
<span id="cb1-60"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">figure</span></span>
<span id="cb1-61"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">t</span>(<span class="fl" 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;">5000</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;">1e3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>(<span class="fl" 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;">5000</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;">'b'</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;">'LineWidth'</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</span>)</span>
<span id="cb1-62"></span>
<span id="cb1-63"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xlabel</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Time [ms]'</span>)</span>
<span id="cb1-64"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ylabel</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Output voltage [V]'</span>)</span>
<span id="cb1-65"></span>
<span id="cb1-66"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">grid</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">on</span></span>
<span id="cb1-67"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">set</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gca</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;">"FontSize"</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;">14</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;">"LineWidth"</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.5</span>)</span></code></pre></div>
</details>
</div>
<p>A single realization of the simulated RC output voltage is shown in Figure&nbsp;2. The waveform no longer resembles ideal white noise. The RC filter introduces temporal correlation, producing the smooth random fluctuations characteristic of a first-order Gauss–Markov process. The signal remains random, but it now carries memory: nearby samples are no longer statistically independent.</p>
<p>This is exactly the stochastic structure predicted by the continuous-time model discussed above.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-rc-output" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-rc-output-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/stochastic_model_1/noise.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-rc-output-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Single realization of the simulated RC thermal-noise output voltage. The waveform exhibits the expected smooth random fluctuations of a first-order Gauss–Markov process, reflecting the finite memory introduced by the RC low-pass filter.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="why-autocorrelation-estimation-is-always-hungry-for-data" class="level2">
<h2 class="anchored" data-anchor-id="why-autocorrelation-estimation-is-always-hungry-for-data">Why autocorrelation estimation is always hungry for data</h2>
<p>The final piece of the puzzle is not the simulation itself, but its statistical verification. Generating a discrete-time realization of the RC output voltage is easy. Demonstrating that its statistical properties actually agree with the theory of the Gauss–Markov process is much less trivial. In particular, the key quantity to recover is the exponential autocorrelation law in Equation&nbsp;4, from which both the stationary variance and the characteristic time constant (RC) can be estimated.</p>
<p>A single realization is never enough. Even when the model is perfectly correct, empirical autocorrelation estimates are noisy, strongly variable at large lags, and notoriously hungry for data. Second-order statistics require long observation windows, and the apparent quality of a simulation often depends more on record length than on the model itself.</p>
<p>To make this visible, a Monte Carlo strategy is particularly useful. Rather than generating many completely independent short simulations, it is often more convenient to generate one very long stationary sequence, discard the initial transient needed to reach equilibrium, and then divide the remaining portion into many consecutive segments of equal length.</p>
<p>In MATLAB, this can be done naturally by reshaping the vector into a matrix:</p>
<ul>
<li>rows represent time evolution within each realization<br>
</li>
<li>columns represent approximately independent replications</li>
</ul>
<p>This provides a practical ensemble over which autocorrelation functions can be estimated and compared.</p>
<div class="cell">
<details class="code-fold">
<summary>Monte Carlo verification of the RC Gauss-Markov process</summary>
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode matlab code-with-copy"><code class="sourceCode matlab"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% The physical constants, sampling settings, and Tustin coefficients</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% are the same as in the single-realization example above.</span></span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">rng</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span>)           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Reproducibility</span></span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Monte Carlo settings</span></span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nRep</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;">200</span><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;">% Number of Monte Carlo repetitions</span></span>
<span id="cb2-9"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nPerRep</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;">2</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;">14</span><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;">% Samples per repetition</span></span>
<span id="cb2-10"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nTransient</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;">20</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">oversampling</span><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;">% Initial transient to discard</span></span>
<span id="cb2-11"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Ntot</span>       <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nTransient</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nRep</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nPerRep</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-12"></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Input white-noise calibration</span></span>
<span id="cb2-14"></span>
<span id="cb2-15"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N0</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">kB</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">T</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">R</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-16"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma2_x</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">BN</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">N0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-17"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma_x</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma2_x</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-18"></span>
<span id="cb2-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Generate calibrated discrete-time white Gaussian noise</span></span>
<span id="cb2-20"></span>
<span id="cb2-21"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma_x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">randn</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Ntot</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-22"></span>
<span id="cb2-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Filter input noise through the discrete-time RC filter</span></span>
<span id="cb2-24"></span>
<span id="cb2-25"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zeros</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Ntot</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-26"></span>
<span id="cb2-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</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;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Ntot</span></span>
<span id="cb2-28">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">b1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">x</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</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</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">a1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">n</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-29"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb2-30"></span>
<span id="cb2-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Remove transient and reshape into Monte Carlo replications</span></span>
<span id="cb2-32"></span>
<span id="cb2-33"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y_stat</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nTransient</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-34"></span>
<span id="cb2-35"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fprintf</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Theoretical output variance = %.4e V^2\n'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma2_y</span>)</span>
<span id="cb2-36"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fprintf</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Empirical output variance   = %.4e V^2\n'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">var</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y_stat</span>))</span>
<span id="cb2-37"></span>
<span id="cb2-38"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Y</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">reshape</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">y_stat</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nPerRep</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nRep</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-39"></span>
<span id="cb2-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Autocorrelation estimation</span></span>
<span id="cb2-41"></span>
<span id="cb2-42"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">maxLag</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">round</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tau</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Ts</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-43"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lags</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">maxLag</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">';</span></span>
<span id="cb2-44"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tLag</span>   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">lags</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Ts</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-45"></span>
<span id="cb2-46"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfMat</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zeros</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">maxLag</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nRep</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-47"></span>
<span id="cb2-48"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">r</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nRep</span></span>
<span id="cb2-49">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">yr</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Y</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">r</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-50">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">yr</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">yr</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">yr</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-51"></span>
<span id="cb2-52">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% The biased estimator is used to obtain a smoother and more</span></span>
<span id="cb2-53">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% stable estimate of the exponentially decaying autocorrelation.</span></span>
<span id="cb2-54">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfFull</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xcorr</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">yr</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">maxLag</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;">'biased'</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-55"></span>
<span id="cb2-56">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfMat</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">r</span>) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfFull</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">maxLag</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-57"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb2-58"></span>
<span id="cb2-59"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfMean</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfMat</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;">2</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-60"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfLow</span>  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">prctile</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfMat</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;">2.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-61"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfHigh</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">prctile</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfMat</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;">97.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-62"></span>
<span id="cb2-63"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfTheory</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">sigma2_y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">exp</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tLag</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tau</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-64"></span>
<span id="cb2-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Plot autocorrelation estimates</span></span>
<span id="cb2-66"></span>
<span id="cb2-67"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">figure</span></span>
<span id="cb2-68"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">hold</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">on</span></span>
<span id="cb2-69"></span>
<span id="cb2-70"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fill</span>([<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tLag</span>(<span class="fl" 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="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">flipud</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tLag</span>(<span class="fl" 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="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span>))]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tau</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>
<span id="cb2-71">     [<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfLow</span>(<span class="fl" 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="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">flipud</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfHigh</span>(<span class="fl" 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="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</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>
<span id="cb2-72">     [<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</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>
<span id="cb2-73">     <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'EdgeColor'</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;">'none'</span>)</span>
<span id="cb2-74"></span>
<span id="cb2-75"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tLag</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tau</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfMean</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;">'b'</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;">'LineWidth'</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;">2</span>)</span>
<span id="cb2-76"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">plot</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tLag</span>(<span class="fl" 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;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tau</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfTheory</span>(<span class="fl" 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;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</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>
<span id="cb2-77">     <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'k.'</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;">'MarkerSize'</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;">18</span>)</span>
<span id="cb2-78"></span>
<span id="cb2-79"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xlabel</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Lag / RC'</span>)</span>
<span id="cb2-80"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">ylabel</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Autocorrelation [V^2]'</span>)</span>
<span id="cb2-81"></span>
<span id="cb2-82"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">legend</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'95% Monte Carlo band'</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>
<span id="cb2-83">       <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Monte Carlo mean'</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>
<span id="cb2-84">       <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Theory'</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>
<span id="cb2-85">       <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Location'</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;">'northeast'</span>)</span>
<span id="cb2-86"></span>
<span id="cb2-87"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">grid</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">off</span></span>
<span id="cb2-88"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">set</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">gca</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;">"FontSize"</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;">14</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;">"LineWidth"</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.5</span>)</span>
<span id="cb2-89"></span>
<span id="cb2-90"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Estimate time constant from semilog regression</span></span>
<span id="cb2-91"></span>
<span id="cb2-92"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fitMaxLag</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">round</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tau</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">Ts</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-93"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fitIdx</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fitMaxLag</span>)<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;">% Exclude lag zero</span></span>
<span id="cb2-94"></span>
<span id="cb2-95"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tauHat</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zeros</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nRep</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-96"></span>
<span id="cb2-97"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">r</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">nRep</span></span>
<span id="cb2-98">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acf_r</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acfMat</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">r</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-99"></span>
<span id="cb2-100">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">yFit</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">acf_r</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fitIdx</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-101"></span>
<span id="cb2-102">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Keep only positive values before taking logs</span></span>
<span id="cb2-103">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">valid</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">yFit</span> <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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-104"></span>
<span id="cb2-105">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xReg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tLag</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fitIdx</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">valid</span>))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-106">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zReg</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">log</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">yFit</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">valid</span>))<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-107"></span>
<span id="cb2-108">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">polyfit</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">xReg</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">zReg</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</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-109"></span>
<span id="cb2-110">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tauHat</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">r</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</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">p</span>(<span class="fl" 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-111"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">end</span></span>
<span id="cb2-112"></span>
<span id="cb2-113"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Summary of estimated time constants</span></span>
<span id="cb2-114"></span>
<span id="cb2-115"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tauMean</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mean</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tauHat</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-116"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tauCI</span>   <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">prctile</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tauHat</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;">2.5</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">97.5</span>])<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb2-117"></span>
<span id="cb2-118"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fprintf</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Theoretical tau = %.4e s\n'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tau</span>)</span>
<span id="cb2-119"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fprintf</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'Estimated tau   = %.4e s\n'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tauMean</span>)</span>
<span id="cb2-120"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">fprintf</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'95%% Monte Carlo CI = [%.4e, %.4e] s\n'</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>
<span id="cb2-121">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">tauCI</span>(<span class="fl" 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="va" style="color: #111111;
background-color: null;
font-style: inherit;">tauCI</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span></code></pre></div>
</details>
</div>
<p>Two situations are especially instructive:</p>
<ol type="1">
<li><p>Experimentally realistic regime<br>
Longer records allow the expected exponential law to emerge clearly, with confidence intervals narrow enough to support reliable parameter estimation.</p></li>
<li><p>Data-poor regime<br>
Short records produce unstable autocorrelation estimates, large uncertainty bands, and poor estimates of the decay constant.</p></li>
</ol>
<p>The decay constant can be estimated by exploiting the exponential form of the theoretical autocorrelation. Taking logarithms,</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Clog%20R_%7BYY%7D(%5Ctau)=%5Clog%5Cleft(%5Cfrac%7Bk_BT%7D%7BC%7D%5Cright)-%5Cfrac%7B%5Ctau%7D%7BRC%7D,%0A"></p>
<p>so that the time constant can be recovered by linear regression on the semilog scale.</p>
<p>From repeated Monte Carlo replications, one obtains not only a point estimate of (RC), but also an empirical confidence interval for the estimate itself. This final step closes the loop between theory and simulation: not merely generating a noise-like waveform, but verifying quantitatively that the stochastic process observed numerically is statistically consistent with the physical model from which it originated.</p>
<p>And this is precisely where simulation stops being coding and becomes measurement.</p>
<p>The Monte Carlo results for the experimentally realistic regime are shown in Figure&nbsp;3.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-acf-montecarlo" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-acf-montecarlo-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/stochastic_model_1/ACF.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-acf-montecarlo-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Monte Carlo estimate of the autocorrelation function of the RC output process. The shaded region represents the empirical 95% confidence band obtained from repeated segments of a long stationary realization. The theoretical exponential law is shown as discrete markers. The agreement confirms that the simulated process reproduces the second-order structure of the continuous-time Gauss–Markov model.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The agreement between the empirical mean autocorrelation and the theoretical exponential law of Equation&nbsp;4 is excellent. The shaded region represents the empirical 95% confidence band obtained from repeated stationary segments of the same long realization. Even with a correctly specified model, the uncertainty remains substantial, especially at larger lags where autocorrelation estimates become progressively less stable.</p>
<p>In the present simulation, the theoretical time constant was</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctau%20=%20RC%20=%201.00%5Ctimes10%5E%7B-3%7D%5C%20%5Cmathrm%7Bs%7D,%0A"></p>
<p>while the Monte Carlo estimate obtained from semilog regression gave</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7B%5Ctau%7D=9.69%5Ctimes10%5E%7B-4%7D%5C%20%5Cmathrm%7Bs%7D,%0A"></p>
<p>with empirical 95% confidence interval</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5B5.32%5Ctimes10%5E%7B-4%7D,%5C%201.78%5Ctimes10%5E%7B-3%7D%5Cright%5D%5C%20%5Cmathrm%7Bs%7D.%0A"></p>
<p>The mean estimate is accurate, while the relatively wide confidence interval confirms how demanding autocorrelation-based parameter estimation can be, even when the underlying model is perfectly correct.</p>
<p>The contrast becomes even clearer in the data-poor regime shown in Figure&nbsp;4.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-acf-datapoor" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-acf-datapoor-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/stochastic_model_1/acf_1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-acf-datapoor-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Autocorrelation estimate obtained from much shorter records. The increased variability and the widening confidence band illustrate the dramatic loss of statistical reliability when the available data are limited.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Repeating exactly the same procedure with much shorter records (<img src="https://latex.codecogs.com/png.latex?n_%7B%5Cmathrm%7BPerRep%7D%7D%20=%202%5E%7B10%7D"> rather than <img src="https://latex.codecogs.com/png.latex?n_%7B%5Cmathrm%7BPerRep%7D%7D%20=%202%5E%7B14%7D">) produced</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7B%5Ctau%7D=9.53%5Ctimes10%5E%7B-4%7D%5C%20%5Cmathrm%7Bs%7D,%0A"></p>
<p>with empirical 95% confidence interval</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5B2.87%5Ctimes10%5E%7B-4%7D,%5C%203.32%5Ctimes10%5E%7B-3%7D%5Cright%5D%5C%20%5Cmathrm%7Bs%7D.%0A"></p>
<p>The mean estimate remains reasonable, but the uncertainty increases substantially. Although the physical time constant of the system is only <img src="https://latex.codecogs.com/png.latex?1"> ms, reliable estimation of that constant from autocorrelation requires observation windows hundreds or even thousands of times longer:</p>
<p><em>The process forgets quickly; the statistic does not.</em></p>
</section>
<section id="concluding-remarks" class="level2">
<h2 class="anchored" data-anchor-id="concluding-remarks">Concluding remarks</h2>
<p>At first sight, simulating thermal noise in an RC filter looks like a very simple problem. A resistor generates white noise, the filter shapes it, and a few lines of MATLAB code seem enough to reproduce the expected stochastic output. The real difficulty appears only when one asks a slightly more careful question: what exactly is being simulated?</p>
<p>Ideal continuous-time white noise, with infinite bandwidth and impulsive autocorrelation, cannot be sampled in any literal sense. Trying to do so leads immediately to paradoxes involving aliasing, infinite power, and meaningless pointwise variance. The resolution is not mathematical trickery, but physical modeling. Real systems always place bandwidth somewhere.</p>
<p>If thermal noise is assumed to be effectively white only over a sufficiently large frequency range—larger than the bandwidth of the RC filter itself—then discrete-time simulation becomes perfectly well defined. A Gaussian white sequence generated numerically is not the impossible sampling of an idealized process, but the Nyquist-rate representation of a band-limited stochastic process. The filter then completes the picture: the discrete-time implementation is not merely a numerical approximation, but a way of preserving the structure of the underlying Gauss–Markov process.</p>
<p>And finally, statistics reminds us that even a correct model is not enough. Recovering exponential autocorrelation from finite data requires long records, careful estimation, and a healthy respect for the fact that second-order statistics are always hungry for data.</p>
<p>In the end, the most useful question is not</p>
<p><em>how do we simulate white noise?</em></p>
<p>but rather</p>
<p><em>where does physics place bandwidth?</em></p>
<p>Everything else—including <code>randn</code>, Tustin discretization, and Monte Carlo verification—comes only after that.</p>
</section>
<section id="final-note" class="level2">
<h2 class="anchored" data-anchor-id="final-note">Final note</h2>
<p>Give an engineer a noisy oscilloscope trace, and they will immediately start looking for hidden periodicities.</p>
<blockquote class="blockquote">
<p>“Wait… is that mains hum?<br>
A badly placed notch?<br>
Some forgotten pole?<br>
Why does that look suspiciously like 50 Hz?<br>
And does the time base actually support that conclusion?”</p>
</blockquote>
<p>At that point, the waveform stops being “just noise” and becomes an investigation. Perhaps that is exactly how it should be. For an engineer, noise is rarely just noise. It is usually a signal that is trying—sometimes very stubbornly—to reveal the physical system that generated it. And yes, looking at the thumbnail of this post, I am still not entirely convinced that the oscilloscope trace is innocent.</p>


</section>

 ]]></description>
  <category>stochastic modeling</category>
  <category>signal processing</category>
  <category>measurement</category>
  <guid>https://amsabatini.netlify.app/posts/stochastic_model_1/</guid>
  <pubDate>Thu, 14 May 2026 22:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/stochastic_model_1/thermal_noise.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Discretizing motion under stochastic acceleration</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/stochastic_model/</link>
  <description><![CDATA[ 




<blockquote class="blockquote">
<p>💾 <strong>Prefer reading offline?</strong> You can <a href="stochastic_model.pdf">download a PDF version of this post</a> for printing or future reference.</p>
</blockquote>
<section id="problem-statement" class="level2">
<h2 class="anchored" data-anchor-id="problem-statement">Problem statement</h2>
<p>Consider the simplest possible problem in Newtonian kinematics: the motion of a point along a straight line. Its position, velocity and acceleration are denoted by <img src="https://latex.codecogs.com/png.latex?x(t),%20v(t)"> and <img src="https://latex.codecogs.com/png.latex?a(t)">, respectively. The equations of motion are elementary:</p>
<p><span id="eq-eq1"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Balign%7D%0A%5Cdot%7Bx%7D(t)&amp;=v(t)%5C%5C%0A%5Cddot%7Bx%7D(t)&amp;=%5Cdot%7Bv%7D(t)=a(t)%0A%5Cend%7Balign%7D%0A%5Cright.%0A%5Ctag%7B1%7D"></span></p>
<p>Here and throughout the post, the dot notation denotes differentiation with respect to time.</p>
<p>If the acceleration is known as a deterministic function of time, the solution is trivial: integrate once to obtain the velocity, and integrate again to obtain the position, starting from known initial conditions.</p>
<p>In many practical problems, however, acceleration is unknown. Examples abound:</p>
<ul>
<li>a vehicle subject to unpredictable maneuvers,</li>
<li>a moving object affected by small, unmodeled disturbances,</li>
<li>a target whose motion can only be described approximately.</li>
</ul>
<p>In all these cases, acceleration must be modeled, rather than specified explicitly. This raises the apparently simple question of how the acceleration should be represented.</p>
<p>At first sight, the answer seems obvious: treat acceleration as a stochastic process, that is, specify the statistical rules governing its time evolution. More precisely, acceleration can often be described as the superposition of deterministic and stochastic components. For instance, a known nominal motion—such as constant velocity or constant acceleration—may be affected by unpredictable perturbations. In this post, the focus is on the stochastic component alone, in order to isolate and understand its effect on the statistical properties of position (and velocity).</p>
<p>Even when the equations of motion are trivial, the modeling choices for the input <img src="https://latex.codecogs.com/png.latex?a(t)"> have profound consequences — especially when we recall that, while the motion of the point naturally occurs in continuous time, in practice we study it in discrete-time:</p>
<ul>
<li>measurement data are sampled,</li>
<li>tracking algorithms operate on sequences rather than continuous trajectories,</li>
<li>computer simulations are run with a fixed time step.</li>
</ul>
<p>When the input to the system is deterministic, this passage is straightforward. However, when the input is stochastic, the transition from continuous time to discrete time can become surprisingly subtle.</p>
<p>This post is about understanding what it really means to discretize Equation&nbsp;1 when <img src="https://latex.codecogs.com/png.latex?a(t)"> is modeled as a stochastic process. The goal of this post is to show that, while the physical problem is simple, the associated modeling choices are not trivial.</p>
</section>
<section id="the-motion-model" class="level2">
<h2 class="anchored" data-anchor-id="the-motion-model">The motion model</h2>
<p>Before introducing any stochastic description, it is useful to clarify what is given and what is unknown in the motion model itself.</p>
<p>The kinematic structure of the problem is completely known. The relationships between position, velocity, and acceleration are fixed by Newtonian kinematics, as shown in Equation&nbsp;1. From a systems point of view, <img src="https://latex.codecogs.com/png.latex?a(t)"> is not treated as a state variable, but as an external input to an otherwise deterministic system.</p>
<p>Because velocity and position are obtained by integrating acceleration, any assumption made on the acceleration propagates through the system. Small differences in how acceleration is modeled can result in large differences in the statistical properties of position and velocity, once the model is discretized.</p>
<p>The state of the system described by Equation&nbsp;1 is defined as:</p>
<p><span id="eq-eq2"><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7Bx%7D(t)%20%5Ctriangleq%20%5Cbegin%7Bbmatrix%7D%20x(t)%20%5C%5C%20v(t)%20%5Cend%7Bbmatrix%7D%0A%5Ctag%7B2%7D"></span></p>
<p>The continuous-time kinematic model is:</p>
<p><span id="eq-eq3"><img src="https://latex.codecogs.com/png.latex?%0A%5Cdot%7B%5Cmathbf%7Bx%7D%7D(t)%20=%20A%5C,%5Cmathbf%7Bx%7D(t)%20+%20B%5C,a(t)%0A%5Ctag%7B3%7D"></span></p>
<p>with:</p>
<p><span id="eq-eq4"><img src="https://latex.codecogs.com/png.latex?%0AA=%5Cbegin%7Bbmatrix%7D0&amp;1%5C%5C0&amp;0%5Cend%7Bbmatrix%7D,%5C;%0AB=%5Cbegin%7Bbmatrix%7D0%5C%5C1%5Cend%7Bbmatrix%7D%0A%5Ctag%7B4%7D"></span></p>
<p>Consider sampling the motion at times <img src="https://latex.codecogs.com/png.latex?t_k%20=%20kT">, with sampling interval <img src="https://latex.codecogs.com/png.latex?T">. For any input <img src="https://latex.codecogs.com/png.latex?a(t)"> (deterministic or stochastic), the exact solution at the sampling instants <img src="https://latex.codecogs.com/png.latex?t_k=kT"> is:</p>
<p><span id="eq-eq5"><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7Bx%7D_%7Bk+1%7D%20=%20e%5E%7BAT%7D%5C,%5Cmathbf%7Bx%7D_k%20+%20%5Cmathbf%7Bu%7D_k,%0A%5Ctag%7B5%7D"></span></p>
<p>where the state transition matrix is</p>
<p><span id="eq-eq6"><img src="https://latex.codecogs.com/png.latex?%0Ae%5E%7BAT%7D%20=%20%5Cbegin%7Bbmatrix%7D%0A1%20&amp;%20T%5C%5C%0A0%20&amp;%201%0A%5Cend%7Bbmatrix%7D%0A%5Ctag%7B6%7D"></span></p>
<p>and the input vector <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D_k"> can be written:</p>
<p><span id="eq-eq7"><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7Bu%7D_k%20=%20%5Cint_%7B0%7D%5E%7BT%7D%20e%5E%7BA(T-%5Ctau)%7D%5C,B%5C,a(t_k+%5Ctau)%5C,d%5Ctau%0A%5Ctag%7B7%7D"></span></p>
<p>This is a standard result in systems theory: discretization affects the deterministic state transition through the matrix exponential <img src="https://latex.codecogs.com/png.latex?e%5E%7BAT%7D">, while the input contributes through an integral that “accumulates” its effect over the sampling interval.</p>
<p>It is worth noting that the discrete-time representation in Equation&nbsp;5, with <img src="https://latex.codecogs.com/png.latex?e%5E%7BAT%7D"> as in Equation&nbsp;6 and <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D_k"> as in Equation&nbsp;7, is exact at the sampling times <img src="https://latex.codecogs.com/png.latex?kT"> and holds regardless of how acceleration is modeled. What is still unspecified is the statistical characterization of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D_k">, which depends entirely on the modeling assumptions made for <img src="https://latex.codecogs.com/png.latex?a(t)">.</p>
<p>Different assumptions on the time evolution of acceleration (for instance, whether it is memoryless or correlated) lead to different statistical properties of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D_k">, and therefore to different statistical properties of the state <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bx%7D_k"> at the sampling times. In particular, the position component <img src="https://latex.codecogs.com/png.latex?x_k"> is obtained by integrating acceleration twice (via velocity). As a result, the statistical behavior of position is highly sensitive to how acceleration is modeled. In many applications, position is the central quantity of interest: we care about predicting where the point will be, often more than how fast it will be moving.</p>
<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">
A note on the input integral and its interpretation
</div>
</div>
<div class="callout-body-container callout-body">
<p>The term <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D_k"> collects the effect of acceleration over the sampling interval. The integral in Equation&nbsp;7 can be understood in the most basic sense: as a Riemann integral of a time-varying input. To give this integral a concrete meaning, it is sufficient to assume that the acceleration process is locally piecewise constant. More precisely, we assume that <img src="https://latex.codecogs.com/png.latex?a(t)"> is constant over each subinterval of an arbitrary partition of the integration interval <img src="https://latex.codecogs.com/png.latex?(0,T)">. This assumption is made with respect to the integration variable and does not impose any restriction tied to the sampling interval <img src="https://latex.codecogs.com/png.latex?T">.</p>
<p>At this level, <img src="https://latex.codecogs.com/png.latex?T"> plays no special role beyond defining the limits of integration. There is no implicit connection with sampling theory in the sense of Shannon, nor with any bandwidth limitation on <img src="https://latex.codecogs.com/png.latex?a(t)">. The piecewise-constant assumption should therefore be interpreted as a minimal modeling device that allows the effect of acceleration over a finite time interval to be accumulated through integration.</p>
</div>
</div>
</section>
<section id="the-discrete-time-input-covariance-q_d" class="level2">
<h2 class="anchored" data-anchor-id="the-discrete-time-input-covariance-q_d">The discrete-time input covariance <img src="https://latex.codecogs.com/png.latex?Q_d"></h2>
<p>A specific stochastic model is now introduced: acceleration <img src="https://latex.codecogs.com/png.latex?a(t)"> is regarded as a wide-sense stationary stochastic process with zero mean, <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5Ba(t)%5D=0">, and autocorrelation function:</p>
<p><span id="eq-eq8"><img src="https://latex.codecogs.com/png.latex?%0AR_a(%5Ctau)%20%5C;%5Ctriangleq%5C;%20%5Cmathbb%7BE%7D%5C!%5Cleft%5Ba(t)%5C,a(t+%5Ctau)%5Cright%5D%0A%5Ctag%7B8%7D"></span></p>
<p>In the constant-velocity kinematic model, a zero-mean acceleration describes random deviations around a nominal constant velocity, with no preferred direction of motion.</p>
<p>The covariance matrix of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D_k"> is defined as</p>
<p><span id="eq-eq9"><img src="https://latex.codecogs.com/png.latex?%0AQ_d%5C;%5Ctriangleq%5C;%5Cmathrm%7BCov%7D(%5Cmathbf%7Bu%7D_k)%0A%20%20%20%20%20%20%20%20%20%20%20%5C;=%5C;%5Cmathbb%7BE%7D%5C!%5Cleft%5B%5Cmathbf%7Bu%7D_k%5C,%5Cmathbf%7Bu%7D_k%5E%5Ctop%5Cright%5D%0A%5Ctag%7B9%7D"></span></p>
<p>where the last equality requires that <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BE%7D%5B%5Cmathbf%7Bu%7D_k%5D=%5Cmathbf%7B0%7D"> (this follows from the linearity of both the expectation operator and the integral).</p>
<p>Substituting the expression for <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D_k"> in Equation&nbsp;7 into Equation&nbsp;9 yields:</p>
<p><span id="eq-eq10"><img src="https://latex.codecogs.com/png.latex?%0AQ_d%20=%20%5Cint_0%5ET%20%5C!%5C!%5Cint_0%5ET%0Ae%5E%7BA(T-%5Ctau)%7D%5C,B%5C;%0A%5Cmathbb%7BE%7D%5C!%5Cleft%5Ba(t_k+%5Ctau)%5C,a(t_k+s)%5Cright%5D%5C;%0AB%5E%5Ctop%20e%5E%7BA%5E%5Ctop(T-s)%7D%5C,%20d%5Ctau%5C,ds.%0A%5Ctag%7B10%7D"></span></p>
<p>Under the stationarity assumption, the expectation depends only on <img src="https://latex.codecogs.com/png.latex?%5Ctau-s">, and we obtain:</p>
<p><span id="eq-eq11"><img src="https://latex.codecogs.com/png.latex?%0AQ_d%20=%20%5Cint_0%5ET%20%5C!%5C!%5Cint_0%5ET%0Ae%5E%7BA(T-%5Ctau)%7D%5C,B%5C;%0AR_a(%5Ctau-s)%5C;%0AB%5E%5Ctop%20e%5E%7BA%5E%5Ctop(T-s)%7D%5C,%20d%5Ctau%5C,ds.%0A%5Ctag%7B11%7D"></span></p>
<p>This expression makes the central point explicit: the discrete-time covariance <img src="https://latex.codecogs.com/png.latex?Q_d"> is determined by the autocorrelation structure of acceleration over the sampling interval.</p>
<p>For the one-dimensional constant-velocity kinematic model, with <img src="https://latex.codecogs.com/png.latex?B"> and <img src="https://latex.codecogs.com/png.latex?e%5E%7BAT%7D"> as in Equation&nbsp;4 and Equation&nbsp;6 respectively, we can write:</p>
<p><span id="eq-eq12"><img src="https://latex.codecogs.com/png.latex?%0Ae%5E%7BA(T-%5Ctau)%7D%5C,B%20=%20%5Cbegin%7Bbmatrix%7D%20T-%5Ctau%5C%5C%201%20%5Cend%7Bbmatrix%7D%0A%5Ctag%7B12%7D"></span></p>
<p>As a result, the discrete-time covariance matrix can be written explicitly as</p>
<p><span id="eq-eq13"><img src="https://latex.codecogs.com/png.latex?%0AQ_d%20=%20%5Cint_0%5ET%5C!%5C!%5Cint_0%5ET%20%5Cbegin%7Bbmatrix%7D%20T-%5Ctau%5C%5C%201%20%5Cend%7Bbmatrix%7D%0AR_a(%5Ctau-s)%0A%5Cbegin%7Bbmatrix%7D%20T-s%20&amp;%201%20%5Cend%7Bbmatrix%7D%20%5C,d%5Ctau%5C,ds%0A%5Ctag%7B13%7D"></span></p>
<p>Written component-wise, this expression highlights how each entry of <img src="https://latex.codecogs.com/png.latex?Q_d"> is obtained by weighting the autocorrelation function of acceleration with deterministic kernels:</p>
<p><span id="eq-eq14"><img src="https://latex.codecogs.com/png.latex?%0AQ_d%20=%0A%5Cbegin%7Bbmatrix%7D%0A%5Cdisplaystyle%20%5Cint_0%5ET%5C!%5C!%5Cint_0%5ET%20(T-%5Ctau)(T-s)%5C,R_a(%5Ctau-s)%5C,d%5Ctau%5C,ds%0A&amp;%0A%5Cdisplaystyle%20%5Cint_0%5ET%5C!%5C!%5Cint_0%5ET%20(T-%5Ctau)%5C,R_a(%5Ctau-s)%5C,d%5Ctau%5C,ds%0A%5C%5C%5B0.6em%5D%0A%5Cdisplaystyle%20%5Cint_0%5ET%5C!%5C!%5Cint_0%5ET%20(T-s)%5C,R_a(%5Ctau-s)%5C,d%5Ctau%5C,ds%0A&amp;%0A%5Cdisplaystyle%20%5Cint_0%5ET%5C!%5C!%5Cint_0%5ET%20R_a(%5Ctau-s)%5C,d%5Ctau%5C,ds%0A%5Cend%7Bbmatrix%7D%0A%5Ctag%7B14%7D"></span></p>
<p>At this point, no assumption has yet been made on the specific form of <img src="https://latex.codecogs.com/png.latex?R_a(%5Ccdot)">. The structure of <img src="https://latex.codecogs.com/png.latex?Q_d"> is entirely determined by the kinematics; the autocorrelation function of acceleration determines its numerical values.</p>
</section>
<section id="white-noise-acceleration-as-a-limiting-model" class="level2">
<h2 class="anchored" data-anchor-id="white-noise-acceleration-as-a-limiting-model">White noise acceleration as a limiting model</h2>
<p>We now consider a specific, and widely used, stochastic model for acceleration: <em>white noise acceleration</em>. Formally, acceleration is modeled as a zero-mean stochastic process with autocorrelation</p>
<p><span id="eq-eq15"><img src="https://latex.codecogs.com/png.latex?%0AR_a(%5Ctau)%20=%20q%5C,%5Cdelta(%5Ctau)%0A%5Ctag%7B15%7D"></span></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cdelta(%5Ccdot)"> denotes the Dirac delta and the noise strength <img src="https://latex.codecogs.com/png.latex?q"> is a constant. This expression should not be interpreted literally as a function: white noise does not exist as a time function in the usual sense. Rather, it is a mathematical idealization that captures the idea of rapidly varying, uncorrelated fluctuations. What matters for our purposes is that the autocorrelation is impulsive, and that all second-order properties of the process are encoded in the single parameter <img src="https://latex.codecogs.com/png.latex?q">.</p>
<p>The parameter <img src="https://latex.codecogs.com/png.latex?q"> has a clear physical dimension. Acceleration has units of length over time squared, <img src="https://latex.codecogs.com/png.latex?%5Ba%5D%20=%20%5Cmathrm%7BL%7D/%5Cmathrm%7BT%7D%5E2">, the Dirac delta has units of inverse time, and the autocorrelation has units <img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7BL%7D%5E2/%5Cmathrm%7BT%7D%5E4">. It follows that <img src="https://latex.codecogs.com/png.latex?%5Bq%5D%20=%20%5Cmathrm%7BL%7D%5E2/%5Cmathrm%7BT%7D%5E3."> This dimensional analysis makes explicit that <img src="https://latex.codecogs.com/png.latex?q"> is the (constant) power spectral density (PSD) of the white noise acceleration.</p>
<p>Substituting Equation&nbsp;15 into Equation&nbsp;14, each double integral defining <img src="https://latex.codecogs.com/png.latex?Q_d"> collapses to a single integral:</p>
<p><span id="eq-eq16"><img src="https://latex.codecogs.com/png.latex?%0AQ_d%0A=%0Aq%20%5Cint_0%5ET%0A%5Cbegin%7Bbmatrix%7D%0A(T-%5Ctau)%5E2%20&amp;%20T-%5Ctau%5C%5C%0AT-%5Ctau%20&amp;%201%0A%5Cend%7Bbmatrix%7D%0A%5C,d%5Ctau.%0A%5Ctag%7B16%7D"></span></p>
<p>Evaluating the integral yields the well-known result:</p>
<p><span id="eq-eq17"><img src="https://latex.codecogs.com/png.latex?%0AQ_d%20=%0Aq%5Cbegin%7Bbmatrix%7D%0AT%5E3/3%20&amp;%20T%5E2/2%20%5C%5C%0AT%5E2/2%20&amp;%20T%0A%5Cend%7Bbmatrix%7D.%0A%5Ctag%7B17%7D"></span></p>
<p>This matrix summarizes, in discrete time, the cumulative effect of continuous-time white acceleration over one sampling interval.</p>
<p>It is worth stressing that the discrete-time covariance <img src="https://latex.codecogs.com/png.latex?Q_d"> depends not only on the noise PSD, but also explicitly on the sampling interval <img src="https://latex.codecogs.com/png.latex?T">. Changing <img src="https://latex.codecogs.com/png.latex?T"> without recomputing <img src="https://latex.codecogs.com/png.latex?Q_d"> therefore amounts to changing the underlying model, not merely its numerical implementation.</p>
<section id="on-simulation-and-sample-paths" class="level3">
<h3 class="anchored" data-anchor-id="on-simulation-and-sample-paths">On simulation and sample paths</h3>
<p>Although white noise acceleration is a convenient analytical model, it cannot be simulated directly as a time function. To generate consistent sample paths, one must work with a time-integrated process. In practice, this amounts to simulating independent increments whose variance scales with the time step, in the same spirit as the construction of the Wiener process. These increments are assumed to be Gaussian, and position and velocity trajectories are obtained by integrating them, rather than by sampling acceleration itself. Strictly speaking, the Gaussian assumption is not essential, but it provides a convenient and widely used framework; more generally, a normal joint distribution for position and velocity may arise as a limiting approximation via the central limit theorem.</p>
<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">
Generating correlated increments via a linear transformation
</div>
</div>
<div class="callout-body-container callout-body">
<p>In the discrete-time model Equation&nbsp;5, the random vector <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D_k"> represents the integrated effect of acceleration over one sampling interval. It is a two-dimensional Gaussian vector, with zero mean and covariance <img src="https://latex.codecogs.com/png.latex?Q_d">.</p>
<p>A convenient way to generate <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D_k"> is to start from a vector of independent standard Gaussian variables, <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bz%7D_k%20%5Csim%20%5Cmathcal%7BN%7D(%5Cmathbf%7B0%7D,%20I)">, and apply the linear transformation</p>
<p><span id="eq-eq18"><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7Bu%7D_k%20=%20L%5C,%5Cmathbf%7Bz%7D_k,%0A%5Cqquad%0AQ_d%20=%20L%5C,L%5E%5Ctop.%0A%5Ctag%7B18%7D"></span></p>
<p>This construction makes the role of the transformation explicit: the matrix <img src="https://latex.codecogs.com/png.latex?L"> introduces both the correct scaling and the correlation structure between the components of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D_k">. In practice, <img src="https://latex.codecogs.com/png.latex?L"> can be obtained via a Cholesky factorization of <img src="https://latex.codecogs.com/png.latex?Q_d">, but the key idea is the linear mapping from independent to correlated random variables. In the example discussed in this post, the matrix <img src="https://latex.codecogs.com/png.latex?L"> can be written as follows: <span id="eq-eq19"><img src="https://latex.codecogs.com/png.latex?%0AL%20=%20%5Csqrt%7Bq%5C,T%7D%0A%5Cbegin%7Bbmatrix%7D%20%5Cdfrac%7BT%7D%7B2%5Csqrt%7B3%7D%7D%20&amp;%20%5Cdfrac%7BT%7D%7B2%7D%20%5C%5C%200%20&amp;%201%20%5Cend%7Bbmatrix%7D%0A%5Ctag%7B19%7D"></span></p>
</div>
</div>
</section>
<section id="a-conceptual-remark-on-dimensions-and-modeling" class="level3">
<h3 class="anchored" data-anchor-id="a-conceptual-remark-on-dimensions-and-modeling">A conceptual remark on dimensions and modeling</h3>
<p>It is worth pausing for a moment to reflect on what has just happened.</p>
<p>We started in continuous time with a system driven by an acceleration input, whose physical dimension is <img src="https://latex.codecogs.com/png.latex?%5B%5Cmathrm%7BL%7D%5C,%5Cmathrm%7BT%7D%5E%7B-2%7D%5D">. After discretization, we end up with a two-dimensional input (<img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D%5C_k">) whose components have dimensions <img src="https://latex.codecogs.com/png.latex?%5B%5Cmathrm%7BL%7D%5D"> and <img src="https://latex.codecogs.com/png.latex?%5B%5Cmathrm%7BL%7D%5C,%5Cmathrm%7BT%7D%5E%7B-1%7D%5D">, corresponding to increments of position and velocity.</p>
<p>This is not a contradiction, but a direct consequence of integration over time. In the discrete-time model, the input no longer represents an instantaneous physical quantity, but the cumulative effect of acceleration over a finite interval.</p>
<p>What is particularly noteworthy is that these two components are correlated. They are statistically “glued together” through the covariance matrix <img src="https://latex.codecogs.com/png.latex?Q_d">, which encodes how uncertainty injected through acceleration propagates jointly into position and velocity during a single time step.</p>
<p>In this sense, discretization transforms a scalar continuous-time acceleration input into a vector-valued discrete-time input with coupled components and nontrivial physical units. Understanding this transformation is essential to avoid misinterpreting the role of process noise in discrete-time motion models.</p>
</section>
<section id="a-final-remark-on-the-role-of-the-sampling-interval" class="level3">
<h3 class="anchored" data-anchor-id="a-final-remark-on-the-role-of-the-sampling-interval">A final remark on the role of the sampling interval</h3>
<p>A final comment concerns the role of the sampling interval <img src="https://latex.codecogs.com/png.latex?T">. In the present context, <img src="https://latex.codecogs.com/png.latex?T"> should be interpreted primarily as an integration step. The discrete-time model is exact at the sampling times, and the construction of the random input <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D_k"> guarantees that the resulting position and velocity sample paths have the correct second-order statistics, regardless of the specific value of <img src="https://latex.codecogs.com/png.latex?T">, within broad limits dictated by the validity of the underlying continuous-time model.</p>
<p>This is different from what happens in a Kalman filtering or tracking context, where <img src="https://latex.codecogs.com/png.latex?T"> determines how long the system must evolve “open-loop” based on the motion model alone, between successive measurements. Here, no estimation is being performed, and no measurements are involved.</p>
<p>As a result, <img src="https://latex.codecogs.com/png.latex?T"> does not control prediction horizon or estimator performance, but simply sets the time scale over which the continuous-time stochastic input is integrated. As long as the discrete-time covariance <img src="https://latex.codecogs.com/png.latex?Q_d"> is constructed consistently, changing <img src="https://latex.codecogs.com/png.latex?T"> does not introduce integration errors: it only changes how uncertainty accumulates over time.</p>
<p>It is also important to stress what this construction does — and does not — provide. Nothing can be said about what happens between sampling times. The continuous-time trajectory is not reconstructed, nor is it assumed to be known in any meaningful sense. What is guaranteed, instead, is that the stochastic process is characterized exactly at the sampling times. Moreover, in the Gaussian case considered here, the statistical behavior of position and velocity at discrete times is fully described by their mean and covariance.</p>
<p>In this sense, the discrete-time model provides a complete and exact statistical description of the motion at sampling times, even though the underlying continuous-time path remains unknown. Strictly speaking, <img src="https://latex.codecogs.com/png.latex?Q_d"> should not be thought of as a generic hyperparameter to be tuned (e.g., in a Kalman-based tracker), but as a quantity implied by the chosen stochastic model for acceleration — even though, in practice, it is often used as a tool for absorbing the effects of unmodeled dynamics.</p>
<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">
Two difference equations, one stochastic model?
</div>
</div>
<div class="callout-body-container callout-body">
<p>A natural question arises at this point; a related discussion can be found in one <a href="https://amsabatini.netlify.app/posts/numerical_simulation/">previous post</a> of mine. If white noise acceleration is written in continuous time as a stochastic differential equation, we have:</p>
<p><span id="eq-eq20"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Balign%7D%0Adx(t)&amp;=v(t)%5C,dt%5C%5C%0Adv(t)&amp;=%5Csqrt%7Bq%7D%5C,dW(t)%0A%5Cend%7Balign%7D%0A%5Cright.%0A%5Ctag%7B20%7D"></span></p>
<p>then the Euler–Maruyama (EM) scheme on a fine grid with step <img src="https://latex.codecogs.com/png.latex?h"> reads</p>
<p><span id="eq-eq21"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Balign%7D%0Ax_%7Bn+1%7D&amp;=x_n+h%5C,v_n%5C%5C%0Av_%7Bn+1%7D&amp;=v_n+%5Csqrt%7Bq%7D%5C,%5Csqrt%7Bh%7D%5C,%5Cxi_n%5Cqquad%20%5Cxi_n%20%5Csim%20%5Cmathcal%20N(0,1)%0A%5Cend%7Balign%7D%0A%5Cright.%0A%5Ctag%7B21%7D"></span></p>
<p>At first sight, this update looks structurally different from that in Equation&nbsp;5: the noise appears explicitly only in the velocity equation, while in the exact discrete-time model randomness enters both position and velocity through <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bu%7D_k">.</p>
<p>The apparent mismatch disappears once one recognizes that the two constructions address different objects.</p>
<ul>
<li>EM is a pathwise approximation method: it builds an approximate continuous-time trajectory on a refined grid. Under standard regularity conditions (satisfied here, since the model is linear), EM converges with strong order <img src="https://latex.codecogs.com/png.latex?1/2"> (mean-square or pathwise accuracy) and with weak order <img src="https://latex.codecogs.com/png.latex?1"> (accuracy in the distribution of smooth functionals).</li>
<li>The exact discrete-time model does not approximate trajectories between samples. It constructs directly the correct joint distribution of <img src="https://latex.codecogs.com/png.latex?(x(t_k),v(t_k))">, by integrating the stochastic input over a finite interval of length <img src="https://latex.codecogs.com/png.latex?T">.</li>
</ul>
<p>As a consequence, the two difference equations are not expected to coincide in form. What can be reconciled is their statistical output at fixed times: if EM is run with a sufficiently small step <img src="https://latex.codecogs.com/png.latex?h"> and the resulting trajectory is inspected only at the coarse sampling times <img src="https://latex.codecogs.com/png.latex?t_k=kT"> (with <img src="https://latex.codecogs.com/png.latex?T"> an integer multiple of <img src="https://latex.codecogs.com/png.latex?h">), then the distribution of the EM state <img src="https://latex.codecogs.com/png.latex?(x_k,v_k)"> converges (as <img src="https://latex.codecogs.com/png.latex?h%5Cto0">) to the same distribution implied by the exact discrete-time covariance <img src="https://latex.codecogs.com/png.latex?Q_d">. The covariance <img src="https://latex.codecogs.com/png.latex?P_k"> of the EM state at the sampling times can be calculated by solving:</p>
<p><span id="eq-eq22"><img src="https://latex.codecogs.com/png.latex?%0AP_%7Bk+1%7D=e%5E%7BAT%7DP_k(e%5E%7BAT%7D)%5E%5Ctop+Q_d%0A%5Ctag%7B22%7D"></span></p>
<p>In this sense, EM converges to the correct model in distribution, even though its local update equation looks different.</p>
</div>
</div>
</section>
</section>
<section id="concluding-remarks" class="level2">
<h2 class="anchored" data-anchor-id="concluding-remarks">Concluding remarks</h2>
<p>The goal of this post was not to provide an exhaustive discussion of stochastic acceleration models, nor to explore the interpretation of white noise as the limit of rapidly fluctuating correlated processes.</p>
<p>Rather, the focus was on understanding how a simple kinematic model, when driven by a stochastic input, can be consistently carried from continuous time to discrete time, and how the statistical properties of that input propagate into the discrete-time state increments.</p>
<p>Within this framework, white acceleration was introduced as a convenient and widely used specialization that leads to a closed-form expression for the discrete-time covariance <img src="https://latex.codecogs.com/png.latex?Q_d">. It should be regarded as one modeling choice among others, not as a physical description of acceleration itself.</p>
<p>What matters, ultimately, is not the specific choice of model, but the clarity with which its assumptions are stated and their consequences understood. Even for the simplest possible motion problem, careful modeling at this level can make a substantial difference.</p>


</section>

 ]]></description>
  <category>stochastic modeling</category>
  <guid>https://amsabatini.netlify.app/posts/stochastic_model/</guid>
  <pubDate>Sun, 01 Feb 2026 23:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/stochastic_model/simulation.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>PCA, MANOVA, and the geometry of multivariate comparison</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/manova/</link>
  <description><![CDATA[ 




<blockquote class="blockquote">
<p>💾 <strong>Prefer reading offline?</strong> You can <a href="manova.pdf">download a PDF version of this post</a> for printing or future reference.</p>
</blockquote>
<section id="why-multivariate-thinking-is-important" class="level2">
<h2 class="anchored" data-anchor-id="why-multivariate-thinking-is-important">Why multivariate thinking is important</h2>
<p>In many experimental settings, statistical analysis still proceeds one response variable (outcome) at a time. Group differences are assessed through a sequence of tests applied to single outcomes, implicitly assuming that each carries independent and self-contained information. This approach is widely regarded as inadequate when variables are numerous and correlated, as is often the case in empirical data drawn from natural, social, or cultural processes.</p>
<p>Multivariate statistics addresses this limitation by shifting the focus from individual outcomes to configurations of outcomes. Rather than asking whether a single response differs across groups, the central question becomes whether groups occupy distinct regions of a multidimensional space, and how such separation relates to the variability observed within each group.</p>
<p>From this perspective, multivariate methods are less about hypothesis testing in the narrow sense, and more about geometry. Concepts such as centroids, dispersion, and variance–covariance structure characterize the data before any formal test is performed. Understanding what a given method assesses therefore requires understanding the space in which the data are represented.</p>
<p>This post focuses on one widely used approach to multivariate group comparison: Multivariate Analysis of Variance (MANOVA). The aim here is to clarify what the method actually measures, under which conditions it is informative, and how it can be used responsibly in exploratory analyses, either directly or after Principal Component Analysis (PCA).</p>
</section>
<section id="a-pedagogical-example-for-multivariate-reasoning" class="level2">
<h2 class="anchored" data-anchor-id="a-pedagogical-example-for-multivariate-reasoning">A pedagogical example for multivariate reasoning</h2>
<p>To ground the discussion, it is useful to start from a simple and well-understood example. The iris dataset is a canonical choice in this respect, not because it is representative of real-world complexity, but because it allows key geometric ideas to be illustrated without unnecessary distractions.</p>
<p>The dataset consists of 150 observations from three biological species (<em>Iris setosa</em>, <em>Iris versicolor</em>, <em>Iris virginica</em>). Each observation is described by four continuous measurements: sepal length, sepal width, petal length, and petal width.</p>
<p>While iris is often used to demonstrate classification performance, it serves a different purpose here: providing a controlled environment in which the relationship between data representation, group separation, and variability can be examined explicitly.</p>
</section>
<section id="principal-component-analysis-as-a-geometric-transformation" class="level2">
<h2 class="anchored" data-anchor-id="principal-component-analysis-as-a-geometric-transformation">Principal component analysis as a geometric transformation</h2>
<p>Principal component analysis (PCA) is often introduced as a dimensionality reduction technique. While this description is not incorrect, it can be misleading if taken at face value. Conceptually, PCA is best understood as a change of coordinates: the original variables are replaced by a new set of orthogonal axes that capture maximal variance.</p>
<p>In the context of multivariate group comparison, PCA can play a crucial preparatory role. By projecting the data into a lower-dimensional space that preserves most of the variance, it becomes easier to visualize group structure and to reason about distances and dispersion.</p>
<p>Importantly, PCA does not create separation between groups. Any apparent separation observed in principal component space reflects structure already present in the data. PCA merely provides a coordinate system in which that structure can be more easily inspected.</p>
<section id="what-pca-actually-does" class="level3">
<h3 class="anchored" data-anchor-id="what-pca-actually-does">What PCA actually does</h3>
<p>In R, principal component analysis is commonly performed using the function <code>prcomp()</code> from the <code>stats</code> package. While its usage is straightforward, it is worth recalling what this function implicitly assumes about the data and how its output should be interpreted.</p>
<p>First, <code>prcomp()</code> operates on a numeric matrix and expects variables to be measured on a comparable scale. When variables differ substantially in variance or units, centering and scaling are usually required. In the examples discussed here, variables are centered and scaled prior to PCA in order to prevent components from being dominated by features with larger numerical ranges.</p>
<p>Second, <code>prcomp()</code> performs a singular value decomposition of the centered (and optionally scaled) data matrix. No distributional assumptions are made at this stage: PCA is a purely geometric transformation. Normality, when invoked later, concerns the distribution of component scores rather than the original variables.</p>
<p>The output of <code>prcomp()</code> consists of two complementary objects: scores and loadings. These play distinct roles and are often conflated in applied work.</p>
<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">
Scores and loadings: two sides of the same transformation
</div>
</div>
<div class="callout-body-container callout-body">
<p>Principal component scores represent the projection of observations onto the new coordinate system defined by the principal components. They locate each observation in principal component space and are the quantities used for visualization, clustering, and group comparison.</p>
<p>Principal component loadings, by contrast, describe how the original variables contribute to each component. They define the orientation of the new axes with respect to the original feature space.</p>
<p>In multivariate group comparisons, scores are the primary objects of interest, as they encode the relative positions of observations. Loadings become relevant at a later stage, when the goal is to interpret the components in terms of the original variables.</p>
</div>
</div>
<p>Throughout this post, scatterplots of principal components refer to plots of component scores. Such plots visualize the relative positions of observations in reduced-dimensional space and should not be interpreted as representations of variable contributions, which are instead encoded in the loadings.</p>
</section>
<section id="a-first-look-at-principal-component-space" class="level3">
<h3 class="anchored" data-anchor-id="a-first-look-at-principal-component-space">A first look at principal component space</h3>
<p>As shown in Figure&nbsp;1, the three species occupy partially overlapping yet distinct regions of principal component space.</p>
<div class="cell" data-code-label="pca-iris">
<div class="cell-output-display">
<div id="fig-iris-pca-scores" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-iris-pca-scores-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/manova/index_files/figure-html/fig-iris-pca-scores-1.png" class="img-fluid figure-img" width="576">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-iris-pca-scores-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Iris dataset projected onto the first two principal components.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="explained-variance-and-dimensionality" class="level3">
<h3 class="anchored" data-anchor-id="explained-variance-and-dimensionality">Explained variance and dimensionality</h3>
<p>Principal components are ordered by decreasing variance. Reporting the proportion of variance explained is useful to justify why a low-dimensional projection (typically the first two components) is an informative geometric summary.</p>
<p>In this example, the first two components capture a substantial fraction of total variability, supporting their use for visualization and for subsequent multivariate comparisons in component space.</p>
<p>Table&nbsp;1 reports the proportion of variance explained by each component.</p>
<div class="cell" data-code-label="iris-pca-var">
<div id="tbl-iris-pca-var" class="cell quarto-float quarto-figure quarto-figure-center anchored" data-code-label="iris-pca-var">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl quarto-uncaptioned" id="tbl-iris-pca-var-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1
</figcaption>
<div aria-describedby="tbl-iris-pca-var-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="font-size: 14px; width: auto !important; margin-left: auto; margin-right: auto;">
<caption style="font-size: initial !important;">Explained variance and cumulative variance for the iris PCA.</caption>
 <thead>
  <tr>
   <th style="text-align:left;"> Component </th>
   <th style="text-align:right;"> Proportion of variance explained </th>
   <th style="text-align:right;"> Cumulative </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> PC1 </td>
   <td style="text-align:right;"> 0.730 </td>
   <td style="text-align:right;"> 0.730 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> PC2 </td>
   <td style="text-align:right;"> 0.229 </td>
   <td style="text-align:right;"> 0.958 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> PC3 </td>
   <td style="text-align:right;"> 0.037 </td>
   <td style="text-align:right;"> 0.995 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> PC4 </td>
   <td style="text-align:right;"> 0.005 </td>
   <td style="text-align:right;"> 1.000 </td>
  </tr>
</tbody>
</table>

</div>
</div>
</figure>
</div>
</div>
<p>The scree plot in Figure&nbsp;2 provides an alternative view of the information reported in Table&nbsp;1.</p>
<div class="cell" data-code-label="iris-pca-scree">
<div class="cell-output-display">
<div id="fig-iris-pca-scree" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-iris-pca-scree-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/manova/index_files/figure-html/fig-iris-pca-scree-1.png" class="img-fluid figure-img" width="576">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-iris-pca-scree-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Scree plot showing the proportion of variance explained by each principal component.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="from-pca-to-multivariate-group-comparison" class="level3">
<h3 class="anchored" data-anchor-id="from-pca-to-multivariate-group-comparison">From PCA to multivariate group comparison</h3>
<p>Once the data have been projected into a low-dimensional principal component space, the next step is to assess whether groups differ in a multivariate sense. At this stage, the relevant question is no longer whether individual components differ across groups, but whether group centroids are separated relative to the variability observed within each group. This is where the MANOVA framework naturally fits in.</p>
<p>However, before considering MANOVA in principal component space, it is useful to recall the conventional formulation of the method, as it is typically introduced in applied tutorials.</p>
</section>
</section>
<section id="manova-assumptions-scope-and-interpretation" class="level2">
<h2 class="anchored" data-anchor-id="manova-assumptions-scope-and-interpretation">MANOVA: assumptions, scope, and interpretation</h2>
<p>Multivariate Analysis of Variance (MANOVA) extends the logic of univariate ANOVA to situations in which each observational unit is described by multiple response variables. Rather than testing group differences on individual outcomes one at a time, MANOVA evaluates whether groups differ in their multivariate location, taking into account the joint distribution of the responses.</p>
<section id="what-manova-actually-tests" class="level3">
<h3 class="anchored" data-anchor-id="what-manova-actually-tests">What MANOVA actually tests</h3>
<p>At its core, MANOVA addresses a geometric question. Groups are represented by centroids in a multidimensional response space, and the method assesses whether these centroids are separated relative to the variability observed within groups.</p>
<p>A common point of confusion arises when deciding whether a full multivariate procedure is necessary. One might reasonably ask why MANOVA should be preferred to a sequence of univariate ANOVAs, or to a single ANOVA applied to a composite or summary measure. The key distinction is that MANOVA explicitly incorporates the covariance structure among response variables. By doing so, it tests for patterns of group separation that may only emerge when variables are considered jointly, and that cannot be recovered from marginal analyses alone.</p>
</section>
<section id="manova-versus-univariate-anova" class="level3">
<h3 class="anchored" data-anchor-id="manova-versus-univariate-anova">MANOVA versus univariate ANOVA</h3>
<p>The distinction between MANOVA and univariate ANOVA is not merely technical, but conceptual. Univariate ANOVA evaluates group differences along a single response dimension at a time. Even when applied repeatedly to multiple outcomes, each test remains blind to the relationships among variables.</p>
<p>From a geometric standpoint, univariate ANOVA assesses separation along one predefined direction in response space. Each outcome defines its own axis, and group differences are evaluated independently along those axes. This approach implicitly assumes that relevant group structure can be detected through marginal comparisons, and that correlations among responses are either negligible or irrelevant.</p>
<p>MANOVA, by contrast, retains a genuinely multivariate perspective. Rather than focusing on individual response dimensions, it evaluates separation in the subspace jointly defined by the responses. Group differences are therefore allowed to emerge along arbitrary linear combinations of variables, not restricted to the original coordinate axes.</p>
<p>This distinction becomes especially important when group separation is not aligned with any single response variable. In such cases, marginal tests may fail to detect meaningful structure, even when groups are clearly separated in multivariate space. MANOVA is specifically designed to capture these patterns, as it evaluates differences in location relative to the joint covariance structure of the responses.</p>
</section>
<section id="assumptions-and-their-practical-role" class="level3">
<h3 class="anchored" data-anchor-id="assumptions-and-their-practical-role">Assumptions and their practical role</h3>
<p>Like all parametric methods, MANOVA relies on a set of assumptions concerning the distributional and structural properties of the data. These include multivariate normality of the response variables within groups, homogeneity of covariance matrices across groups, and independence of observations.</p>
<p>In practice, these assumptions are rarely met exactly. Their relevance therefore depends on the role assigned to the analysis. When MANOVA is used as a formal inferential procedure, substantial violations may compromise the validity of <img src="https://latex.codecogs.com/png.latex?p">-values and associated significance tests. In applied and exploratory settings, however, MANOVA is often employed as a descriptive and comparative tool, aimed at quantifying multivariate separation rather than at establishing strict inferential claims.</p>
<p>From this perspective, assumptions function less as rigid prerequisites and more as diagnostics guiding interpretation. Departures from multivariate normality or covariance homogeneity do not automatically invalidate the analysis, but they do affect how results should be read and reported. Emphasis shifts from binary decisions based on <img src="https://latex.codecogs.com/png.latex?p">-values to the magnitude and structure of multivariate separation.</p>
</section>
<section id="choice-of-test-statistics" class="level3">
<h3 class="anchored" data-anchor-id="choice-of-test-statistics">Choice of test statistics</h3>
<p>Several test statistics are available within the MANOVA framework, including Wilks’ lambda, Pillai’s trace, the Hotelling–Lawley trace, and Roy’s largest root. Although these statistics are asymptotically equivalent under ideal conditions, they differ in their sensitivity to assumption violations and to specific patterns of group separation.</p>
<p>Wilks’ lambda is perhaps the most widely reported statistic, but it is also among the most sensitive to departures from multivariate normality and to heterogeneity of covariance matrices. Roy’s largest root, by contrast, focuses exclusively on the strongest direction of separation and may overemphasize a single dominant effect.</p>
<p>Pillai’s trace is often recommended in applied and exploratory settings because of its relative robustness to violations of assumptions, particularly when group sizes are unequal or covariance matrices differ across groups. Rather than concentrating on a single dimension of separation, Pillai’s trace aggregates evidence across all response dimensions, providing a more stable summary of multivariate effects.</p>
<p>For this reason, when MANOVA is used primarily as a descriptive tool for assessing multivariate separation, emphasis should be placed on the overall pattern of results rather than on any single test statistic taken in isolation. Reporting Pillai’s trace alongside a clear description of the analytical context helps balance robustness and interpretability.</p>
</section>
<section id="post-hoc-analyses-and-interpretation" class="level3">
<h3 class="anchored" data-anchor-id="post-hoc-analyses-and-interpretation">Post hoc analyses and interpretation</h3>
<p>When a MANOVA indicates evidence of multivariate group separation, post hoc analyses are often performed to aid interpretation. These analyses typically involve examining group differences on individual response variables or on selected dimensions derived from the multivariate space.</p>
<p>It is important to emphasize that post hoc tests following MANOVA do not constitute independent confirmations of group differences. Rather, they serve an explanatory role, helping to identify which dimensions of the response space contribute most strongly to the observed multivariate separation.</p>
<p>In this sense, post hoc analyses should be interpreted descriptively rather than inferentially. Their purpose is not to re-test hypotheses already addressed by the multivariate procedure, but to provide insight into the structure of the effect detected at the multivariate level.</p>
<p>When MANOVA is applied in exploratory settings, post hoc results are therefore best viewed as complementary summaries that facilitate interpretation, visualization, and substantive discussion. Used in this way, they enrich the multivariate analysis without overstating the evidential weight of individual comparisons.</p>
<div class="callout callout-style-default callout-tip 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">
A conventional MANOVA workflow
</div>
</div>
<div class="callout-body-container callout-body">
<p>Applied tutorials often present MANOVA through a standard sequence of steps:<br>
(i) fit an omnibus MANOVA model on the original variables (without PCA),<br>
(ii) inspect assumptions diagnostically,<br>
(iii) report a multivariate test statistic (often including Pillai’s trace), and<br>
(iv) use follow-up analyses primarily for interpretation.</p>
<p>A well-known reference implementation of this workflow, based on the iris dataset and using the package <code>rstatix</code>, is provided by <a href="https://www.datanovia.com/en/lessons/one-way-manova-in-r/#google_vignette">Datanovia</a>. The goal here is not to reproduce that tutorial, but to summarize the logic of the pipeline in a compact and reusable form.</p>
<p>Table&nbsp;2 summarizes the assumptions typically discussed in rstatix-style MANOVA workflows, together with their practical role in applied analyses. Here, <em>n</em> denotes the number of observations per group, and <em>p</em> the number of response variables.</p>
<div id="tbl-manova-assumptions" class="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-manova-assumptions-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;2: Diagnostic checks commonly discussed in rstatix-style MANOVA workflows and their practical interpretation.
</figcaption>
<div aria-describedby="tbl-manova-assumptions-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="caption-top table">
<colgroup>
<col style="width: 24%">
<col style="width: 24%">
<col style="width: 24%">
<col style="width: 26%">
</colgroup>
<thead>
<tr class="header">
<th>Aspect assessed</th>
<th>Typical diagnostic</th>
<th>Common tool</th>
<th>Practical interpretation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Sample size</td>
<td>Check that <em>n</em> &gt; <em>p</em> per group</td>
<td>—</td>
<td>Small samples limit stability</td>
</tr>
<tr class="even">
<td>Independence</td>
<td>Study design</td>
<td>—</td>
<td>Violations invalidate inference</td>
</tr>
<tr class="odd">
<td>Outliers</td>
<td>Distance-based inspection</td>
<td><code>identify_outliers()</code> (rstatix)</td>
<td>Inspect, do not auto-remove</td>
</tr>
<tr class="even">
<td>Normality</td>
<td>Marginal / multivariate tests</td>
<td><code>shapiro_test()</code>, <code>mshapiro_test()</code> (rstatix)</td>
<td>Moderate deviations often tolerable</td>
</tr>
<tr class="odd">
<td>Multicollinearity</td>
<td>Correlation inspection</td>
<td><code>cor_test()</code> (rstatix)</td>
<td>Very high correlations problematic</td>
</tr>
<tr class="even">
<td>Linearity</td>
<td>Pairwise scatterplots</td>
<td><code>GGally::ggpairs()</code></td>
<td>Strong nonlinearities affect geometry</td>
</tr>
<tr class="odd">
<td>Covariance homogeneity</td>
<td>Box’s M test</td>
<td><code>box_m()</code> (rstatix)</td>
<td>Highly sensitive; interpret cautiously</td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
</div>
</div>
<p>In line with the Datanovia example, the iris dataset shows no major pathologies with respect to most diagnostic checks. In particular, sample size, independence, normality, and linearity assumptions are reasonably satisfied. As is often the case in practice, the only notable deviation concerns homogeneity of variance–covariance matrices, a common situation in applied settings and one that reinforces the use of robust multivariate statistics such as Pillai’s trace.</p>
</section>
<section id="back-to-principal-component-space" class="level3">
<h3 class="anchored" data-anchor-id="back-to-principal-component-space">Back to principal component space</h3>
<p>The discussion above treated MANOVA in its conventional formulation, applied directly to the original response variables. This clarifies what MANOVA tests and how multivariate separation is quantified.</p>
<p>In many applied settings, however, the original variables are numerous, correlated, or difficult to interpret individually. In such cases, it is often advantageous to work in a reduced-dimensional representation that preserves the main geometric structure of the data while simplifying interpretation. Principal component analysis re-enters here as a representational step.</p>
<p>A natural question arises at this point: should MANOVA-style diagnostics have been assessed already when PCA was performed?</p>
<p>The answer is largely negative. PCA, as implemented by <code>prcomp()</code>, is a purely geometric transformation based on a singular value decomposition of the data matrix. It does not rely on distributional assumptions, nor does it require normality, homogeneity of variances, or independence across groups.</p>
<p>For these reasons, diagnostic checks discussed in MANOVA workflows are appropriately revisited after projection into principal component space, when the scores are treated as inputs to a multivariate comparison. By reducing <em>p</em> (the number of response variables) to a small set of informative principal components, this choice ensures a more favorable <em>n/p</em> ratio, thereby enhancing stability and interpretability.</p>
</section>
</section>
<section id="manova-in-principal-component-space" class="level2">
<h2 class="anchored" data-anchor-id="manova-in-principal-component-space">MANOVA in principal component space</h2>
<p>With the conceptual and methodological groundwork in place, the combination of PCA and MANOVA can now be operationalized. In this setting, PCA defines a low-dimensional space that captures the dominant structure of the data. MANOVA is then applied to the corresponding component scores to quantify group separation within that space.</p>
<p>Operationally, this amounts to replacing the original response variables with a selected subset of principal component scores. These scores serve as multivariate responses in the MANOVA model, while group membership remains unchanged. The logic of the test is identical to that of a conventional MANOVA; what changes is the space in which multivariate separation is assessed.</p>
<p>This approach offers several practical advantages. By working with a reduced number of orthogonal components, the dimensionality of the response space is controlled explicitly, multicollinearity is eliminated by construction, and the ratio between the number of observations and response variables is improved. At the same time, because principal components are ordered by explained variance, the analysis can be restricted to those dimensions that capture the most informative aspects of the data.</p>
<p>In what follows, this procedure is illustrated on the iris dataset by applying MANOVA to the first two principal component scores. This example is not intended to optimize classification or prediction, but to demonstrate how multivariate separation can be measured and interpreted once the data have been embedded in principal component space.</p>
<section id="choosing-the-number-of-principal-components" class="level3">
<h3 class="anchored" data-anchor-id="choosing-the-number-of-principal-components">Choosing the number of principal components</h3>
<p>Applying MANOVA in principal component space requires an explicit decision about how many components to retain. This choice is not dictated by the MANOVA framework itself, but by considerations of representation and interpretability.</p>
<p>In practice, the goal is not to preserve all variability in the data, but to retain those dimensions that capture the dominant structure relevant for group comparison. Components explaining only a negligible fraction of total variance are unlikely to contribute meaningfully to multivariate separation, while still increasing the dimensionality of the response space.</p>
<p>A common and pragmatic strategy is therefore to restrict the analysis to the first few principal components, guided by explained variance and by visual inspection. In the iris dataset, the first two components account for the vast majority of total variability and already provide a clear geometric representation of group structure.</p>
<p>For this reason, the analysis below focuses on the first two principal component scores. This keeps the response space low-dimensional, improves the <em>n/p</em> ratio, and allows multivariate separation to be interpreted directly in relation to the score plot.</p>
</section>
<section id="model-fitting-to-the-iris-dataset" class="level3">
<h3 class="anchored" data-anchor-id="model-fitting-to-the-iris-dataset">Model fitting to the iris dataset</h3>
<p>In the iris example, the first two principal components already account for the vast majority of total variance and therefore provide a compact yet informative representation of the data geometry.</p>
<p>The MANOVA fitted above tests whether species centroids differ in the two-dimensional space defined by PC1 and PC2, relative to the dispersion observed within each species. The result, reported in Table&nbsp;3, shows a highly significant multivariate effect according to Pillai’s trace.</p>
<div class="cell">
<div id="tbl-pca-manova-pillai" 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-pca-manova-pillai-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;3: Omnibus PCA-MANOVA result (Pillai’s trace).
</figcaption>
<div aria-describedby="tbl-pca-manova-pillai-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="font-size: 14px; width: auto !important; margin-left: auto; margin-right: auto;">
<caption style="font-size: initial !important;"></caption>
 <thead>
  <tr>
   <th style="text-align:left;"> Effect </th>
   <th style="text-align:right;"> Df </th>
   <th style="text-align:right;"> Pillai </th>
   <th style="text-align:right;"> approx F </th>
   <th style="text-align:right;"> num Df </th>
   <th style="text-align:right;"> den Df </th>
   <th style="text-align:right;"> Pr(&gt;F) </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> Species </td>
   <td style="text-align:right;"> 2 </td>
   <td style="text-align:right;"> 1.1 </td>
   <td style="text-align:right;"> 89.894 </td>
   <td style="text-align:right;"> 4 </td>
   <td style="text-align:right;"> 294 </td>
   <td style="text-align:right;"> 0 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Residuals </td>
   <td style="text-align:right;"> 147 </td>
   <td style="text-align:right;">  </td>
   <td style="text-align:right;">  </td>
   <td style="text-align:right;">  </td>
   <td style="text-align:right;">  </td>
   <td style="text-align:right;">  </td>
  </tr>
</tbody>
</table>

</div>
</div>
</figure>
</div>
</div>
<div class="cell">
<div id="tbl-pca-manova-classical-pillai" 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-pca-manova-classical-pillai-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;4: Omnibus conventional MANOVA result (Pillai’s trace).
</figcaption>
<div aria-describedby="tbl-pca-manova-classical-pillai-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="font-size: 14px; width: auto !important; margin-left: auto; margin-right: auto;">
<caption style="font-size: initial !important;"></caption>
 <thead>
  <tr>
   <th style="text-align:left;"> Effect </th>
   <th style="text-align:right;"> Df </th>
   <th style="text-align:right;"> Pillai </th>
   <th style="text-align:right;"> approx F </th>
   <th style="text-align:right;"> num Df </th>
   <th style="text-align:right;"> den Df </th>
   <th style="text-align:right;"> Pr(&gt;F) </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> Species </td>
   <td style="text-align:right;"> 2 </td>
   <td style="text-align:right;"> 1.192 </td>
   <td style="text-align:right;"> 53.466 </td>
   <td style="text-align:right;"> 8 </td>
   <td style="text-align:right;"> 290 </td>
   <td style="text-align:right;"> 0 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Residuals </td>
   <td style="text-align:right;"> 147 </td>
   <td style="text-align:right;">  </td>
   <td style="text-align:right;">  </td>
   <td style="text-align:right;">  </td>
   <td style="text-align:right;">  </td>
   <td style="text-align:right;">  </td>
  </tr>
</tbody>
</table>

</div>
</div>
</figure>
</div>
</div>
<p>It is instructive to compare the PCA-based MANOVA result with the conventional MANOVA fitted directly on the original iris measurements (Table&nbsp;4).</p>
<p>Importantly, both analyses rely on the same underlying statistical machinery: in both cases, the model is fitted using the function <code>manova()</code>. What differs is not the test itself, but the geometry of the response space on which the test is applied.</p>
<p>In the conventional formulation, separation is assessed in the original four-dimensional measurement space. In the PCA-based formulation, the same separation is evaluated in a two-dimensional space that preserves the dominant structure of the data. The fact that both approaches yield a strong and consistent multivariate effect supports the interpretation of PCA-based MANOVA as a faithful low-dimensional representation of multivariate group separation.</p>
</section>
<section id="interpreting-multivariate-separation-in-principal-component-space" class="level3">
<h3 class="anchored" data-anchor-id="interpreting-multivariate-separation-in-principal-component-space">Interpreting multivariate separation in principal component space</h3>
<p>The omnibus MANOVA result establishes that the three species occupy distinct regions of the two-dimensional principal component space. To interpret this separation, it is useful to examine how individual components and groups contribute to the observed multivariate effect.</p>
<section id="contribution-of-individual-principal-components" class="level4">
<h4 class="anchored" data-anchor-id="contribution-of-individual-principal-components">Contribution of individual principal components</h4>
<p>Although MANOVA evaluates separation jointly across all response dimensions, separation in principal component space may be driven unevenly by different components. To make this explicit, univariate ANOVAs can be applied to the individual component scores (Table&nbsp;5 and Table&nbsp;6). These analyses are not used as independent hypothesis tests, but as descriptive tools that help locate multivariate separation within the score space.</p>
<div class="cell">
<div id="tbl-pc-anova-components-1" 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-pc-anova-components-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;5: Univariate ANOVA on PC1.
</figcaption>
<div aria-describedby="tbl-pc-anova-components-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="font-size: 14px; width: auto !important; margin-left: auto; margin-right: auto;">
<caption style="font-size: initial !important;"></caption>
 <thead>
  <tr>
   <th style="text-align:left;">   </th>
   <th style="text-align:right;"> Df </th>
   <th style="text-align:right;"> Sum Sq </th>
   <th style="text-align:right;"> Mean Sq </th>
   <th style="text-align:right;"> F value </th>
   <th style="text-align:right;"> Pr(&gt;F) </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> Species </td>
   <td style="text-align:right;"> 2 </td>
   <td style="text-align:right;"> 406.424 </td>
   <td style="text-align:right;"> 203.212 </td>
   <td style="text-align:right;"> 1050.631 </td>
   <td style="text-align:right;"> 0 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Residuals </td>
   <td style="text-align:right;"> 147 </td>
   <td style="text-align:right;"> 28.433 </td>
   <td style="text-align:right;"> 0.193 </td>
   <td style="text-align:right;">  </td>
   <td style="text-align:right;">  </td>
  </tr>
</tbody>
</table>

</div>
</div>
</figure>
</div>
</div>
<div class="cell">
<div id="tbl-pc-anova-components-2" 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-pc-anova-components-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;6: Univariate ANOVA on PC2.
</figcaption>
<div aria-describedby="tbl-pc-anova-components-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="font-size: 14px; width: auto !important; margin-left: auto; margin-right: auto;">
<caption style="font-size: initial !important;"></caption>
 <thead>
  <tr>
   <th style="text-align:left;">   </th>
   <th style="text-align:right;"> Df </th>
   <th style="text-align:right;"> Sum Sq </th>
   <th style="text-align:right;"> Mean Sq </th>
   <th style="text-align:right;"> F value </th>
   <th style="text-align:right;"> Pr(&gt;F) </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> Species </td>
   <td style="text-align:right;"> 2 </td>
   <td style="text-align:right;"> 22.569 </td>
   <td style="text-align:right;"> 11.285 </td>
   <td style="text-align:right;"> 14.6 </td>
   <td style="text-align:right;"> 0 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Residuals </td>
   <td style="text-align:right;"> 147 </td>
   <td style="text-align:right;"> 113.621 </td>
   <td style="text-align:right;"> 0.773 </td>
   <td style="text-align:right;">  </td>
   <td style="text-align:right;">  </td>
  </tr>
</tbody>
</table>

</div>
</div>
</figure>
</div>
</div>
<p>As expected from the geometry of the score plot in Figure&nbsp;1, separation along the first principal component accounts for a substantial portion of the group differences. The second component, while explaining a smaller fraction of total variance, contributes to separating <em>versicolor</em> and <em>virginica</em>, illustrating that dimensions with lower variance can still play a meaningful role in discrimination.</p>
</section>
<section id="which-groups-drive-multivariate-separation" class="level4">
<h4 class="anchored" data-anchor-id="which-groups-drive-multivariate-separation">Which groups drive multivariate separation?</h4>
<p>Further insight can be gained by examining the relative positions of group centroids in principal component space (Table&nbsp;7).</p>
<div class="cell">
<div id="tbl-centroids" 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-centroids-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;7: Centroids in PC space.
</figcaption>
<div aria-describedby="tbl-centroids-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="font-size: 14px; width: auto !important; margin-left: auto; margin-right: auto;">
<caption style="font-size: initial !important;"></caption>
 <thead>
  <tr>
   <th style="text-align:left;"> Species </th>
   <th style="text-align:right;"> PC1 </th>
   <th style="text-align:right;"> PC2 </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> setosa </td>
   <td style="text-align:right;"> -2.217 </td>
   <td style="text-align:right;"> -0.288 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> versicolor </td>
   <td style="text-align:right;"> 0.495 </td>
   <td style="text-align:right;"> 0.548 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> virginica </td>
   <td style="text-align:right;"> 1.723 </td>
   <td style="text-align:right;"> -0.260 </td>
  </tr>
</tbody>
</table>

</div>
</div>
</figure>
</div>
</div>
</section>
<section id="loadings-versus-discrimination" class="level4">
<h4 class="anchored" data-anchor-id="loadings-versus-discrimination">Loadings versus discrimination</h4>
<p>A natural question at this stage is whether principal component loadings could be used to assess “what matters” for group separation. Loadings, however, describe how the original variables contribute to defining each principal axis. They do not quantify how strongly groups differ along that axis.</p>
<p>In other words, loadings quantify variance, not discrimination. To measure how important a given component is for group separation, one must examine the distribution of scores across groups.</p>
<p>To illustrate this point in the simplest possible setting, consider PC1 alone. Once attention is restricted to a single component, the multivariate problem reduces to a univariate one, where effect sizes such as <img src="https://latex.codecogs.com/png.latex?%5Ceta%5E2">, summarize how much variance in PC1 is explained by group membership.</p>
<p>Finally, univariate <img src="https://latex.codecogs.com/png.latex?%5Ceta%5E2"> values quantify how strongly each raw variable is associated with group membership when considered in isolation (Table&nbsp;8). These values are not additive and should not be normalized into a decomposition of multivariate separation, but they provide a useful descriptive ranking of marginal discriminatory strength.</p>
<div class="cell">
<div id="tbl-eta-squared" 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-eta-squared-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;8: Univariate eta-squared (η²) for the association between Species and each raw iris measurement. Values are reported per variable (not additive) and are used here as a descriptive ranking of marginal discriminatory strength.
</figcaption>
<div aria-describedby="tbl-eta-squared-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="font-size: 14px; width: auto !important; margin-left: auto; margin-right: auto;">
<caption style="font-size: initial !important;"></caption>
 <thead>
  <tr>
   <th style="text-align:left;"> variable </th>
   <th style="text-align:right;"> η² </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> Petal length </td>
   <td style="text-align:right;"> 0.94 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Petal width </td>
   <td style="text-align:right;"> 0.93 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Sepal length </td>
   <td style="text-align:right;"> 0.62 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Sepal width </td>
   <td style="text-align:right;"> 0.40 </td>
  </tr>
</tbody>
</table>

</div>
</div>
</figure>
</div>
</div>
</section>
</section>
</section>
<section id="conclusions" class="level2">
<h2 class="anchored" data-anchor-id="conclusions">Conclusions</h2>
<p>This post argued that PCA and MANOVA address complementary stages of the same analytical problem. PCA defines a low-dimensional geometric representation in which multivariate structure becomes visible, while MANOVA quantifies group separation by comparing between-group and within-group variability within that space.</p>
<p>Using the iris dataset as a pedagogical example, the analysis showed that applying MANOVA in principal component space does not alter the underlying statistical machinery—the model is still fitted via <code>manova()</code>—but changes the geometry on which separation is assessed. In this sense, PCA-based MANOVA should not be viewed as a different test, but as the same test applied to a different, and often more interpretable, representation of the data. Seen in this light, PCA and MANOVA are best understood not as a sequence of techniques, but as complementary tools for reasoning geometrically about multivariate structure.</p>
<p>A central conceptual distinction was emphasized throughout: <em>principal component loadings explain variance</em>, whereas <em>group discrimination must be assessed on the distribution of scores across groups</em>. Confusing these two roles leads to common misinterpretations, particularly when PCA is used as a preparatory step for group comparison.</p>
<p>More broadly, the discussion highlighted the value of multivariate methods as tools for geometric reasoning rather than as mere extensions of univariate testing. By shifting attention from individual variables to configurations, centroids, and dispersion, PCA and MANOVA encourage a more structural understanding of group differences.</p>
<p>A natural next step is to revisit the same geometric questions using distance-based approaches, leading to Permutational MANOVA (PERMANOVA). This extension replaces model-based assumptions with permutation-based inference, offering a complementary perspective on multivariate separation that can be explored in a follow-up post.</p>


</section>

 ]]></description>
  <category>multivariate statistics</category>
  <guid>https://amsabatini.netlify.app/posts/manova/</guid>
  <pubDate>Mon, 22 Dec 2025 23:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/manova/iris.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Partial Least-Squares Discriminant Analysis for text classification: A linear model that works</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/pca_pls_da/</link>
  <description><![CDATA[ 




<blockquote class="blockquote">
<p>💾 <strong>Prefer reading offline?</strong><br>
You can <a href="index.pdf">download a PDF version of this post</a> for printing or future reference.</p>
</blockquote>
<p><strong>Partial Least-Squares Discriminant Analysis</strong> (PLS-DA) is a multivariate dimensionality-reduction method that has been widely used in so-called <em>omics</em> data analyses (Ruiz-Perez et al).</p>
<p>In these contexts — such as genomics, transcriptomics, or metabolomics — datasets often consist of thousands of variables (e.g., gene expression levels or metabolite intensities) measured over a relatively small number of samples. This characteristic regime of many more features than observations presents significant challenges for classical dimensionality-reduction methods like Principal Component Analysis (PCA), which do not account for the underlying class structure of the dataset and can be easily misled by noise-dominated variability.</p>
<p>PLS-DA addresses this issue by introducing supervision: as will be illustrated in the following, it seeks projections of the original data that are <em>maximally correlated with the known class labels</em>, rather than relying solely on overall variance to identify promising projection directions.</p>
<p>The aim of dimensionality-reduction methods such as PCA and PLS-DA is to compute a linear transformation that projects the data into a lower-dimensional space — optimally, in the sense of minimizing some form of reconstruction or classification error, depending on the method.</p>
<p>Let us suppose that the original data matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D"> is composed of <img src="https://latex.codecogs.com/png.latex?n"> samples, each characterized by a feature vector with <img src="https://latex.codecogs.com/png.latex?m"> components - that is, <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D"> is an <img src="https://latex.codecogs.com/png.latex?n%5Ctimes%20m"> matrix. A linear transformation is described by a matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BA%7D"> of size <img src="https://latex.codecogs.com/png.latex?m%5Ctimes%20d"> that <em>optimally</em> maps <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D"> into a set of <img src="https://latex.codecogs.com/png.latex?n"> transformed vectors in <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BR%7D%5Ed">, commonly referred to as <em>scores</em>. These scores are collected row-wise in the matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BS%7D%5Cin%5Cmathbb%7BR%7D%5E%7Bn%5Ctimes%20d%7D">. Given the error matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BE%7D"> of the same size, the model being fit takes the form:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7BS%7D=%5Cmathbf%7BX%7D%5Cmathbf%7BA%7D+%5Cmathbf%7BE%7D%0A"></p>
<p>The transformed variables, one for each observation and each axis in the projected space, are referred to as <em>principal components</em> (PC) in the context of Principal Component Analysis (PCA).</p>
<section id="principal-component-analysis" class="level2">
<h2 class="anchored" data-anchor-id="principal-component-analysis">Principal Component Analysis</h2>
<p>The PC vectors are computed by doing the eigenvector-eigenvalue analysis of the covariance matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BC%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7BC%7D%20=%20%5Cdfrac%7B1%7D%7Bn%20-%201%7D%20%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BC%7D_n%20%5Cmathbf%7BX%7D%0A"></p>
<p>where the centering matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BC%7D_n">, of size <img src="https://latex.codecogs.com/png.latex?n%20%5Ctimes%20n">, is defined as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7BC%7D_n%20=%20%5Cmathbf%7BI%7D_n%20-%20%5Cdfrac%7B1%7D%7Bn%7D%20%5Cmathbf%7B1%7D_n%20%5Cmathbf%7B1%7D_n%5ET%0A"></p>
<p>Here, <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BI%7D_n"> is the <img src="https://latex.codecogs.com/png.latex?n%20%5Ctimes%20n"> identity matrix, and <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7B1%7D_n"> is a column vector of ones of length <img src="https://latex.codecogs.com/png.latex?n">. The matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BC%7D_n"> (symmetric and idempotent) acts as a projection operator that removes the mean from each column of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D">, thereby centering the data.</p>
<p>The <em>loading vectors</em>, denoted by <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bd%7D_i">, are expressed in terms of the eigenvectors <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Be%7D_i"> and eigenvalues <img src="https://latex.codecogs.com/png.latex?%5Clambda_i"> of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BC%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7Bd%7D_i%20=%20%5Csqrt%7B%5Clambda_i%7D%5C,%5Cmathbf%7Be%7D_i,%20%5Cquad%20i%20=%201,%5Cdots,n%0A"></p>
<p>Since <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BC%7D"> is symmetric and positive semi-definite, its eigenvectors form an orthonormal basis and the associated eigenvalues are real and non-negative.</p>
<p>Let us suppose that the eigenvalues are sorted in decreasing order. The first PC is then given by the first loading vector <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bd%7D_1%20=%20%5Csqrt%7B%5Clambda_1%7D%20%5Cmathbf%7Be%7D_1">, which corresponds to the direction of <em>maximum variance</em> in the data. Each subsequent PC <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bd%7D_2,%20%5Cmathbf%7Bd%7D_3,%20%5Cdots"> defines a new axis that is orthogonal to the previous ones and captures the largest remaining variance.</p>
<p>The set <img src="https://latex.codecogs.com/png.latex?%5C%7B%20%5Cmathbf%7Bd%7D_1,%20%5Cdots,%20%5Cmathbf%7Bd%7D_d%20%5C%7D"> spans the reduced feature space onto which the data are projected.</p>
<p>The associated score vectors are obtained by projecting the original data onto each loading vector:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7Bt%7D_i%20=%20%5Cmathbf%7BX%7D%20%5Cmathbf%7Bd%7D_i,%20%5Cquad%20i%20=%201,%5Cdots,d%0A"></p>
<p>These form the columns of the score matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BS%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20d%7D">, where each row represents a transformed observation in the low-dimensional space. This is the same matrix introduced earlier in the general model formulation.</p>
<p>PCA identifies the loading vectors <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bd%7D_i"> by maximizing the variance of the projected data along each component, subject to orthogonality constraints. This ensures that each PC captures the greatest possible amount of variance not already explained by previous components. Implicitly, the PCA model assumes that the residuals in <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BE%7D"> are uncorrelated, homoscedastic, and normally distributed — although in practice, PCA is often used as a purely geometric transformation without relying on strict distributional assumptions.</p>
</section>
<section id="partial-least-squares-discriminant-analysis" class="level2">
<h2 class="anchored" data-anchor-id="partial-least-squares-discriminant-analysis">Partial Least-Squares Discriminant Analysis</h2>
<p>Let us suppose that the class labels for each observational unit are known. In the binary case, the labels can be collected in a column vector <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7By%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%201%7D">; in the multiclass setting, they are typically represented by a matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BY%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20q%7D">, where <img src="https://latex.codecogs.com/png.latex?q"> is the number of distinct classes and each row is a one-hot encoded vector.</p>
<p>As in PCA, we seek a set of <img src="https://latex.codecogs.com/png.latex?d"> latent components by projecting the original data <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D"> onto a lower-dimensional subspace. However, unlike PCA, PLS-DA chooses the projection directions in such a way that the <em>covariance between the projected data and the class labels</em> is maximized.</p>
<p>The matrix of cross-covariance is defined as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7BC%7D_%7BXY%7D%20=%20%5Cfrac%7B1%7D%7B(n-1)%5E2%7D%20%5Cmathbf%7BX%7D%5E%5Ctop%20%5Cmathbf%7BC%7D_n%20%5Cmathbf%7BY%7D%20%5Cmathbf%7BY%7D%5E%5Ctop%20%5Cmathbf%7BC%7D_n%20%5Cmathbf%7BX%7D%0A"></p>
<p>and the optimal projection direction <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Ba%7D_1"> (the first loading vector) is found as the leading eigenvector of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BC%7D_%7BXY%7D">. This ensures that the first latent component captures as much class-discriminative information as possible in a single direction.</p>
<p>The algorithm proceeds iteratively. Once the first loading vector <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Ba%7D_1"> has been determined, the corresponding score vector <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bt%7D_1%20=%20%5Cmathbf%7BX%7D%20%5Cmathbf%7Ba%7D_1"> is computed, and a linear regression of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BY%7D"> on <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bt%7D_1"> is performed to obtain <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bc%7D_1">, the weight vector in label space. The outer product <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bt%7D_1%20%5Cmathbf%7Bc%7D_1%5ET"> yields a rank-one approximation of the class label structure.</p>
<p>In order to extract additional components, the information already captured by the current component is removed from both <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D"> and <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BY%7D"> through a deflation step. This is achieved by subtracting the contribution explained by <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bt%7D_1">) and <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bc%7D_1">, resulting in residual matrices:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7BX%7D%5E%7B(1)%7D%20=%20%5Cmathbf%7BX%7D%20-%20%5Cmathbf%7Bt%7D_1%20%5Cmathbf%7Bp%7D_1%5ET,%20%5Cquad%0A%5Cmathbf%7BY%7D%5E%7B(1)%7D%20=%20%5Cmathbf%7BY%7D%20-%20%5Cmathbf%7Bt%7D_1%20%5Cmathbf%7Bc%7D_1%5ET%0A"></p>
<p>The process is then repeated on the residuals to obtain the next components. Each new latent component is orthogonal to the previous ones in <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BT%7D">, and captures additional class-discriminative information, if available.</p>
<p>Unlike PCA, PLS-DA explicitly exploits the structure of <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BY%7D"> to guide dimensionality reduction, making it a supervised projection method. It is particularly well suited to settings where the number of features is large and class separation is expected to be encoded in specific linear combinations of variables. Similar to PCA, the error structure in PLS-DA follows the same assumptions — namely, centered, uncorrelated, and homoscedastic residuals — although in practice, the method is often used with minimal reference to distributional properties.</p>
<p>PLS-DA can be further extended by introducing sparsity constraints on the loading vectors, yielding sparse PLS-DA (sPLS-DA). In this variant, only a subset of the original variables — those deemed most informative for class separation — are retained in the projection. The number of retained features per component is controlled by the parameter , and the optimization is regularized accordingly.</p>
<p>This formulation enables both class separation and embedded feature selection, which is particularly useful in high-dimensional datasets such as omics or text.</p>
<p>Geometrically, sPLS-DA defines a projection of the data into a low-dimensional latent space, just like PCA, but the directions are chosen to maximize class separability rather than data variance. This distinction is crucial: while PCA may preserve structure unrelated to the class labels, PLS-DA aligns the representation with the supervised task.</p>
<p>As will become evident in the following sections, this shift in optimization criterion can make PLS-DA a powerful tool for supervised dimension reduction. However, it also introduces specific vulnerabilities — particularly in high-dimensional settings with limited sample size — where the method may overfit to noise or to accidental separability. Careful validation is therefore essential to assess the generalization performance of PLS-DA models.</p>
<div id="exm-1" class="callout-example theorem example">
<p><span class="theorem-title"><strong>Example 1 (PCA and PLS-DA in action)</strong></span> The geometric distinction between unsupervised and supervised dimensionality-reduction methods can be visualized in a simplified example. Suppose that all data points are drawn from a single elliptical distribution, yet class labels are assigned based on their position relative to a tilted decision boundary. In this case, the direction of maximum variance — as identified by PCA — is not informative about the class structure. In contrast, PLS-DA selects a projection direction that aligns with the known class labels, even if it corresponds to a direction of lower overall variance.</p>
<p>Figure&nbsp;1 illustrates this contrast.</p>
</div>
<div id="fig-1" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/pca_pls_da/Fig1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Illustration of PCA vs PLS-DA when class labels are assigned across a tilted boundary. PCA (red) follows the direction of maximum variance in the data cloud. PLS-DA (blue) aligns with the supervised class structure, even if that direction does not correspond to maximal variance.
</figcaption>
</figure>
</div>
</section>
<section id="the-great-library-heist" class="level2">
<h2 class="anchored" data-anchor-id="the-great-library-heist">The Great Library Heist</h2>
<p>To illustrate the use of dimensionality-reduction and classification techniques in text data, I consider a corpus known as the “Great Library Heist”, originally featured by Silge and Robinson in <em>Text Mining with R</em> (https://www.tidytextmining.com), where it is used to demonstrate the potential of Latent Dirichlet Allocation — one of the most common algorithms for topic modeling.</p>
<p>Topic modeling is a method for unsupervised classification of documents, in this case applied to the chapters of four well-known English novels from Project Gutenberg:</p>
<ul>
<li><em>Great Expectations</em> by Charles Dickens</li>
<li><em>Pride and Prejudice</em> by Jane Austen</li>
<li><em>The War of the Worlds</em> by H. G. Wells</li>
<li><em>Twenty Thousand Leagues Under the Sea</em> by Jules Verne</li>
</ul>
<div class="cell">
<details class="code-fold">
<summary>upload libraries</summary>
<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>(tidyverse)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Core data wrangling and piping (includes dplyr, tidyr, etc.)</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Grammar of graphics for plots (explicit, though included in tidyverse)</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(MASS)        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For multivariate data simulation (e.g., mvrnorm)</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidymodels)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Unified framework for modeling and machine learning workflows</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(textrecipes) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Text preprocessing steps within tidymodels recipes</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stringr)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Consistent string handling (regex, manipulation)</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mixOmics)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Multivariate methods including sPLS-DA, PLS, PCA</span></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(yardstick)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Performance metrics and confusion matrices</span></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine multiple ggplot2 plots into one layout</span></span></code></pre></div>
</details>
</div>
<p>Following the general strategy introduced by Silge and Robinson, each chapter of the four books in the “Great Library Heist” corpus is treated as a separate document.</p>
<p>In author’s own words: &gt; When examining a statistical method, it can be useful to try it on a very simple case where we know the “right answer”. For example, a set of documents that definitely relate to four separate topics is collected, then topic modeling is performed to see whether the algorithm can correctly distinguish the four groups. This lets us double-check that the method is useful, and gain a sense of how and when it can go wrong.</p>
<p>The goal of the analysis is to construct a term-document matrix based on term frequency (TF) scores, which will serve as the input for both dimensionality reduction and classification. In this classical bag-of-words approach, each document is represented as an unordered collection of words — ignoring grammar, syntax, and word order. What matters is the frequency of each term across documents.</p>
<p>I intend to do the same with sPLS-DA. In doing so, I take inspiration from Saccenti and Tenori, who applied sPLS-DA to the stylometric analysis of the Divina Commedia.</p>
<div class="cell">
<details class="code-fold">
<summary>The Great Library Heist</summary>
<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;">load</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"books.rda"</span>)</span>
<span id="cb2-2"></span>
<span id="cb2-3">df_books <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> books <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-4">    </span>
<span id="cb2-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Identify chaper for each book</span></span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(title) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-7">  <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;">chapter =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cumsum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(text, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">regex</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^chapter</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;">s"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ignore_case =</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;">%&gt;%</span></span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(chapter <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-10">  </span>
<span id="cb2-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prepare label and document name </span></span>
<span id="cb2-12">  <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;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_squish</span>(title)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unite</span>(document, title, chapter, <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;">%&gt;%</span></span>
<span id="cb2-14">  </span>
<span id="cb2-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All text for each chapter, in each book, is united in a single field</span></span>
<span id="cb2-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(label, document) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_c</span>(text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</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;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-18">  </span>
<span id="cb2-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove headings like "CHAPTER I", "CHAPTER TWENTY-ONE", etc.</span></span>
<span id="cb2-20">  <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;">text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_remove</span>(text, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">regex</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"^CHAPTER</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;">s+.*?</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;">s+"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ignore_case =</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;">%&gt;%</span></span>
<span id="cb2-21"></span>
<span id="cb2-22">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># To get chapters in order</span></span>
<span id="cb2-23">  <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;">chapter_num =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_extract</span>(document, <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><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+$"</span>))) <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;">arrange</span>(label, chapter_num) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-25">  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;">select</span>(label, document, text)</span></code></pre></div>
</details>
</div>
<section id="data-preparation-and-feature-encoding" class="level3">
<h3 class="anchored" data-anchor-id="data-preparation-and-feature-encoding">Data preparation and feature encoding</h3>
<p>In the data preparation phase, I split the dataset into three parts:</p>
<ul>
<li>a training set (<code>train_set</code>), used to learn the TF vocabulary and train the model;</li>
<li>a validation set (<code>val_set</code>), reserved for potential model selection;</li>
<li>and a test set (<code>test_set</code>), used exclusively for out-of-sample evaluation.</li>
</ul>
<p>In the current pipeline, however, the validation set is not used. This is because I adopt internal cross-validation (<code>Mfold</code>) within the <code>tune.splsda()</code> function from the <code>mixOmics</code> package to select the optimal number of features (<code>keepX</code>) per component. The tuning procedure relies entirely on resampling the training set, rendering the explicit use of <code>val_set</code> unnecessary.</p>
<p>The reason for keeping a separate <code>val_set</code> is flexibility: it enables external model selection (e.g., grid search or alternative tuning strategies beyond those provided by <code>mixOmics</code>). Although the validation set (<code>val_set</code>) is not used in the current analysis, it is explicitly carved out from the training data to support future extensions. To maximize the data available for model training and internal cross-validation, only 5% of the training set is reserved for <code>val_set</code>. This ensures methodological robustness while preserving as much data as possible for fitting and tuning the model via <code>tune.splsda()</code>.</p>
<p>Feature engineering is performed using the <code>textrecipes</code> package, which allows defining a preprocessing pipeline through <code>recipe</code>. In this case, the text is tokenized, stop words are removed, the vocabulary is limited to the top 500 most frequent tokens, and a term frequency (TF) encoding is applied. This recipe is trained on the training set and then used to transform all data splits consistently, avoiding data leakage.</p>
<div class="cell">
<details class="code-fold">
<summary>Train/validation/test splits with TF encoding</summary>
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First split 80% train_val vs 20% test</span></span>
<span id="cb3-4">split_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;">initial_split</span>(df_books, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.80</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strata =</span> label)</span>
<span id="cb3-5">train_val <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;">training</span>(split_1)</span>
<span id="cb3-6">test_set  <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;">testing</span>(split_1)</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Second split 95% train vs 5% val (part of training)</span></span>
<span id="cb3-9">split_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;">initial_split</span>(train_val, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strata =</span> label)</span>
<span id="cb3-10">train_set <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;">training</span>(split_2)</span>
<span id="cb3-11">val_set   <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;">testing</span>(split_2)</span>
<span id="cb3-12"></span>
<span id="cb3-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check</span></span>
<span id="cb3-14">df_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;">bind_rows</span>(</span>
<span id="cb3-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">train =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(train_set, label),</span>
<span id="cb3-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">validation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(val_set, label),</span>
<span id="cb3-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(test_set, label),</span>
<span id="cb3-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.id =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"split"</span></span>
<span id="cb3-19">)</span>
<span id="cb3-20">n_chapters <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>(df_check<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n)</span>
<span id="cb3-21"></span>
<span id="cb3-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Feature engineering via textrecipes::recipe</span></span>
<span id="cb3-23">text_rec <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;">recipe</span>(label <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> train_set) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_tokenize</span>(text) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_stopwords</span>(text) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_tokenfilter</span>(text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_tokens =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>) <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;">step_tf</span>(text)</span>
<span id="cb3-28"></span>
<span id="cb3-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Prep/bake the recipe</span></span>
<span id="cb3-30">rec_prep <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;">prep</span>(text_rec)</span>
<span id="cb3-31"></span>
<span id="cb3-32">X_train  <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;">bake</span>(rec_prep, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</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;">%&gt;%</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;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>label) <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;">as.matrix</span>()</span>
<span id="cb3-33">y_train  <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;">bake</span>(rec_prep, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</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;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(label)</span>
<span id="cb3-34"></span>
<span id="cb3-35">X_test <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;">bake</span>(rec_prep, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> test_set) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</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;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>label) <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;">as.matrix</span>()</span>
<span id="cb3-36">y_test <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;">bake</span>(rec_prep, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> test_set) <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;">pull</span>(label)</span></code></pre></div>
</details>
</div>
</section>
<section id="model-tuning-with-adaptive-feature-selection" class="level3">
<h3 class="anchored" data-anchor-id="model-tuning-with-adaptive-feature-selection">Model tuning with adaptive feature selection</h3>
<p>In the <code>tune.splsda()</code> step, I explicitly include <code>max_tokens</code> (i.e., the full vocabulary size) in the grid of <code>keepX</code>. This allows the tuning procedure to consider not only sparse sPLS‑DA models, but also the special case of full PLS‑DA with no feature selection. Crucially, <code>tune.splsda()</code> returns a separate optimal <code>keepX</code> for each component, enabling each latent dimension to adaptively select the number of features most useful for class separation.</p>
<p>Because the procedure runs efficiently, I can afford a relatively fine grid for robust comparison. A typical choice might be:</p>
<div class="cell">
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span>)</span>
<span id="cb4-2">tune_result <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;">tune.splsda</span>(</span>
<span id="cb4-3">  X_train, y_train,</span>
<span id="cb4-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncomp =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb4-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test.keepX =</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;">25</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;">75</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">125</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>),</span>
<span id="cb4-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">validation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mfold"</span>,</span>
<span id="cb4-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">folds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb4-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max.dist"</span>,</span>
<span id="cb4-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">progressBar =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb4-10">)</span></code></pre></div>
</div>
<div class="cell">
<details class="code-fold">
<summary>sPLS-DA model fit</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tuning keepX for sPLS-DA</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span>)</span>
<span id="cb5-3">tune_result <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;">tune.splsda</span>(</span>
<span id="cb5-4">  X_train, y_train,</span>
<span id="cb5-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncomp =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,</span>
<span id="cb5-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test.keepX =</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;">25</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;">75</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">125</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">250</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>),</span>
<span id="cb5-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">validation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mfold"</span>,</span>
<span id="cb5-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">folds =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb5-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dist =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"max.dist"</span>,</span>
<span id="cb5-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">progressBar =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb5-11">)</span>
<span id="cb5-12"></span>
<span id="cb5-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Final fit with best keepX</span></span>
<span id="cb5-14">best_keepX <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tune_result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>choice.keepX</span>
<span id="cb5-15">splsda_fit <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;">splsda</span>(X_train, y_train, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncomp =</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;">keepX =</span> best_keepX)</span>
<span id="cb5-16"></span>
<span id="cb5-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict on training set</span></span>
<span id="cb5-18">pred_train <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;">predict</span>(splsda_fit, X_train, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">comp =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb5-19"></span>
<span id="cb5-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict on test set</span></span>
<span id="cb5-21">pred_test <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;">predict</span>(splsda_fit, X_test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">comp =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<details class="code-fold">
<summary>Model evaluation with yardstick</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- 1. PREDICTIONS </span><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">TEST</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"> SET</span></span>
<span id="cb6-2"></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tibble for accuracy calculation</span></span>
<span id="cb6-4">df_test_proj             <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.data.frame</span>(pred_test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>variates)</span>
<span id="cb6-5">df_test_proj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.truth      <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>(y_test)</span>
<span id="cb6-6">df_test_proj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.pred_class <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>(pred_test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>class<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>max.dist[, <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;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(df_test_proj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.truth))</span>
<span id="cb6-7"></span>
<span id="cb6-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Accuracy calculated on the test set</span></span>
<span id="cb6-9">acc_test <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;">accuracy</span>(df_test_proj, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> .truth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> .pred_class)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.estimate</span>
<span id="cb6-10"></span>
<span id="cb6-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- 2. PREDICTIONS TRAINING SET</span></span>
<span id="cb6-12"></span>
<span id="cb6-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tibble for accuarcy calculation</span></span>
<span id="cb6-14">df_train_proj <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.data.frame</span>(pred_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>variates)</span>
<span id="cb6-15">df_train_proj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.truth <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>(y_train)</span>
<span id="cb6-16">df_train_proj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.pred_class <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>(pred_train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>class<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>max.dist[, <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;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(df_train_proj<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.truth))</span>
<span id="cb6-17"></span>
<span id="cb6-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Accuracy calculated on the training set</span></span>
<span id="cb6-19">acc_train <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;">accuracy</span>(df_train_proj, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> .truth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> .pred_class)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.estimate</span>
<span id="cb6-20"></span>
<span id="cb6-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- 3. COMPLETE METRICS TABLE</span></span>
<span id="cb6-22"></span>
<span id="cb6-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test set</span></span>
<span id="cb6-24">metrics_test <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;">metrics</span>(df_test_proj, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> .truth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> .pred_class)</span>
<span id="cb6-25"></span>
<span id="cb6-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Training set</span></span>
<span id="cb6-27">metrics_train <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;">metrics</span>(df_train_proj, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> .truth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> .pred_class)</span>
<span id="cb6-28"></span>
<span id="cb6-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- 4. LATENT SPACE VISUALIZATION USING TRUE LABELS</span></span>
<span id="cb6-30"></span>
<span id="cb6-31">p_12 <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_test_proj, <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> dim1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> dim2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> .truth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-32">  <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="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="cb6-33">  <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;">"sPLS-DA: Test set projected by true labels"</span>,</span>
<span id="cb6-34">       <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;">"Component 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;">"Component 2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-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.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>())</span>
<span id="cb6-37"></span>
<span id="cb6-38">p_13 <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_test_proj, <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> dim1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> dim3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> .truth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-39">  <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="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="cb6-40">  <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;">"sPLS-DA: Test set projected by true labels"</span>,</span>
<span id="cb6-41">       <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;">"Component 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;">"Component 3"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-43">  <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.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>())</span>
<span id="cb6-44"></span>
<span id="cb6-45">p_23 <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_test_proj, <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> dim2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> dim3, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> .truth)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-46">  <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="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="cb6-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;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sPLS-DA: Test set projected by true labels"</span>,</span>
<span id="cb6-48">       <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;">"Component 2"</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;">"Component 3"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-49">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-50">  <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.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>())</span>
<span id="cb6-51"></span>
<span id="cb6-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- 5. IMPORTANT WORD VISUALIZATION (the visualization will be produced later using ggplot)</span></span>
<span id="cb6-53"></span>
<span id="cb6-54"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Component 1</span></span>
<span id="cb6-55"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># p_1 &lt;- plotLoadings(splsda_fit,</span></span>
<span id="cb6-56"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     comp = 1,</span></span>
<span id="cb6-57"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     method = "mean",</span></span>
<span id="cb6-58"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     contrib = "max",</span></span>
<span id="cb6-59"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     ndisplay = 20,</span></span>
<span id="cb6-60"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     title = "Words most contributing to Component 1")</span></span>
<span id="cb6-61"></span>
<span id="cb6-62"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Component 2</span></span>
<span id="cb6-63"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># p_2 &lt;- plotLoadings(splsda_fit,</span></span>
<span id="cb6-64"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     comp = 2,</span></span>
<span id="cb6-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     method = "mean",</span></span>
<span id="cb6-66"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     contrib = "max",</span></span>
<span id="cb6-67"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     ndisplay = 20,</span></span>
<span id="cb6-68"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     title = "Words most contributing to Component 2")</span></span>
<span id="cb6-69"></span>
<span id="cb6-70"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Component 3</span></span>
<span id="cb6-71"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># p_3 &lt;- plotLoadings(splsda_fit,</span></span>
<span id="cb6-72"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     comp = 3,</span></span>
<span id="cb6-73"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     method = "mean",</span></span>
<span id="cb6-74"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     contrib = "max",</span></span>
<span id="cb6-75"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     ndisplay = 20,</span></span>
<span id="cb6-76"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#                     title = "Words most contributing to Component 3")</span></span>
<span id="cb6-77"></span>
<span id="cb6-78"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># --- 6. CONFUSION MATRIX </span></span>
<span id="cb6-79"></span>
<span id="cb6-80">pred_labels <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pred_test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>class<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>max.dist[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]  </span>
<span id="cb6-81">test_coords <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.data.frame</span>(pred_test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>variates)</span>
<span id="cb6-82"></span>
<span id="cb6-83">df_test_proj <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> test_coords <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-84">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb6-85">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.truth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(y_test),</span>
<span id="cb6-86">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.pred_class =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(pred_labels, <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;">levels</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(y_test)))</span>
<span id="cb6-87">  )</span>
<span id="cb6-88"></span>
<span id="cb6-89">confmat <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;">conf_mat</span>(df_test_proj, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> .truth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> .pred_class)</span></code></pre></div>
</details>
</div>
<p>As shown in Figure&nbsp;2, the model achieves an accuracy of 97.6% on the test set. Since the training accuracy is 97.9%, this small difference indicates that the model generalizes well, with no signs of overfitting.</p>
<div id="fig-2" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/pca_pls_da/Fig2.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Confusion matrix for the sPLS-DA classifier (ncomp = 3), evaluated on the test set. The classifier achieves near-perfect accuracy, demonstrating that TF representations of chapter-level text segments are sufficient to support robust discrimination.
</figcaption>
</figure>
</div>
<p>The tuning procedure selected <code>keepX = c(200, 200, 500)</code> as the optimal number of features to retain on each of the three components. This means that the first two components focus on relatively sparse subsets of the vocabulary (200 terms each), while the third component includes the entire trimmed vocabulary of 500 terms. This adaptive behavior reflects the role of each component in capturing residual discriminative structure: early components rely on compact feature sets, while later ones may require broader lexical support to separate harder-to-distinguish classes.</p>
<p>Since the method assumes that classes can be linearly separated in the latent space, it becomes relatively straightforward to assign an importance score to each feature, reflecting its contribution to the separation along each component. This enables direct interpretability of the model in terms of the most discriminative words associated with each class. The function <code>plotLoadings()</code> from the <code>mixOmics</code> package conveniently extracts and visualizes these features. As shown in Figure&nbsp;3, the method assigns a class-specific contribution to each selected term, allowing us to interpret each component in terms of discriminative words. It is important to note that terms are not necessarily exclusive to a single novel — many occur in multiple chapters across books. The color in the barplots reflects the class (i.e., book) for which the term contributes most strongly to class separation along that component, not exclusivity of occurrence. Component 1 highlights features specific to <em>Great Expectations</em> (e.g., “pip”, “havisham’s”). Component 2 reflects opposing contributions from <em>Pride and Prejudice</em> and <em>Twenty Thousand Leagues Under the Sea</em> — with “jane”, “elizabeth”, and “darcy” linked to the former, and “captain”, “nemo”, and “nautilus” to the latter. Component 3 is almost entirely dominated by lexical signals from <em>The War of the Worlds</em> (e.g., “martians”, “martian”, “people”).</p>
<div id="fig-3" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center" data-fig-pos="H">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/pca_pls_da/Fig3.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%" data-fig-pos="H">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: For each of the three components selected by sPLS-DA, the top 20 contributing terms are shown with their relative importance. The color indicates the class (i.e., book) most associated with each term’s loading.
</figcaption>
</figure>
</div>
<p>Several of the terms highlighted by sPLS-DA (see Figure&nbsp;3) also appear among the top-ranked topic terms extracted via LDA, indicating that lexical cues effective for supervised classification often coincide with those that define unsupervised thematic structure.</p>
<p>Quantitatively, both models perform remarkably well on the Great Library Heist dataset. The original LDA model (trained on the full corpus) misclassified only 2 out of 193 documents, despite being entirely unsupervised (accuracy: 99%). The sPLS-DA classifier, trained on just 80% of the corpus, reached 97.6% on a held-out test set. When retrained on the full corpus (with no sparsity constraint), sPLS-DA misclassified 3 documents (accuracy: 98.4%).</p>
<blockquote class="blockquote">
<p>This post illustrated how sparse PLS-DA — a linear, supervised method — can be used effectively for document classification. Along the way, I compared it with topic modeling via LDA, observed strong performance on real-world text data, and provided a complete, reproducible R workflow. Despite its simplicity, this approach proved to be both interpretable and surprisingly effective.</p>
</blockquote>
</section>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<p>Ruiz-Perez D, Guan H, Madhivanan P, Mathee K, and Narasimhan G, “So you think you can PLS-DA?” <em>BMC Bioinformatics</em> 2020, vol 21, (suppl 1):2.</p>
<p>Saccenti E, Tenori L, “Stylometric investigation of Dante’s Divina Commedia by means of multivariate data analysis techniques” <em>Internat J Comput Linguistics Res</em> 2012, vol 3, n 2, pp 35-48.</p>


</section>

 ]]></description>
  <category>multivariate statistics</category>
  <category>machine learning</category>
  <category>text mining</category>
  <guid>https://amsabatini.netlify.app/posts/pca_pls_da/</guid>
  <pubDate>Sun, 06 Jul 2025 22:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/pca_pls_da/Fig1.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Why memory matters: A tale of two Markov chains</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/markov_chain_acf/</link>
  <description><![CDATA[ 




<section id="the-two-state-markov-chain" class="level2">
<h2 class="anchored" data-anchor-id="the-two-state-markov-chain">The two-state Markov chain</h2>
<p>Let us begin the discussion with the well-known two-state Markov chain (two-state MC) defined by the transition matrix:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7BP%7D%20=%20%5Cbegin%7Bbmatrix%7D%0Ap_1%20&amp;%201-p_1%20%5C%5C%0Ap_0%20&amp;%201-p_0%0A%5Cend%7Bbmatrix%7D%0A"></p>
<p>Figure&nbsp;1 show the graph representation of this two-state MC.</p>
<div id="fig-1" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/markov_chain_acf/2-state-MC.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Graph representation of the two-state Markov chain used to encode the transition from two states <img src="https://latex.codecogs.com/png.latex?S_0"> and <img src="https://latex.codecogs.com/png.latex?S_1">.
</figcaption>
</figure>
</div>
<p>Here, <img src="https://latex.codecogs.com/png.latex?p_1"> and <img src="https://latex.codecogs.com/png.latex?p_0"> represent the probabilities of remaining in state <img src="https://latex.codecogs.com/png.latex?S_0"> and moving from state <img src="https://latex.codecogs.com/png.latex?S_1"> to <img src="https://latex.codecogs.com/png.latex?S_0">, respectively. Their complements, <img src="https://latex.codecogs.com/png.latex?q_1%20=%201%20-%20p_1"> and <img src="https://latex.codecogs.com/png.latex?q_0%20=%201%20-%20p_0">, correspond to the probabilities of leaving <img src="https://latex.codecogs.com/png.latex?S_0"> and remaining in <img src="https://latex.codecogs.com/png.latex?S_1">.</p>
<p>The conditions <img src="https://latex.codecogs.com/png.latex?p_0,%20p_1%20%3E%200"> ensure that the chain is both irreducible (any state can be reached from any other) and aperiodic (no strict periodic cycle is enforced), two structural properties that guarantee the existence of a stationary distribution. This stationary distribution reflects the long-run proportion of time the process spends in each state, and can be written as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPr%7D(S_0)%20=%20p,%20%5Cquad%20%5Ctext%7BPr%7D(S_1)%20=%20q%20=%201%20-%20p%0A"></p>
<p>Recall that the <img src="https://latex.codecogs.com/png.latex?r">-step transition matrix, <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BP%7D%5E%7B(r)%7D">, provides the probabilities of transitioning from each state to any other state in exactly <img src="https://latex.codecogs.com/png.latex?r"> steps. In the case of the two-state MC introduced above, it is known that this matrix admits the following elegant closed-form expression:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7BP%7D%5E%7B(r)%7D%20=%20%5Cfrac%7B1%7D%7B1%20-%20(p_1%20-%20p_0)%7D%0A%5Cbegin%7Bbmatrix%7D%0Ap_0%20&amp;%20q_1%20%5C%5C%0Ap_0%20&amp;%20q_1%0A%5Cend%7Bbmatrix%7D%0A+%20%5Cfrac%7B(p_1%20-%20p_0)%5Er%7D%7B1%20-%20(p_1%20-%20p_0)%7D%0A%5Cbegin%7Bbmatrix%7D%0Aq_1%20&amp;%20-q_1%20%5C%5C%0A-p_0%20&amp;%20p_0%0A%5Cend%7Bbmatrix%7D%0A"></p>
<p>This decomposition separates the long-run behavior (first term) from the transient dynamics (second term), which decays geometrically as <img src="https://latex.codecogs.com/png.latex?(p_1%20-%20p_0)%5Er">. In other words, the process progressively “forgets” its initial state, and the transition probabilities stabilize over time.</p>
<p>This convergence is a hallmark of well-behaved Markov chains and underlies the existence of a unique stationary distribution:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboldsymbol%7B%5Cpi%7D%20=%20%5Cfrac%7B1%7D%7B1%20-%20(p_1%20-%20p_0)%7D%20%5Bp_0%20%5C;%5C;%20q_1%5D%0A"></p>
<p>This stationary distribution satisfies the fundamental balance condition <img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Cpi%7D%20%5Cmathbf%7BP%7D%20=%20%5Cboldsymbol%7B%5Cpi%7D"> and it characterizes the long-run proportion of time the process spends in each state — independently of the starting configuration.</p>
<p>We are now interested in the properties of the stochastic process <img src="https://latex.codecogs.com/png.latex?%5C%7BX_t%5C%7D"> generated by this chain, where <img src="https://latex.codecogs.com/png.latex?X_t"> denotes the state occupied by the process at time <img src="https://latex.codecogs.com/png.latex?t">. Since the chain evolves over two discrete states, this process is binary, and its behavior can be fully characterized by its first and second-order moments, including the autocorrelation function (ACF).</p>
<p>Assuming that the states <img src="https://latex.codecogs.com/png.latex?S_0"> and <img src="https://latex.codecogs.com/png.latex?S_1"> are coded as <img src="https://latex.codecogs.com/png.latex?%5C%7B1,%200%5C%7D">, the expectation and variance of the process <img src="https://latex.codecogs.com/png.latex?X_t"> are:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AE%5BX_t%5D%20&amp;=%20%5Cpi_1%20=%20%5Cdfrac%7Bp_0%7D%7B1%20-%20(p_1%20-%20p_0)%7D%20%5C%5C%0A%5Ctext%7BVar%7D(X_t)%20&amp;=%20E%5BX_t%5E2%5D%20-%20E%5BX_t%5D%5E2%20=%20E%5BX_t%5D%20-%20E%5BX_t%5D%5E2%20%5C%5C%0A&amp;=%20%5Cpi_1%20-%20%5Cpi_1%5E2%20=%20%5Cdfrac%7Bp_0%20q_1%7D%7B%5Cleft%5B1%20-%20(p_1%20-%20p_0)%5Cright%5D%5E2%7D%0A%5Cend%7Baligned%7D%0A"></p>
<p>To compute the ACF at lag <img src="https://latex.codecogs.com/png.latex?r">, note that only the case <img src="https://latex.codecogs.com/png.latex?X_t%20=%201,%20X_%7Bt+r%7D%20=%201"> contributes to <img src="https://latex.codecogs.com/png.latex?E%5BX_t%20X_%7Bt+r%7D%5D">, due to the binary coding:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AE%5BX_t%20X_%7Bt+r%7D%5D%20&amp;=%20%5Csum_%7Bi,j%7D%20i%20%5Ccdot%20j%20%5Ccdot%20%5CPr(X_t%20=%20i,%20X_%7Bt+r%7D%20=%20j)%20%5C%5C%0A&amp;=%20%5CPr(X_t%20=%201,%20X_%7Bt+r%7D%20=%201)%20=%20%5Cpi_1%20%5Ccdot%20P%5E%7B(r)%7D_%7B11%7D%0A%5Cend%7Baligned%7D%0A"></p>
<p>The covariance at lag <img src="https://latex.codecogs.com/png.latex?r"> is therefore:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BCov%7D(X_t,%20X_%7Bt+r%7D)%20&amp;=%20E%5BX_t%20X_%7Bt+r%7D%5D%20-%20E%5BX_t%5D%5E2%20%5C%5C%0A&amp;=%20%5Cpi_1%20%5Ccdot%20P%5E%7B(r)%7D_%7B11%7D%20-%20%5Cpi_1%5E2%20%5C%5C%0A&amp;=%20(p_1%20-%20p_0)%5Er%20%5Ccdot%20%5Cfrac%7Bp_0%20q_1%7D%7B%5B1%20-%20(p_1%20-%20p_0)%5D%5E2%7D%0A%5Cend%7Baligned%7D%0A"></p>
<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">
A note on the variance computation
</div>
</div>
<div class="callout-body-container callout-body">
<p>Since <img src="https://latex.codecogs.com/png.latex?X_t%20%5Cin%20%5C%7B0,1%5C%7D">, it follows that <img src="https://latex.codecogs.com/png.latex?X_t%5E2%20=%20X_t"> for all <img src="https://latex.codecogs.com/png.latex?t">, and hence <img src="https://latex.codecogs.com/png.latex?E%5BX_t%5E2%5D%20=%20E%5BX_t%5D">. This identity simplifies the computation of the variance, which becomes <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVar%7D(X_t)%20=%20E%5BX_t%5D%20-%20E%5BX_t%5D%5E2">.</p>
</div>
</div>
<p>The ACF is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%5Crho_r%20=%20%5Ctext%7BCorr%7D(X_t,%20X_%7Bt+r%7D)%20=%20%5Cfrac%7B%5Ctext%7BCov%7D(X_t,%20X_%7Bt+r%7D)%7D%7B%5Ctext%7BVar%7D(X_t)%7D%20=%20(p_1%20-%20p_0)%5Er%7D%0A"></p>
<p>This geometric decay reflects the memory of the chain, governed by the second eigenvalue of the transition matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BP%7D">, namely <img src="https://latex.codecogs.com/png.latex?%5Clambda_2%20=%20p_1%20-%20p_0">.</p>
<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">
Coding and autocorrelation
</div>
</div>
<div class="callout-body-container callout-body">
<p>The ACF derived above is based on a binary coding of the process <img src="https://latex.codecogs.com/png.latex?X_t%20%5Cin%20%5C%7B0,1%5C%7D">. In this setting, the autocovariance at lag <img src="https://latex.codecogs.com/png.latex?r"> is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(X_t,%20X_%7Bt+r%7D)%20=%20E%5BX_t%20X_%7Bt+r%7D%5D%20-%20E%5BX_t%5D%5E2%0A"></p>
<p>and the ACF is obtained as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Crho_r%20=%20%5Cfrac%7B%5Ctext%7BCov%7D(X_t,%20X_%7Bt+r%7D)%7D%7B%5Ctext%7BVar%7D(X_t)%7D%20=%20(p_1%20-%20p_0)%5Er%0A"></p>
<p>Now, suppose we recode the process using values <img src="https://latex.codecogs.com/png.latex?%5C%7B-1,%20+1%5C%7D">, defining <img src="https://latex.codecogs.com/png.latex?Z_t%20=%202X_t%20-%201">. Then <img src="https://latex.codecogs.com/png.latex?Z_t%20%5Cin%20%5C%7B-1,%20+1%5C%7D"> and:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0AE%5BZ_t%5D%20&amp;=%202E%5BX_t%5D%20-%201%20%5C%5C%0A%5Ctext%7BVar%7D(Z_t)%20&amp;=%204%20%5Ccdot%20%5Ctext%7BVar%7D(X_t)%20%5C%5C%0AE%5BZ_t%20Z_%7Bt+r%7D%5D%20&amp;=%204E%5BX_t%20X_%7Bt+r%7D%5D%20-%204E%5BX_t%5D%20+%201%0A%5Cend%7Baligned%7D%0A"></p>
<p>So the autocovariance of <img src="https://latex.codecogs.com/png.latex?Z_t"> becomes:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCov%7D(Z_t,%20Z_%7Bt+r%7D)%20=%20E%5BZ_t%20Z_%7Bt+r%7D%5D%20-%20E%5BZ_t%5D%5E2%20=%204%20%5Ccdot%20%5Ctext%7BCov%7D(X_t,%20X_%7Bt+r%7D)%0A"></p>
<p>and the autocorrelation remains invariant:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BCorr%7D(Z_t,%20Z_%7Bt+r%7D)%20=%20%5Cfrac%7B%5Ctext%7BCov%7D(Z_t,%20Z_%7Bt+r%7D)%7D%7B%5Ctext%7BVar%7D(Z_t)%7D%20=%20%5Cfrac%7B4%20%5Ccdot%20%5Ctext%7BCov%7D(X_t,%20X_%7Bt+r%7D)%7D%7B4%20%5Ccdot%20%5Ctext%7BVar%7D(X_t)%7D%20=%20%5Crho_r%0A"></p>
<p>This means that while the numerical values of expectation, variance, and autocovariance change under linear recoding, the autocorrelation (a dimensionless, normalized measure) does not.</p>
</div>
</div>
</section>
<section id="a-four-state-markov-chain-encoding-second-order-binary-dependencies" class="level2">
<h2 class="anchored" data-anchor-id="a-four-state-markov-chain-encoding-second-order-binary-dependencies">A four-state Markov chain encoding second-order binary dependencies</h2>
<p>Now consider a more structured chain where the state at time <img src="https://latex.codecogs.com/png.latex?t"> depends on both <img src="https://latex.codecogs.com/png.latex?X_%7Bt-1%7D"> and <img src="https://latex.codecogs.com/png.latex?X_%7Bt-2%7D">. This can be seen as a natural extension of the two-state MC to capture memory over two steps. This corresponds to a second-order Markov chain on binary values <img src="https://latex.codecogs.com/png.latex?X_t%20%5Cin%20%7B0,1%7D">. It can be encoded as a first-order Markov chain with 4 states (four-state MC), each representing a pair (<img src="https://latex.codecogs.com/png.latex?X_%7Bt-2%7D,%20X_%7Bt-1%7D">):</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>state</th>
<th>pair</th>
<th><img src="https://latex.codecogs.com/png.latex?X_t"></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?S_0"></td>
<td>(0,0)</td>
<td>0</td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?S_1"></td>
<td>(0,1)</td>
<td>1</td>
</tr>
<tr class="odd">
<td><img src="https://latex.codecogs.com/png.latex?S_2"></td>
<td>(1,0)</td>
<td>0</td>
</tr>
<tr class="even">
<td><img src="https://latex.codecogs.com/png.latex?S_3"></td>
<td>(1,1)</td>
<td>1</td>
</tr>
</tbody>
</table>
<p>The transition matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BP%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7B4%20%5Ctimes%204%7D"> then takes the form:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cmathbf%7BP%7D%20=%20%5Cbegin%7Bbmatrix%7D%0Ap_%7B11%7D%20&amp;%201%20-%20p_%7B11%7D%20&amp;%200%20&amp;%200%20%5C%5C%0A0%20&amp;%200%20&amp;%20p_%7B10%7D%20&amp;%201%20-%20p_%7B10%7D%20%5C%5C%0Ap_%7B01%7D%20&amp;%201%20-%20p_%7B01%7D%20&amp;%200%20&amp;%200%20%5C%5C%0A0%20&amp;%200%20&amp;%20p_%7B00%7D%20&amp;%201%20-%20p_%7B00%7D%0A%5Cend%7Bbmatrix%7D%0A"></p>
<p>Each row corresponds to a transition determined by the current pair (<img src="https://latex.codecogs.com/png.latex?X_%7Bt-2%7D,%20X_%7Bt-1%7D">), and the choice of the next value <img src="https://latex.codecogs.com/png.latex?X_t"> determines the new state Figure&nbsp;2.</p>
<div id="fig-2" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/markov_chain_acf/4-state-MC.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:40.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Graph representation of the four-state Markov chain used to encode the particular alternation structure represented by the transition matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BP%7D">.
</figcaption>
</figure>
</div>
<section id="stationary-distribution" class="level3">
<h3 class="anchored" data-anchor-id="stationary-distribution">Stationary distribution</h3>
<p>We can compute the stationary distribution <img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Cpi%7D%20=%20%5B%5Cpi_0,%20%5Cpi_1,%20%5Cpi_2,%20%5Cpi_3%5D"> by solving the system:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboldsymbol%7B%5Cpi%7D%20%5Ccdot%20%5Cmathbf%7BP%7D%20=%20%5Cboldsymbol%7B%5Cpi%7D,%20%5Cquad%20%5Csum_%7Bi=0%7D%5E3%20%5Cpi_i%20=%201%0A"></p>
<p>Thanks to the sparsity and regularity of the transition matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BP%7D">, this system admits a closed-form solution. In particular, the stationary probabilities satisfy the following relations:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Baligned%7D%0A%5Cpi_1%20&amp;=%20%5Cfrac%7B1%20-%20p_%7B11%7D%7D%7Bp_%7B01%7D%7D%5Cpi_0%20%5C%5C%0A%5Cpi_2%20&amp;=%20%5Cpi_1%20%5C%5C%0A%5Cpi_3%20&amp;=%20%5Cfrac%7B1%20-%20p_%7B10%7D%7D%7Bp_%7B00%7D%7D%5Cpi_1%0A%5Cend%7Baligned%7D%0A"></p>
<p>Substituting into the normalization condition gives:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5B%201%20+%20%5Cfrac%7B2(1%20-%20p_%7B11%7D)%7D%7Bp_%7B01%7D%7D%20+%20%5Cfrac%7B(1%20-%20p_%7B11%7D)(1%20-%20p_%7B10%7D)%7D%7Bp_%7B01%7D%20p_%7B00%7D%7D%20%5Cright%5D%5Cpi_0%20=%201%0A"></p>
<p>Once <img src="https://latex.codecogs.com/png.latex?%5Cpi_0"> is computed from this expression, all remaining <img src="https://latex.codecogs.com/png.latex?%5Cpi_i">, <img src="https://latex.codecogs.com/png.latex?i%20=%201,%20%5Cdots,%203">, follow directly.</p>
</section>
<section id="autocorrelation-function" class="level3">
<h3 class="anchored" data-anchor-id="autocorrelation-function">Autocorrelation function</h3>
<p>To compute the ACF, we first define a projection function <img src="https://latex.codecogs.com/png.latex?f(i)">, which recovers the binary value <img src="https://latex.codecogs.com/png.latex?X_t"> encoded by state <img src="https://latex.codecogs.com/png.latex?S_i">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af(i)%20=%20%5Cbegin%7Bcases%7D%0A1%20&amp;%20%5Ctext%7Bif%20%7D%20i%20%5Cin%20%5C%7B1,%203%5C%7D%20%5C%5C%0A0%20&amp;%20%5Ctext%7Botherwise%7D%0A%5Cend%7Bcases%7D%0A"></p>
<p>Then, the expectation of <img src="https://latex.codecogs.com/png.latex?X_t"> is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AE%5BX_t%5D%20=%20%5Csum_%7Bi=0%7D%5E3%20f(i)%20%5Ccdot%20%5Cpi_i%20=%20%5Cpi_1%20+%20%5Cpi_3%0A"></p>
<p>and the variance becomes:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(X_t)%20=%20E%5BX_t%5D(1%20-%20E%5BX_t%5D)%0A"></p>
<p>To evaluate the autocorrelation at lag <img src="https://latex.codecogs.com/png.latex?r">, we compute the joint moment:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AE%5BX_t%20X_%7Bt+r%7D%5D%20=%20%5Csum_%7Bi=0%7D%5E3%20%5Csum_%7Bj=0%7D%5E3%20f(i)%20%5Ccdot%20f(j)%20%5Ccdot%20%5Cpi_i%20%5Ccdot%20%5Cleft%5B%5Cmathbf%7BP%7D%5Er%5Cright%5D_%7Bij%7D%0A"></p>
<p>so that the ACF is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Crho_r%20=%20%5Cdfrac%7BE%5BX_t%20X_%7Bt+r%7D%5D%20-%20E%5BX_t%5D%5E2%7D%7B%5Ctext%7BVar%7D(X_t)%7D%0A"></p>
<p>This method relies only on the stationary distribution and the <img src="https://latex.codecogs.com/png.latex?r">-step transition matrix <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BP%7D%5Er">. Unlike the two-state MC discussed above, here <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BP%7D%5Er"> does not admit a closed-form expression and must be computed numerically.</p>
<p>The following R function implements the numerical computation of the ACF for the four-state MC described above. It constructs the transition matrix, solves for the stationary distribution, and computes the projected ACF based on the binary encoding of the states.</p>
<div class="cell">
<details class="code-fold">
<summary>Numerical computation of ACF</summary>
<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">compute_acf_four_state <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>(p11, p10, p01, p00, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_lag =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) {</span>
<span id="cb1-2">    </span>
<span id="cb1-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Transition matrix</span></span>
<span id="cb1-4">  P <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(p11, <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> p11, <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-5">                <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>, p10, <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> p10,</span>
<span id="cb1-6">                p01, <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> p01, <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-7">                <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>, p00, <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> p00), <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;">4</span>, <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-8"></span>
<span id="cb1-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Solve for stationary distribution π such that π %*% P = π</span></span>
<span id="cb1-10">  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;">t</span>(P) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-11">  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;">rbind</span>(A, <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>))             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add normalization row</span></span>
<span id="cb1-12">  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;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-13">  pi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">solve</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(A) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> A, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(A) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> b)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Least-squares solution</span></span>
<span id="cb1-14"></span>
<span id="cb1-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Projection function f(i) = 1 for states 1 and 3, else 0</span></span>
<span id="cb1-16">  f <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="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;">1</span>)</span>
<span id="cb1-17"></span>
<span id="cb1-18">  EX   <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>(f <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi)</span>
<span id="cb1-19">  VarX <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> EX <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> EX)</span>
<span id="cb1-20"></span>
<span id="cb1-21">  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>(max_lag)</span>
<span id="cb1-22">  P_power <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-23"></span>
<span id="cb1-24">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (r <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>max_lag) {</span>
<span id="cb1-25">    P_power <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> P_power <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> P</span>
<span id="cb1-26">    EXX <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;">0</span></span>
<span id="cb1-27">    <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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) {</span>
<span id="cb1-28">      <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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) {</span>
<span id="cb1-29">        EXX <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> EXX <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> f[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> f[j] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> pi[i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> P_power[i, j]</span>
<span id="cb1-30">      }</span>
<span id="cb1-31">    }</span>
<span id="cb1-32">    rho[r] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (EXX <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> EX<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> VarX</span>
<span id="cb1-33">  }</span>
<span id="cb1-34"></span>
<span id="cb1-35">  <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;">list</span>(</span>
<span id="cb1-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pi   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(pi, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb1-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">EX   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(EX, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb1-38">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">VarX =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(VarX, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb1-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rho   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(rho, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb1-40">  ))</span>
<span id="cb1-41">}</span></code></pre></div>
</details>
</div>
<p>Figure&nbsp;3 shows the resulting autocorrelation pattern, which reflects the alternating behavior encoded in the four-state structure and highlights the persistence introduced by second-order dependencies.</p>
<div id="fig-3" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/markov_chain_acf/fig_acf.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Autocorrelation function for the four-state Markov chain with parameters <img src="https://latex.codecogs.com/png.latex?p_%7B00%7D%20=%200.80,%20p_%7B01%7D%20=%200.20,%20p_%7B10%7D%20=%200.50,%20p_%7B11%7D%20=%200.10">.
</figcaption>
</figure>
</div>
</section>
</section>
<section id="estimating-the-standard-error-of-a-sample-proportion" class="level2">
<h2 class="anchored" data-anchor-id="estimating-the-standard-error-of-a-sample-proportion">Estimating the standard error of a sample proportion</h2>
<p>When working with binary time series, a fundamental question is how to estimate the standard error (SE) of the sample proportion</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbar%7BX%7D_n%20=%20%5Cdfrac%7B1%7D%7Bn%7D%5Csum_%7Bt=1%7D%5En%20X_t%0A"></p>
<p>In the case of independent Bernoulli trials, the variance of the sample mean <img src="https://latex.codecogs.com/png.latex?%5Cbar%7BX%7D_n"> is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(%5Cbar%7BX%7D_n)%20=%20%5Cfrac%7B%5Csigma%5E2%7D%7Bn%7D,%20%5Cquad%20%5Ctext%7Bwith%20%7D%20%5Csigma%5E2%20=%20p(1%20-%20p)%0A"></p>
<p>and the corresponding i.i.d. formula for the standard error is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BSE%7D%20=%20%5Csqrt%7B%5Cdfrac%7Bp(1-p)%7D%7Bn%7D%7D%0A"></p>
<p>However, when the data are generated from a dependent process such as a Markov chain (either order 1 or 2), this formula underestimates or overestimates the true SE, because the observations are no longer independent.</p>
<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">
Variance of the sample mean with autocorrelated data
</div>
</div>
<div class="callout-body-container callout-body">
<p>Let <img src="https://latex.codecogs.com/png.latex?%7BX_t%7D"> be a weakly stationary process with mean <img src="https://latex.codecogs.com/png.latex?%5Cmu">, variance <img src="https://latex.codecogs.com/png.latex?%5Csigma%5E2">, and autocovariance function <img src="https://latex.codecogs.com/png.latex?%5Cgamma_r%20=%20%5Ctext%7BCov%7D(X_t,%20X_%7Bt+r%7D)">.</p>
<p>We are interested in the variance of the sample mean, which can be written:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(%5Cbar%7BX%7D_n)%20=%20%5Cfrac%7B1%7D%7Bn%5E2%7D%20%5Csum_%7Bt=1%7D%5En%20%5Csum_%7Bs=1%7D%5En%20%5Ctext%7BCov%7D(X_t,%20X_s)%0A=%20%5Cfrac%7B1%7D%7Bn%5E2%7D%20%5Csum_%7Bt=1%7D%5En%20%5Csum_%7Bs=1%7D%5En%20%5Cgamma_%7B%7Ct-s%7C%7D%0A"></p>
<p>We now reorganize this sum by collecting all terms with the same lag <img src="https://latex.codecogs.com/png.latex?r%20=%20%5Cmid%20t%20-%20s%5C,%5Cmid">. The number of terms with lag <img src="https://latex.codecogs.com/png.latex?r"> is <img src="https://latex.codecogs.com/png.latex?n%20-%20r"> (for <img src="https://latex.codecogs.com/png.latex?r%20=%200,%201,%5Ccdots,n%20-%201">). So:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(%5Cbar%7BX%7D_n)%20=%20%5Cfrac%7B1%7D%7Bn%5E2%7D%20%5Csum_%7Br=-(n-1)%7D%5E%7Bn-1%7D%20(n%20-%20%7Cr%7C)%20%5Cgamma_%7B%7Cr%7C%7D%0A=%20%5Cfrac%7B1%7D%7Bn%5E2%7D%20%5Cleft(%20n%20%5Cgamma_0%20+%202%20%5Csum_%7Br=1%7D%5E%7Bn-1%7D%20(n%20-%20r)%20%5Cgamma_r%20%5Cright)%0A"></p>
<p>Finally, using <img src="https://latex.codecogs.com/png.latex?%5Cgamma_0%20=%20%5Csigma%5E2"> and dividing numerator and denominator by <img src="https://latex.codecogs.com/png.latex?n">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVar%7D(%5Cbar%7BX%7D_n)%20=%20%5Cfrac%7B%5Csigma%5E2%7D%7Bn%7D%20%5Cunderbrace%7B%5Cleft(1%20+%202%20%5Csum_%7Br=1%7D%5E%7Bn-1%7D%20%5Cleft(1%20-%20%5Cfrac%7Br%7D%7Bn%7D%5Cright)%20%5Crho_r%20%5Cright)%7D_%5Ctext%7BVIF%7D%0A"></p>
<p>This formula shows how the autocorrelation structure inflates the variance of the sample mean. The term in parentheses is usually referred to as the variance inflation factor (<img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D">). <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D"> reduces to 1 in the i.i.d. (independent identically distributed) case, when all <img src="https://latex.codecogs.com/png.latex?%5Crho_r%20=%200"> for <img src="https://latex.codecogs.com/png.latex?r%20%5Cgeq%201">.</p>
<p>For large <img src="https://latex.codecogs.com/png.latex?n"> and fast decay of the ACF, a common simplification for <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D"> applies:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVIF%7D%20%5Capprox%201%20+%202%20%5Csum_%7Br=1%7D%5ER%20%5Crho_r%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?R"> is a cutoff lag beyond which autocorrelation is negligible.</p>
</div>
</div>
<p>With the introduction of the <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D">, the expression of <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BSE%7D"> becomes:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7B%5Ctext%7BSE%7D%20=%20%5Csqrt%7B%5Cdfrac%7Bp(1-p)%7D%7Bn%7D%7D%20%5Csqrt%7B%5Ctext%7BVIF%7D%5Cmathstrut%7D%7D%0A"></p>
<p>The <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D"> can be computed for both the two-state and four-state MCs, for example when estimating the proportion of time spent in each state — and its standard error — from an observed binary sequence of length <img src="https://latex.codecogs.com/png.latex?n">.</p>
<ul>
<li>In the two-state MC, define the transition probabilities as follows:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0Ap_0%20=%20%5Ctext%7BPr%7D(0%20%5Cto%201),%20%5Cquad%20p_1%20=%20%5Ctext%7BPr%7D(1%20%5Cto%201)%0A"></p>
<p>With this parametrization, the lag-<img src="https://latex.codecogs.com/png.latex?r"> autocorrelation of the process is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Crho_r%20=%20(p_1%20-%20p_0)%5Er%0A"></p>
<p>This expression fully determines the ACF, which decays geometrically. The corresponding <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D"> becomes:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BVIF%7D%20=%20%5Cfrac%7B1%20+%20%5Crho_1%7D%7B1%20-%20%5Crho_1%7D%20=%20%5Cfrac%7B1%20+%20(p_1%20-%20p_0)%7D%7B1%20-%20(p_1%20-%20p_0)%7D%0A"></p>
<p>In the two-state case, the process is i.i.d. only when the transition matrix is both symmetric and memoryless — that is, when transitions occur with equal probability in both directions. In this case, the autocorrelations vanish and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D%20=%201">.</p>
<ul>
<li>In the four-state MC, the autocorrelations <img src="https://latex.codecogs.com/png.latex?%5Crho_r"> must be computed numerically (as done above), and plugged into the expression for <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D"> above. As shown in Figure&nbsp;3, the ACF decays fast enough to justify using the simplified formula above.</li>
</ul>
<p>In both cases, accounting for autocorrelation is essential to avoid underestimating uncertainty in sample-based inference.</p>
<div class="callout callout-style-default callout-tip callout-titled" title="Example. Comparing VIF in two-state and four-state MCs">
<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">
Example. Comparing VIF in two-state and four-state MCs
</div>
</div>
<div class="callout-body-container callout-body">
<p>Let us consider two different Markov models generating a binary sequence <img src="https://latex.codecogs.com/png.latex?%7BX_t%7D">:</p>
<ul>
<li><p>two-state MC directly over <img src="https://latex.codecogs.com/png.latex?%5C%7B0,1%5C%7D">,</p></li>
<li><p>four-state MC, which overlaps bigrams <img src="https://latex.codecogs.com/png.latex?(X_%7Bt-1%7D,%20X_t)%20%5Cin%20%5C%7B00,01,10,11%5C%7D">, where the actual sequence is recovered by projecting on the second element of each pair.</p></li>
</ul>
<p>The two-state MC has transition parameters:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Baligned%7D%0Ap_0&amp;=0.665%5C%5C%0Ap_1&amp;=0.115%0A%5Cend%7Baligned%7D%0A%5Cright.%0A"></p>
<p>Hence, the stationary distribution is given by:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BPr%7D(X_t=0)&amp;=%5Cdfrac%7Bp_0%7D%7B1-(p_1-p_0)%7D=0.429%5C%5C%0A%5Ctext%7BPr%7D(X_t=1)&amp;=%5Cdfrac%7B1-p_1%7D%7B1-(p_1-p_0)%7D=0.571%0A%5Cend%7Baligned%7D%0A%5Cright.%0A"></p>
<p>The four-state MC has transition parameters:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Baligned%7D%0Ap_%7B00%7D&amp;=%200.86%5C%5C%0Ap_%7B01%7D&amp;=%200.12%5C%5C%0Ap_%7B10%7D&amp;=%200.56%5C%5C%0Ap_%7B11%7D&amp;=%200.12%0A%5Cend%7Baligned%7D%0A%5Cright.%0A"></p>
<p>The stationary distribution of the binary sequence is computed by projecting on the second digit of each pair:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BPr%7D(X_t=0)&amp;=%20%5Cpi_0+%5Cpi_2%5Capprox%200.429%5C%5C%0A%5Ctext%7BPr%7D(X_t=1)&amp;=%20%5Cpi_1+%5Cpi_3%5Capprox%200.571%5C%5C%0A%5Cend%7Baligned%7D%0A%5Cright.%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Cpi%7D%20=%20%5B%5Cpi_0,%20%5Cpi_1,%20%5Cpi_2,%20%5Cpi_3%5D"> is the stationary distribution of the four-state MC (obtained from, e.g., <code>compute_acf_four_state()</code>).</p>
<p>Even if the marginal distributions are almost identical, the two models may produce very different autocorrelation structures, and hence different values of the <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Baligned%7D%0A%5Ctext%7BVIF%7D_%7B%5Ctext%7Btwo-state%7D%7D&amp;=%5Cdfrac%7B1+(p_1-p_0)%7D%7B1-(p_1-p_0)%7D=0.290%5C%5C%0A%5Ctext%7BVIF%7D_%7B%5Ctext%7Bfour-state%7D%7D&amp;=0.196%0A%5Cend%7Baligned%7D%0A%5Cright.%0A"></p>
<p>where the value of <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D_%7B%5Ctext%7Bfour-state%7D%7D"> is obtained from <code>compute_acf_four_state()</code>).</p>
<p>As shown by the negative ACF at lag 1 (–0.55 for the two-state MC, –0.54 for the four-state MC), both chains tend to promote alternation rather than persistence. When computing the SE, this alternation leads to an overestimation if one naively applies the i.i.d. formula. In both cases, the <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D"> is smaller than 1, indicating anti-correlation. Moreover, since a smaller <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D"> implies longer memory, we can conclude that the four-state MC retains information more persistently than the two-state one, despite having the same marginal distribution.</p>
</div>
</div>
<p>This example illustrates how two Markov models generating binary sequences with identical marginal distributions can nevertheless lead to different effective sample sizes, due to distinct autocorrelation structures that significantly impact inference.</p>
<div class="callout callout-style-default callout-tip 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">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>To achieve the same standard error in both settings, one would need to inflate the sample size in the less autocorrelated case by a factor approximately equal to the ratio of the corresponding <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D">s. In the example above, since <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D_%7B%5Ctext%7Btwo-state%7D%7D%20=%200.290"> and <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BVIF%7D_%7B%5Ctext%7Bfour-state%7D%7D%20=%200.196">, the required sample size under the two-state model should be roughly 1.5 times larger to achieve the same standard error as under the four-state model.</p>
</div>
</div>
<blockquote class="blockquote">
<p><strong>In short, correlation matters—even when proportions look the same.</strong> These insights remind us that dependencies in data—often subtle—can have major consequences for statistical inference. Understanding and quantifying them is essential, even in the simplest of models.</p>
</blockquote>


</section>

 ]]></description>
  <category>probability</category>
  <category>statistics</category>
  <guid>https://amsabatini.netlify.app/posts/markov_chain_acf/</guid>
  <pubDate>Sun, 08 Jun 2025 22:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/markov_chain_acf/2-4-state-MC.png" medium="image" type="image/png" height="188" width="144"/>
</item>
<item>
  <title>Backwards through the model</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/calibration/</link>
  <description><![CDATA[ 




<p>Calibrating a transducer means establishing a functional relationship between a physical input (e.g., pressure, temperature, force) and a measured output. In many real-world applications, however, the actual objective lies elsewhere: once the system is calibrated, the focus shifts to model inversion. Given an observed output, the task is to determine which input value could plausibly have produced it. This is precisely the challenge faced during deployment, when the transducer is integrated into an operational system.</p>
<p>This post explores — through a simple but realistic example — the conceptual limits of direct model inversion in regression. The discussion also includes a brief note on orthogonal regression and introduces a Monte Carlo simulation strategy for addressing inverse prediction.</p>
<section id="simulating-a-calibration-scenario" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="simulating-a-calibration-scenario">Simulating a calibration scenario</h2>
<p>A simplified yet technically plausible calibration scenario is considered. The system under calibration is a nonlinear transducer — for instance, a sensor whose output voltage varies smoothly with a physical input quantity such as pressure or displacement. The underlying (noiseless) response function is assumed to be injective, meaning that each output corresponds to exactly one input value. In functional terms, the response curve is monotonic, ensuring that inversion remains well-defined in the absence of noise.</p>
<p>The true input–output relationship is assumed to follow a smooth nonlinear curve, here modeled as a fourth-degree polynomial (truth model). Measurement noise is introduced by adding zero-mean Gaussian noise with standard deviation <img src="https://latex.codecogs.com/png.latex?%5Csigma"> to the target outputs.</p>
<p>The R code below simulates 25 calibration data points from the reference model. A distinction is drawn between the transducer’s full operating range, <img src="https://latex.codecogs.com/png.latex?%5B0,12%5D">, and the actual calibration interval, <img src="https://latex.codecogs.com/png.latex?%5B1,11%5D"> — henceforth referred to as the measurement interval. This setup reflects common practice, where calibration is limited to a central portion of the device’s range. Within this interval, an operating point <img src="https://latex.codecogs.com/png.latex?x%5E*=3.5"> is selected, corresponding to a target output <img src="https://latex.codecogs.com/png.latex?y%5E*=11">. All variables are expressed in arbitrary units.</p>
<div class="cell">
<details class="code-fold">
<summary>Simulate the calibration dataset</summary>
<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>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb1-3"></span>
<span id="cb1-4">x_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;">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;">12</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;">25</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Coefficients for a strictly increasing polynomial on [0, 10]</span></span>
<span id="cb1-7">beta <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.681125</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</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.05</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.002</span>)</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the operating point ...</span></span>
<span id="cb1-10">x_star <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;">3.5</span></span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ... the corresponding target output</span></span>
<span id="cb1-12">y_star <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;">11</span></span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Truth model function</span></span>
<span id="cb1-15">f_true <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb1-16">  beta[<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> beta[<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 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> beta[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]<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="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> beta[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> beta[<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 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="cb1-17">}</span>
<span id="cb1-18"></span>
<span id="cb1-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># True outputs</span></span>
<span id="cb1-20">y_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;">f_true</span>(x_full)</span>
<span id="cb1-21"></span>
<span id="cb1-22">sigma <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-23">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> y_true <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x_full), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</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;">sd =</span> sigma)</span>
<span id="cb1-24"></span>
<span id="cb1-25"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Build full dataset</span></span>
<span id="cb1-26">df_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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x_full, <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;">y_true =</span> y_true)</span>
<span id="cb1-27"></span>
<span id="cb1-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Subset for calibration: (1, 11)</span></span>
<span id="cb1-29">df_calib <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;">subset</span>(df_full, x <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> x <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;">11</span>)</span></code></pre></div>
</details>
</div>
<p><strong>Cautionary note</strong>: The noise added to the simulated outputs assumes constant standard deviation and Gaussian distribution — a deliberate simplification. In reality, calibration is often performed under carefully controlled laboratory conditions, which minimize thermal drift, power supply variations, and other environmental effects.</p>
<p>Once deployed, however, the transducer is left to “breathe” — it operates in a dynamic environment, subject to factors that were effectively considered constant during calibration. As a result, the actual variability in output readings is typically greater than what the calibration model suggests.</p>
<p>Moreover, it is not uncommon for the noise standard deviation to depend on the input value itself — for instance, in thermocouples or strain gauge sensors, measurement variability often increases with temperature or applied force due to physical nonlinearities and sensitivity drift and/or drop.</p>
</section>
<section id="fitting-the-calibration-model" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="fitting-the-calibration-model">Fitting the calibration model</h2>
<p>Calibration is performed by fitting a polynomial regression model to the observed data — restricted to the measurement interval, as would typically be the case in practice to avoid excessive extrapolation at the boundaries. A fourth-degree polynomial is fitted using least squares, which coincides with the true data-generating mechanism adopted in the simulation (though in real applications, such knowledge would not be available).</p>
<div class="cell">
<details class="code-fold">
<summary>Fit a 4th-degree polynomial model</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit the calibration model only within (1, 11)</span></span>
<span id="cb2-2">model_poly4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(y <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;">poly</span>(x, <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;">data =</span> df_calib)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict over the full domain for visualization</span></span>
<span id="cb2-5">df_full<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y_pred <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;">predict</span>(model_poly4, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</span> df_full)</span></code></pre></div>
</details>
</div>
<p>To better understand the validity range of the model, we plot the full dataset along with the fitted curve, highlighting the measurement interval.</p>
<p>To assess the validity domain of the fitted model, the entire dataset is visualized alongside the estimated regression curve Figure&nbsp;1. The measurement interval is explicitly marked, highlighting the region where data were actually used for model fitting. Outside this interval, the model operates in extrapolation mode, relying on limited or no information — a situation in which prediction accuracy and reliability can rapidly deteriorate.</p>
<div id="fig-1" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/calibration/fig1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:75.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: The red points represent noisy measurements, the dotted black line indicates the true model, and the solid blue line shows the fitted polynomial curve. The dashed vertical lines mark the boundaries of the measurement interval.
</figcaption>
</figure>
</div>
</section>
<section id="from-direct-prediction-to-inversion" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="from-direct-prediction-to-inversion">From direct prediction to inversion</h2>
<p>Now suppose the calibrated system is to be used in the field. For a new observed target output <img src="https://latex.codecogs.com/png.latex?y%5E*">, the goal is to estimate the corresponding input <img src="https://latex.codecogs.com/png.latex?x"> under the fitted model. This task is known as <strong>inverse prediction</strong>. A common misunderstanding is to simply reverse the roles of input and output and fit a new regression model of the form <img src="https://latex.codecogs.com/png.latex?x%20%5Csim%20y">. This “reversed regression” approach, however, lacks a solid statistical foundation and yields incorrect uncertainty estimates — especially when measurement noise affects the output, not the input.</p>
<p>In most calibration settings, the values of the input variable are assumed to be known with negligible uncertainty, while the output is subject to measurement noise. This asymmetry is foundational: classical regression assumes uncertainty in the response, not in the predictor. Swapping their roles invalidates that assumption and leads to biased estimates. The underlying statistical inconsistency is further discussed below in the context of orthogonal regression.</p>
<p>Instead, the correct formulation requires solving the equation</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Chat%7By%7D(x)%20=%20y%5E*%0A"></p>
<p>i.e., finding the value of <img src="https://latex.codecogs.com/png.latex?x"> for which the predicted output equals the observed measurement. In general, this equation cannot be solved in closed form for a polynomial of degree greater than two, unless specific simplifying conditions apply. However, since the fitted polynomial is strictly increasing over the full input domain <img src="https://latex.codecogs.com/png.latex?%5B0,%2012%5D">, the solution exists and is unique for any <img src="https://latex.codecogs.com/png.latex?y%5E*"> in the measurement interval, where reliable inverse predictions can thus be expected.</p>
<p>The inversion can be carried out numerically — for example, using <code>uniroot()</code> in R, which implements the bisection method. This approach is robust and well-suited to continuous, monotonic functions like the fitted polynomial. Below, the inversion is performed for the chosen target output.</p>
<div class="cell">
<details class="code-fold">
<summary>Invert the calibration model</summary>
<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 the target output</span></span>
<span id="cb3-2">y_star <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;">11</span></span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the prediction function from the model</span></span>
<span id="cb3-5">predict_poly <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb3-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model_poly4, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">newdata =</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> x))</span>
<span id="cb3-7">}</span>
<span id="cb3-8"></span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the function to solve: f(x) = y_pred(x) - y_star</span></span>
<span id="cb3-10">f_inversion <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict_poly</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y_star</span>
<span id="cb3-11"></span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Solve numerically in the interval [1, 11] (calibration zone)</span></span>
<span id="cb3-13">x_hat <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;">uniroot</span>(f_inversion, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</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;">upper =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>root</span></code></pre></div>
</details>
</div>
<p>An target output of 11 corresponds to an estimated input of 3.499, based on the fitted calibration model.</p>
<div id="fig-2" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/calibration/fig2.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:75.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: The fitted regression curve (blue line) is inverted numerically for the target output <img src="https://latex.codecogs.com/png.latex?y%5E*%20=%2011"> (horizontal red dashed line), yielding the corresponding estimate <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bx%7D"> of the operating point (vertical dotted line). The point <img src="https://latex.codecogs.com/png.latex?(%5Chat%7Bx%7D,%20y%5E*)"> is marked on the curve to illustrate the solution.
</figcaption>
</figure>
</div>
<p>This kind of inverse prediction relies entirely on the deterministic structure of the fitted model and does not account for uncertainty. In the next section, a Monte Carlo approach will be introduced to estimate the distribution of plausible inputs given an observed output.</p>
<p>Before proceeding, it is useful to reflect on the geometry of the problem — in particular, on the fact that ordinary least-squares regression minimizes the sum of squared vertical distances from the data points to the fitted curve. This choice is justified when the input variable is regarded as fixed and known without error, and all uncertainty is attributed to the output. When this assumption no longer holds — as is often the case in inverse usage — minimizing vertical deviations may no longer be the most appropriate strategy.</p>
<section id="a-geometric-note-vertical-vs-orthogonal-residuals" class="level3 unnumbered">
<h3 class="unnumbered anchored" data-anchor-id="a-geometric-note-vertical-vs-orthogonal-residuals">A geometric note: vertical vs orthogonal residuals</h3>
<p>In classical regression, the output variable <img src="https://latex.codecogs.com/png.latex?y"> is modeled as random, while treating the input <img src="https://latex.codecogs.com/png.latex?x"> as fixed or measured without error — or equivalently, as a random variable with <strong>zero standard deviation</strong>. This modeling choice is what justifies minimizing the <em>vertical distance</em> between the observed <img src="https://latex.codecogs.com/png.latex?y"> values and the fitted curve <img src="https://latex.codecogs.com/png.latex?%5Chat%7By%7D(x)"> in least-squares estimation.</p>
<p>However, when using the model in reverse — i.e., to estimate <img src="https://latex.codecogs.com/png.latex?x"> given <img src="https://latex.codecogs.com/png.latex?y"> — this assumption becomes questionable. In real-world settings, the input itself is subject to measurement uncertainty, or the roles of input and output may conceptually reverse (e.g., in calibration vs deployment).</p>
<p>This motivates the idea of orthogonal regression, or total least squares, where the residuals are measured perpendicularly to the curve, taking into account errors in both variables.</p>
<p>Although not implemented here, orthogonal regression serves as an important conceptual reminder: the least-squares fit is geometrically asymmetric. Direct prediction (forward use) is naturally supported; inverse prediction must be handled with care, especially in nonlinear settings like the one examined here.</p>
<div class="callout callout-style-default callout-important 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">
Orthogonal regression in practice
</div>
</div>
<div class="callout-body-container callout-body">
<p>A practical method for regression with errors in both x and y is Deming regression, which minimizes the sum of squared orthogonal distances to the fitted line, assuming a known error variance ratio.</p>
<p>While classical regression assumes the input x is known precisely — or has negligible variance — this is often not the case in calibration tasks, particularly when inputs are indirectly observed or reversed during deployment.</p>
<p>Deming regression has two key limitations here:</p>
<ol type="1">
<li><p>It applies only to linear models — not to polynomials like the one used above;</p></li>
<li><p>It requires the specification of the error variance ratio between <img src="https://latex.codecogs.com/png.latex?x"> and <img src="https://latex.codecogs.com/png.latex?y">. In practice, this ratio must be inferred from physical knowledge of the system, including the intrinsic noise of the transducer and the propagation of input uncertainty through the calibration curve.</p></li>
</ol>
</div>
</div>
<p>While orthogonal regression becomes numerically complex in nonlinear settings, the next section takes a different route — not to improve the regression fit, but to explore how output noise affects the inference of input values. The goal is to simulate, under known conditions, how a calibrated system behaves when used in reverse.</p>
</section>
</section>
<section id="inverse-prediction-under-different-noise-levels" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="inverse-prediction-under-different-noise-levels">Inverse prediction under different noise levels</h2>
<p>This section illustrates a simulation-based strategy for inverse prediction. Given a target output, the goal is to characterize the distribution of input values <img src="https://latex.codecogs.com/png.latex?x"> that could plausibly have generated it, under the assumption that the noise affecting the output is Gaussian and matches the standard deviation used during calibration.</p>
<p>The procedure is as follows:</p>
<ol type="1">
<li><p>Fix a target output <img src="https://latex.codecogs.com/png.latex?y%5E*">;</p></li>
<li><p>Generate samples of random noise <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon_i%20%5Csim%20%5Cmathcal%7BN%7D(0,%20%5Csigma%5E2)">, using the known standard deviation;</p></li>
<li><p>For each sample, solve the perturbed equation:</p></li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0Ay(x_i)=y%5E*+%5Cvarepsilon_i%0A"></p>
<p>using the truth model (i.e., the exact polynomial coefficients), to obtain a sample of input values consistent with the observed output.</p>
<p>This approach provides a probabilistic reconstruction of the likely inputs that could have produced a given output under a known model with output noise. It highlights how variability in the output propagates backward through the system, inducing uncertainty in the inferred inputs.</p>
<p>To illustrate how noise affects inverse estimation, two scenarios are compared. The first replicates the controlled conditions of the calibration process, where the noise level is low and well-characterized. The second represents a more realistic deployment setting, where the same transducer operates in a less stable environment, leading to larger variability in the output. By repeating the Monte Carlo simulation under both conditions, it becomes possible to observe how increased noise distorts the distribution of plausible input values — including shifts in bias, widening of confidence intervals, and an increase in failed inversion attempts due to extrapolation beyond the calibrated domain Figure&nbsp;3.</p>
<div class="cell">
<details class="code-fold">
<summary>Compare two noise scenarios for inverse prediction</summary>
<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">n_sim <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;">5000</span></span>
<span id="cb4-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First scenario: sigma = 1</span></span>
<span id="cb4-3">sigma_A <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sigma</span>
<span id="cb4-4">eps_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;">rnorm</span>(n_sim, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, sigma_A)</span>
<span id="cb4-5">x_inv_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;">sapply</span>(y_star <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> eps_A, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(y_t) {</span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tryCatch</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">uniroot</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;">f_true</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y_t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</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;">upper =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>root, </span>
<span id="cb4-7">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>)</span>
<span id="cb4-8">})</span>
<span id="cb4-9">x_emp_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;">mean</span>(x_inv_A, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-10">bias_A   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x_emp_A <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x_hat</span>
<span id="cb4-11">ci_emp_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;">quantile</span>(x_inv_A, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">probs =</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.025</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.975</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-12">n_NA_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;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(x_inv_A))</span>
<span id="cb4-13"></span>
<span id="cb4-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Second scenario: sigma = 2</span></span>
<span id="cb4-15">sigma_B <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="cb4-16">eps_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;">rnorm</span>(n_sim, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, sigma_B)</span>
<span id="cb4-17">x_inv_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;">sapply</span>(y_star <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> eps_B, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(y_t) {</span>
<span id="cb4-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tryCatch</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">uniroot</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;">f_true</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y_t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</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;">upper =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>root, </span>
<span id="cb4-19">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>)</span>
<span id="cb4-20">})</span>
<span id="cb4-21">x_emp_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;">mean</span>(x_inv_B, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-22">bias_B   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> x_emp_B <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> x_hat</span>
<span id="cb4-23">ci_emp_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;">quantile</span>(x_inv_B, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">probs =</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.025</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.975</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-24">n_NA_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;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(x_inv_B))</span>
<span id="cb4-25"></span>
<span id="cb4-26">df_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;">tibble</span>(</span>
<span id="cb4-27">  <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;">c</span>(x_inv_A, x_inv_B),</span>
<span id="cb4-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scenario =</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;">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;">"Calibration (σ = 1)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deployment (σ = 2)"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> n_sim)))</span></code></pre></div>
</details>
</div>
<div class="cell" data-tbl-caption="Monte Carlo simulation: distribution of input values consistent with the observed output">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="font-size: 14px; width: auto !important; margin-left: auto; margin-right: auto;">
<caption style="font-size: initial !important;">Monte Carlo simulation under two noise scenarios. The table summarizes the distribution of input values consistent with the target output. The number of failed inversions (NA) indicates the samples falling outside the measurement interval.</caption>
 <thead>
  <tr>
   <th style="text-align:left;"> Scenario </th>
   <th style="text-align:right;"> Monte Carlo mean (x̄) </th>
   <th style="text-align:right;"> Bias </th>
   <th style="text-align:left;"> 95% CI (quantiles) </th>
   <th style="text-align:right;"> Failed inversions (NA) </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> σ = 1 (calibration) </td>
   <td style="text-align:right;"> 3.494 </td>
   <td style="text-align:right;"> -0.005 </td>
   <td style="text-align:left;"> [3.112, 3.837] </td>
   <td style="text-align:right;"> 0 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> σ = 2 (deployment) </td>
   <td style="text-align:right;"> 3.474 </td>
   <td style="text-align:right;"> -0.024 </td>
   <td style="text-align:left;"> [2.663, 4.136] </td>
   <td style="text-align:right;"> 0 </td>
  </tr>
</tbody>
</table>

</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">
Notation key
</div>
</div>
<div class="callout-body-container callout-body">
<p><img src="https://latex.codecogs.com/png.latex?y%5E*">: target output to be inverted (deployment phase)</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Chat%7Bx%7D">: input estimate obtained by inverting the fitted model</p>
<p><img src="https://latex.codecogs.com/png.latex?x_i">: individual input values sampled via Monte Carlo simulation</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D">: empirical mean of the Monte Carlo input distribution</p>
<p>Bias: computed as <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D%20-%20%5Chat%7Bx%7D"></p>
</div>
</div>
<div id="fig-3" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/calibration/fig3.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:75.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Monte Carlo density estimates of input values consistent with the target output under two noise conditions. Blue: calibration scenario (<img src="https://latex.codecogs.com/png.latex?%5Csigma%20=%201">). Red: deployment scenario (<img src="https://latex.codecogs.com/png.latex?%5Csigma%20=%202">). The vertical dashed line marks <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bx%7D">, the input estimate obtained by inverting the fitted model. As noise increases, the density skews leftward — a consequence of the fact that sensitivity (i.e., the slope of the calibration curve) tends to increase with input value. This results in asymmetric confidence intervals, with heavier tails toward lower <img src="https://latex.codecogs.com/png.latex?x"> values.
</figcaption>
</figure>
</div>
<p>This method illustrates how even a fixed output measurement may correspond to a range of plausible inputs. The spread of this distribution depends on the noise level, the local slope of the calibration curve near <img src="https://latex.codecogs.com/png.latex?y%5E*">, and the model’s nonlinearity.</p>
<p>In linear settings, Monte Carlo inversion recovers the classical prediction interval for <img src="https://latex.codecogs.com/png.latex?x%5E*"> derived from theory. In nonlinear cases, however, simulation becomes essential to reveal the actual shape and spread of the inverse uncertainty.</p>
<p><strong>Key observations:</strong></p>
<p>The peak of the simulated density typically does not align with <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bx%7D">, the inverse prediction from the fitted model. This reflects a small bias introduced by the asymmetry of the transformation <img src="https://latex.codecogs.com/png.latex?x%20%5Crightarrow%20y">, which distorts symmetric output noise into skewed input uncertainty.</p>
<p>Despite Gaussian noise on the output, the resulting distribution of plausible <img src="https://latex.codecogs.com/png.latex?x"> values is not normal. Nonlinear calibration curves map symmetric errors into asymmetric input spreads, especially in regions with varying slope or curvature.</p>
<p>As noise increases, the inverse distribution becomes wider and increasingly left-skewed. This behavior is consistent with the increasing sensitivity of the model at higher <img src="https://latex.codecogs.com/png.latex?x"> values and results in confidence intervals that shift and become asymmetric — as clearly shown in Figure&nbsp;3. However, the use of numerical solvers like <code>uniroot()</code> comes with important caveats near the edges of the calibration domain. When the slope of the model becomes very small — as it often does at low input values — the inversion becomes ill-conditioned: small variations in output may correspond to large variations in input, or even result in multiple or no real-valued solutions. In such cases, the solver may return only one of several possible roots, typically the one closer to the lower boundary. This effect contributes to the asymmetric shape and heavy left tail of the inverse distribution shown in Figure&nbsp;3, even when the output noise itself is symmetric.</p>
<p>A further complication arises from the fitted polynomial model used for inversion. While the interpolating polynomial approximates the calibration curve well over central regions, it tends to oscillate or flatten toward the boundaries, particularly in higher-order fits. These edge effects can amplify instability in the inverse prediction — both for numerical solvers and for lookup-table interpolation — unless the prediction range is appropriately restricted or the model complexity reduced.</p>
</section>
<section id="a-final-word-this-is-the-optimistic-case" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="a-final-word-this-is-the-optimistic-case">A final word: this is the optimistic case</h2>
<p>The Monte Carlo simulations shown so far illustrate a powerful approach to inverse prediction — but under conditions that are more optimistic than most real-world applications. In particular, the numerical solver was applied to the truth model, i.e., the same polynomial function used to generate the data. This means that, although noise was present in the output, the model structure was assumed to be perfectly known and invertible. In reality, due to computational constraints at deployment time or industrial protocols that can prevent real-time inversion via numerical methods, inverse prediction is rarely implemented via numerical solvers — more often, it relies on lookup tables derived from synthetic calibration curves.</p>
<section id="a-first-step-toward-table-based-inverse-prediction" class="level3 unnumbered">
<h3 class="unnumbered anchored" data-anchor-id="a-first-step-toward-table-based-inverse-prediction">A first step toward table-based inverse prediction</h3>
<p>Lookup tables link input values to their expected outputs based on the fitted model, enabling fast and resource-efficient retrieval of approximate inverses during deployment.</p>
<p>The idea is simple:</p>
<ul>
<li>Step 1: Generate a synthetic grid of input values over the calibration interval;</li>
<li>Step 2: Evaluate the fitted model on each input to obtain corresponding outputs;</li>
<li>Step 3: Store the resulting pairs <img src="https://latex.codecogs.com/png.latex?(x_j,%20%5Chat%7By%7D_j)"> in a lookup table;</li>
<li>Step 4: At deployment, when an output <img src="https://latex.codecogs.com/png.latex?y%5E*"> is observed, find the two closest values in the table and return the associated <img src="https://latex.codecogs.com/png.latex?%5Chat%7Bx%7D"> (possibly via linear interpolation between adjacent points). Of course, this step can work only if the table columns are strictly monotonic.</li>
</ul>
<p>It is worth noting that the requirement of monotonicity is shared with the numerical solver: non-monotonic regions in the model may lead to multiple valid solutions or to failed inversions when the output falls outside the achievable range. However, the problem is often more acute with lookup tables. Unlike solvers, which can dynamically refine their search and isolate a valid root, lookup tables are constrained to a discrete grid of stored values — and interpolation may become unstable or misleading near flat or turning regions of the model.</p>
<p>This limitation highlights a trade-off between computational efficiency and robustness of inversion. It also suggests that, before deploying lookup-based inverse prediction, one should:</p>
<ol type="1">
<li>carefully inspect the fitted model for monotonicity;</li>
<li>possibly restrict the input range to regions where inversion is well-defined;</li>
<li>or simplify the model structure (e.g., reducing polynomial degree) to avoid non-monotonic behavior.</li>
</ol>
</section>
</section>
<section id="test-with-a-lookup-table-under-deployment-conditions" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="test-with-a-lookup-table-under-deployment-conditions">Test with a lookup table under deployment conditions</h2>
<p>To evaluate inverse prediction under realistic deployment conditions, I simulate noisy output observations centered at the target output, with a standard deviation of <img src="https://latex.codecogs.com/png.latex?%5Csigma=2">.</p>
<p>A lookup table is constructed from the fitted model, but only over a restricted interval where monotonicity has been both visually and numerically verified. This ensures that the inversion procedure remains well-defined and avoids regions where multiple or unstable solutions might occur.</p>
<div id="fig-4" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-4-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/calibration/fig4.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:75.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-4-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Density of inverse input values obtained via a lookup table. Because of the good quality of the table in terms of monotonicity and range, the distribution closely resembles that obtained using the numerical solver in Figure&nbsp;3. No inversion failures occurred in the Monte Carlo simulation. The 95% confidence interval is <img src="https://latex.codecogs.com/png.latex?%5B2.652,%204.126%5D">, with a Monte Carlo mean <img src="https://latex.codecogs.com/png.latex?%5Cbar%7Bx%7D%20=%203.471"> and a bias of approximately <img src="https://latex.codecogs.com/png.latex?-0.028">.
</figcaption>
</figure>
</div>
</section>
<section id="conclusion-and-outlook" class="level2 unnumbered">
<h2 class="unnumbered anchored" data-anchor-id="conclusion-and-outlook">Conclusion and outlook</h2>
<p>This post has explored the challenge of inverse prediction in static calibration, highlighting the often-overlooked asymmetry of uncertainty when working backward from output to input. Using both numerical solvers and lookup tables, I showed how Monte Carlo simulation can quantify the resulting variability — even in optimistic settings where the model is perfectly known and the only uncertainty comes from measurement noise.</p>
<p>Along the way, I emphasized that inverse prediction is more sensitive than forward prediction to model structure, output noise, and edge behavior. In particular, it should be remarked that:</p>
<ol type="1">
<li><p>confidence intervals become skewed and asymmetric as noise increases;</p></li>
<li><p>the numerical solver can fail silently near the domain boundaries;</p></li>
<li><p>lookup tables, while efficient and interpretable, require monotonicity and careful grid design.</p></li>
</ol>
<p>I deliberately kept the noise levels moderate and the model structure well-behaved. This allowed us to isolate key phenomena without introducing unnecessary complications. But real-world problems are rarely so kind: noise and non-monotonicity are recurring nightmares — not just in calibration, but in data-driven projects, where transducers often serve as the gateway to the information we rely on.</p>
<p>The next post in this series will dive deeper into these complications. I’ll explore what happens when the true model structure is unknown or misestimated, when noise levels are high enough to push the inverse prediction outside the measurement interval, and when the forward function is non-monotonic. These “edge cases” pose significant risks — but also offer valuable opportunities to rethink how calibration is designed and implemented in practice.</p>


</section>

 ]]></description>
  <category>measurement</category>
  <category>statistics</category>
  <guid>https://amsabatini.netlify.app/posts/calibration/</guid>
  <pubDate>Wed, 28 May 2025 22:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/calibration/calibration.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Statistics and Machine Learning: A shared landscape</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/statistics_vs_ML/</link>
  <description><![CDATA[ 




<p>One of the most important similarities between statistics and machine learning (ML) is that both rely heavily on models. While this connection is sometimes overlooked, it forms the backbone of how we organize and analyze data. Regardless of the number and type of variables involved—whether explanatory or response variables, continuous or categorical—statistical modeling remains at the core of the process. Whether our goal is to predict outcomes, uncover relationships, or assess differences, modeling gives structure and meaning to the patterns we observe. ML, too, builds on this idea of modeling, but with a slightly different emphasis. Here, the focus is often more explicitly on defining a mathematical mapping between inputs and outputs—a modeling aspect that tends to be more immediately visible than in traditional statistical practice.</p>
<p>Several practical factors make a full overlap between the two approaches less straightforward. One is the sheer difference in the size of datasets: classical statistical analyses often rely on relatively small samples, especially in fields such as medicine or social sciences. This distinction, however, is beginning to blur: modern applications in fields like digital health, education technology, or online behavioral studies increasingly generate larger datasets, enabling the use of ML-inspired methods even in traditionally small-sample domains. ML, by contrast, tends to operate in contexts where large datasets are available—or at least assumed. Another difference lies in the typical workflow: in ML, it is standard practice to split data into training, validation, and testing sets, managing the trade-off between fitting well and generalizing. This concern, central to ML, is less explicitly present in traditional statistical workflows.</p>
<p>Despite these differences, statistics and ML increasingly share common objectives and approaches: building models that capture meaningful patterns while balancing complexity against generalization.(For readers unfamiliar with some of the technical terms, a brief summary is provided in the Key Concepts sections below.)</p>
<section id="what-is-a-statistical-model" class="level2">
<h2 class="anchored" data-anchor-id="what-is-a-statistical-model">What is a statistical model?</h2>
<p>In classical statistics — for example, for methods introduced early in the training of sophomore students, such as simple linear regression and ANOVA (ANalysis Of VAriance) — the model is typically written in a very structured form. Let us suppose that <img src="https://latex.codecogs.com/png.latex?Y"> is the response variable (outcome) and <img src="https://latex.codecogs.com/png.latex?X_1,X_2,%5Ccdots,X_m"> are explanatory variables (predictors), collected into the vector <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D">; the model is the equation that we suppose relate together the predictors to the outcome, for any of a number <img src="https://latex.codecogs.com/png.latex?n"> of observational units, with an important element noticed in the formulation of the model-the random error term, <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon">:</p>
<p><span id="eq-model-general"><img src="https://latex.codecogs.com/png.latex?%0AY_%7Bi%7D=f(%5Cmathbf%7BX%7D_i;%5Ctheta)+%5Cvarepsilon_i,%5Cquad%20i=1,...,n%0A%5Ctag%7B1%7D"></span></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> are the parameters to be estimated. This decomposition highlights two essential ideas:</p>
<ul>
<li>There is an underlying systematic component (<img src="https://latex.codecogs.com/png.latex?f(%5Cmathbf%7BX%7D_i;%5Ctheta)">),</li>
<li>Plus an error component (<img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon_i">), which captures variability not explained by the model.</li>
</ul>
<p>The classical statistical inferential procedures—whether hypothesis testing or interval estimation—are fundamentally designed around the behavior of this error term. It is also important to realize that we can never observe the error term, but we can infer its behavior from the analysis of the model residual — the difference between observed and predicted values.</p>
<p>In classical parametric inference, crucial assumptions are made about the behavior of the error terms <img src="https://latex.codecogs.com/png.latex?%5Cvarepsilon_i"> in the general model Equation&nbsp;1.</p>
<p>To ensure analytical tractability and valid inference, it is common to summarize these assumptions using the acronym LINE — Linearity, Independence, Normality, and Equality of variance:</p>
<ol type="1">
<li><p><strong>Linearity</strong>: The model is linear in the parameters <img src="https://latex.codecogs.com/png.latex?%5Ctheta">; that is, the expected value of the outcome variable <img src="https://latex.codecogs.com/png.latex?Y"> can be expressed as a linear combination of the parameters, possibly through transformations of the predictors <img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7BX%7D">.</p></li>
<li><p><strong>Independence</strong>: The error terms are independent across observations. In particular, for time series data, consecutive errors are assumed to be uncorrelated. In cross-sectional data, failure of independence often reflects issues in experimental design, such as inadequate random assignment or hidden confounding. Detecting such problems can be challenging and typically requires careful planning at the data collection stage.</p></li>
<li><p><strong>Normality</strong>: The error terms are normally distributed with mean zero.</p></li>
<li><p><strong>Equality of variance (Homoscedasticity)</strong>: The error terms have constant variance across the range of the predictors.</p></li>
</ol>
<p>These assumptions provide the theoretical foundation for estimating parameters, constructing confidence intervals, and performing hypothesis tests in a coherent analytical framework. Understanding how residuals behave is not only crucial in classical inference, but also serves as a bridge toward modern predictive approaches, as discussed in the note below.</p>
<div class="callout callout-style-default callout-note callout-titled" title="Residuals and ML">
<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">
Residuals and ML
</div>
</div>
<div class="callout-body-container callout-body">
<p>The concept of residuals — as discrepancies between observed outcomes and model predictions — remains central in ML as well. Although the term “residual” is less frequently emphasized explicitly, the analysis of prediction errors underpins critical tasks such as model evaluation, hyperparameter tuning, and post-hoc diagnostics across supervised learning workflows.</p>
<p>Understanding residuals provides a natural bridge between classical inferential thinking and modern predictive modeling practices.</p>
</div>
</div>
<p>This careful structure is what allows classical statistics to calibrate confidence intervals, compute p-values, and estimate statistical power, all based on theoretically derived sampling distributions. However, when these assumptions are violated, the inferential reliability may break down — often in unpredictable ways. Modern approaches like <strong>bootstrap resampling</strong> offer alternatives that reduce dependency on strong theoretical assumptions, and interestingly, align closely with resampling strategies widely used in ML (such as cross-validation for hyperparameter tuning). In a sense, bootstrapping can be seen as a bridge: a classical method moving closer to the data-driven, flexible spirit that characterizes much of modern ML practice.</p>
</section>
<section id="key-concepts" class="level2">
<h2 class="anchored" data-anchor-id="key-concepts">Key Concepts</h2>
<div class="callout callout-style-default callout-important 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">
Statistics-Based Key Concepts
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li><p><strong>Estimator</strong>: A function of sample data used to infer the value of an unknown population parameter. Estimators form the basis for constructing confidence intervals and conducting hypothesis tests.</p></li>
<li><p><strong>Sampling Distribution</strong>: The probability distribution of an estimator over many hypothetical repeated samples from the same population.</p></li>
<li><p><strong>Confidence Interval</strong>: An estimated range, derived from an estimator, that likely contains the true value of the population parameter with a given level of confidence.</p></li>
<li><p><strong>p-value</strong>: The probability, under the null hypothesis, of obtaining a test statistic at least as extreme as the one observed.</p></li>
<li><p><strong>Statistical Power</strong>: The probability that a statistical test correctly rejects the null hypothesis when the alternative hypothesis is true.</p></li>
<li><p><strong>Type I Error</strong>: Incorrectly rejecting a true null hypothesis (false positive).</p></li>
<li><p><strong>Type II Error</strong>: Failing to reject a false null hypothesis (false negative).</p></li>
<li><p><strong>Residual</strong>: The difference between the observed value and the corresponding predicted value from a statistical model. Residuals are empirical proxies for the unobservable error terms, enabling diagnostic analyses of model fit and assumptions.</p></li>
<li><p><strong>Degrees of Freedom</strong>: The number of independent values in a calculation that are free to vary. In statistical inference, degrees of freedom determine the shape of sampling distributions (e.g., t or F) and reflect the amount of information available to estimate variability.</p></li>
</ul>
</div>
</div>
<div class="callout callout-style-default callout-important 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">
ML-based key concepts
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li><p><strong>Confusion Matrix</strong>: a table showing true positives, false positives, true negatives, and false negatives.</p></li>
<li><p><strong>ROC Curve</strong>: a plot of true positive rate vs false positive rate across different thresholds.</p></li>
<li><p><strong>ROC AUC</strong>: a single-number summary of a model’s discrimination ability across thresholds.</p></li>
<li><p><strong>Operating Point (Threshold Selection)</strong>: the decision threshold balancing false positives and false negatives, based on context.</p></li>
<li><p><strong>Class Imbalance</strong>: a condition where the categories in the outcome variable are unequally represented. It can lead to biased models, misleading performance metrics, and poor generalization — especially when one class dominates. Common remedies include resampling, weighting, and using metrics like ROC AUC instead of raw accuracy. Although widely addressed in ML, class imbalance also affects classical inference, where low-prevalence categories can distort p-values and test power.</p></li>
</ul>
</div>
</div>
<div class="callout callout-style-default callout-important 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">
Advanced key concepts
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li><p><strong>Cross-Entropy Loss</strong>: measures the dissimilarity between predicted probabilities and true labels.</p></li>
<li><p><strong>Regularization</strong>: techniques (e.g., L1, L2) to prevent overfitting by penalizing model complexity.</p></li>
<li><p><strong>Gradient Descent</strong>: an optimization method for minimizing loss functions.</p></li>
<li><p><strong>Overfitting and Underfitting</strong>: learning too much noise (overfitting) or missing the signal (underfitting).</p></li>
</ul>
</div>
</div>
</section>
<section id="from-hypothesis-testing-to-predictive-modeling" class="level2">
<h2 class="anchored" data-anchor-id="from-hypothesis-testing-to-predictive-modeling">From Hypothesis Testing to Predictive Modeling</h2>
<p>Let us now illustrate the modeling perspective starting with a classical example: the independent-samples t-test. First, the neded libraries are uploaded.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load required libraries</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidymodels)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(broom)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(kableExtra)</span></code></pre></div>
</details>
</div>
<section id="simulating-data-for-classical-inference" class="level3">
<h3 class="anchored" data-anchor-id="simulating-data-for-classical-inference">Simulating Data for Classical Inference</h3>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate two groups with slightly different means</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb2-3">group1 <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;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>)</span>
<span id="cb2-4">group2 <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;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>)</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine data into a single data frame</span></span>
<span id="cb2-7">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="cb2-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</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;">"B"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>))),</span>
<span id="cb2-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(group1, group2)</span>
<span id="cb2-10">)</span></code></pre></div>
</div>
<p>We simulate two groups, randomly drawn from two normally distributed populations, with means <img src="https://latex.codecogs.com/png.latex?%5Cmu_A%20=%205.0"> and <img src="https://latex.codecogs.com/png.latex?%5Cmu_B%20=%205.5">, and standard deviations <img src="https://latex.codecogs.com/png.latex?%5Csigma_A%20=%20%5Csigma_B%20=%201.0">. Each group contains <img src="https://latex.codecogs.com/png.latex?n%20=%2064"> observations. In the spirit of a real experiment, the population parameters are unknown — the only information we have comes from the observed data. Our inferential task is to test whether a difference exists between the group means, under the standard null and alternative hypotheses:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AH_0:%20%5Cmu_A%20=%20%5Cmu_B%20%5Cquad%5Cquad%20H_1:%20%5Cmu_A%20%5Cne%20%5Cmu_B%0A"></p>
<p>In this example, we simulate 64 observations per group — a total of 128 — to achieve 80% statistical power for detecting a medium effect size (<img src="https://latex.codecogs.com/png.latex?d%20=%200.5">) with a two-sided t-test at the conventional 5% significance level. This decision is not arbitrary: in a well-planned experiment, the sample size should be defined <em>before</em> data collection, based on acceptable error rates, either of Type I or Type II. Surprisingly, many — even published — experimental studies skip this crucial planning step, treating sample size as an afterthought rather than a design parameter.</p>
<p>This consideration can be visualized by examining how statistical power varies with the sample size and the effect size. The plot in Figure&nbsp;1 shows the power curves for three typical values of Cohen’s effect size (<img src="https://latex.codecogs.com/png.latex?d%20=%200.2">, <img src="https://latex.codecogs.com/png.latex?0.5">, <img src="https://latex.codecogs.com/png.latex?0.8">), representing small, medium, and large effects. The dashed line marks the conventional threshold of 80% power.</p>
<p>As the plot shows, detecting a medium effect with 80% reliability requires at least 64 observations <em>per group</em>. Informal rules of thumb such as “30 is enough” may drastically underestimate the sample size needed for meaningful results.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-power-curve" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-power-curve-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/statistics_vs_ML/power_curve_ttest.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-power-curve-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Power curves for two-sample t-tests at significance level (= 0.05), across three common effect sizes ((d = 0.2), (0.5), (0.8)). The dashed horizontal line marks the conventional 80% power threshold, often adopted as a design goal. The plot shows how detecting even a moderate effect requires at least 64 observations <em>per group</em> — far more than suggested by popular heuristics.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="performing-a-t-test-and-fitting-a-regression-model" class="level3">
<h3 class="anchored" data-anchor-id="performing-a-t-test-and-fitting-a-regression-model">Performing a t-test and Fitting a Regression Model</h3>
<p>In the snippet below, we perform a t-test and fit a regression model on the <em>same</em> simulated dataset. The outputs are produced by different functions but convey equivalent statistical information — all centered on testing whether the two group means differ. This equivalence is not accidental: it arises from how categorical variables are treated in regression models. Specifically, dummy (or indicator) coding when doing ANOVA translates group membership into a numeric predictor, allowing the regression coefficient to represent the mean difference between groups — exactly as tested by the t-test.</p>
<div class="cell">
<details class="code-fold">
<summary>Analysis code</summary>
<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;"># Perform an independent-samples t-test</span></span>
<span id="cb3-2">t_test_res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(outcome <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var.equal =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit a linear model with group as a predictor</span></span>
<span id="cb3-5">lm_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;">lm</span>(outcome <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> df)</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply ANOVA to the linear model</span></span>
<span id="cb3-8">anova_res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(lm_model)</span></code></pre></div>
</details>
</div>
</section>
<section id="display-results" class="level3">
<h3 class="anchored" data-anchor-id="display-results">Display results</h3>
<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>
→ t-test
──────────────</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Two Sample t-test

data:  outcome by group
t = -2.8597, df = 126, p-value = 0.004964
alternative hypothesis: true difference in means between group A and group B is not equal to 0
95 percent confidence interval:
 -0.7629029 -0.1388665
sample estimates:
mean in group A mean in group B 
       5.038478        5.489362 </code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
→ ANOVA
──────────────</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>Analysis of Variance Table

Response: outcome
           Df  Sum Sq Mean Sq F value   Pr(&gt;F)   
group       1   6.506  6.5055  8.1781 0.004964 **
Residuals 126 100.231  0.7955                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
→ Linear Model (summary)
──────────────</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = outcome ~ group, data = df)

Residuals:
     Min       1Q   Median       3Q      Max 
-2.29853 -0.59548 -0.05597  0.53407  2.19797 

Coefficients:
            Estimate Std. Error t value Pr(&gt;|t|)    
(Intercept)   5.0385     0.1115   45.19  &lt; 2e-16 ***
groupB        0.4509     0.1577    2.86  0.00496 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.8919 on 126 degrees of freedom
Multiple R-squared:  0.06095,   Adjusted R-squared:  0.0535 
F-statistic: 8.178 on 1 and 126 DF,  p-value: 0.004964</code></pre>
</div>
</div>
<p>How should we interpret these numbers? The goal of our analysis was to assess whether the two groups differ in their mean outcome. Both the t-test and the regression model provide consistent answers to this question.</p>
<p>In the regression output, the coefficient <code>groupB = 0.4509</code> represents the estimated difference in means between the two groups. The associated p-value (0.00496) indicates that this difference is statistically significant at the conventional 5% level — strong evidence against the null hypothesis <img src="https://latex.codecogs.com/png.latex?(H_0:%5Cmu_A=%5Cmu_B)">.</p>
<p>The F-statistic from the ANOVA table (8.178) confirms this result, and it is numerically equal to the square of the t-statistic from the regression output <img src="https://latex.codecogs.com/png.latex?(F=t%5E2)">, as expected when comparing two groups using a linear model.</p>
<p>The residual standard error (0.8919) provides an estimate of the variability within groups, and the degrees of freedom (126) reflect that all available data were used for estimation. This is typical in classical inference, where no data is held out for validation — in contrast with ML practice.</p>
<div class="callout callout-style-default callout-tip callout-titled" title="🎯 Interpretation">
<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">
🎯 Interpretation
</div>
</div>
<div class="callout-body-container callout-body">
<p>As expected, the t-statistic and the p-value from the t-test correspond exactly to the t-statistic and p-value for the group coefficient in the regression model.</p>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled" title="t, F and the language of models">
<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">
t, F and the language of models
</div>
</div>
<div class="callout-body-container callout-body">
<p>Although <code>t.test()</code> and <code>lm()</code> appear to be different tools, they both test the same hypothesis — that the group means differ. The t-statistic for the group coefficient in the regression is identical to the one returned by the t-test. Furthermore, the <code>anova()</code> function applied to the linear model produces an F-statistic equal to the square of the t-statistic: <img src="https://latex.codecogs.com/png.latex?(F%20=%20t%5E2)">. Different outputs, same logic.</p>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled" title="Normality and sample size">
<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">
Normality and sample size
</div>
</div>
<div class="callout-body-container callout-body">
<p>The values of the t and F statistics shown in the tables are derived from the Student’s t and Fisher’s F distributions, respectively. These distributions — and the reliability of the associated inference — rely on the assumption that residuals follow a normal distribution.</p>
<p>This assumption is especially critical when sample sizes are small. As sample size increases, the central limit theorem ensures that the sampling distribution of the test statistic becomes approximately normal, even if the underlying data are not. With 64 observations per group, the inference is considered robust to moderate departures from normality.</p>
</div>
</div>
<p>The p-value expresses the degree of statistical surprise associated with the observed value of the test statistic — under the assumption that the null hypothesis is true. In other words, it quantifies how incompatible the data are with what would be expected from the sampling distribution under the null. Paradoxically, in statistical practice we often look for surprise: the lower the p-value, the more tempted we are to conclude that <strong>the null hypothesis may not hold</strong>.</p>
<p>The degrees of freedom associated with a statistical model offer a practical indication of its vulnerability to overfitting. Since they increase with the amount of data and decrease with the number of parameters estimated, a low number of degrees of freedom is a warning signal: the model may be too complex relative to the information available. In traditional statistical practice, this aspect is often underappreciated — yet it plays a critical role in determining the stability and generalizability of inferential results.</p>
<p>This example shows how seemingly different statistical tools — a t-test, a linear model, and an ANOVA — all converge toward the same conclusion, using distinct but mathematically connected representations. What may appear at first as separate techniques are, in fact, different expressions of the same modeling logic.</p>
<p>It is also worth noting that, in this classical setting, all available observations — 64 per group, for a total of 128 — are used entirely for model estimation. No data is reserved for model validation. All degrees of freedom are “spent” in fitting and inference, with no external check on how well the model might generalize to new data. This design choice — to use all data for estimation — is rooted in the assumptions and goals of classical inference. In contrast, ML workflows routinely reserve part of the data for validation, tuning, and performance assessment.</p>
</section>
</section>
<section id="from-inference-to-prediction-a-first-machine-learning-example" class="level2">
<h2 class="anchored" data-anchor-id="from-inference-to-prediction-a-first-machine-learning-example">From Inference to Prediction: A First Machine Learning Example</h2>
<p>We now shift from inference to prediction. In supervised ML, model validation is built into the workflow — often using separate training and testing sets — to evaluate how well the model generalizes.</p>
<p>Before diving deeper, it’s worth highlighting that although ML is often associated with “algorithms,” the core idea remains modeling. Algorithms are tools to find models — representations of patterns in the data — much as in statistics. The shift lies in the greater flexibility and adaptability expected of models built via ML.</p>
<p>Let’s now construct a simple classification example using simulated data. To keep the comparison with the previous example meaningful, we simulate two equally sized groups — 64 observations each — and generate two continuous predictors for classification. As with the t-test setting, the balanced design is not strictly necessary, but it remains highly advisable. Severe class imbalance can distort statistical inference and degrade the performance of classifiers, especially when probabilistic calibration is involved.</p>
<p>Wherever possible, experimental design should strive for balanced or near-balanced group sizes. In statistical inference, imbalance may reduce the precision of estimates or distort p-values. In ML, it can lead to biased classifiers, especially when the outcome classes are highly skewed. While techniques exist to handle imbalance — such as weighting, oversampling, or downsampling, it is good experimental practice to minimize it at the design stage whenever possible.</p>
<section id="simulating-data-for-binary-classification" class="level3">
<h3 class="anchored" data-anchor-id="simulating-data-for-binary-classification">Simulating Data for Binary Classification</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate data</span></span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb10-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;">100</span></span>
<span id="cb10-4">x1 <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</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;">rnorm</span>(n, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.75</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb10-5">x2 <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</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;">rnorm</span>(n, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.75</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb10-6">class <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>(<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;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, n), <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 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;">"0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>))</span>
<span id="cb10-7"></span>
<span id="cb10-8">df_ml <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x1 =</span> x1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x2 =</span> x2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">class =</span> class)</span></code></pre></div>
</div>
<p>The two classes are linearly separable, by design — each group was generated from a distinct bivariate Gaussian distribution, Figure&nbsp;2. This separation facilitates learning and allows us to explore classification behavior in a clean, controlled setting.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-bivariate-dataset" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-bivariate-dataset-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/statistics_vs_ML/index_files/figure-html/fig-bivariate-dataset-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-bivariate-dataset-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Simulated dataset with two linearly separable classes, used to illustrate binary classification.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="fitting-a-logistic-regression-model" class="level3">
<h3 class="anchored" data-anchor-id="fitting-a-logistic-regression-model">Fitting a Logistic Regression Model</h3>
<p>Although the name might suggest otherwise, logistic regression is not a method for modeling continuous outcomes. It is, in fact, one of the simplest and most widely used algorithms for <strong>binary classification</strong>. Given its solid statistical foundations, efficiency, and interpretability, logistic regression often serves as a reliable first choice in many applied ML workflows.</p>
<p>Rather than predicting a numeric value, the model estimates the <strong>probability that each observation belongs to a given class</strong> — typically class 1. The model learns a relationship between the features and this probability, and a threshold (usually 0.5) is then applied to assign class labels.</p>
<p>Here we use the <a href="https://www.tidymodels.org/"><code>tidymodels</code></a> framework to fit the model. Tidymodels provides a unified and expressive syntax for specifying, training, and evaluating models in R, following the tidyverse principles.</p>
<p>To keep the example simple but realistic, we perform a basic train/test split before fitting the model. This reflects a fundamental aspect of the ML workflow: models are expected to generalize to new, unseen data. In contrast to traditional statistical modeling — where inference is often based on the full dataset, with uncertainty captured via confidence intervals or p-values — ML emphasizes predictive performance on held-out data.</p>
<p>Since logistic regression has no tunable hyperparameters, and our example involves just two features, we do not perform cross-validation or model tuning. A single split suffices to illustrate the predictive logic.</p>
<div class="cell">
<details class="code-fold">
<summary>Analysis code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Split and annotate</span></span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb11-3">split <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;">initial_split</span>(df_ml, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strata =</span> class)</span>
<span id="cb11-4">train_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;">training</span>(split) <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">set =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Train"</span>)</span>
<span id="cb11-5">test_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;">testing</span>(split)  <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">set =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Test"</span>)</span>
<span id="cb11-6"></span>
<span id="cb11-7">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;">bind_rows</span>(train_df, test_df)</span>
<span id="cb11-8"></span>
<span id="cb11-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit logistic regression on training set</span></span>
<span id="cb11-10">model_ml <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;">logistic_reg</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_engine</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"glm"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit</span>(class <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> x2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> train_df)</span></code></pre></div>
</details>
</div>
</section>
<section id="visualizing-the-decision-boundary" class="level3">
<h3 class="anchored" data-anchor-id="visualizing-the-decision-boundary">Visualizing the Decision Boundary</h3>
<p>In classical statistical testing, the choice of an operational point is often driven by conventions: a significance level of 0.05 and a power of 0.80 are typically adopted without extensive reflection. In ML, however, this decision is more explicitly tied to the ROC curve. The model provides estimated probabilities, and it is up to the practitioner — in consultation with stakeholders — to choose a classification threshold that reflects real-world priorities: minimizing false positives, false negatives, or some balanced trade-off. In the example discussed here, the boundary decision is plotted as a line over the point distributions of the two classes, as shown in Figure&nbsp;3.</p>
<div class="cell">
<div class="cell-output-display">
<div id="fig-bivariate-decision" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-bivariate-decision-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/statistics_vs_ML/index_files/figure-html/fig-bivariate-decision-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-bivariate-decision-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Decision boundary produced by a logistic regression classifier fitted on the training set (circles). Points in the test set are shown as triangles. Class 0 and Class 1 are represented in blue and red, respectively. The model learns to separate the two classes by estimating a probability of class membership and applying a threshold (here, 0.5). This visualization helps assess how well the model generalizes, especially when overlaid on unseen test data.
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="evaluating-performance-roc-curve-and-auc" class="level3">
<h3 class="anchored" data-anchor-id="evaluating-performance-roc-curve-and-auc">Evaluating Performance: ROC Curve and AUC</h3>
<p>We now evaluate model performance using the ROC curve, introduced earlier in the <em>ML-based key concepts</em> callout.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict and evaluate</span></span>
<span id="cb12-2">test_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> test_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_cols</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model_ml, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> test_df, <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;">"prob"</span>))</span>
<span id="cb12-4"></span>
<span id="cb12-5">roc_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;">roc_curve</span>(test_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> class, .pred_0)</span>
<span id="cb12-6">auc_val  <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;">roc_auc</span>(test_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> class, .pred_0)</span>
<span id="cb12-7"></span>
<span id="cb12-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict on training set (add probabilities + class prediction)</span></span>
<span id="cb12-9">train_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> train_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_cols</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(model_ml, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_data =</span> ., <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;">"prob"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-11">  <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;">pred_class =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(.pred_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span>) <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;">factor</span>(<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;">"0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>)))</span>
<span id="cb12-12"></span>
<span id="cb12-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ensure predicted class exists on test set</span></span>
<span id="cb12-14">test_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> test_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-15">  <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;">pred_class =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(.pred_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span>) <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;">factor</span>(<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;">"0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>)))</span></code></pre></div>
</details>
</div>
<p>This curve, shown in Figure&nbsp;4, summarizes the trade-off between sensitivity and specificity across all possible thresholds, and provides a basis for threshold-independent comparison between classifiers.</p>
<div class="cell">
<div class="cell-output-display">
<div id="fig-roc" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-roc-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/statistics_vs_ML/index_files/figure-html/fig-roc-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-roc-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: ROC curve for the logistic regression model, evaluated on the test set. The curve shows how sensitivity (true positive rate) and specificity (1 - false positive rate) evolve as the decision threshold varies. This version reproduces the staircase structure typical of ROC curves, using a step plot with cleaned and ordered data. The dashed line represents chance-level performance. The area under the curve (AUC) summarizes overall discriminative ability.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The ROC curve provides a global view of model discrimination across all possible thresholds, but model decisions are ultimately made at specific thresholds. This is where the confusion matrix (Figure&nbsp;5) and the classification metrics table come into play: they reveal how a fixed cutoff — here 0.5 — translates into concrete outcomes in terms of correct and incorrect predictions. Such threshold-dependent evaluations are essential when aligning models with real-world decision-making contexts.</p>
<div class="cell">
<div class="cell-output-display">
<div id="fig-confmat" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-confmat-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/statistics_vs_ML/index_files/figure-html/fig-confmat-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-confmat-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: Confusion matrix for the logistic regression model on the test set (threshold = 0.5). The matrix summarizes the number of correct and incorrect predictions, broken down by actual and predicted class. It highlights how model decisions play out at a specific threshold, complementing the global view offered by the ROC curve.
</figcaption>
</figure>
</div>
</div>
</div>
<p>To complement the confusion matrix, we report key performance metrics in Table&nbsp;1. These include accuracy, sensitivity (recall for the positive class), and specificity (true negative rate), computed separately on the training and test sets.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute each metric separately, then combine</span></span>
<span id="cb13-2">metrics_train <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;">tibble</span>(</span>
<span id="cb13-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">set =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Train"</span>,</span>
<span id="cb13-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> yardstick<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;">accuracy</span>(train_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> class, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> pred_class) <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;">pull</span>(.estimate),</span>
<span id="cb13-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sensitivity =</span> yardstick<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;">sens</span>(train_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> class, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> pred_class) <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;">pull</span>(.estimate),</span>
<span id="cb13-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">specificity =</span> yardstick<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;">spec</span>(train_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> class, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> pred_class) <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;">pull</span>(.estimate)</span>
<span id="cb13-7">)</span>
<span id="cb13-8"></span>
<span id="cb13-9">metrics_test <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;">tibble</span>(</span>
<span id="cb13-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">set =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Test"</span>,</span>
<span id="cb13-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">accuracy =</span> yardstick<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;">accuracy</span>(test_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> class, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> pred_class) <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;">pull</span>(.estimate),</span>
<span id="cb13-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sensitivity =</span> yardstick<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;">sens</span>(test_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> class, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> pred_class) <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;">pull</span>(.estimate),</span>
<span id="cb13-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">specificity =</span> yardstick<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;">spec</span>(test_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> class, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> pred_class) <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;">pull</span>(.estimate)</span>
<span id="cb13-14">)</span></code></pre></div>
</details>
</div>
<p>This summary highlights how well the model generalizes and reinforces the idea that threshold selection — while often fixed by convention in statistical testing — plays a pivotal role in applied ML.</p>
<div class="cell">
<div id="tbl-performance-metrics" 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-performance-metrics-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: Summary of classification metrics at threshold 0.5 for training and test sets. Reported metrics include accuracy, sensitivity (recall for class 1), and specificity (true negative rate). These help interpret model performance beyond the AUC, and illustrate how different metrics depend on the choice of decision threshold.
</figcaption>
<div aria-describedby="tbl-performance-metrics-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="font-size: 14px; width: auto !important; margin-left: auto; margin-right: auto;">
<caption style="font-size: initial !important;"></caption>
 <thead>
  <tr>
   <th style="text-align:left;"> set </th>
   <th style="text-align:right;"> accuracy </th>
   <th style="text-align:right;"> sensitivity </th>
   <th style="text-align:right;"> specificity </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> Train </td>
   <td style="text-align:right;"> 0.76 </td>
   <td style="text-align:right;"> 0.787 </td>
   <td style="text-align:right;"> 0.733 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Test </td>
   <td style="text-align:right;"> 0.74 </td>
   <td style="text-align:right;"> 0.680 </td>
   <td style="text-align:right;"> 0.800 </td>
  </tr>
</tbody>
</table>

</div>
</div>
</figure>
</div>
</div>
</section>
</section>
<section id="concluding-remarks" class="level2">
<h2 class="anchored" data-anchor-id="concluding-remarks">Concluding remarks</h2>
<p>The performance table above reveals not just how well the model generalizes from training to test data, but also highlights a deeper contrast between traditional <em>statistical inference</em> and <em>machine learning</em> practice. In classical statistics, the choice of a decision threshold — say, a significance level <img src="https://latex.codecogs.com/png.latex?%5Calpha=0.05"> — is often fixed by convention, serving as a gatekeeper for hypothesis testing. In ML, by contrast, the threshold (<img src="https://latex.codecogs.com/png.latex?0.5"> in our case) is not sacred: it can and should be <em>adapted</em> depending on context, cost, and stakeholder priorities.</p>
<p>The ROC curve offers a <em>global view</em> of the model’s discriminative ability across all thresholds, while the confusion matrix and the table of metrics make concrete the implications of a <em>specific threshold</em>. Together, they illustrate that prediction is not just about <em>accuracy</em>. It’s about <em>decisions</em>, <em>trade-offs</em>, and the <em>flexibility to adapt</em> models to real-world needs. And that’s precisely where statistical reasoning and ML begin to meet — not in opposition, but in <em>dialogue</em>.</p>
<p>The tension between theoretical rigor and empirical adaptability will continue to shape the evolving relationship between statistics and ML. In future explorations, we will examine how models behave when ideal conditions break down, and how different strategies — from regularization to robust modeling — attempt to meet the timeless challenge of separating signal from noise.</p>
</section>
<section id="further-reading" class="level2">
<h2 class="anchored" data-anchor-id="further-reading">Further Reading</h2>
<p>If you’d like to dig deeper into the ideas behind inference, prediction, and modern data modeling, here are some recommended readings — ranging from foundational texts to more advanced perspectives:</p>
<p><strong><em>All of Statistics</em></strong> — Larry Wasserman (2004)<br> A concise and mathematically grounded guide to statistical reasoning, ideal for those transitioning into data science.</p>
<p><strong><em>Statistics</em></strong> — Freedman, Pisani, Purves (2007)<br> A clear and accessible introduction to the logic and language of statistics.</p>
<p><strong><em>Applied Predictive Modeling</em></strong> — Kuhn &amp; Johnson (2013)<br> A hands-on resource focused on practical machine learning with a statistical backbone.</p>
<p><strong><em>Deep Learning</em></strong> — Goodfellow, Bengio, Courville (2016)<br> The reference text for deep learning, covering theory and implementation.</p>
<p><strong><em>Computer Age Statistical Inference</em></strong> — Efron &amp; Hastie (2016)<br> A beautifully written journey through the convergence of statistics and algorithmic thinking.</p>


</section>

 ]]></description>
  <category>statistics</category>
  <category>machine learning</category>
  <guid>https://amsabatini.netlify.app/posts/statistics_vs_ML/</guid>
  <pubDate>Sun, 04 May 2025 22:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/statistics_vs_ML/roc-thumb.png" medium="image" type="image/png" height="115" width="144"/>
</item>
<item>
  <title>Supercharge Deep Learning in R with a hybrid R–Colab workflow</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/parallel_processing_GPU/</link>
  <description><![CDATA[ 




<p>Exploring deep learning in R with the Keras and tensorflow packages reveals an intuitive and powerful interface. With just a few lines of R code, deep neural networks can be built, trained, and evaluated for tasks such as image recognition, time series forecasting, or natural language processing—all while maintaining the efficiency and flexibility of a tidyverse-powered workflow.</p>
<p>But here’s the catch: training deep learning models on a typical laptop or single-CPU machine can be painfully slow. GPUs are built to handle the massive parallelism required for deep learning, but most R users don’t have one sitting idle in their local setup.</p>
<p>Enter Google Colab—a free, cloud-based environment that provides access to powerful NVIDIA GPUs, without the need for installation or cost. It offers the experience of renting a high-performance machine for deep learning, directly from a browser. For many R users, this opens up a new realm of possibilities, enabling models that would take hours to train on a local machine to run in mere minutes.</p>
<p>But there’s one little twist: Colab speaks Python. The environment is built for the Python ecosystem, especially tools like TensorFlow, PyTorch, and Keras—all in their native Python form.</p>
<p>This doesn’t mean R users are excluded. Instead, it offers an opportunity to combine the strengths of both environments. All data preparation, exploration, and evaluation can be done in R using tools like dplyr, ggplot2, and yardstick, while the deep learning model training can be delegated to Colab. Colab serves as an outsourced training assistant—working seamlessly in Python.</p>
<p>This hybrid approach requires some initial setup, but once established, it becomes smooth, scalable, and remarkably efficient. R can be used for data wrangling, visualization, and model evaluation—leveraging the power of the tidyverse. When it’s time to train the model, the heavy lifting can be offloaded to Colab, with its GPU handling the computations. The two environments—R and Python—communicate through shared files, ensuring a flexible, fast, and efficient workflow.</p>
<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">
Google Colab
</div>
</div>
<div class="callout-body-container callout-body">
<p>Google Colab (short for Colaboratory) is a free, cloud-based platform designed for executing Python code with minimal setup. It provides access to high-performance hardware accelerators, including GPUs and TPUs, making it especially valuable for deep learning tasks that would otherwise be time-consuming on a standard CPU.</p>
<p>Although Colab is Python-native, it can be seamlessly integrated into an R-based workflow. Data preprocessing, visualization, and evaluation can remain in R, while model training is delegated to Colab, leveraging its GPU capabilities. This hybrid approach combines the strengths of both environments—tidyverse-friendly development and accelerated training performance.</p>
</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">
Connecting Google Colab to Google Drive
</div>
</div>
<div class="callout-body-container callout-body">
<p>Google Colab offers a powerful, cloud-based environment with access to free GPU acceleration. However, Colab notebooks run in a temporary virtual machine, meaning any files saved during a session are lost when the session ends.</p>
<p>To support reproducibility and a hybrid R–Python workflow, it’s essential to connect Colab to Google Drive. This enables persistent storage, shared access to data and model outputs, and smooth integration between Colab and local R projects.</p>
<p>In order to connect the following code is added at the top of the Colab notebook:</p>
<div class="cell">
<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;">from</span> google.colab <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> drive</span>
<span id="cb1-2">drive.mount(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/content/drive'</span>)</span></code></pre></div>
</div>
<p>The user is then prompted to authorize access to her/his Google account. Once mounted, the Drive will be available at:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>content<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>drive<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>MyDrive<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span></span></code></pre></div>
</div>
<p>The user can now read from and write to any project folder shared via Google Drive.</p>
</div>
</div>
<section id="full-mnist-classification-pipeline-in-r" class="level2">
<h2 class="anchored" data-anchor-id="full-mnist-classification-pipeline-in-r">Full MNIST Classification Pipeline in R</h2>
<p>The MNIST (Modified National Institute of Standards and Technology) dataset is a widely used benchmark for testing machine learning algorithms, particularly in the field of deep learning. It consists of 70,000 handwritten digit images, each labeled with the corresponding digit (0-9). The images are 28x28 pixels in grayscale, making them a relatively simple task for modern machine learning models, though still an effective tool for evaluating model performance.</p>
<p>The following R code snippet presents the entire pipeline for the MNIST classification task using Keras for deep learning. This pipeline includes:</p>
<ul>
<li><strong>Data Preparation</strong>: Loading and preprocessing the MNIST dataset, including normalization and reshaping of image data to fit the model.</li>
<li><strong>Model Definition</strong>: Constructing a deep neural network (DNN) using Keras, with layers for dense processing, dropout regularization, and softmax output.</li>
<li><strong>Model Training</strong>: Compiling and fitting the model to the training data, while using a validation split for model evaluation.</li>
<li><strong>Performance Evaluation</strong>: Timing the training process and calculating the overall duration.</li>
<li><strong>Exporting Data</strong>: Saving both the training and test datasets, as well as predictions, for further analysis or use in external systems.</li>
</ul>
<p>The implementation also ensures reproducibility by setting a fixed random seed across the entire workflow, and leverages the tidyverse for data manipulation and visualization, alongside the Keras library for deep learning.</p>
<p>This pipeline provides a streamlined approach to model development, training, and evaluation while integrating seamlessly with Python-based environments (such as Google Colab) for GPU-accelerated training.</p>
<p><strong>Minimal setup</strong></p>
<p>This section outlines the essential libraries for the project and specifies the base directory path at which Google Drive is mounted, enabling file access within both R and Colab-based Python environments.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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>(tensorflow)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(keras)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(reticulate)</span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb3-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(yardstick)</span>
<span id="cb3-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readr)</span>
<span id="cb3-8"></span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define Google Drive base path</span></span>
<span id="cb3-10">gdrive_base <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/Library/CloudStorage/GoogleDrive-&lt;your-mail&gt;@gmail.com/My Drive/&lt;your-project-folder&gt;"</span></span></code></pre></div>
</details>
</div>
<p><strong>Load and preprocess the MNIST dataset</strong></p>
<p>The dataset is reshaped to match the input requirements of a fully connected (dense) neural network architecture. Additionally, class labels are one-hot encoded to facilitate categorical classification during model training.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">mnist <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;">dataset_mnist</span>()</span>
<span id="cb4-2"></span>
<span id="cb4-3">x_train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mnist<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>train<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span></span>
<span id="cb4-4">x_test  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mnist<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>test<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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">255</span></span>
<span id="cb4-5">y_train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mnist<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>train<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y</span>
<span id="cb4-6">y_test  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mnist<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y</span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reshape for dense model (flatten 28x28 images)</span></span>
<span id="cb4-9">x_train <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;">array_reshape</span>(x_train, <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;">nrow</span>(x_train), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">784</span>))</span>
<span id="cb4-10">x_test  <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;">array_reshape</span>(x_test, <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;">nrow</span>(x_test), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">784</span>))</span>
<span id="cb4-11"></span>
<span id="cb4-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># One-hot encode labels</span></span>
<span id="cb4-13">y_train_cat <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;">to_categorical</span>(y_train, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb4-14">y_test_cat  <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;">to_categorical</span>(y_test, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
</details>
</div>
<p><strong>Define model</strong></p>
<p>The model is defined using the Keras API as a sequential architecture composed of fully connected layers. Following its construction, the model is compiled by specifying the loss function, optimization algorithm, and evaluation metrics, thereby preparing it for training.</p>
<p>Dropout is used to help the model generalize better and avoid overfitting. During training, it randomly turns off a portion of the neurons in the network. This forces the model to learn more robust features instead of relying too heavily on any one part of the network.</p>
<p><code>tf$keras$optimizers$legacy$Adam()</code> is used to maintain the original behavior of the Adam optimizer, which can be especially valuable on M1/M2 MacBook architectures where newer versions might encounter compatibility issues. Newer versions changed how optimizers work, so the legacy version ensures compatibility with older code and stable training results.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">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;">keras_model_sequential</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_dense</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">units =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">activation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relu"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">input_shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">784</span>),</span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_dropout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>),</span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_dense</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">units =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">activation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relu"</span>),</span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_dropout</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>),</span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_dense</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">units =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">activation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"softmax"</span>)</span>
<span id="cb5-7">))</span>
<span id="cb5-8"></span>
<span id="cb5-9">model <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;">compile</span>(</span>
<span id="cb5-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">loss =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"categorical_crossentropy"</span>,</span>
<span id="cb5-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">optimizer =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"adam"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#tf$keras$optimizers$legacy$Adam(),</span></span>
<span id="cb5-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">metrics =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"accuracy"</span></span>
<span id="cb5-13">)</span></code></pre></div>
</details>
</div>
<p><strong>Training and time the training</strong></p>
<p>Setting a seed ensures reproducibility of training results, while timing the training process helps assess the efficiency of the model’s learning and optimization.</p>
<p>The validation split refers to the portion of the training data set aside during training to evaluate model performance on unseen data, helping to detect overfitting (and tune hyperparameters, though not considered in this post).</p>
<p>The number of epochs is set to 10. For the purposes of this post, I will not address whether the training process adequately accounts for the issue of overfitting and generalization.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set seed for reproducibility across R, TensorFlow, NumPy</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb6-3">tensorflow<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;">set_random_seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb6-4"></span>
<span id="cb6-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Time the training</span></span>
<span id="cb6-6">start_time <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;">Sys.time</span>()</span>
<span id="cb6-7"></span>
<span id="cb6-8">model <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;">fit</span>(</span>
<span id="cb6-9">  x_train, y_train_cat,</span>
<span id="cb6-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">epochs =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,</span>
<span id="cb6-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">batch_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span>,</span>
<span id="cb6-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">validation_split =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>,</span>
<span id="cb6-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb6-14">)</span>
<span id="cb6-15"></span>
<span id="cb6-16">end_time <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;">Sys.time</span>()</span>
<span id="cb6-17"></span>
<span id="cb6-18">duration <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="cb6-19">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> end_time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>start_time</span>
<span id="cb6-20">duration[<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;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">regmatches</span>(x,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">regexpr</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><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d*</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;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+"</span>,x)))</span></code></pre></div>
</details>
</div>
<p><strong>Export training and testing sets to Drive</strong></p>
<p>The training and testing sets saved to Drive will be used later in a Colab notebook for GPU-based training of the same model defined in Keras above.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert data to data frames with labels</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(x_train) <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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pixel_"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(x_train)))</span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(x_test)  <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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pixel_"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(x_test)))</span>
<span id="cb7-4"></span>
<span id="cb7-5">train_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;">as_tibble</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> y_train, x_train))</span>
<span id="cb7-6">test_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;">as_tibble</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> y_test, x_test))</span>
<span id="cb7-7"></span>
<span id="cb7-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_csv</span>(train_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.path</span>(gdrive_base, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mnist_train.csv"</span>))</span>
<span id="cb7-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_csv</span>(test_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.path</span>(gdrive_base, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mnist_test.csv"</span>))</span></code></pre></div>
</details>
</div>
<p><strong>Predict in R and export</strong></p>
<p>After making predictions in R, we can export the results and visualize the confusion matrix to evaluate the model’s performance. We also compute and export the accuracy, along with the training time, to track the model’s efficiency.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Only needed if you want to compare R vs Colab predictions.</span></span>
<span id="cb8-2"></span>
<span id="cb8-3">preds_r <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;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(x_test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</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;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">k_argmax</span>()</span>
<span id="cb8-4">preds_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;">tibble</span>(</span>
<span id="cb8-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">actual =</span> y_test,</span>
<span id="cb8-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">predicted =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.array</span>(preds_r)</span>
<span id="cb8-7">)</span>
<span id="cb8-8"></span>
<span id="cb8-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to factors</span></span>
<span id="cb8-10">preds_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> preds_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb8-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">actual =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(actual),</span>
<span id="cb8-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">predicted =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(predicted, <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;">levels</span>(actual))</span>
<span id="cb8-14">  )</span>
<span id="cb8-15"></span>
<span id="cb8-16">acc <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="cb8-17">acc[<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;">accuracy</span>(preds_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> actual, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> predicted)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.estimate</span>
<span id="cb8-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">conf_mat</span>(preds_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> actual, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> predicted) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">autoplot</span>(<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;">"heatmap"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R-based DNN confusion matrix for the MNIST dataset"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/parallel_processing_GPU/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details class="code-fold">
<summary>Code</summary>
<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;">write_csv</span>(preds_df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.path</span>(gdrive_base, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"predictions_r.csv"</span>))</span></code></pre></div>
</details>
</div>
<p><strong>(Later) Compare with Colab predictions</strong></p>
<p>The predictions made in R can be compared with those generated in Colab to assess the consistency and performance of the model across different environments.</p>
<ol type="1">
<li>Open the notebook in <a href="colab.research.google.com">Google Colab</a>.</li>
<li>At the top menu bar, click “Runtime”</li>
<li>Select “Change runtime type”</li>
<li>In the “Hardware accelerator” dropdown menu, choose:</li>
</ol>
<ul>
<li>GPU to enable GPU support</li>
<li>TPU if you want to experiment with TPU acceleration (more advanced)</li>
<li>None to use CPU only</li>
</ul>
<ol start="5" type="1">
<li>Click “Save”</li>
<li>Colab will restart the kernel and launch an environment with the selected hardware.</li>
</ol>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># After Colab finishes and saves 'predictions_colab.csv':</span></span>
<span id="cb10-2">preds_colab <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.path</span>(gdrive_base, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"predictions_colab.csv"</span>))</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to factors</span></span>
<span id="cb10-5">preds_colab <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> preds_colab <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb10-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">actual =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(actual),</span>
<span id="cb10-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">predicted =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(predicted, <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;">levels</span>(actual))</span>
<span id="cb10-9">  )</span>
<span id="cb10-10"></span>
<span id="cb10-11">acc[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</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;">accuracy</span>(preds_colab, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> actual, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> predicted)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>.estimate</span>
<span id="cb10-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">conf_mat</span>(preds_colab, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">truth =</span> actual, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">estimate =</span> predicted) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">autoplot</span>(<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;">"heatmap"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Python-based DNN confusion matrix for the MNIST dataset"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/parallel_processing_GPU/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Optionally print Colab training time (if saved)</span></span>
<span id="cb11-2">duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</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;">read_lines</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file.path</span>(gdrive_base, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"colab_duration.txt"</span>))</span></code></pre></div>
</details>
</div>
<p><strong>Comparative analysis</strong></p>
<p>After running the model for 10 epochs—with all other elements of the pipeline held constant—I repeated the classification task using the DNN trained for 50 and 100 epochs. The results, in terms of accuracy and training time, are presented in the table below. Although not strictly necessary — or even correct — I used three decimal places to highlight subtle differences in accuracy observed in the experiments. Moreover, I chose to round the training time to the nearest integer.</p>
<div class="cell">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;">
<caption style="font-size: initial !important;">Comparative analysis</caption>
 <thead>
  <tr>
   <th style="text-align:left;"> Method </th>
   <th style="text-align:right;"> Epoch </th>
   <th style="text-align:right;"> Accuracy, % </th>
   <th style="text-align:right;"> Time, s </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> R-based </td>
   <td style="text-align:right;"> 10 </td>
   <td style="text-align:right;"> 0.972 </td>
   <td style="text-align:right;"> 5 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> R-based </td>
   <td style="text-align:right;"> 50 </td>
   <td style="text-align:right;"> 0.977 </td>
   <td style="text-align:right;"> 26 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> R-based </td>
   <td style="text-align:right;"> 100 </td>
   <td style="text-align:right;"> 0.979 </td>
   <td style="text-align:right;"> 54 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Python-based </td>
   <td style="text-align:right;"> 10 </td>
   <td style="text-align:right;"> 0.974 </td>
   <td style="text-align:right;"> 33 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Python-based </td>
   <td style="text-align:right;"> 50 </td>
   <td style="text-align:right;"> 0.979 </td>
   <td style="text-align:right;"> 72 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> Python-based </td>
   <td style="text-align:right;"> 100 </td>
   <td style="text-align:right;"> 0.979 </td>
   <td style="text-align:right;"> 128 </td>
  </tr>
</tbody>
</table>

</div>
</div>
<p>Even with matching seeds, data splits, architectures, and all the same training settings in R and Python, small differences in accuracy still appeared (compare the confusion matrices above). These are likely due to subtle differences in weight initialization, batch shuffling, and dropout masking between the two frameworks. Such variations are common in deep learning and typically don’t affect overall performance trends.</p>
<p><strong>Why MacBook CPUs (M1/M2) can beat Colab GPUs for MNIST</strong></p>
<ul>
<li><p><em>Model + Dataset are too small for GPU to shine</em>: MNIST has only 60,000 grayscale 28×28 images, and the DNN models used here have a relatively limited number of parameters to learn (109386). With only 100k parameters, each training step is very fast on modern CPUs and the overhead of using a GPU — loading the data, launching kernels, moving data around — starts to outweigh the benefits of parallelism.</p></li>
<li><p><em>Apple Silicon (M1/M2) is extremely optimized</em>: M1/M2 chips have Unified Memory (RAM + GPU share memory), fast memory access, and dedicated matrix multiplication engines (AMX). TensorFlow and PyTorch now have native Metal backends or use Accelerate.framework under the hood on macOS, enabling them to be blazingly fast even without explicit GPU use.</p></li>
<li><p><em>Colab GPUs aren’t top-tier GPUs</em>: Colab’s free tier provides K80 or T4 GPUs, which are relatively old or mid-tier and often shared among users. Their performance can fluctuate depending on server load, whereas a MacBook runs everything locally without such interruptions.</p></li>
<li><p><em>Overhead on the GPU pipeline</em>: the GPU has to load the data, transfer it to GPU memory, perform the computation, and then transfer the results back. This overhead adds latency unless the batch size and model complexity are large enough to justify the transfer.</p></li>
</ul>
</section>
<section id="mnist-classification-training-in-python" class="level2">
<h2 class="anchored" data-anchor-id="mnist-classification-training-in-python">MNIST Classification Training in Python</h2>
<p>In the snippet below, I present an example of a *.ipynb notebook that facilitates the sharing of input and output files with a local R project via a Google Drive synced folder.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mount Google Drive</span></span>
<span id="cb12-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> google.colab <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> drive</span>
<span id="cb12-3">drive.mount(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/content/drive'</span>)</span>
<span id="cb12-4"></span>
<span id="cb12-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Imports</span></span>
<span id="cb12-6"><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="cb12-7"><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="cb12-8"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tensorflow <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> tf</span>
<span id="cb12-9"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tensorflow <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> keras</span>
<span id="cb12-10"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tensorflow.keras.models <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Sequential</span>
<span id="cb12-11"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tensorflow.keras.layers <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Dense, Dropout</span>
<span id="cb12-12"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tensorflow.keras.utils <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> to_categorical</span>
<span id="cb12-13"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> sklearn.metrics <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> accuracy_score, confusion_matrix</span>
<span id="cb12-14"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb12-15"></span>
<span id="cb12-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set seed</span></span>
<span id="cb12-17">np.random.seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb12-18">tf.random.set_seed(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb12-19"></span>
<span id="cb12-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load training and testing data from Drive</span></span>
<span id="cb12-21">gdrive_path <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;">"/content/drive/MyDrive/parallel_processing_colab/data"</span></span>
<span id="cb12-22">train_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_csv(<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>gdrive_path<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;">/mnist_train.csv"</span>)</span>
<span id="cb12-23">test_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_csv(<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>gdrive_path<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;">/mnist_test.csv"</span>)</span>
<span id="cb12-24"></span>
<span id="cb12-25">x_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> train_df.drop(columns <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;">"label"</span>]).values.astype(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"float32"</span>)</span>
<span id="cb12-26">y_train <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> to_categorical(train_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>].values, num_classes <span 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-27"></span>
<span id="cb12-28">x_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> test_df.drop(columns <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;">"label"</span>]).values.astype(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"float32"</span>)</span>
<span id="cb12-29">y_test <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> test_df[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label"</span>].values</span>
<span id="cb12-30"></span>
<span id="cb12-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the same model as in R</span></span>
<span id="cb12-32">model <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Sequential([</span>
<span id="cb12-33">    Dense(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>, activation <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;">'relu'</span>, input_shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">784</span>, )),</span>
<span id="cb12-34">    Dropout(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>),</span>
<span id="cb12-35">    Dense(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>, activation <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;">'relu'</span>),</span>
<span id="cb12-36">    Dropout(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>),</span>
<span id="cb12-37">    Dense(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, activation <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;">'softmax'</span>)</span>
<span id="cb12-38">])</span>
<span id="cb12-39"></span>
<span id="cb12-40">model.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">compile</span>(</span>
<span id="cb12-41">    loss <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;">'categorical_crossentropy'</span>,</span>
<span id="cb12-42">    optimizer <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;">"adam"</span>,</span>
<span id="cb12-43">    metrics <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;">'accuracy'</span>]</span>
<span id="cb12-44">)</span>
<span id="cb12-45"></span>
<span id="cb12-46"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Time the training</span></span>
<span id="cb12-47">start <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb12-48"></span>
<span id="cb12-49">model.fit(</span>
<span id="cb12-50">    x_train, y_train,</span>
<span id="cb12-51">    epochs <span 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-52">    batch_size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>,</span>
<span id="cb12-53">    validation_split <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>
<span id="cb12-54">    verbose <span 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-55">)</span>
<span id="cb12-56"></span>
<span id="cb12-57">end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb12-58">duration <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> end <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> start</span>
<span id="cb12-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;">"Training time (seconds):"</span>, duration)</span>
<span id="cb12-60"></span>
<span id="cb12-61"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save Colab training time to Google Drive</span></span>
<span id="cb12-62"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/content/drive/MyDrive/parallel_processing_colab/data/colab_duration.txt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb12-63">    f.write(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>(duration))</span>
<span id="cb12-64">    </span>
<span id="cb12-65"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Predict on test set and export predictions</span></span>
<span id="cb12-66">preds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> model.predict(x_test)</span>
<span id="cb12-67">pred_labels <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.argmax(preds, 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="cb12-68"></span>
<span id="cb12-69"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save predictions to Drive</span></span>
<span id="cb12-70">preds_df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame({</span>
<span id="cb12-71">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"actual"</span>: y_test,</span>
<span id="cb12-72">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"predicted"</span>: pred_labels</span>
<span id="cb12-73">})</span>
<span id="cb12-74">preds_df.to_csv(<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>gdrive_path<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;">/predictions_colab.csv"</span>, index <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-75"></span>
<span id="cb12-76"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Evaluate metrics (optional here)</span></span>
<span id="cb12-77"><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;">"Accuracy:"</span>, accuracy_score(y_test, pred_labels))</span>
<span id="cb12-78"><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;">"Confusion matrix:</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>, confusion_matrix(y_test, pred_labels))</span></code></pre></div>
</details>
</div>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<p>When working with deep learning tasks, timing considerations between R and Python (especially in Colab) can significantly impact performance.</p>
<ol type="1">
<li><em>Hardware acceleration</em></li>
</ol>
<ul>
<li>R: limited GPU support unless utilizing packages like tensorflow, often relying on the CPU for training, leading to longer processing times for complex models.</li>
<li>Python (Colab): seamless GPU support accelerates training dramatically, reducing training times from hours to minutes.</li>
</ul>
<ol start="2" type="1">
<li><em>Execution times</em></li>
</ol>
<ul>
<li>R: training on a CPU can be slow for deep learning tasks. Execution time can be measured using system.time().</li>
<li>Python (Colab): with GPU enabled, training times are drastically reduced. Timing can be monitored using Python’s time module.</li>
</ul>
<ol start="3" type="1">
<li><em>Colab overheads</em></li>
</ol>
<ul>
<li>Initialization time: setting up Colab (mounting Drive, loading data, GPU setup) adds some overhead, especially when starting a new session.</li>
<li>Session limitations: free GPU access has time limits and potential disconnects, requiring reinitialization.</li>
<li>File access delays: reading and writing to Google Drive in Colab can slow down the workflow compared to local file systems.</li>
<li>Cold start for GPU: initializing GPU resources adds delay when starting a session.</li>
</ul>
<ol start="4" type="1">
<li><p><em>Hybrid workflow</em>: a hybrid R-Python workflow maximizes the strengths of both ecosystems. R handles data preprocessing, feature engineering, evaluation, and visualization, while Python in Colab handles heavy-duty model training with GPU support. This combination offers a flexible and efficient solution, especially for large models and datasets.</p></li>
<li><p><em>Total execution time</em>: while Colab’s GPU accelerates training, overheads like initialization and session limitations can add to the overall time. For simpler models or smaller datasets, running everything locally may be more efficient, but for larger models, the speedup from Colab’s GPU outweighs the overheads.</p></li>
</ol>
<p>What I’ve shown here is how to leverage Colab’s GPU support for deep learning while staying within the comfort of R, taking full advantage of the tidyverse ecosystem for analysis and visualization. In a future post, I’ll circle back to this topic—probably with a hands-on example using LSTM for time series forecasting, where Colab’s GPU support will really get a chance to shine compared to single-CPU runs.</p>
<blockquote class="blockquote">
<p>In summary, the hybrid R + Python approach offers a balance of computational power (via Colab’s GPUs) and flexibility (using R for preprocessing and evaluation), though the Colab overheads should be considered when designing the workflow.</p>
</blockquote>
<p><a href="https://www.flaticon.com/free-icons/parallel" title="parallel icons">Parallel icons created by juicy_fish - Flaticon</a></p>


</section>

 ]]></description>
  <category>machine learning</category>
  <guid>https://amsabatini.netlify.app/posts/parallel_processing_GPU/</guid>
  <pubDate>Tue, 22 Apr 2025 22:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/parallel_processing_GPU/processing.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Unleashing the power of Apple Silicon for R: Parallel processing on M1/M2</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/parallel_processing/</link>
  <description><![CDATA[ 




<p>Parallel computation is a big deal in machine learning (ML), especially when working with large datasets, complex models (like deep neural networks), or intensive tasks like hyperparameter tuning. Parallel computation refers to executing multiple calculations or processes simultaneously.</p>
<p>In ML, this helps speed up training and inference by utilizing multiple processing units (CPU cores, GPUs, TPUs, clusters). For my (small) projects in ML, I run everything on a single-CPU machine, an M2 MacBook Air, and write my code in R. Calculations can be excruciatingly slow; however, many tasks are also considered <em>embarrassingly parallel</em>. For example, models created during resampling are independent of each other and can be fit simultaneously without issue. Right now I’m running everything sequentially, but I want to switch to parallel processing to speed things up. In this post, I will review popular methods that are available in R <em>to go parallel</em> with MacBooks equipped with M1 and M2 chips.</p>
<div class="callout callout-style-default callout-tip 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">
Apple M1 and M2 Macs: A quick overview
</div>
</div>
<div class="callout-body-container callout-body">
<p>Apple’s M1 and M2 chips are custom-designed ARM-based processors that mark Apple’s move away from Intel. These chips power Macs and iPads, offering significant boosts in performance and efficiency.</p>
<p>M1 Chip (2020):</p>
<ul>
<li>First Apple Silicon chip.</li>
<li>5nm process, 8-core CPU (4 performance + 4 efficiency), up to 8-core GPU.</li>
<li>Unified memory architecture (up to 16 GB).</li>
<li>High performance per watt; fanless in MacBook Air.</li>
<li>Found in MacBook Air, MacBook Pro 13”, Mac Mini, and iMac 24”.</li>
</ul>
<p>M2 Chip (2022):</p>
<ul>
<li>Second-generation chip.</li>
<li>Improved 5nm design, up to 18% faster CPU and 35% faster GPU than M1.</li>
<li>Supports up to 24 GB of unified memory.</li>
<li>Enhanced media engine and ProRes acceleration.</li>
<li>Used in updated MacBook Air, MacBook Pro 13”, and newer Macs.</li>
</ul>
</div>
</div>
<p>For computations conducted on a single computer, the number of possible worker processes is determined by the parallel package:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The number of physical cores in the hardware:</span></span>
<span id="cb1-2">parallel<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;">detectCores</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">logical =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)<span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The number of possible independent processes that can </span></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># be simultaneously used:  </span></span>
<span id="cb1-6">parallel<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;">detectCores</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">logical =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</div>
<p>My MacBook Air has:</p>
<ul>
<li>4 Performance Cores (P-cores)</li>
<li>4 Efficiency Cores (E-cores)</li>
</ul>
<p>When training models, especially with resampling (like cross-validation, bootstrapping), I want consistency and high throughput, which is best achieved on the performance cores. So:</p>
<ul>
<li>Choosing 4 cores ensures only the P-cores are used</li>
<li>Avoids overloading the system, leaving E-cores and other system resources free for background tasks or RStudio</li>
</ul>
<p>Using all 8 logical cores (4P + 4E) can lead to inconsistent performance, because efficiency cores are slower and not ideal for heavy computational tasks.</p>
<section id="overview" class="level2">
<h2 class="anchored" data-anchor-id="overview">Overview</h2>
<p>R supports three popular parallel processing methods that work well on M1/M2 MacBooks. Programming hints on how to set up/activate and deactivate/reset each of the three methods are provided in the table below.</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><strong>Method</strong></th>
<th><strong>Setup / Activate</strong></th>
<th><strong>Deactivate / Reset</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>doParallel</strong></td>
<td><code>library(doParallel)</code> <br> <code>cl &lt;- makeCluster(4)</code> <br> <code>registerDoParallel(cl)</code></td>
<td><code>stopCluster(cl)</code></td>
</tr>
<tr class="even">
<td><strong>doMC</strong></td>
<td><code>library(doMC)</code> <br> <code>registerDoMC(cores = 4)</code></td>
<td><code>registerDoSEQ()</code></td>
</tr>
<tr class="odd">
<td><strong>furrr + future</strong></td>
<td><code>library(furrr)</code> <br> <code>library(future)</code> <br> <code>plan(multisession, workers = 4)</code></td>
<td><code>plan(sequential)</code></td>
</tr>
</tbody>
</table>
<p>The table below summarizes the key pros and cons of each method.</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><strong>Method</strong></th>
<th><strong>Pros</strong></th>
<th><strong>Cons</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>doParallel</strong></td>
<td>✅ Cross-platform (Mac/Linux/Windows) <br> ✅ Works with <code>foreach</code>, <code>caret</code>, <code>tidymodels</code></td>
<td>❌ Verbose setup <br> ❌ Full R sessions increase memory</td>
</tr>
<tr class="even">
<td><strong>doMC</strong></td>
<td>✅ Simple &amp; fast on macOS <br> ✅ Lightweight (uses forked processes)</td>
<td>❌ Not Windows-compatible <br> ❌ Not safe for GUI/Shiny apps</td>
</tr>
<tr class="odd">
<td><strong>furrr + future</strong></td>
<td>✅ Clean tidyverse integration <br> ✅ Works with <code>future_map()</code> <br> ✅ Scales to cloud</td>
<td>❌ More overhead <br> ❌ Full R sessions</td>
</tr>
</tbody>
</table>
<div id="exm-exm1" class="theorem example">
<p><span class="theorem-title"><strong>Example 1 (The Ames housing dataset)</strong></span> The Ames housing dataset is a well-known real estate dataset used for regression modeling, especially as an improved alternative to the Boston Housing dataset. It contains detailed information about residential homes in Ames, Iowa, sold between 2006 and 2010 (<a href="https://www.tmwr.org">here</a>).</p>
<p>Here’s a clean and minimal setup to load the essential libraries for working with the Ames Housing dataset using a tidymodels regression workflow.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data handling and wrangling</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For the Ames Housing dataset</span></span>
<span id="cb2-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(modeldata)</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Modeling and resampling</span></span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidymodels)</span>
<span id="cb2-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidymodels_prefer</span>()</span></code></pre></div>
</details>
</div>
<p>The dataset can be loaded easily in R using the modeldata package.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">data</span>(ames, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">package =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"modeldata"</span>)</span>
<span id="cb3-2">ames <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ames <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Sale_Price =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log10</span>(Sale_Price))</span></code></pre></div>
</details>
</div>
<p>Suppose a regression model is to be fit to the pre-logged sale prices (Sale_Price). In this post, we will focus on a small subset of the predictors available in the Ames housing dataset:</p>
<ul>
<li>The neighborhood-(qualitative): physical locations within Ames city limits</li>
<li>The gross above-grade-living area-(continuous): the standard measure for determining the amount of space in residential properties)</li>
<li>The year built (Year_Built)-(discrete): original construction date</li>
<li>The type of building (Bldg_Type)-(nominal): type of dwelling</li>
</ul>
<p>I set up my regression modeling using tidymodels, including:</p>
<ul>
<li>Data splitting</li>
<li>Recipe for preprocessing</li>
<li>Model specification</li>
<li>Workflow combining recipe + model</li>
</ul>
<p>My workflow fits together with v-fold cross-validation in the tidymodels framework. Using the <code>strata = Sale_Price</code> argument in <code>initial_split()</code> or <code>vfold_cv()</code>, I use stratified sampling, which helps ensure that the distribution of the response variable (<code>Sale_Price</code>) remains balanced across splits.</p>
<p>The recipe defines the preprocessing steps applied to the dataset before modeling, via <code>step_*()</code> functions without immediately executing them; it is only a specification of what should be done.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">502</span>)</span>
<span id="cb4-2">ames_split <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;">initial_split</span>(ames, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.80</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strata =</span> Sale_Price) </span>
<span id="cb4-3">ames_train <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;">training</span>(ames_split)</span>
<span id="cb4-4">ames_test  <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;">testing</span>(ames_split)</span>
<span id="cb4-5"></span>
<span id="cb4-6"><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;">1004</span>)</span>
<span id="cb4-7">ames_folds <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;">vfold_cv</span>(ames_train, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">v =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strata =</span> Sale_Price) </span>
<span id="cb4-8"></span>
<span id="cb4-9">ames_rec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">recipe</span>(Sale_Price <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Neighborhood <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Gr_Liv_Area <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Year_Built <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Bldg_Type,</span>
<span id="cb4-11">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> ames_train) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_log</span>(Gr_Liv_Area, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">step_dummy</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_nominal_predictors</span>())</span></code></pre></div>
</details>
</div>
<p>I fit a random forest model to the training set using the ranger engine, which uses the ranger package for fast computation. Random forests are very powerful — they can learn complex patterns in the data with high accuracy. One big advantage is that they don’t need much preprocessing, so they’re easy to use. The tradeoff is that they can be slower to train, especially on large datasets.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">rf_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;">rand_forest</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trees =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_engine</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ranger"</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;">set_mode</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"regression"</span>)</span>
<span id="cb5-4"></span>
<span id="cb5-5">rf_wflow <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;">workflow</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_recipe</span>(ames_rec) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb5-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_model</span>(rf_model)</span></code></pre></div>
</details>
</div>
<p>The <code>control_resamples()</code> function is used to customize how results are saved during cross-validation. <code>save_pred = TRUE</code> saves the predictions from each fold, so we can analyze or plot them later; <code>save_workflow = TRUE</code> saves the workflow used in each fold, which is helpful if we want to inspect the model or preprocessing steps afterward.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">keep_pred <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;">control_resamples</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">save_pred =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">save_workflow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details>
</div>
<p><strong>Sequential approach</strong></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1003</span>) </span>
<span id="cb7-2"></span>
<span id="cb7-3">start_time <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;">Sys.time</span>() </span>
<span id="cb7-4"></span>
<span id="cb7-5">rf_res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rf_wflow <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_resamples</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resamples =</span> ames_folds, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control =</span> keep_pred) </span>
<span id="cb7-7"></span>
<span id="cb7-8">end_time <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;">Sys.time</span>() </span>
<span id="cb7-9"></span>
<span id="cb7-10">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;">collect_metrics</span>(rf_res) </span>
<span id="cb7-11">performance <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="cb7-12">performance[[<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> metrics <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(.metric <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;">"rmse"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rsq"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-14">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(mean)</span>
<span id="cb7-15"></span>
<span id="cb7-16">duration <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="cb7-17">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> end_time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>start_time</span>
<span id="cb7-18">duration[<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;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">regmatches</span>(x,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">regexpr</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><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d*</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;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+"</span>,x)))</span></code></pre></div>
</details>
</div>
<p><strong>Approach using doParallel</strong></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">library</span>(doParallel) </span>
<span id="cb8-2"></span>
<span id="cb8-3"><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;">1003</span>) </span>
<span id="cb8-4"></span>
<span id="cb8-5">cl <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;">makeCluster</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) </span>
<span id="cb8-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">registerDoParallel</span>(cl) </span>
<span id="cb8-7"></span>
<span id="cb8-8">start_time <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;">Sys.time</span>() </span>
<span id="cb8-9">rf_res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rf_wflow <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_resamples</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resamples =</span> ames_folds, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control =</span> keep_pred) </span>
<span id="cb8-11">end_time <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;">Sys.time</span>()</span>
<span id="cb8-12"></span>
<span id="cb8-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopCluster</span>(cl)</span>
<span id="cb8-14"></span>
<span id="cb8-15">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;">collect_metrics</span>(rf_res)  </span>
<span id="cb8-16">performance[[<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> metrics <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(.metric <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;">"rmse"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rsq"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-18">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(mean)</span>
<span id="cb8-19"></span>
<span id="cb8-20">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> end_time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> start_time</span>
<span id="cb8-21">duration[<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;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">regmatches</span>(x,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">regexpr</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><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d*</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;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+"</span>,x)))</span></code></pre></div>
</details>
</div>
<p><strong>Approach using doMC</strong></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1003</span>) </span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(doMC) </span>
<span id="cb9-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">registerDoMC</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) </span>
<span id="cb9-5"></span>
<span id="cb9-6">start_time <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;">Sys.time</span>() </span>
<span id="cb9-7"></span>
<span id="cb9-8">rf_res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rf_wflow <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_resamples</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resamples =</span> ames_folds, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control =</span> keep_pred) </span>
<span id="cb9-10"></span>
<span id="cb9-11">end_time <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;">Sys.time</span>()</span>
<span id="cb9-12"></span>
<span id="cb9-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">registerDoSEQ</span>()</span>
<span id="cb9-14"></span>
<span id="cb9-15">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;">collect_metrics</span>(rf_res)  </span>
<span id="cb9-16">performance[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> metrics <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(.metric <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;">"rmse"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rsq"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-18">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(mean)</span>
<span id="cb9-19"></span>
<span id="cb9-20">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> end_time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> start_time</span>
<span id="cb9-21">duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</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;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">regmatches</span>(x,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">regexpr</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><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d*</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;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+"</span>,x)))</span></code></pre></div>
</details>
</div>
<p><strong>Approach using furrr + future</strong></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(furrr) </span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(future) </span>
<span id="cb10-3"></span>
<span id="cb10-4"><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;">1003</span>) </span>
<span id="cb10-5"></span>
<span id="cb10-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plan</span>(multisession, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">workers =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) </span>
<span id="cb10-7"></span>
<span id="cb10-8">start_time <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;">Sys.time</span>() </span>
<span id="cb10-9"></span>
<span id="cb10-10">rf_res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rf_wflow <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb10-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit_resamples</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resamples =</span> ames_folds, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control =</span> keep_pred) </span>
<span id="cb10-12"></span>
<span id="cb10-13">end_time <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;">Sys.time</span>()</span>
<span id="cb10-14"></span>
<span id="cb10-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plan</span>(sequential)</span>
<span id="cb10-16"></span>
<span id="cb10-17">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;">collect_metrics</span>(rf_res)  </span>
<span id="cb10-18">performance[[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> metrics <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(.metric <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;">"rmse"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rsq"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-20">               <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(mean)</span>
<span id="cb10-21"></span>
<span id="cb10-22">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> end_time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> start_time</span>
<span id="cb10-23">duration[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</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;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">regmatches</span>(x,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">regexpr</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><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d*</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;">\\</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">d+"</span>,x)))</span></code></pre></div>
</details>
</div>
<p><strong>Comparative analysis</strong></p>
<p>Training time (in seconds) for each method is shown below.</p>
<div class="cell">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;">
<caption style="font-size: initial !important;">Timing results</caption>
 <thead>
  <tr>
   <th style="text-align:left;"> Method </th>
   <th style="text-align:right;"> Time, s </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> doMC </td>
   <td style="text-align:right;"> 6.06 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> doParallel </td>
   <td style="text-align:right;"> 7.94 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> purrr + future </td>
   <td style="text-align:right;"> 10.26 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> sequential </td>
   <td style="text-align:right;"> 13.34 </td>
  </tr>
</tbody>
</table>

</div>
</div>
</div>
</section>
<section id="recommended-core-use-on-m1m2-macs" class="level2">
<h2 class="anchored" data-anchor-id="recommended-core-use-on-m1m2-macs">Recommended core use on M1/M2 Macs</h2>
<blockquote class="blockquote">
<p>Use <strong>4 cores</strong> to target the <strong>performance cores</strong> only. This choice offers a good balance between performance and system responsiveness.</p>
</blockquote>
</section>
<section id="summary" class="level2">
<h2 class="anchored" data-anchor-id="summary">Summary</h2>
<ul>
<li>Use <code>doMC</code> for lightweight, fast, Mac-only workflows.</li>
<li>Use <code>doParallel</code> if you need Windows compatibility or more control.</li>
<li>Use <code>furrr + future</code> for a modern, tidyverse-friendly setup and future scalability.</li>
</ul>
<p><a href="https://www.flaticon.com/free-icons/parallel" title="parallel icons">Parallel icons created by juicy_fish - Flaticon</a></p>


</section>

 ]]></description>
  <category>machine learning</category>
  <guid>https://amsabatini.netlify.app/posts/parallel_processing/</guid>
  <pubDate>Mon, 21 Apr 2025 22:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/parallel_processing/processing.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Using Long Short Term Memory (LSTM) in R for time series forecasting</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/lstm/</link>
  <description><![CDATA[ 




<p>Long Short Term Memory (LSTM) networks are special kind of Recurrent Neural Networks (RNNs). LSTM networks can be used for time series forecasting, because of their ability to capture and learn the long-term dependencies, such as trends or patterns persisting over time, that are frequently exhibited by time series. In particular, LSTM networks can retain such information on long term behavior of time series much better than ordinary RNNs can do.</p>
<p>Figure&nbsp;1 shows the cell of an ordinary RNN, <img src="https://latex.codecogs.com/png.latex?A">, which outputs a value <img src="https://latex.codecogs.com/png.latex?h_t"> in response to the input <img src="https://latex.codecogs.com/png.latex?x_t">.</p>
<div id="fig-fig1" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/lstm/RNN_cell_1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="134">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Cell of an ordinary RNN.
</figcaption>
</figure>
</div>
<p>Since the cell has a loop into it, information is allowed to persist through multiple steps of the network. An RNN can be thought as multiple copies of this cell, each copy passing a message to the next one, as shown in Figure&nbsp;2. It is exactly this ability to retain information over multiple steps of the network that provides an RNN with the ability, in principle, to learn long-term dependencies of the input sequence.</p>
<div id="fig-fig2" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/lstm/RNN_cell_2.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="576">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: An unrolled RNN.
</figcaption>
</figure>
</div>
<p>LSTM are, in this regard, just a special kind of RNN, with much better capabilities to learn the long-term dependencies, compared with an ordinary RNN. Figure&nbsp;3 shows that each repeating cell in an LSTM has four neural network layers (sigmoid <img src="https://latex.codecogs.com/png.latex?%5Csigma"> and hyperbolic tangent <img src="https://latex.codecogs.com/png.latex?%5Ctext%7Btanh%7D">). In this figure each line carries a vector, from the output of one node to the input of others. Pointwise operations of multiplication and addition coexist with the learned neural network layers.</p>
<div id="fig-fig3" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/lstm/LSTM_cell.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="720">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Cell of an LSTM network.
</figcaption>
</figure>
</div>
<p>The cell state is the horizontal line at the top of the diagram. The flow of information along the cell state is controlled by the neural network layers, which learn which information to pass and which information to forget through a number of gates:</p>
<ul>
<li>The <strong>forget gate</strong> decides what information to discard from the cell state:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0Af_t=%5Csigma%5Cleft%5Bw_f%5Ccdot(h_%7Bt-1%7D,x_t)+b_f%5Cright%5D%0A"></p>
<ul>
<li>The <strong>input gate</strong> decides what new information to store in the cell state:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Balign%7D%0Ai_t&amp;=%5Csigma%5Cleft%5Bw_i%5Ccdot(h_%7Bt-1%7D,x_t)+b_i%5Cright%5D%5C%5C%0A%5Ctilde%7Bc%7D_t&amp;=%5Ctanh%5Cleft%5Bw_c%5Ccdot(h_%7Bt-1%7D,x_t)+b_c%5Cright%5D%0A%5Cend%7Balign%7D%0A%5Cright.%0A"></p>
<ul>
<li>The <strong>cell state update</strong> is based on the following equations:</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0Ac_t=f_t%5Ccdot%20c_%7Bt-1%7D+i_t%5Ccdot%20%5Ctilde%7Bc%7D_t%0A"></p>
<ul>
<li>The <strong>output gate</strong> computes the output <img src="https://latex.codecogs.com/png.latex?h_t"> based on the updated cell state <img src="https://latex.codecogs.com/png.latex?c_t">.</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Balign%7D%0Ao_t&amp;=%5Csigma%5Cleft%5Bw_o%5Ccdot(h_%7Bt-1%7D,x_t)+b_o%5Cright%5D%5C%5C%0Ah_t&amp;=o_t%5Ccdot%20%5Ctanh(%7Bc_t%7D)%0A%5Cend%7Balign%7D%0A%5Cright.%0A"></p>
<p>It is worth noting that the structure of the LSTM cell indicated in Figure&nbsp;3 is just one of many variants that have been proposed in the literature.</p>
<section id="load-the-libraries-and-the-dataset" class="level2">
<h2 class="anchored" data-anchor-id="load-the-libraries-and-the-dataset">Load the libraries and the dataset</h2>
<p>For demonstration purposes, the historical end-of-day stock price data <img src="https://latex.codecogs.com/png.latex?p_t"> for Apple Inc.&nbsp;are considered (period: 01/01/2022-06/30/2024). The <code>quantmod</code> package in R can be used to download this data from Yahoo Finance.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(quantmod)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(keras)</span>
<span id="cb1-4"></span>
<span id="cb1-5">tensorflow<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;">set_random_seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1234</span>)</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the stock symbol for Apple Inc. and specifiy the date range</span></span>
<span id="cb1-8">stock_symbol <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AAPL"</span></span>
<span id="cb1-9">start_date   <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.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2021-12-31"</span>)</span>
<span id="cb1-10">end_date     <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.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2024-06-30"</span>)</span>
<span id="cb1-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Download data from Yahoo Finance using quantmod</span></span>
<span id="cb1-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">getSymbols</span>(stock_symbol, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">src =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"yahoo"</span>, </span>
<span id="cb1-13">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> start_date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> end_date) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> a</span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set data as dataframe</span></span>
<span id="cb1-15">apple_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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">index</span>(AAPL)), </span>
<span id="cb1-16">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">close =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Cl</span>(AAPL)))</span>
<span id="cb1-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compute log-returns from end-of-day stock price, adjusting for date</span></span>
<span id="cb1-18">log_return <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;">diff</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(apple_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>close))</span>
<span id="cb1-19">date <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> apple_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb1-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(date)</span>
<span id="cb1-21">date <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> date[<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-22">apple_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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log_return =</span> log_return)</span></code></pre></div>
</details>
</div>
</section>
<section id="data-preparation" class="level2">
<h2 class="anchored" data-anchor-id="data-preparation">Data preparation</h2>
<section id="transform-data-to-stationary" class="level3">
<h3 class="anchored" data-anchor-id="transform-data-to-stationary">Transform data to stationary</h3>
<p>Usually, this is done by getting the difference between two consecutive values in the series. This transformation, commonly known as <em>differencing</em>, is considered in traditional time series models, such as ARIMA and GARCH. For a short discussion about non-stationarity in time series and statistical tests that can be used to detect whether a time series is stationary or not, see my previous post (<a href="https://amsabatini.netlify.app/posts/dickey_fuller/">here</a>). There are different approaches in the literature regarding whether LSTM networks can be applied to stationary or non-stationary time series (<a href="https://medium.com/@robinaiqbal/using-r-to-compare-the-applicability-of-long-short-term-memory-lstm-models-for-stock-prices-and-7a9b72250a95">here</a>). For the example discussed in this post, I decided to analyze the <strong>log-returns</strong> <img src="https://latex.codecogs.com/png.latex?z_t"> of the end-of-day stock prices:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Az_t=%5Clog%5Cleft(%5Cdfrac%7Bp_t%7D%7Bp_%7Bt-1%7D%7D%5Cright)=%5Clog(p_t)-%5Clog(p_%7Bt-1%7D)%0A"></p>
<div class="cell">
<div class="cell-output-display">
<div id="fig-fig4" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig4-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/lstm/index_files/figure-html/fig-fig4-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig4-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Log-returns of end-of-day stock price.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The time series of log-returns plotted in Figure&nbsp;4 is to be regarded as being stationary.</p>
</section>
<section id="data-normalization" class="level3">
<h3 class="anchored" data-anchor-id="data-normalization">Data normalization</h3>
<p>For better learning, LSTM needs normalized data, i.e., data that are centered by the mean and scaled by the standard deviation. This step can be implemented using the <code>scale()</code> function of base R; the function <code>get_scaling_factors()</code> stores the scaling factors used for data normalization. When the original data scale needs to be restored at the time of making predictions, the function <code>reverse_data()</code> can be used.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">get_scaling_factors <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) {</span>
<span id="cb2-2">  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;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(data), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(data))</span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(out)</span>
<span id="cb2-4">}</span>
<span id="cb2-5"></span>
<span id="cb2-6">reverse_data <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, scaling_factors) {</span>
<span id="cb2-7">  temp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>scaling_factors[<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> scaling_factors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb2-8">  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;">as.matrix</span>(temp)</span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(out)</span>
<span id="cb2-10">}</span>
<span id="cb2-11"></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data processing using differencing with diff() and normalization with scale() </span></span>
<span id="cb2-13">scaling_factors <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;">get_scaling_factors</span>(apple_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>log_return)</span>
<span id="cb2-14">log_return      <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;">scale</span>(apple_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>log_return)</span>
<span id="cb2-15">log_return      <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>(log_return)</span></code></pre></div>
</details>
</div>
</section>
<section id="split-dataset-in-training-and-testing-sets" class="level3">
<h3 class="anchored" data-anchor-id="split-dataset-in-training-and-testing-sets">Split dataset in training and testing sets</h3>
<p>LSTM works in a supervised learning mode, with a predictor <img src="https://latex.codecogs.com/png.latex?%5Ctextbf%7BX%7D"> and a target variable <img src="https://latex.codecogs.com/png.latex?Y">. The time series needs therefore to be transformed by lagging the series, so as to have the values at times <img src="https://latex.codecogs.com/png.latex?(t%E2%88%92k),k=1,%5Ccdots,L"> as the input and the value at time <img src="https://latex.codecogs.com/png.latex?t"> as the ouput, for a <img src="https://latex.codecogs.com/png.latex?L">-step lagged dataset: <img src="https://latex.codecogs.com/png.latex?%5Ctextbf%7BX%7D_t=%5Bx_%7Bt-L%7D,x_%7Bt-L+1%7D,%5Ccdots,x_%7Bt-1%7D%5D"> and <img src="https://latex.codecogs.com/png.latex?Y_t=x_t">. Here, I choose <img src="https://latex.codecogs.com/png.latex?L=7">. Moreover, unlike in most analysis where training and testing data sets are randomly sampled, with time series data the order of the observations does matter. The following code splits the first 80% of the series as training set and the remaining 20% as testing set.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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 the train and test split</span></span>
<span id="cb3-2">split_data <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>(series, lag) {</span>
<span id="cb3-3">    lag <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lag<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-4">    data_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;">as.matrix</span>(series) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># convert to matrix (series is a data frame)</span></span>
<span id="cb3-5">    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;">array</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dim =</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>, lag, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(data_raw)))</span>
<span id="cb3-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create all possible sequences of length lag</span></span>
<span id="cb3-7">    <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(data_raw)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>lag)) {</span>
<span id="cb3-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;">rbind</span>(data, data_raw[i<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>lag<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>), ])</span>
<span id="cb3-9">    }</span>
<span id="cb3-10">    train_set_size <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;">round</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 class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(data))</span>
<span id="cb3-11">    test_set_size  <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;">nrow</span>(data)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>train_set_size</span>
<span id="cb3-12">    x_train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data[<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>train_set_size, <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>(lag<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>)]</span>
<span id="cb3-13">    y_train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data[<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>train_set_size, lag]</span>
<span id="cb3-14">    x_test <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data[(train_set_size<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;">nrow</span>(data), <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>(lag<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>)]</span>
<span id="cb3-15">    y_test <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data[(train_set_size<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;">nrow</span>(data), lag]</span>
<span id="cb3-16">    <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;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_train =</span> x_train, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_train =</span> y_train,</span>
<span id="cb3-17">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_test =</span> x_test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_test =</span> y_test))</span>
<span id="cb3-18">}</span>
<span id="cb3-19"></span>
<span id="cb3-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Divide data into train and test</span></span>
<span id="cb3-21">lag <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;">7</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Choose sequence length</span></span>
<span id="cb3-22">split_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;">split_data</span>(log_return, lag)</span>
<span id="cb3-23">x_train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> split_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x_train</span>
<span id="cb3-24">y_train <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> split_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y_train</span>
<span id="cb3-25">x_test <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> split_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x_test</span>
<span id="cb3-26">y_test <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> split_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y_test</span>
<span id="cb3-27">n_train <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;">dim</span>(x_train)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span>
<span id="cb3-28">n_test <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;">dim</span>(x_test)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span></code></pre></div>
</details>
</div>
</section>
</section>
<section id="model-construction" class="level2">
<h2 class="anchored" data-anchor-id="model-construction">Model construction</h2>
<p>It is important to consider that the input to every LSTM layer must be three-dimensional. The three dimensions of the input are:</p>
<ul>
<li><p><strong>Samples</strong> This dimension indicates how many samples are included in each batch; in this example, we have 494 samples for the training data and 123 for the testing data.</p></li>
<li><p><strong>Time Steps</strong> This dimension indicates how many points of observation are included in each sample; in this example, we have 7 points.</p></li>
<li><p><strong>Features</strong> This dimension indicates how many features are in each observation at each time step; for a univariate case, like in this example, we have just one feature.</p></li>
</ul>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">input_dim <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="cb4-2"></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reshape the training and test data to have a 3D array</span></span>
<span id="cb4-4">x_train <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;">array_reshape</span>(x_train, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(n_train, lag, input_dim))</span>
<span id="cb4-5">x_test  <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;">array_reshape</span>(x_test, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(n_test, lag, input_dim))</span></code></pre></div>
</details>
</div>
<section id="define-the-model" class="level3">
<h3 class="anchored" data-anchor-id="define-the-model">Define the model</h3>
<p>A sequential model is defined using <code>keras_model_sequential()</code>:</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Decide structure</span></span>
<span id="cb5-2">hidden_dim <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;">32</span></span>
<span id="cb5-3">num_layers <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="cb5-4">output_dim <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="cb5-5"></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the LSTM model using Keras</span></span>
<span id="cb5-7">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;">keras_model_sequential</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_lstm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">units =</span> hidden_dim, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">input_shape =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(lag, input_dim), </span>
<span id="cb5-9">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">return_sequences =</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;">%&gt;%</span></span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer_lstm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">units =</span> hidden_dim) <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;">layer_dense</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">units =</span> output_dim) </span>
<span id="cb5-12">model</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Model: "sequential"
________________________________________________________________________________
 Layer (type)                       Output Shape                    Param #     
================================================================================
 lstm_1 (LSTM)                      (None, 7, 32)                   4352        
 lstm (LSTM)                        (None, 32)                      8320        
 dense (Dense)                      (None, 1)                       33          
================================================================================
Total params: 12705 (49.63 KB)
Trainable params: 12705 (49.63 KB)
Non-trainable params: 0 (0.00 Byte)
________________________________________________________________________________</code></pre>
</div>
</div>
<p>The LSTM input layer is defined by the <code>input_shape</code> argument on the first hidden layer, which takes a tuple of two values that define the number of time steps and features.</p>
</section>
<section id="compile-and-fit-the-model" class="level3">
<h3 class="anchored" data-anchor-id="compile-and-fit-the-model">Compile and fit the model</h3>
<p>The model is compiled in-place using <code>compile()</code>, which specifies the loss function (mean squared error) and the optimizer (Adam), with learning rate assigned over each update. The model is then fit using the training data and validated on the testing data. The training history is stored in the <code>history</code> object.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">num_epochs <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;">125</span></span>
<span id="cb7-2">batch      <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;">16</span></span>
<span id="cb7-3"></span>
<span id="cb7-4">model <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compile</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">loss =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean_squared_error"</span>, </span>
<span id="cb7-6">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">optimizer =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">optimizer_adam</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">learning_rate =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>))</span>
<span id="cb7-7"></span>
<span id="cb7-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Train the model on the training data</span></span>
<span id="cb7-9">history <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;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fit</span>(x_train, y_train, </span>
<span id="cb7-10">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">epochs =</span> num_epochs, </span>
<span id="cb7-11">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">batch_size =</span> batch, </span>
<span id="cb7-12">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">validation_data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(x_test, y_test),</span>
<span id="cb7-13">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb7-14">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shuffle =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output-display">
<div id="fig-fig5" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig5-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/lstm/index_files/figure-html/fig-fig5-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig5-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: Loss over the number of epochs.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The loss over the training epochs is shown in Figure&nbsp;5. The validation mean squared error is 1.2078.</p>
</section>
<section id="extract-predictions" class="level3">
<h3 class="anchored" data-anchor-id="extract-predictions">Extract predictions</h3>
<p>Figure&nbsp;6 shows the results of the forecasting exercise using LSTM.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract predictions from the estimated model</span></span>
<span id="cb8-2">y_train_pred <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;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(x_train, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb8-3">y_test_pred  <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;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(x_test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb8-4"></span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rescale the predictions and original values</span></span>
<span id="cb8-6">y_train_pred_orig <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;">reverse_data</span>(y_train_pred, scaling_factors)</span>
<span id="cb8-7">y_train_orig      <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;">reverse_data</span>(y_train, scaling_factors)</span>
<span id="cb8-8">y_test_pred_orig  <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;">reverse_data</span>(y_test_pred, scaling_factors)</span>
<span id="cb8-9">y_test_orig       <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;">reverse_data</span>(y_test, scaling_factors)</span>
<span id="cb8-10"></span>
<span id="cb8-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shift the predicted values to start from where the training data predictions end</span></span>
<span id="cb8-12">y_test_pred_orig_shifted <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, n_train), y_test_pred_orig[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])</span>
<span id="cb8-13">y_train_pred_orig        <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>(y_train_pred_orig, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, n_test))</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output-display">
<div id="fig-fig6" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig6-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/lstm/index_files/figure-html/fig-fig6-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig6-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;6: LSTM predictions.
</figcaption>
</figure>
</div>
</div>
</div>
<p><a href="https://www.flaticon.com/free-icons/forecasting" title="forecasting icons">Forecasting icons created by zero_wing - Flaticon</a></p>


</section>
</section>

 ]]></description>
  <category>time series</category>
  <guid>https://amsabatini.netlify.app/posts/lstm/</guid>
  <pubDate>Mon, 10 Mar 2025 23:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/lstm/forecast-analytics.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Frequency spectrum of a sine-wave tone burst</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/tone_burst/</link>
  <description><![CDATA[ 




<section id="sine-wave-tone-burst" class="level2">
<h2 class="anchored" data-anchor-id="sine-wave-tone-burst">Sine-wave tone burst</h2>
<p>A sine-wave tone burst signal is shown in Figure&nbsp;1.</p>
<div id="fig-fig1" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/tone_burst/fig_tone_burst.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="350">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Sine-wave tone burst of two cycles on, four cycles off.
</figcaption>
</figure>
</div>
<p>It is formed by “turning on and off” (gating) a sinusoidal signal (<a href="https://www.ietlabs.com/pdf/GR_Appnote/IN-105%20Frequency%20Spectrum%20of%20a%20Tone%20Burst.pdf">here</a>). The sinusoidal signal, with fundamental period <img src="https://latex.codecogs.com/png.latex?T_b"> (frequency: <img src="https://latex.codecogs.com/png.latex?f_b%20=%201/T_b">) and initial phase <img src="https://latex.codecogs.com/png.latex?%5Ctheta_b">, is assumed here to have unit amplitude:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax_b(t)=%5Csin%5Cleft(2%5Cpi%5Cdfrac%7Bt%7D%7BT_b%7D+%5Ctheta_b%5Cright)%0A"></p>
<p>Gating amounts to multiply it by <img src="https://latex.codecogs.com/png.latex?x_p(t)">, which can be described as the time-delayed rectangular function <img src="https://latex.codecogs.com/png.latex?%5Ctext%7Brect%7D(t/T_g)">, periodized by the fundamental period <img src="https://latex.codecogs.com/png.latex?T_r">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax_p(t)=%5Csum_%7Bn=-%5Cinfty%7D%5E%7B+%5Cinfty%7D%5Ctext%7Brect%7D%5Cleft(%5Cdfrac%7Bt-%5Ctau_r-nT_r%7D%7BT_g%7D%5Cright)%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Ctau_r"> is the time-shift of the rectangular function before submitting it to periodization. Henceforth, we assume that <img src="https://latex.codecogs.com/png.latex?T_r%3ET_b">, and a multiple integer of full cycles of <img src="https://latex.codecogs.com/png.latex?x_b(t)"> is captured in either the time interval <img src="https://latex.codecogs.com/png.latex?T_g"> or the time interval <img src="https://latex.codecogs.com/png.latex?T_r">: in Figure&nbsp;1, <img src="https://latex.codecogs.com/png.latex?T_g=2T_b"> (<img src="https://latex.codecogs.com/png.latex?N=2">) and <img src="https://latex.codecogs.com/png.latex?T_r=6T_b"> (<img src="https://latex.codecogs.com/png.latex?M=4">, hence <img src="https://latex.codecogs.com/png.latex?N+M=6">).</p>
<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">
Rectangular function
</div>
</div>
<div class="callout-body-container callout-body">
<p>The rectangular function (also known as the rect function, gate function) is defined as:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Brect%7D%5Cleft(%5Cdfrac%7Bt%7D%7BT%7D%5Cright)=%5Cleft%5C%7B%0A%5Cbegin%7Balign%7D%0A1,&amp;%5Cquad%5Cvert%20t%5Cvert%5Cleq%20T/2%5C%5C%0A1/2,&amp;%5Cquad%20t=T/2%5C%5C%0A0,&amp;%5Cquad%20t%3ET/2%0A%5Cend%7Balign%7D%0A%5Cright.%0A"></p>
<p>The Continuous Time Fourier Transform of the rectangular function <img src="https://latex.codecogs.com/png.latex?%5Ctext%7Brect%7D(t/T)"> is:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AX(f)=T%5C,%5Ctext%7Bsinc%7D(fT)%0A"></p>
</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">
Sinc function
</div>
</div>
<div class="callout-body-container callout-body">
<p>In digital signal processing and information theory, the <strong>sinc function</strong> is commonly defined for <img src="https://latex.codecogs.com/png.latex?x%5Cneq0"> by</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Bsinc%7D(x)=%5Cdfrac%7B%5Csin(%5Cpi%20x)%7D%7B%5Cpi%20x%7D%0A"></p>
</div>
</div>
<p>The duty-ratio factor is defined as the ratio of time the tone is ON compared to the total time when it is either ON or OFF, namely <img src="https://latex.codecogs.com/png.latex?D=N/(N+M)=N/L">. The duty-ratio factor approaches 0 for widely spaced, narrow bursts or 1 for closely spaced, wide bursts: when <img src="https://latex.codecogs.com/png.latex?D=1">, <img src="https://latex.codecogs.com/png.latex?x_r(t)=1"> and <img src="https://latex.codecogs.com/png.latex?x(t)=x_b(t)"> (pure sine-wave).</p>
<p>When the duty-ratio factor is a rational number, the sine-wave tone burst <img src="https://latex.codecogs.com/png.latex?x(t)"> can be modeled as the product of two periodical signals, namely <img src="https://latex.codecogs.com/png.latex?x_b(t)"> (with period <img src="https://latex.codecogs.com/png.latex?T_b">) and <img src="https://latex.codecogs.com/png.latex?x_r(t)"> (with period <img src="https://latex.codecogs.com/png.latex?T_r">):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax(t)=x_r(t)%5Ccdot%20x_b(t)%0A"></p>
<p>The fundamental period of <img src="https://latex.codecogs.com/png.latex?x(t)"> is <img src="https://latex.codecogs.com/png.latex?T=T_r">. We can appropriately choose the phase shift <img src="https://latex.codecogs.com/png.latex?%5Ctheta_b"> and the time delay <img src="https://latex.codecogs.com/png.latex?%5Ctau_r"> so as to yield a signal <img src="https://latex.codecogs.com/png.latex?x(t)"> without discontinuities and further endowed with odd symmetry (e.g., <img src="https://latex.codecogs.com/png.latex?%5Ctheta_b=0">, <img src="https://latex.codecogs.com/png.latex?%5Ctau_r=0">). Under these conditions, <img src="https://latex.codecogs.com/png.latex?x(t)"> can be regarded as composed by infinitely many replicas (with period <img src="https://latex.codecogs.com/png.latex?T_r">) of the time-limited signal <img src="https://latex.codecogs.com/png.latex?x_p(t)">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax_p(t)=%5Cleft%5C%7B%0A%5Cbegin%7Balign%7D%0A%5Csin%5Cleft(2%5Cpi%5Cdfrac%7Bt%7D%7BT_b%7D%5Cright),&amp;%5Cquad%5Cvert%20t%5Cvert%5Cleq%20T_g/2%5C%5C%0A0,&amp;%5Cquad%5Ctext%7Belsewhere%7D%0A%5Cend%7Balign%7D%0A%5Cright.%0A"></p>
</section>
<section id="fourier-analysis" class="level2">
<h2 class="anchored" data-anchor-id="fourier-analysis">Fourier analysis</h2>
<p>The Fourier series of <img src="https://latex.codecogs.com/png.latex?x(t)"> is defined as a trigonometric series of the form:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ax(t)=A_0+%5Csum_%7Bn=1%7D%5E%7B+%5Cinfty%7DA_n%5Ccos%5Cleft(2%5Cpi%20%5Cdfrac%7Bn%7D%7BT_r%7Dt%5Cright)+%5Csum_%7Bi=1%7D%5E%7B+%5Cinfty%7DB_n%5Csin%5Cleft(2%5Cpi%20%5Cdfrac%7Bn%7D%7BT_r%7Dt%5Cright)%0A"></p>
<p>where the Fourier series coefficients are defined by the integrals:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Balign%7D%0AA_0&amp;=%5Cdfrac%7B1%7D%7BT_r%7D%5Cint_%7B-T_%7Br%7D/2%7D%5E%7BT_%7Br%7D/2%7Dx(t)%5C,dt%5C%5C%0AA_n&amp;=%5Cdfrac%7B2%7D%7BT_r%7D%5Cint_%7B-T_%7Br%7D/2%7D%5E%7BT_%7Br%7D/2%7Dx(t)%5Ccos%5Cleft(2%5Cpi%5Cdfrac%7Bnt%7D%7BT_r%7D%5Cright)%5C,dt%5C%5C%0AB_n&amp;=%5Cdfrac%7B2%7D%7BT_r%7D%5Cint_%7B-T_%7Br%7D/2%7D%5E%7BT_%7Br%7D/2%7Dx(t)%5Csin%5Cleft(2%5Cpi%5Cdfrac%7Bnt%7D%7BT_r%7D%5Cright)%5C,dt%0A%5Cend%7Balign%7D%0A%5Cright.%0A"></p>
<p>Since <img src="https://latex.codecogs.com/png.latex?x(t)"> is odd-symmetric, <img src="https://latex.codecogs.com/png.latex?A_0"> is null (i.e., the sine-wave tone burst has no DC component) and <img src="https://latex.codecogs.com/png.latex?A_n=0,n=1,%5Ccdots,+%5Cinfty">. The harmonic coefficients <img src="https://latex.codecogs.com/png.latex?B_n"> are given by:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AB_n=%5Cdfrac%7B4%7D%7BT_r%7D%5Cint_0%5E%7BT_g/2%7D%5Csin%5Cleft(2%5Cpi%5Cdfrac%7Bt%7D%7BT_b%7D%5Cright)%5Csin%5Cleft(2%5Cpi%5Cdfrac%7Bnt%7D%7BT_r%7D%5Cright)dt%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?T_r=LT_b"> (<img src="https://latex.codecogs.com/png.latex?L=N+M">) and <img src="https://latex.codecogs.com/png.latex?T_g=N%20T_b">. To make the notation lighter, <img src="https://latex.codecogs.com/png.latex?T_b=T"> in the following. The spectrum of the sine-wave tone burst requires the calculation of the Fourier integrals:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AB_n=%5Cdfrac%7B4%7D%7BLT%7D%5Cint_%7B0%7D%5E%7BN%20T/2%7D%5Csin%5Cleft(%5Cdfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%5Csin%5Cleft(%5Cdfrac%7B2%5Cpi%7D%7BT%7D%5Cdfrac%7Bn%7D%7BL%7Dt%5Cright)dt%0A"></p>
<div class="callout callout-style-default callout-tip 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">
Werner formulas
</div>
</div>
<div class="callout-body-container callout-body">
<p>The Werner formulas are the trigonometric product formulas:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0A2%5Csin%5Calpha%5Ccos%5Cbeta&amp;=%5Csin(%5Calpha-%5Cbeta)+%5Csin(%5Calpha+%5Cbeta)%5C%5C%0A2%5Ccos%5Calpha%5Ccos%5Cbeta&amp;=%5Ccos(%5Calpha-%5Cbeta)+%5Ccos(%5Calpha+%5Cbeta)%5C%5C%0A2%5Ccos%5Calpha%5Csin%5Cbeta&amp;=%5Csin(%5Calpha+%5Cbeta)-%5Csin(%5Calpha-%5Cbeta)%5C%5C%0A2%5Csin%5Calpha%5Csin%5Cbeta&amp;=%5Ccos(%5Calpha-%5Cbeta)-%5Ccos(%5Calpha+%5Cbeta)%0A%5Cend%7Balign%7D%0A"></p>
</div>
</div>
<p>Applying the Werner formulas, the integrand can be written:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Csin%5Cleft(%5Cdfrac%7B2%5Cpi%7D%7BT%7Dt%5Cright)%5Csin%5Cleft(%5Cdfrac%7B2%5Cpi%7D%7BT%7D%5Cdfrac%7Bn%7D%7BL%7Dt%5Cright)=%5Cdfrac%7B1%7D%7B2%7D%5Cleft%5B%5Ccos%5Cleft(%5Cdfrac%7B2%5Cpi%7D%7BT%7D%5Cleft(1-%5Cdfrac%7Bn%7D%7BL%7D%5Cright)t%5Cright)-%5Ccos%5Cleft(%5Cdfrac%7B2%5Cpi%7D%7BT%7D%5Cleft(1+%5Cdfrac%7Bn%7D%7BL%7D%5Cright)t%5Cright)%5Cright%5D%0A"></p>
<p>Therefore:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0AB_n%20&amp;=%20%5Cdfrac%7B2%7D%7BLT%7D%5Cdfrac%7B1%7D%7B%5Cdfrac%7B2%5Cpi%7D%7BT%7D%5Cleft(1-%5Cdfrac%7Bn%7D%7BL%7D%5Cright)%7D%5Csin%5Cleft(%5Cdfrac%7B2%5Cpi%7D%7BT%7D%5Cleft(1-%5Cdfrac%7Bn%7D%7BL%7D%5Cright)t%5Cright)%5Cbiggr%5Cvert_0%5E%7BNT/2%7D%5C%5C%0A&amp;-%5Cdfrac%7B2%7D%7BLT%7D%5Cdfrac%7B1%7D%7B%5Cdfrac%7B2%5Cpi%7D%7BT%7D%5Cleft(1+%5Cdfrac%7Bn%7D%7BL%7D%5Cright)%7D%5Csin%5Cleft(%5Cdfrac%7B2%5Cpi%7D%7BT%7D%5Cleft(1+%5Cdfrac%7Bn%7D%7BL%7D%5Cright)t%5Cright)%5Cbiggr%5Cvert_0%5E%7BNT/2%7D%0A%5Cend%7Balign%7D%0A"></p>
<p>and</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Balign%7D%0AB_n%20&amp;=%20%5Cdfrac%7B2%7D%7B%5Cpi%20L%7D%5Cdfrac%7B1%7D%7B%5Cleft(1-%5Cdfrac%7Bn%7D%7BL%7D%5Cright)%7D%5Csin%5Cleft(%5Cpi%20N%5Cleft(1-%5Cdfrac%7Bn%7D%7BL%7D%5Cright)%5Cright)-%5Cdfrac%7B2%7D%7B%5Cpi%20L%7D%5Cdfrac%7B1%7D%7B%5Cleft(1+%5Cdfrac%7Bn%7D%7BL%7D%5Cright)%7D%5Csin%5Cleft(%5Cpi%20N%5Cleft(1+%5Cdfrac%7Bn%7D%7BL%7D%5Cright)%5Cright)%5C%5C%0A&amp;=%5Cdfrac%7BN%7D%7BL%7D%5Cleft%5B%5Ctext%7Bsinc%7D%5Cleft(N%5Cleft(1-%5Cdfrac%7Bn%7D%7BL%7D%5Cright)%5Cright)-%5Ctext%7Bsinc%7D%5Cleft(N%5Cleft(1+%5Cdfrac%7Bn%7D%7BL%7D%5Cright)%5Cright)%5Cright%5D%0A%5Cend%7Balign%7D%0A"></p>
<p>Using the duty-ratio factor, we have:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AB_n=D%5Cleft%5B%5Ctext%7Bsinc%7D(D(L-n))-%5Ctext%7Bsinc%7D(D(L+n))%5Cright%5D%0A"></p>
<p>When <img src="https://latex.codecogs.com/png.latex?D=1"> (i.e., <img src="https://latex.codecogs.com/png.latex?x_r(t)=1">), we get <img src="https://latex.codecogs.com/png.latex?B_n%20=%200"> for all <img src="https://latex.codecogs.com/png.latex?n">, but <img src="https://latex.codecogs.com/png.latex?B_L=1">. Recall that the <img src="https://latex.codecogs.com/png.latex?L">th harmonic component in the spectrum of <img src="https://latex.codecogs.com/png.latex?x(t)"> corresponds to the fundamental frequency of the sine-wave, <img src="https://latex.codecogs.com/png.latex?f_b"> - as noted above, if <img src="https://latex.codecogs.com/png.latex?x_r(t)=1">, then <img src="https://latex.codecogs.com/png.latex?x(t)=x_b(t)">. For any value of <img src="https://latex.codecogs.com/png.latex?D"> different from 1, the maximum value of <img src="https://latex.codecogs.com/png.latex?B_n">, <img src="https://latex.codecogs.com/png.latex?B_%7B%5Ctext%7Bmax%7D%7D=D"> still occurs when <img src="https://latex.codecogs.com/png.latex?n=L">, however other lines are present in the spectrum. These lines are at the (analog) frequencies <img src="https://latex.codecogs.com/png.latex?f_n=f_b+(n-L)f_r">.</p>
</section>
<section id="examples" class="level2">
<h2 class="anchored" data-anchor-id="examples">Examples</h2>
<p>A sine-wave tone burst with duty-ratio factor <img src="https://latex.codecogs.com/png.latex?D=0.25"> (<img src="https://latex.codecogs.com/png.latex?f_b=3%5C,%5Ctext%7BHz%7D,f_r=0.25%5C,%5Ctext%7BHz%7D,T_b=1%5C,%5Ctext%7Bs%7D">) was simulated in MATLAB. The sampling frequency chosen for the simulation was <img src="https://latex.codecogs.com/png.latex?f_s=80%5C,%5Ctext%7BHz%7D">. The Fast Fourier Transform (FFT) algorithm was applied for spectrum computation. The guidelines discussed in one previous post of mine (<a href="https://amsabatini.netlify.app/posts/resolution/">here</a>) were followed to prevent spectral leakage and amplitude ambiguity. Superimposed on the values computed by the FFT (solid black points) is the envelope obtained by interpolating the discrete spectrum <img src="https://latex.codecogs.com/png.latex?B_n"> (see above), Figure&nbsp;2.</p>
<div id="fig-fig2" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/tone_burst/fig_spectrum.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="400">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: The main part of the envelope of the spectrum of a sine-wave tone burst at 3 Hz.
</figcaption>
</figure>
</div>
<p>The MATLAB code was run again in a scenario where the frequency of the sine wave moved up to <img src="https://latex.codecogs.com/png.latex?f_b=60%5C,%5Ctext%7BHz%7D">, with a corresponding adjustment of the sampling frequency (<img src="https://latex.codecogs.com/png.latex?f_s=800%5C,%5Ctext%7BHz%7D">). The results of the FFT-based spectrum analysis are reported in Figure&nbsp;3.</p>
<div id="fig-fig3" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/tone_burst/fig_spectrum_1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="400">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: The main part of the envelope of the spectrum of a sine-wave tone burst at 60 Hz.
</figcaption>
</figure>
</div>
<p>The FFT-based spectrum can be also interpreted as the outcome of the following cascaded frequency-domain signal-processing operations:</p>
<ul>
<li><p><em>frequency sampling</em> the spectrum <img src="https://latex.codecogs.com/png.latex?T_g%5C,%5Ctext%7Bsinc%7D(f%5C,T_g)"> - this is explained by the time periodization of the rectangular function <img src="https://latex.codecogs.com/png.latex?%5Ctext%7Brect%7D(t/T_g)"> with period <img src="https://latex.codecogs.com/png.latex?T_r">.</p></li>
<li><p><em>frequency shifting</em> the frequency-sampled spectrum - this is explained by the amplitude modulation impressed on the periodized rectangular function when the latter is multiplied by the sine wave at frequency <img src="https://latex.codecogs.com/png.latex?f_b">.</p></li>
</ul>


</section>

 ]]></description>
  <category>signal processing</category>
  <guid>https://amsabatini.netlify.app/posts/tone_burst/</guid>
  <pubDate>Wed, 15 Jan 2025 23:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/tone_burst/fig_spectrum.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Spell checking using hunspell</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/spell_checking/</link>
  <description><![CDATA[ 




<section id="spell-checking" class="level2">
<h2 class="anchored" data-anchor-id="spell-checking">Spell checking</h2>
<p>Spell checking text consists of the following steps:</p>
<ul>
<li>Parse a document by extracting (tokenizing) words that we want to check</li>
<li>Analyze each word by breaking it down in its root (stemming) and conjugation affix</li>
<li>Lookup in a dictionary if the word+affix combination if valid for the stated language</li>
<li>(optional) For incorrect words, suggest corrections by finding similar (correct) words in the dictionary</li>
</ul>
<section id="check-individual-words" class="level3">
<h3 class="anchored" data-anchor-id="check-individual-words">Check individual words</h3>
<p>Let us suppose that the text to be spell checked is composed of a char array, with each element of the array being a single word expressed in the specified language (Italian, here). A custom dictionary can be set in the <code>dict</code> parameter when functions hunspell_* are invoked.</p>
<div class="cell">
<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>(hunspell)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check individual words</span></span>
<span id="cb1-4">words   <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;">"amore"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ammore"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"prof"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"professsore"</span>)</span>
<span id="cb1-5">correct <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;">hunspell_check</span>(words, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dict =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dictionary</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"it_IT"</span>))</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(correct)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1]  TRUE FALSE  TRUE FALSE</code></pre>
</div>
<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;"># incorrect words</span></span>
<span id="cb3-2">incorrect.words <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> words[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>correct]</span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># find suggestions for incorrect words</span></span>
<span id="cb3-4">suggested.words <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;">hunspell_suggest</span>(incorrect.words, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dict =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dictionary</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"it_IT"</span>))</span>
<span id="cb3-5">suggested.words <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;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(suggested.words, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span></code></pre></div>
</div>
<p>Note that the outcome of the function <code>hunspell_suggest()</code> is a list. An array of corrected words can then be constructed for the corresponding misspelled words. Clearly, some words are not the best choice, and the first word is usually the best alternative. The quality of data can be improved by careful exploration of the list.</p>
<div class="cell">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="width: auto !important; margin-left: auto; margin-right: auto;">
<caption>Spell checking text</caption>
 <thead>
  <tr>
   <th style="text-align:left;"> word </th>
   <th style="text-align:left;"> best alternative </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> ammore </td>
   <td style="text-align:left;"> amore </td>
  </tr>
  <tr>
   <td style="text-align:left;"> professsore </td>
   <td style="text-align:left;"> professore </td>
  </tr>
</tbody>
</table>

</div>
</div>
</section>
</section>
<section id="an-example" class="level2">
<h2 class="anchored" data-anchor-id="an-example">An example</h2>
<p>Let us suppose that we want to analyze the lyrics of the song titled “Il mio canto libero” (<a href="https://genius.com/Lucio-battisti-il-mio-canto-libero-lyrics">here</a>). One single occurrence of the words “emozioni”, “nuda” and “pianto” was intentionally misspelled, namely, “emozzioni”, “nudda”, “painto” were transcripted, respectively.</p>
<p>To get the list of words used in the lyrics, I first split the text into individual words. To do so, I use <code>unnest_tokens()</code> from the tidytext package.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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>(tidyverse)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stringi)</span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidytext)</span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data upload</span></span>
<span id="cb4-6">lyrics <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;">stri_read_lines</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Il mio canto libero.txt"</span>)</span>
<span id="cb4-7">df_pre <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;">text =</span> lyrics) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-8">  <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;">id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_number</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(id, text)</span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># tokenization</span></span>
<span id="cb4-11">tokenized_text_pre <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_pre <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest_tokens</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> word, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">token =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"words"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">input =</span> text) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(word, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sort =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb4-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check spelling</span></span>
<span id="cb4-15">correct <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;">hunspell_check</span>(tokenized_text_pre<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>word, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dict =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dictionary</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"it_IT"</span>))</span>
<span id="cb4-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># incorrect words</span></span>
<span id="cb4-17">incorrect.words <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tokenized_text_pre<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>word[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>correct]</span>
<span id="cb4-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># find suggestions for incorrect words</span></span>
<span id="cb4-19">suggested.words <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;">hunspell_suggest</span>(incorrect.words, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dict =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dictionary</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"it_IT"</span>))</span>
<span id="cb4-20">suggested.words <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;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(suggested.words, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) x[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]))</span>
<span id="cb4-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># list of incorrect and suggested words</span></span>
<span id="cb4-22">word.list <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">word =</span> incorrect.words, <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;">best alternative</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> suggested.words))</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="width: auto !important; margin-left: auto; margin-right: auto;">
<caption>Spell checking text - 'Il mio canto libero'</caption>
 <thead>
  <tr>
   <th style="text-align:left;"> word </th>
   <th style="text-align:left;"> best alternative </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> emozzioni </td>
   <td style="text-align:left;"> emozioni </td>
  </tr>
  <tr>
   <td style="text-align:left;"> nudda </td>
   <td style="text-align:left;"> nuda </td>
  </tr>
  <tr>
   <td style="text-align:left;"> painto </td>
   <td style="text-align:left;"> pianto </td>
  </tr>
</tbody>
</table>

</div>
</div>
<p>The misspelled words in the song lyrics can be replaced with the suggested alternative using the code below.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">incorrect.whole.words <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;">paste0</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><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">b"</span>, word.list<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>word, <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><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">b"</span>)</span>
<span id="cb5-2">lyrics <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;">stri_replace_all_regex</span>(lyrics, incorrect.whole.words, suggested.words, </span>
<span id="cb5-3">                                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vectorize_all =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
</details>
</div>
<p>The count of the words that were misspelled is correctly updated after spell checking and replacement.</p>
<div class="cell">
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="width: auto !important; margin-left: auto; margin-right: auto;">
<caption>Before spell checking</caption>
 <thead>
  <tr>
   <th style="text-align:left;"> word </th>
   <th style="text-align:right;"> n </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> emozioni </td>
   <td style="text-align:right;"> 1 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> emozzioni </td>
   <td style="text-align:right;"> 1 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> nuda </td>
   <td style="text-align:right;"> 1 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> nudda </td>
   <td style="text-align:right;"> 1 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> painto </td>
   <td style="text-align:right;"> 1 </td>
  </tr>
</tbody>
</table>

</div>
<div class="cell-output-display">
<table quarto-disable-processing="true" class="table table-condensed table-bordered" style="width: auto !important; margin-left: auto; margin-right: auto;">
<caption>After spell checking</caption>
 <thead>
  <tr>
   <th style="text-align:left;"> word </th>
   <th style="text-align:right;"> n </th>
  </tr>
 </thead>
<tbody>
  <tr>
   <td style="text-align:left;"> emozioni </td>
   <td style="text-align:right;"> 2 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> nuda </td>
   <td style="text-align:right;"> 2 </td>
  </tr>
  <tr>
   <td style="text-align:left;"> pianto </td>
   <td style="text-align:right;"> 1 </td>
  </tr>
</tbody>
</table>

</div>
</div>
<p>The thumbnail image is credited to <a href="https://www.flaticon.com/free-icons/text-mining" title="text mining icons">Text mining icons created by Freepik - Flaticon</a></p>


</section>

 ]]></description>
  <category>text mining</category>
  <guid>https://amsabatini.netlify.app/posts/spell_checking/</guid>
  <pubDate>Wed, 01 Jan 2025 23:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/spell_checking/text-mining.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Correspondence analysis: Part II</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/correspondence_analysis_2/</link>
  <description><![CDATA[ 




<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>In this post, we will briefly discuss an example of how Simple Correspondence Analysis (SCA) can be performed using the R programming language. Several functions from different packages are available in the R software for computing SCA. No matter what package we decide to use for the analysis (in this post, we will use the package FactoMineR), the package factoextra is great for producing ggplot2-based elegant visualizations of analysis results. A couple of other packages, namely gplots and corrplot, will also be used.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Libraries </span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gplots)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(corrplot)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(FactoMineR)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(factoextra)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(kableExtra)</span></code></pre></div>
</details>
</div>
</section>
<section id="exemplary-dataset" class="level2">
<h2 class="anchored" data-anchor-id="exemplary-dataset">Exemplary dataset</h2>
<p><code>housetasks</code>, available in the package factoextra, is a data frame that contains the frequency of execution of 13 house tasks performed by the couple in four different ways: a) the wife only; b) alternatively; c) the husband only; d) jointly.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">data</span>(housetasks)</span>
<span id="cb2-2">housetasks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb2-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rrrr"</span>,</span>
<span id="cb2-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Wife"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alternating"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Husband"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jointly"</span>),</span>
<span id="cb2-6">    <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;">"House tasks contingency table"</span>,</span>
<span id="cb2-7">  ) <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;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>House tasks contingency table</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Wife</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Alternating</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Husband</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Jointly</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Laundry</td>
<td style="text-align: right;">156</td>
<td style="text-align: right;">14</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">4</td>
</tr>
<tr class="even">
<td style="text-align: left;">Main_meal</td>
<td style="text-align: right;">124</td>
<td style="text-align: right;">20</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">4</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Dinner</td>
<td style="text-align: right;">77</td>
<td style="text-align: right;">11</td>
<td style="text-align: right;">7</td>
<td style="text-align: right;">13</td>
</tr>
<tr class="even">
<td style="text-align: left;">Breakfeast</td>
<td style="text-align: right;">82</td>
<td style="text-align: right;">36</td>
<td style="text-align: right;">15</td>
<td style="text-align: right;">7</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Tidying</td>
<td style="text-align: right;">53</td>
<td style="text-align: right;">11</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">57</td>
</tr>
<tr class="even">
<td style="text-align: left;">Dishes</td>
<td style="text-align: right;">32</td>
<td style="text-align: right;">24</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">53</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Shopping</td>
<td style="text-align: right;">33</td>
<td style="text-align: right;">23</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">55</td>
</tr>
<tr class="even">
<td style="text-align: left;">Official</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">46</td>
<td style="text-align: right;">23</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Driving</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">51</td>
<td style="text-align: right;">75</td>
<td style="text-align: right;">3</td>
</tr>
<tr class="even">
<td style="text-align: left;">Finances</td>
<td style="text-align: right;">13</td>
<td style="text-align: right;">13</td>
<td style="text-align: right;">21</td>
<td style="text-align: right;">66</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Insurance</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">53</td>
<td style="text-align: right;">77</td>
</tr>
<tr class="even">
<td style="text-align: left;">Repairs</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">160</td>
<td style="text-align: right;">2</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holidays</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">153</td>
</tr>
</tbody>
</table>


</div>
<details class="code-fold">
<summary>Code</summary>
<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;"># Convert the data frame to a table</span></span>
<span id="cb3-2">dt <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.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(housetasks))</span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Graph</span></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">balloonplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(dt), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"House tasks contingency table"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb3-5">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.margins =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="implementation" class="level2">
<h2 class="anchored" data-anchor-id="implementation">Implementation</h2>
<p>The function <code>CA()</code> from the package FactoMineR is used. It performs the Singular Value Decomposition of the standardized residuals, see my previous post <a href="https://amsabatini.netlify.app/posts/correspondence_analysis_1">here</a>. The first step of the analysis was done there in order to evaluate whether there was a significant dependency between the rows and columns using the chi-square statistic.</p>
<div class="cell">
<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">res.ca <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;">CA</span>(housetasks, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">graph =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(res.ca)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>**Results of the Correspondence Analysis (CA)**
The row variable has  13  categories; the column variable has 4 categories
The chi square of independence between the two variables is equal to 1944.456 (p-value =  0 ).
*The results are available in the following objects:

   name              description                   
1  "$eig"            "eigenvalues"                 
2  "$col"            "results for the columns"     
3  "$col$coord"      "coord. for the columns"      
4  "$col$cos2"       "cos2 for the columns"        
5  "$col$contrib"    "contributions of the columns"
6  "$row"            "results for the rows"        
7  "$row$coord"      "coord. for the rows"         
8  "$row$cos2"       "cos2 for the rows"           
9  "$row$contrib"    "contributions of the rows"   
10 "$call"           "summary called parameters"   
11 "$call$marge.col" "weights of the columns"      
12 "$call$marge.row" "weights of the rows"         </code></pre>
</div>
</div>
</section>
<section id="visualization-and-interpretation" class="level2">
<h2 class="anchored" data-anchor-id="visualization-and-interpretation">Visualization and interpretation</h2>
<p>The following functions available the factoextra package can be used to help in the SCA interpretation and visualization:</p>
<div class="cell">
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Functions from FactoMineR package for SCA</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">Function name</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Purpose</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">get_eigenvalue()</td>
<td style="text-align: left;">Extract the eigenvalues/variances retained by each dimension (axis)</td>
</tr>
<tr class="even">
<td style="text-align: left;">fviz_eig()</td>
<td style="text-align: left;">Visualize the eigenvalues</td>
</tr>
<tr class="odd">
<td style="text-align: left;">fviz_screeplot()</td>
<td style="text-align: left;">Scree plot</td>
</tr>
<tr class="even">
<td style="text-align: left;">get_ca_row()</td>
<td style="text-align: left;">Extract the results for rows</td>
</tr>
<tr class="odd">
<td style="text-align: left;">get_ca_col()</td>
<td style="text-align: left;">Extract the results for columns</td>
</tr>
<tr class="even">
<td style="text-align: left;">fviz_ca_row()</td>
<td style="text-align: left;">Visualize the results for rows</td>
</tr>
<tr class="odd">
<td style="text-align: left;">fviz_ca_col()</td>
<td style="text-align: left;">Visualize the results for columns</td>
</tr>
<tr class="even">
<td style="text-align: left;">fviz_ca_cos2()</td>
<td style="text-align: left;">Cos2 of rows on selected axes</td>
</tr>
<tr class="odd">
<td style="text-align: left;">fviz_ca_biplot()</td>
<td style="text-align: left;">Make a biplot of rows and columns</td>
</tr>
</tbody>
</table>


</div>
</div>
<section id="eigenvaluesvariance" class="level3">
<h3 class="anchored" data-anchor-id="eigenvaluesvariance">Eigenvalues/variance</h3>
<p>Eigenvalues correspond to the amount of information retained by each axis. Dimensions are ordered from largest to smallest and reported with the amount of variance each of them <em>explains</em> in the solution. Dimension 1 (Dim.1) explains the most variance in the solution, followed by dimension 2 (Dim.2) and so on. The cumulative percentage explained is obtained by adding the successive proportions of variance explained. For instance, 48.69% plus 39.91% equals 88.6%, and so forth. Therefore, about 88.6% of the variance is explained by the first two dimensions.</p>
<p>Eigenvalues can be used to determine the number of axes to retain. There is no “rule of thumb” to choose the number of dimensions to keep for the data interpretation. It depends on the research question and the need of the researcher. For example, if 80% of the total variation explained is believed satisfactory for the purpose of the analysis, then the number of dimensions necessary to achieve that goal will be considered. Generally, a good dimension reduction is achieved with the first few dimensions accounting for a large proportion of the variation. In the example in this post, the first two axes explain 88.6% of the variation.</p>
<p>An alternative method to determine the number of dimensions is to look at a Scree Plot, which is the plot of eigenvalues/variances ordered from largest to smallest. The number of components is determined at the point, beyond which the remaining eigenvalues are all relatively small and of comparable size.</p>
<p>In the example in this post, the house tasks contingency table contains 13 rows and 4 columns. If the data were random, the expected value of the eigenvalue for each axis would be 1/(nrow(housetasks)-1) (rows), and 1/(ncol(housetasks)-1) (columns). Any axis contributing more than the maximum of these two percentages, namely <img src="https://latex.codecogs.com/png.latex?33.33%5C%25"> in the example here, should be considered as important and included in the final solution.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">eig.val <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;">get_eigenvalue</span>(res.ca)</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(eig.val, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>      eigenvalue variance.percent cumulative.variance.percent
Dim.1       0.54            48.69                       48.69
Dim.2       0.45            39.91                       88.60
Dim.3       0.13            11.40                      100.00</code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<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">v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(housetasks)<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(housetasks)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb8-2"></span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_screeplot</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">addlabels =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>According to the graph above, only Dim.1 and Dim.2 should be used in the solution. Dim.3 explains only 11.4% of the total variation, which is well below the stated threshold at 33.33%, hence it can be discarded. Dim.1 and Dim.2 explain approximately 48.7% and 39.9% of the total variation, respectively. This corresponds to a cumulative total of 88.6% of total variation retained by them.</p>
</section>
<section id="biplot" class="level3">
<h3 class="anchored" data-anchor-id="biplot">Biplot</h3>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># repel = TRUE to avoid text overlapping</span></span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_ca_biplot</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The graph above is called symmetric plot, or biplot. The biplot is helpful to show global patterns existing within the data. Rows are represented by blue points and columns by red triangles. The distance between any pair of row points, or any pair of column points, gives a measure of their similarity (or dissimilarity). Row (column) points with similar profile are close in the biplot.</p>
<p>The biplot represents the row and column profiles simultaneously in a common space. It is worth stress that:</p>
<ul>
<li><em>the distance between any row and column items is not meaningful</em></li>
<li><em>only the distance between row points or the distance between column points can be interpreted</em>.</li>
</ul>
<p>In order to interpret the distance between column <em>and</em> row points, the column (row) profiles must be presented in row (column) space. The resulting type of map is called asymmetric biplot; it is not discussed in this post.</p>
</section>
<section id="graph-of-row-variables" class="level3">
<h3 class="anchored" data-anchor-id="graph-of-row-variables">Graph of row variables</h3>
<p>The function <code>get_ca_row()</code> is used to extract the results for row points. This function returns a list containing the coordinates, the squared cosine (cos2), the contribution and the inertia of row points:</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">row <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;">get_ca_row</span>(res.ca)</span>
<span id="cb10-2">row</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Correspondence Analysis - Results for rows
 ===================================================
  Name       Description                
1 "$coord"   "Coordinates for the rows" 
2 "$cos2"    "Cos2 for the rows"        
3 "$contrib" "contributions of the rows"
4 "$inertia" "Inertia of the rows"      </code></pre>
</div>
</div>
<p>The components of the get_ca_row() function can be used in the plot of rows as follows:</p>
<ul>
<li><code>row$coord</code>: coordinates of each row point in each dimension (1, 2 and 3).</li>
<li><code>row$cos2</code>: quality of representation of rows.</li>
<li><code>var$contrib</code>: contribution of rows (in %) to the definition of the dimensions.</li>
</ul>
<p><strong>Coordinates of row points</strong></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_ca_row</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.row =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape.row =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The plot above shows the relationships between row points:</p>
<ul>
<li>Rows with a similar profile are grouped together.</li>
<li>Negatively correlated rows are positioned on opposite sides of the plot origin.</li>
<li>The distance between row points and the origin measures the quality of the row points on the scatter plot. The higher the distance, the better the quality.</li>
</ul>
<p><strong>Quality of representation of rows</strong></p>
<p>I recall that the quality of representation of the rows on the biplot is called the squared cosine (cos2) or the squared correlation. The cos2 measures the degree of association between rows and a particular axis. The values of the cos2 range in the interval between 0 and 1. The sum of the cos2 for rows on all the CA dimensions is equal to one. The quality of representation of a row in <img src="https://latex.codecogs.com/png.latex?n"> dimensions is simply the sum of the squared cosine of that row over the <img src="https://latex.codecogs.com/png.latex?n"> dimensions. If a row item is well represented by two dimensions, the sum of the cos2 should be close to one. For some of the row items, more than 2 dimensions may be required to perfectly represent the data.</p>
<p>It is possible to color row points by their cos2 values using the argument <code>col.row = "cos2"</code> in <code>fviz_ca_row()</code>. This produces a color gradient, which can be customized using the argument gradient.cols. For instance, <code>gradient.cols = c("white", "blue", "red")</code> means that:</p>
<ul>
<li>variables with low cos2 values will be colored in “white”</li>
<li>variables with mid cos2 values will be colored in “blue”</li>
<li>variables with high cos2 values will be colored in “red”</li>
</ul>
<p>The row variables with the larger value of cos2, contribute the most to the definition of the dimensions. Rows that contribute the most to the retained dimensions, i.e., Dim.1 and Dim.2, are the most important in the process of variance explanation. Rows that do not contribute much to the retained dimensions or that contribute to the last dimensions, i.e., Dim.3, are less important.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Color by cos2 values</span></span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_ca_row</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.row =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cos2"</span>,</span>
<span id="cb13-3">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gradient.cols =</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;">"#00AFBB"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E7B800"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FC4E07"</span>), </span>
<span id="cb13-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cos2 of rows on Dim.1 and Dim.2</span></span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_cos2</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">choice =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axes =</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></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-9-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The function <code>fviz_contrib()</code> can be used to draw a bar plot of row contributions. The red dashed line on the graph below indicates the expected average value, if the contributions were uniform.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Contributions of rows to dimension 1</span></span>
<span id="cb15-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_contrib</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">choice =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axes =</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;">top =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Contributions of rows to dimension 2</span></span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_contrib</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">choice =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axes =</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;">top =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-10-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Total contribution to dimension 1 and 2</span></span>
<span id="cb17-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_contrib</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">choice =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axes =</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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">top =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-10-3.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>It can be seen that:</p>
<ul>
<li>The row items “Repairs”, “Laundry”, “Main_meal” and “Driving” are the most important in the definition of the first dimension.</li>
<li>The row items “Holidays” and “Repairs” contribute the most to the dimension 2.</li>
<li>The most important row points can be highlighted on the biplot, as explained above.</li>
</ul>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">fviz_ca_row</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.row =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"contrib"</span>,</span>
<span id="cb18-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gradient.cols =</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;">"#00AFBB"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E7B800"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FC4E07"</span>), </span>
<span id="cb18-3">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The scatter plot gives an idea of what pole of the retained dimensions the row categories are contributing to. The row categories “Repair” and “Driving” have an important contribution to the positive pole of the first dimension, while the categories “Laundry” and “Main_meal” have an important contribution to the negative pole of the first dimension, and so forth. In other words, Dim.1 is mainly defined by the opposition of “Repair” and “Driving” (positive pole), and “Laundry” and “Main_meal” (negative pole).</p>
</section>
<section id="graph-of-column-variables" class="level3">
<h3 class="anchored" data-anchor-id="graph-of-column-variables">Graph of column variables</h3>
<p>The function <code>get_ca_col()</code> is used to extract the results for column variables. This function returns a list containing the coordinates, the squared cosine (cos2), the contribution and the inertia of column variables:</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">col <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;">get_ca_col</span>(res.ca)</span>
<span id="cb19-2">col</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>Correspondence Analysis - Results for columns
 ===================================================
  Name       Description                   
1 "$coord"   "Coordinates for the columns" 
2 "$cos2"    "Cos2 for the columns"        
3 "$contrib" "contributions of the columns"
4 "$inertia" "Inertia of the columns"      </code></pre>
</div>
</div>
<p>The components of the <code>get_ca_col()</code> function can be used in the plot of columns as follows:</p>
<ul>
<li><code>col$coord</code>: coordinates of each column point in each dimension (1, 2 and 3).</li>
<li><code>col$cos2</code>: quality of representation of columns</li>
<li><code>var$contrib</code>: contribution of columns (in %) to the definition of the dimensions.</li>
</ul>
<p><strong>Coordinates of column points</strong></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">fviz_ca_col</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape.col =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The plot above shows the relationships between column points:</p>
<ul>
<li>Columns with a similar profile are grouped together.</li>
<li>Negatively correlated columns are positioned on opposite sides of the plot origin.</li>
<li>The distance between column points and the origin measures the quality of the column points on the scatter polot. The higher the distance, the better the quality.</li>
</ul>
<p><strong>Quality of representation of columns</strong></p>
<p>I recall that the quality of representation of the columns on the biplot is called the squared cosine (cos2) or the squared correlation. The cos2 measures the degree of association between columns and a particular axis. The values of the cos2 range in the interval between 0 and 1. The sum of the cos2 for columns on all the CA dimensions is equal to one. The quality of representation of a column in <img src="https://latex.codecogs.com/png.latex?n"> dimensions is simply the sum of the squared cosine of that column over the <img src="https://latex.codecogs.com/png.latex?n"> dimensions. If a column item is well represented by two dimensions, the sum of the cos2 should be close to one. For some of the column items, more than 2 dimensions may be required to perfectly represent the data.</p>
<p>It is possible to color column points by their cos2 values using the argument <code>col.col = "cos2"</code> in <code>fviz_ca_col()</code>. This produces a color gradient, which can be customized using the argument gradient.cols. For instance, <code>gradient.cols = c("white", "blue", "red")</code> means that:</p>
<ul>
<li>variables with low cos2 values will be colored in “white”</li>
<li>variables with mid cos2 values will be colored in “blue”</li>
<li>variables with high cos2 values will be colored in “red”</li>
</ul>
<p>The column variables with the larger value of cos2, contribute the most to the definition of the dimensions. Columns that contribute the most to the retained dimensions, i.e., Dim.1 and Dim.2, are the most important in the process of variance explanation. Columns that do not contribute much to the retained dimensions or that contribute to the last dimensions, i.e., Dim.3, are less important.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Color by cos2 values</span></span>
<span id="cb22-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_ca_col</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cos2"</span>,</span>
<span id="cb22-3">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gradient.cols =</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;">"#00AFBB"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E7B800"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FC4E07"</span>), </span>
<span id="cb22-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details class="code-fold">
<summary>Code</summary>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cos2 of rows on Dim.1 and Dim.2</span></span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_cos2</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">choice =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axes =</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></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-14-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The function <code>fviz_contrib()</code> can be used to draw a bar plot of column contributions. The red dashed line on the graph below indicates the expected average value, if the contributions were uniform.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Contributions of columns to dimension 1</span></span>
<span id="cb24-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_contrib</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">choice =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axes =</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;">top =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Contributions of colums to dimension 2</span></span>
<span id="cb25-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_contrib</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">choice =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axes =</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;">top =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-15-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Total contribution to dimension 1 and 2</span></span>
<span id="cb26-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_contrib</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">choice =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axes =</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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">top =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-15-3.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>It can be seen that only the column item “Alternating” is not very well displayed on the first two dimensions. The position of this item must be interpreted with caution in the space formed by Dim.1 and Dim.2.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fviz_ca_col</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"contrib"</span>,</span>
<span id="cb27-2">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gradient.cols =</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;">"#00AFBB"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#E7B800"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FC4E07"</span>), </span>
<span id="cb27-3">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="symmetric-biplot" class="level3">
<h3 class="anchored" data-anchor-id="symmetric-biplot">Symmetric biplot</h3>
<p>As mentioned above, the standard plot of SCAs is a symmetric biplot, in which both rows (blue points) and columns (red triangles) are represented in the same space using the principal coordinates. These coordinates represent the row and column profiles. In this case, only the distance between row points or the distance between column points can be interpreted.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">fviz_ca_biplot</span>(res.ca, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">repel =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_2/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<blockquote class="blockquote">
<p>The series of two posts I have compiled so far concerning SCA just scratched the surface of their fascinating, and challenging, world. A lot more should be said to achieve a serious and meaningful interpretation of the results they can offer to the analyst. Maybe, in the future, I will continue the series with additional contributions. Up to now, that’s it.</p>
</blockquote>


</section>
</section>

 ]]></description>
  <category>multivariate statistics</category>
  <guid>https://amsabatini.netlify.app/posts/correspondence_analysis_2/</guid>
  <pubDate>Mon, 09 Dec 2024 23:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/correspondence_analysis_2/cover.png" medium="image" type="image/png" height="102" width="144"/>
</item>
<item>
  <title>Correspondence analysis: Part I</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/correspondence_analysis_1/</link>
  <description><![CDATA[ 




<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Correspondence Analysis (CA) is a type of multidimensional scaling, one of several methods that are available for developing spatial models that reveal associations between two or more categorical variables. If data of only two variables are involved, the method is usually called <strong>Simple Correspondence Analysis</strong> (SCA); if data of more than two variables are considered in the dataset, then the method is usually called Multiple Correspondence Analysis (MCA).</p>
<p>SCA is often used in combination with a standard chi-squared test of independence for two categorical variables that form a contingency table. We recall that a contingency table displays frequencies for combinations of two categorical variables; analysts also refer to contingency tables as crosstabulation and two-way tables. Contingency tables classify outcomes for one variable in rows and the other in columns. The values at the row and column intersections are frequencies for each unique combination of the two variables. These values can suggest whether or not the two variables are correlated.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Libraries </span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gplots)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(corrplot)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(factoextra)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(kableExtra) </span></code></pre></div>
</details>
</div>
</section>
<section id="exemplary-dataset" class="level2">
<h2 class="anchored" data-anchor-id="exemplary-dataset">Exemplary dataset</h2>
<p><code>housetasks</code>, available in the package factoextra, is a data frame that contains the frequency of execution of 13 house tasks performed by the couple in four different ways: a) the wife only; b) alternatively; c) the husband only; d) jointly.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;">data</span>(housetasks)</span>
<span id="cb2-2">housetasks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb2-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rrrr"</span>,</span>
<span id="cb2-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Wife"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alternating"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Husband"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jointly"</span>),</span>
<span id="cb2-6">    <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;">"House tasks contingency table"</span>,</span>
<span id="cb2-7">  ) <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;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>House tasks contingency table</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Wife</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Alternating</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Husband</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Jointly</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Laundry</td>
<td style="text-align: right;">156</td>
<td style="text-align: right;">14</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">4</td>
</tr>
<tr class="even">
<td style="text-align: left;">Main_meal</td>
<td style="text-align: right;">124</td>
<td style="text-align: right;">20</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">4</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Dinner</td>
<td style="text-align: right;">77</td>
<td style="text-align: right;">11</td>
<td style="text-align: right;">7</td>
<td style="text-align: right;">13</td>
</tr>
<tr class="even">
<td style="text-align: left;">Breakfeast</td>
<td style="text-align: right;">82</td>
<td style="text-align: right;">36</td>
<td style="text-align: right;">15</td>
<td style="text-align: right;">7</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Tidying</td>
<td style="text-align: right;">53</td>
<td style="text-align: right;">11</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">57</td>
</tr>
<tr class="even">
<td style="text-align: left;">Dishes</td>
<td style="text-align: right;">32</td>
<td style="text-align: right;">24</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">53</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Shopping</td>
<td style="text-align: right;">33</td>
<td style="text-align: right;">23</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">55</td>
</tr>
<tr class="even">
<td style="text-align: left;">Official</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">46</td>
<td style="text-align: right;">23</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Driving</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">51</td>
<td style="text-align: right;">75</td>
<td style="text-align: right;">3</td>
</tr>
<tr class="even">
<td style="text-align: left;">Finances</td>
<td style="text-align: right;">13</td>
<td style="text-align: right;">13</td>
<td style="text-align: right;">21</td>
<td style="text-align: right;">66</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Insurance</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">53</td>
<td style="text-align: right;">77</td>
</tr>
<tr class="even">
<td style="text-align: left;">Repairs</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">160</td>
<td style="text-align: right;">2</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holidays</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">153</td>
</tr>
</tbody>
</table>


</div>
</div>
<p>To easily interpret the contingency table, a graphical matrix can be drawn using the function <code>balloonplot()</code> from the gplots package. In this graph, each cell contains a dot whose size reflects the relative magnitude of the value it contains.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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;"># Convert the data frame to a table</span></span>
<span id="cb3-2">dt <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.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(housetasks))</span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Graph</span></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">balloonplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(dt), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"House tasks contingency table"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>,</span>
<span id="cb3-5">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show.margins =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_1/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="theory-an-overview" class="level2">
<h2 class="anchored" data-anchor-id="theory-an-overview">Theory: An overview</h2>
<p>Let us consider a generic two-dimensional contingency table <img src="https://latex.codecogs.com/png.latex?%5Cbf%7BT%7D">, having <img src="https://latex.codecogs.com/png.latex?R"> rows (corresponding to the levels of the categorical variable <img src="https://latex.codecogs.com/png.latex?X">) and <img src="https://latex.codecogs.com/png.latex?C"> columns (corresponding to the levels of the categorical variable <img src="https://latex.codecogs.com/png.latex?Y">). The table is built based on a dataset of <img src="https://latex.codecogs.com/png.latex?n"> observations, which are distributed across the <img src="https://latex.codecogs.com/png.latex?R%5Ccdot%20C"> cells of <img src="https://latex.codecogs.com/png.latex?%5Cbf%7BT%7D">. Each cell shows the count <img src="https://latex.codecogs.com/png.latex?n_%7Bij%7D,%5C,i=1,%5Ccdots,R;%5C,j=1,%5Ccdots,C">.</p>
<section id="key-terms" class="level3">
<h3 class="anchored" data-anchor-id="key-terms">Key terms</h3>
<p><strong>Row marginals</strong></p>
<p>The row marginals (or row margins) can be computed by taking the row sums of the counts <img src="https://latex.codecogs.com/png.latex?n_%7Bij%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0An_%7Bi+%7D=%5Csum_%7Bj=1%7D%5EC%20n_%7Bij%7D,%5C;i=1,%5Cdots,R%0A"></p>
<p><strong>Column marginals</strong></p>
<p>The column marginals (or col margins) can be computed by taking the column sums of the counts <img src="https://latex.codecogs.com/png.latex?n_%7Bij%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0An_%7B+j%7D=%5Csum_%7Bi=1%7D%5ER%20n_%7Bij%7D%5C;j=1,%5Cdots,C%0A"></p>
<p><strong>Grand total</strong></p>
<p>The gran total is the complete number after everything has been added up:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0An_%7B++%7D=%5Csum_%7Bi=1%7D%5ER%20n_%7Bi+%7D=%5Csum_%7Bj=1%7D%5ECn_%7B+j%7D=%5Csum_%7Bi=1%7D%5ER%5Csum_%7Bj=1%7D%5ECn_%7Bij%7D=n%0A"></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row marginals</span></span>
<span id="cb4-2">row.sum <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>(housetasks, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, sum)</span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Column marginals</span></span>
<span id="cb4-4">col.sum <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>(housetasks, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, sum)</span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Gran total</span></span>
<span id="cb4-6">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;">sum</span>(housetasks)</span>
<span id="cb4-7">housetasks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-8">  <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;">total =</span> row.sum) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Total"</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>(col.sum, n)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb4-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rrrrr"</span>,</span>
<span id="cb4-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Wife"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alternating"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Husband"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jointly"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Total"</span>),</span>
<span id="cb4-13">    <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;">"House task contingency table with row, column margins and gran total"</span>,</span>
<span id="cb4-14">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>House task contingency table with row, column margins and gran total</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Wife</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Alternating</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Husband</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Jointly</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Total</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Laundry</td>
<td style="text-align: right;">156</td>
<td style="text-align: right;">14</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">176</td>
</tr>
<tr class="even">
<td style="text-align: left;">Main_meal</td>
<td style="text-align: right;">124</td>
<td style="text-align: right;">20</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">153</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Dinner</td>
<td style="text-align: right;">77</td>
<td style="text-align: right;">11</td>
<td style="text-align: right;">7</td>
<td style="text-align: right;">13</td>
<td style="text-align: right;">108</td>
</tr>
<tr class="even">
<td style="text-align: left;">Breakfeast</td>
<td style="text-align: right;">82</td>
<td style="text-align: right;">36</td>
<td style="text-align: right;">15</td>
<td style="text-align: right;">7</td>
<td style="text-align: right;">140</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Tidying</td>
<td style="text-align: right;">53</td>
<td style="text-align: right;">11</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">57</td>
<td style="text-align: right;">122</td>
</tr>
<tr class="even">
<td style="text-align: left;">Dishes</td>
<td style="text-align: right;">32</td>
<td style="text-align: right;">24</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">53</td>
<td style="text-align: right;">113</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Shopping</td>
<td style="text-align: right;">33</td>
<td style="text-align: right;">23</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">55</td>
<td style="text-align: right;">120</td>
</tr>
<tr class="even">
<td style="text-align: left;">Official</td>
<td style="text-align: right;">12</td>
<td style="text-align: right;">46</td>
<td style="text-align: right;">23</td>
<td style="text-align: right;">15</td>
<td style="text-align: right;">96</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Driving</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">51</td>
<td style="text-align: right;">75</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">139</td>
</tr>
<tr class="even">
<td style="text-align: left;">Finances</td>
<td style="text-align: right;">13</td>
<td style="text-align: right;">13</td>
<td style="text-align: right;">21</td>
<td style="text-align: right;">66</td>
<td style="text-align: right;">113</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Insurance</td>
<td style="text-align: right;">8</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">53</td>
<td style="text-align: right;">77</td>
<td style="text-align: right;">139</td>
</tr>
<tr class="even">
<td style="text-align: left;">Repairs</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">160</td>
<td style="text-align: right;">2</td>
<td style="text-align: right;">165</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holidays</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">6</td>
<td style="text-align: right;">153</td>
<td style="text-align: right;">160</td>
</tr>
<tr class="even">
<td style="text-align: left;">Total</td>
<td style="text-align: right;">600</td>
<td style="text-align: right;">254</td>
<td style="text-align: right;">381</td>
<td style="text-align: right;">509</td>
<td style="text-align: right;">1744</td>
</tr>
</tbody>
</table>


</div>
</div>
<p>The gran total is obtained by summing the values of the row margin or the values of the col margin (<img src="https://latex.codecogs.com/png.latex?n="> 1744).</p>
<p>To compare rows (or columns), their profiles can be analyzed, in the search for similar rows (or columns).</p>
<p><strong>Row profile</strong></p>
<p>The profile of a row (or row profile) is calculated by taking each row point and dividing by the corresponding value of the row margin:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cunderline%7Ba%7D_i,%5C;i=1,%5Cdots,R=%5C%7B%5Cunderline%7Ba%7D_%7Bi_j%7D,%5C;j=1,%5Cdots,C%5C%7D=%5C%7Bn_%7Bij%7D/n_%7Bi+%7D,%5C;j=1,%5Cdots,C%5C%7D%0A"></p>
<p><strong>Average row profile</strong></p>
<p>The average row profile can be computed from the col margin after dividing it by the grand total:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cunderline%7Br%7D=%5C%7Br_j=n_%7B+j%7D/n,%5C;j=1,%5Cdots,C%5C%7D%0A"></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row profile</span></span>
<span id="cb5-2">row.profile <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> housetasks<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>row.sum</span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Average row profile</span></span>
<span id="cb5-4">average.row.profile <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> col.sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>n</span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row margins</span></span>
<span id="cb5-6">row.profile.sum <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>(row.profile, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, sum)</span>
<span id="cb5-7"></span>
<span id="cb5-8">row.profile <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-9">  <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;">TOTAL =</span> row.profile.sum) <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;">rbind</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;">Average row profile</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>(average.row.profile, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(average.row.profile))) <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;">kable</span>(</span>
<span id="cb5-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rrrrr"</span>,</span>
<span id="cb5-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Wife"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alternating"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Husband"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jointly"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Total"</span>),</span>
<span id="cb5-14">    <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>
<span id="cb5-15">    <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;">"House tasks data frame with row profiles and average row profile"</span>,</span>
<span id="cb5-16">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>House tasks data frame with row profiles and average row profile</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Wife</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Alternating</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Husband</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Jointly</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Total</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Laundry</td>
<td style="text-align: right;">0.8864</td>
<td style="text-align: right;">0.0795</td>
<td style="text-align: right;">0.0114</td>
<td style="text-align: right;">0.0227</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: left;">Main_meal</td>
<td style="text-align: right;">0.8105</td>
<td style="text-align: right;">0.1307</td>
<td style="text-align: right;">0.0327</td>
<td style="text-align: right;">0.0261</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Dinner</td>
<td style="text-align: right;">0.7130</td>
<td style="text-align: right;">0.1019</td>
<td style="text-align: right;">0.0648</td>
<td style="text-align: right;">0.1204</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: left;">Breakfeast</td>
<td style="text-align: right;">0.5857</td>
<td style="text-align: right;">0.2571</td>
<td style="text-align: right;">0.1071</td>
<td style="text-align: right;">0.0500</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Tidying</td>
<td style="text-align: right;">0.4344</td>
<td style="text-align: right;">0.0902</td>
<td style="text-align: right;">0.0082</td>
<td style="text-align: right;">0.4672</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: left;">Dishes</td>
<td style="text-align: right;">0.2832</td>
<td style="text-align: right;">0.2124</td>
<td style="text-align: right;">0.0354</td>
<td style="text-align: right;">0.4690</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Shopping</td>
<td style="text-align: right;">0.2750</td>
<td style="text-align: right;">0.1917</td>
<td style="text-align: right;">0.0750</td>
<td style="text-align: right;">0.4583</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: left;">Official</td>
<td style="text-align: right;">0.1250</td>
<td style="text-align: right;">0.4792</td>
<td style="text-align: right;">0.2396</td>
<td style="text-align: right;">0.1562</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Driving</td>
<td style="text-align: right;">0.0719</td>
<td style="text-align: right;">0.3669</td>
<td style="text-align: right;">0.5396</td>
<td style="text-align: right;">0.0216</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: left;">Finances</td>
<td style="text-align: right;">0.1150</td>
<td style="text-align: right;">0.1150</td>
<td style="text-align: right;">0.1858</td>
<td style="text-align: right;">0.5841</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Insurance</td>
<td style="text-align: right;">0.0576</td>
<td style="text-align: right;">0.0072</td>
<td style="text-align: right;">0.3813</td>
<td style="text-align: right;">0.5540</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: left;">Repairs</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0182</td>
<td style="text-align: right;">0.9697</td>
<td style="text-align: right;">0.0121</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holidays</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0063</td>
<td style="text-align: right;">0.0375</td>
<td style="text-align: right;">0.9562</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: left;">Average row profile</td>
<td style="text-align: right;">0.3440</td>
<td style="text-align: right;">0.1456</td>
<td style="text-align: right;">0.2185</td>
<td style="text-align: right;">0.2919</td>
<td style="text-align: right;">1</td>
</tr>
</tbody>
</table>


</div>
</div>
<p><strong>Column profile</strong></p>
<p>The profile of a column (or col profile) is calculated by taking each column point and dividing by the corresponding value of the col margin:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cunderline%7Bb%7D_j,%5C;j=1,%5Cdots,C=%5C%7B%5Cunderline%7Bb%7D_%7Bj_i%7D,%5C;i=1,%5Cdots,R%5C%7D=%5C%7Bn_%7Bij%7D/n_%7B+j%7D,%5C;i=1,%5Cdots,R%5C%7D%0A"></p>
<p><strong>Average column profile</strong></p>
<p>The average column profile (or average col profile) can be computed from the row marginal after dividing it by the grand total:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cunderline%7Bc%7D=%5C%7Bc_i=n_%7Bi+%7D/n,%5C;i=1,%5Cdots,R%5C%7D%0A"></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Column profile</span></span>
<span id="cb6-2">col.profile <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;">t</span>(housetasks)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>col.sum</span>
<span id="cb6-3">col.profile <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.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(col.profile))</span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Average column profile</span></span>
<span id="cb6-5">average.col.profile <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> row.sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>n</span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Column margins</span></span>
<span id="cb6-7">col.profile.sum <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>(col.profile, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, sum)</span>
<span id="cb6-8"></span>
<span id="cb6-9">col.profile <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-10">  <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;">Average col profile</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> average.col.profile) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Total =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(col.profile.sum, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(average.col.profile))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb6-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rrrrr"</span>,</span>
<span id="cb6-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Wife"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alternating"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Husband"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jointly"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Average col profile"</span>),</span>
<span id="cb6-15">    <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>
<span id="cb6-16">    <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;">"House tasks data frame with col profiles and average col profile"</span>,</span>
<span id="cb6-17">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>House tasks data frame with col profiles and average col profile</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Wife</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Alternating</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Husband</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Jointly</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Average col profile</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Laundry</td>
<td style="text-align: right;">0.2600</td>
<td style="text-align: right;">0.0551</td>
<td style="text-align: right;">0.0052</td>
<td style="text-align: right;">0.0079</td>
<td style="text-align: right;">0.1009</td>
</tr>
<tr class="even">
<td style="text-align: left;">Main_meal</td>
<td style="text-align: right;">0.2067</td>
<td style="text-align: right;">0.0787</td>
<td style="text-align: right;">0.0131</td>
<td style="text-align: right;">0.0079</td>
<td style="text-align: right;">0.0877</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Dinner</td>
<td style="text-align: right;">0.1283</td>
<td style="text-align: right;">0.0433</td>
<td style="text-align: right;">0.0184</td>
<td style="text-align: right;">0.0255</td>
<td style="text-align: right;">0.0619</td>
</tr>
<tr class="even">
<td style="text-align: left;">Breakfeast</td>
<td style="text-align: right;">0.1367</td>
<td style="text-align: right;">0.1417</td>
<td style="text-align: right;">0.0394</td>
<td style="text-align: right;">0.0138</td>
<td style="text-align: right;">0.0803</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Tidying</td>
<td style="text-align: right;">0.0883</td>
<td style="text-align: right;">0.0433</td>
<td style="text-align: right;">0.0026</td>
<td style="text-align: right;">0.1120</td>
<td style="text-align: right;">0.0700</td>
</tr>
<tr class="even">
<td style="text-align: left;">Dishes</td>
<td style="text-align: right;">0.0533</td>
<td style="text-align: right;">0.0945</td>
<td style="text-align: right;">0.0105</td>
<td style="text-align: right;">0.1041</td>
<td style="text-align: right;">0.0648</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Shopping</td>
<td style="text-align: right;">0.0550</td>
<td style="text-align: right;">0.0906</td>
<td style="text-align: right;">0.0236</td>
<td style="text-align: right;">0.1081</td>
<td style="text-align: right;">0.0688</td>
</tr>
<tr class="even">
<td style="text-align: left;">Official</td>
<td style="text-align: right;">0.0200</td>
<td style="text-align: right;">0.1811</td>
<td style="text-align: right;">0.0604</td>
<td style="text-align: right;">0.0295</td>
<td style="text-align: right;">0.0550</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Driving</td>
<td style="text-align: right;">0.0167</td>
<td style="text-align: right;">0.2008</td>
<td style="text-align: right;">0.1969</td>
<td style="text-align: right;">0.0059</td>
<td style="text-align: right;">0.0797</td>
</tr>
<tr class="even">
<td style="text-align: left;">Finances</td>
<td style="text-align: right;">0.0217</td>
<td style="text-align: right;">0.0512</td>
<td style="text-align: right;">0.0551</td>
<td style="text-align: right;">0.1297</td>
<td style="text-align: right;">0.0648</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Insurance</td>
<td style="text-align: right;">0.0133</td>
<td style="text-align: right;">0.0039</td>
<td style="text-align: right;">0.1391</td>
<td style="text-align: right;">0.1513</td>
<td style="text-align: right;">0.0797</td>
</tr>
<tr class="even">
<td style="text-align: left;">Repairs</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0118</td>
<td style="text-align: right;">0.4199</td>
<td style="text-align: right;">0.0039</td>
<td style="text-align: right;">0.0946</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holidays</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0039</td>
<td style="text-align: right;">0.0157</td>
<td style="text-align: right;">0.3006</td>
<td style="text-align: right;">0.0917</td>
</tr>
<tr class="even">
<td style="text-align: left;">Total</td>
<td style="text-align: right;">1.0000</td>
<td style="text-align: right;">1.0000</td>
<td style="text-align: right;">1.0000</td>
<td style="text-align: right;">1.0000</td>
<td style="text-align: right;">1.0000</td>
</tr>
</tbody>
</table>


</div>
</div>
<p><strong>Distance (or similarity) between row profiles</strong></p>
<p>This metrics is useful to compare how similar to each other are two rows of the contingency table:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ad%5E2_%7Bkl%7D,%5C;k,l=1,%5Cdots,R=%5CVert%20%5Cunderline%7Ba%7D_k-%5Cunderline%7Ba%7D_%7Bl%7D%5CVert%5E2_%5Cunderline%7Br%7D=%5Clarge%5Csum_%7Bj=1%7D%5EC%5Cdfrac%7B(%5Cunderline%7Ba%7D_%7Bk_j%7D-%5Cunderline%7Ba%7D_%7Bl_j%7D)%5E2%7D%7Br_j%7D%0A"></p>
<p>Recall that we need to normalize each component within the sum to the corresponding element of the average row profile.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Dinner" and "Driving" profiles</span></span>
<span id="cb7-2">dinner.p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> row.profile[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dinner"</span>, ]</span>
<span id="cb7-3">driving.p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> row.profile[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Driving"</span>, ]</span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Distance between "Dinner" and "Driving"</span></span>
<span id="cb7-5">d2_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;">sum</span>(((dinner.p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> driving.p)<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>average.row.profile)</span>
<span id="cb7-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Breakfeast" profile</span></span>
<span id="cb7-7">breakfast.p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> row.profile[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Breakfeast"</span>, ]</span>
<span id="cb7-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Distance between "Dinner" and "Breakfeast"</span></span>
<span id="cb7-9">d2_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;">sum</span>(((dinner.p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> breakfast.p)<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>average.row.profile)</span></code></pre></div>
</details>
</div>
<p>Example - The similarity between “Dinner” and “Driving” profiles is 2.742, whereas the similarity between “Dinner” and “Breakfeast” is 0.238.</p>
<p><strong>Distance (or similarity) between col profiles</strong></p>
<p>This metrics is useful to compare how similar to each other are two columns of the contingency table:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ad%5E2_%7Bkl%7D,%5C;k,l=1,%5Cdots,C=%5CVert%20%5Cunderline%7Bb%7D_k-%5Cunderline%7Bb%7D_l%5CVert%5E2_%5Cunderline%7Bc%7D=%5Clarge%5Csum_%7Bi=1%7D%5ER%5Cdfrac%7B(%5Cunderline%7Bb%7D_%7Bk_i%7D-%5Cunderline%7Bb%7D_%7Bl_i%7D)%5E2%7D%7Bc_i%7D%0A"></p>
<p>Recall that we need to normalize each component within the sum to the corresponding element of the average col profile.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Wife" and "Husband" profiles</span></span>
<span id="cb8-2">wife.p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> col.profile[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wife"</span>]</span>
<span id="cb8-3">husband.p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> col.profile[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Husband"</span>]</span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Distance between "Wife" and "Husband"</span></span>
<span id="cb8-5">d2_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;">sum</span>(((wife.p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> husband.p)<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> average.col.profile)</span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Jointly" profile</span></span>
<span id="cb8-7">jointly.p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> col.profile[, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jointly"</span>]</span>
<span id="cb8-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Distance between "Wife" and "Jointly"</span></span>
<span id="cb8-9">d2_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;">sum</span>(((wife.p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> jointly.p)<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> average.col.profile)</span></code></pre></div>
</details>
</div>
<p>Example - The similarity between “Wife” and “Husband” profiles is 4.05, whereas the similarity between “Wife” and “Jointly” is 2.935.</p>
<p><strong>Distance (or similarity) between each row profile and the average row profile</strong></p>
<p>This metrics is useful to compare how similar to the average row profile is any row profile.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ad%5E2_%7Bk%7D,%5C;k=1,%5Cdots,R=%5CVert%5Cunderline%7Ba%7D_k-%5Cunderline%7Br%7D%5CVert%5E2_%5Cunderline%7Br%7D=%5Clarge%5Csum_%7Bj=1%7D%5EC%5Cdfrac%7B(%5Cunderline%7Ba%7D_%7Bk_j%7D-r_%7Bj%7D)%5E2%7D%7Br_j%7D%0A"></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">d2.row <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>(row.profile, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb9-2">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(row.p, av.p){<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(((row.p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> av.p)<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>av.p)}, </span>
<span id="cb9-3">        average.row.profile)</span>
<span id="cb9-4"></span>
<span id="cb9-5"><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;">round</span>(d2.row,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb9-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rc"</span>,</span>
<span id="cb9-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Task"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Similarity"</span>),</span>
<span id="cb9-9">    <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;">3</span>,</span>
<span id="cb9-10">    <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;">"Similarity between each row profile and the average row profile"</span>,</span>
<span id="cb9-11">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Similarity between each row profile and the average row profile</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">Task</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Similarity</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Laundry</td>
<td style="text-align: right;">1.329</td>
</tr>
<tr class="even">
<td style="text-align: left;">Main_meal</td>
<td style="text-align: right;">1.034</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Dinner</td>
<td style="text-align: right;">0.618</td>
</tr>
<tr class="even">
<td style="text-align: left;">Breakfeast</td>
<td style="text-align: right;">0.512</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Tidying</td>
<td style="text-align: right;">0.353</td>
</tr>
<tr class="even">
<td style="text-align: left;">Dishes</td>
<td style="text-align: right;">0.302</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Shopping</td>
<td style="text-align: right;">0.218</td>
</tr>
<tr class="even">
<td style="text-align: left;">Official</td>
<td style="text-align: right;">0.968</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Driving</td>
<td style="text-align: right;">1.274</td>
</tr>
<tr class="even">
<td style="text-align: left;">Finances</td>
<td style="text-align: right;">0.456</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Insurance</td>
<td style="text-align: right;">0.727</td>
</tr>
<tr class="even">
<td style="text-align: left;">Repairs</td>
<td style="text-align: right;">3.307</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holidays</td>
<td style="text-align: right;">2.140</td>
</tr>
</tbody>
</table>


</div>
</div>
<p><strong>Distance (or similarity) between each col profile and the average col profile</strong></p>
<p>This metrics is useful to compare how similar to the average col profile is any col profile.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ad%5E2_%7Bl%7D,%5C;l=1,%5Cdots,C=%5CVert%5Cunderline%7Bb%7D_l-%5Cunderline%7Bc%7D%5CVert%5E2_%5Cunderline%7Bc%7D=%5Clarge%5Csum_%7Bi=1%7D%5ER%5Cdfrac%7B(%5Cunderline%7Bb%7D_%7Bl_i%7D-c_i)%5E2%7D%7Bc_i%7D%0A"></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">d2.col <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>(col.profile, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, </span>
<span id="cb10-2">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(col.p, av.p){<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(((col.p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> av.p)<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>av.p)}, </span>
<span id="cb10-3">        average.col.profile)</span>
<span id="cb10-4"></span>
<span id="cb10-5"><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;">round</span>(d2.col,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb10-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rc"</span>,</span>
<span id="cb10-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Couple"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Similarity"</span>),</span>
<span id="cb10-9">    <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;">3</span>,</span>
<span id="cb10-10">    <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;">"Similarity between each col profile and the average col profile"</span>,</span>
<span id="cb10-11">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Similarity between each col profile and the average col profile</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">Couple</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Similarity</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Wife</td>
<td style="text-align: right;">0.875</td>
</tr>
<tr class="even">
<td style="text-align: left;">Alternating</td>
<td style="text-align: right;">0.809</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Husband</td>
<td style="text-align: right;">1.746</td>
</tr>
<tr class="even">
<td style="text-align: left;">Jointly</td>
<td style="text-align: right;">1.078</td>
</tr>
</tbody>
</table>


</div>
</div>
<p><strong>Distance matrix</strong></p>
<p>The similarity can be computed between each row (col) profile and the other row (col) profiles in the contingency table, using, for example, the function <code>dist-matrix()</code> <a href="https://www.sthda.com/english/articles/31-principal-component-methods-in-r-practical-guide/120-correspondence-analysis-theory-and-practice/#ca---singular-value-decomposition-of-the-standardized-residuals">(here)</a>.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data: a data frame or matrix </span></span>
<span id="cb11-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># average.profile: average profile</span></span>
<span id="cb11-3">dist.matrix <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, average.profile) {</span>
<span id="cb11-4">   mat <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(data))</span>
<span id="cb11-5">   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;">ncol</span>(mat)</span>
<span id="cb11-6">   dist.mat <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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, n, n)</span>
<span id="cb11-7">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(dist.mat) <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;">0</span></span>
<span id="cb11-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="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="cb11-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> (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>n) {</span>
<span id="cb11-10">            d2 <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>(((mat[, i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> mat[, 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;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> average.profile)</span>
<span id="cb11-11">            dist.mat[i, j] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dist.mat[j, i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d2</span>
<span id="cb11-12">        }</span>
<span id="cb11-13">    }</span>
<span id="cb11-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(dist.mat) <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;">rownames</span>(dist.mat) <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;">colnames</span>(mat)</span>
<span id="cb11-15">  dist.mat</span>
<span id="cb11-16">}</span></code></pre></div>
</details>
</div>
<p>The result is a distance matrix (a kind of correlation or dissimilarity matrix), either orientated per rows or columns. The distance between row (col) profiles can be computed using the package corrplot for visualization.</p>
<p><strong>Row distance matrix</strong></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Distance matrix (row)</span></span>
<span id="cb12-2">dist.mat <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;">dist.matrix</span>(row.profile, average.row.profile)</span>
<span id="cb12-3">dist.mat <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;">round</span>(dist.mat, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb12-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualize the matrix (row)</span></span>
<span id="cb12-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corrplot</span>(dist.mat, <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;">"upper"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">is.corr =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorRampPalette</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;">"white"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>))(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_1/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Col distance matrix</strong></p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Distance matrix (column)</span></span>
<span id="cb13-2">dist.mat <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;">dist.matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(col.profile), average.col.profile)</span>
<span id="cb13-3">dist.mat <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;">round</span>(dist.mat, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualize the matrix (column)</span></span>
<span id="cb13-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corrplot</span>(dist.mat, <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;">"upper"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">is.corr =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colorRampPalette</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;">"white"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>))(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">128</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://amsabatini.netlify.app/posts/correspondence_analysis_1/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><strong>Row mass and inertia</strong></p>
<p>The <strong>row mass</strong> can be defined as the total frequency of each row, obtained by dividing its row sum by the gran total. The <strong>row inertia</strong> is calculated as the row mass multiplied by the squared distance between the row and the average row profile. The inertia is a measure of the information contained in each row.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row mass</span></span>
<span id="cb14-2">row.sum <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>(housetasks, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, sum)</span>
<span id="cb14-3">grand.total <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>(housetasks)</span>
<span id="cb14-4">row.mass <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> row.sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>grand.total</span>
<span id="cb14-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row inertia</span></span>
<span id="cb14-6">row.inertia <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> row.mass<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>d2.row</span>
<span id="cb14-7"></span>
<span id="cb14-8">row.inertia <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb14-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rc"</span>,</span>
<span id="cb14-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Task"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Inertia"</span>),</span>
<span id="cb14-12">    <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;">3</span>,</span>
<span id="cb14-13">    <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;">"Row inertia of the house tasks contingency table"</span>,</span>
<span id="cb14-14">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Row inertia of the house tasks contingency table</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">Task</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Inertia</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Laundry</td>
<td style="text-align: right;">0.134</td>
</tr>
<tr class="even">
<td style="text-align: left;">Main_meal</td>
<td style="text-align: right;">0.091</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Dinner</td>
<td style="text-align: right;">0.038</td>
</tr>
<tr class="even">
<td style="text-align: left;">Breakfeast</td>
<td style="text-align: right;">0.041</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Tidying</td>
<td style="text-align: right;">0.025</td>
</tr>
<tr class="even">
<td style="text-align: left;">Dishes</td>
<td style="text-align: right;">0.020</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Shopping</td>
<td style="text-align: right;">0.015</td>
</tr>
<tr class="even">
<td style="text-align: left;">Official</td>
<td style="text-align: right;">0.053</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Driving</td>
<td style="text-align: right;">0.102</td>
</tr>
<tr class="even">
<td style="text-align: left;">Finances</td>
<td style="text-align: right;">0.030</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Insurance</td>
<td style="text-align: right;">0.058</td>
</tr>
<tr class="even">
<td style="text-align: left;">Repairs</td>
<td style="text-align: right;">0.313</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holidays</td>
<td style="text-align: right;">0.196</td>
</tr>
</tbody>
</table>


</div>
</div>
<p><strong>Column mass and inertia</strong></p>
<p>The <strong>column mass</strong> can be defined as the total frequency of each column, obtained by dividing its col sum by the gran total. The <strong>column inertia</strong> is calculated as the column mass multiplied by the squared distance between the column and the average col profile. The inertia is a measure of the information contained in each column.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Column mass</span></span>
<span id="cb15-2">col.sum <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>(housetasks, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, sum)</span>
<span id="cb15-3">grand.total <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>(housetasks)</span>
<span id="cb15-4">col.mass <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> col.sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>grand.total</span>
<span id="cb15-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Column inertia</span></span>
<span id="cb15-6">col.inertia <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> col.mass<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>d2.col</span>
<span id="cb15-7"></span>
<span id="cb15-8">col.inertia <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb15-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rc"</span>,</span>
<span id="cb15-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Couple"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Inertia"</span>),</span>
<span id="cb15-12">    <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;">3</span>,</span>
<span id="cb15-13">    <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;">"Column inertia of the house tasks contingency table"</span>,</span>
<span id="cb15-14">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Column inertia of the house tasks contingency table</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">Couple</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Inertia</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Wife</td>
<td style="text-align: right;">0.301</td>
</tr>
<tr class="even">
<td style="text-align: left;">Alternating</td>
<td style="text-align: right;">0.118</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Husband</td>
<td style="text-align: right;">0.381</td>
</tr>
<tr class="even">
<td style="text-align: left;">Jointly</td>
<td style="text-align: right;">0.315</td>
</tr>
</tbody>
</table>


</div>
</div>
<p><strong>Total inertia</strong></p>
<p>The total inertia is the total information contained in the contingency table.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Total inertia</span></span>
<span id="cb16-2">inertia_total_row <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>(row.inertia)</span>
<span id="cb16-3">inertia_total_col <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>(col.inertia)</span></code></pre></div>
</details>
</div>
<p>The total inertia is computed as the sum of row inertias (or equivalently, as the sum of column inertias), in the present case we get the value 1.115.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row mass</span></span>
<span id="cb17-2">row.sum <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>(housetasks, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, sum)</span>
<span id="cb17-3">grand.total <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>(housetasks)</span>
<span id="cb17-4">row.mass <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> row.sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>grand.total</span>
<span id="cb17-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row inertia</span></span>
<span id="cb17-6">row.inertia <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> row.mass<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>d2.row</span>
<span id="cb17-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Total inertia</span></span>
<span id="cb17-8">inertia_total_row <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>(row.inertia)</span>
<span id="cb17-9"></span>
<span id="cb17-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Column mass</span></span>
<span id="cb17-11">col.sum <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>(housetasks, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, sum)</span>
<span id="cb17-12">col.mass <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> col.sum<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>grand.total</span>
<span id="cb17-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Column inertia</span></span>
<span id="cb17-14">col.inertia <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> col.mass<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>d2.col</span>
<span id="cb17-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Total inertia</span></span>
<span id="cb17-16">inertia_total_col <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>(col.inertia)</span></code></pre></div>
</details>
</div>
</section>
</section>
<section id="results" class="level2">
<h2 class="anchored" data-anchor-id="results">Results</h2>
<p>The result for rows can be summarized in terms of the squared distance between row profiles and the average row profile (i.e., <code>d2.row</code>), the row mass (i.e., <code>row mass</code>), and the row inertia (i.e., <code>row.inertia</code>). Similarly, the results for columns can be summarized using <code>d2.col</code>, <code>col.mass</code> and <code>col.inertia</code>.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">row <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.data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d2 =</span> d2.row, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mass =</span> row.mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inertia =</span> row.inertia)</span>
<span id="cb18-2">col <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.data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d2 =</span> d2.col, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mass =</span> col.mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inertia =</span> col.inertia)</span>
<span id="cb18-3"></span>
<span id="cb18-4">row <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb18-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rrrr"</span>,</span>
<span id="cb18-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Task"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Squared distance"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mass"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Inertia"</span>),</span>
<span id="cb18-8">    <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;">3</span>,</span>
<span id="cb18-9">    <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;">"Summary statistics for the rows of the contingency table"</span>,</span>
<span id="cb18-10">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Summary statistics for the rows of the contingency table</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">Task</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Squared distance</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Mass</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Inertia</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Laundry</td>
<td style="text-align: right;">1.329</td>
<td style="text-align: right;">0.101</td>
<td style="text-align: right;">0.134</td>
</tr>
<tr class="even">
<td style="text-align: left;">Main_meal</td>
<td style="text-align: right;">1.034</td>
<td style="text-align: right;">0.088</td>
<td style="text-align: right;">0.091</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Dinner</td>
<td style="text-align: right;">0.618</td>
<td style="text-align: right;">0.062</td>
<td style="text-align: right;">0.038</td>
</tr>
<tr class="even">
<td style="text-align: left;">Breakfeast</td>
<td style="text-align: right;">0.512</td>
<td style="text-align: right;">0.080</td>
<td style="text-align: right;">0.041</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Tidying</td>
<td style="text-align: right;">0.353</td>
<td style="text-align: right;">0.070</td>
<td style="text-align: right;">0.025</td>
</tr>
<tr class="even">
<td style="text-align: left;">Dishes</td>
<td style="text-align: right;">0.302</td>
<td style="text-align: right;">0.065</td>
<td style="text-align: right;">0.020</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Shopping</td>
<td style="text-align: right;">0.218</td>
<td style="text-align: right;">0.069</td>
<td style="text-align: right;">0.015</td>
</tr>
<tr class="even">
<td style="text-align: left;">Official</td>
<td style="text-align: right;">0.968</td>
<td style="text-align: right;">0.055</td>
<td style="text-align: right;">0.053</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Driving</td>
<td style="text-align: right;">1.274</td>
<td style="text-align: right;">0.080</td>
<td style="text-align: right;">0.102</td>
</tr>
<tr class="even">
<td style="text-align: left;">Finances</td>
<td style="text-align: right;">0.456</td>
<td style="text-align: right;">0.065</td>
<td style="text-align: right;">0.030</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Insurance</td>
<td style="text-align: right;">0.727</td>
<td style="text-align: right;">0.080</td>
<td style="text-align: right;">0.058</td>
</tr>
<tr class="even">
<td style="text-align: left;">Repairs</td>
<td style="text-align: right;">3.307</td>
<td style="text-align: right;">0.095</td>
<td style="text-align: right;">0.313</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holidays</td>
<td style="text-align: right;">2.140</td>
<td style="text-align: right;">0.092</td>
<td style="text-align: right;">0.196</td>
</tr>
</tbody>
</table>


</div>
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">col <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb19-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rrrr"</span>,</span>
<span id="cb19-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"couple"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"squared distance"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mass"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"inertia"</span>),</span>
<span id="cb19-5">    <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;">3</span>,</span>
<span id="cb19-6">    <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;">"Summary statistics for the columns of the contingency table"</span>,</span>
<span id="cb19-7">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Summary statistics for the columns of the contingency table</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">couple</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">squared distance</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">mass</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">inertia</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Wife</td>
<td style="text-align: right;">0.875</td>
<td style="text-align: right;">0.344</td>
<td style="text-align: right;">0.301</td>
</tr>
<tr class="even">
<td style="text-align: left;">Alternating</td>
<td style="text-align: right;">0.809</td>
<td style="text-align: right;">0.146</td>
<td style="text-align: right;">0.118</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Husband</td>
<td style="text-align: right;">1.746</td>
<td style="text-align: right;">0.218</td>
<td style="text-align: right;">0.381</td>
</tr>
<tr class="even">
<td style="text-align: left;">Jointly</td>
<td style="text-align: right;">1.078</td>
<td style="text-align: right;">0.292</td>
<td style="text-align: right;">0.315</td>
</tr>
</tbody>
</table>


</div>
</div>
<section id="chi-square-test" class="level3">
<h3 class="anchored" data-anchor-id="chi-square-test">Chi-square test</h3>
<p>The <strong>chi-square statistic</strong> (<img src="https://latex.codecogs.com/png.latex?%5Cchi%5E2">) is an overall measure of the difference between the frequencies observed in a contingency table and the expected frequencies calculated under the assumption of homogeneity of row profiles (or column profiles). Geometrically, the inertia measures how far away row profiles (or column profiles) are from their average profiles. The average profile can be thought of as the representative of the hypothesis of homogeneity (i.e., equality) of profiles. The distances between profiles are measured using the distance of the chi-square (distance <img src="https://latex.codecogs.com/png.latex?%5Cchi%5E2">). This distance is similar in its formulation to the Euclidean distance between points in a physical space, except that any quadratic difference between coordinates is divided by the corresponding element of the mean profile.</p>
<p>It can be shown that the total inertia <img src="https://latex.codecogs.com/png.latex?%5CPhi"> of a contingency table is the statistic (<img src="https://latex.codecogs.com/png.latex?%5Cchi%5E2">) divided by the grand total <img src="https://latex.codecogs.com/png.latex?n">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CPhi=%5Cdfrac%7B%5Cchi%5E2%7D%7Bn%7D=%5Csum_%7Bi%7D%5ERc_i%5CVert%5Cunderline%7Ba%7D_i-%5Cunderline%7Br%7D%5CVert%5E2_%7B%5Cunderline%7Br%7D%7D=%5Csum_%7Bj%7D%5ECr_j%5CVert%5Cunderline%7Bb%7D_j-%5Cunderline%7Bc%7D%5CVert%5E2_%7B%5Cunderline%7Bc%7D%7D%0A"></p>
<p>The <strong>Chi-squared test</strong> can be used to examine whether rows and columns of a contingency table are statistically significantly associated:</p>
<ul>
<li>Null hypothesis (H0): the row and the column variables of the contingency table are independent;</li>
<li>Alternative hypothesis (H1): row and column variables are dependent</li>
</ul>
<p>For each cell of the table, we have to calculate the expected value under H0. For a given cell <img src="https://latex.codecogs.com/png.latex?(i,j)">, the expected value is calculated by taking the product of the <img src="https://latex.codecogs.com/png.latex?i">-th element of the row margin (sum of the elements in the <img src="https://latex.codecogs.com/png.latex?i">-th row of the contingency table) and the <img src="https://latex.codecogs.com/png.latex?j">-th element of the col margin (sum of the elements in the <img src="https://latex.codecogs.com/png.latex?j">-th column of the contingency table), divided by the gran total.</p>
<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">
Statistical testing of homogeneity/independence
</div>
</div>
<div class="callout-body-container callout-body">
<p>Suppose that the population is divided into <img src="https://latex.codecogs.com/png.latex?R"> groups and each group (or the entire population) is divided into <img src="https://latex.codecogs.com/png.latex?C"> categories. We would like to test whether the distribution of categories in each group is the same (<strong>homogeneity test</strong>). If observations <img src="https://latex.codecogs.com/png.latex?X_1,%5Cdots,X_n"> are sampled independently from the entire population then homogeneity over groups is the same as independence of groups and categories. If we have homogeneity:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPr%7D(%5Ctext%7BCategory%7D_j%5Cvert%5Ctext%7BGroup%7D_i)=%5Ctext%7BPr%7D(%5Ctext%7BCategory%7D_j)%0A"></p>
<p>then we have independence:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7BPr%7D(%5Ctext%7BCategory%7D_j,%5Ctext%7BGroup%7D_i)=%5Ctext%7BPr%7D(%5Ctext%7BCategory%7D_j%5Cvert%5Ctext%7BGroup%7D_i)%5Ctext%7BPr%7D(%5Ctext%7BGroup%7D_i)=%5Ctext%7BPr%7D(%5Ctext%7BCategory%7D_j)%5Ctext%7BPr%7D(%5Ctext%7BGroup%7D_i)%0A"></p>
<p>It is also possible to move the other way around (i.e., start from independence to obtain homogeneity). In other words, to test homogeneity, we can use the test of independence. The computation of the expected cell counts under the null hypothesis H0 (the row and the column variables of the contingency table are independent) is done according to the scheme of Figure&nbsp;1.</p>
<div id="fig-fig1" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/correspondence_analysis_1/fig1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="400">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: The expected counts are involved in testing either homegeneity or independence
</figcaption>
</figure>
</div>
</div>
</div>
<p>For example, in the case of the house tasks contingency table, the scheme of computation for the cell “Shopping”-“Alternating” is shown in Figure&nbsp;2.</p>
<div id="fig-fig2" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/correspondence_analysis_1/fig2.png" class="img-fluid quarto-figure quarto-figure-center figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: How to calculate the expected counts
</figcaption>
</figure>
</div>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Table of expected counts</span></span>
<span id="cb20-2">E <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> (row.sum <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;">t</span>(col.sum))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>n</span>
<span id="cb20-3"></span>
<span id="cb20-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row.names</span>(E) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row.names</span>(housetasks)</span>
<span id="cb20-5">E <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb20-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb20-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rrrr"</span>,</span>
<span id="cb20-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Wife"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alternating"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Husband"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jointly"</span>),</span>
<span id="cb20-9">    <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;">2</span>,</span>
<span id="cb20-10">    <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;">"Table of expected counts"</span>,</span>
<span id="cb20-11">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Table of expected counts</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Wife</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Alternating</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Husband</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Jointly</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Laundry</td>
<td style="text-align: right;">60.55</td>
<td style="text-align: right;">25.63</td>
<td style="text-align: right;">38.45</td>
<td style="text-align: right;">51.37</td>
</tr>
<tr class="even">
<td style="text-align: left;">Main_meal</td>
<td style="text-align: right;">52.64</td>
<td style="text-align: right;">22.28</td>
<td style="text-align: right;">33.42</td>
<td style="text-align: right;">44.65</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Dinner</td>
<td style="text-align: right;">37.16</td>
<td style="text-align: right;">15.73</td>
<td style="text-align: right;">23.59</td>
<td style="text-align: right;">31.52</td>
</tr>
<tr class="even">
<td style="text-align: left;">Breakfeast</td>
<td style="text-align: right;">48.17</td>
<td style="text-align: right;">20.39</td>
<td style="text-align: right;">30.58</td>
<td style="text-align: right;">40.86</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Tidying</td>
<td style="text-align: right;">41.97</td>
<td style="text-align: right;">17.77</td>
<td style="text-align: right;">26.65</td>
<td style="text-align: right;">35.61</td>
</tr>
<tr class="even">
<td style="text-align: left;">Dishes</td>
<td style="text-align: right;">38.88</td>
<td style="text-align: right;">16.46</td>
<td style="text-align: right;">24.69</td>
<td style="text-align: right;">32.98</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Shopping</td>
<td style="text-align: right;">41.28</td>
<td style="text-align: right;">17.48</td>
<td style="text-align: right;">26.22</td>
<td style="text-align: right;">35.02</td>
</tr>
<tr class="even">
<td style="text-align: left;">Official</td>
<td style="text-align: right;">33.03</td>
<td style="text-align: right;">13.98</td>
<td style="text-align: right;">20.97</td>
<td style="text-align: right;">28.02</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Driving</td>
<td style="text-align: right;">47.82</td>
<td style="text-align: right;">20.24</td>
<td style="text-align: right;">30.37</td>
<td style="text-align: right;">40.57</td>
</tr>
<tr class="even">
<td style="text-align: left;">Finances</td>
<td style="text-align: right;">38.88</td>
<td style="text-align: right;">16.46</td>
<td style="text-align: right;">24.69</td>
<td style="text-align: right;">32.98</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Insurance</td>
<td style="text-align: right;">47.82</td>
<td style="text-align: right;">20.24</td>
<td style="text-align: right;">30.37</td>
<td style="text-align: right;">40.57</td>
</tr>
<tr class="even">
<td style="text-align: left;">Repairs</td>
<td style="text-align: right;">56.77</td>
<td style="text-align: right;">24.03</td>
<td style="text-align: right;">36.05</td>
<td style="text-align: right;">48.16</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holidays</td>
<td style="text-align: right;">55.05</td>
<td style="text-align: right;">23.30</td>
<td style="text-align: right;">34.95</td>
<td style="text-align: right;">46.70</td>
</tr>
</tbody>
</table>


</div>
</div>
<p>The calculated Chi-square statistic <img src="https://latex.codecogs.com/png.latex?%5Cchi%5E2"> is compared to the critical value (obtained from statistical tables) with <img src="https://latex.codecogs.com/png.latex?df=(R-1)(C-1)"> degrees of freedom and <img src="https://latex.codecogs.com/png.latex?p=0.05">. Recall that <img src="https://latex.codecogs.com/png.latex?R,%20C"> are the number of rows and columns in the contingency table, respectively. If the calculated Chi-square statistic is greater than the critical value, then we must conclude that the row and the column variables are not independent of each other. This implies that they are significantly associated. The function <code>chisq.test()</code> can be used to implement the test.</p>
<p>The result of <code>chisq.test()</code> function is a list containing the following components:</p>
<ul>
<li>statistic: the value the chi-squared test statistic.</li>
<li>parameter: the degrees of freedom</li>
<li>p.value: the p-value of the test</li>
<li>observed: the observed count</li>
<li>expected: the expected count</li>
<li>residuals: Pearson residuals</li>
</ul>
<p>It is worth noting that the total inertia is equal to the total Chi-square score divided by the grand total. The square root of the total inertia is called <strong>trace</strong> and may be interpreted as a correlation coefficient. Any value of the trace &gt; 0.2 indicates a significant dependency between rows and columns. In the present example 1.056.</p>
</section>
<section id="pearson-residuals" class="level3">
<h3 class="anchored" data-anchor-id="pearson-residuals">Pearson residuals</h3>
<p>If we want to know the most contributing cells to the total Chi-square score, we just have to calculate the Chi-square statistic for each cell:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Ar_%7Bij%7D%20=%20%5Cdfrac%7Bo_%7Bij%7D-e_%7Bij%7D%7D%7B%5Csqrt%7Be_%7Bij%7D%7D%7D,%5C,i=1,%5Cdots,R;%5C,j=1,%5Cdots,C%0A"></p>
<p>The above formula returns the so-called Pearson residuals (<img src="https://latex.codecogs.com/png.latex?r">) for each cell (or standardized residuals). Cells with the highest absolute standardized residuals contribute the most to the total Chi-square score. Pearson residuals can be easily extracted from the output of the function <code>chisq.test()</code>.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">chisq <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;">chisq.test</span>(housetasks)</span>
<span id="cb21-2">chisq</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>
    Pearson's Chi-squared test

data:  housetasks
X-squared = 1944.5, df = 36, p-value &lt; 2.2e-16</code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Observed counts</span></span>
<span id="cb23-2">chisq<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>observed</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>           Wife Alternating Husband Jointly
Laundry     156          14       2       4
Main_meal   124          20       5       4
Dinner       77          11       7      13
Breakfeast   82          36      15       7
Tidying      53          11       1      57
Dishes       32          24       4      53
Shopping     33          23       9      55
Official     12          46      23      15
Driving      10          51      75       3
Finances     13          13      21      66
Insurance     8           1      53      77
Repairs       0           3     160       2
Holidays      0           1       6     153</code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected counts</span></span>
<span id="cb25-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(chisq<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>expected, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>            Wife Alternating Husband Jointly
Laundry    60.55       25.63   38.45   51.37
Main_meal  52.64       22.28   33.42   44.65
Dinner     37.16       15.73   23.59   31.52
Breakfeast 48.17       20.39   30.58   40.86
Tidying    41.97       17.77   26.65   35.61
Dishes     38.88       16.46   24.69   32.98
Shopping   41.28       17.48   26.22   35.02
Official   33.03       13.98   20.97   28.02
Driving    47.82       20.24   30.37   40.57
Finances   38.88       16.46   24.69   32.98
Insurance  47.82       20.24   30.37   40.57
Repairs    56.77       24.03   36.05   48.16
Holidays   55.05       23.30   34.95   46.70</code></pre>
</div>
<details class="code-fold">
<summary>Code</summary>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pearson's residuals</span></span>
<span id="cb27-2">e <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> chisq<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>residuals</span>
<span id="cb27-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Contribution in percentage of each cell (%)</span></span>
<span id="cb27-4">contrib <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>chisq<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>residuals<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>chisq<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>statistic</span></code></pre></div>
</details>
</div>
<p>The sign of the standardized residuals is also very important to interpret the association between rows and columns. Positive values in cells specify an attraction (positive association) between the corresponding row and column variables, such as, for instance, between the column “Wife” and the row “Laundry”. There is a strong positive association between the column “Husband” and the row “Repairs”. A negative residual implies a repulsion (negative association) between the corresponding row and column variables. For example the column “Wife” is negatively associated (i.e., “not associated”) with the row “Repairs”. There is a repulsion between the column “Husband” and the rows “Laundry” and “Main_meal”.</p>
<p>The contribution (in %) of a given cell to the total Chi-square score can be calculated as follows:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Ctext%7Bcontrib%7D%5C,(%5C%25)=100%5C,%5Cdfrac%7Br%5E2%7D%7B%5Cchi%5E2%7D%0A"></p>
<p>The relative contribution of each cell to the total Chi-square score give some indication of the nature of the dependency between rows and columns of the contingency table. It can be seen that:</p>
<ul>
<li>The column “Wife” is strongly associated with the rows “Laundry”</li>
<li>The column “Husband” is strongly associated with the row “Repairs”</li>
<li>The column “Jointly” is frequently associated with the row “Holidays”</li>
</ul>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">e <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb28-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb28-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rrrr"</span>,</span>
<span id="cb28-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Wife"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alternating"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Husband"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jointly"</span>),</span>
<span id="cb28-5">    <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;">2</span>,</span>
<span id="cb28-6">    <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;">"Pearson's residuals"</span>,</span>
<span id="cb28-7">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb28-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Pearson's residuals</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Wife</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Alternating</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Husband</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Jointly</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Laundry</td>
<td style="text-align: right;">12.27</td>
<td style="text-align: right;">-2.30</td>
<td style="text-align: right;">-5.88</td>
<td style="text-align: right;">-6.61</td>
</tr>
<tr class="even">
<td style="text-align: left;">Main_meal</td>
<td style="text-align: right;">9.84</td>
<td style="text-align: right;">-0.48</td>
<td style="text-align: right;">-4.92</td>
<td style="text-align: right;">-6.08</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Dinner</td>
<td style="text-align: right;">6.54</td>
<td style="text-align: right;">-1.19</td>
<td style="text-align: right;">-3.42</td>
<td style="text-align: right;">-3.30</td>
</tr>
<tr class="even">
<td style="text-align: left;">Breakfeast</td>
<td style="text-align: right;">4.88</td>
<td style="text-align: right;">3.46</td>
<td style="text-align: right;">-2.82</td>
<td style="text-align: right;">-5.30</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Tidying</td>
<td style="text-align: right;">1.70</td>
<td style="text-align: right;">-1.61</td>
<td style="text-align: right;">-4.97</td>
<td style="text-align: right;">3.59</td>
</tr>
<tr class="even">
<td style="text-align: left;">Dishes</td>
<td style="text-align: right;">-1.10</td>
<td style="text-align: right;">1.86</td>
<td style="text-align: right;">-4.16</td>
<td style="text-align: right;">3.49</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Shopping</td>
<td style="text-align: right;">-1.29</td>
<td style="text-align: right;">1.32</td>
<td style="text-align: right;">-3.36</td>
<td style="text-align: right;">3.38</td>
</tr>
<tr class="even">
<td style="text-align: left;">Official</td>
<td style="text-align: right;">-3.66</td>
<td style="text-align: right;">8.56</td>
<td style="text-align: right;">0.44</td>
<td style="text-align: right;">-2.46</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Driving</td>
<td style="text-align: right;">-5.47</td>
<td style="text-align: right;">6.84</td>
<td style="text-align: right;">8.10</td>
<td style="text-align: right;">-5.90</td>
</tr>
<tr class="even">
<td style="text-align: left;">Finances</td>
<td style="text-align: right;">-4.15</td>
<td style="text-align: right;">-0.85</td>
<td style="text-align: right;">-0.74</td>
<td style="text-align: right;">5.75</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Insurance</td>
<td style="text-align: right;">-5.76</td>
<td style="text-align: right;">-4.28</td>
<td style="text-align: right;">4.11</td>
<td style="text-align: right;">5.72</td>
</tr>
<tr class="even">
<td style="text-align: left;">Repairs</td>
<td style="text-align: right;">-7.53</td>
<td style="text-align: right;">-4.29</td>
<td style="text-align: right;">20.65</td>
<td style="text-align: right;">-6.65</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holidays</td>
<td style="text-align: right;">-7.42</td>
<td style="text-align: right;">-4.62</td>
<td style="text-align: right;">-4.90</td>
<td style="text-align: right;">15.56</td>
</tr>
</tbody>
</table>


</div>
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">contrib <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb29-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(</span>
<span id="cb29-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rrrr"</span>,</span>
<span id="cb29-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.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;">"Wife"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alternating"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Husband"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jointly"</span>),</span>
<span id="cb29-5">    <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;">2</span>,</span>
<span id="cb29-6">    <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;">"Percentage contribution to the total Chi square"</span>,</span>
<span id="cb29-7">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb29-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_classic</span>()</span></code></pre></div>
</details>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Percentage contribution to the total Chi square</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Wife</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Alternating</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Husband</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Jointly</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Laundry</td>
<td style="text-align: right;">7.74</td>
<td style="text-align: right;">0.27</td>
<td style="text-align: right;">1.78</td>
<td style="text-align: right;">2.25</td>
</tr>
<tr class="even">
<td style="text-align: left;">Main_meal</td>
<td style="text-align: right;">4.98</td>
<td style="text-align: right;">0.01</td>
<td style="text-align: right;">1.24</td>
<td style="text-align: right;">1.90</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Dinner</td>
<td style="text-align: right;">2.20</td>
<td style="text-align: right;">0.07</td>
<td style="text-align: right;">0.60</td>
<td style="text-align: right;">0.56</td>
</tr>
<tr class="even">
<td style="text-align: left;">Breakfeast</td>
<td style="text-align: right;">1.22</td>
<td style="text-align: right;">0.61</td>
<td style="text-align: right;">0.41</td>
<td style="text-align: right;">1.44</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Tidying</td>
<td style="text-align: right;">0.15</td>
<td style="text-align: right;">0.13</td>
<td style="text-align: right;">1.27</td>
<td style="text-align: right;">0.66</td>
</tr>
<tr class="even">
<td style="text-align: left;">Dishes</td>
<td style="text-align: right;">0.06</td>
<td style="text-align: right;">0.18</td>
<td style="text-align: right;">0.89</td>
<td style="text-align: right;">0.63</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Shopping</td>
<td style="text-align: right;">0.09</td>
<td style="text-align: right;">0.09</td>
<td style="text-align: right;">0.58</td>
<td style="text-align: right;">0.59</td>
</tr>
<tr class="even">
<td style="text-align: left;">Official</td>
<td style="text-align: right;">0.69</td>
<td style="text-align: right;">3.77</td>
<td style="text-align: right;">0.01</td>
<td style="text-align: right;">0.31</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Driving</td>
<td style="text-align: right;">1.54</td>
<td style="text-align: right;">2.40</td>
<td style="text-align: right;">3.37</td>
<td style="text-align: right;">1.79</td>
</tr>
<tr class="even">
<td style="text-align: left;">Finances</td>
<td style="text-align: right;">0.89</td>
<td style="text-align: right;">0.04</td>
<td style="text-align: right;">0.03</td>
<td style="text-align: right;">1.70</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Insurance</td>
<td style="text-align: right;">1.71</td>
<td style="text-align: right;">0.94</td>
<td style="text-align: right;">0.87</td>
<td style="text-align: right;">1.68</td>
</tr>
<tr class="even">
<td style="text-align: left;">Repairs</td>
<td style="text-align: right;">2.92</td>
<td style="text-align: right;">0.95</td>
<td style="text-align: right;">21.92</td>
<td style="text-align: right;">2.28</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holidays</td>
<td style="text-align: right;">2.83</td>
<td style="text-align: right;">1.10</td>
<td style="text-align: right;">1.23</td>
<td style="text-align: right;">12.45</td>
</tr>
</tbody>
</table>


</div>
</div>
<blockquote class="blockquote">
<p>SCA is just the singular value decomposition of the standardized residuals. How to perform this analysis using the R programming software will be explained in a future post (<a href="https://amsabatini.netlify.app/posts/correspondence_analysis_2">here</a>), together with some discussion of the implications that the results of the analysis may have.</p>
</blockquote>


</section>
</section>

 ]]></description>
  <category>multivariate statistics</category>
  <guid>https://amsabatini.netlify.app/posts/correspondence_analysis_1/</guid>
  <pubDate>Sun, 08 Dec 2024 23:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/correspondence_analysis_1/cover.png" medium="image" type="image/png" height="114" width="144"/>
</item>
<item>
  <title>Audio features for free</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/audio_features/</link>
  <description><![CDATA[ 




<p>In this post, I’ll be exploring how to pull back audio features and other information available for any track in the Spotify library. This information is usually hidden to users, however it can be easily retrieved. This will be done here using the <a href="https://github.com/charlie86/spotifyr">spotifyr</a> R wrapper package, whose description sounds as <em>a quick and easy wrapper for pulling back audio features from Spotify’s Web API in bulk</em>. Information about the various audio features offered by Spotify in the analysis of each track, as well as what all those features actually mean, is part of the discussion herein.</p>
<p>The first step in accessing Spotify data is to get an API key. This can be done by first logging into the Personal Dashboard available in the “Spotify for Developers” page (this requires that I signed on to a valid account, of course!) - see Figure&nbsp;1.</p>
<div id="fig-fig1" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/audio_features/dashboard.png" class="img-fluid quarto-figure quarto-figure-center figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Personal Dashboard view.
</figcaption>
</figure>
</div>
<p>From within the ‘Basic information page’ of the app lyrics I have created in my Dashboard, I was directed to select “Create a Client ID”. After filling out the required questions, I received my Client ID and Client Secret. The following code chunk is then needed to get the Spotify access token:</p>
<div class="cell">
<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>(spotifyr)</span>
<span id="cb1-2"></span>
<span id="cb1-3">id <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'my client ID'</span></span>
<span id="cb1-4">secret <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'my client secret'</span></span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.setenv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">SPOTIFY_CLIENT_ID =</span> id)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.setenv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">SPOTIFY_CLIENT_SECRET =</span> secret)</span>
<span id="cb1-8"></span>
<span id="cb1-9">access_token <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;">get_spotify_access_token</span>()</span></code></pre></div>
</div>
<p>For one of the several playlists that I have created using my Spotify account, namely <code>trial</code>, I invoke the function <code>get_user_playlists()</code> to obtain the associated <code>playlist_id</code>. This is possible once I have retrieved the information about the desired playlist using, e.g., the verb <code>filter</code> from tidyverse, as in the code chunk below, where how to use the <a href="https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids">Spotify ID</a> to complete the authentication process is also shown.</p>
<div class="cell">
<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>(tidyverse)</span>
<span id="cb2-2"></span>
<span id="cb2-3">my_id <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'my Spotify ID'</span></span>
<span id="cb2-4">playlist_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;">get_user_playlists</span>(my_id) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(name <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;">'trial'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(id)</span></code></pre></div>
</div>
<p>All is done. Now, a number of functions are available in spotifyr to get access to the audio features of all the tracks contained in the playlist <code>trial</code>.</p>
<div class="cell">
<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">tracks <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;">get_playlist_tracks</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">playlist_id =</span> playlist_id, </span>
<span id="cb3-2">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limit =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, </span>
<span id="cb3-3">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">authorization =</span> access_token)</span>
<span id="cb3-4">features <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;">get_track_audio_features</span>(tracks,</span>
<span id="cb3-5">                                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">authorization =</span> access_token)</span></code></pre></div>
</div>
<p>The call to <code>get_playlist_tracks()</code> with <code>limit</code> fixed to 100 (default maximum value) clarifies that only data of up 100 tracks can be retrieved from any Spotify playlist using just one call. The argument <code>authorization</code> requires a valid access token, as explained above. With the function <code>get_track_audio_features()</code> we finally get the values of the audio features. These numerical representations of various characteristics of each music track of interest are derived, behind the scenes, through the advanced audio analysis algorithms developed by Spotify as an important part of their business strategy. It is worth noting that <code>tracks</code> contains a wealth of other relevant information, including track title, author, album, release date and so forth.</p>
<section id="audio-features" class="level2">
<h2 class="anchored" data-anchor-id="audio-features">Audio features</h2>
<p>The following table reports a list of the audio features, see <a href="https://developer.spotify.com/documentation/web-api/reference/get-audio-features">here</a>. Since my native language is Italian, I translate each English term (in <strong>bold</strong>) into a corresponding Italian term (in <em>italic.</em>) Next to the column <strong>Audio feature</strong>, the column <strong>Explanation</strong> provides information regarding what each audio feature can mean.</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Audio feature</th>
<th>Explanation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>acousticness</strong> (<em>acusticità</em>)</td>
<td>Index from 0.0 to 1.0 of whether the track is acoustic. 1.0 represents high confidence that the track is acoustic, i.e., with little or no electronic elements.</td>
</tr>
<tr class="even">
<td><strong>danceability</strong> (<em>ballabilità</em>)</td>
<td>Index from 0.0 to 1.0 describing how suitable a track is for dancing. The index is based on a combination of tempo, rhythm stability, beat strength and overall regularity. A higher danceability score indicates a more danceable track.</td>
</tr>
<tr class="odd">
<td><strong>duration_ms</strong> (<em>durata</em>)</td>
<td>Track duration, in ms.</td>
</tr>
<tr class="even">
<td><strong>energy</strong> (<em>energia</em>)</td>
<td>Index from 0.0 to 1.0, which helps representing the intensity and activity level of a track. Tracks with high energy values tend to be more upbeat and energetic (they sound faster, louder and noisier than tracks with lower energy). Perceptual features contributing to this attribute include dynamic range, perceived loudness, timbre, onset rate, and general entropy.</td>
</tr>
<tr class="odd">
<td><strong>instrumentalness</strong> (<em>proporzione di parte strumentale su parte vocale</em>)</td>
<td>Index from 0.0 to 1.0, which helps predicting whether a track contains no vocals. A higher instrumentalness values indicates a higher likelihood that the track is purely instrumental. Values above 0.5 are intended to represent instrumental tracks, but confidence is higher as the value approaches 1.0.</td>
</tr>
<tr class="even">
<td><strong>key</strong> (<em>chiave</em>) (*)</td>
<td>The estimated overall key of the track. Integers, from -1 to 11, map to pitches using standard <a href="https://en.wikipedia.org/wiki/Pitch_class">Pitch Class</a> notation. E.g., 0 = C (<em>do</em>), 1 = C♯/D♭ (<em>do diesis</em>/<em>re bemolle</em>), 2 = D (<em>re</em>) up to 11 = B (<em>si</em>). If no key is detected, the value is -1.</td>
</tr>
<tr class="odd">
<td><strong>liveness</strong> (<em>vivacità</em>)</td>
<td>Index from 0.0 to 1.0, which helps detecting the presence of an audience in the recording. A higher liveness values suggests the track was performed live.</td>
</tr>
<tr class="even">
<td><strong>loudness</strong> (<em>presenza</em>)</td>
<td>This index represents the overall loudness of a track in decibels (dB). Loudness values are averaged across the entire track and are useful for comparing relative loudness of tracks. Typical values range between -60 and 0 dB.</td>
</tr>
<tr class="odd">
<td><strong>mode</strong> (<em>tonalità</em>) (*)</td>
<td>Mode indicates the modality (major or minor) of a track, the type of scale from which its melodic content is derived. Major is represented by 1 and minor by 0.</td>
</tr>
<tr class="even">
<td><strong>speechiness</strong> (<em>proporzione di parlato</em>)</td>
<td>Index from 0.0 to 1.0, which helps identifying tracks that contain spoken words. Higher values indicate more speech-like sounds, distinguishing them from purely instrumental tracks. Values above 0.66 describe tracks that are probably made entirely of spoken words. Values between 0.33 and 0.66 describe tracks that may contain both music and speech. Values below 0.33 most likely represent music and other non-speech-like tracks.</td>
</tr>
<tr class="odd">
<td><strong>tempo</strong> (<em>tempo</em>)</td>
<td>The overall estimated tempo of a track in beats per minute (BPM). In musical terminology, tempo is the speed or pace of a given piece and derives directly from the average beat duration.</td>
</tr>
<tr class="even">
<td><strong>time signature</strong> (<em>indicazione del tempo</em>) (*)</td>
<td>It yiels an estimated overall time signature for a track. The time signature (meter) is a notational convention to specify how many beats are in each bar (or measure). The time signature ranges from 3 to 7 indicating time signatures of “3/4”, to “7/4”.</td>
</tr>
<tr class="odd">
<td><strong>valence</strong> (<em>positività emotiva</em>)</td>
<td>Also known as the “positiveness” index, valence describes, in a range from 0.0 to 1.0, the musical positiveness conveyed by a track. Tracks with higher valence values tend to sound more positive, cheerful, and euphoric, while lower scores suggest more negative, sad, or depressing emotions.</td>
</tr>
</tbody>
</table>
<p>(*) Key, mode and time signature relate to the fundamental musical components of a track - these features are essential for understanding its harmonic structure and rhythmic pattern.</p>
<p>Another parameter that can be retrieved is named <strong>popularity</strong>. The popularity of a track will be between 0 and 100, with 100 being the most popular. The popularity is calculated by an algorithm and is mostly based on the total number of plays the track has had and how recent those plays are. Generally speaking, tracks that are being played a lot now will have a higher popularity than tracks that were played a lot in the past.</p>
</section>
<section id="example" class="level2">
<h2 class="anchored" data-anchor-id="example">Example</h2>
<p>As a matter of example, my Spotify’s playlist <code>trial</code> contains just four tracks. They were extracted from those published by a beloved singer of mine, <a href="https://www.sergiocaputo.com">Sergio Caputo</a> in his debut album “Un Sabato Italiano”, which was released in 1983. The magazine “Rolling Stone Italia” ranks the album “Un Sabato Italiano” at the 37th position in the list of the greatest 100 Italian albums of all time (<a href="https://it.wikipedia.org/wiki/I_100_migliori_album_italiani_secondo_Rolling_Stone">here</a>).</p>
<p>The songs in this album are a sort of diary of the life that the singer lived in the early Eighties. We can enjoy the chance to find ourselves immersed with him and his great friend Rino into smoky bars and complicated love stories, dealt with in noisy never-ending Saturday nights. Prevalent in all the tracks is the rhythm of swing and jazz. The high quality of the lyrics concurs, with the music, to evoke brilliantly those atmospheres, and even today the album can be considered a remarkable <em>testimonianza</em> of the mood of the early Eighties.</p>
<p>For the sake of better visualization, I grouped the results of the audio analysis of these tracks in the following three tables; moreover, I condensed the values of the three parameters key, mode and time signature into only one cell.</p>
<div class="cell">
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Key, mode, time signature, tempo, duration and loudness</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">track title</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">key-mode-time signature</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">tempo, BPM</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">duration, min</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">loudness, dB</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Mercy bocù</td>
<td style="text-align: right;">F major in 3/4</td>
<td style="text-align: right;">116.19</td>
<td style="text-align: right;">4.06</td>
<td style="text-align: right;">-14.09</td>
</tr>
<tr class="even">
<td style="text-align: left;">Bimba se sapessi</td>
<td style="text-align: right;">F major in 4/4</td>
<td style="text-align: right;">153.76</td>
<td style="text-align: right;">3.45</td>
<td style="text-align: right;">-13.70</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Mettimi giù</td>
<td style="text-align: right;">G major in 4/4</td>
<td style="text-align: right;">76.35</td>
<td style="text-align: right;">4.35</td>
<td style="text-align: right;">-18.34</td>
</tr>
<tr class="even">
<td style="text-align: left;">Spicchio di luna</td>
<td style="text-align: right;">G major in 4/4</td>
<td style="text-align: right;">77.73</td>
<td style="text-align: right;">3.18</td>
<td style="text-align: right;">-14.47</td>
</tr>
</tbody>
</table>


</div>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Acousticness, instrumentalness, liveness and speechiness</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">track title</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">acousticness</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">instrumentalness</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">liveness</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">speechiness</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Mercy bocù</td>
<td style="text-align: right;">0.40</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.15</td>
<td style="text-align: right;">0.05</td>
</tr>
<tr class="even">
<td style="text-align: left;">Bimba se sapessi</td>
<td style="text-align: right;">0.30</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.18</td>
<td style="text-align: right;">0.06</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Mettimi giù</td>
<td style="text-align: right;">0.12</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.09</td>
<td style="text-align: right;">0.07</td>
</tr>
<tr class="even">
<td style="text-align: left;">Spicchio di luna</td>
<td style="text-align: right;">0.47</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.11</td>
<td style="text-align: right;">0.09</td>
</tr>
</tbody>
</table>


</div>
<div class="cell-output-display">
<table class="lightable-classic caption-top table table-sm table-striped small" data-quarto-postprocess="true">
<caption>Danceability, energy, valence and popularity</caption>
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">track title</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">danceability</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">energy</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">valence</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">popularity</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Mercy bocù</td>
<td style="text-align: right;">0.60</td>
<td style="text-align: right;">0.40</td>
<td style="text-align: right;">0.42</td>
<td style="text-align: right;">26</td>
</tr>
<tr class="even">
<td style="text-align: left;">Bimba se sapessi</td>
<td style="text-align: right;">0.55</td>
<td style="text-align: right;">0.48</td>
<td style="text-align: right;">0.88</td>
<td style="text-align: right;">32</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Mettimi giù</td>
<td style="text-align: right;">0.67</td>
<td style="text-align: right;">0.46</td>
<td style="text-align: right;">0.89</td>
<td style="text-align: right;">19</td>
</tr>
<tr class="even">
<td style="text-align: left;">Spicchio di luna</td>
<td style="text-align: right;">0.40</td>
<td style="text-align: right;">0.36</td>
<td style="text-align: right;">0.14</td>
<td style="text-align: right;">26</td>
</tr>
</tbody>
</table>


</div>
</div>
<blockquote class="blockquote">
<p><em>Translating music in numbers</em> as exemplified above, might be, and actually is, the starting point of some heavy data mining for doing interesting research at the intersection between several different domains, which may include data science, statistics, machine learning, sociology, psychology, neuroscience, just to mention a few of them. Something more about these opportunities will be discussed in future posts … up to now, that’s my two cents for the day!</p>
</blockquote>
<p>The thumbnail image is credited to <a href="https://www.flaticon.com/free-icons/spotify" title="spotify icons">Spotify icons created by Creative - Flaticon</a></p>


</section>

 ]]></description>
  <category>data mining</category>
  <guid>https://amsabatini.netlify.app/posts/audio_features/</guid>
  <pubDate>Mon, 11 Nov 2024 23:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/audio_features/music_reduced.png" medium="image" type="image/png" height="137" width="144"/>
</item>
<item>
  <title>Numerical simulation for stochastic differential equations</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/numerical_simulation/</link>
  <description><![CDATA[ 




<p>We consider here the class of stochastic processes known as diffusion processes, which are solutions to stochastic differential equation (SDE) of the form:</p>
<p><span id="eq-eq1"><img src="https://latex.codecogs.com/png.latex?%0AdX(t)=b(t,X(t))%5C,dt+%5Csigma(t,X(t))%5C,dW(t)%0A%5Ctag%7B1%7D"></span></p>
<p>with some initial condition <img src="https://latex.codecogs.com/png.latex?X(0)"> which can be regarded as being either constant or random. The SDE is interpreted in the Itô sense:</p>
<p><span id="eq-eq2"><img src="https://latex.codecogs.com/png.latex?%0AX(t)=X(0)+%5Cint_0%5Etb(s,X(s))%5C,ds+%5Cint_0%5Et%5Csigma(s,X(s))%5C,dW(s)%0A%5Ctag%7B2%7D"></span></p>
<p>and <img src="https://latex.codecogs.com/png.latex?%5C%7BW(t),t%5Cin%5B0,T%5D%5C%7D"> is the so-called Brownian motion or Wiener process.</p>
<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">
The Wiener process
</div>
</div>
<div class="callout-body-container callout-body">
<p>The Wiener process <img src="https://latex.codecogs.com/png.latex?%5C%7BW(t),t%5Cin%5B0,T%5D%5C%7D"> has independent Gaussian increments and continuous sample paths, and comes endowed with the following properties:</p>
<ol type="1">
<li><p><img src="https://latex.codecogs.com/png.latex?W(0)=0"> (with probability 1).</p></li>
<li><p>For <img src="https://latex.codecogs.com/png.latex?0%5Cleq%20s%5Cleq%20t%5Cleq%20T"> the random variable given by the Gaussian increment <img src="https://latex.codecogs.com/png.latex?W(t)-W(s)"> is distributed with mean zero and variance <img src="https://latex.codecogs.com/png.latex?t-s">; equivalently, <img src="https://latex.codecogs.com/png.latex?W(t)-W(s)%5Cpropto%5Csqrt%7Bt-s%7D%5C,Z">, where <img src="https://latex.codecogs.com/png.latex?Z"> denotes a standard Gaussian random variable, with a mean of zero and a variance of one.</p></li>
<li><p>For <img src="https://latex.codecogs.com/png.latex?0%5Cleq%20s%5Cleq%20t%5Cleq%20u%5Cleq%20v%5Cleq%20T"> the increments <img src="https://latex.codecogs.com/png.latex?W(t)-W(s)"> and <img src="https://latex.codecogs.com/png.latex?W(v)-W(u)"> are independent (condition of independence on any two disjoint intervals).</p></li>
</ol>
</div>
</div>
<p>The two deterministic functions <img src="https://latex.codecogs.com/png.latex?b(%5Ccdot,%5Ccdot)"> and <img src="https://latex.codecogs.com/png.latex?%5Csigma(%5Ccdot,%5Ccdot)"> are called, respectively, the drift and the diffusion coefficients of the SDE. Under a number of assumptions regarding their properties (i.e., global Lipschitz and linear growth, see <span class="citation" data-cites="iacus2008">(Iacus 2008)</span> for mathematical details) the SDE has a unique solution with continuous sample paths such that</p>
<p><span id="eq-eq3"><img src="https://latex.codecogs.com/png.latex?%0AE%5Cleft%5B%5Cint_0%5ET%5Cvert%5C,X(t)%5C,%5Cvert%5E2%5C,dt%5Cright%5D%3C%5Cinfty%0A%5Ctag%7B3%7D"></span></p>
<p>For the sake of simpler notation, <img src="https://latex.codecogs.com/png.latex?X(t)%5Crightarrow%20X_t"> is used in the following, therefore Equation&nbsp;1 and Equation&nbsp;2 can be written:</p>
<p><span id="eq-eq4"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0AdX_t&amp;=b(t,X_t)%5C,dt+%5Csigma(t,X_t)%5C,dW_t%5C%5C%0AX_t&amp;=X_0+%5Cint_0%5Etb(s,X_s)%5C,ds+%5Cint_0%5Et%5Csigma(s,X_s)%5C,dW_s%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B4%7D"></span></p>
<p>The Itô formula is an important tool of stochastic calculus. It can be regarded as the stochastic version of the Taylor expansion of a function <img src="https://latex.codecogs.com/png.latex?f(t,X_t)"> stopped at the second order, where <img src="https://latex.codecogs.com/png.latex?X_t"> is a diffusion process. If the function <img src="https://latex.codecogs.com/png.latex?f(%5Ccdot,%5Ccdot)"> is a twice differentiable function on both arguments, we have:</p>
<p><span id="eq-eq5"><img src="https://latex.codecogs.com/png.latex?%0Adf(t,X_t)=f_t(t,X_t)%5C,dt+f_x(t,X_t)%5C,dX_t+%5Cfrac%7B1%7D%7B2%7Df_%7Bxx%7D(t,X_t)(dX_t)%5E2%0A%5Ctag%7B5%7D"></span></p>
<p>where:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af_t(t,x)=%5Cdfrac%7B%5Cpartial%7D%7B%5Cpartial%20t%7Df(t,x),%5Cquad%20f_x(t,x)=%5Cdfrac%7B%5Cpartial%20f(t,x)%7D%7B%5Cpartial%20x%7D,%5Cquad%20f_%7Bxx%7D(t,x)=%5Cdfrac%7B%5Cpartial%5E2%20f(t,x)%7D%7B%5Cpartial%20x%5E2%7D%0A"></p>
<p>Using Equation&nbsp;1 and the Itô lemma, which states that <img src="https://latex.codecogs.com/png.latex?(dW_t)%5E2=dt">, we have:</p>
<p><span id="eq-eq6"><img src="https://latex.codecogs.com/png.latex?%0A(dX_t)%5E2=%5Cleft%5Bb(t,X_t)%5C,dt+%5Csigma(t,X_t)%5C,dW_t%5Cright%5D%5E2=%5Csigma%5E2(t,X_t)%5C,dt+%5Ctext%7BO%7D(dt%5E%7B3/2%7D)%0A%5Ctag%7B6%7D"></span></p>
<p>Equation&nbsp;5 can thus be written:</p>
<p><span id="eq-eq7"><img src="https://latex.codecogs.com/png.latex?%0Adf(t,X_t)=%5Cleft%5Bf_t(t,X_t)+f_x(t,X_t)b(t,X_t)+%5Cfrac%7B1%7D%7B2%7Df_%7Bxx%7D(t,X_t)%5Cright%5Ddt+f_x(t,X_t)%5Csigma(t,X_t)%5C,dW_t%0A%5Ctag%7B7%7D"></span></p>
<section id="approximation-methods" class="level2">
<h2 class="anchored" data-anchor-id="approximation-methods">Approximation methods</h2>
<p>To apply a numerical method to the generic SDE of Equation&nbsp;1 over the time interval <img src="https://latex.codecogs.com/png.latex?%5B0,T%5D">, the time interval needs to be discretized <span class="citation" data-cites="higham.2001">(Higham 2001)</span>. Let <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t=T/M"> for some positive integer <img src="https://latex.codecogs.com/png.latex?M">, and <img src="https://latex.codecogs.com/png.latex?t_n=n%5CDelta%20t">. The numerical approximation to <img src="https://latex.codecogs.com/png.latex?X(t_n)"> will be denoted <img src="https://latex.codecogs.com/png.latex?X_n"> (<img src="https://latex.codecogs.com/png.latex?n=1,%5Ccdots,M">).</p>
<p>The Euler-Maruyama method takes the form:</p>
<p><span id="eq-eq8"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0AX_n&amp;=X_%7Bn-1%7D+b(X_%7Bn-1%7D)%5CDelta%20t+%5Csigma(X_%7Bn-1%7D)%5CDelta%20W_%7Bn-1%7D%5C%5C%0AX_0&amp;=x_0%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B8%7D"></span></p>
<p>It is worth noting that in the case when <img src="https://latex.codecogs.com/png.latex?%5Csigma(X_t)=0">, then the problem becomes deterministic: this approximation method reduces to the standard Euler method for the ordinary differential equation <img src="https://latex.codecogs.com/png.latex?%5Cdot%7BX%7D_t=b(t,X_t)">.</p>
<p>The Milstein’s method adds a correction to the stochastic increment to produce the solution to the SDE. The correction arises because the Taylor expansion must be modified in the case of Itô calculus. Truncating the Taylor expansion to the second order at an appropriate point yields:</p>
<p><span id="eq-eq9"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0AX_n&amp;=X_%7Bn-1%7D+b(t_%7Bn-1%7D,X_%7Bn-1%7D)%5CDelta%20t+%5Csigma(t_%7Bn-1%7D,X_%7Bn-1%7D)%5CDelta%20W_%7Bn-1%7D%5C%5C%0A&amp;+%5Cdfrac%7B1%7D%7B2%7D%5Csigma(X_%7Bn-1%7D)%5Csigma_x(X_%7Bn-1%7D)%5Cleft%5B(%5CDelta%20W_%7Bn-1%7D)%5E2-%5CDelta%20t%5Cright%5D%5C%5C%0AX_0&amp;=x_0%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B9%7D"></span></p>
<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">
Strong and weak convergence
</div>
</div>
<div class="callout-body-container callout-body">
<p>The methods of approximation of the continuous solution to an SDE are classified according to their different properties. Mainly two criteria of optimality are used in the literature: the strong and the weak (orders of) convergence.</p>
<p>A time-discretized approximation <img src="https://latex.codecogs.com/png.latex?X_%5Cdelta"> of a continuous-time process <img src="https://latex.codecogs.com/png.latex?X">, with <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t"> the time increment of the discretization, is said to be of strong order of convergence <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> to <img src="https://latex.codecogs.com/png.latex?X"> if for any fixed time horizon <img src="https://latex.codecogs.com/png.latex?T"> it holds true that</p>
<p><span id="eq-eq10"><img src="https://latex.codecogs.com/png.latex?%0AE%5B%5C,%5Cvert%20X_%7B%5CDelta%20t%7D(%5Ctau)-X(%5Ctau)%5Cvert%5C,%5D%5Cleq%20C%5CDelta%20t%5E%5Cgamma,%5Cquad%5Cforall%5CDelta%20t%3C%5CDelta%20t_0%0A%5Ctag%7B10%7D"></span></p>
<p>for any <img src="https://latex.codecogs.com/png.latex?%5Ctau%5Cin%5B0,T%5D">, with <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t_0%3E0"> and <img src="https://latex.codecogs.com/png.latex?C"> a constant that does not depend on <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t">.</p>
<p>Along with the strong convergence, the weak convergence can also be defined. In the same conditions as above, <img src="https://latex.codecogs.com/png.latex?X_%7B%5CDelta%20t%7D"> is said to converge weakly of order <img src="https://latex.codecogs.com/png.latex?%5Cgamma"> to <img src="https://latex.codecogs.com/png.latex?X"> if for any fixed horizon <img src="https://latex.codecogs.com/png.latex?T"> and any <img src="https://latex.codecogs.com/png.latex?2(%5Cbeta+1)"> continuous differentiable function <img src="https://latex.codecogs.com/png.latex?g"> of polynomial growth, it holds true that</p>
<p><span id="eq-eq11"><img src="https://latex.codecogs.com/png.latex?%0A%5Cvert%5C,E_g%5BX_%7B%5CDelta%20t%7D(%5Ctau)%5D-E_g%5BX(%5Ctau)%5D%5C,%5Cvert%5Cleq%20C%5CDelta%20t%5E%5Cbeta%0A%5Ctag%7B11%7D"></span></p>
<p>for any <img src="https://latex.codecogs.com/png.latex?%5Ctau%5Cin%5B0,T%5D">, with <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t_0%3E0"> and <img src="https://latex.codecogs.com/png.latex?C"> a constant that does not depend on <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t">.</p>
<p>Whereas the strong order of convergence measures the rate at which the <em>mean of the error</em> decays as <img src="https://latex.codecogs.com/png.latex?%5CDelta%20t%5Crightarrow0">, the weak order of convergence measures the rate of decay of the <em>error of the means</em>.</p>
<p>Methods of approximation of some order that strongly converge usually have a higher order of weak convergence. This is the case with the Euler-Maruyama method, which is strongly convergent of order <img src="https://latex.codecogs.com/png.latex?%5Cgamma=1/2"> and weakly convergent of order <img src="https://latex.codecogs.com/png.latex?%5Cbeta=1"> (under some smoothness conditions on the coefficients of the SDE). The Milstein’s method has both weak and strong order of convergence, <img src="https://latex.codecogs.com/png.latex?%5Cgamma=%5Cbeta=1">, which is superior to the Euler–Maruyama method.</p>
<p>It is interesting noting that, when the diffusion coefficient does not depend on the state variable of the process, the Euler-Maruyama and Milstein’s methods coincide. This is one important case, therefore, when the Euler-Maruyama method is strongly convergent of order <img src="https://latex.codecogs.com/png.latex?%5Cgamma=1">.</p>
</div>
</div>
</section>
<section id="numerical-test" class="level2">
<h2 class="anchored" data-anchor-id="numerical-test">Numerical test</h2>
<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">
Geometric Brownian motion
</div>
</div>
<div class="callout-body-container callout-body">
<p>The geometric Brownian motion (GBM) is the solution to the SDE:</p>
<p><span id="eq-eq12"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0AdX_t&amp;=%5Clambda%5C,X_t%5C,dt+%5Cmu%5C,X_t%5C,dW_t%5C%5C%0AX_0&amp;=x_0%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B12%7D"></span></p>
<p>where the parameter <img src="https://latex.codecogs.com/png.latex?%5Clambda"> is interpreted as the constant interest rate and <img src="https://latex.codecogs.com/png.latex?%5Cmu%3E0"> as the volatility. For a GBM, the drift and diffusion coefficients are both linearly related to the state variable <img src="https://latex.codecogs.com/png.latex?X">, namely <img src="https://latex.codecogs.com/png.latex?b(t,X_t)=%5Clambda%5C,X_t"> and <img src="https://latex.codecogs.com/png.latex?%5Csigma(X_t)=%5Cmu%5C,X_t">.</p>
<p>The explicit solution of the SDE Equation&nbsp;12 can be found by operating the following change of variable:</p>
<p><span id="eq-eq13"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0AY_t=%5Clog%20X_t%5Cquad%5Crightarrow%5Cquad%20dY_t&amp;=%5Cfrac%7B1%7D%7BX_t%7D%5C,dX_t-%5Cfrac%7B1%7D%7B2X_t%5E2%7D%5C,(dX_t)%5E2%5C%5C%0A&amp;=%5Cleft(%5Clambda-%5Cfrac%7B1%7D%7B2%7D%5Cmu%5E2%5Cright)dt+%5Cmu%5C,dW_t%0A%5Cend%7Bsplit%7D%0A%5Ctag%7B13%7D"></span></p>
<p>We obtain:</p>
<p><span id="eq-eq14"><img src="https://latex.codecogs.com/png.latex?%0AX_t=x_0%5Cexp%5Cleft%5B%5Cleft(%5Clambda-%5Cfrac%7B1%7D%7B2%7D%5Cmu%5E2%5Cright)t+%5Cmu%5C,W_t%5Cright%5D%0A%5Ctag%7B14%7D"></span></p>
</div>
</div>
<p>For the GBM, the following stochastic difference equations are obtained for the Euler-Maruyama method:</p>
<p><span id="eq-eq15"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0AX%5EE_n&amp;=X%5EE_%7Bn-1%7D(1+%5Clambda%5CDelta%20t+%5Cmu%5CDelta%20W_%7Bn-1%7D)%5C%5C%0AX%5EE_0&amp;=x_0%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B15%7D"></span></p>
<p>and for the Milstein method:</p>
<p><span id="eq-eq16"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0AX%5EM_n&amp;=X%5EM_%7Bn-1%7D+%5Clambda%20X%5EM_%7Bn-1%7D%5CDelta%20t+%5Cmu%20X%5EM_%7Bn-1%7D%5CDelta%20W_%7Bn-1%7D%5C%5C%0A&amp;+%5Cdfrac%7B1%7D%7B2%7D%5Cmu%5E2%20X%5EM_%7Bn-1%7D%5Cleft%5B(%5CDelta%20W_%7Bn-1%7D)%5E2-%5CDelta%20t%5Cright%5D%5C%5C%0A&amp;=X%5EM_%7Bn-1%7D%5Cleft%5B1+%5Cleft(%5Clambda-%5Cdfrac%7B1%7D%7B2%7D%5Cmu%5E2%5Cright)%5CDelta%20t+%5Cmu%5CDelta%20W_%7Bn-1%7D+%5Cdfrac%7B1%7D%7B2%7D%5Cmu%5E2(%5CDelta%20W_%7Bn-1%7D)%5E2%5Cright%5D%5C%5C%0AX%5EM_0&amp;=x_0%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B16%7D"></span></p>
<p>Recall that <img src="https://latex.codecogs.com/png.latex?%5CDelta%20W_%7Bn-1%7D=%5Csqrt%7B%5CDelta%20t%7D%5C,Z">, where <img src="https://latex.codecogs.com/png.latex?Z%5Cpropto%20N(0,1)">.</p>
<p>Equation&nbsp;15 reads:</p>
<p><span id="eq-eq17"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0AX%5EE_n&amp;=X%5EE_%7Bn-1%7D(1+%5Clambda%5CDelta%20t+%5Cmu%5Csqrt%7B%5CDelta%20t%7D%5C,Z)%5C%5C%0AX%5EE_0&amp;=x_0%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B17%7D"></span></p>
<p>Equation&nbsp;16 reads:</p>
<p><span id="eq-eq18"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0AX%5EM_n&amp;=X%5EM_%7Bn-1%7D%5Cleft%5B1+%5Cleft(%5Clambda+%5Cdfrac%7B1%7D%7B2%7D%5Cmu%5E2(Z%5E2-1)%5Cright)%5CDelta%20t+%5Cmu%5Csqrt%7B%5CDelta%20t%7D%5C,Z%5Cright%5D%5C%5C%0AX%5EM_0&amp;=x_0%5C%5C%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B18%7D"></span></p>
<p>Comparing the exact solution Equation&nbsp;14 with Equation&nbsp;18 shows how the Milstein method makes the Taylor expansion exact up to order <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BO%7D(%5CDelta%20t)">.</p>
<p>The following code will test the strong convergence of both methods when they are applied to the linear SDE Equation&nbsp;12, with <img src="https://latex.codecogs.com/png.latex?%5Clambda=2,%5Cmu=1,x_0=1">. One thousand sample paths are simulated over the time interval <img src="https://latex.codecogs.com/png.latex?%5B0,1%5D">, for step sizes being integer multiples of <img src="https://latex.codecogs.com/png.latex?2%5E%7B-9%7D%5C;(8,4,2,1)">. The order of convergence is tested at the end point of the chosen time interval. The results of the simulations are shown in Figure&nbsp;1.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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>(tidyverse)</span>
<span id="cb1-2"></span>
<span id="cb1-3"><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;">1792</span>)</span>
<span id="cb1-4">lambda  <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="cb1-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>
<span id="cb1-6">Xzero   <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-7">T       <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-8">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;">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;">9</span></span>
<span id="cb1-9">dt      <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>N</span>
<span id="cb1-10">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;">1000</span></span>
<span id="cb1-11">Xerr_EM <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> M, <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;">5</span>)</span>
<span id="cb1-12">Xerr_M  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">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> M, <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;">5</span>)</span>
<span id="cb1-13"></span>
<span id="cb1-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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, M, <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;">1</span>)) {</span>
<span id="cb1-15">  dW    <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;">rnorm</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</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;">sd =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(dt))</span>
<span id="cb1-16">  W     <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;">cumsum</span>(dW)</span>
<span id="cb1-17">  Xtrue <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Xzero<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>((lambda <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.5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>mu<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>T <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>W[N])</span>
<span id="cb1-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="sc" 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>
<span id="cb1-19">    R  <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>(j<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>)    </span>
<span id="cb1-20">    Dt <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>dt </span>
<span id="cb1-21">    L  <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>R</span>
<span id="cb1-22">    Xtemp_EM <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Xzero</span>
<span id="cb1-23">    Xtemp_M  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Xzero</span>
<span id="cb1-24">    <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, L, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) {</span>
<span id="cb1-25">      Winc     <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>(dW[(R<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;">-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>(R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>k)])</span>
<span id="cb1-26">      Xtemp_EM <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Xtemp_EM<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> Dt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>lambda <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>Winc)</span>
<span id="cb1-27">      Xtemp_M  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Xtemp_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> Dt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>lambda <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.5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>mu<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>(Winc<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> Dt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> mu<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>Winc)</span>
<span id="cb1-28">    }</span>
<span id="cb1-29">    Xerr_EM[i, j] <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;">abs</span>(Xtemp_EM <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Xtrue)</span>
<span id="cb1-30">    Xerr_M[i, j]  <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;">abs</span>(Xtemp_M <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> Xtrue)</span>
<span id="cb1-31">  }</span>
<span id="cb1-32">}</span>
<span id="cb1-33">Dtvals <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="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;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>dt</span>
<span id="cb1-34">Xm_EM  <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;">array</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;">dim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-35">Xm_M   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">array</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;">dim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb1-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;">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;">4</span>) {</span>
<span id="cb1-37">  Xm_EM[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(Xerr_EM[, i])</span>
<span id="cb1-38">  Xm_M[i]  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(Xerr_M[, i])</span>
<span id="cb1-39">}</span>
<span id="cb1-40">t   <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>(Dtvals, <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;">2</span>)</span>
<span id="cb1-41">g   <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Euler-Maruyama"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Milstein"</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;">4</span>)</span>
<span id="cb1-42">e   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(Xm_EM, Xm_M)</span>
<span id="cb1-43">e_t <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(t), t)</span>
<span id="cb1-44">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> e, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error_true =</span> e_t)</span></code></pre></div>
</details>
</div>
<div class="cell">
<div class="cell-output-display">
<div id="fig-fig1" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/numerical_simulation/index_files/figure-html/fig-fig1-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Strong error plot for the Euler-Maruyama and Milstein’s methods; dashed lines give the appropriate reference slope for each method, <img src="https://latex.codecogs.com/png.latex?%5Cgamma=0.5"> (Euler-Maruyama) and <img src="https://latex.codecogs.com/png.latex?%5Cgamma=1"> (Milstein).
</figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="lamperti-transform" class="level2">
<h2 class="anchored" data-anchor-id="lamperti-transform">Lamperti transform</h2>
<p>An interesting application of the Itô formula Equation&nbsp;7 is now discussed in connection with a formulation of Equation&nbsp;4 where the diffusion coefficient is not an explicit function of time, but only depends on the state variable <img src="https://latex.codecogs.com/png.latex?X">:</p>
<p><span id="eq-eq19"><img src="https://latex.codecogs.com/png.latex?%0AdX_t=b(t,X_t)%5C,dt+%5Csigma(X_t)%5C,dW_t%0A%5Ctag%7B19%7D"></span></p>
<p>The Lamperti transform is defined as follows:</p>
<p><span id="eq-eq20"><img src="https://latex.codecogs.com/png.latex?%0AY_t=F(X_t)=%5Cint_z%5E%7BX_t%7D%5Cfrac%7B1%7D%7B%5Csigma(s)%7D%5C,ds%0A%5Ctag%7B20%7D"></span></p>
<p>where <img src="https://latex.codecogs.com/png.latex?z"> is any arbitrary value in the state space of <img src="https://latex.codecogs.com/png.latex?X">. We assume that the function <img src="https://latex.codecogs.com/png.latex?F(%5Ccdot)"> defines a one-to-one mapping from the state space of <img src="https://latex.codecogs.com/png.latex?X"> to <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BR%7D">. We have:</p>
<p><span id="eq-eq21"><img src="https://latex.codecogs.com/png.latex?%0AF_t(X_t)=0%5Cquad%5Cquad%20F_x(X_t)=%5Cdfrac%7B1%7D%7B%5Csigma(X_t)%7D%5Cquad%5Cquad%20F_%7Bxx%7D(X_t)=-%5Cdfrac%7B%5Csigma_x(X_t)%7D%7B%5Csigma%5E2(X_t)%7D%0A%5Ctag%7B21%7D"></span></p>
<p>Applying the Itô formula Equation&nbsp;5, we obtain:</p>
<p><span id="eq-eq22"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0AdY_t&amp;=%5Cleft%5B%5Cdfrac%7Bb(t,X_t))%7D%7B%5Csigma(X_t)%7D-%5Cdfrac%7B1%7D%7B2%7D%5Csigma_x(X_t)%5Cright%5Ddt+dW_t%5C%5C%0AX_t&amp;=F%5E%7B-1%7D(Y_t)%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B22%7D"></span></p>
<p>The Lamperti transform changes the generic SDE Equation&nbsp;4 into another SDE with unitary diffusion coefficient. As an example of application of the Lamperti transform, we consider here a result on transformations of SDEs that is relevant for the topic of interest in this post.</p>
<p>First, we discretize the Lamperti transform of Equation&nbsp;22 using the Euler-Maruyama method (see Equation&nbsp;8):</p>
<p><span id="eq-eq23"><img src="https://latex.codecogs.com/png.latex?%0AY_n=Y_%7Bn-1%7D+%5Cleft%5B%5Cdfrac%7Bb(t_%7Bn-1%7D,X_%7Bn-1%7D)%7D%7B%5Csigma(X_%7Bn-1%7D)%7D-%5Cdfrac%7B1%7D%7B2%7D%5Csigma_x(X_%7Bn-1%7D)%5Cright%5D%5CDelta%20t+%5Csqrt%7B%5CDelta%20t%7D%5C,Z%0A%5Ctag%7B23%7D"></span></p>
<p>Second, we apply the Taylor expansion to the inverse transform <img src="https://latex.codecogs.com/png.latex?X_t=G(Y_t)">, noting that:</p>
<p><span id="eq-eq24"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0AG_y(Y_t)&amp;=%5Csigma(G(Y_t))%5C%5C%0AG_%7Byy%7D(Y_t)&amp;=%5Csigma(G(Y_t))%5Csigma_x(G(Y_t))%0A%5Cend%7Bsplit%7D%0A%5Ctag%7B24%7D"></span></p>
<p>We obtain:</p>
<p><span id="eq-eq25"><img src="https://latex.codecogs.com/png.latex?%0AG(Y_%7Bn%7D)=G(Y_%7Bn-1%7D)+G_y(Y_%7Bn-1%7D)(Y_n-Y_%7Bn-1%7D)+%5Cfrac%7B1%7D%7B2%7DG_%7Byy%7D(Y_%7Bn-1%7D)(Y_n-Y_%7Bn-1%7D)%5E2%0A%5Ctag%7B25%7D"></span></p>
<p>and hence:</p>
<p><span id="eq-eq26"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0AX_n&amp;=X_%7Bn-1%7D+%5Cleft%5Bb(t_%7Bn-1%7D,X_%7Bn-1%7D)-%5Cdfrac%7B1%7D%7B2%7D%5Csigma(X_%7Bn-1%7D)%5Csigma_x(X_%7Bn-1%7D)%5Cright%5D%5CDelta%20t%5C%5C%0A&amp;+%5Csigma(X_%7Bn-1%7D)%5Csqrt%7B%5CDelta%20t%7D%5C,Z+%5Cfrac%7B1%7D%7B2%7D%5Csigma(X_%7Bn-1%7D)%5Csigma_x(X_%7Bn-1%7D)%5CDelta%20t%5C,Z%5E2%0A%5Cend%7Bsplit%7D%0A%5Ctag%7B26%7D"></span></p>
<p>In conclusion, the Milstein method on the original process and the Euler-Maruyama method on the transformed process are equal up to and including the order <img src="https://latex.codecogs.com/png.latex?%5Ctext%7BO%7D(%5CDelta%20t)">.</p>
<p>In general if a transformation such as the Lamperti transform eliminates the interactions between the state of the process and the increments of the Wiener process, the performance of the Euler-Maruyama method on the transformed SDE are expected to improve.</p>
<p>The logarithmic transformation <img src="https://latex.codecogs.com/png.latex?F(X_t)=%5Clog%20X_t"> is an example of nonlinear transformation that is capable of eliminating the interaction between the state <img src="https://latex.codecogs.com/png.latex?X_t"> of the GBM process and the increments of the Wiener process <img src="https://latex.codecogs.com/png.latex?dW_t">, as shown in Equation&nbsp;13. Therefore the Euler-Maruyama method can be confidently applied to the transformed process; the Milstein method is then obtained by simply taking the Taylor expansion of the inverse transform.</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" data-entry-spacing="0">
<div id="ref-higham.2001" class="csl-entry">
Higham, Desmond J. 2001. <span>“An Algorithmic Introduction to Numerical Simulation of Stochastic Differential Equations.”</span> <em>SIAM Review</em> 43 (3): 525–46. <a href="https://doi.org/10.1137/s0036144500378302">https://doi.org/10.1137/s0036144500378302</a>.
</div>
<div id="ref-iacus2008" class="csl-entry">
Iacus, Stefano M. 2008. <em>Simulation and Inference for Stochastic Differential Equations</em>. Springer New York. <a href="https://doi.org/10.1007/978-0-387-75839-8">https://doi.org/10.1007/978-0-387-75839-8</a>.
</div>
</div></section></div> ]]></description>
  <category>stochastic calculus</category>
  <guid>https://amsabatini.netlify.app/posts/numerical_simulation/</guid>
  <pubDate>Wed, 05 Jun 2024 22:00:00 GMT</pubDate>
</item>
<item>
  <title>Significant figures</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/significant_figures/</link>
  <description><![CDATA[ 




<section id="significant-figures" class="level2">
<h2 class="anchored" data-anchor-id="significant-figures">Significant figures</h2>
<p>It happens frequently that the number of digits that are available to report measurement results is high. Usually, measurement results are produced by carrying arithmetic operations with computers or calculators, whose level of numerical precision, albeit finite, is too high given the true information gathered by the measurements. In other words, the precision can be excessive, and too many digits can simply swamp the observer, making the message in the measurements more obscure. <strong>Significant figures</strong>, also referred to as significant digits, are specific digits within a number written in positional notation that carry both reliability and necessity in reporting a measurement result. Proper use of significant figures is thus an essential element in the presentation of both experimental and calculated results together with their associated uncertainty.</p>
<section id="rules-for-significant-figures" class="level3">
<h3 class="anchored" data-anchor-id="rules-for-significant-figures">Rules for significant figures</h3>
<p>A number of rules exist for determining how many significant figures are in a number:</p>
<ol type="1">
<li>non-zero digits are always significant</li>
</ol>
<ul>
<li>4.6 has two significant figures</li>
</ul>
<ol start="2" type="1">
<li>leading zeros placed before the first non-zero digit are not significant (they are called <em>placeholders</em>)</li>
</ol>
<ul>
<li>0.046 has two significant figures</li>
</ul>
<ol start="3" type="1">
<li>trailing zeros placed after all other digits but behind a decimal point are significant</li>
</ol>
<ul>
<li>4.60 has three significant figures</li>
</ul>
<p>The leftmost digit which is not a zero is referred to as the <em>most significant digit</em> (MSD); the rightmost digit of a decimal number is the <em>least significant digit</em> (LSD), regardless it is a zero or not: 4 and 0 are thus, respectively, the MSD and the LSD of 4.60; 4 and 1 are, respectively, the MSD and the LSD of 4.61. Every digit between the LSD and the MSD, including zeros, should be counted as significant figures, hence 4.60 and 40.60 have, respectively, three and four significant figures.</p>
<p>Ambiguous situations arise when zeros are at the end of the number and not behind a decimal point as, for example, in the number 4600. Confusion can be avoided if the number is expressed in <em>scientific notation</em>.</p>
<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">
Scientific notation
</div>
</div>
<div class="callout-body-container callout-body">
<p>Scientific notation is a way of expressing numbers that are much too large or much too small to be conveniently written in decimal form (i.e., their representation would involve a long string of digits). In scientific notation, nonzero numbers are written in the form:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Am%5Ctimes10%5En%0A"></p>
<p>where <img src="https://latex.codecogs.com/png.latex?n"> is an integer, and the coefficient <img src="https://latex.codecogs.com/png.latex?m"> is a nonzero real number (usually <img src="https://latex.codecogs.com/png.latex?1%5Cleq%5Cvert%5C,m%5C,%5Cvert%3C10">). The integer <img src="https://latex.codecogs.com/png.latex?n"> is called the <em>exponent</em> and the real number <img src="https://latex.codecogs.com/png.latex?m"> is called the <em>mantissa</em>. If the number is negative, then a minus sign precedes <img src="https://latex.codecogs.com/png.latex?m">, as in ordinary decimal notation.</p>
</div>
</div>
<p>In scientific notation, the number 4600 can be written using a different number of significant figures, based on rule 3. above:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A4.600%5Ctimes%2010%5E3&amp;%5Cquad%5Ctext%7Bfour%20significant%20figures%7D%5C%5C%0A4.60%5Ctimes%2010%5E3&amp;%5Cquad%5Ctext%7Bthree%20significant%20figures%7D%5C%5C%0A4.6%5Ctimes%2010%5E3&amp;%5Cquad%5Ctext%7Btwo%20significant%20figures%7D%0A%5Cend%7Bsplit%7D%0A"></p>
</section>
<section id="rounding" class="level3">
<h3 class="anchored" data-anchor-id="rounding">Rounding</h3>
<p>A number can be <em>rounded</em> so as to drop digits until a prescribed number of significant figures is retained in the final representation. Recall that all the digits after the decimal point to the right of the desired LSD are to be dropped and not replaced with zeros, which otherwise should add to the number of significant figures (rule 3 above). The rules of rounding are the following:</p>
<ol type="1">
<li>if the digit to the right of the desired LSD is greater than 5, add 1 to the LSD, otherwise do nothing</li>
</ol>
<ul>
<li>Example - round at the fourth significant figure</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A53.8%5Cunderline%7B7%7D4&amp;%5Crightarrow53.87%5C%5C%0A53.8%5Cunderline%7B7%7D9&amp;%5Crightarrow53.88%0A%5Cend%7Bsplit%7D%0A"></p>
<ol start="2" type="1">
<li>if the digit to the right of the LSD is 5, apply a tie-breaking rule, also called the <em>five rule</em>. When the first digit to be dropped is 5, the leading digit next to it is examined. If this digit is even, including zero, it is left unaltered; otherwise, one unit is added. This helps avoiding the accumulation of errors that would be otherwise determined by rounding systematically up or down. Using the five rule, five out of ten cases consist of rounding up and five out of ten cases consist of rounding down.</li>
</ol>
<ul>
<li>Example - round at the fifth significant figure</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A726.8%5Cunderline%7B0%7D51%5Crightarrow%20726.80%5C%5C%0A726.8%5Cunderline%7B3%7D51%5Crightarrow%20726.84%5C%5C%0A726.8%5Cunderline%7B6%7D51%5Crightarrow%20726.86%5C%5C%0A726.8%5Cunderline%7B9%7D51%5Crightarrow%20726.90%0A%5Cend%7Bsplit%7D%0A"></p>
</section>
</section>
<section id="finite-precision-arithmetic" class="level2">
<h2 class="anchored" data-anchor-id="finite-precision-arithmetic">Finite precision arithmetic</h2>
<p>In mathematical operations involving significant figures, the result cannot be more precise than the least precise number. Calculations in finite precision arithmetic can be done following a few simple rules. One rule applies to multiplication and division, and another rule applies to addition and subtraction. Recall that values that are considered exact numbers, e.g., known conversion factors or physical constants, are not to be included in the determination of the number of significant figures.</p>
<section id="multiplication-and-division" class="level3">
<h3 class="anchored" data-anchor-id="multiplication-and-division">Multiplication and division</h3>
<blockquote class="blockquote">
<p>When we multiply/divide two numbers, we should add their relative uncertainties. The uncertainty of the result is given roughly by the number of the digits, regardless of their placement.</p>
</blockquote>
<p>In a calculation involving multiplication/division the number of significant figures in the result should equal the least number of significant figures in any one of the numbers being multiplied or divided.</p>
<p>In the following example, the number 1.6 is reported with two significant figures; the number 2, seen as a known constant, can be considered having an infinite number of significant figures, whereas the number 2.0 has two significant figures. The result should be reported with two significant figures in both cases:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A&amp;1.6%5Ctimes2=3.2&amp;%5C%5C%0A&amp;1.61%5Ctimes2.0=3.2&amp;%5Cquad%5Ctext%7Bnot%7D%5C;3.22%0A%5Cend%7Bsplit%7D%0A"></p>
</section>
<section id="addition-and-subtraction" class="level3">
<h3 class="anchored" data-anchor-id="addition-and-subtraction">Addition and subtraction</h3>
<blockquote class="blockquote">
<p>When we add/subtract two numbers, we should add their uncertainties. The uncertainty of the result is given roughly by the placement of the digits, not by the number of digits.</p>
</blockquote>
<p>In a calculation involving addition/subtraction, the number of decimal places in the result should equal the least number of decimal places in any one of the numbers being added or subtracted.</p>
<p>In the following example, the number 132.03 is reported with five significant figures, and the number 3.210 is reported with four significant figures. However, when the two numbers are added, what matters really is the number of decimal places, i.e., two for the number 132.03 and three for the number 3.210. The result should be reported with two decimal digits and not reported using four significant figures.</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A&amp;132.03+3.210=135.24&amp;%5C;%5Ctext%7Band%20not%7D%5C;135.2%5C%5C%0A&amp;132.03+3=135&amp;%5C;%5Ctext%7Band%20not%7D%5C;135.03%5C%5C%0A&amp;132.03+3.00=135.03&amp;%5C;%5Ctext%7Band%20not%7D%5C;135%5C%5C%0A%5Cend%7Bsplit%7D%0A"></p>
<p>The prescription about the minimum number of decimal places of any of the numbers involved in the calculation can be explained by considering that, implicitly, the precision of any measurement is dictated by the decimal place. For a measurement of length expressed in meters, for example, the second decimal digit implies a measurement precise to the hundredths (centimeter-level), the third decimal digit to the thousandth (millimeter-level). So by keeping the result with the minimum number of decimal places we basically state that we do not want to imply to get a result more precise than the least precise measurement that was needed to produce the result itself.</p>
</section>
<section id="multiple-arithmetic-operations" class="level3">
<h3 class="anchored" data-anchor-id="multiple-arithmetic-operations">Multiple arithmetic operations</h3>
<blockquote class="blockquote">
<p>In a calculation involving multiple arithmetic operations, the rules are applied without rounding results after each intermediate step. Instead keep track of the rightmost digit that would be retained. The operations would be performed in the following order:</p>
</blockquote>
<ul>
<li><p>operations in parentheses ( )</p></li>
<li><p>multiplication</p></li>
<li><p>division</p></li>
<li><p>addition</p></li>
<li><p>subtraction</p></li>
</ul>
<p>It is important to always perform intermediate calculations without rounding the numbers that are involved in the operations. If numbers are rounded every time during many sequential calculations, the results are skewed and some systematic error is surely introduced. Only after that all calculations are carried out with all digits retained at each step, the final result has to be rounded to the desired number of significant figures.</p>
<p>As an example, two numbers reported with five significant figures each are added, and the final result is rounded to three significant figures. If the addends are first rounded to three significant figures and then added, the result we produce is wrong:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A&amp;1.4248+1.2732=2.6980%5Crightarrow%202.70&amp;%5Cquad%5Ctext%7Bcorrect%7D%5C%5C%0A&amp;1.42+1.27=2.69%5Crightarrow%202.70&amp;%5Cquad%5Ctext%7Bwrong%7D%0A%5Cend%7Bsplit%7D%0A"></p>
<div id="exm-exm1" class="theorem example">
<p><span class="theorem-title"><strong>Example 1 (Sequential calculation)</strong></span> Suppose that we want to perform the following operation:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A(2.5%5Ctimes3.42)+13.681-0.1%0A"></p>
<ol type="1">
<li>perform first the product between parentheses - we keep track of the first decimal place, which would be retained based on rule B above.</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0A2.5%5Ctimes3.42=8.%5Cunderline%7B5%7D500%0A"></p>
<ol start="2" type="1">
<li>perform addition - although, based on rule A above, the result would be expressed using five significant figures, only the first decimal place is kept tracked:</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0A8.5500+13.681=22.%5Cunderline%7B2%7D310%0A"></p>
<ol start="3" type="1">
<li>perform subtraction:</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0A22.2310-0.1=22.%5Cunderline%7B1%7D31%0A"></p>
<ol start="4" type="1">
<li>rounding to three significant figures:</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0A(2.5%5Ctimes3.42)+13.681-0.1%5Crightarrow22.1%0A"></p>
</div>
<p>When doing multi-step calculations, we need:</p>
<ul>
<li><p>to keep at least one more significant figure in intermediate results than needed in the final answer. Furthermore, never round intermediate answers: rounding, say, to two significant figures in an intermediate answer, and then writing three significant figures in the final answer is wrong.</p></li>
<li><p>not to write more significant figures in the final result (of a measurement process) than justified (by the measurement uncertainty).</p></li>
</ul>
</section>
</section>
<section id="significant-figures-and-measurement-uncertainty" class="level2">
<h2 class="anchored" data-anchor-id="significant-figures-and-measurement-uncertainty">Significant figures and measurement uncertainty</h2>
<p>The value of one measurand must be delivered by rounding the digit loaded by the measurement uncertainty <img src="https://latex.codecogs.com/png.latex?U">, where <img src="https://latex.codecogs.com/png.latex?U"> is represented by a number with, usually, no more than one or two significant figures (rounded up, possibly). The additional uncertainty due to rounding must be checked for being negligible compared to <img src="https://latex.codecogs.com/png.latex?U">. Essentially, <img src="https://latex.codecogs.com/png.latex?U"> gives an estimate of the errors incurred in the measurement.</p>
<p>For example, if we have a length <img src="https://latex.codecogs.com/png.latex?L=(12.37%5Cpm0.10)%5C;%5Ctext%7Bcm%7D">, we can report the length as <img src="https://latex.codecogs.com/png.latex?L=12.4%5C;%5Ctext%7Bcm%7D">. When we express a number with three significant figures, what we are saying is that the first two digits are essentially exactly correct, and the last one is uncertain by a small amount (generally it is only uncertain by about <img src="https://latex.codecogs.com/png.latex?%5Cpm1">). In the example above, we rounded our answer to <img src="https://latex.codecogs.com/png.latex?12.4%5C;%5Ctext%7Bcm%7D"> because our answer is uncertain to <img src="https://latex.codecogs.com/png.latex?%5Cpm0.1%5C;%5Ctext%7Bcm%7D">, namely our answer is uncertain in the last digit by about 1.</p>
<div id="exm-exm2" class="theorem example">
<p><span class="theorem-title"><strong>Example 2 (Rounding)</strong></span> Round the measurement <img src="https://latex.codecogs.com/png.latex?z=12.0349%5C;%5Ctext%7Bcm%7D">, whose uncertainty is stated being <img src="https://latex.codecogs.com/png.latex?%5CDelta%20z=0.153%5C;%5Ctext%7Bcm%7D">.</p>
<ol type="1">
<li>round the uncertainty to two significant figures:</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5CDelta%20z=0.15%5C,%5Ctext%7Bcm%7D%0A"></p>
<ol start="2" type="1">
<li>round <img src="https://latex.codecogs.com/png.latex?z"> using the same number of decimal places as <img src="https://latex.codecogs.com/png.latex?%5CDelta%20z">:</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0Az=12.03%5C,%5Ctext%7Bcm%7D%0A"></p>
<ol start="3" type="1">
<li>provide the measurement report:</li>
</ol>
<p><img src="https://latex.codecogs.com/png.latex?%0Az%5Cpm%5CDelta%20z=(12.03%5Cpm0.15)%5C,%5Ctext%7Bcm%7D%0A"></p>
</div>
<div id="exm-exm3" class="theorem example">
<p><span class="theorem-title"><strong>Example 3 (Use of the scientific notation)</strong></span> When the answer is given in scientific notation, the uncertainty should be given in scientific notation with the same power of ten as the answer. Suppose that <img src="https://latex.codecogs.com/png.latex?z=1.43%5Ctimes10%5E6%5C;%5Ctext%7Bs%7D"> and <img src="https://latex.codecogs.com/png.latex?%5CDelta%20z=2%5Ctimes10%5E4%5C;%5Ctext%7Bs%7D">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Az%5Cpm%5CDelta%20z=(1.43%5Cpm0.02)%5C,10%5E6%5C;%5Ctext%7Bs%7D%0A"></p>
</div>
<div id="exm-exm4" class="theorem example">
<p><span class="theorem-title"><strong>Example 4 (Addition/subtraction of uncertain numbers)</strong></span> The length of two blocks is measured, and the measurements are <img src="https://latex.codecogs.com/png.latex?l_1=1.13%5C;%5Ctext%7Bm%7D"> (considered precise to the level of centimeters) and <img src="https://latex.codecogs.com/png.latex?l_2=0.551%5C;%5Ctext%7Bm%7D"> (considered precise to the level of millimeters). We need to compute the length <img src="https://latex.codecogs.com/png.latex?l"> of the block resulting from stacking the two blocks together:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Al=l_1+l_2=1.681%5C;%5Ctext%7Bm%7D%5Crightarrow%20l=1.68%5C;%5Ctext%7Bm%7D%0A"></p>
<p>It does not make any physical sense to consider the length of the overall block precise to the level of the millimeters, given that one of the two blocks is measured less precisely. The result should be at least as precise as the least precise term involved in the addition, as stated by the rule for addition/subtraction of uncertain numbers.</p>
</div>
<div id="exm-exm5" class="theorem example">
<p><span class="theorem-title"><strong>Example 5 (Multiplication/division of uncertain numbers)</strong></span> A rectangular floor needs to be covered by a number of squared tiles. According to the measurements that are available, the rectangular floor has width <img src="https://latex.codecogs.com/png.latex?w=1.91%5C;%5Ctext%7Bm%7D"> and length <img src="https://latex.codecogs.com/png.latex?l=1.57%5C;%5Ctext%7Bm%7D"> and each squared tile has size <img src="https://latex.codecogs.com/png.latex?a=0.15%5C;%5Ctext%7Bm%7D">. All measurements are considered precise to the level of centimeters, and three significant figures should then be considered for their numerical representation. The number of tiles can be easily calculated:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AN%5Capprox%5Cdfrac%7Bw%5C,l%7D%7Ba%5E2%7D=133.2756%5C;%5Ctext%7Bm%7D%5E2%5C,%5Ctext%7Bm%7D%5E%7B-2%7D%0A"></p>
<p>To comply with the rule for multiplication/division between uncertain numbers, <img src="https://latex.codecogs.com/png.latex?133.2756"> has to be rounded using three significant figures, yielding the integer <img src="https://latex.codecogs.com/png.latex?N=133">, which is then the number of tiles expected to cover the floor.</p>
</div>
<div id="exm-exm6" class="theorem example">
<p><span class="theorem-title"><strong>Example 6 (Conversion of scale)</strong></span> A measurement of temperature is performed, leading to the following report:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AT_c=(54.0%5Cpm0.5)%5C;%5E%7B%5Ccirc%7D%5Ctext%7BC%7D%0A"></p>
<p>We want to convert this expression in units of kelvin:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AT_K=T_C+273.15=(327.15%5Cpm0.5)%5C;%5Ctext%7BK%7D%0A"></p>
<p>The uncertainty expressed in degree Celsius (<img src="https://latex.codecogs.com/png.latex?%5Cpm0.5%5C;%5E%7B%5Ccirc%7D%5Ctext%7BC%7D">) translates directly in the uncertainty expressed in kelvin (<img src="https://latex.codecogs.com/png.latex?%5Cpm%200.5%5C;%5Ctext%7BK%7D">). This is because transforming a measurement expressed in degree Celsius into a measurement expressed in kelvin implies a change of offset, but not a change of scale. Since the uncertainty loads the first decimal digit of the numerical representation of the measured temperature, we should report <img src="https://latex.codecogs.com/png.latex?T_K"> with one decimal digit, which requires rounding (based on the five rule):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AT_k=(327.2%5Cpm0.5)%5C;%5Ctext%7BK%7D%0A"></p>
<p>The prescription of the minimum number of significant figures (rule for the addition/subtraction of uncertain numbers) would yield <img src="https://latex.codecogs.com/png.latex?T_k=327%5C;%5Ctext%7BK%7D">. This is because <img src="https://latex.codecogs.com/png.latex?54.0"> has three significant figures, and <img src="https://latex.codecogs.com/png.latex?273.15"> can be considered to have an infinite number of significant figures, since it is a known constant; hence <img src="https://latex.codecogs.com/png.latex?T_K"> should be reported with three significant figures according to the rule for addition/subtraction of uncertain numbers. However, this rule is superseded by considering the prescription concerning how to express the measurement uncertainty.</p>
</div>
<blockquote class="blockquote">
<p>The concept of significant figures and their relation with measurement uncertainty has been briefly reviewed. This topic is important because many measured quantities are often reported with more significant figures than necessary, in the face of the loaded uncertainty. Reporting too many digits is confusing for the reader and of no relevance as for the information content associated to the measurements.</p>
</blockquote>


</section>

 ]]></description>
  <category>measurement</category>
  <guid>https://amsabatini.netlify.app/posts/significant_figures/</guid>
  <pubDate>Mon, 13 May 2024 22:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/significant_figures/significant_figures.png" medium="image" type="image/png" height="150" width="144"/>
</item>
<item>
  <title>Augmented Dickey-Fuller test</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/dickey_fuller/</link>
  <description><![CDATA[ 




<section id="stationary-time-series" class="level2">
<h2 class="anchored" data-anchor-id="stationary-time-series">Stationary time series</h2>
<p>Roughly, for a time series to be stationary three conditions are needed:</p>
<ul>
<li><p>it shows mean reversion, namely it fluctuates around a constant long-term mean</p></li>
<li><p>it has finite variance that is time-invariant.</p></li>
<li><p>autocorrelations decay relatively fast as lag lenghts increase.</p></li>
</ul>
<p>The identification of stationary series can be done by checking whether the autocorrelation function (ACF) drops to zero relatively quickly; usually, the ACF of non-stationary data decreases slowly, and the first-lag value is often large and positive. However, this method is necessarily imprecise, leading to ambiguous situations that cannot be easily deciphered, especially when the sample size is small.</p>
<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">
Backshift notation
</div>
</div>
<div class="callout-body-container callout-body">
<p>The backward shift operator <img src="https://latex.codecogs.com/png.latex?B"> is defined as follows:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0ABY_t=Y_%7Bt-1%7D%0A"></p>
<p>The operator <img src="https://latex.codecogs.com/png.latex?B"> operates on the <img src="https://latex.codecogs.com/png.latex?t">th sample of a time series, with the effect of shifting the sample back one period (first difference). Recall that two applications of the backward shift operator to <img src="https://latex.codecogs.com/png.latex?Y_t"> shift the sample back two periods (second difference):</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AB(BY_t)=B%5E2Y_t=Y_%7Bt-2%7D%0A"></p>
<p>For example, in the case of monthly data, if we wish to consider <em>the same month last year</em>, the notation is <img src="https://latex.codecogs.com/png.latex?B%5E%7B12%7DY_t=Y_%7Bt-12%7D">.</p>
<p>The backward shift operator is useful to describe the operation of <strong>differencing</strong>, the technique of election to stabilize the mean value of nonstationary time series by removing changes in the level of a time series, and therefore eliminating (or reducing) trend and seasonality.</p>
<p>A first-order difference is defined as follows:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AY%5E%7B%5Cprime%7D_t=Y_t-Y_%7Bt-1%7D=Y_t-BY_t=(1-B)Y_t%0A"></p>
<p>If a second-order difference is considered, namely the first-order difference of a first-order difference, we can write:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0AY%5E%7B%5Cprime%5Cprime%7D_t&amp;=Y%5E%7B%5Cprime%7D_t-Y%5E%7B%5Cprime%7D_%7Bt-1%7D%5C%5C%0A&amp;=(1-B)%5E2Y_t=Y_t-2BY_t+B%5E2Y_t%5C%5C%0A&amp;=Y_t-2Y_%7Bt-1%7D+Y_%7Bt-2%7D%0A%5Cend%7Bsplit%7D%0A"></p>
<p>A <img src="https://latex.codecogs.com/png.latex?d">-order difference can be written <img src="https://latex.codecogs.com/png.latex?(1-B)%5Ed">. It is important to note that a second-order difference, denoted by <img src="https://latex.codecogs.com/png.latex?(1-B)%5E2">, is not the same as a second difference, which is denoted by <img src="https://latex.codecogs.com/png.latex?B%5E2">.</p>
<p>For example, a seasonal difference followed by a first difference can be written:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0AY%5E%7B%5Cprime%7D_t&amp;=(1-B)(1-B%5E%7B12%7D)Y_t%5C%5C%0A&amp;=(1-B-B%5E%7B12%7D-B%5E%7B13%7D)Y_t%5C%5C%0A&amp;=Y_t-Y_%7Bt-1%7D-Y_%7Bt-12%7D-Y_%7Bt-13%7D%0A%5Cend%7Bsplit%7D%0A"></p>
<p>Sometimes, as I will always do in the following of this post, the notation <img src="https://latex.codecogs.com/png.latex?%5CDelta"> is also used to indicate the first-order difference, i.e., <img src="https://latex.codecogs.com/png.latex?%5CDelta%20Y_t=(1-B)Y_t">.</p>
</div>
</div>
<p>A <strong>unit root process</strong>, also called difference-stationary process (DSP), is a data-generating process whose first difference is stationary.</p>
<p>There are two basic models for time series with linear growth characteristics:</p>
<ul>
<li>Trend stationary process</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0AY_t=c+%5Cdelta%20t+%5Ctext%7Bstationary%20process%7D%0A"></p>
<ul>
<li>Unit root process</li>
</ul>
<p><img src="https://latex.codecogs.com/png.latex?%0AY_t=Y_%7Bt-1%7D+%5Ctext%7Bstationary%20process%7D%0A"></p>
<p>The processes are indistinguishable for short data records, in the sense that both a trend stationary process (TSP) and a DSP can fit short data records extremely well. However, the processes can be distinguished when restricted to a particular subclass of data-generating processes, such as AR(<img src="https://latex.codecogs.com/png.latex?p">) processes.</p>
<p>Consider the case of an AR(1) process:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0AY_t=a_1Y_%7Bt-1%7D+%5Cepsilon_t%0A"></p>
<p>We may be interested in testing the hypothesis <img src="https://latex.codecogs.com/png.latex?a_1=1">. Since under the null hypothesis the sequence <img src="https://latex.codecogs.com/png.latex?%5C%7BY_t%5C%7D"> is generated by a nonstationary process, and the variance becomes infinitely large as <img src="https://latex.codecogs.com/png.latex?t"> increases, classical statistical methods cannot be used.</p>
<p>Dickey and Fuller devised a procedure to formally test for the presence of a unit root against a number of possible alternatives for explaining the data. It is important to recall that TSP and DSP can produce different forecasts and can give rise to spurious regressions, therefore their detection in time series is a truly important task.</p>
</section>
<section id="augmented-dickey-fuller-test" class="level2">
<h2 class="anchored" data-anchor-id="augmented-dickey-fuller-test">Augmented Dickey-Fuller test</h2>
<p>A distinction between stationary and nonstationary time series is made by formal statistical procedures such as the ADF (Augmented Dickey-Fuller) test, which is frequently used since it also accounts for serial correlation in the time series.</p>
<p>Three specifications of the ADF test are based on the following regression equations.</p>
<section id="type-1-unit-root-with-none" class="level4">
<h4 class="anchored" data-anchor-id="type-1-unit-root-with-none">Type 1 (unit root with “none”)</h4>
<p><span id="eq-eq8"><img src="https://latex.codecogs.com/png.latex?%0A%5CDelta%20Y_t=%5Cgamma%20Y_%7Bt-1%7D+%5Cunderbrace%7B%5Csum_%7Bi=2%7D%5Ep%5Cbeta_i%5CDelta%20Y_%7Bt-i+1%7D%7D_%7B%5Ctext%7Bserial%20correlation%7D%7D+%5Cepsilon_t%0A%5Ctag%7B1%7D"></span></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Cepsilon_t"> is white Gaussian noise, stationary with zero mean and constant variance. The statistical hypothesis test is:</p>
<p><span id="eq-eq9"><img src="https://latex.codecogs.com/png.latex?%0A(%5Ctau_1)%5Crightarrow%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7DH_0:&amp;%5Cquad%5Cgamma=0%5C%5C%0AH_1:&amp;%5Cquad%5Cgamma%5Cneq%200%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B2%7D"></span></p>
<p>The null hypothesis prescribes the existence of a unit-root in the model. Rejection of the null implies that the original time series <em>does not have a unit root</em>.</p>
</section>
<section id="type-2-unit-root-with-drift" class="level4">
<h4 class="anchored" data-anchor-id="type-2-unit-root-with-drift">Type 2 (unit root with “drift”)</h4>
<p><span id="eq-eq10"><img src="https://latex.codecogs.com/png.latex?%0A%5CDelta%20Y_t=%5Cgamma%20Y_%7Bt-1%7D+%5Cunderbrace%7Ba_0%7D_%7B%5Ctext%7Bdrift%7D%7D+%5Cunderbrace%7B%5Csum_%7Bi=2%7D%5Ep%5Cbeta_i%5CDelta%20Y_%7Bt-i+1%7D%7D_%7B%5Ctext%7Bserial%20correlation%7D%7D+%5Cepsilon_t%0A%5Ctag%7B3%7D"></span></p>
<p>The statistical hypothesis tests are:</p>
<p><span id="eq-eq11"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A&amp;(%5Cphi_1)%5Crightarrow%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7DH_0:&amp;%5Cquad%5Cgamma=0%5C;%5Ctext%7Band%7D%5C;a_0=0%5C%5C%0AH_1:&amp;%5Cquad%5Cgamma%5Cneq%200%5C;%5Ctext%7B%20%20or%7D%5C;%5C;%5C,a_0%5Cneq0%0A%5Cend%7Bsplit%7D%0A%5Cright.%5C%5C%0A&amp;%5C%5C%0A&amp;(%5Ctau_2)%5Crightarrow%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7DH_0:&amp;%5Cquad%5Cgamma=0%5C%5C%0AH_1:&amp;%5Cquad%5Cgamma%5Cneq%200%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Cend%7Bsplit%7D%0A%5Ctag%7B4%7D"></span></p>
</section>
<section id="type-3-unit-root-with-drift-and-trend" class="level4">
<h4 class="anchored" data-anchor-id="type-3-unit-root-with-drift-and-trend">Type 3 (unit root with “drift and trend”)</h4>
<p><span id="eq-eq12"><img src="https://latex.codecogs.com/png.latex?%0A%5CDelta%20Y_t=%5Cgamma%20Y_%7Bt-1%7D+%5Cunderbrace%7Ba_0%7D_%7B%5Ctext%7Bdrift%7D%7D+%5Cunderbrace%7Ba_2t%7D_%7B%5Ctext%7Btrend%7D%7D+%5Cunderbrace%7B%5Csum_%7Bi=2%7D%5Ep%5Cbeta_i%5CDelta%20Y_%7Bt-i+1%7D%7D_%7B%5Ctext%7Bserial%20correlation%7D%7D+%5Cepsilon_t%0A%5Ctag%7B5%7D"></span></p>
<p>The statistical hypothesis tests are:</p>
<p><span id="eq-eq13"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A&amp;(%5Cphi_2)%5Crightarrow%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7DH_0:&amp;%5Cquad%5Cgamma=0%5C;%5Ctext%7Band%7D%5C;a_0=0%5C;%5Ctext%7Band%7D%5C;a_2=0%5C%5C%0AH_1:&amp;%5Cquad%5Cgamma%5Cneq%200%5C;%5Ctext%7B%20%20or%7D%5C;%5C;%5C,a_0%5Cneq0%5C;%5Ctext%7B%20%20or%7D%5C;%5C;%5C,a_2%5Cneq0%5C%5C%0A%5Cend%7Bsplit%7D%0A%5Cright.%5C%5C%0A&amp;%5C%5C%0A&amp;(%5Cphi_3)%5Crightarrow%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7DH_0:&amp;%5Cquad%5Cgamma=0%5C;%5Ctext%7Band%7D%5C;a_2=0%5C%5C%0AH_1:&amp;%5Cquad%5Cgamma%5Cneq%200%5C;%5Ctext%7B%20%20or%7D%5C;%5C;%5C,a_2%5Cneq0%0A%5Cend%7Bsplit%7D%0A%5Cright.%5C%5C%0A&amp;%5C%5C%0A&amp;(%5Ctau_3)%5Crightarrow%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7DH_0:&amp;%5Cquad%5Cgamma=0%5C%5C%0AH_1:&amp;%5Cquad%5Cgamma%5Cneq%200%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Cend%7Bsplit%7D%0A%5Ctag%7B6%7D"></span></p>
<p>Each of the six tests <img src="https://latex.codecogs.com/png.latex?(%5Ctau_1),(%5Ctau_2),(%5Ctau_3),(%5Cphi_1),(%5Cphi_2),(%5Cphi_3)"> in Equation&nbsp;2, Equation&nbsp;4 and Equation&nbsp;6 corresponds to a progressively more complex linear regression. In all of them there is the root, but in the “drift” model there is also a drift term, and in the “drift and trend” model there are also drift and trend terms. All the coefficients in the models have an associated significance level. While the significance of the root coefficient is the most important and the main focus of the ADF test, we might also be interested in knowing whether or not the drift and trend coefficients are statistically significant.</p>
<div class="callout callout-style-default callout-important 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">
Summary of the Dickey-Fuller tests
</div>
</div>
<div class="callout-body-container callout-body">
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0A&amp;%5Cbegin%7Bsplit%7D%0A&amp;%5Ctextbf%7BTrend%7D%5Cquad%5Cquad%5CDelta%20Y_t=%5Cgamma%20Y_%7Bt-1%7D+a_0+a_2t+%5Csum_%7Bi=2%7D%5Ep%5Cbeta_i%5CDelta%20Y_%7Bt-i+1%7D+%5Cepsilon_t%5C%5C%0A&amp;%5Cbegin%7Bsplit%7D%0A&amp;%5Ctext%7Bif%7D%5C;(%5Cphi_2)%5C;%5Ctext%7Bis%20rejected,%20unit%20root%20is%20NOT%20present%20OR%20there%20is%20trend%20OR%20there%20is%20drift%7D%5C%5C%0A&amp;%5Ctext%7Bif%7D%5C;(%5Cphi_2)%5C;%5Ctext%7Bfails%20to%20be%20rejected,%20unit%20root%20is%20present%20AND%20there%20is%20NO%20trend%20AND%20there%20is%20NO%20drift%7D%5C%5C%0A&amp;%5Ctext%7Bif%7D%5C;(%5Cphi_3)%5C;%5Ctext%7Bis%20rejected,%20unit%20root%20is%20NOT%20present%20OR%20there%20is%20trend%7D%5C%5C%0A&amp;%5Ctext%7Bif%7D%5C;(%5Cphi_3)%5C;%5Ctext%7Bfails%20to%20be%20rejected,%20unit%20root%20is%20present%20AND%20there%20is%20NO%20trend%7D%5C%5C%0A&amp;%5Ctext%7Bif%7D%5C;(%5Ctau_3)%5C;%5Ctext%7Bis%20rejected,%20unit%20root%20is%20NOT%20present%7D%5C%5C%0A&amp;%5Ctext%7Bif%7D%5C;(%5Ctau_3)%5C;%5Ctext%7Bfails%20to%20be%20rejected,%20unit%20root%20is%20present%7D%5C%5C%0A&amp;%5Cend%7Bsplit%7D%0A&amp;%5Cend%7Bsplit%7D%5C%5C%0A&amp;%5Cbegin%7Bsplit%7D%0A&amp;%5Ctextbf%7BDrift%7D%5Cquad%5Cquad%5CDelta%20Y_t=%5Cgamma%20Y_%7Bt-1%7D+a_0+%5Csum_%7Bi=2%7D%5Ep%5Cbeta_i%5CDelta%20Y_%7Bt-i+1%7D+%5Cepsilon_t%5C%5C%0A&amp;%5Cbegin%7Bsplit%7D%0A&amp;%5Ctext%7Bif%7D%5C;(%5Cphi_1)%5C;%5Ctext%7Bis%20rejected,%20unit%20root%20is%20NOT%20present%20OR%20there%20is%20drift%7D%5C%5C%0A&amp;%5Ctext%7Bif%7D%5C;(%5Cphi_1)%5C;%5Ctext%7Bfails%20to%20be%20rejected,%20unit%20root%20is%20present%20AND%20there%20is%20NO%20drift%7D%5C%5C%0A&amp;%5Ctext%7Bif%7D%5C;(%5Ctau_2)%5C;%5Ctext%7Bis%20rejected,%20unit%20root%20is%20NOT%20present%7D%5C%5C%0A&amp;%5Ctext%7Bif%7D%5C;(%5Ctau_2)%5C;%5Ctext%7Bfails%20to%20be%20rejected,%20unit%20root%20is%20present%7D%5C%5C%0A&amp;%5Cend%7Bsplit%7D%0A&amp;%5Cend%7Bsplit%7D%5C%5C%0A&amp;%5Cbegin%7Bsplit%7D%0A&amp;%5Ctextbf%7BNone%7D%5Cquad%5Cquad%5CDelta%20Y_t=%5Cgamma%20Y_%7Bt-1%7D+%5Csum_%7Bi=2%7D%5Ep%5Cbeta_i%5CDelta%20Y_%7Bt-i+1%7D+%5Cepsilon_t%5C%5C%0A&amp;%5Cbegin%7Bsplit%7D%0A&amp;%5Ctext%7Bif%7D%5C;(%5Ctau_1)%5C;%5Ctext%7Bis%20rejected,%20unit%20root%20is%20NOT%20present%7D%5C%5C%0A&amp;%5Ctext%7Bif%7D%5C;(%5Ctau_1)%5C;%5Ctext%7Bfails%20to%20be%20rejected,%20unit%20root%20is%20present%7D%0A&amp;%5Cend%7Bsplit%7D%0A&amp;%5Cend%7Bsplit%7D%0A%5Cend%7Bsplit%7D%0A"></p>
</div>
</div>
<p>An important extension of the ADF test concerns the case when the noise error term is not white. If the error term is not white and we run the ADF test as it is without accounting for serial correlation, many more rejection of the null tend to be produced than stated at the specified significance level. As the ADF test also deal with the serial correlation by introducing lagged terms, hence we need to select this lag order. This is accomplished by investigating several information criteria, including the autocorrelation function (ACF), but henceforth I limit to use the automatic lag selection functionality provided by the same function of R I will use for running the ADF test. Recall that, in general, the test statistics of the ADF test are very sensitive to changes in the lag structure.</p>
</section>
</section>
<section id="example" class="level2">
<h2 class="anchored" data-anchor-id="example">Example</h2>
<p>I use functions and data from the <code>urca</code> package. The data set contains the series that S. Johansen and K. Juselius considered for estimating the money demand function of Denmark <span class="citation" data-cites="johansen1990">(Johansen and Juselius 1990)</span>. A data frame with quarterly data from Denmark starting in 1974:Q1 until 1987:Q3 contains six variables, including the log real income <code>LRY</code>, whose evolution, together with that of its first difference, is shown in Figure&nbsp;1.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggpubr)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(grid)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(urca)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(zoo)</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(denmark)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attach</span>(denmark)</span>
<span id="cb1-9">denmark <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.data.frame</span>(denmark)</span>
<span id="cb1-10">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;">as.character</span>(ENTRY)</span>
<span id="cb1-11">k <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>(a)</span>
<span id="cb1-12"><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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">substring</span>(a[i], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">first =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">last =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">"-"</span></span>
<span id="cb1-13">ENTRY <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.yearqtr</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(a))</span>
<span id="cb1-14"></span>
<span id="cb1-15">df_ts  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> ENTRY, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> LRY)</span>
<span id="cb1-16">df_dts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> ENTRY[<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;">-1</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;">diff</span>(LRY))</span>
<span id="cb1-17"></span>
<span id="cb1-18">my_theme <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;">theme</span>(</span>
<span id="cb1-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>),</span>
<span id="cb1-20">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>),</span>
<span id="cb1-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>),</span>
<span id="cb1-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>),</span>
<span id="cb1-23">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>),</span>
<span id="cb1-24">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>),</span>
<span id="cb1-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-26">  <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-27">  <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-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-29">  <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_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>))</span>
<span id="cb1-30"></span>
<span id="cb1-31">p_ts <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_ts, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-32">  <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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-33">  <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;">""</span>,</span>
<span id="cb1-34">       <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;">"LRY"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-35">  my_theme</span>
<span id="cb1-36"></span>
<span id="cb1-37">p_dts <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_dts, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(x, y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-38">  <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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-39">  <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;">""</span>,</span>
<span id="cb1-40">       <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;">"first difference, LRY"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-41">  my_theme</span>
<span id="cb1-42"></span>
<span id="cb1-43"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.newpage</span>()</span>
<span id="cb1-44"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid.draw</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;">ggplotGrob</span>(p_ts), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplotGrob</span>(p_dts), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"last"</span>))</span></code></pre></div>
</details>
<div class="cell-output-display">
<div id="fig-fig1" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/dickey_fuller/index_files/figure-html/fig-fig1-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Time series to be tested for stationarity. On the left, the natural logarithm of real income vs.&nbsp;time; on the right its first difference.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The function <code>ur.df()</code> from the <code>urca</code> package performs the ADF test, with three types of models and related tests, named “none” (i.e., Equation&nbsp;12), “drift” (i.e., Equation&nbsp;34) and “trend” (Equation&nbsp;56). The argument <code>selectlags</code> in <code>ur.df()</code> allows to perform automatic selection of the lag structure according to a predefined criterion, as shown in the following code block.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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">mdl_none_ts   <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;">ur.df</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> df_ts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y, <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;">"none"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">selectlags =</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;">"BIC"</span>))</span>
<span id="cb2-2">mdl_drift_ts  <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;">ur.df</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> df_ts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y, <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;">"drift"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">selectlags =</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;">"BIC"</span>))</span>
<span id="cb2-3">mdl_trend_ts  <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;">ur.df</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> df_ts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y, <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;">"trend"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">selectlags =</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;">"BIC"</span>))</span>
<span id="cb2-4">mdl_none_dts  <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;">ur.df</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> df_dts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y, <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;">"none"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">selectlags =</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;">"BIC"</span>))</span>
<span id="cb2-5">mdl_drift_dts <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;">ur.df</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> df_dts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y, <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;">"drift"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">selectlags =</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;">"BIC"</span>))</span>
<span id="cb2-6">mdl_trend_dts <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;">ur.df</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> df_dts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y, <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;">"trend"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">selectlags =</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;">"BIC"</span>))</span>
<span id="cb2-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(mdl_trend_ts)</span></code></pre></div>
</details>
</div>
<p>The summary produced when the ADF test is applied to the original time series with the type “trend” is shown in Figure&nbsp;2.</p>
<div id="fig-fig2" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/dickey_fuller/summary.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="500">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Results of fitting the “trend” regression model in the ADF test when applied to the `LRY`variable from the `denmark` dataset.
</figcaption>
</figure>
</div>
<p>The part of interest for the analysis is within the rectangle highlighted in orange. The “Value of test-statistic is -2.4216 2.1927 2.9343” for tests <img src="https://latex.codecogs.com/png.latex?(%5Ctau_3),%20(%5Cphi_2)"> and <img src="https://latex.codecogs.com/png.latex?(%5Cphi_3)"> are given and the corresponding “Critical values for test statistics” at significance levels 1%, 5% and 10% are reported below, denoted by <code>tau3</code>, <code>phi2</code> and <code>phi3</code>, respectively. For instance, for the test <img src="https://latex.codecogs.com/png.latex?(%5Ctau_3)">, given that the test statistic -2.4216 is within the three regions -4.04, -3.45, -3.15 (1%, 5%, 10%) where we fail to reject the null, we do not have evidence to reject the presence of a unit root in the regression model: we can say that there is a unit root. From the <img src="https://latex.codecogs.com/png.latex?(%5Cphi_2)">-statistic, the joint null hypothesis is not rejected, therefore there is a unit root AND drift and trend terms are not needed. Similarly, the <img src="https://latex.codecogs.com/png.latex?(%5Cphi_3)">-statistic shows that there is a unit root AND a trend term is not needed and the <img src="https://latex.codecogs.com/png.latex?(%5Ctau_3)">-statistic shows that there is a unit root. A summary of test results for the variable <code>LRY</code> is reported within the rectangle highlighted in blue in Figure&nbsp;3.</p>
<div id="fig-fig3" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/dickey_fuller/summary_1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="400">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: Results of the three type of ADF tests for the variable `LRY`from the `denmark`dataset.
</figcaption>
</figure>
</div>
<p>A close examination of Figure&nbsp;3 shows that all the tests applied to data of the variable <code>LRY</code> are consistent with failing to reject the corresponding nulls. From the <img src="https://latex.codecogs.com/png.latex?(%5Cphi_1)">-statistic, the joint null hypothesis is not rejected, therefore there is a unit root AND the drift term is not needed, and the <img src="https://latex.codecogs.com/png.latex?(%5Ctau_2)">-statistic shows that there is a unit root. Finally, the <img src="https://latex.codecogs.com/png.latex?(%5Ctau_1)">-statistic also shows that there is a unit root.</p>
<p>On the other hand, all the tests applied to the first difference of the variable <code>LRY</code> reject the corresponding nulls, as shown in the same Figure&nbsp;3 (summary within the rectangle highlighted in red): although drift and trend terms might be presumed, this variable does not therefore contain a unit root.</p>
<blockquote class="blockquote">
<p>Finally, we can conclude that the logarithm of real income contains a unit root and can be a stationary time series by taking the first difference.</p>
</blockquote>



</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" data-entry-spacing="0">
<div id="ref-johansen1990" class="csl-entry">
Johansen, Søren, and Katarina Juselius. 1990. <span>“Maximum Likelihood Estimation and Inference on Cointegration <span></span> with Applications to the Demand for Money.”</span> <em>Oxford Bulletin of Economics and Statistics</em> 52 (2): 169–210. <a href="https://doi.org/10.1111/j.1468-0084.1990.mp52002003.x">https://doi.org/10.1111/j.1468-0084.1990.mp52002003.x</a>.
</div>
</div></section></div> ]]></description>
  <category>time series</category>
  <guid>https://amsabatini.netlify.app/posts/dickey_fuller/</guid>
  <pubDate>Sun, 12 May 2024 22:00:00 GMT</pubDate>
</item>
<item>
  <title>Gambler’s ruin</title>
  <dc:creator>Angelo Maria Sabatini</dc:creator>
  <link>https://amsabatini.netlify.app/posts/gambler/</link>
  <description><![CDATA[ 




<section id="problem" class="level2">
<h2 class="anchored" data-anchor-id="problem">Problem</h2>
<p>The gambler’s ruin problem, first mentioned by <a href="https://en.wikipedia.org/wiki/Blaise_Pascal">Blaise Pascal</a> in one of his letters to <a href="https://en.wikipedia.org/wiki/Pierre_de_Fermat">Pierre Fermat</a>, was then reformulated by <a href="https://en.wikipedia.org/wiki/Christiaan_Huygens">Christiaan Huygens</a>, leading to important advances in the mathematical theory of probability. The statement of the problem considered here is slightly different from the one described by either Pascal or Huygens <span class="citation" data-cites="noauthororeditor">(Bertsekas and Tsitsiklis 2008)</span>.</p>
<blockquote class="blockquote">
<p>Consider a gambler who starts with an initial wealth of €<img src="https://latex.codecogs.com/png.latex?k"> and then on each successive bet either wins €1 or loses €1 with probabilities <img src="https://latex.codecogs.com/png.latex?p"> and <img src="https://latex.codecogs.com/png.latex?q=1%E2%88%92p"> respectively. Different bets are assumed independent. The gambler’s objective is to reach a total wealth of €<img src="https://latex.codecogs.com/png.latex?n"> (<img src="https://latex.codecogs.com/png.latex?n%3Ek">). If the gambler succeeds, then the gambler is said to win the game. The gambler stops playing after winning or getting ruined (running out of money), whichever happens first.</p>
</blockquote>
<p>Calculate the probability that the gambler wins. In particular, which is the probability of the gambler becoming infinitely rich?</p>
</section>
<section id="solution" class="level2">
<h2 class="anchored" data-anchor-id="solution">Solution</h2>
<p>Let us denote by <img src="https://latex.codecogs.com/png.latex?A"> the event that the gambler ends up with €<img src="https://latex.codecogs.com/png.latex?n"> and by <img src="https://latex.codecogs.com/png.latex?F"> the event that the gambler wins the first bet. The probability of the event <img src="https://latex.codecogs.com/png.latex?A"> given that the gambler starts with €<img src="https://latex.codecogs.com/png.latex?k"> is written <img src="https://latex.codecogs.com/png.latex?w_k">. The total probability law allows us to write:</p>
<p><span id="eq-eq1"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bsplit%7D%0Aw_k&amp;=P(A%5C,%5Cvert%5C,F)P(F)+P(A%5C,%5Cvert%5C,%5Coverline%7BF%7D)P(%5Coverline%7BF%7D)%5C%5C%0A&amp;=pP(A%5C,%5Cvert%5C,F)+(1-p)P(A%5C,%5Cvert%5C,%5Coverline%7BF%7D)%0A%5Cend%7Bsplit%7D%0A%5Ctag%7B1%7D"></span></p>
<p>where <img src="https://latex.codecogs.com/png.latex?%5Coverline%7BF%7D"> is the complement of <img src="https://latex.codecogs.com/png.latex?F">, namely the event <img src="https://latex.codecogs.com/png.latex?%5Coverline%7BF%7D"> is that the gambler loses the first bet.</p>
<p>Now, the event <img src="https://latex.codecogs.com/png.latex?A%5C,%5Cvert%5C,F"> is the event <img src="https://latex.codecogs.com/png.latex?A"> given that the gambler starts with €<img src="https://latex.codecogs.com/png.latex?(k+1)">, whose probability is <img src="https://latex.codecogs.com/png.latex?w_%7Bk+1%7D"> (because of the independence from the past). The event <img src="https://latex.codecogs.com/png.latex?A%5C,%5Cvert%5C,%5Coverline%7BF%7D"> is the event <img src="https://latex.codecogs.com/png.latex?A"> given that the gambler starts with €<img src="https://latex.codecogs.com/png.latex?(k-1)">, whose probability is <img src="https://latex.codecogs.com/png.latex?w_%7Bk-1%7D"> (because of the independence from the past). Equation&nbsp;1 reads:</p>
<p><span id="eq-eq10"><img src="https://latex.codecogs.com/png.latex?%0Aw_k=p%5C,w_%7Bk+1%7D+(1-p)%5C,w_%7Bk-1%7D%0A%5Ctag%7B2%7D"></span></p>
<p>If we define <img src="https://latex.codecogs.com/png.latex?r=(1-p)/p">, we can also write:</p>
<p><span id="eq-eq2"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0A&amp;w_%7Bk+1%7D-w_k=r(w_k-w_%7Bk-1%7D),%5C;0%3Ck%3Cn%5C%5C%0A&amp;w_0=0%5C%5C%0A&amp;w_n=1%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B3%7D"></span></p>
<p>Since <img src="https://latex.codecogs.com/png.latex?w_%7Bk+1%7D=w_k+r%5Ek(w_1-w_0)=w_k+r%5Ekw_1=w_1%5Csum_%7Bi=0%7D%5Ekr%5Ei">, we can calculate <img src="https://latex.codecogs.com/png.latex?w_k"> as follows:</p>
<p><span id="eq-eq3"><img src="https://latex.codecogs.com/png.latex?%0Aw_k=%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0A&amp;%5Cfrac%7B1-r%5Ek%7D%7B1-r%7Dw_1&amp;%5Cquad%5Ctext%7Bif%7D%5C;r%5Cneq1%5C%5C%0A&amp;kw_1&amp;%5Cquad%5Ctext%7Bif%7D%5C;r=1%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B4%7D"></span></p>
<p>We also know that <img src="https://latex.codecogs.com/png.latex?w_n=1">. Therefore, we can solve Equation&nbsp;4 for <img src="https://latex.codecogs.com/png.latex?w_1">:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Aw_1=%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0A&amp;%5Cfrac%7B1-r%7D%7B1-r%5En%7D&amp;%5Cquad%5Ctext%7Bif%7D%5C;r%5Cneq1%5C%5C%0A&amp;%5Cfrac%7B1%7D%7Bn%7D&amp;%5Cquad%5Ctext%7Bif%7D%5C;r=1%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A"></p>
<p>Plugging this expression of <img src="https://latex.codecogs.com/png.latex?w_1"> into Equation&nbsp;4, we obtain:</p>
<p><span id="eq-eq4"><img src="https://latex.codecogs.com/png.latex?%0A%5Cboxed%7Bw_k=%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0A&amp;%5Cfrac%7B1-r%5Ek%7D%7B1-r%5En%7D&amp;%5Cquad%5Ctext%7Bif%7D%5C;r%5Cneq1%5C%5C%0A&amp;%5Cfrac%7Bk%7D%7Bn%7D&amp;%5Cquad%5Ctext%7Bif%7D%5C;r=1%0A%5Cend%7Bsplit%7D%0A%5Cright.%7D%0A%5Ctag%7B5%7D"></span></p>
<p>The probability that the gambler becomes infinitely rich (event <img src="https://latex.codecogs.com/png.latex?R">) is written:</p>
<p><span id="eq-eq5"><img src="https://latex.codecogs.com/png.latex?%0A%5Cleft%5C%7B%0A%5Cbegin%7Bsplit%7D%0A%5Ctext%7BPr%7D(R)&amp;=%5Clim_%7Bn%5Crightarrow%5Cinfty%7Dw_k=1-%5Cleft(%5Cfrac%7B1-p%7D%7Bp%7D%5Cright)%5Ek%3E0,&amp;%5Cquad%20p%3E1/2%5C;(r%3C1)%5C%5C%0A%5Ctext%7BPr%7D(R)&amp;=%5Clim_%7Bn%5Crightarrow%5Cinfty%7Dw_k=0,&amp;%5Cquad%20p%5Cleq1/2%5C;(r%5Cgeq1)%5C%5C%0A%5Cend%7Bsplit%7D%0A%5Cright.%0A%5Ctag%7B6%7D"></span></p>
<p>To interpret the meaning of Equation&nbsp;6, suppose that the gambler starting with an initial wealth of €<img src="https://latex.codecogs.com/png.latex?k"> wishes to continue gambling, with the intention of earning as much money as possible. So there is no winning value €<img src="https://latex.codecogs.com/png.latex?n">: the gambler will only stop if ruined. If <img src="https://latex.codecogs.com/png.latex?p%3E1/2"> (each gamble is in favor of the gambler), then there is a positive probability that the gambler will never get ruined but instead will become infinitely rich. Conversely, if <img src="https://latex.codecogs.com/png.latex?p%5Cleq0.5"> (each gamble is not in favor of the gambler), then the gambler will get ruined with probability one.</p>
</section>
<section id="markov-chain-model" class="level2">
<h2 class="anchored" data-anchor-id="markov-chain-model">Markov chain model</h2>
<p>The discrete-state discrete-time Markov chain show in Figure&nbsp;1 can be proposed to model the gambler process <span class="citation" data-cites="noauthororeditor">(Bertsekas and Tsitsiklis 2008)</span>. The <img src="https://latex.codecogs.com/png.latex?n+1"> states, numbered from 0 to <img src="https://latex.codecogs.com/png.latex?n">, represent the gambler’s wealth at the start of each bet. The first state 0 corresponds to ruin (getting out of money), the last state <img src="https://latex.codecogs.com/png.latex?n"> corresponds to win. Given the rules of the game, the states 0 and <img src="https://latex.codecogs.com/png.latex?n"> are absorbing, in the sense that it is not possible to escape from each of them anymore, once they are entered. All the other states are transient.</p>
<div id="fig-fig1" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/gambler/markov_chain.png" class="img-fluid quarto-figure quarto-figure-center figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig1-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Transition probability graph for the gambler’s ruin problem.
</figcaption>
</figure>
</div>
<p>The problem is then to find the probabilities of absorption at each one of the two absorbing states. These absorption probabilities depend on the initial state <img src="https://latex.codecogs.com/png.latex?k%5C;(0%3Ck%3Cn)">. It is worthy noting that the memoryless character of the process implies that, if the gambler happens to revisit the initial state <img src="https://latex.codecogs.com/png.latex?k"> after a while, the absorption probabilities are the same they were initially.</p>
<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">
Transition probability matrix
</div>
</div>
<div class="callout-body-container callout-body">
<p>The state of a discrete-state discrete-time Markov chain is denoted by <img src="https://latex.codecogs.com/png.latex?X_t">, that can change at certain discrete time instants <img src="https://latex.codecogs.com/png.latex?n">. The state space of the chain is composed of a finite set <img src="https://latex.codecogs.com/png.latex?%5Cmathscr%7BS%7D=%5C%7B1,%5Ccdots,m%5C%7D">. The Markov chain is described in terms of its transition probability <img src="https://latex.codecogs.com/png.latex?p_%7Bij%7D">:</p>
<p><span id="eq-eq6"><img src="https://latex.codecogs.com/png.latex?%0Ap_%7Bij%7D=P(X_%7Bt+1%7D=j%5C,%5Cvert%5C,X_t=i),%5Cquad%20i,j%5Cin%5Cmathscr%7BS%7D%0A%5Ctag%7B7%7D"></span></p>
<p>The key assumption underlying Markov chains is that the <strong>Markov property</strong> holds:</p>
<p><span id="eq-eq7"><img src="https://latex.codecogs.com/png.latex?%0AP(X_%7Bt+1%7D=j%5C,%5Cvert%5C,X_t=i,X_%7Bt-1%7D=i_%7Bt-1%7D,%5Ccdots,X_0=i_0)=P(X_%7Bt+1%7D=j%20%5C,%5Cvert%5C,X_t=i)=p_%7Bij%7D%0A%5Ctag%7B8%7D"></span></p>
<p>for all times <img src="https://latex.codecogs.com/png.latex?t">, all states <img src="https://latex.codecogs.com/png.latex?i,j%5Cin%5Cmathscr%7BS%7D"> and all possible sequences <img src="https://latex.codecogs.com/png.latex?i_0,i_1,%5Ccdots,i_%7Bt-1%7D"> of earlier states. Thus, the probability law of the next state <img src="https://latex.codecogs.com/png.latex?X_%7Bt+1%7D"> depends on the past only through the value of the present state <img src="https://latex.codecogs.com/png.latex?X_t">.</p>
<p>All of the elements of a Markov chain model can be encoded in a <strong>transition probability matrix</strong>, which is simply a two-dimensional array whose elements at the <img src="https://latex.codecogs.com/png.latex?i">th row and <img src="https://latex.codecogs.com/png.latex?j">th column is <img src="https://latex.codecogs.com/png.latex?P_%7Bij%7D">:</p>
<p><span id="eq-eq8"><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bbmatrix%7D%0Ap_%7B11%7D&amp;p_%7B12%7D&amp;%5Ccdots&amp;p_%7B1m%7D%5C%5C%0Ap_%7B21%7D&amp;p_%7B22%7D&amp;%5Ccdots&amp;p_%7B2m%7D%5C%5C%0A%5Cvdots&amp;%5Cvdots&amp;%5Cvdots&amp;%5Cvdots%5C%5C%0Ap_%7Bm1%7D&amp;p_%7Bm2%7D&amp;%5Ccdots&amp;p_%7Bmm%7D%0A%5Cend%7Bbmatrix%7D%0A%5Ctag%7B9%7D"></span></p>
<p>Consider a Markov chain in which each state is either transient or absorbing. We fix a particular absorbing state <img src="https://latex.codecogs.com/png.latex?s">. It is possible to show that the probabilities <img src="https://latex.codecogs.com/png.latex?w_k"> of eventually reaching state <img src="https://latex.codecogs.com/png.latex?s">, starting from <img src="https://latex.codecogs.com/png.latex?k">, are the unique solution of the equations <img src="https://latex.codecogs.com/png.latex?w_s=1,%20w_i=0">, for all absorbing <img src="https://latex.codecogs.com/png.latex?i%5Cneq%20s">, and for all transient <img src="https://latex.codecogs.com/png.latex?i">:</p>
<p><span id="eq-eq9"><img src="https://latex.codecogs.com/png.latex?%0Aw_i=%5Csum_%7Bj=1%7D%5Emp_%7Bij%7Dw_j%0A%5Ctag%7B10%7D"></span></p>
</div>
</div>
<div id="exm-exm1" class="theorem example">
<p><span class="theorem-title"><strong>Example 1 (Gambler’s discrete-time Markov chain)</strong></span> For <img src="https://latex.codecogs.com/png.latex?n=4,p,q=1-p"> the transition probability matrix of the gambler’s discrete-time Markov chain is given by:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Cbegin%7Bbmatrix%7D%0A1&amp;0&amp;0&amp;0&amp;0%5C%5C%0Aq&amp;0&amp;p&amp;0&amp;0%5C%5C%0A0&amp;q&amp;0&amp;p&amp;0%5C%5C%0A0&amp;0&amp;q&amp;0&amp;p%5C%5C%0A0&amp;0&amp;0&amp;0&amp;1%0A%5Cend%7Bbmatrix%7D%0A"></p>
<p>In the case of the gambler’s ruin problem, Equation&nbsp;10 can be reformulated as follows:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Aw_k=(1-p)%5C,w_%7Bk-1%7D+p%5C,w_%7Bk+1%7D%0A"></p>
<p>with <img src="https://latex.codecogs.com/png.latex?w_0=0,w_n=1">, leading to Equation&nbsp;2.</p>
</div>
</section>
<section id="simulation" class="level2">
<h2 class="anchored" data-anchor-id="simulation">Simulation</h2>
<p>The following code written in R simulates the gambler process (adapted from <span class="citation" data-cites="dobrow2016">(Dobrow 2016)</span>).</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2001</span>)</span>
<span id="cb1-2">gambler <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>(k, n, p) {</span>
<span id="cb1-3">  stake <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> k</span>
<span id="cb1-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> (stake <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> stake <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> n) {</span>
<span id="cb1-5">        bet <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;">sample</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="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="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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>p, p))</span>
<span id="cb1-6">        stake <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> stake <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bet</span>
<span id="cb1-7">    }</span>
<span id="cb1-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (stake <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> n) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</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;">else</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb1-9">}   </span>
<span id="cb1-10">k <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;">15</span>         <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># initial wealth</span></span>
<span id="cb1-11">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;">120</span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># final wealth</span></span>
<span id="cb1-12">p <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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># probability of win at each bet</span></span>
<span id="cb1-13">ntrials <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of independent trials</span></span>
<span id="cb1-14"></span>
<span id="cb1-15">sim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">replicate</span>(ntrials, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gambler</span>(k, n, p))</span>
<span id="cb1-16">p_ruin <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;">sum</span>(sim)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>ntrials </span></code></pre></div>
</details>
</div>
<p>The probability of ruin estimated over 1000 iterations of the gambler process is 0.888, in close agreement with the value 0.875 computed from Equation&nbsp;5.</p>
<p>It is noted that, at each time step, the gambler appears to move randomly, either to the left or to the right by a fixed unit distance with probability <img src="https://latex.codecogs.com/png.latex?p"> and <img src="https://latex.codecogs.com/png.latex?1-p">, respectively. The gambler undergoes a simple random walk in one dimension, whose state space is the set of the integers <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BZ%7D">. The limitations to values of <img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BZ%7D"> between 0 (ruin) and <img src="https://latex.codecogs.com/png.latex?n"> (win) are the consequence of the gambling rules: these rules settle two absorbing barriers to the motion of the random walker. Nine exemplary sample paths of the random walk are shown in Figure&nbsp;2, for the case that <img src="https://latex.codecogs.com/png.latex?k=30,n=120,p=0.5">.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<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>(tidyverse)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggpubr)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><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;">2001</span>)</span>
<span id="cb2-5">k <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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># initial wealth</span></span>
<span id="cb2-6">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;">120</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># final wealth</span></span>
<span id="cb2-7">p <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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># probability of win at each bet</span></span>
<span id="cb2-8">ntrials <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;">9</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of independent trials</span></span>
<span id="cb2-9">nstep <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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of steps</span></span>
<span id="cb2-10">traj <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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> nstep, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> ntrials)</span>
<span id="cb2-11"><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>ntrials) {</span>
<span id="cb2-12">  traj[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> k</span>
<span id="cb2-13">  jmax <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> nstep</span>
<span id="cb2-14">  <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;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>nstep) {</span>
<span id="cb2-15">          bet <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;">sample</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="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="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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>p, p))</span>
<span id="cb2-16">          traj[j, i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> traj[j<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>, i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> bet</span>
<span id="cb2-17">          <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (traj[j, 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;">0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> traj[j, i] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> n) {</span>
<span id="cb2-18">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb2-19">          }</span>
<span id="cb2-20">  }</span>
<span id="cb2-21">}   </span>
<span id="cb2-22">t <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="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>, nstep<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;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), ntrials)</span>
<span id="cb2-23">path <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"4"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"6"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"7"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"8"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"9"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> nstep)</span>
<span id="cb2-24">traj <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>(traj)</span>
<span id="cb2-25">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path =</span> path, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> traj)</span>
<span id="cb2-26"></span>
<span id="cb2-27">my_theme <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;">theme</span>(</span>
<span id="cb2-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>),</span>
<span id="cb2-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>),</span>
<span id="cb2-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>),</span>
<span id="cb2-31">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>),</span>
<span id="cb2-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>),</span>
<span id="cb2-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>),</span>
<span id="cb2-34">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-35">    <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="cb2-36">    <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="cb2-37">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</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 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_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>))</span>
<span id="cb2-40"></span>
<span id="cb2-41"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(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> t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path =</span> path)) <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_line</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;">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;">"bets"</span>,</span>
<span id="cb2-44">       <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;">"gambler's wealth"</span>) <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;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</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="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;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</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="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;">scale_y_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;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</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;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>path, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labeller =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"label_both"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb2-49">  my_theme</span></code></pre></div>
</details>
<div class="cell-output-display">
<div id="fig-fig2" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-fig2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://amsabatini.netlify.app/posts/gambler/index_files/figure-html/fig-fig2-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-fig2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Gambler’s random walk: nine sample paths. The outcome of up to a maximum of 1000 bets is considered here.
</figcaption>
</figure>
</div>
</div>
</div>



</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent" data-entry-spacing="0">
<div id="ref-noauthororeditor" class="csl-entry">
Bertsekas, Dimitri P., and John N. Tsitsiklis. 2008. <span>“Introduction to Probability.”</span> <a href="https://ocw.mit.edu/resources/res-6-012-introduction-to-probability-spring-2018/part-i-the-fundamentals/MITRES_6_012S18_Textbook.pdf">https://ocw.mit.edu/resources/res-6-012-introduction-to-probability-spring-2018/part-i-the-fundamentals/MITRES_6_012S18_Textbook.pdf</a>.
</div>
<div id="ref-dobrow2016" class="csl-entry">
Dobrow, Robert P. 2016. <span>“Introduction to Stochastic Processes with r,”</span> March. <a href="https://doi.org/10.1002/9781118740712">https://doi.org/10.1002/9781118740712</a>.
</div>
</div></section></div> ]]></description>
  <category>probability</category>
  <guid>https://amsabatini.netlify.app/posts/gambler/</guid>
  <pubDate>Sat, 04 May 2024 22:00:00 GMT</pubDate>
  <media:content url="https://amsabatini.netlify.app/posts/gambler/ruin.png" medium="image" type="image/png" height="117" width="144"/>
</item>
</channel>
</rss>
