Last active
May 2, 2016 02:01
-
-
Save RHDZMOTA/2d5905ac7ad5cb26f743d5519af4496e to your computer and use it in GitHub Desktop.
General methodology to calculate the inverse function via a numeric approach.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# INVERSE FUNCTION | |
# by: Rodrigo Hernández Mota | |
# created in: 29/04/2016 | |
# last modify: 02/05/2016 | |
source("generate_dataset.R") | |
library(ggplot2) | |
library(tibble) | |
library(tidyr) | |
# Inverse Function Estimated ---------------------------------------------- | |
# This is the numerical estimation of the inverse function. | |
# The following variables area needed: | |
# - u: the variable [0, 1] to calculate the inverse into (runif) | |
# - x: the x-axis | |
# - z: the accumulated values (cdf function) | |
inverse_func <- function(u, x, z){ | |
# determine the "position" of u in z | |
i <- z <= u | |
i <- sum(i) | |
# use linear interpolation to map | |
# the variable | |
m <- (x[i+1]-x[i])/(z[i+1]-z[i]) | |
b <- x[i] - m * z[i] | |
# return the result | |
m * u + b | |
} | |
# numeric estimation and generation of rv | |
n <- 5000 | |
uniform_rv <- runif(n) | |
sub_data <- dataset[dataset$dens == "cdf", ] | |
random_variables <- sapply(uniform_rv, inverse_func, | |
x = sub_data$x, z = sub_data$freq) | |
# ggplot(data_frame(random_variables), aes(random_variables, y = ..density..)) + | |
# geom_density(fill = "blue", alpha = 0.2) + theme_light() | |
aux <- data_frame(x = 1:n, Desired = random_variables, Uniform = uniform_rv) | |
aux1 <- gather(aux[1:50, ], Distribution, Value, -x) | |
ggplot(aux, aes(Uniform, Desired)) + theme_light() + geom_point(size = 0.5) + | |
ggtitle("Relation Among the Random Numbers") + xlab("Uniform distr. rv") + | |
ylab("Desired distr. rv") | |
ggplot(aux1, aes(x,Value, group = Distribution)) + theme_light() + | |
geom_point(aes(color = Distribution)) + | |
geom_line(aes(color = Distribution), alpha = 0.3) + | |
scale_color_brewer(palette = 3, direction = 1) + | |
ggtitle("Sample: first 50 transformations") + | |
xlab("Number") + ylab("Value") | |
# Estimated inverse function ---------------------------------------------- | |
ggplot(data_frame(x = (1:n)/n, y = random_variables[order(random_variables)]), | |
aes(x,y)) + theme_light() + | |
geom_ribbon(aes(ymin = 0, ymax = y), fill = "blue", alpha = 0.2) + | |
geom_line(size = 0.75) + geom_hline(yintercept = 0) + | |
ggtitle("Estimated Inverse Function") + | |
xlab("Freq.") + ylab("Value") | |
# Frequency histogram ----------------------------------------------------- | |
ggplot(data_frame(random_variables), aes(random_variables, y = ..density..)) + | |
geom_histogram(fill = "blue", alpha = 0.3, bins = floor(sqrt(n)), color = "black") + | |
theme_light() + | |
ggtitle("Frequency histogram of generated random variables") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment