Skip to content

Instantly share code, notes, and snippets.

@vankesteren
vankesteren / tree_print.R
Last active July 7, 2019 12:07
Pretty print R list structures
# Pretty print list structures with the unicode stuff
# using https://stackoverflow.com/questions/1649027/how-do-i-print-out-a-tree-structure
str2 <- function(x, name = NULL, max.depth = 10L, indent = NULL, depth = 0L, last = TRUE) {
cat(indent)
if (last) {
cat("\u2514\u2500")
indent <- paste0(indent, " ")
} else {
cat("\u251c\u2500")
indent <- paste0(indent, "\u2502 ")
@vankesteren
vankesteren / get_the_correlation.js
Last active December 23, 2020 10:04
Cheat on guessthecorrelation.com
// cheat on guessthecorrelation.com
// don't actually do this to get the high-score. it would be sad.
function get_the_cor() {
// Get points from html
var points = document.getElementsByClassName("nv-group")[0]
// create dataset from transform attribute on the svg points
var transs = Array.from(points.children).map(p => p.getAttribute("transform"))
var data = transs.map(x => x.split(",").map(y => y.match(/[\d\.]+/g)).map(Number))
var tdata = data[0].map((col, i) => data.map(row => row[i]));
@vankesteren
vankesteren / mv-norm-sampling.R
Last active July 18, 2019 12:51
Sampling from multivariate distribution & population analysis
# Covariance matrix and random normal data
Sigma <- matrix(c(1, .5, .5, 1), 2)
X <- matrix(rnorm(1000), ncol = 2)
S <- cov(X)
# random multivariate normal sample
R_mvn <- chol(Sigma)
X_mvn <- X %*% R_mvn
cov(X_mvn)
@vankesteren
vankesteren / all_pdf_to_eps.sh
Last active October 8, 2019 14:18
convert all pdfs in a folder to eps
mkdir eps; for file in $(ls | grep .pdf); do pdf2ps $file eps/${file%.*}.eps; done
@vankesteren
vankesteren / mscomputer.md
Last active October 14, 2024 08:52
Documentation for using the Methods & Statistics department compute server
@vankesteren
vankesteren / sparsecoding.jl
Created November 13, 2019 07:46
Sparse coding for MNIST feature extraction using autodiff
# Sparse coding for MNIST feature extraction using autodiff
using Zygote: gradient
using MLDatasets: MNIST
using LinearAlgebra: Diagonal
using ImageCore
# Goodfellow page 629, equation 19.16, but per pixel
function loss(H::Matrix{Float64}, W::Matrix{Float64})
(sum(abs.(H)) + sum((X - H*W).^2)) / L
end
@vankesteren
vankesteren / Adamopt.jl
Last active June 22, 2024 08:00
Julia implementation of Adam optimizer
module Adamopt
# This is a module implementing vanilla Adam (https://arxiv.org/abs/1412.6980).
export Adam, step!
# Struct containing all necessary info
mutable struct Adam
theta::AbstractArray{Float64} # Parameter array
loss::Function # Loss function
grad::Function # Gradient function
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vankesteren
vankesteren / Bayesian regression.ipynb
Last active November 26, 2019 14:01
Bayesian regression in Julia
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vankesteren
vankesteren / wine_plots.R
Last active April 21, 2021 09:58
Plots for a wine data visualisation
# script that outputs a graph
library(tidyverse)
library(firatheme)
wine <- read_delim("https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data",
delim = ",",
col_names = c(
"Cultivar", "Alcohol", "Malic acid", "Ash", "Alcalinity of ash", "Magnesium",
"Total phenols", "Flavanoids", "Nonflavanoid phenols", "Proanthocyanins",
"Color intensity", "Hue", "OD280/OD315", "Proline"
)