\[ %% % Add your macros here; they'll be included in pdf and html output. %% \newcommand{\R}{\mathbb{R}} % reals \newcommand{\E}{\mathbb{E}} % expectation \renewcommand{\P}{\mathbb{P}} % probability \DeclareMathOperator{\logit}{logit} \DeclareMathOperator{\logistic}{logistic} \DeclareMathOperator{\SE}{SE} \DeclareMathOperator{\sd}{sd} \DeclareMathOperator{\var}{var} \DeclareMathOperator{\cov}{cov} \DeclareMathOperator{\cor}{cor} \DeclareMathOperator{\Normal}{Normal} \DeclareMathOperator{\LogNormal}{logNormal} \DeclareMathOperator{\Poisson}{Poisson} \DeclareMathOperator{\Beta}{Beta} \DeclareMathOperator{\Binom}{Binomial} \DeclareMathOperator{\Gam}{Gamma} \DeclareMathOperator{\Exp}{Exponential} \DeclareMathOperator{\Cauchy}{Cauchy} \DeclareMathOperator{\Unif}{Unif} \DeclareMathOperator{\Dirichlet}{Dirichlet} \DeclareMathOperator{\Wishart}{Wishart} \DeclareMathOperator{\StudentsT}{StudentsT} \DeclareMathOperator{\Weibull}{Weibull} \newcommand{\given}{\;\vert\;} \]

Matrix multiplication

Peter Ralph

1 December 2020 – Advanced Biological Statistics

Math minute: matrix multiplication

To simulate from: \[\begin{aligned} \mu_i &= b_0 + b_1 X_{i1} + \cdots + b_k X_{ik} \\ Y_i &\sim \Normal(\mu_i, \sigma) . \end{aligned}\]

either

coefs <- list(b0=1.0, b=c(3.0, -1.0, 0.0, 0.0),
              sigma=0.5)
n <- 200
X <- matrix(rnorm(4*n, mean=0, sd=3), ncol=4)
Y <- coefs$b0 
for (k in 1:ncol(X)) {
    Y <- Y + coefs$b[k] * X[,k]
}
Y <- Y + rnorm(n, mean=0, sd=coefs$sigma)

To simulate from: \[\begin{aligned} \mu_i &= b_0 + b_1 X_{i1} + \cdots + b_k X_{i1} \\ Y_i &\sim \Normal(\mu_i, \sigma) . \end{aligned}\]

of

coefs <- list(b0=1.0, b=c(3.0, -1.0, 0.0, 0.0),
              sigma=0.5)
n <- 200
X <- matrix(rnorm(4*n, mean=0, sd=3), ncol=4)
Y <- coefs$b0 + X %*% coefs$b + rnorm(n, mean=0, sd=coefs$sigma)

Because

In R, %*% is matrix multiplication: if

  • \(b\) is a \(k\)-vector
  • \(X\) is an \(n \times k\) matrix

then X %*% b (or, \(X b\) in math notation) is shorthand for the \(n\)-vector \[ (Xb)_i = \sum_{j=1}^k X_{ij} b_j . \]

In Stan, matrix multiplication is *.

// reveal.js plugins