Skip to content

Instantly share code, notes, and snippets.

View teguhn's full-sized avatar

Teguh Nugraha teguhn

View GitHub Profile
@teguhn
teguhn / abtest.js
Last active November 22, 2016 15:55
splitting js
//md5 function
!function(n){"use strict";function t(n,t){var r=(65535&n)+(65535&t),e=(n>>16)+(t>>16)+(r>>16);return e<<16|65535&r}function r(n,t){return n<<t|n>>>32-t}function e(n,e,o,u,c,f){return t(r(t(t(e,n),t(u,f)),c),o)}function o(n,t,r,o,u,c,f){return e(t&r|~t&o,n,t,u,c,f)}function u(n,t,r,o,u,c,f){return e(t&o|r&~o,n,t,u,c,f)}function c(n,t,r,o,u,c,f){return e(t^r^o,n,t,u,c,f)}function f(n,t,r,o,u,c,f){return e(r^(t|~o),n,t,u,c,f)}function i(n,r){n[r>>5]|=128<<r%32,n[(r+64>>>9<<4)+14]=r;var e,i,a,h,d,l=1732584193,g=-271733879,v=-1732584194,m=271733878;for(e=0;e<n.length;e+=16)i=l,a=g,h=v,d=m,l=o(l,g,v,m,n[e],7,-680876936),m=o(m,l,g,v,n[e+1],12,-389564586),v=o(v,m,l,g,n[e+2],17,606105819),g=o(g,v,m,l,n[e+3],22,-1044525330),l=o(l,g,v,m,n[e+4],7,-176418897),m=o(m,l,g,v,n[e+5],12,1200080426),v=o(v,m,l,g,n[e+6],17,-1473231341),g=o(g,v,m,l,n[e+7],22,-45705983),l=o(l,g,v,m,n[e+8],7,1770035416),m=o(m,l,g,v,n[e+9],12,-1958414417),v=o(v,m,l,g,n[e+10],17,-42063),g=o(g,v,m,l,n[e+11],22,-1990404162),l=o(l,g,v,m,n[e
function getCol(data, colname) {
var x = [];
for (var i = 0; i < data.length; i++) {
x.push(data[i][colname]);
}
return x;
}
function xy(data, x_col, y_col) {
return {
@teguhn
teguhn / ab_result.R
Last active September 26, 2016 05:54
Function to calculate the winning probability of an binary (control vs 1 variant) experiment. Reference: www.evanmiller.org/bayesian-ab-testing.html
ab.result <- function(control_success, control_failed, variant_success, variant_failed){
growth <- (variant_success/(variant_success+variant_failed))/(control_success/(control_success+control_failed))-1
control_success <- control_success+1
control_failed <- control_failed+1
variant_success <- variant_success+1
variant_failed <- variant_failed+1
winning_prob <- 0
for(i in 0:(variant_success-1)){
winning_prob <- winning_prob + exp(lbeta(control_success+i, variant_failed+control_failed)
- log(variant_failed+i) - lbeta(1+i, variant_failed) - lbeta(control_success, control_failed))
@teguhn
teguhn / iqr.R
Last active December 20, 2016 14:22
#returning vector without extreme/outlier values
rm.extreme <- function(v) {
q3<-quantile(v, .75)
q1<-quantile(v, .25)
iqr <- 1.5 * ( q3- q1)
sapply(v, function(x) {
x[x > (q1 - iqr) & x < (q3 + iqr)]
})
}
@teguhn
teguhn / relfreq.R
Last active November 22, 2016 16:04
relative frequency
# v vector input
# relfreq output data.frame of frquency and relative frequency
relfreq <- function(v){
tab.v <- table(v)
result <- cbind(data.frame(tab.v), rel=tab.v/length(v))
result[,4] <- format(round(result[,4]*100, 2), nsmall = 2)
names(result) <- c("var","freq",NA,"relfreq")
return(result[order(-result$freq),c(1,2,4)])
}