Skip to content

Instantly share code, notes, and snippets.

@Torvaney
Torvaney / simple-rng.R
Last active November 29, 2022 18:48
Create uniform distribution from non-uniform distribution
library(tidyverse)
answers <-
read_csv("https://git.io/fjoZ2") %>%
mutate(rounded = round(pick_a_random_number_from_1_10)) %>%
filter(!is.na(rounded))
modulo_rng <- function(n, data) {
data %>%
@Torvaney
Torvaney / concentration.py
Last active April 4, 2019 20:24
Why did I do this?
'''
How many turns does it take to complete a game of Concentration?
https://en.wikipedia.org/wiki/Concentration_(game)
With perfect memory and strategy, for a game of n pairs, this converges to
(approximately) 1.61n moves.
https://www.jstor.org/stable/10.4169/amer.math.monthly.120.09.787
What about with imperfect memory?
'''
@Torvaney
Torvaney / calendar.R
Last active March 28, 2021 00:03
Make a circular calendar in ggplot2
library(tidyverse)
year <- 2019
start_date <- lubridate::date(str_glue("{year}-01-01")) # there must be a better way??
month_colours <- c(
"January" = "#a5cdff",
"February" = "#c9e1ff",
"March" = "#b2edb6",
@Torvaney
Torvaney / zagreba-metodo.py
Created November 8, 2018 11:43
Download images of la angla zagreba metodo textbook
import os
import urllib.request
import bs4
import progressbar
import requests
URL = 'http://esperantofre.com/zagreb/zagreba.htm'
IMG_DIR = os.path.join(os.path.dirname(__file__), 'zagreba-metodo')
library(tidyverse)
# https://math.stackexchange.com/questions/35791/birthday-problem-expected-number-of-collisions
exp_collisions <- function(n, d) {
n * (1 - (1 - (1 / d)) ^ (n - 1))
}
tibble(n = 1:80) %>%
mutate(collisions = exp_collisions(n, 365)) %>%
ggplot(aes(x = n, y = collisions)) +
library(tidyverse)
library(rvest)
url <- "https://en.wikipedia.org/wiki/List_of_minimum_annual_leave_by_country"
clean_colnames <-
. %>%
str_to_lower() %>%
str_remove_all("\\[.*\\]") %>%
str_remove_all("\\(.*\\)") %>%
suppressPackageStartupMessages({
library(tidyverse)
library(sf)
})
# c.f. http://web.mit.edu/tee/www/bertrand/problem.html
chord_length <- function(theta) {
2 * sin(theta / 2)
}
@Torvaney
Torvaney / classifier-agreement-bounds.R
Created July 20, 2018 12:47
Bounds on classifier error given disagreement rate
lower_bound <- function(d) {
(1 - sqrt(1 - 2*d)) / 2
}
upper_bound <- function(d) {
(1 + sqrt(1 - 2*d)) / 2
}
tibble(d = seq(0, 0.5, 0.001),
lower = lower_bound(d),
to_clipboard <- function(data) {
clip <- pipe("pbcopy", "w")
write.table(data, file = clip, row.names = FALSE)
close(clip)
}
benchmark <-
games %>%
mutate(
outcome = case_when(
hgoals > agoals ~ "home_win",
agoals > hgoals ~ "away_win",
hgoals == agoals ~ "draw"
)
) %>%
count(outcome) %>%