I want to compute a posterior density plot with conjugate prior
I have data with known parameters (mean =30 , sd =10)
I have two priors one with normal distribution with known parameter ( mean =10 , sd=5) and other with t distribution with same mean and sd but degree of freedom 4
I want a graph with density plots for prior,data and posterior ?
can you help me with r code for this problem ?
Plus I am getting wrong density function for posterior in my opinion..Here is my code so far
x=seq(from=-90, to=90, by= 1)
data=dnorm(x,mean=30,sd =10)
prior = dnorm(x,mean=10,sd =5)
posterior = dnorm(x,mean=10,sd =5)*dnorm(x,mean=30,sd =10) # prior* data #Prior*data
plot(x,data , type="l", col="blue")
lines(x,prior, type="l", col="red")
lines(x,posterior , type="l", col="green")
You need to add the two distributions together not multiply. I attach an example below that uses equal weight between the two distributions:
x <- seq(from = -90, to = 90, by = 1)
data <- dnorm(x, mean = 30, sd = 10)
prior <- dnorm(x, mean = 10, sd = 5)
posterior <- 0.5 * dnorm(x, mean = 10, sd = 5) + 0.5 * dnorm(x, mean = 30, sd = 10)
plot(x, prior, type = "l", col = "red")
lines(x, posterior, type = "l", col = "green")
lines(x, data , type = "l", col = "blue")
Related
Is the wrong or am I overthinking?
For a binary classification problem {0, 1} with 1 predictor X. The prior probability of being in class 0 is = 0.6 and the density function is standard normal f0(x) = Normal (0,1)
The density function for X in class 1 is also normal but with mean = 1 and variance = 0.5. f1(x) = Normal (1, 0.5)
and I need to plot priorprobabilityf0(x) and priorprobailityf1(x) into the same figure.
This is what I have written in R but I cant seem to work out if I was meant to include the prior probabilities.
x <- seq(-5, 5, length=200)
y <- dnorm(x, mean= 0, sd=1)
t = sqrt(0.5)
z <- seq(-5, 5, length=200)
a <- dnorm(x, mean=1, sd=t)
plot(z, a, type="l", lwd=2, col='red', xlim = c(-5,5), ylim = c(0,0.65), xlab = "Observed Value", ylab = "Probability Density")
lines(x,y, col='blue', lwd=2)
I am trying to generate 100 random data(s) from normal distribution, create histogram of it and put density function over the histogram.
So far i have created
set.seed(123)
rs <- rnorm(100, mean = weighted.mean(femals$Salary), sd = sd(femals$Salary))
h <- hist(rs, col = "lightgray" , density = 50 )
xfit <- seq(min(femals$Salary), max(femals$Salary), length = 40)
yfit <- dnorm(xfit, mean = mean(femals$Salary), sd = sd(femals$Salary))
yfit <- yfit * diff(h$mids[1:2]) * length(femals$Salary)
lines(xfit, yfit, col = "red", lwd = 2)
The result of this is
However i am unsure if this is correct. Isnt density function way to low for that histogram? Shouldnt density follow the edges of histogram? Is this correct or did i make mistake in my code?
the mean and standard deviaton are:
weighted mean(femals$Salary) = 5138.852
sed(femals$Salary) = 539.8707
I'm trying to find if (a) fewer than 62% or more than 74% of the sample means within one standard deviation of the expected value, or (b) fewer than 92% or more than 98% of the sample means within two standard deviations of the expected value.
Given that we have already set mu and sigma, and Finv is a quantile function. I was given the last two lines of code. Can someone please explain to me what they mean and what kind of output I should be getting? (Currently my only output is 0)
n.iterations <- 100000
n <- 10
xbar <- numeric(n.iterations)
for (i in 1:n.iterations){
x <- sapply(runif(n), Finv)
xbar[i] <- mean(x)
}
mean((mu-1*sigma/sqrt(n) <= xbar) & (xbar <= mu+1*sigma/sqrt(n)))
mean((mu-2*sigma/sqrt(n) <= xbar) & (xbar <= mu+2*sigma/sqrt(n)))
I'm a little bit puzzled by your question, because it askes about data "within standard deviation" but also asks about quantiles - which seems odd... and here is why
Consider the upper picture generated from the following code:
mymean <- 5
mysd <- 2
curve(dnorm(x, mean = mymean, sd = mysd), from = -2, to = 12)
abline(v = mymean, col = "red", lwd = 2)
xtimessd = 1
abline(v = c(mymean - mysd*xtimessd, mymean + mysd*xtimessd), col = "blue", lwd = 1, lty = 2)
xtimessd = 2
abline(v = c(mymean - mysd*xtimessd, mymean + mysd*xtimessd), col = "cyan", lwd = 1, lty = 2)
xtimessd = 3
abline(v = c(mymean - mysd*xtimessd, mymean + mysd*xtimessd), col = "green", lwd = 1, lty = 2)
# 62th and 74th quantile
targetQunatiles <- qnorm(c(0.62, 0.75), mean = mymean, sd = mysd)
abline(v = targetQunatiles, col = "orange", lwd = 2, lty = 1)
Given your population mean and standard deviation the figure about shows the probability density function of a normal distribution.
The dotted lines are the "xtimes within sd" values. (There is really no magic, but it is related to the 68–95–99.7 rule).
On the other hand, if we look into the quantile function, i.e., in your example we are looking into values 62% and 74%, that can be computed by qnorm.
As you can see, based on your question "fewer than 62% or more than 74% of the sample means", you will exclude values between 5.610962 and 6.348980.
So, still, from your question I don't know what you are asking about the relation between the statement of "within sd" and "looking for quantiles" as both are independen of each other.
I am trying to numerically calculate marginal likelihood (marginalize over a positive parameter). I am using Gamma distribution as prior for that parameter. Here I looked at the behavior of Gamma distribution for two specific parameter settings:
s = 28.4; r = 17000
plot(x, dgamma(x, shape=s, rate = r), type = 'l', ylab = 'density')
abline(v = s/r, col = 'red')
I got the following results:
Then I tried the following to get a tighter Gamma distribution:
lines(x, dgamma(x, shape=s*1000, rate = r*1000), col = 'blue')
and the result:
I am confused. As distribution gets tighter, the height should grow taller, otherwise the area won't integrate to 1. Did I miss anything? Or is there any numerally problems? Thanks!
Your x variable needs to have more samples to capture the narrow peak in the second density function:
x = seq(0, .01, .000001)
s = 28.4; r = 17000
plot(x, dgamma(x, shape=s, rate = r), type = 'l', ylab = 'density')
abline(v = s/r, col = 'red')
lines(x, dgamma(x, shape=s*1000, rate = r*1000), col = 'blue')
I want to create 3 plots for illustration purposes:
- normal distribution
- right skewed distribution
- left skewed distribution
This should be an easy task, but I found only this link, which only shows a normal distribution. How do I do the rest?
If you are not too tied to normal, then I suggest you use beta distribution which can be symmetrical, right skewed or left skewed based on the shape parameters.
hist(rbeta(10000,5,2))
hist(rbeta(10000,2,5))
hist(rbeta(10000,5,5))
Finally I got it working, but with both of your help, but I was relying on this site.
N <- 10000
x <- rnbinom(N, 10, .5)
hist(x,
xlim=c(min(x),max(x)), probability=T, nclass=max(x)-min(x)+1,
col='lightblue', xlab=' ', ylab=' ', axes=F,
main='Positive Skewed')
lines(density(x,bw=1), col='red', lwd=3)
This is also a valid solution:
curve(dbeta(x,8,4),xlim=c(0,1))
title(main="posterior distrobution of p")
just use fGarch package and these functions:
dsnorm(x, mean = 0, sd = 1, xi = 1.5, log = FALSE)
psnorm(q, mean = 0, sd = 1, xi = 1.5)
qsnorm(p, mean = 0, sd = 1, xi = 1.5)
rsnorm(n, mean = 0, sd = 1, xi = 1.5)
** mean, sd, xi location parameter mean, scale parameter sd, skewness parameter xi.
Examples
## snorm -
# Ranbdom Numbers:
par(mfrow = c(2, 2))
set.seed(1953)
r = rsnorm(n = 1000)
plot(r, type = "l", main = "snorm", col = "steelblue")
# Plot empirical density and compare with true density:
hist(r, n = 25, probability = TRUE, border = "white", col = "steelblue")
box()
x = seq(min(r), max(r), length = 201)
lines(x, dsnorm(x), lwd = 2)
# Plot df and compare with true df:
plot(sort(r), (1:1000/1000), main = "Probability", col = "steelblue",
ylab = "Probability")
lines(x, psnorm(x), lwd = 2)
# Compute quantiles:
round(qsnorm(psnorm(q = seq(-1, 5, by = 1))), digits = 6)