linear_algebra_stats

Simple Linear Algebra Functions for Statistics

Description

โ€˜broadcastโ€™ provides some simple Linear Algebra Functions for Statistics:
cinv()
sd_lc()
ecumprob()


Usage

cinv(x)

sd_lc(X, vc, bad_rp = NaN)

ecumprob(y, sim, eps = 0)

Arguments

x a real symmetric positive-definite square matrix.
X a numeric (or logical) matrix of multipliers/constants
vc the variance-covariance matrix for the (correlated) random variables.
bad_rp if vc is not a Positive (semi-) Definite matrix, give here the value to replace bad standard deviations with.

y values to estimate the cumulative probability for.
sim a matrix (or data.frame) with at least 500 columns of simulated values.
If sim is given as a dimensionless vector, it will be treated as a matrix with 1 row and length(sim) columns, and this will be noted with a message.
eps a non-negative numeric scaler smaller than 0.1, giving the cut-off value for probabilities.
Probabilities smaller than eps will be replaced with eps, and probabilities larger than 1 - eps will be replaced with 1 - eps.
Set eps = 0 to disable probability trimming.

Details

cinv()
cinv() computes the Choleski inverse of a real symmetric positive-definite square matrix.


sd_lc()
Given the linear combination X %*% b, where:

  • X is a matrix of multipliers/constants;

  • b is a vector of (correlated) random variables;

  • vc is the symmetric variance-covariance matrix for b;

sd_lc(X, vc) computes the standard deviations for the linear combination X %*% b, without making needless copies.
sd_lc(X, vc) will use much less memory than a base โ€˜Rโ€™ approach.
sd_lc(X, vc) will usually be faster than a base โ€˜Rโ€™ approach (depending on the Linear Algebra Library used for base โ€˜Rโ€™).


ecumprob()
The ecumprod(y, sim) function takes a matrix (or data.frame) of simulated values sim, and for each row i (after broadcasting), estimates the cumulative distribution function of sim[i, ], and returns the cumulative probability for y[i].

In terms of statistics, it is equivalent to the following operation for each index i:
ecdf(sim[i,])(y[i])
However, ecumprob() is much faster, and supports NAs/NaNs.

In terms of linear algebra, it is equivalent to the following broadcasted operation:
rowMeans(sim <= y)
where y and sim are broadcaster arrays.
However, ecumprob() is much more memory-efficient, supports a data.frame for sim, and has statistical safety checks.

Value

For cinv():
A matrix.

For sd_lc():
A vector of standard deviations.

For ecumprob():
A vector of cumulative probabilities.
If for any observation i (after broadcasting,) y[i] is NA/NaN or any of sim[i,] is NA/NaN, the result for i will be NA.
If zero-length y or sim is given, a zero-length numeric vector is returned.

References

John A. Rice (2007), Mathematical Statistics and Data Analysis (6th Edition)

See Also

chol, chol2inv

Examples

library("broadcast")


# variances ====
vc <- datasets::ability.cov$cov
X <- matrix(rnorm(100), 100, ncol(vc))

solve(vc)
#>              general       picture        blocks         maze       reading
#> general  0.082259001 -0.0312406436 -7.750932e-03 -0.013309494 -2.061705e-02
#> picture -0.031240644  0.2369906996 -2.484938e-02  0.017844845  8.603286e-04
#> blocks  -0.007750932 -0.0248493822  1.344272e-02 -0.012544830 -3.802671e-05
#> maze    -0.013309494  0.0178448450 -1.254483e-02  0.101625400  5.508423e-03
#> reading -0.020617049  0.0008603286 -3.802671e-05  0.005508423  5.713620e-02
#> vocab   -0.002420800  0.0019394999 -1.157864e-03 -0.002857265 -2.406969e-02
#>                vocab
#> general -0.002420800
#> picture  0.001939500
#> blocks  -0.001157864
#> maze    -0.002857265
#> reading -0.024069692
#> vocab    0.020323179
cinv(vc) # faster than `solve()`, but only works on positive definite matrices
#>              [,1]          [,2]          [,3]         [,4]          [,5]
#> [1,]  0.082259001 -0.0312406436 -7.750932e-03 -0.013309494 -2.061705e-02
#> [2,] -0.031240644  0.2369906996 -2.484938e-02  0.017844845  8.603286e-04
#> [3,] -0.007750932 -0.0248493822  1.344272e-02 -0.012544830 -3.802671e-05
#> [4,] -0.013309494  0.0178448450 -1.254483e-02  0.101625400  5.508423e-03
#> [5,] -0.020617049  0.0008603286 -3.802671e-05  0.005508423  5.713620e-02
#> [6,] -0.002420800  0.0019394999 -1.157864e-03 -0.002857265 -2.406969e-02
#>              [,6]
#> [1,] -0.002420800
#> [2,]  0.001939500
#> [3,] -0.001157864
#> [4,] -0.002857265
#> [5,] -0.024069692
#> [6,]  0.020323179
all(round(solve(vc), 6) == round(cinv(vc), 6)) # they're the same
#> [1] TRUE

sd_lc(X, vc)
#>   [1]  6.47644968 27.01523143 37.24796381 46.18285062 36.88207316 27.02816144
#>   [7] 27.73650753 15.89799338 17.70232076 13.05870480  7.89087885 26.47510987
#>  [13]  9.95024654 18.54953881 41.69225257  4.14115283 43.10782859 23.88001499
#>  [19] 11.47939621 26.66207497  3.66576572 32.85329948 28.68185582 26.47476696
#>  [25] 51.81315938 39.97034800 30.10706800 23.67536000 51.38926358 38.29608016
#>  [31] 35.83780684 12.57974050 29.26976739 55.81693098 21.57949218 60.01666340
#>  [37] 41.18645451 12.67664238 17.98448328  0.07459999 61.40507391 42.60169737
#>  [43] 13.41051242 40.15603081 12.02438297 22.09461690 42.02468738 14.08883427
#>  [49] 67.82456034 23.79598610 43.70368884 46.71423828 20.93597274 19.42560444
#>  [55] 22.47876453 18.53551969  2.06598346  2.33344192 47.01280265 14.35382800
#>  [61] 35.22450390 51.24869890 27.89896216 23.32134076 13.94619803 18.16521350
#>  [67]  4.37420041 18.11324287  9.15244025  7.32137424 48.01466081  7.16386427
#>  [73] 25.55558280  6.37174088 16.29919855  7.66069838  5.01526721 20.15959195
#>  [79] 33.65134584  2.03332716 28.30724675 14.07543998 26.03504278 13.29082651
#>  [85] 13.77241588 34.63465375 10.92353379 91.87357787 27.58266620 76.75230977
#>  [91] 16.76955745  2.60695989  7.07536491 46.32894759  9.89729011 34.56674548
#>  [97] 26.28725550 32.62500557 17.21442295 12.31586950



# ecumprob() ====

sim <- rnbinom(10 * 1e4, mu = 3, size = 2) |> matrix(10, 1e4)
y <- sample(0:9)

# vector:
pnbinom(y[1], mu = 3, size = 2) # real probability
#> [1] 0.9294561
ecumprob(y[1], sim[1, , drop = TRUE]) # approximation
#> [1] 0.9285

# matrix:
cbind(
  real = pnbinom(y, mu = 3, size = 2), # real probability
  approx = ecumprob(y, sim) # approximation
)
#>            real approx
#>  [1,] 0.9294561 0.9285
#>  [2,] 0.9697669 0.9732
#>  [3,] 0.9536426 0.9514
#>  [4,] 0.7667200 0.7709
#>  [5,] 0.3520000 0.3454
#>  [6,] 0.8413696 0.8444
#>  [7,] 0.1600000 0.1610
#>  [8,] 0.6630400 0.6641
#>  [9,] 0.8936243 0.8904
#> [10,] 0.5248000 0.5262

# data.frame:
cbind(
  real = pnbinom(y, mu = 3, size = 2), # real probability
  approx = ecumprob(y, as.data.frame(sim)) # approximation
)
#>            real approx
#>  [1,] 0.9294561 0.9285
#>  [2,] 0.9697669 0.9732
#>  [3,] 0.9536426 0.9514
#>  [4,] 0.7667200 0.7709
#>  [5,] 0.3520000 0.3454
#>  [6,] 0.8413696 0.8444
#>  [7,] 0.1600000 0.1610
#>  [8,] 0.6630400 0.6641
#>  [9,] 0.8936243 0.8904
#> [10,] 0.5248000 0.5262