-
Notifications
You must be signed in to change notification settings - Fork 0
/
density_ratio.Rmd
42 lines (31 loc) · 1.43 KB
/
density_ratio.Rmd
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
---
title: "``brms::density_ratio`` : compute Density Ratios"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(brms)
library(ggplot2)
```
#### Description
Compute the ratio of two densities at given points based on samples of the corresponding distributions.
#### Usage
<pre><code>
density_ratio(x, y = NULL, point = 0, n = 4096, ...)
</code></pre>
#### Arguments
* ``x``: Vector of samples from the first distribution, usually the posterior distribution of the quantity of interest.
* ``y``: Optional vector of samples from the second distribution, usually the prior distribution of the quantity of interest. If NULL (the default), only the density of x will be evaluated.
* ``point``: Numeric values at which to evaluate and compare the densities. Defaults to 0.
* ``n``: Single numeric value. Influences the accuracy of the density estimation. See density for details.
* `` ... ``: Further arguments passed to density.
#### Details
In order to achieve sufficient accuracy in the density estimation, more samples than usual are required. That is you may need an effective sample size of 10,000 or more to reliably estimate the densities.
#### Value
A vector of length equal to length(point). If y is provided, the density ratio of x against y is returned. Else, only the density of x is returned.
#### Examples
```{r}
x <- rnorm(10000)
y <- rnorm(10000, mean = 1)
density_ratio(x, y, point = c(0, 1))
``