In a previous assignment, you have computed weighted sums and means. If \(x_1,...,x_n\) is a set of observations and \(w_1,...,w_n\) a set of weights, then \(\sum \limits_{i=1}^n w_i x_i\) is the weighted sum.
In R, sum(w * x)
calculates the weighted sum, for vectors w
(containing \(w_1,...,w_n\)) and x
(containing \(x_1,...,x_n\)).
Write a function in C++ called weighted_sumC
which returns the weighted sum (a double
), given a NumericVector
x
and a NumericVector
w
. Import this function into R using Rcpp::cppFunction
, and verify that it works.
Compare the speed of weighted_sumC(x, w)
to sum(x * w)
and crossprod(x, w)
in R.
The sample variance of \(x_1,...,x_n\) is defined by
\(\frac{1}{n - 1} \sum \limits_{i=1}^n (x_i - \overline{x})^2\)
where \(\overline{x}\) is the sample mean.
varC
which returns the sample variance (a double
), given a NumericVector
x
. Import this function into R using Rcpp::cppFunction
, and verify that it works.Note: pow(a, b)
computes \(a^b\) in C++.
varC(x)
to var(x)
in R.Let \(A\) be an \(n \times m\) matrix (\(n\) rows and \(m\) columns), and \(B\) an \(m \times p\) matrix. The product of \(C = AB\) is a matrix of size \(n \times p\), with entries
\(C_{ij} = \sum \limits_{k=1}^m A_{ik}B_{kj}\)
matrix_productC
which takes in two NumericMatrix
objects, and returns their product (another NumericMatrix
).Note:
NumericMatrix result(a, b);
will create a NumericMatrix of 0s, with \(a\) rows and \(b\) columns.