paroussisc / stats

Repo to track the material I cover on Wednesdays
MIT License
1 stars 0 forks source link

Transformation of random variables example #4

Open paroussisc opened 5 years ago

paroussisc commented 5 years ago

This issue is looking at the transformation of a Gaussian rv Z. Here X = log(Z).

paroussisc commented 5 years ago

Using

codecogseqn 14

we get:

f_x(x) = dnorm(exp(x))*exp(x)

paroussisc commented 5 years ago

We compare simulated rvs to the analytical pdf using this code:

library(ggplot2)
library(distr)

mu <- 13
sd <- 1.3
transformed_pdf <- function(x)
{
    return(dnorm(exp(x), mu, sd) * exp(x))
}

z <- rnorm(10000, mu, sd)
x <- data.frame(x = log(z))

ggplot(x, aes(x = x)) + geom_density(color = "blue", alpha = 0.25) + stat_function(data = data.frame(x = c(2.4, 2.7)),
                                                                                   color = "red",
                                                                                   aes(x),
                                                                                   fun = transformed_pdf)
paroussisc commented 5 years ago

And it looks like the analytical solution is correct:

double_plot

Of course, taking the log of a Gaussian isn't the best idea, but there shouldn't be any negative values to take the log of due to the relatively high mean and low variance.