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] 5.3921800 15.6702931 27.6960442 10.4651956 27.1582770 33.7684852
#> [7] 23.9529111 21.9723394 11.3131670 28.6407643 25.4783191 0.9672553
#> [13] 16.6832653 20.9368116 82.4821266 31.0887316 84.3227203 5.3152370
#> [19] 50.0480281 12.5882546 48.3673789 36.3124383 49.0699047 17.2265113
#> [25] 20.2120365 53.6408764 18.1864230 7.8687021 1.5369309 38.0783909
#> [31] 9.7026216 21.6591995 10.0500709 8.7327049 23.1548512 32.5096781
#> [37] 12.5688127 9.8766428 9.9159698 13.4057327 28.3326919 27.4003936
#> [43] 48.7378688 38.6565134 40.2791401 22.5204636 58.4410926 19.3396232
#> [49] 76.1689259 19.2861775 3.5752671 86.8543025 44.9522830 21.5311932
#> [55] 54.7970624 27.4037072 14.8818960 27.2147262 7.3462714 25.0039737
#> [61] 22.0322972 46.7188939 39.5586150 3.2787437 55.0187195 26.7691619
#> [67] 30.7718076 10.0012829 12.7375663 14.7551748 6.2051295 2.7656205
#> [73] 75.8966345 76.9424992 34.0668905 9.7020514 10.6674285 7.5192860
#> [79] 50.7647089 49.4064228 64.9060609 18.6723133 65.3023505 44.9804040
#> [85] 11.4396512 7.7051407 61.2682816 7.5359841 41.5311651 71.9517784
#> [91] 39.1355669 6.5688233 18.8662780 14.8221591 65.2642377 20.6653162
#> [97] 39.6233359 12.8140271 2.1216310 43.5935944
# 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.9357
# matrix:
cbind(
real = pnbinom(y, mu = 3, size = 2), # real probability
approx = ecumprob(y, sim) # approximation
)
#> real approx
#> [1,] 0.9294561 0.9357
#> [2,] 0.8413696 0.8437
#> [3,] 0.7667200 0.7642
#> [4,] 0.6630400 0.6612
#> [5,] 0.3520000 0.3564
#> [6,] 0.9536426 0.9532
#> [7,] 0.8936243 0.8952
#> [8,] 0.1600000 0.1621
#> [9,] 0.5248000 0.5226
#> [10,] 0.9697669 0.9672
# 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.9357
#> [2,] 0.8413696 0.8437
#> [3,] 0.7667200 0.7642
#> [4,] 0.6630400 0.6612
#> [5,] 0.3520000 0.3564
#> [6,] 0.9536426 0.9532
#> [7,] 0.8936243 0.8952
#> [8,] 0.1600000 0.1621
#> [9,] 0.5248000 0.5226
#> [10,] 0.9697669 0.9672linear_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