\documentclass[]{article}
\usepackage{proceed2e}
\usepackage{amssymb,amsmath,amsthm}
\usepackage{graphicx}
\usepackage{preamble}
\usepackage{natbib}
\usepackage{hyperref}
\usepackage{color}
\definecolor{mydarkblue}{rgb}{0,0.08,0.45}
\hypersetup{ %
    pdftitle={},
    pdfauthor={},
    pdfsubject={},
    pdfkeywords={},
    pdfborder=0 0 0,
    pdfpagemode=UseNone,
    colorlinks=true,
    linkcolor=mydarkblue,
    citecolor=mydarkblue,
    filecolor=mydarkblue,
    urlcolor=mydarkblue,
    pdfview=FitH}

\title{Bayesian Quadrature minimizes Maximum Mean Discrepancy}

\author{ {\bf Ferenc Husz\'{a}r} \\
Department of Engineering\\
Cambridge University\\
\texttt{fh277@cam.ac.uk}
\And
{\bf David Duvenaud } \\ %\thanks{Both authors contributed equally.} \\
Department of Engineering\\
Cambridge University \\
\texttt{dkd23@cam.ac.uk}
}

\begin{document}

\maketitle

%\begin{abstract}
%Maximum Mean Discrepancy is equivalent to the posterior variance of an integrated Gaussian process.
%\end{abstract}

\section{INTRODUCTION}
A common problem in statistical machine learning is to compute expectations of functions over probability distributions of the form:
\begin{equation}
	Z_{f,p} = \int f(x) p(x) dx \label{eqn:integral}
\end{equation}
Examples include computing marginal distributions, making predictions marginalizing over parameters, or computing the Bayes risk in a decision problem. %In this paper we assume that the distribution $p(x)$ is known in analytic form, and $f(x)$ can be evaluated at arbitrary locations.

%Monte Carlo methods produce random samples from the distribution $p$ and then approximate the integral by taking the empirical mean $\hat{Z} = \frac{1}{N}\sum_{n=1}^{N}f_{x_n}$ of the function evaluated at those points. This non-deterministic estimate converges at a rate $\mathcal{O}(\frac{1}{\sqrt{N}})$. When exact sampling from $p$ is impossible or impractical, Markov chain Monte Carlo (MCMC) methods are often used. MCMC methods can be applied to almost any problem but convergence of the estimate depends on several factors and is hard to estimate \citep{CowlesCarlin96}. The focus of this paper is on quasi-Monte Carlo methods that -- instead of sampling randomly -- produce a set of pseudo-samples in a deterministic fashion. These methods operate by directly minimising some sort of discrepancy between the empirical distribution of pseudo-samples and the target distribution. Whenever these methods are applicable, they achieve convergence rates superior to the $\mathcal{O}(\frac{1}{\sqrt{N}})$ rate typical of random sampling.

%In this paper we highlight and explore the connections between two deterministic sampling and integration methods: Bayesian quadrature (\bq{}) \citep{BZHermiteQuadrature,BZMonteCarlo} (also known as Bayesian Monte Carlo) and kernel herding \citep{chen2010super}. Bayesian quadrature estimates integral \eqref{eqn:integral} by inferring a posterior distribution over $f$ conditioned on the observed evaluations $f_{x_n}$, and then computing the posterior expectation of $Z_{f,p}$. The points where the function should be evaluated can be found via Bayesian experimental design, providing a deterministic procedure for selecting sample locations.% We call this procedure Sequential Bayesian Quadrature (\sbq).

%Herding, proposed recently by \cite{chen2010super}, produces pseudosamples by minimising the discrepancy of moments between the sample set and the target distribution. Similarly to traditional Monte Carlo, an estimate is formed by taking the empirical mean over samples $\hat{Z} = \frac{1}{N}\sum_{n=1}^{N}f_{x_n}$. Under certain assumptions, herding has provably fast, $\mathcal{O}(\frac{1}{N})$ convergence rates in the parametric case, and has demonstrated strong empirical performance in a variety of tasks.

In this paper, we make two main contributions.  Firs, we show that the Maximum Mean Discrepancy (MMD) criterion (used to choose samples in kernel herding) is identical to the expected error in the estimate of the integral $Z_{f,p}$ under a Gaussian process prior for $f$.  This expected error is the criterion being minimized when choosing samples for Bayesian quadrature (BQ).  Because Bayesian quadrature assigns different weights to each of the observed function values $f(\vx)$, we can view Bayesian quadrature as a weighted version of kernel herding.
 We show that these weights are optimal in a minimax sense over all functions in the Hilbert space defined by our kernel.  This implies that Bayesian quadrature dominates kernel herding and other non-optimally weighted herding in rate of convergence.

We further show that the MMD, when using BQ weights, is submodular in the samples chosen, which implies that sequential BQ achieves the optimal rate of convergence for any sampling method.

\section{HERDING}

Herding was introduced by \cite{welling2009herding} as a method for generating pseudo-samples from a distribution in such a way that certain nonlinear moments of the sample set closely match those of the target distribution.  The empirical mean $\frac{1}{N}\sum_{n=1}^{N}f_{x_n}$ over these pseudosamples is then used to estimate integral \eqref{eqn:integral}.

\subsection{Maximum Mean Discrepancy}

For selecting pseudosamples, herding relies on an objective based on the maximum mean discrepancy \citep[MMD;\ ][]{Sriperumbudur2010}: %MMD measures the divergence between two distributions, $p$ and $q$ with respect to a class of integrand functions $\mathcal{F}$ as follows:
%
\begin{align}
	\mmd_{\mathcal{F}}\left(p,q\right) = \sup_{f\in\mathcal{F}}\left\vert\int f_x p(x) dx - \int f_x q(x) dx \right\vert
\end{align}
%
%Intuitively, if two distributions are close in the MMD sense, then no matter which function $f$ we choose from $\mathcal{F}$, the difference in its integral over $p$ or $q$ should be small. A particularly interesting case is
%
When the function class $\mathcal{F}$ is functions of unit norm from a reproducing kernel Hilbert space (RKHS) $\He$, the MMD between two distributions can be conveniently expressed using only expectations of the associated kernel $k(x, x')$ \citep{Sriperumbudur2010}.
%
%\begin{align}
%MMD^2_{\He}(p,q) =& \sup_{\substack{f\in\He\\\Hnorm{f}=1}}\left\vert\int f_x p(x) dx - \int f_x q(x) dx\right\vert^2\\
%	=& \Hnorm{\mu_{p} - \mu_{q}}^2\\
%\nonumber	=&\iint k(x,y) p(x) p(y) dx dy\\
%\nonumber	-2 &\iint k(x,y) p(x) q(y) dx dy\\
%	+ &\iint k(x,y) q(x) q(y) dx dy,
%\end{align}
%
%where in the above formula $\mu_{p}=\int \phi(\vx)p(\vx)d\vx\in\He$ denotes the \emph{mean element} associated with the distribution $p$.
% For characteristic kernels, such as the Gaussian kernel, the mapping between a distribution and its mean element is bijective. As a consequence $\mmd_{\He}(p,q)=0$ if and only if $p=q$, making it a powerful measure of divergence.
%
Herding uses MMD to evaluate how well the sample set $\{\vx_1,\ldots,\vx_{N}\}$ represents the target distribution $p$, adding points greedily according to:
%
%\begin{align}
%	\epsilon_{herding}&\left(\{\vx_1,\ldots,\vx_{N}\}\right) = \mmd_{\He}\left(p,\frac{1}{N}\sum_{n=1}^{N}\delta_{x_n}\right)\\
%\nonumber	=&\iint k(x,y) p(x) p(y) dx dy\\
%		-2 &\frac{1}{N}\sum_{n=1}^{N}\int k(x,x_n) p(x) dx
%		+ \frac{1}{N^2}\sum_{n,m=1}^{N} k(x_n,x_m)
%\label{eq:mmd_assumption}
%\end{align}
%
%The herding procedure greedily minimizes its objective $\epsilon_{herding}\left(\{\vx_1,\ldots,\vx_{N}\}\right)$ , adding pseudosamples $\vx_n$ one at a time. When selecting the $n+1$-st pseudosample:
%
\begin{align}
\vx_{n+1} &\leftarrow \argmin_{\vx \in \mathcal{X}} \label{eqn:herding_criterion} \epsilon_{herding}\left(\{\vx_1,\ldots,\vx_{n},\vx\}\right)\\
	&= \argmax_{\vx \in \mathcal{X}} 2 \expectargs{\vx' \sim p}{k(\vx, \vx')} - \frac{1}{n+1}\sum_{m=1}^{n} k(\vx,\vx_m)\mbox{,}\notag
\end{align}
%
assuming $k(\vx,\vx) = \mbox{const}$.
The formula \eqref{eqn:herding_criterion} admits an intuitive interpretation: the first term encourages sampling in areas with high mass under the target distribution $p(\vx)$.  The second term discourages sampling at points close to existing samples.

\section{BAYESIAN QUADRATURE}

%\begin{figure}
%\centering
%\includegraphics[width=\columnwidth]{figures/bq_intro4}
%\caption{An illustration of Bayesian Quadrature.  The function $f(x)$ is sampled at a set of input locations.  %This induces a Gaussian process posterior distribution on $f$, which is integrated in closed form against the target density, $p(\vx)$.  Since the amount of volume under $f$ is uncertain, this gives rise to a (Gaussian) posterior distribution over $Z_{f,p}$.}
%\label{fig:bq_intro}
%\end{figure}

Bayesian quadrature (\bq) puts a prior distribution on $\vf$, then estimates integral \eqref{eqn:integral} by inferring a posterior distribution over the function $\vf$, conditioned on the observations $\vf(\vx_n)$ at some query points $\vx_n$.  For simplicity, $\vf$ is assigned a Gaussian process prior with kernel function $k$ and mean $0$.  This assumption is very similar to the one made by kernel herding.  The posterior distribution over $f$ then implies a distribution over $Z_{f,p}$. %See Figure \ref{fig:bq_intro} for an illustration of Bayesian Quadrature.
%
%\subsection{ BQ Estimator}
%
%Here we derive the \bq{} estimate of \eqref{eqn:integral}, after conditioning on function evaluations $\vf(\vx_1) \dots \vf(\vx_N)$, denoted as $f(\vX)$.
 The mean of this distribution, $\expectargs{}{Z}$ is the optimal Bayesian estimator for a squared loss.
%After conditioning on $\vf(\vX)$, we obtain a closed-form posterior over $\vf$.
%
%\begin{align}
%p(\vf(\vx\st)|\vf(\vX)) = \N{\vf(\vx\st)}{\mf(\vx\st)}{\cov(\vx\st,\vx\st')}
%\end{align}
%where
%\begin{align}
%\mf(\vx\st) = & k(\vx\st, \vX) K^{-1} \vf(\vX) \\
%\cov(\vx\st, \vx\st') = & k(\vx\st,\vx\st) - k(\vx\st, \vX) K^{-1} k(\vX, \vx\st)
%\end{align}
%
%and $K = k(\vX, \vX)$.
%
Conveniently, the \gp{} posterior allows us to compute the expectation of \eqref{eqn:integral} in closed form:
%
%\begin{align}
%Z & = \int f(\vx)p(\vx)d\vx
%\end{align}
%so we integrate over functions to get:
\begin{align}
\expectargs{\gp}{Z} & = \vz^T K^{-1} \vf(\vX)
\label{eq:marg_mean_symbolic}
\end{align}
where
\begin{align}
z_n & = \int\!\! k(\vx, \vx_n) p(\vx) d\vx = \expectargs{\vx' \sim p}{k(\vx_n, \vx')}.
\end{align}
%
As in kernel herding, the desired expectation of $Z_{f,p}$ is simply a linear combination of observed function values $\vf(\vx)$:
%
\begin{align}
\expectargs{\gp}{Z} & = \vz^T K^{-1} \vf(\vX) \\
    & = \sum_n w_{\bq}^{(n)} f(x_n)  \quad \textrm{where} \quad w_{\bq}^{(n)} = \sum_m \vz_j K^{-1}_{nm}
\end{align}
%
%\begin{align}
%w_{\bq}^{(n)} & = \sum_m \vz_j K^{-1}_{nm}
%\label{eq:bq_weights}
%\end{align}
%
Thus, we can view the BQ estimate as a weighted version of the herding estimate.

\subsection{Optimal sampling for BQ}

The posterior variance $\varianceargs{}{Z_{f,p}|f_{x_1}, \dots, f_{x_N}}$ quantifies our uncertainty in the estimate of the integral. When selecting locations to evaluate the function $f$, minimising the posterior variance is a sensible strategy. Below, we give a closed form formula for the posterior variance of $Z_{f,p}$, conditioned on the observations $f_{x_1} \dots f_{x_N}$, which we will denote by $\epsilon^2_{\bq{}}$.
\begin{align}
\epsilon^{2}_{\bq{}} = \varianceargs{}{Z_{f,p}|f_{x_1}, \dots, f_{x_N}}
% \nonumber & = \expectargs{f \sim \gp, p\sim p(x)}{ \left( f(\vx) - \mf(\vx) \right)\left( f(\vx') - \mf(\vx') \right)} \\
%\nonumber & = \int \Bigg( \!\! \left( \int f(\vx) p(\vx) d\vx - \int \mf(\vx') p(\vx') d\vx' \right) \\
%\nonumber & \quad \times \left( \int f(\vx) p(\vx) d\vx - \int \mf(\vx') p(\vx') d\vx' \right) \!\! \Bigg) p(f) df \\
%\nonumber & = \int\!\!\! \int\!\! \int\!\! \left[ f(\vx) - \mf(\vx) \right] \left[ f((\vx') - \mf(\vx') \right] p(f) df \\
%\nonumber & \qquad \times   p(\vx) p(\vx') d\vx d\vx' \\
%\nonumber & = \int\!\! \!\int\!\! \Cov \left[ f((\vx), f((\vx') \right] p(\vx) p(\vx') d\vx d\vx' \\
%\nonumber & = \int\!\!\! \int\!\! \left[ k(\vx, \vx') - k(\vx, \vX) K^{-1} k(\vX, \vx') \right] \\
%\nonumber          & \qquad \times p(\vx) p(\vx') d\vx d\vx' \\
%\nonumber & = \int\!\!\! \int\!\! k(\vx, \vx') p(\vx) p(\vx') d\vx d\vx' \\
%\nonumber & \quad - \left[ \int\!\! k(\vx, \vX) p(\vx) d\vx \right] K^{-1} \left[ \int\!\! k(\vX, \vx') p(\vx') d\vx' \right] \\
  = \expectargs{\vx, \vx' \sim p}{k(\vx, \vx')} - \vz K^{-1} \vz\mbox{,}
\label{eq:marg_var_symbolic}
\end{align}
%where $\vz_n = \expectargs{\vx' \sim p}{k(\vx_n, \vx')}$ as before.
Perhaps surprisingly, the posterior variance of $Z_{f,p}$ does not depend on the observed function values, only on the location $x_n$ of samples.  This allows the optimal samples to be computed ahead of time, before observing any values of $f$ at all \citep{minka2000dqr}.
%\begin{align}
%\epsilon^{2}_{\bq{}}(\vx_1,\ldots,\vx_N) = \expectargs{\vx, \vx' \sim p}{k(\vx, \vx')} - \vz K^{-1} \vz
%\end{align}
%We can contrast the \bq{} objective $\epsilon^{2}_{\bq{}}$ to the objective being minimized in herding, $\epsilon^{2}_{herding}$ of equation \eqref{eq:mmd_assumption}.
%Just like $\epsilon^{2}_{herding}$, $\epsilon^{2}_{\bq{}}$ expresses a trade-off between accuracy and diversity of samples. On the one hand, as samples get close to high density regions under $p$, the values in $\vz$ increase, which results in decreasing variance. On the other hand, as samples get closer to each other, eigenvalues of $K$ increase, resulting in an increase in variance.

\section{RELATING $\varianceargs{}{Z_{f,p}}$ TO $\mmd$}

The similarity in the behaviour of $\epsilon^{2}_{herding}$ and $\epsilon^{2}_{\bq{}}$ is not a coincidence, the two quantities are closely related to each other, and to \mmd.

\begin{prop} The expected variance in the Bayesian quadrature $\epsilon^{2}_{\bq{}}$  is the maximum mean discrepancy between the target distribution $p$ and $q_{\bq{}}(x) = \sum_{n=1}^{N}w^{(n)}_{\bq{}}\delta_{x_n}(x)$
\end{prop}
%
%\begin{proof} Proof omitted for space reasons.
%The proof involves invoking the representer theorem, using bilinearity of scalar products and the fact that if $f$ is a standard Gaussian process then $\forall g\in\He: \left\langle f,g\right\rangle \sim \mathcal{N}(0,\Hnorm{g})$:
%\begin{align}
%&\varianceargs{}{Z_{f,p}\vert f_{x_1}, \dots, f_{x_N}}=\\
%	&= \mathbb{E}_{f\sim GP} \left( \int f(x) p(x) dx - \sum_{n=1}^{N}w^{(n)}_{\bq{}} f(x_n)\right)^2\\
%	&= \mathbb{E}_{f\sim GP} \left( \int \left\langle f, \phi (x)\right\rangle p(x) dx - \sum_{n=1}^{N}w^{(n)}_{\bq{}} \left\langle f, \phi (x_n)\right\rangle\right)^2\\
%	&= \mathbb{E}_{f\sim GP} \left\langle f ,  \int\phi(x) dx - \sum_{n=1}^{N}w^{(n)}_{\bq{}}\phi(x_n)\right\rangle^2\\
%	&= \Hnorm{\mu_p - \mu_{q_{\bq{}}}}^2\\
%	&= \mmd^2(p,q_{\bq{}})
%\end{align}
%\end{proof}

We know that the the posterior mean $\expectargs{\gp}{Z_{f,p}\vert f_1,\ldots,f_N}$ is a Bayes estimator and has therefore the minimal expected squared error amongst all estimators. This allows us to further rewrite $\epsilon^{2}_{\bq{}}$ into the following minimax forms:
%
\begin{align}
\epsilon^{2}_{\bq{}} %&= \sup_{\substack{f\in\He\\\Hnorm{f}{\He}=1}} \left| \int f_x p(x) dx - \sum_{n=1}^{N}w^{(n)}_{\bq{}} f_{x_n}\right|^2\\
%	&= \inf_{\hat{Z}:\mathcal{X}^N\mapsto\mathbb{R}} \sup_{\substack{f\in\He\\\Hnorm{f}{\He}=1}} \left| Z - \hat{Z}\left(f_{x_1},\ldots,f_{x_N}\right)\right|^2\\
	&= \inf_{\bm{w}\in\mathbb{R}^N} \sup_{\substack{f\in\He\\\Hnorm{f}{\He}=1}} \left| \int f_x p(x) dx - \sum_{n=1}^{N}w_n 	f_{x_n}\right|^2
\end{align}
%
%Looking at $\epsilon^{2}_{\bq{}}$  this way, we may discover the deep similarity to the criterion $\epsilon^2_{herding}$ that kernel herding minimises.
Optimal sampling for Bayesian quadrature minimises the same objective as kernel herding, but with the uniform $\frac{1}{N}$ weights replaced by the optimal weights. As a corollary
%
\begin{align}
\epsilon^{2}_{\bq{}}(x_1,\ldots,x_N)  \leq \epsilon^{2}_{KH} (x_1,\ldots,x_N)
\end{align}
%
It is interesting that $\epsilon^{2}_{\bq{}}$ has both a Bayesian interpretation as posterior variance under a Gaussian process prior, and a frequentist interpretation as a minimax bound on estimation error with respect to an RKHS.  Figure \ref{fig:bound_curve} demonstrates this closed-form, quickly shrinking bound on the error of the Bayesian estimator.  This equivalence also implies that the MMD is proportional to the expected error when estimating integrals of functions drawn from a GP prior.
\begin{figure}
\includegraphics[width=\columnwidth]{figures/bound_curve_rkhs}
\caption{The empirical error rate in estimating $Z_{f,p}$,  for the \sbq{} estimator, on 10 random functions drawn from the RKHS corresponding to the kernel used.  Also shown is the upper bound on the error rate implied by the $\mmd$.}
\label{fig:bound_curve}
\end{figure}
%
%Figure \ref{fig:bound_curve} demonstrates the MMD bound on the error of the \sbq{} estimator, on random functions drawn from the RKHS corresponding to the kernel used.  The empirical error always falls below the bound given by the MMD.

\subsection{Convergence Rates}

In finite dimensional Hilbert spaces, the herding algorithm has been shown to reduce $\mmd$ at a rate $\mathcal{O}(\frac{1}{N})$, which compares favourably with the $\mathcal{O}(\frac{1}{\sqrt{N}})$ rate obtained by non-deterministic Monte Carlo samplers.% However, as pointed out by \cite{bach2012equivalence}, this fast convergence is not guaranteed in infinite dimensional Hilbert spaces, such as the RKHS corresponding to the Gaussian kernel.
we have shown that the greedy Bayesian estimator obtains the optimal rate for any sampling method.  We have empirical results demonstrating a rate of convergence much faster than $\mathcal{O}(\frac{1}{N})$, but this rate is unkown to the authors, and we wish to pose this question to the kernel community.

\pagebreak

\bibliographystyle{icml2012}
\bibliography{herding}

\end{document}


    \filetitle{omega}{Get or set the covariance matrix of shocks}{model/omega}

	\paragraph{Syntax for getting covariance
matrix}\label{syntax-for-getting-covariance-matrix}

\begin{verbatim}
OMG = omega(M)
\end{verbatim}

\paragraph{Syntax for setting covariance
matrix}\label{syntax-for-setting-covariance-matrix}

\begin{verbatim}
M = omega(M,OMG)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{M} {[} model \textbar{} bkwmodel {]} - Model or bkwmodel
  object.
\item
  \texttt{OMG} {[} numeric {]} - Covariance matrix that will be
  converted to new values for std deviations and cross-corr
  coefficients.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\item
  \texttt{OMG} {[} numeric {]} - Covariance matrix of shocks or
  residuals based on the currently assigned std deviations and
  cross-correlation coefficients.
\item
  \texttt{M} {[} model \textbar{} bkwmodel {]} - Model or bkwmodel
  object with new values for std deviations and cross-corr coefficients
  based on the input covariance matrix.
\end{itemize}

\paragraph{Description}\label{description}

\paragraph{Example}\label{example}


\chapter{{\tt testMT.c} --- A Multithreaded Driver Program}
\label{chapter:MT_driver}

\begin{verbatim}
/*  testMT.c  */

#include "../BridgeMT.h"

void FactorMT ( ) ;
void MatMulMT ( ) ;
void SolveMT ( ) ;

/*--------------------------------------------------------------------*/

void main ( int argc, char *argv[] )
/*
   ----------------------------------------------------------------
   read in Harwell-Boeing matrices, using multithreaded factor,
   solve, and multiply routines based on spooles, invoke eigensolver

   created  -- 98mar31, jcp
   modified -- 98dec18, cca
   ----------------------------------------------------------------
*/
{
BridgeMT  bridge ;
char      *inFileName_A, *inFileName_B, *parmFileName, *type ;
char      buffer[20], pbtype[4], which[4] ;
double    lftend, rhtend, center, shfscl, t1, t2 ;
double    c__1 = 1.0, c__4 = 4.0, tolact =  2.309970868130169e-11 ;
double    eigval[1000], sigma[2];
double    *evec;
int       error, fstevl, lfinit, lstevl, msglvl, mxbksz, ncol, ndiscd,
          neig, neigvl, nfound, nnonzeros, nrhs, nrow, nthreads,
          prbtyp, rc, retc, rfinit, seed, warnng ;
int       c__5 = 5, output = 6 ;
int       *lanczos_wksp;
InpMtx    *inpmtxA, *inpmtxB ;
FILE      *msgFile, *parmFile ;
/*--------------------------------------------------------------------*/
if ( argc != 8 ) {
   fprintf(stdout,
"\n\n usage : %s msglvl msgFile parmFile seed nthread inFileA inFileB"
"\n    msglvl   -- message level"
"\n    msgFile  -- message file"
"\n    parmFile -- input parameters file"
"\n    seed     -- random number seed, used for ordering"
"\n    nthreads -- number of threads "
"\n    inFileA  -- stiffness matrix, in Harwell-Boeing format"
"\n    inFileB  -- mass matrix, in Harwell-Boeing format"
"\n                used for prbtype = 1 or 2"
"\n", argv[0]) ;
   return ;
}
msglvl = atoi(argv[1]) ;
if ( strcmp(argv[2], "stdout") == 0 ) {
   msgFile = stdout ;
} else if ( (msgFile = fopen(argv[2], "a")) == NULL ) {
   fprintf(stderr, "\n fatal error in %s"
           "\n able to open file %s\n", argv[0], argv[2]) ;
   exit(-1) ;
}
parmFileName = argv[3] ;
seed         = atoi(argv[4]) ;
nthreads     = atoi(argv[5]) ;
inFileName_A = argv[6] ;
inFileName_B = argv[7] ;
fprintf(msgFile,
        "\n %s "
        "\n msglvl                -- %d"
        "\n message file          -- %s"
        "\n parameter file        -- %s"
        "\n stiffness matrix file -- %s"
        "\n mass matrix file      -- %s"
        "\n random number seed    -- %d"
        "\n number of threads     -- %d"
        "\n",
        argv[0], msglvl, argv[2], parmFileName, inFileName_A,
        inFileName_B, seed, nthreads) ;
fflush(msgFile) ;
/*
   ---------------------------------------------
   read in the Harwell-Boeing matrix information
   ---------------------------------------------
*/
if ( strcmp(inFileName_A, "none") == 0 ) {
   fprintf(msgFile, "\n no file to read from") ;
   exit(0) ;
}
MARKTIME(t1) ;
readHB_info (inFileName_A, &nrow, &ncol, &nnonzeros, &type, &nrhs) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : read in harwell-boeing header info",
        t2 - t1) ;
fflush(msgFile) ;
/*--------------------------------------------------------------------*/
/*
   ---------------------------------------------------------------
   read in eigenvalue problem data
   neigvl -- # of desired eigenvalues
   which  -- which eigenvalues to compute
     'l' or 'L' lowest (smallest magnitude)
     'h' or 'H' highest (largest magnitude)
     'n' or 'N' nearest to central value
     'c' or 'C' nearest to central value
     'a' or 'A' all eigenvalues in interval
   pbtype -- type of problem
     'v' or 'V' generalized symmetric problem (K,M)
                with M positive semidefinite (vibration problem)
     'b' or 'B' generalized symmetric problem (K,K_s)
                with K positive semidefinite
                with K_s posibly indefinite (buckling problem)
     'o' or 'O' ordinary symmetric eigenproblem
   lfinit -- if true, lftend is restriction on lower bound of
             eigenvalues. if false, no restriction on lower bound
   lftend -- left endpoint of interval
   rfinit -- if true, rhtend is restriction on upper bound of
             eigenvalues.  if false, no restriction on upper bound
   rhtend -- right endpoint of interval
   center -- center of interval
   mxbksz -- upper bound on block size for Lanczos recurrence
   shfscl -- shift scaling parameter, an estimate on the magnitude
             of the smallest nonzero eigenvalues
   ---------------------------------------------------------------
*/
MARKTIME(t1) ;
parmFile = fopen(parmFileName, "r");
fscanf(parmFile, "%d %s %s %d %le %d %le %le %d %le",
       &neigvl, which, pbtype, &lfinit, &lftend,
       &rfinit, &rhtend, &center, &mxbksz, &shfscl) ;
fclose(parmFile);
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : read in eigenvalue problem data",
        t2 - t1) ;
fflush(msgFile) ;
/*
   ----------------------------------------
   check and set the problem type parameter
   ----------------------------------------
*/
switch ( pbtype[1] ) {
case 'v' :
case 'V' :
   prbtyp = 1 ;
   break ;
case 'b' :
case 'B' :
   prbtyp = 2 ;
   break ;
case 'o' :
case 'O' :
   prbtyp = 3 ;
   break ;
default :
   fprintf(stderr, "\n invalid problem type %s", pbtype) ;
   exit(-1) ;
}
/*
   ----------------------------
   Initialize Lanczos workspace
   ----------------------------
*/
MARKTIME(t1) ;
lanczos_init_ ( &lanczos_wksp ) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : initialize Lanczos workspace",
        t2 - t1) ;
fflush(msgFile) ;
/*
   ----------------------------------
   initialize communication structure
   ----------------------------------
*/
MARKTIME(t1) ;
lanczos_set_parm( &lanczos_wksp, "order-of-problem", &nrow, &retc );
lanczos_set_parm( &lanczos_wksp, "accuracy-tolerance", &tolact, &retc);
lanczos_set_parm( &lanczos_wksp, "max-block-size", &mxbksz, &retc );
lanczos_set_parm( &lanczos_wksp, "shift-scale", &shfscl, &retc );
lanczos_set_parm( &lanczos_wksp, "message_level", &msglvl, &retc );
lanczos_set_parm( &lanczos_wksp, "number-of-threads", &nthreads, &retc);
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : init lanczos communication structure",
        t2 - t1) ;
/*--------------------------------------------------------------------*/
/*
   ---------------------------------------------
   create the InpMtx objects for matrix A and B
   ---------------------------------------------
*/
if ( strcmp(inFileName_A, "none") == 0 ) {
   fprintf(msgFile, "\n no file to read A from") ;
   exit(-1) ;
}
MARKTIME(t1) ;
inpmtxA = InpMtx_new() ;
InpMtx_readFromHBfile ( inpmtxA, inFileName_A ) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : read in A", t2 - t1) ;
fflush(msgFile) ;
if ( msglvl > 2 ) {
  fprintf(msgFile, "\n\n InpMtx A object after loading") ;
  InpMtx_writeForHumanEye(inpmtxA, msgFile) ;
  fflush(msgFile) ;
}
lanczos_set_parm( &lanczos_wksp, "matrix-type", &c__1, &retc );
if ( prbtyp != 3 ) {
   if ( strcmp(inFileName_B, "none") == 0 ) {
      fprintf(msgFile, "\n no file to read from") ;
      exit(0) ;
   }
   MARKTIME(t1) ;
   inpmtxB = InpMtx_new() ;
   InpMtx_readFromHBfile ( inpmtxB, inFileName_B ) ;
   MARKTIME(t2) ;
   fprintf(msgFile, "\n CPU %8.3f : read in B", t2 - t1) ;
   fflush(msgFile) ;
   if ( msglvl > 2 ) {
      fprintf(msgFile, "\n\n InpMtx B object after loading") ;
      InpMtx_writeForHumanEye(inpmtxB, msgFile) ;
      fflush(msgFile) ;
    }
} else {
  inpmtxB = NULL ;
  lanczos_set_parm( &lanczos_wksp, "matrix-type", &c__4, &retc );
}
/*
   -----------------------------
   set up the solver environment
   -----------------------------
*/
MARKTIME(t1) ;
rc = SetupMT((void *) &bridge, &prbtyp, &nrow, &mxbksz, inpmtxA,
             inpmtxB, &seed, &nthreads, &msglvl, msgFile) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : set up the solver environment",
        t2 - t1) ;
fflush(msgFile) ;
if ( rc != 1 ) {
   fprintf(stderr, "\n error return %d from SetupMT()", rc) ;
   exit(-1) ;
}
/*--------------------------------------------------------------------*/
/*
   -----------------------------------------------
   invoke eigensolver
   nfound -- # of eigenvalues found and kept
   ndisc  -- # of additional eigenvalues discarded
   -----------------------------------------------
*/
MARKTIME(t1) ;
lanczos_run ( &neigvl, &which[1] , &pbtype[1], &lfinit, &lftend,
	  &rfinit, &rhtend, &center, &lanczos_wksp, &bridge, &nfound,
	  &ndiscd, &warnng, &error, FactorMT, MatMulMT, SolveMT ) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : time for lanczos_run", t2 - t1) ;
fflush(msgFile) ;
/*
   -------------------------
   get eigenvalues and print
   -------------------------
*/
MARKTIME(t1) ;
neig   = nfound + ndiscd ;
lstevl = nfound ;
lanczos_eigenvalues (&lanczos_wksp, eigval, &neig, &retc);
fstevl = 1 ;
if ( nfound == 0 ) fstevl = -1 ;
if ( ndiscd > 0 ) lstevl = -ndiscd ;
hdslp5_ ("computed eigenvalues returned by hdserl",
         &neig, eigval, &output, 39L ) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : get and print eigenvalues", t2 - t1) ;
fflush(msgFile) ;
/*
   -------------------------
   get eigenvectors and print
   -------------------------
*/
/*
MARKTIME(t1) ;
neig = min ( 50, nrow );
Lncz_ALLOCATE(evec, double, nrow, retc);
for (i = 1; i<= nfound; i++) {d
   lanczos_eigenvector ( &lanczos_wksp, &i, &i, newToOld,
                        evec, &nrow, &retc ) ;
   hdslp5_ ( "computed eigenvector returned by hdserc",
             &neig, evec, &output, 39L ) ;
}
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : get and print eigenvectors", t2 - t1) ;
fflush(msgFile) ;
*/
/*
   ------------------------
   free the working storage
   ------------------------
*/
MARKTIME(t1) ;
lanczos_free( &lanczos_wksp ) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : free lanczos workspace", t2 - t1) ;
fflush(msgFile) ;
MARKTIME(t1) ;
CleanupMT(&bridge) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n CPU %8.3f : free solver workspace", t2 - t1) ;
fflush(msgFile) ;

fprintf(msgFile, "\n") ;
fclose(msgFile) ;

return ; }

\end{verbatim}


%==============================================================================
\chapter{In silico quantitative mapping of force-pCa curves to whole heart contraction and relaxation}\label{cha:chapter8}
%==============================================================================
%
%
%
\begin{remark}{Outline}
    In this chapter, we focus on the assessment of the steady-state F-pCa relationship (this is traditionally used to assess the myofilament $\Ca$ sensitivity) and its quantitative mapping to the LV contractile function. We start by investigating the F-pCa curve using the cell contraction sub-model of our full personalised SHAM rat heart contraction model (Section~\ref{sec:cellcontr}). Next, using a combination of simulators and emulators (Section~\ref{subsec:quantlink}), we show that changes in key features of the F-pCa curve cannot be uniquely described by changes in the sarcomere properties (Sections~\ref{sec:changespCa50result1}--\ref{sec:changespCa50result2}). At the same time, we show that changes in the LV function (e.g. ejection fraction, isovolumetric relaxation time, peak pressure) cannot be uniquely described by changes in the sarcomere properties (Section~\ref{sec:changesLVfunctionresult}). By coupling these two pieces of information, we demonstrate that the mapping from the F-pCa curve to the LV function and the corresponding inverse mapping are non-unique (Section~\ref{sec:ch8nonuniquemappings}). We then include a discussion of results and address specific limitations (Section~\ref{sec:ch8discussion}), and we conclude with a brief summary (Section~\ref{sec:ch8summary}).
\end{remark}

%
%
%
\section{Motivation}\label{sec:ch8motivation}
One potential assessment for sarcomere modulators is their impact on the F-pCa~\cite{Walker:2010}, which is a technique that is widely used in many HF diseases including rare/genetic diseases such as dilated and hypertrophic cardiomyopathies~\cite{Groen:2020,Bai:2013,Michael:2016,Kirschner:2005,Harris:2002}, as well as for HF with reduced and preserved ejection fraction~\cite{Nagy:2015,Kampourakis:2018,Kieu:2019,Awinda:2021,Mamidi:2018,Sparrow:2020}. The impact on the F-pCa curve is often examined in terms of observed shifts in the curve half-maximal activation ($\pCaf$), which is used as an index of $\Ca$ sensitivity. Leftward shifts in the $\pCaf$ are expected to improve contractility, whereas rightward shifts to decrease contractility. Sometimes there is an assumption that changes in $\Ca$ sensitivity are a surrogate for changes in whole heart function and that these changes are monotonic in that, for instance, an increase in myofilament $\Ca$ sensitivity (i.e. a leftward shift) would improve the whole heart cardiac output. In this case study, whose related manuscript is currently in submission~\cite{Longobardi:2022}, we discuss the limitations of such an assumption and demonstrate why the dynamic whole heart behaviour cannot be predicted or assessed with a shift in the F-pCa curve alone.

\vspace{0.2cm}
We use a mathematical model of active tension generation at the sarcomere level in LV rat myocytes to elucidate the relationship between sarcomere properties and the steady-state F-pCa curve. We next use our previously developed multi-scale model of SHAM rat heart, which integrates the cell tension model within a $3$D biventricular rat heart contraction model (Section~\ref{sec:ch7rat_heart_contraction_model}) and provides a quantitative link between sarcomere properties and whole heart contractile function to investigate the mapping from F-pCa curves to the LV function. In particular, using our framework, we are able to quantitatively link changes in the F-pCa curve to/from changes in the LV function for the first time to the best of our knowledge. Previously, shifts in the F-pCa curve were linked to changes in cellular tension~\cite{Chung:2016} but not to the whole-organ dynamic contractile behaviour and without investigating the inverse mapping. We use our modelling framework to show that observations made at the sarcomere level (e.g. a leftward shift in the F-pCa curve) do not uniquely map to desired effects at the whole-organ level (e.g. an increase in EF or improved relaxation). We also show that the opposite holds: given a change in LV function, there exist many ways this could have been achieved through sarcomeric modulation, and thus the same change in LV function can correspond to different shifts in the F-pCa curve. The implications of these phenomena on using F-pCa curves to interpret whole heart dynamics are discussed in Section~\ref{sec:ch8discussion}.

%
%
%
\section{Methods}\label{sec:ch8methods}

%
%
%
\subsection{Cellular contraction model}\label{sec:cellcontr}
We employed the Land et al.~\cite{Land:2012} myocyte contraction model (Section~\ref{sec:ch2contractionmodel}) to simulate active tension generation at the sarcomere level and isometric steady-state F-pCa relationship in the rat heart. Parameters were all set to the personalised SHAM rat model values (Table~\ref{tab:bestfitparametersvalues}) when applicable, or to the Land et al.~\cite{Land:2012} model baseline values when otherwise.

\vspace{0.2cm}
We recall that an experimentally derived F-pCa curve can be described by a Hill-type relationship between the force ($F$) and the negative logarithm of the $\Ca$ concentration ($\pCa$)~\cite{Walker:2010}:
%
\begin{equation}
    \frac{F}{F_0} = \frac{1}{1+10^{h(\pCaf-\pCa)}}
\end{equation}

\noindent
where $F_{0}$ is the maximal reference force and $\pCaf$ and $h$ are, respectively, the negative logarithm of the half-maximal effective $\Ca$ concentration and the Hill coefficient of this relationship. It immediately follows that F-pCa curves are uniquely determined by the $\pCaf$ and $h$ values. We have also seen that the steady-state force solution of the Land et al.~\cite{Land:2012} model can be derived analytically, where both the $\pCaf$ and $h$ features are expressed in terms of model parameters. In particular, we recall that the $\pCaf$ was given as:
%
\begin{align}
    & \pCaf(\mathbf{p}) =  -\log\left[\Caif\left(\frac{\koff}{\kon}\frac{\trpnf}{1-\trpnf}\right)^{1/\ntrpn}\right]\,,\quad\text{with} \label{eq:pCa50}\\
    & \mathbf{p} := (\Caif, \kon, \koff, \trpnf, \ntrpn) \label{eq:pCa50params}
\end{align}

\vspace{0.2cm}\noindent
In the next section, we shall make use of equations~\eqref{eq:pCa50}--\eqref{eq:pCa50params} to investigate on F-pCa curve shifts.

%
%
%
\subsection{Non-unique mapping of changes in $\pCaf$ to changes in sarcomere properties}\label{sec:changespCa50}
A given change ($\Delta$) in the $\pCaf$ feature value of the F-pCa curve can be written as:
%
\begin{equation}\label{eq:delta}
    \Delta = \pCaf^{\text{new}} - \pCaf
\end{equation}

\vspace{0.2cm}\noindent
where $\pCaf^{\text{new}}$ is the feature value characterising the newly observed F-pCa curve. We tested whether a shift of $\Delta$ units in the $\pCaf$ could be the result of unique changes in sarcomere properties. Testing for non-unique changes was carried out analytically and numerically (as visualised in Figure~\ref{fig:2perclwrwshift}) in the case where the change was driven by perturbations in one model parameter (Section~\ref{sec:changespCa50result1}), and numerically (as visualised in Figure~\ref{fig:pca50isolines}) in the case where this was driven by perturbations in two model parameters (Section~\ref{sec:changespCa50result2}).

\vspace{0.2cm}
In the one-parameter case, it sufficed to prove that for each parameter $p_i,\,i=1,\,\dots,\,5$ of equation~\eqref{eq:pCa50params} there exists a scaling coefficient $\alpha_i\in\mathbb{R}$ such that
%
\begin{equation}
    \mathbf{p}^{\text{new}} := (p_1,\,\dots,\,\p_{i-1},\,\alpha_i\times\p_i,\,p_{i+1},\,\dots,\,p_5)
\end{equation}

\noindent
is such that
%
\begin{equation}
    \pCaf^{\text{new}} = \pCaf(\mathbf{p}^{\text{new}})
\end{equation}

\vspace{0.2cm}\noindent
We computed this scaling coefficient for each parameter $\p_i$ by solving for $\alpha_i$ equation~\eqref{eq:delta} for a given $\Delta$.

\vspace{0.2cm}
In the two-parameter case, we proceeded as follows. We allowed $2$ parameters to take equally-spaced values within a $\pm\SI{50}{\percent}$ perturbation around their baseline values (Table~\ref{tab:bestfitparametersvalues}). We then generated a $2$D uniform grid from all the combinations of $2$ parameter values and used the Land et al.~\cite{Land:2012} cell contraction model to calculate the $\pCaf$ value at each parameter point of the grid. By plotting the resulting $\pCaf$ values across the grid as a heat map, we could discern regions in the $2$D parameter space that share the same $\pCaf$ value (isolines). The same process was repeated for pairs of parameters coming from vector $\mathbf{p}$ (equation~\eqref{eq:pCa50params}), which was shown to regulate the $\pCaf$ feature of the F-pCa curve (equation~\eqref{eq:pCa50}).

%
%
%
\subsection{The relationship between sarcomere properties and LV function}\label{subsec:quantlink}
To quantitatively link sarcomere properties to the LV function, we employed the personalised model of $3$D biventricular healthy (SHAM) rat heart contraction derived in Chapter~\ref{cha:chapter4} and extended in Chapter~\ref{cha:chapter7}. This model integrates the Land et al.~\cite{Land:2012} cell contraction model in the context of whole-organ simulations.

\vspace{0.2cm}
We have seen (Section~\ref{sec:ch7rat_heart_contraction_model}) that this model can be represented as a multi-scale function (simulator) that maps a $16$D vector of input parameters $\mathbf{x}$ to a scalar quantity of interest $y_j$ (e.g. $y_1=\textrm{EDV}$, $y_2=\textrm{ESV}$, etc.):
%
\begin{align}\label{eq:fsimul}
    f_{\textrm{simul}}\colon\mathbb{R}^{16} &\to\mathbb{R}\times\mathbb{R}\times\dots \\
    \mathbf{x} &\mapsto (y_1,\,y_2,\dots) \nonumber
\end{align}

\vspace{0.2cm}\noindent
At the same time, we have replaced (Section~\ref{sec:ch7training_dataset_emulators_global_sensitivity_analysis}) the simulator with a fast-evaluating surrogate model (emulator), based on Gaussian process emulation.

\vspace{0.2cm}
We used the simulator to investigate the dependence of LV volume features, namely EDV, ESV, SV, and EF, on model parameters. Specifically, we were interested in the dependence of the features on the $\Caif$, $\koff$, $\ntrpn$ and $\trpnf$ parameters as these directly modulated the $\pCaf$ feature of the F-pCa curve (equation~\eqref{eq:pCa50}). For each parameter, we performed the following operations. First, the simulator was run at $128$ input points $\mathbf{x}_i,\,i=1,\dots,128$, which had all their $16$ components fixed to baseline values (Table~\ref{tab:bestfitparametersvalues}) but the component corresponding to the examined parameter, which instead was set to $128$ equally-spaced values between $-\SI{50}{\percent}$ and $\SI{50}{\percent}$ of perturbation values from baseline. The LV volume features were then extracted from the simulated $128$ LV volume transients so that we could plot the variation of each feature as a function of the parameter variations around its baseline value.

\vspace{0.2cm}
We then used the emulator to perform a GPE-based GSA. We calculated the Sobol' first-order, second-order and total effects (Section~\ref{sec:ch3globalsensitivityanalysis}) as a measure of model outputs' sensitivity to model inputs. We evaluated the impact of $\Caif$, $\koff$, $\ntrpn$, $\trpnf$ parameters into affecting the total variance of EDV, ESV, SV, EF features. Parameters whose Sobol' indices' distributions' expectation was below the threshold $0.01$ were determined to have negligible effects.

%
%
%
\subsection{Non-unique mapping of changes in LV function to changes in sarcomere properties}\label{sec:changesLVfunction}
Left ventricular EF can be continuously monitored noninvasively as an index of LV systolic function in patients enrolled for clinical trials investigating HF treating drugs. However, it is important to discern whether detected changes in the EF value directly result from a specific cellular modulation caused by the administered drug or not. From a modelling perspective and in the context of sarcomere modulators, we can test whether changes observed in the LV function at the whole-organ level, as described by the EF feature, can be uniquely explained in terms of sarcomere properties' changes.

\vspace{0.2cm}
We therefore proceeded as in Section~\ref{sec:changespCa50} by generating a $2$D uniform grid of parameter values for each considered pair of parameters modulating the F-pCa curve (equation~\eqref{eq:pCa50}). We then used the emulator to predict the EF value at each parameter point of the grid. By plotting the resulting EF values across the grid as a heat map, we could again discern regions in the $2$D parameter space that share the same EF value (isolines).

%
%
%
\subsection{Non-unique mapping of F-pCa curve to LV function and of LV function to F-pCa curve}
In order to test if the mapping from the F-pCa curve to LV function is not unique, we considered all the parameter points of the $2$D grids generated in Section~\ref{sec:changespCa50} that produced the same shift in the reference $\pCaf$ feature value. As mentioned previously, these points all belong to isolines of the local $2$D parameter space. We then used the emulator to map the points to the corresponding organ-level EF value.

\vspace{0.2cm}
We also tested if the inverse mapping from the LV function to the F-pCa curve is not unique. For this purpose, we considered all the parameter points of the $2$D grids generated in Section~\ref{sec:changesLVfunction} that produced the same shift in the reference EF feature value. We then used the cellular contraction sub-model of the full simulator to map the points to the corresponding cell-level $\pCaf$ value.

%
%
%
\section{Results}

%
%
%
\subsection{Non-monotonic relationship between sarcomere parameters and LV function}
The variations of EDV, ESV, SV and EF features as a function of the $4$ considered F-pCa curve-modulating parameters (equation~\eqref{eq:pCa50}) are non-linear and non-monotonic, as shown in Figure~\ref{fig:EFvsparamsnonmonotonic}. An example of full simulator outputs comprising LV volume and pressure transients and PV loops is provided in Figure~\ref{fig:EFvskoff} for the EF-vs-$\koff$ case.

\begin{figure}[h!]
    \myfloatalign
    \includegraphics[width=\textwidth]{figures/chapter08/Fig1.pdf}
    \caption{The full $3$D biventricular rat heart contraction model is run at a fixed parameter set with only one parameter taking equally-spaced values in the $\pm\SI{50}{\percent}$ range of perturbation from its baseline value (vertical red dashed lines). The converging mechanics simulations' output PV loops are analysed to extract the corresponding EDV, ESV, SV and EF features' values (open blue dots), given as percentages from their baseline values (horizontal red dashed lines). The process is repeated separately for each parameter regulating the $\pCaf$ feature of the F-pCa curve. A linear regression (LR) model with second-order degree polynomials is fitted to the data (blue lines) to facilitate the visualisation of non-linear and non-monotonic relationships between the features and each of the parameters considered.}
    \label{fig:EFvsparamsnonmonotonic}
\end{figure}

\begin{figure}[h!]
    \myfloatalign
    \includegraphics[width=\textwidth]{figures/chapter08/Fig2.pdf}
    \caption{The full $3$D biventricular rat heart contraction model is run at a fixed parameter set with only one parameter varying around its baseline value. The resulting LV volume and pressure transients and corresponding PV loops are plotted for some of the parameter values (full lines in blue variants) and compared to the reference parameter set mechanics solution (dashed black line). Example showing $\koff$ parameter variation in the range obtained as a $\pm\SI{40}{\percent}$ perturbation of its baseline value.}
    \label{fig:EFvskoff}
\end{figure}

\vspace{0.2cm}
The obtained GSA Sobol' sensitivity indices are displayed as donut charts in Figure~\ref{fig:gsarestr}. We can see that the reference thin filament $\Ca$ sensitivity ($\Caif$) is the most important parameter in explaining the total variance of EDV, ESV, SV and EF features. The second most important parameter is the degree of cooperativity of $\Ca$ binding to TnC ($\ntrpn$), followed by the fraction of bound $\Ca$-TnC complexes for half-maximal cross-bridges activation ($\trpnf$) and the unbinding rate of $\Ca$ from TnC ($\koff$).

\begin{figure}[h!]
    \myfloatalign
    \includegraphics[width=\textwidth]{figures/chapter08/Fig3.pdf}
    \caption{The impact of $\pCaf$-modulating, sarcomere parameters on EDV, ESV, SV and EF organ-scale LV feature. The contribution of each parameter is represented by the sum of its first- and second-order effects. For each LV feature, higher-order interactions (up to the fourth order) are represented by the sum of all total effects minus the sum of all first- and second-order effects.}
    \label{fig:gsarestr}
\end{figure}

%
%
%
\subsection{$\pCaf$ changes are non-uniquely explained by sarcomere alterations: the one-parameter case}\label{sec:changespCa50result1}
We show that an observed change in $\pCaf$ can be caused by multiple different changes in sarcomere proprieties represented by model parameters. For each parameter $p_i,\,i=1,\,\dots,\,5$ of equation~\eqref{eq:pCa50params}, we report in Table~\ref{tab:alphavalues} the corresponding scaling coefficient $\alpha_i,\,i=1,\,\dots,\,5$ that would yield a shift of exactly $\Delta$ units in the $\pCaf$ value. This proves that an observed change in the F-pCa cannot be uniquely explained by a change in a specific sarcomere property. An example of $\pm\SI{2}{\percent}$ shift from a reference $\pCaf$ value is displayed in Figure~\ref{fig:2perclwrwshift}.

\begin{table}[h!]
    \myfloatalign
    \begin{tabularx}{\textwidth}{XX}
        \toprule
        \tableheadline{Parameter} & \tableheadline{Scaling coefficient $\alpha$} \\
        \midrule
        & \\
        $\Caif$ & $10^{\Delta}$ \\ & \\
        $\kon$ & $10^{-\Delta\cdot\ntrpn}$ \\ & \\
        $\koff$ & $10^{\Delta\cdot\ntrpn}$ \\ & \\
        $\trpnf$ & $\dfrac{10^{\Delta\cdot\ntrpn}}{\trpnf(10^{\Delta\cdot\ntrpn}-1)+1}$ \\ & \\
        $\ntrpn$ & $\dfrac{\log\left(\frac{\koff}{\kon}\frac{\trpnf}{1-\trpnf}\right)}{\Delta\cdot\ntrpn+\log\left(\frac{\koff}{\kon}\frac{\trpnf}{1-\trpnf}\right)}$ \\ & \\
        \bottomrule
    \end{tabularx}
    \caption{The mapping of sarcomere properties to F-pCa variations is non-unique. By scaling each parameter $p_i$ by the corresponding $\alpha_i$ coefficient, the cellular contraction model simulates a $\pCaf$ value which is shifted by exactly $\Delta$ units from the reference value.}
    \label{tab:alphavalues}
\end{table}

\begin{figure}[h!]
    \myfloatalign
    \includegraphics[width=0.8\textwidth]{figures/chapter08/Fig4.pdf}
    \caption{Different parameters can be individually perturbed to achieve the very same shift in the force-calcium relationship. Example showing $\SI{2}{\percent}$ leftwards (upper plot) and rightwards (bottom plot) shifts of the reference $\pCaf$ value. As $\trpnf$ and $\ntrpn$ also regulate the F-pCa curve's Hill coefficient (equation~\eqref{eq:h}), notice that for these two parameters the shift in the $\pCaf$ value is also affecting the curve's slope.}
    \label{fig:2perclwrwshift}
\end{figure}

%
%
%
\subsection{$\pCaf$ changes are non-uniquely explained by sarcomere alterations: the two-parameter case}\label{sec:changespCa50result2}
$\pCaf$ evaluation on $2$-parameter grids highlighted the presence of isolines, whose parameter points induce the same $\pCaf$ feature value (Figure~\ref{fig:pca50isolines}A). This means that moving between any two points on two isolines will result in the same shift in $\pCaf$, demonstrating also in the two-parameter case the non-uniqueness of changes in the half-activation of the F-pCa curve and a change in sarcomere properties.

%
%
%
\subsubsection{Non-unique mapping of F-pCa curve to LV function}\label{sec:fpcatolvnonuniquemapping}
Parameter points inducing the same shifts in the $\pCaf$ feature value (isolines of Figure~\ref{fig:pca50isolines}A) are quantitatively linked to different EF feature values (Figure~\ref{fig:pca50isolines}B), showing that the mapping from F-pCa curve to LV function is not unique.

\begin{figure}[h!]
    \myfloatalign
    \includegraphics[width=\textwidth]{figures/chapter08/Fig5.pdf}
    \caption{For each pair of parameters regulating the $\pCaf$ feature of the F-pCa curve, a $2$D uniform grid is constructed using a $\pm\SI{50}{\percent}$ perturbation around the reference parameter set values (black dots). (A) The cell contraction model is then used to calculate the $\pCaf$ feature value at every parameter point of the grid, and each grid is plotted as a heat map with values given as percentages from the control $\pCaf$ value. Contour plots are finally added to highlight the presence of isolines, whose many different parameter sets induce the same shift ($-\SI{2}{\percent}$ dotted, $\SI{0}{\percent}$ full, $+\SI{2}{\percent}$ dashed black lines) in the control $\pCaf$ value. (B) Parameter points from the obtained isolines are mapped by the emulator into EF feature values. These values (given as percentages of the control EF value) are represented by different colour intensities used to colour each parameter point in the isolines, showing that parameters that share the same $\pCaf$ values are linked to different EF values.}
    \label{fig:pca50isolines}
\end{figure}

%
%
%
\subsection{LV function changes are non-uniquely explained by sarcomere alterations}\label{sec:changesLVfunctionresult}
EF evaluation on $2$-parameter grids highlighted the presence of isolines, where all parameter combinations along these lines result in the same predicted EF (Figure~\ref{fig:EFisolines}A). This means that moving between any two points on each isoline will result in the same EF, demonstrating the non-uniqueness of the LV function and sarcomere properties.

%
%
%
\subsubsection{Non-unique mapping of LV function to F-pCa curve}\label{sec:lvtofpcanonuniquemapping}
Parameter points that give rise to the same predicted EF value (isolines of Figure~\ref{fig:EFisolines}A) are quantitatively linked to different $\pCaf$ values (Figure~\ref{fig:EFisolines}B), showing that the mapping from LV function to the F-pCa curve is not unique.

\begin{figure}[h!]
    \myfloatalign
    \includegraphics[width=\textwidth]{figures/chapter08/Fig6.pdf}
    \caption{For each pair of parameters regulating the $\pCaf$ feature of the F-pCa curve, a $2$D uniform grid is constructed using a $\pm\SI{50}{\percent}$ perturbation around the reference parameter set values (black dots). (A) A trained emulator is then used to predict the $\textrm{EF}$ feature value at every parameter point of the grid, and each grid is plotted as a heat map with values given as percentages from the control $\textrm{EF}$ value. Contour plots are finally added to highlight the presence of isolines (full black lines), whose many different parameter sets induce the same $\SI{0}{\percent}$ shift in the control $\textrm{EF}$ value. (B) Parameter points from the obtained isolines are mapped by the cell contraction model into $\pCaf$ feature values. These values (given as percentages of the control $\pCaf$ value) are represented by different colour intensities used to colour each parameter point in the isolines, showing that parameters that share the same $\textrm{EF}$ values are linked to different $\pCaf$ values.}
    \label{fig:EFisolines}
\end{figure}

%
%
%
\subsection{Non-unique mapping from/to F-pCa curve to/from LV function}\label{sec:ch8nonuniquemappings}
In Sections~\ref{sec:changespCa50result1}--\ref{sec:changespCa50result2}, we proved that changes in the $\pCaf$ value are not uniquely caused by changes in sarcomere properties. In Section~\ref{sec:fpcatolvnonuniquemapping}, we also showed that changes in the EF value are not uniquely caused by changes in the $\pCaf$ value. Because of these two findings, we can state that observed changes in the F-pCa curve cannot be uniquely mapped to changes in the LV function.

\vspace{0.2cm}
At the same time, in Section~\ref{sec:changesLVfunctionresult} we proved that changes in the EF feature value are not uniquely caused by changes in sarcomere properties. In Section~\ref{sec:lvtofpcanonuniquemapping}, we also showed that changes in the $\pCaf$ feature value are not uniquely caused by changes in the $\textrm{EF}$ value. Because of these two other findings, we can state that observed changes in the LV function cannot be uniquely mapped to changes in the F-pCa curve.

%
%
%
\section{Discussion}\label{sec:ch8discussion}
In this study, we have made use of mathematical models to characterise the relationship between sarcomere properties and the LV contractile function in the healthy rat heart. We have highlighted the presence of complex nonlinearities; in particular, we have demonstrated that the relationship between myofilaments' features (e.g. $\Caif$) and PV loop characteristics (e.g. EF) is non-monotonic. As these sarcomere properties also define the steady-state F-pCa relationship in the cardiac muscle, we extended this result in terms of shifts in the F-pCa curve that are often examined when experimentally assessing the effect of sarcomere-targeting pharmacological compounds. Therefore, we have provided both analytical and simulation study evidence that alterations in the F-pCa curve can cause very different changes in whole heart function. At the same time, we have shown that observed changes in the LV function cannot be attributed to a unique modification in sarcomere properties. Although in the previous sections we have characterised the LV function using EF, the obtained results generally hold for many other clinically relevant indexes of LV systolic and diastolic function, e.g. IVRT and PeakP, as visualised in Figures~\ref{fig:IVRTisolines}--\ref{fig:PeakPisolines}. To design new treatment strategies, solely looking at the induced shift in the $\pCaf$ feature value of the F-pCa curve on its own is therefore not enough to predict or to understand predictions of how this will be translated into whole-organ function changes.

\begin{figure}[ht!]
    \myfloatalign
    \includegraphics[width=\textwidth]{figures/chapter08/Fig6_IVRT.pdf}
    \caption{The same IVRT value (A) can be linked to different $\pCaf$ values (B) (i.e. to differently shifted F-pCa curves).}
    \label{fig:IVRTisolines}
\end{figure}

\begin{figure}[ht!]
    \myfloatalign
    \includegraphics[width=\textwidth]{figures/chapter08/Fig6_PeakP.pdf}
    \caption{The same PeakP value (A) can be linked to different $\pCaf$ values (B) (i.e. to differently shifted F-pCa curves).}
    \label{fig:PeakPisolines}
\end{figure}

\vspace{0.2cm}
Also, when studying myofilament $\Ca$ sensitivity using F-pCa curves, simply considering the $\pCaf$ value as a stand-alone measurement is not sufficient for translation into dynamic contraction and relaxation. This concept was previously shown for single-cell dynamics by Chung et al.~\cite{Chung:2016} who highlighted the importance of biophysical measures of $\koff$ and $\kon$ to help predict tension dynamics at the cellular level. However, in this work, we expand this concept further to the whole heart level by analysing LV features and exploring the inverse mapping from LV features to F-pCa curves, highlighting the non-uniqueness of both forward and inverse mapping between F-pCa and LV features. We emphasise that, for instance, an increase in $\Ca$ sensitivity, i.e. a leftward shift in the F-pCa curve, will have two effects. First, it will increase residual tension, decreasing EDV; second, it will increase end-systolic tension, decreasing ESV. The balance of these two effects will impact the change in SV and EF for a given change in $\Ca$ sensitivity. This impact of changes in $\pCaf$ depends on the starting F-pCa curve, the $\Ca$ transient, cardiac material properties and boundary conditions. Determining these multi-scale relationships from experimental preparations remains challenging.

\vspace{0.2cm}
The performed GSA interpreted the variability of EDV, ESV, SV and EF in terms of the uncertainty in F-pCa curve-modulating sarcomere parameters, and it emphasised the importance of the $\Ca$ sensitivity in affecting these PV loop characteristics. Deeper insight can be gained if we look at higher-order interactions' effects. Although these are present for all the four LV features considered, they are remarkably high only for the SV and EF features, explaining more than half of the total variance for SV and almost half of the total variance for EF. We can notice that for the EDV and ESV features, higher-order interactions' effects are instead small, and dominating effects are mostly of the first-order type. Conversely, for the SV and EF features where higher order interactions' effects are high, dominating lower-order effects are mostly of the second-order type. All these considerations can be summarised by stating that although it is possible to interpret changes in terms of the individual contribution of parameters for the EDV and ESV features, this is not the case for SV and EF features. In this sense, it is more the combined effect of the whole sarcomere to determine the LV function rather than single myofilament components.

\vspace{0.2cm}
Indications from the GSA coupled with the information of F-pCa curve vs LV function non-monotonicity shed new light on the problem of interpreting pharmacological interventions' effects on the F-pCa curve in terms of the desired effects on whole-heart function. This concept is illustrated via the schematic in Figure~\ref{fig:schematic}, which highlights existing feedback mechanisms that regulate contraction in the heart. Modulation of the $\Ca$ transient can directly affect the active tension which is generated within the sarcomere, and alteration of the sarcomere generated force will eventually affect the PV loop. As the force-volume relationships at the end-diastolic and end-systolic pressures are fixed, sarcomere interventions that aim at shifting the F-pCa might result in no change in LV contractile function. For this reason, treatment strategies should aim at altering both the end-systolic and end-diastolic force-volume relationships or altering one while maintaining the other to yield an effect on EF.

\begin{figure}[ht!]
    \myfloatalign
    \includegraphics[width=\textwidth]{figures/chapter08/Fig7.pdf}
    \caption{Force-LV volume curves at the end-diastolic and end-systolic pressures (top right, orange and green lines) can be separately manipulated to modulate the LV contractile function. However, interventions on calcium transient (bottom left, blue line) and sarcomeric generated force (top left, orange and green full lines) both manifest as modifications of the former curves, which in turn causes modification of the PV loop (bottom right, blue full line). In addition, pharmacological modulations on the sarcomere might cause a shift of the force-calcium relationship (top left, orange and green dashed lines). However, this will preserve the EF (bottom right, blue dashed line) without improving LV contractile function, which was the desired outcome of the performed sarcomeric intervention.}
    \label{fig:schematic}
\end{figure}

%
%
%
\subsection{Limitations}\label{sec:ch8limitations}
The limitations of this study mainly concern the adopted modelling/emulation framework, and are presented fully in Chapter~\ref{cha:chapter9}, Section~\ref{sec:ch9limitations}.

%
%
%
\section{Summary}\label{sec:ch8summary}
We have used a biophysically detailed mathematical model of a healthy rat heart contraction to quantitatively map sarcomere properties to whole heart function. Using this mapping, we demonstrated that the relationship between the F-pCa curve and LV function is non-linear and non-monotonic. This results in the non-interpretability of observed changes in the LV function in terms of unique sarcomere modulations, which highlights the need for muscle experimental findings to be put into a broader context where not only the $\pCaf$ and Hill coefficient but also active and passive force, length and velocity dependencies, calcium transient and boundary conditions are analysed.


\documentclass[dvipdfmx,uplatex,a4paper]{article}
\def\vector#1{\mbox{\boldmath $#1$}}
\usepackage[hiresbb]{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{ascmac}
\usepackage{braket}
\usepackage{circuitikz}
\usepackage{cite}
\usepackage{comment}
\usepackage{float}
\usepackage{listings}
\usepackage{nameref}
\usepackage{siunitx}
\usepackage{tikz}
\usepackage[version=3]{mhchem}

\usepackage[colorlinks=true, bookmarks=true,
bookmarksnumbered=true, bookmarkstype=toc, linkcolor=blue,
urlcolor=blue, citecolor=blue]{hyperref}

\makeatletter
 \renewcommand{\theequation}{
   \thesubsection.\arabic{equation}}
  \@addtoreset{equation}{section}
\makeatother

\title{Solve RGB+ LEDs PWM from Chromaticity}
\author{CHINZEI, Kiyoyuki}
\date{\today}
\begin{document}
\maketitle

\begin{abstract}
This article\footnote{To cite, please refer to https://github.com/kchinzei/kch-rgbw-lib/docs/rgbw\_solver.pdf} is a private note to develop \texttt{kch-rgbw-lib}, a TypeScript library in \href{https://github.com/kchinzei/kch-rgbw-lib}{github} and \href{https://www.npmjs.com/package/kch-rgbw-lib}{npm}. \texttt{Kch-rgbw-lib} provides classes and functions for multicolor LED, including color space conversions between HSV, RGB, XYZ and xyY and calculation of accurate color composition. This article gives a general solution of multi-number (more than R-G-B) LEDs to represent composite colors. It is not intended to carry new, comprehensive, or most efficient ideas. This document is granted under MIT License.
\end{abstract}

\section{Define the Problem}
\subsection{Past works}\label{s_intro}
Obtaining accurate colors by mixing R-G-B light sources has been utilized as color displays since 1950s. As various colors are available by LED, recent topics are solutions of color composite with additional color sources typically for OLED applications \cite{Chi2011, Lee2014}. Usually white light sources are used as an additional light source \cite{AN1562, Chi2011, Lee2014}. For display purposes additional colors other than R-G-B have been also used to expand the possible color ranges (gamut)\cite{Wikipedia_multicolor}. Sharp once added yellow in Aquos flat-panel TV, but they initially researched 5-primary color display \cite{Sharp2011}. Amber \cite{AN2026}, turquoise, and violet can be other colors to expand the gamut.

\subsection{Given parameters and assumptions}\label{s_assumptions}
We have $N \geq 3$ color sources (LEDs) with chromaticity $(x_i, y_i)$ and maximum luminance $Y_i$, where $i=1 \ldots N$. Our problem is to find the optimum composite output ratio (PWM output) $\boldsymbol{\alpha} = [\alpha_1 \cdots \alpha_N]^T$ where $0 \leq \alpha_i \leq 1$ to represent a given color input with chromaticity $(x, y)$ and luminance $Y$.

Here, we set our goal of optimization as the following:
\begin{enumerate}
  \item\label{I_max_luminance} Under physical constraint $0 \leq Y \leq  Y_1 + \cdots + Y_N$,
  \item\label{I_solution} Obtain exact composite color to represent $(x, y, Y)$.
  \item\label{I_inside_gamut} If $(x, y)$ is outside the gamut of color source, the nearest color in it is used.
  It can be achieved by projecting the input to the gamut contour.
  \item\label{I_min_energy} When possible, minimize energy consumption $E$, \\
  \begin{equation}
    \label{E_min_energy}
    E = \sum_1^N \alpha_i W_i
  \end{equation}
  where $W_i$ is the power of each LED at the maximum luminance.
  \item\label{I_lifetime} When possible, average turn-on time of LEDs to equalize LEDs lifetime,
  \item\label{I_small_alpha} When possible, set $\alpha_i$ to null when it's very small.
  It is preferable to avoid jitter at low PWM output.
\end{enumerate}

It is a typical linear programming (LP) problem. When $N=3$, e.g. R-G-B color sources only, it's a deterministic and not an optimization problem. And when $N=4$, e.g. R-G-B-W LEDs, there is only one parameter to optimize, which makes the problem as simple as we don't need to use sophisticated LP solver.
% Mathematically, it is dependent to $R = rank(\boldsymbol{A})$, where $\boldsymbol{A}$ is a matrix of LEDs, in Eq.~\eqref{E_X=AY}. We first derive a general description of the problem and solve it for $R=3$, $R=4$ and $R > 4$ cases.
We first derive a general description of the problem and solve it for $N=3$, $N=4$ and $N > 4$ cases.

\section{General solution}
We use XYZ color space because a composite of color source in XYZ color space can be obtained as a simple sum of each term.  In XYZ color space, our problem is to determine $\boldsymbol{\alpha} = [\alpha_1 \cdots \alpha_N]$ which gives an equation between input color $[X, Y, Z]^T$ and color source $[X_{i}, Y_{i}, Z_{i}]^T$, $i=1 \ldots N$;

\begin{equation}
  \label{E_XYZcomposite}
  \left[
    \begin{array}{c}
      X \\
      Y \\
      Z
    \end{array}
  \right]
   = \alpha_1
  \left[
    \begin{array}{c}
        X_1 \\
        Y_1 \\
        Z_1
    \end{array}
  \right]
   + \cdots + \alpha_N
  \left[
    \begin{array}{c}
        X_N \\
        Y_N \\
        Z_N
    \end{array}
  \right]
\end{equation}

Color space $[X, Y, Z]^T$ is expressed by using $(x, y, Y)$:

\begin{eqnarray}
  \label{E_xyY2XYZ}
  X & = & \frac{x}{y}  Y \\
  Y & = & Y \\
  Z & = & \frac{1 - x - y}{y} Y
\end{eqnarray}

Using matrix representation, Eq.~\eqref{E_XYZcomposite} is written as

\begin{equation}
  \label{E_X=AY}
  \left[ \boldsymbol{X} \right] =
  \left[ \boldsymbol{A} \right]
  \left[ \boldsymbol{\alpha} \right]
\end{equation}

\noindent
where

\begin{eqnarray}
  \left[ \boldsymbol{X} \right] &=&
  \left[ X \, Y \, Z \right]^T \label{E_XYZ^T} \\
%
  \left[ \boldsymbol{A} \right] &=&
  \left[
    \begin{array}{ccc}
      \frac{x_1}{y_1} Y_1 & \cdots & \frac{x_N}{y_N} Y_N \\
      Y_1 & \cdots & Y_N \\
      \frac{1 - x_1 - y_1}{y_1}Y_1 & \cdots & \frac{1 - x_N - y_N}{y_N}Y_N
    \end{array}
  \right] \\
%
  \left[ \boldsymbol{\alpha} \right] &=&
  \left[ \alpha_1\, \cdots\, \alpha_N \right]^T
\end{eqnarray}

Our goal is to solve Eq.~\eqref{E_X=AY} with respect to $ \boldsymbol{\alpha} = [\alpha_1 \cdots \alpha_N]^T $. To solve it, we can use the singular value decomposition (SVD) (Eq.~\eqref{E_SVD})~\cite{SVD_NRC}.

\begin{equation}
  \label{E_SVD}
  \Bigg[ \boldsymbol{A} \Bigg] =
  \Bigg[ \boldsymbol{U} \Bigg]
  \left[
    \begin{array}{cccc}
      \omega_1 & \\
        & \omega_2 &   &  0 \cdots 0 \\
        &  & \omega_3 &
    \end{array}
  \right]
  \Bigg[ \boldsymbol{V}^T \Bigg]
\end{equation}

\noindent
where $ \boldsymbol{A} $ is $ 3 \times N $,
$ \boldsymbol{U} $ is $ 3 \times 3 $,
$ [\omega_1 \ddots \omega_3, 0 \cdots 0] $ is $ 3 \times N $,
$ \boldsymbol{V}^T $ is $ N \times N $ matrixes
\footnote{Many implementations of SVD return 'economy' $\boldsymbol{V}$ in $ N \times 3$ instead of calculating full $N \times N$ size. Matlab, Octave without 'econ' option and \href{https://www.npmjs.com/package/svd-js}{svd-js} javascript package in NPM with 'f' option (This is my contribution!) give full $\boldsymbol{V}$.}
in this specific case, since $ \boldsymbol{A} $ is a $ 3 \times N $ matrix and there are upto 3 $\omega$'s. When $ N \geq 4 $, $ [\omega_1 \ddots \omega_3 ] $ is null-padded in $ 3 \times (N-3) $.
The pseudo-inverse matrix $ \boldsymbol{A}^{-1} $ is obtained by

\begin{equation}
  \Bigg[ \boldsymbol{A} \Bigg]^{-1} =
  \Bigg[ \boldsymbol{V}_{1-3} \Bigg]
  \left[
    \begin{array}{ccc}
      1/\omega_1 & & \\
      & 1/\omega_2 & \\
      & & 1/\omega_3
    \end{array}
  \right]
  \Bigg[ \boldsymbol{U}^T \Bigg]
\end{equation}

\noindent
where $ \boldsymbol{V}_{1-3} $ is the first 3 columns of $ \boldsymbol{V} $, those correspond to $ \omega_1, \omega_2, \omega_3 $
\footnote{Many implementations of SVD do not sort $ \boldsymbol{U}, \boldsymbol{V}$ by $\omega$s. Matlab and Octave do sort. Algorithm in 'Numerical Recipes in C' and \href{https://www.npmjs.com/package/svd-js}{svd-js} in NPM do not.}.
Using $ \boldsymbol{A}^{-1} $, we obtain

\begin{equation}
  \label{E_Y=A-1X}
  \left[ \boldsymbol{\alpha} \right] =
  \left[ \boldsymbol{A} \right]^{-1}
  \left[ \boldsymbol{X} \right]
\end{equation}

By the way, what about the rest of columns in $ \boldsymbol{V} $? They are null vectors of $ \boldsymbol{A} $. A null vector $\boldsymbol{n}$ of $ \boldsymbol{A}$ is such vector that satisfies $ \boldsymbol{A} \boldsymbol{n} = [0]$. By denoting these columns as $ \boldsymbol{n}_4 \ldots \boldsymbol{n}_N $, Eq.~\eqref{E_Y=A-1X} can be extended as

\begin{equation}
  \label{E_Y=A-1X+n}
  \left[ \boldsymbol{\alpha} \right] =
  \left[ \boldsymbol{A} \right]^{-1}
  \left[ \boldsymbol{X} \right]
  + \beta_4 \boldsymbol{n}_4 + \cdots + \beta_N \boldsymbol{n}_N
\end{equation}
\noindent
where $ \beta_4 \ldots \beta_N $ are arbitrary numbers. By choosing these using other constraints, we can obtain the optimum solution.

% The actual number of null vectors is determined by the rank of $\boldsymbol{A}$.

\section{Solution of $N=3$ case}

When $N=3$, Eq.~\eqref{E_Y=A-1X} gives a deterministic solution. No optimization. However, the obtained $ \alpha_i $ should be physically meaningful, i.e., $ 0 \leq \alpha_i \leq 1 $. It can be out of range when
\begin{itemize}
  \item Input color $[X]$ is out of the gamut defined by the color sources,
  \item $Y$ of $[X]$ is greater (brighter) than the color sources.
\end{itemize}
\noindent
When $ \alpha_i < 0$, the nearest color in the gamut should be used as the input. We can also truncate such $\alpha_i$ to 0. In this case, the output color has certain error.

When $ \alpha_i > 1 $, all $\alpha$s should be normalized by the largest $\alpha$. This way the color will be correctly obtained, but it will be darker than expected.

\section{Solution of $N=4$ case}

When $N=4$, Eq.~\eqref{E_Y=A-1X+n} is simple.

\begin{equation}
  \label{E_Y=A-1X+1}
  \left[ \boldsymbol{\alpha} \right] =
  \left[ \boldsymbol{A} \right]^{-1}
  \left[ \boldsymbol{X} \right]
  + \beta_4 \boldsymbol{n}_4
\end{equation}

\noindent
We will determine parameter $\beta_4$ using the assumptions in section \ref{s_assumptions}.
Since it is always $ 0 \leq \alpha_i \leq 1$, by solving it for $\beta$ (hereafter omitting '$_4$'), we obtain the following conditions.
\begin{eqnarray}
  \beta & \geq & \begin{cases}
    - \frac{b_i}{n_i} \;\;\;\; \text{ if \(n_i > 0\)} \label{E_betamin} \\
    \frac{1 - b_i}{n_i} \;\;\;\; \text{ if \(n_i < 0\)}
  \end{cases} \\
%
  \beta & \leq & \begin{cases}
    \frac{1 - b_i}{n_i} \;\;\;\; \text{ if \(n_i > 0\)} \\
    - \frac{b_i}{n_i} \;\;\;\; \text{ if \(n_i < 0\)} \label{E_betamax}
  \end{cases} \\
%
    E &=& \sum_{i=1}^4 (\beta n_i + b_i) W_i \to min \label{E_energy_n4}
\end{eqnarray}

\noindent
where $ [b_1,\, \ldots{}\, , b_4]^T = [ \boldsymbol{A} ]^{-1} [\boldsymbol{X}] $, $n_i$ are the elements of $\boldsymbol{n}$. Eq.~\eqref{E_energy_n4} is from Eq.~\eqref{E_min_energy}. Finding the largest and smallest values of the right hand side of Eqs.~\eqref{E_betamin} and \eqref{E_betamax}, denoted as $\beta_{min}$ and $\beta_{max}$, Eqs.~\eqref{E_betamin} and \eqref{E_betamax} are rewritten as

\begin{equation}
  \beta_{min} \leq \beta \leq \beta_{max}
\end{equation}

Since Eq.~\eqref{E_energy_n4} is rewritten as $E = s_1\beta + s_2$, here $s_1$ and $s_2$ are constants determined by calculating the sums in Eq.~\eqref{E_energy_n4}, optimized $\beta$ is determined as

\begin{equation}
    \beta = \begin{cases}
      \beta_{min}  & \text{if \(s_1 = \sum_1^4 n_i W_i > 0\)} \\
      \beta_{max}  & \text{else}
  \end{cases}
\end{equation}

\subsection{When $\beta$ is not determined}

When $\beta_{min} > \beta_{max}$, there is no feasible answer. Again, there are two cases, when the input color $[\boldsymbol{X}]$ is out of the gamut, or when $Y$ in Eq.~\eqref{E_XYZ^T} is greater than the color sources. Since $\alpha_i \geq 0$ cannot compromise, we find $\beta$ that satisfies

\begin{eqnarray}
  \beta & \geq & - \frac{b_i}{n_i} \;\;\;\; \text{ if \(n_i > 0\)} \label{E_beta0min} \\
  \beta & \leq & - \frac{b_i}{n_i} \;\;\;\; \text{ if \(n_i < 0\)} \label{E_beta0miax}
\end{eqnarray}
Then we normalize the largest $\alpha_i$ to be 1.

\subsection{When $\alpha_i$ is small}

To implement the assumption \ref{I_small_alpha} in section \ref{s_assumptions}, we can introduce allowance of small $\alpha$, $\alpha_\varepsilon$, and exchange Eqs.~\eqref{E_betamin} and \eqref{E_betamax} as

\begin{equation}
  \beta \geq \frac{\alpha_\varepsilon - b_i}{n_i}
\end{equation}

But this also needs to assert if $\beta_{min} \leq \beta_{max}$.

\section{Solution of $N > 4$ case}
We can optimize Eq.~\eqref{E_min_energy} under constraints of $0 \leq \alpha_i \leq 1$ for Eq.~\eqref{E_Y=A-1X+n} using a linear programming solution\cite{LP_NRC}.
To use linear programming we need to rewrite our problem into the \textit{normal form}. Normal form of a linear programming problem is

\begin{itemize}
  \item Objective functions are to be minimized,
  \item All constraints are equality formulas,
  \item All variables $ \geq 0$.
\end{itemize}

Since our constraints $0 \leq \alpha_i \leq 1$ are unequal and $\beta_i$ can be negative, we introduce \textit{slack variables} $\gamma_i, \delta_i$ and $\epsilon_i, \zeta_i$ to replace $\beta$.

\begin{itemize}
  \item Introduce $\gamma_i \geq 0$ such that $b_i + \beta_4 n_{4i} + \cdots + \beta_N n_{Ni} - \gamma_i = 0$,

  \item Introduce $\delta_i \geq 0$ such that $b_i + \beta_4 n_{4i} + \cdots + \beta_n n_{Ni} + \delta_i = 1$,

  \item Replace $\beta_i = \epsilon_i - \zeta_i$, such that $\epsilon_i \geq 0 $ and $\zeta_i \geq 0$.
\end{itemize}

After this modification, we have $2N$ equations with $2N$ variables (for $\gamma$ and $\delta$) and $2(N-3)$ variables (for $\epsilon$ and $\zeta$). Using these replacements, the objective function (Eq.~\eqref{E_min_energy}) and Eq.~\eqref{E_Y=A-1X+n} are rewritten as

\begin{eqnarray}
%
  \label{E_min_energy5}
  \sum_{i=1}^N (n_{4i} \epsilon_4 - n_{4i} \zeta_4 + \cdots +  n_{Ni} \epsilon_N - n_{Ni} \zeta_N + b_{i}) W_i &\to& min \\
%
  \label{E_bmin5}
  n_{4i} \epsilon_4  - n_{4i} \zeta_4 + \cdots + n_{Ni} \epsilon_N  - n_{Ni} \zeta_N  - \gamma_i + b_i &=& 0 \\
%
  \label{E_bmax5}
  n_{4i} \epsilon_4  - n_{4i} \zeta_4 + \cdots + n_{Ni} \epsilon_N  - n_{Ni} \zeta_N  + \delta_i + b_i &=& 1
\end{eqnarray}

\noindent
Eq.~\eqref{E_min_energy5} is the objective function. Solvers applicable to Eqs.~\eqref{E_min_energy5} - \eqref{E_bmax5} can be found in many numerical packages. \href{https://www.npmjs.com/package/kch-rgbw-lib}{\texttt{Kch-rgbw-lib}} uses \href{https://www.npmjs.com/package/linear-program-parser}{linear-program-parser} and \href{https://www.npmjs.com/package/linear-program-solver}{linear-program-solver} in npm.

\subsection{When solution infeasible}
Linear programming solver may find it's `infeasible' - no solution to satisfy all constraints in Eqs.~\eqref{E_bmin5} and \eqref{E_bmax5}. Another possibility is that the solution would not converge. Yet again, there are two cases, when the input color $[X]$ is out of the gamut, or when $Y$ in Eq.~\eqref{E_XYZ^T} is greater (brighter) than the color sources.

When the object brightness is too large, we can find a solution by omitting Eq.~\eqref{E_bmax5}. Physically doable solution is to normalize the maximum $Y_i$ to $1.0$.

We may also see infeasible case when one or more value in solution is very close to zero and Eq.~\eqref{E_bmin5} fails due to numerical error. If it is the case, you can set a small negative value to the right side of Eq.~\eqref{E_bmin5} instead of $0$.

\section{More to do}

The following sections discuss items not implemented in \href{https://www.npmjs.com/package/kch-rgbw-lib}{\texttt{kch-rgbw-lib}}. We leave them as `homework' fun.

\subsection{Not to compromise brightness $Y$}
When $Y$ in Eq.~\eqref{E_XYZ^T} is greater (brighter) than the color source's capacity, there is no solution. Here we suggested a (compromised) solution to normalize $Y_i$. But we can also take another strategy to maintain the goal brightness by compromising the goal color.

This strategy requires computation of gradient of $Y$ in $\boldsymbol{\alpha}$ space.

\subsection{LED's lifetime averaging}
In section \ref{s_assumptions} '\nameref{s_assumptions}' we introduced an optional condition \ref{I_lifetime}, 'When possible, average turn-on time of LEDs to equalize LEDs lifetime'. It is equivalent to minimize the variance of $\alpha_1, \, \ldots\, , \alpha_N$. It can be an optimization of a quadratic function.

In $N = 4$ case, we have only one variable $\beta$ therefore we can optimize only one condition. This means we need to chose between the energy minimization and this condition.

\bibliographystyle{unsrt}
\bibliography{rgbw}

\section*{MIT License}

Copyright (c) 2020 Kiyo Chinzei

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

\end{document}