mitchelloharawild / distributional

Vectorised distributions for R
https://pkg.mitchelloharawild.com/distributional
GNU General Public License v3.0
94 stars 15 forks source link

Inconsistent return from generate with transformed distributions #12

Closed robjhyndman closed 4 years ago

robjhyndman commented 4 years ago

library(fable)
#> Loading required package: fabletools
library(tsibble)
library(distributional)
#> 
#> Attaching package: 'distributional'
#> The following object is masked from 'package:fabletools':
#> 
#>     dist_normal
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

as_tsibble(USAccDeaths) %>%
  model(ETS(value)) %>%
  forecast() %>%
  mutate(
    sample = generate(value, times=3),
  )
#> # A fable: 24 x 5 [1M]
#> # Key:     .model [1]
#>    .model        index            value  .mean sample   
#>    <chr>         <mth>           <dist>  <dbl> <list>   
#>  1 ETS(value) 1979 Jan   N(8397, 85668)  8397. <dbl [3]>
#>  2 ETS(value) 1979 Feb  N(7599, 115955)  7599. <dbl [3]>
#>  3 ETS(value) 1979 Mar  N(8397, 146241)  8397. <dbl [3]>
#>  4 ETS(value) 1979 Apr  N(8647, 176528)  8647. <dbl [3]>
#>  5 ETS(value) 1979 May  N(9443, 206815)  9443. <dbl [3]>
#>  6 ETS(value) 1979 Jun  N(9893, 237102)  9893. <dbl [3]>
#>  7 ETS(value) 1979 Jul N(10819, 267388) 10819. <dbl [3]>
#>  8 ETS(value) 1979 Aug  N(10107, 3e+05) 10107. <dbl [3]>
#>  9 ETS(value) 1979 Sep  N(9015, 327962)  9015. <dbl [3]>
#> 10 ETS(value) 1979 Oct  N(9354, 358249)  9354. <dbl [3]>
#> # … with 14 more rows

as_tsibble(USAccDeaths) %>%
  model(ETS(log(value))) %>%
  forecast() %>%
  mutate(
    sample = generate(value, times=3),
  )
#> # A fable: 24 x 5 [1M]
#> # Key:     .model [1]
#>    .model             index             value  .mean sample    
#>    <chr>              <mth>            <dist>  <dbl> <list>    
#>  1 ETS(log(value)) 1979 Jan   t(N(9, 0.0011))  8297. <list [1]>
#>  2 ETS(log(value)) 1979 Feb t(N(8.9, 0.0013))  7509. <list [1]>
#>  3 ETS(log(value)) 1979 Mar   t(N(9, 0.0017))  8319. <list [1]>
#>  4 ETS(log(value)) 1979 Apr    t(N(9, 0.002))  8513. <list [1]>
#>  5 ETS(log(value)) 1979 May t(N(9.1, 0.0023))  9381. <list [1]>
#>  6 ETS(log(value)) 1979 Jun t(N(9.2, 0.0027))  9882. <list [1]>
#>  7 ETS(log(value)) 1979 Jul t(N(9.3, 0.0031)) 10750. <list [1]>
#>  8 ETS(log(value)) 1979 Aug t(N(9.2, 0.0033)) 10083. <list [1]>
#>  9 ETS(log(value)) 1979 Sep t(N(9.1, 0.0036))  9020. <list [1]>
#> 10 ETS(log(value)) 1979 Oct t(N(9.1, 0.0039))  9363. <list [1]>
#> # … with 14 more rows

Created on 2020-04-10 by the reprex package (v0.3.0)

mitchelloharawild commented 4 years ago

MRE:

library(distributional)
generate(dist_normal(), 1)
#> [[1]]
#> [1] 0.1499626
generate(exp(dist_normal()), 1)
#> [[1]]
#> [[1]][[1]]
#> [1] 1.175355

Created on 2020-04-10 by the reprex package (v0.3.0)

mitchelloharawild commented 4 years ago

Fixed, thanks.

library(distributional)
generate(dist_normal(), 1)
#> [[1]]
#> [1] -1.334307
generate(exp(dist_normal()), 1)
#> [[1]]
#> [1] 0.07839852

Created on 2020-04-12 by the reprex package (v0.3.0)