Closed goldingn closed 1 month ago
Note: this is the same for the lkj_correlation()
, e.g. try subbing in x <- lkj_correlation(eta = 3, dimension = 3)
in place of the wishart in the reprex
Perhaps adding a test like this will help
library(greta)
#>
#> Attaching package: 'greta'
#> The following objects are masked from 'package:stats':
#>
#> binomial, cov2cor, poisson
#> The following objects are masked from 'package:base':
#>
#> %*%, apply, backsolve, beta, chol2inv, colMeans, colSums, diag,
#> eigen, forwardsolve, gamma, identity, rowMeans, rowSums, sweep,
#> tapply
library(testthat)
x <- wishart(df = 4, Sigma = diag(3))
#> ℹ Initialising python and checking dependencies, this may take a moment.
#> ✔ Initialising python and checking dependencies ... done!
#>
chol_x <- chol(x)
calc_chol <- calculate(x, chol_x, nsim = 1)
calc_chol
#> $x
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 8.300433 -0.4479606 0.57702
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] -0.4479606 0.7182815 -0.01634725
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 0.57702 -0.01634725 1.586213
#>
#>
#> $chol_x
#> , , 1
#>
#> [,1] [,2] [,3]
#> [1,] 1 1 1
#>
#> , , 2
#>
#> [,1] [,2] [,3]
#> [1,] 1 1 1
#>
#> , , 3
#>
#> [,1] [,2] [,3]
#> [1,] 1 1 1
expect_equal(dim(calc_chol$chol_x), c(1,3,3))
calc_chol_mat <- matrix(calc_chol$chol_x, nrow = 3, ncol = 3)
calc_chol_mat
#> [,1] [,2] [,3]
#> [1,] 1 1 1
#> [2,] 1 1 1
#> [3,] 1 1 1
lower_tri <- calc_chol_mat[lower.tri(calc_chol_mat)]
lower_tri
#> [1] 1 1 1
expect_equal(lower_tri, c(0,0,0))
#> Error: `lower_tri` not equal to c(0, 0, 0).
#> 3/3 mismatches (average diff: 1)
#> [1] 1 - 0 == 1
#> [2] 1 - 0 == 1
#> [3] 1 - 0 == 1
Created on 2023-04-19 with reprex v2.0.2
So then we'd end up with tests like this
test_that("Cholesky factor of Wishart should be an upper triangular matrix", {
x <- wishart(df = 4, Sigma = diag(3))
chol_x <- chol(x)
calc_chol <- calculate(x, chol_x, nsim = 1)
expect_equal(dim(calc_chol$chol_x), c(1,3,3))
calc_chol_mat <- matrix(calc_chol$chol_x, nrow = 3, ncol = 3)
expect_equal(calc_chol_mat[lower.tri(calc_chol_mat)], c(0,0,0))
})
test_that("Cholesky factor of LJK_correlation should be an upper triangular matrix", {
x <- lkj_correlation(eta = 3, dimension = 3)
chol_x <- chol(x)
calc_chol <- calculate(x, chol_x, nsim = 1)
expect_equal(dim(calc_chol$chol_x), c(1,3,3))
calc_chol_mat <- matrix(calc_chol$chol_x, nrow = 3, ncol = 3)
expect_equal(calc_chol_mat[lower.tri(calc_chol_mat)], c(0,0,0))
})
Notes from discussion on this, it seems that is an issue with the representation attribute not being properly accessed during sampling.
So, trying to unpack where this is happening - is it happening at the operation node, and why are the cholesky identifying parts not being appropriately used.
For clarity,
now the matrix which should be symmetric looks like a cholesky factor (but lower triangular, when it should be upper triangular), and cholesky factor is still coming out as ones
A cholesky factor should be lower triangular (from https://en.wikipedia.org/wiki/Cholesky_decomposition)
This is now resolved, issue with upper triangular is in #692
1) When running
calculate()
on cholesky factors of a Wishart-distributed matrix, the cholesky factor is wrong - a matrix of ones instead of the correct value 2) Whenmcmc()
is run, it gets even weirder. The value given for the original matrix is a cholesky factor, but it's transposed.I'm not sure what's going on for 1), but it could be something to do with the 'representations' code that reuses the cholesky factor.
For 2), could it be that when MCMC happens, the tensor for the original matrix is being overwritten with the cholesky factor in the same environment, and that is somehow being picked up in calculate?
This seems to be breaking my sampler for a model that uses cholesky factorisation, so I'm guessing something is being overwritten. I can't test on TF1 right now.
Note: I can't set the seed to demonstrate the reprex more clearly because of #559, and this is possibly related to or explains #560
Created on 2023-03-31 with reprex v2.0.2