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] 7.0935336 4.4039143 40.5044980 29.0274590 9.1377819 6.4339718
#> [7] 6.4899960 36.6819057 11.1343181 38.9989761 24.3615505 64.5403207
#> [13] 4.3099945 16.5165742 10.9411791 24.0333023 24.0306816 49.2998205
#> [19] 14.3008510 10.2141598 12.0195865 1.0238765 2.6177075 45.1668918
#> [25] 2.6015625 29.1941503 31.9990633 16.7685713 46.6531901 1.5041211
#> [31] 57.7325246 4.1068136 21.8886248 44.8603654 5.7629804 9.2489302
#> [37] 25.5802405 4.6160282 13.4837574 19.2135286 0.9936953 61.8319883
#> [43] 29.5405596 13.1764267 18.8852922 1.7917467 11.8274659 32.9353245
#> [49] 6.3626943 12.3277829 34.2781842 18.5524801 12.5625179 25.2390591
#> [55] 12.2102632 45.0312606 2.7837870 22.7368049 5.2831424 26.2989893
#> [61] 10.7821278 78.9610135 77.3673026 36.3774393 6.1958325 11.1347350
#> [67] 54.6094923 26.0751410 42.9439789 64.2123914 12.7327565 40.0004614
#> [73] 9.6977960 2.5998939 9.9254578 26.9339709 20.8694171 49.2816793
#> [79] 9.4458911 0.5083072 9.2384728 17.8091907 1.1278270 36.5759389
#> [85] 45.9159310 56.6646701 33.2958219 37.3665444 51.2178431 6.0123428
#> [91] 8.9506918 0.7385095 18.8994098 1.5123316 25.9013571 6.5252095
#> [97] 46.8118975 48.0438795 4.5805404 1.5906783
# 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.9315
# matrix:
cbind(
real = pnbinom(y, mu = 3, size = 2), # real probability
approx = ecumprob(y, sim) # approximation
)
#> real approx
#> [1,] 0.9294561 0.9315
#> [2,] 0.9697669 0.9696
#> [3,] 0.6630400 0.6612
#> [4,] 0.1600000 0.1610
#> [5,] 0.3520000 0.3388
#> [6,] 0.8936243 0.8936
#> [7,] 0.5248000 0.5256
#> [8,] 0.7667200 0.7673
#> [9,] 0.8413696 0.8387
#> [10,] 0.9536426 0.9566
# 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.9315
#> [2,] 0.9697669 0.9696
#> [3,] 0.6630400 0.6612
#> [4,] 0.1600000 0.1610
#> [5,] 0.3520000 0.3388
#> [6,] 0.8936243 0.8936
#> [7,] 0.5248000 0.5256
#> [8,] 0.7667200 0.7673
#> [9,] 0.8413696 0.8387
#> [10,] 0.9536426 0.9566linear_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:
-
Xis a matrix of multipliers/constants; -
bis a vector of (correlated) random variables; -
vcis the symmetric variance-covariance matrix forb;
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