Skip to content

Instantly share code, notes, and snippets.

View twolodzko's full-sized avatar

Timothy Wolodzko twolodzko

View GitHub Profile
@twolodzko
twolodzko / bayesian-ab-example.R
Created March 26, 2018 14:22
Bayesian A/B testing with Bayesian updating using beta-binomial model
library(ggplot2)
set.seed(123)
# simulated data
n <- 100
nx <- ny <- 500
x <- rbinom(n, nx, 0.22)
y <- rbinom(n, ny, 0.21)
@twolodzko
twolodzko / holt.stan
Last active March 26, 2018 06:50
Holt time-series forecasting in Stan
data {
int<lower=3> n;
vector[n] y;
int<lower=0> h;
}
parameters {
real<lower=0, upper=1> alpha;
real<lower=0, upper=1> beta;
real<lower=0> sigma;
vector[2] mu_init;
@twolodzko
twolodzko / rle2.cpp
Created March 26, 2018 06:27
Runnng lengths in Rcpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
List cpp_rle2(
const NumericVector& x,
const double h = 0,
const double eps = 1e-16
) {
import numpy as np
from sklearn.base import BaseEstimator, ClassifierMixin
from sklearn.ensemble import IsolationForest
from sklearn.covariance import EllipticEnvelope
from sklearn.svm import OneClassSVM
def mode(x):
(values, counts) = np.unique(x, return_counts = True)
lst <- function(...) {
dots <- list(...)
call <- match.call()
names(dots) <- sapply(substitute(...()), as.character)
dots
}
funs <- lst(mode, typeof, class, is.atomic, is.array, is.vector, is.numeric, is.character, is.logical, is.matrix, is.data.frame, is.list)
tests <- lst(
library(extraDistr)
bins <- function(x, n = 512L) {
x <- x[!is.na(x)]
h <- diff(range(x))/(n-4)
mids <- seq(min(x)-2*h, max(x)+2*h, length.out = n)
breaks <- c(mids[1L] - h, mids + h)
counts <- unname(tapply(x, cut(x, breaks), length,
default = 0L))
@twolodzko
twolodzko / metropolis-algorithm-example.R
Last active August 17, 2017 09:55
Simple example of sampling using Metropolis algorithm
set.seed(123)
x <- rnorm(7)
dx <- density(x)
plot(dx)
f <- approxfun(dx, yleft = 0, yright = 0)
@twolodzko
twolodzko / fitting-mixtures-example.R
Created August 17, 2017 09:45
Examples of fitting mixture densities (including aggregate data)
set.seed(123)
N <- 1e5
mu <- c(2,4,6)
sigma <- rep(0.5, 3)
lambda <- (1:3)/6
dat <- c(
@twolodzko
twolodzko / bandwidth-choice-in-kde-example.R
Created August 17, 2017 09:43
Examples of bandwidth choice in kernel density estimation
ptmp <- par(mfrow=c(2,2), mar = c(3,3,3,3))
set.seed(123)
n <- 7
x <- rnorm(n, sd = 3)
K <- function(x) dnorm(x)
@twolodzko
twolodzko / uninformative-beta-priors-example.R
Created August 17, 2017 09:41
Examples of uninformative beta priors for binomial distribution
priors <- c(0, 0.001, 0.333, 0.5, 1)
s <- c(0, 1, 1, 5, 5)
f <- c(1, 1, 2, 1, 25)
dbeta2 <- function(x, prior1, prior2 = prior1) {