View arrange_plot.R
library(tidyverse)
mtcars %>%
mutate(car = rownames(.)) %>%
arrange(hp) %>%
ggplot(aes(x = car, y = hp)) +
geom_point() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
View mc.R
# simulating https://en.wikipedia.org/wiki/Markov_chain#/media/File:Markovkate_01.svg
library(ggplot2)
means = c()
ntimes <- 1000
for (t in 1:ntimes) {
n <- 1000
state <- c(1)
View distance.R
library(dplyr)
n <- 1000000
data <- data.frame(id = 1:n,
red = sample(0:255, size = n, replace = TRUE),
green = sample(0:255, size = n, replace = TRUE),
blue = sample(255, size = n, replace = TRUE))
query <- list(red = 80, green = 90, blue = 255)
View monty_hall.R
library(dplyr)
library(ggplot2)
doors <- 1:3
sample_doors <- function() { return(sample(doors, size = 1000, replace = TRUE))}
games <- data.frame(prize = sample_doors(), pick = sample_doors())
games$strategy <- factor(ifelse(games$prize == games$pick, 'stay', 'switch'))
monte_show <- function(prize, pick) {
View clustering.R
library(ggplot2)
cars <- mtcars
cars$cyl <- factor(cars$cyl, labels =
c('Four cylinder', 'Six cylinder', 'Eight cylinder'))
features <- c('wt', 'qsec')
n_clusters <- 3
car_clusters <- kmeans(cars[, features], n_clusters, nstart = 30)
View resize.py
import numpy as np
a = np.arange(1000)
a = a.reshape(2, 500)
a = a.resize((2,600), refcheck = False)
View sin_taylor.py
import numpy as np
from math import factorial
def sin(x):
val = np.float64(0)
for n in range(0, 25):
term = (((-1) ** n) * (x ** (2*n+1)) / factorial(2*n + 1))
val += term
print("for x_{0}, term is {1}, sin({2}) approximation is {3}".format(
n, term, x, val))
View install-pymc-homebrew-os-x.sh
brew install python3
brew install apple-gcc42
brew unlink gcc
ln -sf /usr/local/bin/gfortran-4.2 /usr/local/bin/gfortran
#optional: create virtualenv
python3 -mvenv ~/venvs/my_venv
source ~/venvs/my_venv/bin/activate
#end optional
View quote.py
import re
p = re.compile('\"([^\"]+)\"')
str = 'I said "Hello There." She replied "hi"'
matches = p.findall(str)
for m in matches:
print('found: {0}'.format(m))
View main.cpp
#include <iostream>
#include <iomanip>
#include "math.h"
using namespace std;
float pmf(int k, double lambda) {
// https://en.wikipedia.org/wiki/Poisson_distribution#Definition
return pow(M_E, k * log(lambda) - lambda - lgamma(k + 1.0));
}