Skip to content

Instantly share code, notes, and snippets.

View Sleepingwell's full-sized avatar

Simon Knapp Sleepingwell

  • Canberra, ACT, Australia
View GitHub Profile
@Sleepingwell
Sleepingwell / gist:5076345
Created March 3, 2013 14:38
A comparison of some hypothesis tests for a specific two sample problem (for a forum).
pre <- scan()
51.11
64.44
73.33
77.78
66.67
84.44
68.89
57.78
64.44
@Sleepingwell
Sleepingwell / funkster.cpp
Last active February 8, 2022 13:44
A very simple example of holding a C++ class in Python.
#include <Python.h>
struct MyClass {
int val;
MyClass(void) : val(42) {}
};
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
@Sleepingwell
Sleepingwell / gist:5843755
Last active December 18, 2015 20:59
A wrapper around Rcartogram::cartogram for shape files.
# A wrapper around Rcartogram::cartogram for shape files.
#
# @param polys: Either a SpatialPolygonsDataFrame or the path to a shape file.
# @param variable: Either a string giving the name of the variable to used in polys@data, or a vector
# of the same length as polys@polygons.
# @param nx: The number of columns in the raster used to represent the region.
# @param ny: The number of rows in the raster used to represent the region.
# @param buffer.by: The proportion width of the buffer to add to the region (see ?Rcartogram::addBoundary).
#
# @return The new boundaries as A SpatialPolygonsDataFrame with the attribute 'cartogram.scaling.var'.
@Sleepingwell
Sleepingwell / rng_tests.R
Last active December 19, 2015 23:39
Comparison of performance (speed) or R's Gausian random number generators.
test.func <- function(n.samples) {
nms <- c("Wichmann-Hill", "Marsaglia-Multicarry", "Super-Duper", "Mersenne-Twister", "Knuth-TAOCP-2002", "Knuth-TAOCP", "L'Ecuyer-CMRG")
tmp <- lapply(nms, function(unif.rng.name) {
RNGkind(unif.rng.name)
nms <- c("Kinderman-Ramage", "Ahrens-Dieter", "Box-Muller", "Inversion")
tmp <- sapply(nms, function(norm.rng.name) {
RNGkind(, norm.rng.name)
system.time(rnorm(n.samples))
})[1:3,]
# included here because I'm not sure if rziggurat uses the unif rng internally.
@Sleepingwell
Sleepingwell / ca_rng.cpp
Last active December 19, 2015 23:49
A cellular automata random uniform number generator (with R interface).
/*
Copyright (C) 2006 Anthony M. Pasqualoni
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@Sleepingwell
Sleepingwell / gist:6044051
Created July 20, 2013 06:15
Using Pi as a (very slow) random number generator.
/*
This program implements the BBP algorithm to generate a few hexadecimal
digits beginning immediately after a given position id, or in other words
beginning at position id + 1. On most systems using IEEE 64-bit floating-
point arithmetic, this code works correctly so long as d is less than
approximately 1.18 x 10^7. If 80-bit arithmetic can be employed, this limit
is significantly higher. Whatever arithmetic is used, results for a given
position id can be checked by repeating with id-1 or id+1, and verifying
that the hex digits perfectly overlap with an offset of one, except possibly
for a few trailing digits. The resulting fractions are typically accurate
@Sleepingwell
Sleepingwell / move_or_copy.py
Last active January 29, 2024 12:36
An example of how to copy or move a bunch of files matching a specific regex with python.
# Copyright (C) 2013 Simon Knapp
#
# This program is free software; you can redistribute it and/or
# modify it under the any terms you wish.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# To run this, type:
@Sleepingwell
Sleepingwell / gist:6418936
Last active December 22, 2015 04:39
An example of using showdown to parse markdown in a web page + syntax highlighting.
<html>
<!-- based example found at: http://blog.harakys.com/blog/2012/02/21/embed-markdown-into-your-html/ -->
<header>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
<script src="https://google-code-prettify.googlecode.com/svn/loader/prettify.js"></script>
<script src="http://www.showdown.im/showdown/example/showdown.js"> </script>
<script type="text/javascript">
var generateHTML = function() {
@Sleepingwell
Sleepingwell / gist:6604496
Created September 18, 2013 04:18
A little mechanism for setting options for an R package.
opt.env <- new.env()
setOption <- function(auth_token, remaining_limit) {
cl <- as.list(match.call()[-1])
invisible(mapply(assign, names(cl), cl, MoreArgs=list(envir=opt.env)))
}
getOption <- function(opt) if(exists(opt, envir=opt.env)) get(opt, envir=opt.env) else NA
#---- test ---
@Sleepingwell
Sleepingwell / gist:6726433
Created September 27, 2013 10:03
How to merge a list of data frames in R
# dummy data
maxN <- 10
listLen <- 6
Ns <- sample(as.integer(maxN/2):maxN, listLen)
dat <- lapply(Ns, function(N) data.frame(country=sample(maxN, N), var=rnorm(N)))
# function that (recursively) does the merging
merger <- function(lst, var.name) if(length(lst) == 1) lst else merge(lst[[1]], merger(lst[-1], var.name), by=var.name, all=T)
# do the merge and put sensible names on the result, and fix the NAs (set to zero)