Skip to content

Instantly share code, notes, and snippets.

View broken_render.R
tic(sprintf("Estimation with %d cores:", core_count))
res3 = wrapper_rand(guess3, fun_xb=lklhd_statedep_xb_m, Xs=model_matrices,
choices=choice_matrices, eta=etas, eta_hh=eta_hh_indcs,
NS=NS, v2m=betas_rand_brand_v2m, cores=core_count, trace=1,
toc() # FAILS HERE
# error:
# label: unnamed-chunk-8
jsams / jessica.jl
Created Jan 21, 2019
jessica's slowdown
View jessica.jl
function getprobs(x, a, b)
ea = exp.(x * a)
eb = exp.(x * b)
denom = 1 .+ ea .+ eb
proba = ea ./ denom
probb = eb ./ denom
return proba, probb, 1 .- proba, 1 .- probb
jsams / dsetin.R
Last active Sep 12, 2018
help charlie
View dsetin.R
dt = data.table(i=rep(1:5, each=10), t=rep(1:5, times=2), x=runif(50), key=c('i', 't'))
myfunc = function(x) {
return(data.frame(sum=sum(x), mean=mean(x)))
dt[, myfunc(x), by=.(i, t)]
View stupid_query.jl
function get_tracks_structured(db, user, track_id, author_id, genre, ca, weeks;
random_type::Symbol=:fake, rand_pct=0.20, count_each=2,
nartist=count_each, nartist_repost=count_each,
nseen=count_each, nseen_repost=count_each,
nsameartist=count_each, nsartist_repost=count_each,
if random_type == :fake
do_random = "AND rand01() < $(rand_pct)"
elseif random_type == :sort
jsams / short_circuit.sql
Created May 14, 2018
Way to short circuit logic in SQL(ite)?
View short_circuit.sql
* table plays lists all plays by all users across time
* table affils lists who (fan) follows whom (contact) as of a given point of time
* table reposts lists who reposted what song at what time
* I want to know for each row in plays if the user was following someone who reposted the track
* I also want to know for each row in plays if the user followed someone who was the author of the track
* the author is frequently NULL
I think the below query is correct, but it looks like it realizes the full join of everyone who
may have reposted the track that the listener follows. I am wondering if there is a way to write it
View parallel_iteration.jl
struct ChanWrap
end = take!(d.iter_chan)
const rchan = RemoteChannel(() -> Channel{StructCompData}(args["max_channel_size"]))
@everywhere function produce_data(iter_chan, dbfile, nworkers, batch_size,
niters; kwargs...)
total_iters = batch_size * (niters + 2)
jsams / broken_benchmark.jl
Created May 6, 2018
running with @benchmarks throws an error but runs fine without
View broken_benchmark.jl
function get_all_tracks_info(db, track_id, ca; num=49)
res = vcat(SQLite.query(db, sr"""
SELECT track_id, track_index, author_id, genre, 1 AS listened
FROM tracks
WHERE track_id = ?;""",
SQLite.query(db, sr"""
SELECT track_id, track_index, author_id, genre, 0 AS listened
FROM tracks
WHERE created_at < ? AND track_id <> ?
jsams / gradient_scope_test.jl
Last active May 4, 2018
how to get the gradient wrt to some inputs but not others? scoping?
View gradient_scope_test.jl
using ReverseDiff
using Base.Test
mutable struct data
X::Array{Float64, 2}
const D = data(zeros(Float64, 2, 2))
jsams / intset.jl
Created Apr 16, 2018
benchmark hiding function arguments?
View intset.jl
using BenchmarkTools
function _test(N, prods, pos_prods, maxseen)
z = 0
for i in 1:N
neg = setdiff(prods, pos_prods[i])
z += rand(1:maxseen)
return z
jsams / sd_weirdness.jl
Created Oct 9, 2017
function call different result than inline, also not defined on one branch
View sd_weirdness.jl
function scale_sd!(A::SparseMatrixCSC; corrected=true)
n = size(A, 1)
if corrected
sdcall(v,n,mn) = sqrt(sum(vi^2 for vi in v) / (n - 1) - (n / (n - 1)) * mn^2)
sdcall(v,n,mn) = sqrt(sum(vi^2 for vi in v) / n - mn^2)
for j in 1:size(A, 2)
k = A.colptr[j]:(A.colptr[j+1] - 1)
if length(k) > 0