While writing the code for bayesian modeling, you will have to test
the distribution (prior, likelihood or posterior). Here are two common
scenarios that you might encounter:
We will start by simplifying the above two cases by assuming standard normal distribution. So, in the first case, we have access to “rnorm” function and in second case, we have access to “dnorm” function of R.
Case 1: In this case, we first collect random deviates (in “vals”) and then use ggplot to plot them:
The output of above R script will look something like this:
Case 2: In this case, we have to assume a bounding box and sample inside that to get “x_vals” and “y_vals” (just like rejection sampling):
The output of above R script will look something like this:
Just as a teaser to a post that I will post later, we can use the script somewhat similar to that of case 1 to study the characteristics of a distribution:
- You want to test the function that generates random deviates. For example: you have derived a conjugate formula for a parameter of your model and want to test whether it is correct or not.
- You want to test a probability density function. For example: likelihood or posterior function that you might want to run rejection sampler on.
We will start by simplifying the above two cases by assuming standard normal distribution. So, in the first case, we have access to “rnorm” function and in second case, we have access to “dnorm” function of R.
Case 1: In this case, we first collect random deviates (in “vals”) and then use ggplot to plot them:
library(ggplot2) vals = rnorm(10000, mean=0, sd=1) df = data.frame(xVals=vals) ggplot(df, aes(x=xVals)) + geom_density()
The output of above R script will look something like this:
Case 2: In this case, we have to assume a bounding box and sample inside that to get “x_vals” and “y_vals” (just like rejection sampling):
library(ggplot2) x_vals=runif(10000,min=-4,max=4) y_vals=dnorm(x_vals, mean=0, sd=1) df = data.frame(xVals=x_vals, yVals=y_vals) ggplot(df, aes(x=xVals, y=yVals)) + geom_line()
The output of above R script will look something like this:
Just as a teaser to a post that I will post later, we can use the script somewhat similar to that of case 1 to study the characteristics of a distribution:
No comments:
Post a Comment