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

mixture() doesn't work with single-column arrays #528

Open
hrlai opened this issue May 31, 2022 · 1 comment
Open

mixture() doesn't work with single-column arrays #528

hrlai opened this issue May 31, 2022 · 1 comment
Milestone

Comments

@hrlai
Copy link

hrlai commented May 31, 2022

Hi there, sorry for submitting so many issues lately, but I'm really trying to squeeze a lot from greta now!

I'm playing around with mixture(). One of the examples in ?mixture is

# weights can also be an array, giving different mixing weights
# for each observation (first dimension must be number of components)
dim <- c(5, 4)
weights <- uniform(0, 1, dim = c(2, dim))
b <- mixture(normal(1, 1, dim = dim),
  normal(-1, 1, dim = dim),
  weights = weights
)

I'm trying to mix two distributions that are single-column arrays, but in an attempt to try dim <- c(5, 1) it throws an error:

> dim <- c(5, 1)
> weights <- uniform(0, 1, dim = c(2, dim))
> b <- mixture(normal(1, 1, dim = dim),
+              normal(-1, 1, dim = dim),
+              weights = weights
+ )
Error: the dimension of weights must be either "\033[34m2\033[39mx1" or
"\033[34m2\033[39mx\033[34m\"5x1\"\033[39m"
but was "2x5x1"

Curiously, dim <- c(1, 5) works... did I miss something?

@njtierney
Copy link
Collaborator

Hi @hrlai !

Thanks so much for posting these issues, no need to apologise, you're helping make the software better :) so do keep posting. I'm not sure what is going on with the message getting garbled there, I've posted about that here: r-lib/cli#474

I confirm that I get the same error:

#works
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
dim <- c(5, 4)
weights <- uniform(0, 1, dim = c(2, dim))
#> ℹ Initialising python and checking dependencies, this may take a moment.
#> ✔ Initialising python and checking dependencies ... done!
#> 
b <- mixture(normal(1, 1, dim = dim),
             normal(-1, 1, dim = dim),
             weights = weights)

b
#> greta array (variable following a mixture distribution)
#> 
#>      [,1] [,2] [,3] [,4]
#> [1,]  ?    ?    ?    ?  
#> [2,]  ?    ?    ?    ?  
#> [3,]  ?    ?    ?    ?  
#> [4,]  ?    ?    ?    ?  
#> [5,]  ?    ?    ?    ?

# fails
dim <- c(5, 1)
weights <- uniform(0, 1, dim = c(2, dim))
b <- mixture(normal(1, 1, dim = dim),
             normal(-1, 1, dim = dim),
             weights = weights)
#> Error: the dimension of weights must be either "2x1" or "2x\"5x1\""
#> but was "2x5x1"

# but this works
dim <- c(1,5)
weights <- uniform(0, 1, dim = c(2, dim))
b <- mixture(normal(1, 1, dim = dim),
             normal(-1, 1, dim = dim),
             weights = weights)

Created on 2022-06-01 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.0 (2022-04-22)
#>  os       macOS Big Sur/Monterey 10.16
#>  system   x86_64, darwin17.0
#>  ui       X11
#>  language (EN)
#>  collate  en_AU.UTF-8
#>  ctype    en_AU.UTF-8
#>  tz       Australia/Perth
#>  date     2022-06-01
#>  pandoc   2.17.1.1 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  base64enc     0.1-3   2015-07-28 [1] CRAN (R 4.2.0)
#>  callr         3.7.0   2021-04-20 [1] CRAN (R 4.2.0)
#>  cli           3.3.0   2022-04-25 [1] CRAN (R 4.2.0)
#>  coda          0.19-4  2020-09-30 [1] CRAN (R 4.2.0)
#>  codetools     0.2-18  2020-11-04 [1] CRAN (R 4.2.0)
#>  crayon        1.5.1   2022-03-26 [1] CRAN (R 4.2.0)
#>  digest        0.6.29  2021-12-01 [1] CRAN (R 4.2.0)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.2.0)
#>  evaluate      0.15    2022-02-18 [1] CRAN (R 4.2.0)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.2.0)
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.2.0)
#>  future        1.26.1  2022-05-27 [1] CRAN (R 4.2.0)
#>  globals       0.15.0  2022-05-09 [1] CRAN (R 4.2.0)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
#>  greta       * 0.4.2   2022-03-22 [1] CRAN (R 4.2.0)
#>  here          1.0.1   2020-12-13 [1] CRAN (R 4.2.0)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.2.0)
#>  hms           1.1.1   2021-09-26 [1] CRAN (R 4.2.0)
#>  htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.2.0)
#>  jsonlite      1.8.0   2022-02-22 [1] CRAN (R 4.2.0)
#>  knitr         1.39    2022-04-26 [1] CRAN (R 4.2.0)
#>  lattice       0.20-45 2021-09-22 [1] CRAN (R 4.2.0)
#>  lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.2.0)
#>  listenv       0.8.0   2019-12-05 [1] CRAN (R 4.2.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
#>  Matrix        1.4-1   2022-03-23 [1] CRAN (R 4.2.0)
#>  parallelly    1.31.1  2022-04-22 [1] CRAN (R 4.2.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.0)
#>  png           0.1-7   2013-12-03 [1] CRAN (R 4.2.0)
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.2.0)
#>  processx      3.5.3   2022-03-25 [1] CRAN (R 4.2.0)
#>  progress      1.2.2   2019-05-16 [1] CRAN (R 4.2.0)
#>  ps            1.7.0   2022-04-23 [1] CRAN (R 4.2.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.2.0)
#>  Rcpp          1.0.8.3 2022-03-17 [1] CRAN (R 4.2.0)
#>  reprex        2.0.1   2021-08-05 [1] CRAN (R 4.2.0)
#>  reticulate    1.25    2022-05-11 [1] CRAN (R 4.2.0)
#>  rlang         1.0.2   2022-03-04 [1] CRAN (R 4.2.0)
#>  rmarkdown     2.14    2022-04-25 [1] CRAN (R 4.2.0)
#>  rprojroot     2.0.3   2022-04-02 [1] CRAN (R 4.2.0)
#>  rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.2.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
#>  stringi       1.7.6   2021-11-29 [1] CRAN (R 4.2.0)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.2.0)
#>  tensorflow    2.9.0   2022-05-21 [1] CRAN (R 4.2.0)
#>  tfruns        1.5.0   2021-02-26 [1] CRAN (R 4.2.0)
#>  vctrs         0.4.1   2022-04-13 [1] CRAN (R 4.2.0)
#>  whisker       0.4     2019-08-28 [1] CRAN (R 4.2.0)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.0)
#>  xfun          0.31    2022-05-10 [1] CRAN (R 4.2.0)
#>  yaml          2.3.5   2022-02-21 [1] CRAN (R 4.2.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.2/Resources/library
#> 
#> ─ Python configuration ───────────────────────────────────────────────────────
#>  python:         /Users/njtierney/Library/r-miniconda/envs/greta-env/bin/python
#>  libpython:      /Users/njtierney/Library/r-miniconda/envs/greta-env/lib/libpython3.7m.dylib
#>  pythonhome:     /Users/njtierney/Library/r-miniconda/envs/greta-env:/Users/njtierney/Library/r-miniconda/envs/greta-env
#>  version:        3.7.12 | packaged by conda-forge | (default, Oct 26 2021, 05:59:23)  [Clang 11.1.0 ]
#>  numpy:          /Users/njtierney/Library/r-miniconda/envs/greta-env/lib/python3.7/site-packages/numpy
#>  numpy_version:  1.16.4
#>  tensorflow:     /Users/njtierney/Library/r-miniconda/envs/greta-env/lib/python3.7/site-packages/tensorflow
#>  
#>  NOTE: Python version was forced by use_python function
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Which is erroring here:

greta/R/mixture.R

Lines 126 to 151 in 112a968

# drop a trailing 1 from dim, so user doesn't need to deal with it
# Ugh, need to get rid of column vector thing soon.
weights_extra_dim <- dim
n_extra_dim <- length(weights_extra_dim)
if (weights_extra_dim[n_extra_dim] == 1) {
weights_extra_dim <- weights_extra_dim[-n_extra_dim]
}
# remainder should be 1 or match weights_extra_dim
w_dim <- weights_dim[-1]
dim_1 <- length(w_dim) == 1 && w_dim == 1
dim_same <- all(w_dim == weights_extra_dim)
if (!(dim_1 | dim_same)) {
msg <- cli::format_error(
c(
"the dimension of weights must be either \\
{.val {n_distributions}x1} or \\
{.val {n_distributions}x{paste(dim, collapse = 'x')}}",
" but was {.val {paste(weights_dim, collapse = 'x')}}"
)
)
stop(
msg,
call. = FALSE
)
}

I'm not sure why this is erroring, and I don't think I understand the reason for the error handling, since the error message suggests that the dimension of the weights should be either "2x1" or "2x5x1", but then errors saying that the weights were "2x5x1". The comments seem to suggest that it is trying to help by dropping a trailing 1, but in this instance, I think we want that?

@goldingn do you have thoughts on this?

@njtierney njtierney added this to the 0.6.0 milestone Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants