| Title: | Automatic Differentiation of Multivariate Operations |
|---|---|
| Description: | An object that supports automatic differentiation of matrix- and multidimensional-valued functions with respect to multidimensional independent variables. Automatic differentiation is via 'forward accumulation'. |
| Authors: | Steven E. Pav [aut, cre] (ORCID: <https://orcid.org/0000-0002-4197-6195>) |
| Maintainer: | Steven E. Pav <[email protected]> |
| License: | LGPL-3 |
| Version: | 0.2.7 |
| Built: | 2026-05-08 05:44:51 UTC |
| Source: | https://github.com/shabbychef/madness |
madness value.Extract parts of a madness value.
## S4 method for signature 'madness,ANY,ANY,ANY' x[i, j, ..., drop = TRUE] ## S4 method for signature 'madness,ANY,missing,ANY' x[i, j, ..., drop = TRUE]## S4 method for signature 'madness,ANY,ANY,ANY' x[i, j, ..., drop = TRUE] ## S4 method for signature 'madness,ANY,missing,ANY' x[i, j, ..., drop = TRUE]
x |
a |
i |
indices specifying elements to extract or replace. Indices are
For When indexing arrays by An index value of |
j, ...
|
further indices specifying elements to extract or replace. |
drop |
For matrices and arrays. If |
Steven E. Pav [email protected]
Access slot data from a madness object.
val(x) ## S4 method for signature 'madness' val(x) ## S4 method for signature 'madness' dim(x) ## S4 method for signature 'madness' length(x) dvdx(x) ## S4 method for signature 'madness' dvdx(x) xtag(x) ## S4 method for signature 'madness' xtag(x) vtag(x) ## S4 method for signature 'madness' vtag(x) varx(x) ## S4 method for signature 'madness' varx(x)val(x) ## S4 method for signature 'madness' val(x) ## S4 method for signature 'madness' dim(x) ## S4 method for signature 'madness' length(x) dvdx(x) ## S4 method for signature 'madness' dvdx(x) xtag(x) ## S4 method for signature 'madness' xtag(x) vtag(x) ## S4 method for signature 'madness' vtag(x) varx(x) ## S4 method for signature 'madness' varx(x)
x |
a |
Steven E. Pav [email protected]
These perform basic arithmetic operations on madness objects: unary
plus and minus, addition, subtraction, multiplication, division and power.
## S4 method for signature 'madness,missing' e1 + e2 ## S4 method for signature 'madness,missing' e1 - e2 ## S4 method for signature 'madness,madness' e1 + e2 ## S4 method for signature 'madness,numeric' e1 + e2 ## S4 method for signature 'madness,array' e1 + e2 ## S4 method for signature 'numeric,madness' e1 + e2 ## S4 method for signature 'array,madness' e1 + e2 ## S4 method for signature 'madness,madness' e1 - e2 ## S4 method for signature 'madness,numeric' e1 - e2 ## S4 method for signature 'madness,array' e1 - e2 ## S4 method for signature 'numeric,madness' e1 - e2 ## S4 method for signature 'array,madness' e1 - e2 ## S4 method for signature 'madness,madness' e1 * e2 ## S4 method for signature 'madness,numeric' e1 * e2 ## S4 method for signature 'madness,array' e1 * e2 ## S4 method for signature 'numeric,madness' e1 * e2 ## S4 method for signature 'array,madness' e1 * e2 ## S4 method for signature 'madness,madness' e1 / e2 ## S4 method for signature 'madness,numeric' e1 / e2 ## S4 method for signature 'madness,array' e1 / e2 ## S4 method for signature 'numeric,madness' e1 / e2 ## S4 method for signature 'array,madness' e1 / e2 ## S4 method for signature 'madness,madness' e1 ^ e2 ## S4 method for signature 'madness,numeric' e1 ^ e2 ## S4 method for signature 'madness,array' e1 ^ e2 ## S4 method for signature 'numeric,madness' e1 ^ e2 ## S4 method for signature 'array,madness' e1 ^ e2## S4 method for signature 'madness,missing' e1 + e2 ## S4 method for signature 'madness,missing' e1 - e2 ## S4 method for signature 'madness,madness' e1 + e2 ## S4 method for signature 'madness,numeric' e1 + e2 ## S4 method for signature 'madness,array' e1 + e2 ## S4 method for signature 'numeric,madness' e1 + e2 ## S4 method for signature 'array,madness' e1 + e2 ## S4 method for signature 'madness,madness' e1 - e2 ## S4 method for signature 'madness,numeric' e1 - e2 ## S4 method for signature 'madness,array' e1 - e2 ## S4 method for signature 'numeric,madness' e1 - e2 ## S4 method for signature 'array,madness' e1 - e2 ## S4 method for signature 'madness,madness' e1 * e2 ## S4 method for signature 'madness,numeric' e1 * e2 ## S4 method for signature 'madness,array' e1 * e2 ## S4 method for signature 'numeric,madness' e1 * e2 ## S4 method for signature 'array,madness' e1 * e2 ## S4 method for signature 'madness,madness' e1 / e2 ## S4 method for signature 'madness,numeric' e1 / e2 ## S4 method for signature 'madness,array' e1 / e2 ## S4 method for signature 'numeric,madness' e1 / e2 ## S4 method for signature 'array,madness' e1 / e2 ## S4 method for signature 'madness,madness' e1 ^ e2 ## S4 method for signature 'madness,numeric' e1 ^ e2 ## S4 method for signature 'madness,array' e1 ^ e2 ## S4 method for signature 'numeric,madness' e1 ^ e2 ## S4 method for signature 'array,madness' e1 ^ e2
e1, e2
|
|
Steven E. Pav [email protected]
set.seed(123) y <- array(rnorm(3*3),dim=c(3,3)) dy <- matrix(rnorm(length(y)*2),ncol=2) dx <- crossprod(matrix(rnorm(ncol(dy)*100),nrow=100)) obj0 <- madness(val=y,vtag='y',xtag='x',dvdx=dy,varx=dx) z <- array(rnorm(3*3),dim=c(3,3)) anobj <- + obj0 anobj <- - obj0 anobj <- 6 - obj0 anobj <- 1 + obj0 anobj <- obj0 - 3 anobj <- z + obj0 anobj <- obj0 - z obj1 <- obj0 ^ 2 anobj <- (0.3 * obj0) + (5.1 * obj1) anobj <- 2 ^ obj0 anobj <- obj1 ^ obj0 anobj <- obj1 / obj0 anobj <- z / obj0set.seed(123) y <- array(rnorm(3*3),dim=c(3,3)) dy <- matrix(rnorm(length(y)*2),ncol=2) dx <- crossprod(matrix(rnorm(ncol(dy)*100),nrow=100)) obj0 <- madness(val=y,vtag='y',xtag='x',dvdx=dy,varx=dx) z <- array(rnorm(3*3),dim=c(3,3)) anobj <- + obj0 anobj <- - obj0 anobj <- 6 - obj0 anobj <- 1 + obj0 anobj <- obj0 - 3 anobj <- z + obj0 anobj <- obj0 - z obj1 <- obj0 ^ 2 anobj <- (0.3 * obj0) + (5.1 * obj1) anobj <- 2 ^ obj0 anobj <- obj1 ^ obj0 anobj <- obj1 / obj0 anobj <- z / obj0
Coerce as something else
## S4 method for signature 'madness' as.array(x, ...) ## S4 method for signature 'madness' as.matrix(x, ...) ## S4 method for signature 'madness' as.numeric(x, ...)## S4 method for signature 'madness' as.array(x, ...) ## S4 method for signature 'madness' as.matrix(x, ...) ## S4 method for signature 'madness' as.numeric(x, ...)
x |
a |
... |
further arguments passed to or from other methods. |
Steven E. Pav [email protected]
Convert model to a madness object.
as.madness(x, vtag=NULL, xtag=NULL) ## Default S3 method: as.madness(x, vtag = NULL, xtag = NULL)as.madness(x, vtag=NULL, xtag=NULL) ## Default S3 method: as.madness(x, vtag = NULL, xtag = NULL)
x |
an object which can be fed to |
vtag |
an optional name for the |
xtag |
an optional name for the |
Attempts to stuff the coefficients and variance-covariance matrix of a model into a madness object.
A madness object.
Steven E. Pav [email protected]
xy <- data.frame(x=rnorm(100),y=runif(100),z=runif(100)) amod <- lm(z ~ x + y,xy) amad <- as.madness(amod)xy <- data.frame(x=rnorm(100),y=runif(100),z=runif(100)) amod <- lm(z ~ x + y,xy) amad <- as.madness(amod)
Row and Column Bind
\method{c}{madness}(...) ## S4 method for signature 'madness,missing' cbind2(x, y, ...) ## S4 method for signature 'madness,madness' cbind2(x, y, ...) ## S4 method for signature 'madness,ANY' cbind2(x, y, ...) ## S4 method for signature 'ANY,madness' cbind2(x, y, ...) ## S4 method for signature 'madness,missing' rbind2(x, y, ...) ## S4 method for signature 'madness,madness' rbind2(x, y, ...) ## S4 method for signature 'madness,ANY' rbind2(x, y, ...) ## S4 method for signature 'ANY,madness' rbind2(x, y, ...)\method{c}{madness}(...) ## S4 method for signature 'madness,missing' cbind2(x, y, ...) ## S4 method for signature 'madness,madness' cbind2(x, y, ...) ## S4 method for signature 'madness,ANY' cbind2(x, y, ...) ## S4 method for signature 'ANY,madness' cbind2(x, y, ...) ## S4 method for signature 'madness,missing' rbind2(x, y, ...) ## S4 method for signature 'madness,madness' rbind2(x, y, ...) ## S4 method for signature 'madness,ANY' rbind2(x, y, ...) ## S4 method for signature 'ANY,madness' rbind2(x, y, ...)
... |
optional arguments for methods (ignored here). |
x, y
|
|
Steven E. Pav [email protected]
Replicates a multidimensional object a number of times along given dimensions.
blockrep(x, nreps) repto(x, newdim) repto(x, newdim)blockrep(x, nreps) repto(x, newdim) repto(x, newdim)
x |
a |
nreps |
an l-vector of positive integers, representing how many times to copy the object. |
newdim |
an l-vector of positive integers of the new dimension of the output object. These must be integer multiples of the input dimensions. |
Given a k-dimensional object, and an l-vector of positive integers, for l >= k, copy the input object l_i times in the ith dimension. Useful for replication and (slow, fake) outer products.
repto replicates to the given dimension, assuming the
given dimension are integer multiples of the input dimensions.
A madness object replicated out.
An error will be thrown if nreps or newdim are improper.
Steven E. Pav [email protected]
set.seed(123) y <- array(rnorm(3*3),dim=c(3,3)) dy <- matrix(rnorm(length(y)*2),ncol=2) dx <- crossprod(matrix(rnorm(ncol(dy)*100),nrow=100)) obj0 <- madness(val=y,vtag='y',xtag='x',dvdx=dy,varx=dx) anobj <- blockrep(obj0,c(1,2,1)) anobj <- blockrep(obj0,c(1,1,2)) anobj <- repto(obj0,c(9,12,4))set.seed(123) y <- array(rnorm(3*3),dim=c(3,3)) dy <- matrix(rnorm(length(y)*2),ncol=2) dx <- crossprod(matrix(rnorm(ncol(dy)*100),nrow=100)) obj0 <- madness(val=y,vtag='y',xtag='x',dvdx=dy,varx=dx) anobj <- blockrep(obj0,c(1,2,1)) anobj <- blockrep(obj0,c(1,1,2)) anobj <- repto(obj0,c(9,12,4))
Form Row and Column Sums and Means for madness objects.
## S4 method for signature 'madness' colSums(x, na.rm = FALSE, dims = 1) ## S4 method for signature 'madness' colMeans(x, na.rm = FALSE, dims = 1) ## S4 method for signature 'madness' rowSums(x, na.rm = FALSE, dims = 1) ## S4 method for signature 'madness' rowMeans(x, na.rm = FALSE, dims = 1)## S4 method for signature 'madness' colSums(x, na.rm = FALSE, dims = 1) ## S4 method for signature 'madness' colMeans(x, na.rm = FALSE, dims = 1) ## S4 method for signature 'madness' rowSums(x, na.rm = FALSE, dims = 1) ## S4 method for signature 'madness' rowMeans(x, na.rm = FALSE, dims = 1)
x |
|
na.rm |
logical. Should missing values (including |
dims |
integer: Which dimensions are regarded as ‘rows’ or
‘columns’ to sum over. For |
a madness object. Note that the sums are flattened to a
column vector.
Steven E. Pav [email protected]
Compute the determinant of a matrix. As for base::determinant,
a list of the modulus and sign are returned.
## S3 method for class 'madness' determinant(x, logarithm = TRUE, ...) det(x, ...) ## S4 method for signature 'madness,ANY' determinant(x, logarithm = TRUE, ...) ## S4 method for signature 'madness,missing' determinant(x, logarithm = TRUE, ...) ## S4 method for signature 'madness,logical' determinant(x, logarithm = TRUE, ...)## S3 method for class 'madness' determinant(x, logarithm = TRUE, ...) det(x, ...) ## S4 method for signature 'madness,ANY' determinant(x, logarithm = TRUE, ...) ## S4 method for signature 'madness,missing' determinant(x, logarithm = TRUE, ...) ## S4 method for signature 'madness,logical' determinant(x, logarithm = TRUE, ...)
x |
|
logarithm |
logical; if |
... |
Optional arguments. At present none are used. Previous
versions of |
a list with elements modulus and sign,
which are madness objects.
throws an error for non-square matrices or non-matrix input.
Steven E. Pav [email protected]
Computes eigenvalues and eigenvectors of numeric (double, integer, logical) or
complex madness matrices.
## S4 method for signature 'madness' eigen(x, symmetric, only.values = FALSE, EISPACK = FALSE)## S4 method for signature 'madness' eigen(x, symmetric, only.values = FALSE, EISPACK = FALSE)
x |
|
symmetric |
if |
only.values |
if |
EISPACK |
logical. Defunct and ignored. |
The singular value decomposition of the matrix is
where and are orthogonal, is
transposed, and is a diagonal matrix with the singular
values on the diagonal.
a list with components
a madness object of a vector containing
the eigenvalues of x, sorted in decreasing order,
according to Mod(value) in the assymetric case when they might
be complex (even for real matrices). For real asymmetric matrices
the vector will be complex only if complex conjugate pairs of eigenvalues are
detected.
either a matrix whose columns contain the
eigenvectors of x or NULL if only.values is
TRUE. The vectors are normalized to unit length.
Recall that the eigenvectors are only defined up to a constant:
even when the length is specified they are still only defined up to a
scalar of modulus one (the sign for real matrices).
If r <- eigen(A), and V <- r$vectors; lam <- r$values, then
(up to numerical fuzz), where Lmbd =diag(lam).
Steven E. Pav [email protected]
Izenman, Alan Julian. "Reduced-Rank Regression for the Multivariate Linear Model." Journal of Multivariate Analysis 5, pp 248-264 (1975). http://www.sciencedirect.com/science/article/pii/0047259X75900421
Kato, Tosio. "Perturbation Theory for Linear Operators." Springer (1995). http://www.maths.ed.ac.uk/~aar/papers/kato1.pdf
Element-wise multivariate operations.
## S4 method for signature 'madness' abs(x) ## S4 method for signature 'madness' exp(x) ## S4 method for signature 'madness' log(x) ## S4 method for signature 'madness' log10(x) ## S4 method for signature 'madness' sqrt(x) ## S4 method for signature 'madness' sin(x) ## S4 method for signature 'madness' cos(x) ## S4 method for signature 'madness' tan(x)## S4 method for signature 'madness' abs(x) ## S4 method for signature 'madness' exp(x) ## S4 method for signature 'madness' log(x) ## S4 method for signature 'madness' log10(x) ## S4 method for signature 'madness' sqrt(x) ## S4 method for signature 'madness' sin(x) ## S4 method for signature 'madness' cos(x) ## S4 method for signature 'madness' tan(x)
x |
|
These operations are scalar-to-scalar operations applied to each element of a multidimensional array.
The exp, log, and sqrt
functions are not to be confused with the matrix-wise operations,
expm, logm and sqrtm
Steven E. Pav [email protected]
An S4 class to enable forward differentiation of multivariate computations.
Think of ‘madness’ as ‘multivariate automatic differentiation -ness.’
There is also a constructor method for madness objects, and a
wrapper method.
## S4 method for signature 'madness' initialize( .Object, val, dvdx, xtag = NA_character_, vtag = NA_character_, varx = matrix(nrow = 0, ncol = 0) ) madness(val, dvdx = NULL, vtag = NULL, xtag = NULL, varx = NULL)## S4 method for signature 'madness' initialize( .Object, val, dvdx, xtag = NA_character_, vtag = NA_character_, varx = matrix(nrow = 0, ncol = 0) ) madness(val, dvdx = NULL, vtag = NULL, xtag = NULL, varx = NULL)
.Object |
a |
val |
an |
dvdx |
a |
xtag |
an optional name for the |
vtag |
an optional name for the |
varx |
an optional variance-covariance matrix of
the independent variable, |
A madness object contains a (multidimensional)
value, and the derivative of that with respect to some independent
variable. The purpose is to simplify computation of multivariate
derivatives, especially for use in the Delta method. Towards this
usage, one may store the covariance of the independent variable
in the object as well, from which the approximate variance-covariance
matrix can easily be computed. See vcov.
Note that derivatives are all implicitly 'flattened'. That is,
when we talk of the derivative of
matrix with respect to
matrix , we mean the derivative of the
vector with
respect to the vector
. Moreover,
derivatives follow the 'numerator layout' convention:
this derivative is a matrix
whose first column is the derivative of
with respect
to . Numerator layout feels unnatural
because it makes a gradient vector of a scalar-valued function
into a row vector. Despite this deficiency, it makes
the product rule feel more natural. (2FIX: is this so?)
An object of class madness.
valan array of some numeric value. (Note that
array includes matrix as a subclass.) The numeric
value can have arbitrary dimension.
dvdxa matrix of the derivative of
(the vector of) val with respect to some independent
variable, .
A Derivative is indeed a 2-dimensional matrix.
Derivatives have all been 'flattened'. See the details.
If not given, defaults
to the identity matrix, in which case ,
which is useful to initialization. Note that the derivative
is with respect to an 'unrestricted' .
xtagan optional name for the variable.
Operations between two objects of the class with distinct
xtag data will result in an error, since they are
considered to have different independent variables.
vtagan optional name for the variable.
This will be propagated forward.
varxan optional variance-covariance matrix of
the independent variable, .
Steven E. Pav [email protected]
Petersen, Kaare Brandt and Pedersen, Michael Syskind. "The Matrix Cookbook." Technical University of Denmark (2012). http://www2.imm.dtu.dk/pubdb/p.php?3274
Magnus, Jan R. and Neudecker, H. "Matrix Differential Calculus with Applications in Statistics and Econometrics." 3rd Edition. Wiley Series in Probability and Statistics: Texts and References Section (2007).
obj <- new("madness", val=matrix(rnorm(10*10),nrow=10), dvdx=diag(100), xtag="foo", vtag="foo") obj2 <- madness(val=matrix(rnorm(10*10),nrow=10), xtag="foo", vtag="foo^2")obj <- new("madness", val=matrix(rnorm(10*10),nrow=10), dvdx=diag(100), xtag="foo", vtag="foo") obj2 <- madness(val=matrix(rnorm(10*10),nrow=10), xtag="foo", vtag="foo^2")
News for package ‘madness’.
emergency CRAN release to deal with ellipsis in documentation.
emergency CRAN release to deal with change in generic
signature for colSums, colMeans,
rowSums, rowMeans.
emergency CRAN release to deal with failing vignette on alternative BLAS.
adding to unit tests.
fix scalar to array promotion.
fix broken vtag in aperm.
add FF3 and stock returns data to build vignette.
emergency CRAN release to deal with failing tests under alternative BLAS/LAPACK libraries.
emergency CRAN release for upstream changes to diag.
thanks to Martin Maechler for the patch.
emergency CRAN release for failed build.
no new functionality.
add static vignette.
modify twomoments.
release to CRAN.
adding max and min.
adding eigen.
exporting diag.
first CRAN release.
first github release.
Automatic Differentiation of Matrix Operations.
madness is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
This package is maintained as a hobby.
Steven E. Pav [email protected]
Steven E. Pav [email protected]
Griewank, Andreas and Walther, Andrea. "Evaluating Derivatives: principles and techniques of algorithmic differentiation." SIAM (2008).
Petersen, Kaare Brandt and Pedersen, Michael Syskind. "The Matrix Cookbook." Technical University of Denmark (2012). http://www2.imm.dtu.dk/pubdb/p.php?3274
Magnus, Jan R. and Neudecker, H. "Matrix Differential Calculus with Applications in Statistics and Econometrics." 3rd Edition. Wiley Series in Probability and Statistics: Texts and References Section (2007).
Magnus, Jan R. and Neudecker, H. "The elimination matrix: some lemmas and applications," SIAM Journal on Algebraic Discrete Methods 1, no. 4 (1980): 422-449. http://www.janmagnus.nl/papers/JRM008.pdf
Magnus, Jan R. and Neudecker, H. "Symmetry, 0-1 Matrices and Jacobians," Econometric Theory 2 (1986): 157-190. http://www.janmagnus.nl/papers/JRM014.pdf,
Fackler, Paul L. "Notes on Matrix Calculus." (2005). http://www4.ncsu.edu/~pfackler/MatCalc.pdf
MM: DO use @importFrom (whimps use full imports ..) !
These perform basic matrix arithmetic on madness objects: matrix
multiplication, cross product, Kronecker product.
## S4 method for signature 'madness,madness' x %*% y ## S4 method for signature 'madness,array' x %*% y ## S4 method for signature 'array,madness' x %*% y ## S4 method for signature 'madness,madness' crossprod(x, y) ## S4 method for signature 'madness,ANY' crossprod(x, y) ## S4 method for signature 'madness,missing' crossprod(x, y) ## S4 method for signature 'ANY,madness' crossprod(x, y) ## S4 method for signature 'madness,madness' tcrossprod(x, y) ## S4 method for signature 'madness,ANY' tcrossprod(x, y) ## S4 method for signature 'madness,missing' tcrossprod(x, y) ## S4 method for signature 'ANY,madness' tcrossprod(x, y)## S4 method for signature 'madness,madness' x %*% y ## S4 method for signature 'madness,array' x %*% y ## S4 method for signature 'array,madness' x %*% y ## S4 method for signature 'madness,madness' crossprod(x, y) ## S4 method for signature 'madness,ANY' crossprod(x, y) ## S4 method for signature 'madness,missing' crossprod(x, y) ## S4 method for signature 'ANY,madness' crossprod(x, y) ## S4 method for signature 'madness,madness' tcrossprod(x, y) ## S4 method for signature 'madness,ANY' tcrossprod(x, y) ## S4 method for signature 'madness,missing' tcrossprod(x, y) ## S4 method for signature 'ANY,madness' tcrossprod(x, y)
x, y
|
|
Steven E. Pav [email protected]
set.seed(123) y <- array(rnorm(3*3),dim=c(3,3)) dy <- matrix(rnorm(length(y)*2),ncol=2) dx <- crossprod(matrix(rnorm(ncol(dy)*100),nrow=100)) obj0 <- madness(val=y,vtag='y',xtag='x',dvdx=dy,varx=dx) z <- array(rnorm(3*3),dim=c(3,3)) anobj <- obj0 %*% obj0 anobj <- z %*% obj0 anobj <- crossprod(obj0) anobj <- crossprod(obj0,z) anobj <- tcrossprod(obj0,obj0) # NYI: # anobj <- obj0 %x% obj0set.seed(123) y <- array(rnorm(3*3),dim=c(3,3)) dy <- matrix(rnorm(length(y)*2),ncol=2) dx <- crossprod(matrix(rnorm(ncol(dy)*100),nrow=100)) obj0 <- madness(val=y,vtag='y',xtag='x',dvdx=dy,varx=dx) z <- array(rnorm(3*3),dim=c(3,3)) anobj <- obj0 %*% obj0 anobj <- z %*% obj0 anobj <- crossprod(obj0) anobj <- crossprod(obj0,z) anobj <- tcrossprod(obj0,obj0) # NYI: # anobj <- obj0 %x% obj0
Matrix Trace
matrix.trace(x) ## S4 method for signature 'ANY' matrix.trace(x) ## S4 method for signature 'madness' matrix.trace(x)matrix.trace(x) ## S4 method for signature 'ANY' matrix.trace(x) ## S4 method for signature 'madness' matrix.trace(x)
x |
|
Steven E. Pav [email protected]
Element-wise multivariate operations.
## S4 method for signature 'madness' sqrtm(x) ## S3 method for class 'madness' chol(x, ...)## S4 method for signature 'madness' sqrtm(x) ## S3 method for class 'madness' chol(x, ...)
x |
|
... |
further arguments passed to or from other methods. |
These operations are operations on matrices: compute the symmetric square root or the Cholesky factor. In the future, the matrix exponent and logarithm may be implemented?
Steven E. Pav [email protected]
Return the maxima and minima of the input values.
## S4 method for signature 'madness' max(x, ..., na.rm = FALSE) ## S4 method for signature 'madness' min(x, ..., na.rm = FALSE)## S4 method for signature 'madness' max(x, ..., na.rm = FALSE) ## S4 method for signature 'madness' min(x, ..., na.rm = FALSE)
x |
|
... |
|
na.rm |
a logical indicating whether missing values should be removed. |
max and min return the maximum or minimum of all the
values present in their arguments.
If na.rm is FALSE and NA value in any of the arguments
will cause a value of NA to be returned, otherwise NA values are
ignored.
The minimum and maximum of a numeric empty set are +Inf and
-Inf (in this order!) which ensures transitivity, e.g.,
min(x1, min(x2)) == min(x1, x2). For numeric x
max(x) == -Inf and min(x) == +Inf whenever
length(x) == 0 (after removing missing values if requested).
Steven E. Pav [email protected]
Compute the norm of a vector or matrix, as determined by the
type.
maxeig(x) ## S4 method for signature 'madness' maxeig(x) ## S4 method for signature 'madness,missing' norm(x) ## S4 method for signature 'madness,ANY' norm(x, type = "One")maxeig(x) ## S4 method for signature 'madness' maxeig(x) ## S4 method for signature 'madness,missing' norm(x) ## S4 method for signature 'madness,ANY' norm(x, type = "One")
x |
|
type |
character string, specifying the type of matrix norm to be computed. A character indicating the type of norm desired.
The default is |
the matrix norm, a non-negative number.
This should probably be fixed to return a scalar, not a 1 by 1 matrix?
Steven E. Pav [email protected]
Approximates the derivative of a function at the input by numerical methods.
numderiv(f, x, eps=1e-8, type=c('forward','central','backward'),...) ## S4 method for signature 'ANY,array' numderiv(f, x, eps = 1e-08, type = c("forward", "central", "backward"), ...) ## S4 method for signature 'ANY,madness' numderiv(f, x, eps = 1e-08, type = c("forward", "central", "backward"), ...)numderiv(f, x, eps=1e-8, type=c('forward','central','backward'),...) ## S4 method for signature 'ANY,array' numderiv(f, x, eps = 1e-08, type = c("forward", "central", "backward"), ...) ## S4 method for signature 'ANY,madness' numderiv(f, x, eps = 1e-08, type = c("forward", "central", "backward"), ...)
f |
a function, to be evaluated at and near |
x |
array, matrix, or |
eps |
the 'epsilon', a small value added or subtracted from |
type |
the type of first difference, case-insensitive, substrings ok. |
... |
arguments passed on to |
For a multivariate-valued function of multivariate
data, approximates the derivative at a point via the
forward, central, or backward first differences,
returning a madness object.
A matrix if x is numeric; a madness object if
x is a madness object.
Steven E. Pav [email protected]
f <- function(x,h) { cos(x + h) } x <- array(rnorm(100),dim=c(10,10)) madx <- numderiv(f,x,1e-8,h=pi)f <- function(x,h) { cos(x + h) } x <- array(rnorm(100),dim=c(10,10)) madx <- numderiv(f,x,1e-8,h=pi)
Computes the outer product (or sum, quotient, etc) of the Cartesian product of two inputs.
## S4 method for signature 'ANY,ANY' outer(X, Y, FUN = "*", ...) ## S4 method for signature 'madness,madness' outer(X, Y, FUN = "*", ...) ## S4 method for signature 'madness,array' outer(X, Y, FUN = "*", ...) ## S4 method for signature 'array,madness' outer(X, Y, FUN = "*", ...) X %o% Y ## S4 method for signature 'madness,madness' kronecker(X, Y) ## S4 method for signature 'madness,array' kronecker(X, Y) ## S4 method for signature 'array,madness' kronecker(X, Y)## S4 method for signature 'ANY,ANY' outer(X, Y, FUN = "*", ...) ## S4 method for signature 'madness,madness' outer(X, Y, FUN = "*", ...) ## S4 method for signature 'madness,array' outer(X, Y, FUN = "*", ...) ## S4 method for signature 'array,madness' outer(X, Y, FUN = "*", ...) X %o% Y ## S4 method for signature 'madness,madness' kronecker(X, Y) ## S4 method for signature 'madness,array' kronecker(X, Y) ## S4 method for signature 'array,madness' kronecker(X, Y)
X, Y
|
|
FUN |
a function to use on the outer products, found via
|
... |
optional arguments to be passed to |
a madness object.
Steven E. Pav [email protected]
set.seed(123) y <- array(rnorm(3*3),dim=c(3,3)) dy <- matrix(rnorm(length(y)*2),ncol=2) dx <- crossprod(matrix(rnorm(ncol(dy)*100),nrow=100)) obj0 <- madness(val=y,vtag='y',xtag='x',dvdx=dy,varx=dx) y1 <- array(rnorm(3*3),dim=c(3,3)) dy1 <- matrix(rnorm(length(y1)*2),ncol=2) dx1 <- crossprod(matrix(rnorm(ncol(dy1)*100),nrow=100)) obj1 <- madness(val=y1,vtag='y1',xtag='x',dvdx=dy1,varx=dx1) anobj <- outer(obj0,obj0,'*') anobj <- outer(obj0,obj0,'+') anobj <- outer(obj0,obj1,'-') anobj <- outer(obj0,obj1,'/')set.seed(123) y <- array(rnorm(3*3),dim=c(3,3)) dy <- matrix(rnorm(length(y)*2),ncol=2) dx <- crossprod(matrix(rnorm(ncol(dy)*100),nrow=100)) obj0 <- madness(val=y,vtag='y',xtag='x',dvdx=dy,varx=dx) y1 <- array(rnorm(3*3),dim=c(3,3)) dy1 <- matrix(rnorm(length(y1)*2),ncol=2) dx1 <- crossprod(matrix(rnorm(ncol(dy1)*100),nrow=100)) obj1 <- madness(val=y1,vtag='y1',xtag='x',dvdx=dy1,varx=dx1) anobj <- outer(obj0,obj0,'*') anobj <- outer(obj0,obj0,'+') anobj <- outer(obj0,obj1,'-') anobj <- outer(obj0,obj1,'/')
Basic Reshape Operations
## S4 method for signature 'madness' t(x) ## S4 method for signature 'madness' tril(x, k = 0) ## S4 method for signature 'madness' triu(x, k = 0) ## S4 replacement method for signature 'madness' dim(x) <- value ## S3 method for class 'madness' aperm(a, perm = NULL, resize = TRUE, ...)## S4 method for signature 'madness' t(x) ## S4 method for signature 'madness' tril(x, k = 0) ## S4 method for signature 'madness' triu(x, k = 0) ## S4 replacement method for signature 'madness' dim(x) <- value ## S3 method for class 'madness' aperm(a, perm = NULL, resize = TRUE, ...)
x |
|
k |
the index of the diagonal number from which to extract. |
value |
an array of the new dimensions of the object value. |
a |
the array to be transposed. |
perm |
the subscript permutation vector, usually a permutation of
the integers |
resize |
a flag indicating whether the vector should be
resized as well as having its elements reordered (default |
... |
Optional arguments used by specific methods. (None used at present.) |
Steven E. Pav [email protected]
Modify slot data of a madness object. Note that the value and the
derivative cannot easily be changed, as allowing this form of access would
likely result in badly computed derivatives.
xtag(x) <- value ## S4 replacement method for signature 'madness' xtag(x) <- value vtag(x) <- value ## S4 replacement method for signature 'madness' vtag(x) <- value varx(x) <- value ## S4 replacement method for signature 'madness' varx(x) <- valuextag(x) <- value ## S4 replacement method for signature 'madness' xtag(x) <- value vtag(x) <- value ## S4 replacement method for signature 'madness' vtag(x) <- value varx(x) <- value ## S4 replacement method for signature 'madness' varx(x) <- value
x |
a |
value |
the new value of the tag or derivative. |
Steven E. Pav [email protected]
Displays the madness object.
show(object) ## S4 method for signature 'madness' show(object)show(object) ## S4 method for signature 'madness' show(object)
object |
a |
Steven E. Pav [email protected]
obj <- madness(val=matrix(rnorm(10*10),nrow=10), xtag="foo", vtag="foo^2") objobj <- madness(val=matrix(rnorm(10*10),nrow=10), xtag="foo", vtag="foo^2") obj
Basic Matrix Inversion
## S4 method for signature 'ANY,missing' solve(a, b) ## S4 method for signature 'madness,missing' solve(a, b) ## S4 method for signature 'madness,madness' solve(a, b) ## S4 method for signature 'madness,array' solve(a, b) ## S4 method for signature 'madness,ANY' solve(a, b) ## S4 method for signature 'array,madness' solve(a, b) ## S4 method for signature 'ANY,madness' solve(a, b)## S4 method for signature 'ANY,missing' solve(a, b) ## S4 method for signature 'madness,missing' solve(a, b) ## S4 method for signature 'madness,madness' solve(a, b) ## S4 method for signature 'madness,array' solve(a, b) ## S4 method for signature 'madness,ANY' solve(a, b) ## S4 method for signature 'array,madness' solve(a, b) ## S4 method for signature 'ANY,madness' solve(a, b)
a, b
|
|
Steven E. Pav [email protected]
Historical weekly relative returns of common shares of IBM and AAPL, downloaded from Quandl.
data(stock_returns)data(stock_returns)
A data.frame object with 1930 observations and 3 columns
The columns are defined as follows:
DateThe closing date at which the return was observed, as a Date object.
These are Friday dates, ranging from January 1981 through December 2017.
AAPLThe simple returns of AAPL common shares, based on
weekly (adjusted) close prices. A value of 0.01 corresponds to a one percent return.
Close prices are adjusted for splits and dividends by Quandl.
IBMThe simple returns of IBM common shares, based on
weekly (adjusted) close prices. A value of 0.01 corresponds to a one percent return.
Close prices are adjusted for splits and dividends by Quandl.
Steven E. Pav [email protected]
Data were collated from Quandl on August 25, 2018, from https://www.quandl.com/data/EOD/AAPL-Apple-Inc-AAPL-Stock-Prices-Dividends-and-Splits and https://www.quandl.com/data/EOD/IBM-International-Business-Machines-Corporation-IBM-Stock-Prices-Dividends-and-Splits.
data(stock_returns) str(stock_returns)data(stock_returns) str(stock_returns)
Compute sum or product of madness objects.
## S4 method for signature 'madness' sum(x, ..., na.rm = FALSE) ## S4 method for signature 'madness' prod(x, ..., na.rm = FALSE)## S4 method for signature 'madness' sum(x, ..., na.rm = FALSE) ## S4 method for signature 'madness' prod(x, ..., na.rm = FALSE)
x |
a numeric or |
... |
ignored here. |
na.rm |
logical. Should missing values (including ‘NaN’) be removed? |
a madness object representing a scalar value.
Steven E. Pav [email protected]
xv <- matrix(rnorm(5*5),ncol=5) xmad <- madness(xv) prod(xv) sum(xv)xv <- matrix(rnorm(5*5),ncol=5) xmad <- madness(xv) prod(xv) sum(xv)
Given rows of observations of some vector (or multidimensional
data), estimates the second moment by taking a simple mean,
returning a madness object.
theta(X, vcov.func=vcov, xtag=NULL)theta(X, vcov.func=vcov, xtag=NULL)
X |
a multidimensional array (or a data frame) of observed values. |
vcov.func |
a function which takes an object of class |
xtag |
an optional string tag giving the name of the input data. defaults to figuring it out from the input expression. |
Given a
array whose 'rows' are independent observations of , computes the
array of the mean of based on observations,
returned as a madness object. The variance-covariance
is also estimated, and stored in the object.
One may use the default method for computing covariance,
via the vcov function, or via a 'fancy' estimator,
like sandwich:vcovHAC, sandwich:vcovHC, etc.
A madness object representing the mean of the outer
product of the tail dimensions of X.
Steven E. Pav [email protected]
set.seed(123) X <- matrix(rnorm(1000*3),ncol=3) th <- theta(X) ## Not run: if (require(sandwich)) { th2 <- theta(X,vcov.func=vcovHC) } ## End(Not run) # works on data frames too: set.seed(456) X <- data.frame(a=runif(100),b=rnorm(100),c=1) th <- theta(X)set.seed(123) X <- matrix(rnorm(1000*3),ncol=3) th <- theta(X) ## Not run: if (require(sandwich)) { th2 <- theta(X,vcov.func=vcovHC) } ## End(Not run) # works on data frames too: set.seed(456) X <- data.frame(a=runif(100),b=rnorm(100),c=1) th <- theta(X)
Given a madness object representing a scalar value, strip out
that value and attach an attribute of its derivative as a gradient.
This is a convenience method that simplifies construction of objective
functions for optimization routines.
to_objective(X)to_objective(X)
X |
a |
A scalar numeric with a gradient attribute of the derivative.
An error will be thrown if the value is not a scalar.
Steven E. Pav [email protected]
# an objective function for matrix factorization with penalty: fitfun <- function(R,L,Y,nu=-0.1) { dim(R) <- c(length(R),1) Rmad <- madness(R) dim(Rmad) <- c(ncol(L),ncol(Y)) Err <- Y - L %*% Rmad penalty <- sum(exp(nu * Rmad)) fit <- norm(Err,'f') + penalty to_objective(fit) } set.seed(1234) L <- array(runif(30*5),dim=c(30,5)) Y <- array(runif(nrow(L)*20),dim=c(nrow(L),20)) R0 <- array(runif(ncol(L)*ncol(Y)),dim=c(ncol(L),ncol(Y))) obj0 <- fitfun(R0,L,Y) fooz <- nlm(fitfun, R0, L, Y, iterlim=3)# an objective function for matrix factorization with penalty: fitfun <- function(R,L,Y,nu=-0.1) { dim(R) <- c(length(R),1) Rmad <- madness(R) dim(Rmad) <- c(ncol(L),ncol(Y)) Err <- Y - L %*% Rmad penalty <- sum(exp(nu * Rmad)) fit <- norm(Err,'f') + penalty to_objective(fit) } set.seed(1234) L <- array(runif(30*5),dim=c(30,5)) Y <- array(runif(nrow(L)*20),dim=c(nrow(L),20)) R0 <- array(runif(ncol(L)*ncol(Y)),dim=c(ncol(L),ncol(Y))) obj0 <- fitfun(R0,L,Y) fooz <- nlm(fitfun, R0, L, Y, iterlim=3)
Diagonal Operations
## S4 method for signature 'madness' diag(x) todiag(x) ## S4 method for signature 'madness' todiag(x)## S4 method for signature 'madness' diag(x) todiag(x) ## S4 method for signature 'madness' todiag(x)
x |
|
the (somewhat odd) use of stats::diag for two different
functions is not repeated here, at least for now.
Steven E. Pav [email protected]
Given rows of observations of some vector (or multidimensional
data), estimates the mean and covariance of the values,
returning two madness objects. These have a common covariance
and 'xtag', so can be combined together.
twomoments(X, diag.only=FALSE, vcov.func=vcov, xtag=NULL, df=NULL)twomoments(X, diag.only=FALSE, vcov.func=vcov, xtag=NULL, df=NULL)
X |
a multidimensional array (or a data frame) of observed values. |
diag.only |
logical flag, defaulting to |
vcov.func |
a function which takes an object of class |
xtag |
an optional string tag giving the name of the input data. defaults to figuring it out from the input expression. |
df |
the number of degrees of freedom to subtract from the sample size in the denominator of the covariance matrix estimate. The default value is the number of elements in the mean, the so-called Bessel's correction. |
Given a
array whose 'rows' are independent observations of , computes the
array of the mean of and the
array of the covariance, based on observations,
returned as two madness objects. The variance-covariance
of each is estimated. The two objects have the same 'xtag', and so
may be combined together.
When the diag.only=TRUE, only the diagonal of the covariance is
computed and returned.
One may use the default method for computing covariance,
via the vcov function, or via a 'fancy' estimator,
like sandwich:vcovHAC, sandwich:vcovHC, etc.
A two element list. When diag.only=FALSE, the first
element of the list is mu, representing the mean,
a madness object, the second is Sigma, representing the covariance,
also a madness object. When diag.only=TRUE, the first element
is mu, but the second is sigmasq, a madness object
representing the diagonal of the covariance matrix.
Steven E. Pav [email protected]
set.seed(123) X <- matrix(rnorm(1000*8),ncol=8) alst <- twomoments(X) markowitz <- solve(alst$Sigma,alst$mu) vcov(markowitz) # now compute the Sharpe ratios: alst <- twomoments(X,diag.only=TRUE,df=1) srs <- alst$mu / sqrt(alst$sigmasq)set.seed(123) X <- matrix(rnorm(1000*8),ncol=8) alst <- twomoments(X) markowitz <- solve(alst$Sigma,alst$mu) vcov(markowitz) # now compute the Sharpe ratios: alst <- twomoments(X,diag.only=TRUE,df=1) srs <- alst$mu / sqrt(alst$sigmasq)
Returns the variance-covariance matrix of the parameters
computed by a madness object.
## S3 method for class 'madness' vcov(object, ...)## S3 method for class 'madness' vcov(object, ...)
object |
a |
... |
additional arguments for method functions. Ignored here. |
Let represent some quantity which is estimated from
data. Let be the (known or estimated)
variance-covariance matrix of . If
is some computed function of , then, by the
Delta method (which is a first order Taylor approximation),
the variance-covariance matrix of is approximately
where the derivatives are defined over the 'unrolled' (or vectorized)
and .
Note that can represent a multidimensional quantity. Its
variance covariance matrix, however, is two dimensional, as it too
is defined over the 'unrolled' .
A matrix of the estimated covariances between the values being
estimated by the madness object. While may be
multidimensional, the return value is a square matrix whose side length
is the number of elements of
Steven E. Pav [email protected]
vcov.
y <- array(rnorm(2*3),dim=c(2,3)) dy <- matrix(rnorm(length(y)*2),ncol=2) dx <- crossprod(matrix(rnorm(ncol(dy)*100),nrow=100)) obj <- madness(val=y,dvdx=dy,varx=dx) print(vcov(obj))y <- array(rnorm(2*3),dim=c(2,3)) dy <- matrix(rnorm(length(y)*2),ncol=2) dx <- crossprod(matrix(rnorm(ncol(dy)*100),nrow=100)) obj <- madness(val=y,dvdx=dy,varx=dx) print(vcov(obj))
Turn a multidimensional array into a (column) vector. Turn a (typically symmetric) matrix into a (column) vector of the lower triangular part. Or reverse these operations.
vec(x) ## S4 method for signature 'madness' vec(x) ## S4 method for signature 'array' vec(x) vech(x, k = 0) ## S4 method for signature 'array' vech(x, k = 0) ## S4 method for signature 'madness' vech(x, k = 0) ivech(x, k = 0, symmetric = FALSE) ## S4 method for signature 'ANY' ivech(x, k = 0, symmetric = FALSE) ## S4 method for signature 'madness' ivech(x, k = 0, symmetric = FALSE)vec(x) ## S4 method for signature 'madness' vec(x) ## S4 method for signature 'array' vec(x) vech(x, k = 0) ## S4 method for signature 'array' vech(x, k = 0) ## S4 method for signature 'madness' vech(x, k = 0) ivech(x, k = 0, symmetric = FALSE) ## S4 method for signature 'ANY' ivech(x, k = 0, symmetric = FALSE) ## S4 method for signature 'madness' ivech(x, k = 0, symmetric = FALSE)
x |
a |
k |
the diagonal from which to subselect. |
symmetric |
logical whether to put the array on the antidiagonal
as well. Will throw an error if |
a madness object or an array, of the vectorized array
or the subselected part. For the inverse operations, promotes to a
madness of a matrix, or a matrix.
Steven E. Pav [email protected]
reshapes, in particular tril.
y <- matrix(rnorm(16),ncol=4) sy <- y + t(y) vy <- vec(sy) vmy <- vec(madness(sy)) vhy <- vech(sy) vmhy <- vech(madness(sy)) ivech(c(1,2,3)) ivech(c(1,2,3),-1) ivech(c(1,2,3),-1,symmetric=TRUE) ivech(c(1,2,3,4,5,6,7,8),1)y <- matrix(rnorm(16),ncol=4) sy <- y + t(y) vy <- vec(sy) vmy <- vec(madness(sy)) vhy <- vech(sy) vmhy <- vech(madness(sy)) ivech(c(1,2,3)) ivech(c(1,2,3),-1) ivech(c(1,2,3),-1,symmetric=TRUE) ivech(c(1,2,3,4,5,6,7,8),1)
The weekly returns of the 3 Fama French Factors: Market, the cap factor SMB, and the growth factor HML.
wff3wff3
A data.frame object with 4800 observations and 5 columns.
The data run from July, 1926 through June, 2018.
As in the upstream source, the data are given in percents, meaning a
value of 1.00 corresponds to a 1% movement.
Note also that returns presumably are ‘simple’ returns, not log
returns, though this is not clarified by the upstream source.
The columns are defined as follows:
DateThe closing data, as a Date object. These are typically Saturdays.
MktThe Market weekly return. Note that the risk free rate has been added back to the excess returns published by the upstream source.
SMBThe cap factor weekly return.
HMLThe growth factor weekly return.
RFThe risk-free rate, presumably as an weekly rate, though note that no corrections have been made for weekend effects when adding the risk-free rate back to the market rate.
Steven E. Pav [email protected]
Kenneth French data library, via Quandl. See http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/data_library.html, data description at http://mba.tuck.dartmouth.edu/pages/faculty/ken.french/Data_Library/f-f_factors.html.
data(wff3) str(wff3)data(wff3) str(wff3)