Skip to content

Instantly share code, notes, and snippets.

View FrankPortman's full-sized avatar
🌈
dreaming

Frank Portman FrankPortman

🌈
dreaming
View GitHub Profile
@FrankPortman
FrankPortman / wtf_python.py
Created November 16, 2017 23:39
python recursion stuff
def closure(max_depth):
def f(depth):
if depth >= max_depth:
return depth
else:
return f(depth + 1)
return f
class CLASS(object):
def __init__(self, max_depth):
;; Project euler 1, sum of all multiples of 3 or 5 under 1000
(reduce +
(filter
(fn [x]
(or (zero? (mod x 3))
(zero? (mod x 5))))
(take 1000 (range))))
compose_right_to_left <- function(funs) function(x) Reduce(function(y, f) f(y), funs, x)
some_composition <- compose_right_to_left(list(cumsum, sqrt, log))
# cumsum(sqrt(log(16:35))), these are all vectorized and return length n output
some_composition(16:35)
# Creates a function that takes in a list of functions and returns a function
# that applies those functions to an input argument 'value'. These functions can `reduce` (sum)
# or apply a potentially vectorized operation (`log`, `sqrt`, etc) in whatever order you want.
@FrankPortman
FrankPortman / reticulate.R
Last active April 20, 2017 22:12
reticulate with pymc
# trying to copy https://pymc-devs.github.io/pymc3/notebooks/getting_started.html
library(reticulate)
pymc <- import("pymc3", convert = FALSE)
py <- import_builtins()
np <- import("numpy")
scipy <- import("scipy")
alpha <- 1L
@FrankPortman
FrankPortman / weighted_random.R
Last active January 23, 2017 19:31
weighted random sampling
library(microbenchmark)
library(Rcpp)
weighted_sample <- function(vec, weights, n) {
cdf <- cumsum(weights / sum(weights))
rand <- runif(n, 0, 1)
sapply(rand, function(x) vec[min(which(cdf >= x))])
@FrankPortman
FrankPortman / multicore-benchmark.R
Last active November 1, 2016 02:02
Benchmark of multicore sampling from posterior in bayesAB
## Benchmark
library(bayesAB)
library(microbenchmark)
library(doMC)
A_binom <- rbinom(100, 1, .5)
B_binom <- rbinom(100, 1, .6)
cores <- 4
registerDoMC(cores)
1 '1 |n feat_1:1.0 feat_11:1.0 feat_17:2.0 feat_22:1.0 feat_24:4.0 feat_25:1.0 feat_26:1.0 feat_29:2.0 feat_35:1.0 feat_40:1.0 feat_42:5.0 feat_48:2.0 feat_54:1.0 feat_57:2.0 feat_60:11.0 feat_62:1.0 feat_63:1.0 feat_65:1.0 feat_67:7.0 feat_71:1.0 feat_79:2.0 feat_80:1.0 feat_85:1.0
1 '2 |n feat_8:1.0 feat_18:2.0 feat_37:1.0 feat_58:1.0 feat_64:1.0 feat_67:1.0 feat_73:2.0 feat_74:1.0 feat_76:1.0 feat_78:1.0
1 '3 |n feat_8:1.0 feat_17:1.0 feat_33:1.0 feat_48:1.0 feat_55:1.0 feat_67:6.0 feat_70:2.0 feat_81:1.0
1 '4 |n feat_1:1.0 feat_4:1.0 feat_5:6.0 feat_6:1.0 feat_7:5.0 feat_10:1.0 feat_11:1.0 feat_13:1.0 feat_16:1.0 feat_17:1.0 feat_24:7.0 feat_25:2.0 feat_26:2.0 feat_30:58.0 feat_32:10.0 feat_38:3.0 feat_44:2.0 feat_46:2.0 feat_48:1.0 feat_49:2.0 feat_50:1.0 feat_51:3.0 feat_54:3.0 feat_55:1.0 feat_65:2.0 feat_66:1.0 feat_67:5.0 feat_70:4.0 feat_73:2.0 feat_74:1.0 feat_76:1.0 feat_79:1.0 feat_80:1.0 feat_81:2.0 feat_82:2.0 feat_84:22.0 feat_86:1.0 feat_87:2.0
1 '5 |n feat_17:4.0 feat_24:1.0 feat_41:1.0
header read result: 16
DUMP [16]: 20 00 00 00 a4 00 00 00 00 00 00 00 00 00 00 00 | ...............
loading buffer (awaiting 164 bytes)
parsing parameters (buf=0x7fc037800000, len=164)
DUMP [164]: 04 08 00 00 2e 61 72 67 31 00 01 01 0a 94 00 00 21 90 00 00 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 00 08 40 00 00 00 00 00 ... |.....arg1.......!..........@.......@.......@.......@.......@.......@.......@.......@.......@.......@.......@.......@.......@.....
PAR[0]: 000000a4 (PAR_LEN=8, PAR_TYPE=4, large=no, c=0x7fc037800000, ptr=0x7fc037800004)
PAR[1]: 000000a4 (PAR_LEN=148, PAR_TYPE=10, large=no, c=0x7fc03780000c, ptr=0x7fc037800010)
CMD=00000020, pars=2
>>CMD_set/assignREXP (.arg1, REXP)
decode: type=33, len=144
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sumApplication
{
class Program
{