Skip to content

Instantly share code, notes, and snippets.

@expersso
Created October 18, 2018 15:01
Show Gist options
  • Save expersso/c3df078c013b228325251d5c8453153b to your computer and use it in GitHub Desktop.
Save expersso/c3df078c013b228325251d5c8453153b to your computer and use it in GitHub Desktop.
Illustration of random sequence that convergences in distribution, but not density
library(tidyverse)
library(gganimate)
f <- function(n, x) 1 - cos(2 * pi * n * x)
g <- function(.f, x) map_dbl(x, ~integrate(function(z) .f(z), 0, .x)$value)
df <- cross_df(list(
x = seq(0, 1, length.out = 100),
n = c(1, 2, 3, 5, 10, 15, 25)
)) %>%
mutate(pdf = f(n, x)) %>%
group_by(n) %>%
mutate(cdf = map2_dbl(n, x, ~g(partial(f, n = .x), .y))) %>%
gather("distribution", "f_x", pdf:cdf) %>%
mutate(f_unif = if_else(distribution == "pdf", dunif(x), punif(x)))
ggplot(df, aes(x = x)) +
geom_line(aes(y = f_unif, linetype = "X ~ Unif[0,1]"), color = "black") +
geom_line(aes(y = f_x, color = n, linetype = "Xn")) +
facet_wrap(~distribution, scales = "free") +
transition_states(n, 1, 1) +
scale_color_gradient(low = "red", high = "blue") +
scale_linetype_manual(values = c("dashed", "solid")) +
theme_minimal() +
theme(legend.position = "bottom") +
guides(linetype = guide_legend("Random variable", "top", ncol = 1),
color = guide_colorbar(title.position = "top")) +
labs(
title = "Example of random sequence that converges in distribution but not density",
subtitle = expression(pdf(X[n]): f[n](x) == 1 - cos(2*pi*n*x)*bold(1)[(paste(0, ',', 1))]),
y = "F(x) / f(x)", linetype = NULL
)
anim_save("convergence_in_distribution.gif")
@expersso
Copy link
Author

convergence_in_distribution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment