betanalpha / knitr_case_studies

Inference case studies in knitr
162 stars 94 forks source link

Ordinal regression determinant #44

Open ecmerkle opened 2 years ago

ecmerkle commented 2 years ago

I like your ordinal regression case study, and the approach to setting priors on the cutpoints is relevant to some models I am working on. I think that the determinant of that Jacobian matrix does have an analytic determinant, though. I have not written up a full proof, but I am convinced that the determinant is the product of the diagonal times the matrix dimension. In R, it would be something like

prod(diag(J)) * nrow(J)

Here is some code for testing this expression on matrices of the specified structure, with random entries:

nrep <- 100
## dimension of matrix:
nr <- sample(3:20, 100, replace=TRUE)

res <- matrix(NA, nrep, 2)

for(i in 1:nrep){
  ## matrix entries:
  ents <- rnorm((nr[i] - 1), sd=2)

  tmpmat <- matrix(0, nr[i], nr[i])
  tmpmat[,1] <- 1

  for(j in 1:(nr[i] - 1)){
    tmpmat[j:(j+1), (j+1)] <- c(ents[j], -ents[j])
  }

  res[i,1] <- det(tmpmat)
  res[i,2] <- prod(diag(tmpmat)) * nr[i]
}

all.equal(res[,1], res[,2])