Skip to content

Instantly share code, notes, and snippets.

@johnmyleswhite
johnmyleswhite / non-associative.jl
Last active December 23, 2015 20:49
Floating point arithmetic is not associative, so left-to-right reduce and right-to-left reduce yield different results
a = Float64[]
N = 1_000_000
for i in 1:N
push!(a, 0.1)
end
push!(a, 10.0e8)
@johnmyleswhite
johnmyleswhite / ordered_multiplies.jl
Created September 24, 2013 23:00
Time complexity is not associative: the order of matrix multiplication can change the amount of computation required to perform a sequence of multiplications
function timer(N::Integer)
A = ones(N, N)
B = ones(N, N)
c = ones(N)
@printf "%f\n" @elapsed (A * B) * c
@printf "%f\n" @elapsed A * (B * c)
return
end
@johnmyleswhite
johnmyleswhite / pq.jl
Created September 25, 2013 23:49
Using priority queues in Julia
pq = PriorityQueue{Vector{Float64},Float64}()
enqueue!(pq, [0.0, 110.0], 100.0)
enqueue!(pq, [0.0, 0.0], 10.0)
enqueue!(pq, [0.0, 10.0], 0.0)
enqueue!(pq, [10.0, 10.0], 10.0)
peek(pq)
dequeue!(pq)
@johnmyleswhite
johnmyleswhite / roadmap.md
Created December 15, 2013 18:17
Stats Roadmap

This outlines a roadmap for basic statistical functionality that Julia needs to offer. It is heavily drawn from the table of contents for MASS.

@johnmyleswhite
johnmyleswhite / zany.R
Last active December 31, 2015 15:58
Zany stuff that happens in R 3.0.2 on OS X: some cool, some absolutely awful
M <- matrix(c(1, 0, 0, 1), byrow = 1, nrow = 2)
df <- data.frame(A = 1)
df$B <- list(M)
df
# A B
# 1 1, 0, 0, 1
@johnmyleswhite
johnmyleswhite / slicesampler.jl
Created January 5, 2014 22:54
Multivariate slice sampler
# Code that takes a function,
# a vector,
# x0::Vector{Float64} - Initial point
# x1::Vector{Float64} - Resulting point
# gr::Vector{Float64} - Gradient
# i::Integer - Index of current parameter being updated
# f::Function - Log pdf of x
# Ported from Radford Neal's R code, with a few thinggs missing
@johnmyleswhite
johnmyleswhite / mathutils.jl
Created January 9, 2014 01:47
Some useful math utility functions
function maxdiff(x::Vector, y::Vector)
n = length(x)
if length(y) != n
throw(ArgumentError("Length of x and y must match"))
end
d = 0.0
for i in 1:n
d = max(d, abs(x[i] - y[i]))
end
return d
@johnmyleswhite
johnmyleswhite / identifiers.jl
Last active January 4, 2016 21:29
Code for recognizing and normalizing identifiers in Julia
function isidentifier(s::String)
n = length(s)
if n == 0
return false
end
i = 0
for c in s
i += 1
if i == 1
if !(isalpha(c) || c == '_')
@johnmyleswhite
johnmyleswhite / mean.md
Last active August 29, 2015 13:55
Counterexamples in Statistics

The sample mean is never exactly equal to the true mean when the true mean is irrational

Let D be any distribution over the integers. Suppose that the first moment exists for D and is an irrational number.

In this case, the sample mean is never exactly equal to the true mean because the sample mean is always a rational number.

This is simple to prove: the sample mean is always a sum of integers divided by the number of samples, which is always an integer.

@johnmyleswhite
johnmyleswhite / mutate.R
Created February 3, 2014 23:45
Scope reification in R
foo <- function(frame_number)
{
assign("frame_number", -1, envir = sys.frame(frame_number))
}
bar <- function()
{
frame_number <- sys.nframe()
print(frame_number)
foo(frame_number)