Skip to content

Instantly share code, notes, and snippets.

View twolodzko's full-sized avatar

Timothy Wolodzko twolodzko

View GitHub Profile
@twolodzko
twolodzko / distrtoy.R
Created July 11, 2017 08:14
Interactive visualizations for common distributions in RStudio
library(manipulate)
# Beta
x <- seq(0, 1, by=0.01)
manipulate(
{ ci <- qbeta(c(0.05, .5, 0.95), alpha, beta)
plot(x, dbeta(x, alpha, beta),
col="blue", lwd=2, type="l", las=1, bty="n",
@twolodzko
twolodzko / logistic-growth-curve.R
Created August 5, 2017 19:14
example of logistic growth curve
# use RStudio
library(manipulate)
lgrowth <- function(x, a, b, c) a/(1+exp(-(b+c*x)))
manipulate(
curve(lgrowth(x, a, b, c), -4, 4, ylim = c(-1, 2)),
a = slider(-2, 2, initial = 1, step = 0.01),
b = slider(-2, 2, initial = 0, step = 0.01),
@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) {
@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 / 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 / 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)
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))
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(
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)
@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
) {