Skip to content

Instantly share code, notes, and snippets.

View dharasim's full-sized avatar

Daniel Harasim dharasim

View GitHub Profile
@dharasim
dharasim / unzip_in_julia.jl
Last active March 22, 2018 13:25
generic unzip function in julia
"""
unzip(iter)
unzip an iterable object of indexable objects
"""
function unzip(iter)
ntuple(length(first(iter))) do i
map(iter) do indexable
getindex(indexable, i)
end
using StatsFuns.RFunctions: betarand, gammarand
using LogProbs
function categorical_sample(tokens, weights)
x = rand() * sum(weights)
cum_weights = zero(eltype(weights))
for (t, w) in zip(tokens, weights)
cum_weights += w
if cum_weights > x
return t
import Seaborn
import Pandas
# convert julia dataframe into a pandas dataframe
panda(df) = Pandas.DataFrame(Dict(n => df[n] for n in names(df)))
@dharasim
dharasim / fibs.jl
Created August 13, 2018 09:15
Fibs iterator in Julia v1.0
using Base.Iterators: take
import Base: iterate, IteratorSize, eltype
struct Fibs end
iterate(::Fibs, (i, j) = (1, 1)) = i, (j, i+j)
IteratorSize(::Type{Fibs}) = Base.IsInfinite()
eltype(::Type{Fibs}) = Int
# collect the first ten fibs in an array
partial(f, args...) = let args = args; (more...) -> f(args..., more...) end
@dharasim
dharasim / example_plot.jl
Last active December 12, 2018 11:31
VegaLite plots in Julia
df = DataFrame(x=randn(500), y= randn(500))
df |> vlplot(
mark = :point,
encoding = (
x = (field=:x, typ=:quantitative),
y = (field=:y, typ=:quantitative)
)
)
@dharasim
dharasim / CompoundDistributions.jl
Created February 7, 2019 15:28
Compound Distributions in Julia
module CompoundDistributions
using StatsFuns.RFunctions: betarand, gammarand
using SpecialFunctions: lbeta
using LogProbs
# distribution types
export BetaBern, DirMul, DirCat, UniCat, CatDist, ChineseRest
# conditional distribution type
@dharasim
dharasim / index.html
Last active February 19, 2019 18:14
Pitch Class Barplot
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Vega Lite Barplot</title>
<!-- Import Vega 4 & Vega-Lite 3 (does not have to be from CDN) -->
<script src="https://cdn.jsdelivr.net/npm/vega@4"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@3.0.0-rc12"></script>
@dharasim
dharasim / bootstrap.py
Created April 11, 2019 17:17
2-sample bootstrap test in python
def bootstrap_test(higher, lower, n=100000):
xs = higher
ys = lower
m = np.mean(xs) - np.mean(ys)
zs = np.concatenate((xs,ys))
boots = np.random.choice(ys, size=(n,len(zs)))
return np.sum(np.mean(boots[:,0:len(xs)], 1) - np.mean(boots[:,len(xs)+1:], 1) > m)
# test whether the mean of the array `higher` is higher than the mean of the array `lower`
def bootstrap_pvalue(higher, lower, n=100000):
@dharasim
dharasim / parsetree.jl
Created July 31, 2019 14:22
Using parser combinators in Julia https://github.com/wdebeaum/PEG.jl
module TreeFromString
using PEG
import Base: show
struct Tree{T}
val ::T
children ::Vector{Tree{T}}
end