Skip to content

Instantly share code, notes, and snippets.

View alexpghayes's full-sized avatar

alex hayes alexpghayes

View GitHub Profile
@ntamas
ntamas / walker.py
Last active April 15, 2020 14:37
Weighted sampling with replacement using Walker's alias method - NumPy version
#!/usr/bin/env python
from numpy import arange, array, bincount, ndarray, ones, where
from numpy.random import seed, random, randint
__author__ = "Tamas Nepusz, Denis Bzowy"
__version__ = "27jul2011"
class WalkerRandomSampling(object):
"""Walker's alias method for random objects with different probablities.
@jrnold
jrnold / gist:6799152
Last active August 18, 2021 13:44
Create a plot of the normal distribution with an area shaded in. Useful for teaching z-scores and stuff like that.
library("ggplot2")
#' Draw Normal Distribution Density with an area shaded in.
#'
#' @param lb Lower bound of the shaded area. Use \code{-Inf} for a left tail.
#' @param ub Upper bound of the shaded area. Use \code{Inf} for a right tail.
#' @param mean Mean of the normal distribution
#' @param sd Standard deviation of the normal distribution
#' @param limits Lower and upper bounds on the x-axis of the area displayed.
#' @return ggplot object.
#' ---
#' output:
#' html_document:
#' keep_md: TRUE
#' ---
#+ include = FALSE
library(dplyr)
#' Responses to [my
@stevenpollack
stevenpollack / !README.MD
Last active February 28, 2024 00:56
A simple R package development best-practices-example

R package development "best practices"

The core of this tutorial gist lies in bestPracticesWalkThrough.R. Running assumes you have the following packages at versions equal (or above) those specified

library('devtools') # 1.9.1
library('testthat') # 0.11.0
library('stringr')  # 1.0.0
library('git2r') # 0.12.1
@fredbenenson
fredbenenson / kickstarter_sql_style_guide.md
Last active June 24, 2024 03:28
Kickstarter SQL Style Guide
layout title description tags
default
SQL Style Guide
A guide to writing clean, clear, and consistent SQL.
data
process

Purpose

@mike-lawrence
mike-lawrence / gp_regression.stan
Last active April 7, 2020 18:15
GP Regression example
functions{
// GP: computes noiseless Gaussian Process
vector GP(real volatility, real amplitude, vector normal01, int n_x, real[] x ) {
matrix[n_x,n_x] cov_mat ;
real amplitude_sq_plus_jitter ;
amplitude_sq_plus_jitter = amplitude^2 + 1e-6 ;
cov_mat = cov_exp_quad(x, amplitude, 1/volatility) ;
for(i in 1:n_x){
cov_mat[i,i] = amplitude_sq_plus_jitter ;
}
@noamross
noamross / find_local_tweeps.R
Created August 14, 2017 23:43
A visit to Durham
library(rtweet) #rtweet API creds should already be set up
library(stringi)
library(dplyr)
friends = get_friends(user="noamross")
followers = get_followers("noamross")
tweeps_id = distinct(bind_rows(friends, followers))
tweeps_info = lookup_users(tweeps_id$user_id)
# A regex for a visit to Durham
// [[Rcpp::depends(xtensor)]]
#include <numeric>
#include "xtensor/xmath.hpp"
#include "xtensor-r/rarray.hpp"
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::plugins(cpp14)]]
@thomasp85
thomasp85 / trim_model.R
Created October 24, 2017 07:26
Trim all unnecessary data from model objects
library(future)
trim_model <- function(model, predictor = predict, ..., ignore_warnings = TRUE) {
# Cache the correct output
true_pred <- predictor(model, ...)
# Treat prediction warnings as errors?
if (!ignore_warnings) {
old_ops <- options(warn = 2)
on.exit(options(old_ops))
}
library(rstanarm); library(tidyverse)
options(mc.cores = parallel::detectCores())
set.seed(42)
data("radon")
head(treatment_sample)
# Some levels have no variance in the outcomes, making likelihood estimates impossible
# Adding a tiny bit of noise fixes the problem
radon$log_uranium <- rnorm(nrow(radon), radon$log_uranium, 0.05)