Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created December 29, 2025 10:18
Show Gist options
  • Select an option

  • Save abikoushi/1d084f34ae30a55543f4554cdc75e308 to your computer and use it in GitHub Desktop.

Select an option

Save abikoushi/1d084f34ae30a55543f4554cdc75e308 to your computer and use it in GitHub Desktop.
Canonical form parametrized normal distribution
rcfnorm <- function(n, shape=0, inv_scale=1){
rnorm(n, shape/inv_scale, 1/sqrt(inv_scale))
}
dcfnorm <- function(n, shape=0, inv_scale=1, ...){
dnorm(n, shape/inv_scale, 1/sqrt(inv_scale), ...)
}
pcfnorm <- function(n, shape=0, inv_scale=1, ...){
pnorm(n, shape/inv_scale, 1/sqrt(inv_scale), ...)
}
a1 = 1; b1 = 0.5
a2 = 2; b2 = 2.5
x1 = rcfnorm(10000, a1, b1)
x2 = rcfnorm(10000, a2, b2)
x = x1+x2
hist(x, breaks = "scott", freq=FALSE)
B = 1/(1/b1+1/b2)
A = B*(a1/b1+a2/b2)
curve(dcfnorm(x, A, B), add=TRUE, col="orangered")
plot(ecdf(x1+x2))
curve(pcfnorm(x, A, B), add=TRUE, col="orangered", lty=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment