Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.2.4 release candidate #136

Merged
merged 56 commits into from
Feb 19, 2018
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
ef9b26d
add chains argument
goldingn Sep 9, 2017
d57dfbb
style updates
goldingn Sep 13, 2017
9e68130
handle fix number of leapfrog steps
goldingn Sep 13, 2017
c5906dd
add diagonal emtric estimation to HMC
goldingn Sep 13, 2017
6ddc933
Merge branch 'evaluate' into dev
goldingn Sep 13, 2017
e545b34
update readme with chains arg & new bayesplot
goldingn Sep 13, 2017
bf2abcd
Merge branch 'master' into dev
goldingn Sep 13, 2017
ddd3a54
tweak adaptation scheme
goldingn Sep 13, 2017
b3d140f
make models define tf objects on their own graph
goldingn Sep 15, 2017
8fa916a
remove flushing of default graph in tests
goldingn Sep 15, 2017
7f004ae
run optimisation on tf graph
goldingn Sep 15, 2017
ee0d668
add evaluate function()
goldingn Sep 18, 2017
bf1ba57
enable evaluation of greta arrays from MCMC samples
goldingn Sep 18, 2017
11c7cf6
bugfix evaluate
goldingn Sep 18, 2017
c13bd5d
get n_cores working
goldingn Sep 19, 2017
283e9c8
handle multiple initial values
goldingn Sep 19, 2017
77577e1
lapply for chains, ready for parallelisation
goldingn Sep 19, 2017
c55f3d9
bugfix evaluate_mcmc.list for scalars
goldingn Sep 19, 2017
ae53970
bugfix run_chain()
goldingn Sep 24, 2017
3213da2
make initial values more conservative
goldingn Sep 24, 2017
7e7b702
Update helpers.R
twolodzko Sep 25, 2017
419e103
Merge pull request #106 from twolodzko/master
goldingn Sep 26, 2017
f42b4d0
Merge branch 'master' into dev
goldingn Feb 2, 2018
a0d7c23
Merge branch 'master' into upstream_dev
goldingn Feb 2, 2018
b39519a
little typo: wirds -> words
benmarwick Feb 3, 2018
40ddac3
Merge pull request #126 from goldingn/upstream_dev
goldingn Feb 5, 2018
64db0f8
Merge pull request #125 from benmarwick/patch-1
goldingn Feb 5, 2018
5f9f2ae
fix #119; diagonal variance adaptation bug
goldingn Feb 16, 2018
e2cc286
rename evaluate() to calculate() (fixes #129)
goldingn Feb 16, 2018
f48f106
add tests for calculate()
goldingn Feb 16, 2018
d143e77
don't export raw() or use it on calculate()
goldingn Feb 18, 2018
ac01b85
add final calculate() test
goldingn Feb 18, 2018
8e78c9c
remove raw() altogether
goldingn Feb 18, 2018
d5ea5f6
(quietly) check for mcmc wirks with multiple chains
goldingn Feb 18, 2018
ecf00b0
test new multi-chain initial values setup
goldingn Feb 18, 2018
b954b6f
check metropolis rejects without error
goldingn Feb 18, 2018
875b56a
Merge pull request #132 from goldingn/dev
goldingn Feb 18, 2018
42f3a77
replace unary minus
goldingn Feb 18, 2018
8727c9d
Merge remote-tracking branch 'upstream/dev' into dev
goldingn Feb 19, 2018
d5d7043
don't flush the default graph in model(); do when testing
goldingn Feb 19, 2018
f644ad4
issue better error message in model(); fixes #133
goldingn Feb 19, 2018
46b9e8e
bugfix dimension-checking matrix multiplication
goldingn Feb 19, 2018
4282596
add imultilogit function (fixes #124)
goldingn Feb 19, 2018
37c55f2
switch '=' to '<-' in vignettes
goldingn Feb 19, 2018
21c4e34
redocument
goldingn Feb 19, 2018
29f86d4
switch '=' to '<-' in documentation examples
goldingn Feb 19, 2018
78848f2
switch '=' to '<-' in website demo
goldingn Feb 19, 2018
28bce5e
rebuild website
goldingn Feb 19, 2018
aca5d1b
add hex sticker design
goldingn Feb 19, 2018
a3f72aa
add extra test for %*%
goldingn Feb 19, 2018
600f0e7
rename test script for consistency
goldingn Feb 19, 2018
f802639
fix and test imultilogit error message
goldingn Feb 19, 2018
fe45183
version and date bump
goldingn Feb 19, 2018
6304dd1
add NEWS for v0.2.3 (oops)
goldingn Feb 19, 2018
e1742ef
add NEWS for v0.2.4
goldingn Feb 19, 2018
e9f676c
Merge pull request #135 from goldingn/dev
goldingn Feb 19, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Package: greta
Type: Package
Title: Simple and Scalable Statistical Modelling in R
Version: 0.2.3
Date: 2018-01-23
Version: 0.2.4
Date: 2018-02-20
Authors@R: person("Nick", "Golding", role = c("aut", "cre"),
email = "[email protected]")
Description: Write statistical models in R and fit them by MCMC on CPUs and GPUs, using Google TensorFlow (see <https://goldingn.github.io/greta> for more information).
Description: Write statistical models in R and fit them by MCMC on CPUs and GPUs, using Google TensorFlow (see <https://greta-dev.github.io/greta> for more information).
License: Apache License 2.0
URL: https://github.com/greta-dev/greta
BugReports: https://github.com/greta-dev/greta/issues
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export(hypergeometric)
export(icauchit)
export(icloglog)
export(ilogit)
export(imultilogit)
export(install_tensorflow)
export(inverse_gamma)
export(iprobit)
Expand Down
30 changes: 30 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
### greta 0.2.4
================

Fixes:

* improved error checking/messages in `model()`, `%*%`
* switched docs and examples to always use `<-` for assignment
* fixed the `n_cores` argument to `model()`

New functionality:

* added a `calculate()` function to compute the values of greta arrays conditional on provided values for others
* added `imultilogit()` transform
* added a `chains` argument to `model()`
* improved HMC self-tuning, including a diagonal euclidean metric

### greta 0.2.3
================

Fixes:

* fixed breaking change in extraDistr API (caused test errors on CRAN builds)
* added dontrun statements to pass CRAN checks on winbuilder
* fixed breaking change in tensorflow API (1-based indexing)

New functionality:

* added `cumsum()` and `cumprod()` functions


### greta 0.2.2
================

Expand Down
12 changes: 6 additions & 6 deletions R/calculate.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@
#'
#' # define a variable greta array, and another that is calculated from it
#' # then calculate what value y would take for different values of x
#' x = normal(0, 1, dim = 3)
#' a = lognormal(0, 1)
#' x <- normal(0, 1, dim = 3)
#' a <- lognormal(0, 1)
#' y <- sum(x ^ 2) + a
#' calculate(y, list(x = c(0.1, 0.2, 0.3), a = 2))
#'
#'
#' # define a model
#' alpha = normal(0, 1)
#' beta = normal(0, 1)
#' sigma = lognormal(1, 0.1)
#' alpha <- normal(0, 1)
#' beta <- normal(0, 1)
#' sigma <- lognormal(1, 0.1)
#' mu <- alpha + iris$Petal.Length * beta
#' distribution(iris$Petal.Width) = normal(mu, sigma)
#' distribution(iris$Petal.Width) <- normal(mu, sigma)
#' m <- model(alpha, beta, sigma)
#'
#' # calculate intermediate greta arrays, given some parameter values
Expand Down
6 changes: 3 additions & 3 deletions R/distribution.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
#'
#' # observed data and mean parameter to be estimated
#' # (explicitly coerce data to a greta array so we can refer to it later)
#' y = as_data(rnorm(5, 0, 3))
#' y <- as_data(rnorm(5, 0, 3))
#'
#' mu = uniform(-3, 3)
#' mu <- uniform(-3, 3)
#'
#' # define the distribution over y (the model likelihood)
#' distribution(y) = normal(mu, 1)
#' distribution(y) <- normal(mu, 1)
#'
#' # get the distribution over y
#' distribution(y)
Expand Down
2 changes: 1 addition & 1 deletion R/extract_replace_combine.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#' @examples
#' \dontrun{
#'
#' x = as_data(matrix(1:12, 3, 4))
#' x <- as_data(matrix(1:12, 3, 4))
#'
#' # extract/replace
#' x[1:3, ]
Expand Down
16 changes: 8 additions & 8 deletions R/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@
#' @examples
#' \dontrun{
#'
#' x = as_data(matrix(1:9, nrow = 3, ncol = 3))
#' a = log(exp(x))
#' b = log1p(expm1(x))
#' c = sign(x - 5)
#' d = abs(x - 5)
#' x <- as_data(matrix(1:9, nrow = 3, ncol = 3))
#' a <- log(exp(x))
#' b <- log1p(expm1(x))
#' c <- sign(x - 5)
#' d <- abs(x - 5)
#'
#' e = diag(x)
#' e <- diag(x)
#' diag(x) <- e + 1
#'
#' z = t(a)
#' z <- t(a)
#'
#' y = sweep(x, 1, e, '-')
#' y <- sweep(x, 1, e, '-')
#' }
NULL

Expand Down
32 changes: 25 additions & 7 deletions R/greta_model_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ NULL
#' \dontrun{
#'
#' # define a simple model
#' mu = variable()
#' sigma = lognormal(1, 0.1)
#' x = rnorm(10)
#' distribution(x) = normal(mu, sigma)
#' mu <- variable()
#' sigma <- lognormal(1, 0.1)
#' x <- rnorm(10)
#' distribution(x) <- normal(mu, sigma)
#'
#' m <- model(mu, sigma)
#'
Expand Down Expand Up @@ -81,9 +81,6 @@ model <- function (...,
}
}

# flush all tensors from the default graph
tf$reset_default_graph()

# nodes required
target_greta_arrays <- list(...)

Expand All @@ -102,6 +99,27 @@ model <- function (...,

}

# check they are greta arrays
are_greta_arrays <- vapply(target_greta_arrays,
inherits, "greta_array",
FUN.VALUE = FALSE)

if (!all(are_greta_arrays)) {

unexpected_items <- names(target_greta_arrays)[!are_greta_arrays]

msg <- ifelse(length(unexpected_items) > 1,
"The following objects passed to model() are not greta arrays: ",
"The following object passed to model() is not a greta array: ")

stop (msg,
paste(unexpected_items, sep = ", "),
call. = FALSE)

}



if (length(target_greta_arrays) == 0) {
stop ('could not find any non-data greta arrays',
call. = FALSE)
Expand Down
8 changes: 4 additions & 4 deletions R/inference.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ stash_trace <- function (trace, raw) {
#' @examples
#' \dontrun{
#' # define a simple model
#' mu = variable()
#' sigma = lognormal(1, 0.1)
#' x = rnorm(10)
#' distribution(x) = normal(mu, sigma)
#' mu <- variable()
#' sigma <- lognormal(1, 0.1)
#' x <- rnorm(10)
#' distribution(x) <- normal(mu, sigma)
#' m <- model(mu, sigma)
#'
#' # carry out mcmc on the model
Expand Down
33 changes: 17 additions & 16 deletions R/operators.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@
#' @examples
#' \dontrun{
#'
#' x = as_data(-1:12)
#' x <- as_data(-1:12)
#'
#' # arithmetic
#' a = x + 1
#' b = 2 * x + 3
#' c = x %% 2
#' d = x %/% 5
#' a <- x + 1
#' b <- 2 * x + 3
#' c <- x %% 2
#' d <- x %/% 5
#'
#' # logical
#' e = (x > 1) | (x < 1)
#' f = e & (x < 2)
#' g = !f
#' e <- (x > 1) | (x < 1)
#' f <- e & (x < 2)
#' g <- !f
#'
#' # relational
#' h = x < 1
#' i = (-x) >= x
#' j = h == x
#' h <- x < 1
#' i <- (-x) >= x
#' j <- h == x
#' }
NULL

Expand Down Expand Up @@ -145,15 +145,16 @@ NULL
y <- elem_list[[2]]

# check they're matrices
if (length(dim(x)) != 2 | length(dim(x)) != 2)
stop ('only two-dimensional greta arrays can be matrix-multiplied',
if (length(dim(x)) != 2 | length(dim(y)) != 2) {
stop ("only two-dimensional greta arrays can be matrix-multiplied",
call. = FALSE)
}

# check the dimensions match
if (dim(x)[2] != dim(y)[1]) {
msg <- sprintf('incompatible dimensions: %s vs %s',
paste0(dim(x), collapse = 'x'),
paste0(dim(y), collapse = 'x'))
msg <- sprintf("incompatible dimensions: %s vs %s",
paste0(dim(x), collapse = "x"),
paste0(dim(y), collapse = "x"))
stop (msg)
}

Expand Down
8 changes: 4 additions & 4 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
#' # a simple Bayesian regression model for the iris data
#'
#' # priors
#' int = normal(0, 5)
#' coef = normal(0, 3)
#' sd = lognormal(0, 3)
#' int <- normal(0, 5)
#' coef <- normal(0, 3)
#' sd <- lognormal(0, 3)
#'
#' # likelihood
#' mean <- int + coef * iris$Petal.Length
#' distribution(iris$Sepal.Length) = normal(mean, sd)
#' distribution(iris$Sepal.Length) <- normal(mean, sd)
#'
#' # build and sample
#' m <- model(int, coef, sd)
Expand Down
26 changes: 13 additions & 13 deletions R/probability_distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ cauchy_distribution <- R6Class (
s <- parameters$scale

log_prob = function (x)
-tf$log(fl(pi) * s * (fl(1) + tf$square((x - loc) / s)))
tf$negative(tf$log(fl(pi) * s * (fl(1) + tf$square((x - loc) / s))))

cdf = function (x)
fl(1 / pi) * tf$atan((x - loc) / s) + fl(0.5)
Expand Down Expand Up @@ -1359,39 +1359,39 @@ distribution_classes_module <- module(uniform_distribution,
#' \dontrun{
#'
#' # a uniform parameter constrained to be between 0 and 1
#' phi = uniform(min = 0, max = 1)
#' phi <- uniform(min = 0, max = 1)
#'
#' # a length-three variable, with each element following a standard normal
#' # distribution
#' alpha = normal(0, 1, dim = 3)
#' alpha <- normal(0, 1, dim = 3)
#'
#' # a length-three variable of lognormals
#' sigma = lognormal(0, 3, dim = 3)
#' sigma <- lognormal(0, 3, dim = 3)
#'
#' # a hierarchical uniform, constrained between alpha and alpha + sigma,
#' eta = alpha + uniform(0, 1, dim = 3) * sigma
#' eta <- alpha + uniform(0, 1, dim = 3) * sigma
#'
#' # a hierarchical distribution
#' mu = normal(0, 1)
#' sigma = lognormal(0, 1)
#' theta = normal(mu, sigma)
#' mu <- normal(0, 1)
#' sigma <- lognormal(0, 1)
#' theta <- normal(mu, sigma)
#'
#' # a vector of 3 variables drawn from the same hierarchical distribution
#' thetas = normal(mu, sigma, dim = 3)
#' thetas <- normal(mu, sigma, dim = 3)
#'
#' # a matrix of 12 variables drawn from the same hierarchical distribution
#' thetas = normal(mu, sigma, dim = c(3, 4))
#' thetas <- normal(mu, sigma, dim = c(3, 4))
#'
#' # a multivariate normal variable, with correlation between two elements
#' Sig <- diag(4)
#' Sig[3, 4] <- Sig[4, 3] <- 0.6
#' theta = multivariate_normal(rep(mu, 4), Sig)
#' theta <- multivariate_normal(rep(mu, 4), Sig)
#'
#' # 10 independent replicates of that
#' theta = multivariate_normal(rep(mu, 4), Sig, dim = 10)
#' theta <- multivariate_normal(rep(mu, 4), Sig, dim = 10)
#'
#' # a Wishart variable with the same covariance parameter
#' theta = wishart(df = 5, Sigma = Sig)
#' theta <- wishart(df = 5, Sigma = Sig)
#'
#' }
NULL
Expand Down
6 changes: 6 additions & 0 deletions R/tf_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ tf_icloglog <- function (x)
tf_icauchit <- function (x)
fl(1 / pi) * tf$atan(x) + fl(0.5)

tf_imultilogit <- function (x) {
zeros <- tf$zeros(shape(nrow(x), 1L), tf_float())
latent <- tf$concat(list(x, zeros), 1L)
tf$nn$softmax(latent)
}

# map R's extract and replace syntax to tensorflow, for use in operation nodes
# the following arguments are required:
Expand Down Expand Up @@ -328,6 +333,7 @@ tf_functions_module <- module(tf_as_logical,
tf_iprobit,
tf_icloglog,
tf_icauchit,
tf_imultilogit,
tf_extract,
tf_recombine,
tf_replace,
Expand Down
Loading