This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> import java.util.concurrent.TimeUnit | |
import java.util.concurrent.TimeUnit | |
scala> val units = List((TimeUnit.DAYS,"days"),(TimeUnit.HOURS,"hours"), (TimeUnit.MINUTES,"minutes"), (TimeUnit.SECONDS,"seconds")) | |
units: List[(java.util.concurrent.TimeUnit, String)] = List((DAYS,days), (HOURS,hours), (MINUTES,minutes), (SECONDS,seconds)) | |
scala> def humanReadable(timediff:Long):String = { | |
| val init = ("", timediff) | |
| units.foldLeft(init){ case (acc,next) => | |
| val (human, rest) = acc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ABTest extends App{ | |
case class Rate(successes:Int, failures:Int) { | |
override def toString = "[Successes: %d, Failures: %d]".format(successes, failures) | |
} | |
def BbeatsA(a:Rate, b:Rate) = { | |
val mu = Beta(1 + a.successes, 1 + a.failures).mean - Beta(1 + a.failures, 1 + b.failures).mean | |
val sigma = math.pow(Beta(1 + a.successes, 1 + a.failures).variance + Beta(1 + b.successes, 1 + b.failures).variance, 0.5) | |
100.0 * Gaussian.cdf(0, mu, sigma) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rm(list=ls()) | |
library(MASS) | |
mu <- c(1,0) | |
Sigma <- matrix(c(1,0.5,0.5,1),2,2) | |
n<- 1000 | |
sumc <- c() | |
for(times in 1:1000) { | |
x<- mvrnorm(n=n,mu,Sigma) | |
sum<-0 | |
for(i in 1:(n-1)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.twitter.scalding._ | |
import cern.colt.matrix.{DoubleFactory2D, DoubleFactory1D } | |
import cern.colt.matrix.linalg.Algebra | |
import java.util.StringTokenizer | |
class Portfolios(args : Args) extends Job(args) { | |
val cash = 1000.0 // money at hand | |
val error = 1 // its ok if we cannot invest the last dollar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tsum <- function(n, myn, mydf, myncp) { | |
mylist <- c() | |
for (i in 1:n) { | |
samp <- rt(n=myn, df = mydf, ncp=myncp) | |
mylist<- c(mylist,sum(samp) - min(samp)) | |
} | |
return(mylist) | |
} | |
x<- tsum(1000, 250, 3, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
distribution data => distribution name, params supervised ANN classifier | |
eg. | |
Poisson(5) data 1000 samples => ("poisson", 5) | |
Uniform(10) data 1000 samples => ("uniform", 10) | |
Normal(mu, sig) data 1000 samples => ("normal", mu, sig) | |
etc. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val data = List((1,2),(2,3),(3,4),(4,5),(5,7)) | |
def line(x:Int) = x+1 // guess based on data | |
def mean(xy:List[(Int,Int)]) = xy.map(a=>a._2).sum/(0.0+xy.size) // average the y's | |
def sstot(xy:List[(Int,Int)]) = { val mu = mean(data); xy.map(a=>(a._2-mu)*(a._2-mu)).sum } // total sum of squares | |
def sserr(xy:List[(Int,Int)]) = { xy.map(a=>(a._2-line(a._1))*(a._2-line(a._1))).sum } // sum of squares of residuals | |
def rsq(xy:List[(Int,Int)]) = 1.0 - sserr(xy)/sstot(xy) | |
scala> rsq(data) | |
res5: Double = 0.9324324324324325 // 93% fit, not bad for a guess. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from lmfit import Minimizer, Parameters, report_fit | |
# create data to be fitted | |
x = np.linspace(0, 15, 301) | |
data = 2*x*x+ 3*x+4 | |
# define objective function: returns the array to be minimized |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# maximize abc subject to a + b + c = 10 | |
import numpy as np | |
import tensorflow as tf | |
tf.reset_default_graph() | |
abc = tf.get_variable("abc",shape=(3,1),dtype=tf.float32, initializer=tf.ones_initializer) | |
optimizer = tf.train.GradientDescentOptimizer(0.0001) | |
# grab a, b, c and the lambda l |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exiting paste mode, now interpreting. | |
id = 8, isLeaf = true, predict = 0.0 (prob = -1.0), impurity = 0.0, split = None, stats = None | |
id = 9, isLeaf = true, predict = 1.4736842105263157 (prob = -1.0), impurity = 0.2493074792243767, split = None, stats = None | |
id = 10, isLeaf = true, predict = 3.0 (prob = -1.0), impurity = 0.16666666666666666, split = None, stats = None | |
id = 11, isLeaf = true, predict = 4.1 (prob = -1.0), impurity = 0.09000000000000057, split = None, stats = None | |
id = 12, isLeaf = true, predict = 5.0 (prob = -1.0), impurity = 0.0, split = None, stats = None | |
id = 13, isLeaf = true, predict = 6.444444444444445 (prob = -1.0), impurity = 0.2469135802469143, split = None, stats = None | |
id = 14, isLeaf = true, predict = 7.923076923076923 (prob = -1.0), impurity = 0.2248520710059158, split = None, stats = None | |
id = 15, isLeaf = true, predict = 9.0 (prob = -1.0), impurity = 0.0, split = None, stats = None |