Skip to content

Instantly share code, notes, and snippets.

View johnmyleswhite's full-sized avatar

John Myles White johnmyleswhite

View GitHub Profile
@johnmyleswhite
johnmyleswhite / JuliaGlobals
Last active August 10, 2018 10:58
Naively counting Pythagorean triples in Python and Julia
total = 0
N = 300
start_time = time()
for a in 0:(N - 1)
for b in 0:(N - 1)
for c in 0:(N - 1)
if a^2 + b^2 == c^2
total = total + 1
@johnmyleswhite
johnmyleswhite / demo.jl
Last active December 17, 2015 20:19
Estimating the parameters of a Dirichlet in Julia
using Distributions
d = Dirichlet([100.0, 17.0, 31.0, 45.0])
X = rand(d, 1_000_000)
fixed_point(X)
@elapsed alpha = fixed_point(X)
norm(d.alpha - alpha, Inf)
@johnmyleswhite
johnmyleswhite / invdigamma.jl
Created May 31, 2013 01:57
Polygamma Functions: The Trigamma and Invdigamma Functions There seems to be very few implementations of these functions in modern languages that have clear licenses. So I'm making them available here under the MIT license.
# Implementation of fixed point algorithm described in
# "Estimating a Dirichlet distribution" by Thomas P. Minka, 2000
function invdigamma(y::Float64)
# Closed form initial estimates
if y >= -2.22
x_old = exp(y) + 0.5
x_new = x_old
else
x_old = -1.0 / (y - digamma(1.0))
x_new = x_old
@johnmyleswhite
johnmyleswhite / demo.jl
Created May 31, 2013 18:23
Julia Statistical Vocabulary
fit(TDLearner, choices)
fit(HMM, signal)
fit(Lasso, Y ~ X, data, lambda = 1.0)
loglikelihood(model)
AIC(model)
BIC(model)
@johnmyleswhite
johnmyleswhite / probability_simplex.R
Created May 31, 2013 19:39
Generating Random Probability Vectors
# Uniform
a <- runif(10000, 0, 1)
b <- runif(10000, 0, 1)
c <- runif(10000, 0, 1)
df <- cbind(a, b, c)
df <- transform(df, s = a + b + c)
@johnmyleswhite
johnmyleswhite / repeat.jl
Last active December 17, 2015 23:49
Combining Matlab's `repmat` with R's `rep` for tensors of arbitrary order
function repeat{T}(v::Array{T},
dims::Integer...;
repetitions::Integer = 1)
n = length(v)
n_res = n * repetitions * prod(dims)
res = Array(T, tuple(repetitions * n * dims[1], dims[2:end]...)...)
for i in 0:(n_res - 1)
index = mod(fld(i, repetitions), n)
res[i + 1] = v[index + 1]
end
@johnmyleswhite
johnmyleswhite / bfs_dfs.jl
Last active February 3, 2022 19:29
BFS and DFS for DAG's in Julia
function bfs(start_node::Any,
next_nodes::Function,
at_node::Function)
to_process = Array(Any, 0)
depths = Array(Int, 0)
push!(to_process, start_node)
push!(depths, 0)
while !isempty(to_process)
@johnmyleswhite
johnmyleswhite / gmm.jl
Created June 6, 2013 19:33
Gaussian Mixture Models
# K clusters in N-dimensional space
# TODO: Fill out Distribution methods
# TODO: Restore empty cluster pruning
# TODO: Add method for getting point assignments (for all mixtures?)
# TODO: Make custom cont. univ. mixture type?
immutable MixtureMultivariateNormals <: ContinuousMultivariateDistribution
mu::Matrix{Float64}
sigma::Array{Float64}
p::Vector{Float64}
normals::Vector{MultivariateNormal}
@johnmyleswhite
johnmyleswhite / betainc.jl
Created June 8, 2013 03:19
Draft of the incomplete beta function
function betainc(x, pin, qin)
# Are these appropriate?
eps1 = eps(0.5)
alneps = log(eps1)
sml = eps(0.0)
alnsml = log(sml)
if x < 0.0 || x > 1.0
error("x must lie in (0, 1)")
end
@johnmyleswhite
johnmyleswhite / primearray.jl
Created June 9, 2013 22:40
PrimeArray: Using arbitrary precision integers to define arrays
type PrimeArray
n::BigInt
end
PrimeArray() = PrimeArray(BigInt(1))
function Base.getindex(a::PrimeArray, i::Integer)
if i < 0 || i > length(Base.PRIMES)
throw(BoundsError())
end