Skip to content

Instantly share code, notes, and snippets.

@johnmyleswhite
johnmyleswhite / costly_bigints.jl
Last active December 18, 2015 11:09
Int's vs. BigInt's
function foo(n::Integer)
x::Int = 0
t1::Float64 = @elapsed for i in 1:n
x = x + 1
end
@printf "Int: %f\n" t1
y::BigInt = BigInt(0)
t2::Float64 = @elapsed for i in 1:n
y = y + BigInt(1)
@johnmyleswhite
johnmyleswhite / fdist_entropy.jl
Created June 17, 2013 17:19
The entropy of the F distribution
using Distributions
import Distributions.entropy
function entropy(d::FDist)
a, b = d.ndf, d.ddf
a2, b2 = a / 2.0, b / 2.0
return log(a / b) + lbeta(a2, b2) +
(1.0 - a2) * digamma(a2) -
(1.0 + b2) * digamma(b2) +
(a2 + b2) * digamma(a2 + b2)
@johnmyleswhite
johnmyleswhite / validation.jl
Created June 18, 2013 21:04
Int and Float validation tools
function isint64(s::String)
for chr in s
if chr == 'e' || chr == '.'
return false
end
end
return true
end
@johnmyleswhite
johnmyleswhite / stable.jl
Created July 20, 2013 15:06
Stable distribution sampler in Julia -- forced to have scale of 1 instead of root 2.
# Utilities needed in rand()
# (exp(x) - 1) / x
function d2(z::Real)
const p1 = 0.840066852536483239e3
const p2 = 0.200011141589964569e2
const q1 = 0.168013370507926648e4
const q2 = 0.18013370407390023e3
const q3 = 1.0
@johnmyleswhite
johnmyleswhite / qqplot.jl
Created July 31, 2013 01:28
QQ-plots in Julia
using Stats, Distributions, Vega
Vega.plot(qq::QQPair) = Vega.plot(x = qq.qx, y = qq.qy, kind = :scatter)
qqplot(x::Vector, y::Vector) = plot(qqbuild(x, y))
qqplot(x::Vector, d::UnivariateDistribution) = plot(qqbuild(x, d))
qqplot(d::UnivariateDistribution, x::Vector) = plot(qqbuild(d, x))
x = rand(Normal(), 10_000)
y = rand(Cauchy(), 10_000)
@johnmyleswhite
johnmyleswhite / qqplot.jl
Created July 31, 2013 01:30
qq-plots in Julia
using Stats, Distributions, Vega
Vega.plot(qq::QQPair) = Vega.plot(x = qq.qx, y = qq.qy, kind = :scatter)
qqplot(x::Vector, y::Vector) = plot(qqbuild(x, y))
qqplot(x::Vector, d::UnivariateDistribution) = plot(qqbuild(x, d))
qqplot(d::UnivariateDistribution, x::Vector) = plot(qqbuild(d, x))
x = rand(Normal(), 10_000)
y = rand(Cauchy(), 10_000)
@johnmyleswhite
johnmyleswhite / holomorphic.jl
Last active December 20, 2015 18:59
Probabilistically guessing whether a complex function is holomorphic
# f(z = x + iy) = u(x, y) + i * v(x, y)
function finite_difference{T <: Complex}(f::Function,
z::T,
alongx::Bool = true,
alongu::Bool = true)
epsilon = sqrt(eps(max(abs(one(T)), abs(z))))
if alongx
zplusdz = z + epsilon
else
zplusdz = z + epsilon * im
@johnmyleswhite
johnmyleswhite / type_inference.jl
Created August 9, 2013 17:57
Julia's type inference is where you get performance
# Always ignore one run
x = 0
@elapsed for i in 1:100_000_000
x += 1
end # => 3.330939854
# Second time is more accurate
@johnmyleswhite
johnmyleswhite / porter.jl
Created September 6, 2013 16:07
Porter stemmer translation (IN PROGRESS)
# step1ab!() gets rid of plurals and -ed or -ing. e.g.
#
# caresses -> caress
# ponies -> poni
# ties -> ti
# caress -> caress
# cats -> cat
#
# feed -> feed
# agreed -> agree
@johnmyleswhite
johnmyleswhite / gist:6641768
Created September 20, 2013 18:30
R Semantics: Not quite Stefan's example
f <- function(ex) {}
f(print("Goodbye, cruel R argument passing semantics"))
g <- function(ex) {ex}
g(print("Hello, cruel R argument semantics"))