evaluate.statement: Evaluate sampling statement from Stan program

Description Usage Arguments Details Value Examples

Description

Evaluate sampling statement from Stan program

Usage

1
evaluate.statement(statement, ikde.model, eval.point)

Arguments

statement

A string containing a sampling statement

ikde.model

An object of class ikde.model, which has been built

eval.point

A list of parameter names and the point to evaluate the statement

Details

Parses the given sampling statement and evaluates it at the specified evaluation point. The ikde.model object and eval.point object are needed to resolve variable values in the statement.

Value

A real number indicating value of the log-density of the statement at the evaluation point

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
data(lm.generated)

X <- lm.generated$X
y <- lm.generated$y

data <- list(N = list(type = "int<lower=1>", dim = 1, value = nrow(X)),
             k = list(type = "int<lower=1>", dim = 1, value = ncol(X)),
             X = list(type = "matrix", dim = "[N, k]", value = X),
             y = list(type = "vector", dim = "[N]", value = y))
parameters <- list(beta = list(type = "vector", dim = "[k]"),
                   sigma_sq = list(type = "real<lower=0>", dim = 1))
model <- list(priors = c("beta ~ normal(0, 10);",
                         "sigma_sq ~ inv_gamma(1, 1);"),
              likelihood = c("y ~ normal(X * beta, sqrt(sigma_sq));"))

ikde.model <- define.model(data, parameters, model)

statement <- ikde.model$model$likelihood[1]
eval.point <- list(beta = c(1, 2, 3, 4), sigma_sq = 5)

# These results match:
evaluate.statement(statement, ikde.model, eval.point)
sum(dnorm(y, mean = X %*% eval.point$beta, sd = sqrt(eval.point$sigma_sq), log = TRUE))
# [1] -4178.641
  

tkmckenzie/ikde documentation built on May 13, 2019, 9:53 p.m.