Skip to content

Commit

Permalink
Version 0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
prdm0 committed May 7, 2024
1 parent 6f70ae0 commit 5409119
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 12 deletions.
6 changes: 3 additions & 3 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 0.1.1
Date: 2024-04-24 16:27:00 UTC
SHA: 64032aec8d4d6aef2e7e10002c67c883b4dd6086
Version: 0.1.2
Date: 2024-05-06 19:46:15 UTC
SHA: 6f70ae08bd81bb87481903682b06ebcd8b8141b7
8 changes: 7 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@

* The [DESCRIPTION](https://raw.githubusercontent.com/prdm0/AcceptReject/main/DESCRIPTION) file was edited;

* Another bibliographic reference was added to the `accept_reject()` function.
* Another bibliographic reference was added to the `accept_reject()` function;

* The dependency on the `lbfgs` package has been removed;

* New unit tests have been introduced;

* Bug fix.
40 changes: 38 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ As an example, let $X \sim Poisson(\lambda = 0.7)$. We will generate $n = 1000$
```{r}
library(AcceptReject)
library(cowplot) # install.packages("cowplot")
# Ensuring Reproducibility
set.seed(0)
Expand Down Expand Up @@ -162,6 +163,42 @@ For the discrete case, if the user mistakenly specifies any of these arguments,

If you choose to specify a base density, it's convenient to inspect it by comparing the base density function with the theoretical probability density function. The [`inspect()`](https://prdm0.github.io/AcceptReject/reference/accept_reject.html) function facilitates this task. The [`inspect()`](https://prdm0.github.io/AcceptReject/reference/accept_reject.html) function will plot the base probability density function and the theoretical probability density function, find the intersection between the densities, and display the value of the intersection area on the plot. These are important pieces of information to decide if the base probability density function specified in the `args_f_base` argument and the value of `c` (default is 1) are appropriate.

Another example, considering $X \sim Beta(\alpha = 2, \beta = 2)$:

```{r}
library(AcceptReject)
library(cowplot)
# Ensuring reproducibility
set.seed(0)
x <- accept_reject(
n = 100000L,
f = dbeta,
continuous = TRUE,
args_f = list(shape1 = 2, shape2 = 2),
xlim = c(0, 1)
)
print(x)
a <- plot(x)
b <- qqplot(x)
plot_grid(a, b, nrow = 2L, labels = c("a", "b"))
# Inspecting the estimated value of c by accept_reject()
# Note that c = 1.5 indeed causes the base density,
# which in this case was the default (uniform), to overlap the # density of the beta:
inspect(
f = dbeta,
args_f = list(shape1 = 2, shape2 = 2),
f_base = dunif,
args_f_base = c(min = 0, max = 1),
xlim = c(0, 1),
c = attr(x, "c")
)
```

Note that `c` is an attribute of `x`. To access all attributes, you can use `attributes(x)`. Most likely, you won't need to access these attributes. They are useful for plotting methods.

## 🕵️‍♀️ Example of inspection

```{r}
Expand Down Expand Up @@ -207,8 +244,7 @@ library(tictoc) # install.packages("tictoc")
# Ensuring reproducibility
set.seed(0)
# Não especificando a função densidade de probabilidade base
# Not specifying the base probability density function
tic()
case_1 <- accept_reject(
n = 2000,
Expand Down
68 changes: 62 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ library(AcceptReject)
#>
#> qqplot
library(cowplot) # install.packages("cowplot")

# Ensuring Reproducibility
set.seed(0)

Expand Down Expand Up @@ -293,6 +294,62 @@ plot. These are important pieces of information to decide if the base
probability density function specified in the `args_f_base` argument and
the value of `c` (default is 1) are appropriate.

Another example, considering $X \sim Beta(\alpha = 2, \beta = 2)$:

``` r
library(AcceptReject)
library(cowplot)

# Ensuring reproducibility
set.seed(0)

x <- accept_reject(
n = 100000L,
f = dbeta,
continuous = TRUE,
args_f = list(shape1 = 2, shape2 = 2),
xlim = c(0, 1)
)
print(x)
#>
#> ── Accept-Reject Samples ───────────────────────────────────────────────────────
#> ℹ It's not necessary, but if you want to extract the observations, use as.vector().
#> ✔ Case: continuous
#> ✔ Number of observations: 100000
#> ✔ c: 1.5
#> ✔ Probability of acceptance (1/c): 0.6667
#> ✔ Observations: 0.8967 0.2655 0.3721 0.5729 0.6608 0.6291 0.206 0.1766 0.687 0.3841...
#> ✔ xlim = 0 1
#>
#> ────────────────────────────────────────────────────────────────────────────────
a <- plot(x)
b <- qqplot(x)
plot_grid(a, b, nrow = 2L, labels = c("a", "b"))
```

<img src="man/figures/README-unnamed-chunk-5-1.png" width="100%" />

``` r

# Inspecting the estimated value of c by accept_reject()
# Note that c = 1.5 indeed causes the base density,
# which in this case was the default (uniform), to overlap the # density of the beta:
inspect(
f = dbeta,
args_f = list(shape1 = 2, shape2 = 2),
f_base = dunif,
args_f_base = c(min = 0, max = 1),
xlim = c(0, 1),
c = attr(x, "c")
)
```

<img src="man/figures/README-unnamed-chunk-5-2.png" width="100%" />

Note that `c` is an attribute of `x`. To access all attributes, you can
use `attributes(x)`. Most likely, you won’t need to access these
attributes. They are useful for plotting methods.

## 🕵️‍♀️ Example of inspection

``` r
Expand Down Expand Up @@ -327,7 +384,7 @@ b <- inspect(
plot_grid(a, b, nrow = 2L, labels = c("a", "b"))
```

<img src="man/figures/README-unnamed-chunk-5-1.png" width="100%" />
<img src="man/figures/README-unnamed-chunk-6-1.png" width="100%" />

Notice that considering the distribution in scenario “a” in the code
above is more convenient. Note that the area is approximately 1, the
Expand All @@ -350,8 +407,7 @@ library(tictoc) # install.packages("tictoc")
# Ensuring reproducibility
set.seed(0)

# Não especificando a função densidade de probabilidade base

# Not specifying the base probability density function
tic()
case_1 <- accept_reject(
n = 2000,
Expand All @@ -361,7 +417,7 @@ case_1 <- accept_reject(
xlim = c(0, 6)
)
toc()
#> 0.005 sec elapsed
#> 0.003 sec elapsed

# Specifying the base probability density function
tic()
Expand All @@ -386,7 +442,7 @@ p2 <- plot(case_2)
plot_grid(p1, p2, nrow = 2L)
```

<img src="man/figures/README-unnamed-chunk-6-1.png" width="100%" />
<img src="man/figures/README-unnamed-chunk-7-1.png" width="100%" />

``` r

Expand All @@ -396,7 +452,7 @@ q2 <- qqplot(case_2)
plot_grid(q1, q2, nrow = 1L)
```

<img src="man/figures/README-unnamed-chunk-6-2.png" width="100%" />
<img src="man/figures/README-unnamed-chunk-7-2.png" width="100%" />

Notice that the results were very close in a graphical analysis.
However, the execution time specifying a convenient base density was
Expand Down
Binary file modified man/figures/README-unnamed-chunk-3-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-3-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-4-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-4-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-5-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-5-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-6-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added man/figures/README-unnamed-chunk-7-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added man/figures/README-unnamed-chunk-7-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5409119

Please sign in to comment.