Skip to content

Instantly share code, notes, and snippets.

@vankesteren
vankesteren / rtrunc.R
Created December 9, 2020 15:46
Truncated sampling from any distribution in R (also works with pipes!)
rtrunc <- function(rdist, min, max) {
# deal with pipe and also with normal evaluation
# https://github.com/tidyverse/magrittr/issues/115#issuecomment-173894787
parents <- lapply(sys.frames(), parent.env)
is_magrittr_env <- vapply(parents, identical, logical(1), y = environment(`%>%`))
if (any(is_magrittr_env)) {
distcall <- get("lhs", sys.frames()[[base::max(which(is_magrittr_env))]])
} else {
distcall <- substitute(rdist)
}
@vankesteren
vankesteren / conditional_idw.R
Created November 10, 2020 15:53
Conditional inverse distance weighted smoothing
library(tidyverse)
library(sf)
library(osmdata)
library(osmenrich)
library(patchwork)
# conditional IDW functions
conditional_idw <- function(formula, data, p = 1, max.iter = 100, tol = 1e-10) {
@vankesteren
vankesteren / bag_wfs.R
Created November 6, 2020 12:12
bag wfs query verblijfsobjecten
library(sf)
library(httr)
library(tidyverse)
url <- parse_url("https://geodata.nationaalgeoregister.nl/bag/wfs/v1_1")
postcode_filter <- function(postcode, gebruiksdoel = "woonfunctie") {
sprintf(
"<Filter>
@vankesteren
vankesteren / triangulation.R
Created March 26, 2020 09:23
Delaunay triangulation and voronoi tesselation
library(deldir)
library(ggplot2)
library(firatheme)
N <- 40
dat <- matrix(rnorm(N*2), N)
dat <- dat %*% chol(matrix(c(1, 0, 0, 1.5), 2)) %*% chol(solve(cov(dat)))
dat2 <- deldir(dat[,1], dat[,2])
dat1 <- data.frame(dat)
colnames(dat1) <- c("x1", "y1")
@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"
)
@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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@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
@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 / mscomputer.md
Last active October 14, 2024 08:52
Documentation for using the Methods & Statistics department compute server