Skip to content

Instantly share code, notes, and snippets.

@jaye-ross
jaye-ross / fibmorse.r
Last active August 29, 2015 14:18
Generate sequences from fibmorse substitution
gen.fibmorse = function(start = c(0), depth = 1){
# check inputs
if(class(start) != "numeric"){
print("Input not a numeric vector!")
stop();
}
for(val in start){
if(!(val %in% c(0,1)) ){
print(paste("Encountered non 0/1 value in vector:",val))
stop()
@jaye-ross
jaye-ross / fskill.r
Last active August 29, 2015 14:18
Function to output forecasting skill. Introduces new type of forecasting skill. Includes unit tests.
mse = function(v1,v2){
se = (v2 - v1)^2
mse = mean(se)
return(mse)
}
# See http://en.wikipedia.org/wiki/Forecast_skill for info on forecasting skill.
# One thing I don't like about the vanilla method is that the domain is (-Inf,1] which is kind of hard to understand.
# The Rossignol method, or Rossignol's Modified Forecasting Skill (RMFS) changes this to be [-1,1] which is easier to understand. Use it by setting type="rossignol".
fskill = function(data,type="vanilla"){
@jaye-ross
jaye-ross / alphap.r
Created April 8, 2015 21:52
Calibrated p-values for testing precise null hypotheses
# Calibrated p-values
# -------------------
# See https://stat.duke.edu/courses/Spring10/sta122/Labs/Lab6.pdf
# and http://www.uv.es/sestio/TechRep/tr14-03.pdf
alpha.p = function(p){
val = 1 + (-exp(1)*p*log(p))^-1
return(val^-1)
}
@jaye-ross
jaye-ross / solvefib.r
Created April 11, 2015 17:38
Determine if given string is generated by fibmorse substitution
prev.value = function(str){
if(str == "01") return("0")
if(str %in% c("0","10")) return("1")
return(NULL)
}
tokenize = function(str,prev.tokens=c("")){
if(str == "") return("");
@jaye-ross
jaye-ross / pvalue_fallacy.r
Created April 13, 2015 00:09
Simulate p-value fallacy
# simulate pvalue fallacy
# See https://stat.duke.edu/courses/Spring10/sta122/Labs/Lab6.pdf
# and https://stat.duke.edu/~berger/applet2/applet.pdf
sim.pval = function(pi = 0.5, theta.input = 1, sigma.input = 1, n = 100, max = 1000){
alpha = 0
beta = 0
while(alpha + beta < max){
rand = runif(1)
if(rand > pi){
theta = 0
@jaye-ross
jaye-ross / bench_cat.r
Created April 19, 2015 19:52
Benchmarking in R: pre-allocated vectors vs vector concatenation
f1 = function(n){
x = double(n)
for(i in 1:n) x[i] = runif(1)
return(x)
}
f2 = function(n){
x = c()
for(i in 1:n) x = c(x,runif(1))
@jaye-ross
jaye-ross / gist:93bc6af266f97b369244
Created April 20, 2015 17:57
Draw arrows on plot
curve(dnorm(x,0,1),-5,5)
curve(dnorm(x,1,1),-5,5,add=TRUE)
arrows(0,0.2,1,0.2)
abline(v=0,lty=2)
abline(v=1,lty=2)
@jaye-ross
jaye-ross / stauf.r
Last active August 29, 2015 14:22
stauf solution in R
## solve stauf painting puzzle
get.move = function(val){
move = c(0,0,0,0,0,0,0,0,0)
if(val == 1) move = c(1,1,0,1,1,0,0,0,0)
if(val == 2) move = c(1,1,1,0,0,0,0,0,0)
if(val == 3) move = c(0,1,1,0,1,1,0,0,0)
if(val == 4) move = c(1,0,0,1,0,0,1,0,0)
if(val == 5) move = c(0,1,0,1,1,1,0,1,0)
if(val == 6) move = c(0,0,1,0,0,1,0,0,1)
if(val == 7) move = c(0,0,0,1,1,0,1,1,0)
@jaye-ross
jaye-ross / info_puzzle.r
Last active August 29, 2015 14:25
Information From Randomness?
# https://www.quantamagazine.org/20150722-solution-information-from-randomness/
n = 1000
vals = runif(n) # values shown by opened hand
g = runif(n) # g values
highest = sample(c(TRUE,FALSE),n,replace=TRUE) # was the value in the opened hand the higher value of the two hands?
# one of two things happens
# 1) g is less than the value you chose, you stay, and you are right
# 2) g is greater than the value you chose, you switched, and you were right
# other cases are failures (add 0 to s) so don't care about them since they are recoverable via n-s
@jaye-ross
jaye-ross / quanta.r
Last active November 13, 2015 23:50
Quanta Magazine Puzzle Formula Tester
f = function(expr){
x = c(13,26,2,4,6,3,9,12,8,10,5,15,18,14,7,21,24,16)
failure = FALSE
for(i in seq_along(x)){
result = eval(expr)
if(length(result) != 1) next
print(sprintf("%s, %s, %s",i,x[i],result))
if(result != x[i]){
failure = TRUE
break