Skip to content

Instantly share code, notes, and snippets.

@krishnanraman
krishnanraman / dow.scala
Last active January 30, 2018 00:57
Render a timeseries jpg using JFreeChart
import org.jfree.chart.{ChartFactory, ChartUtilities}
import org.jfree.data.time.{Day, TimeSeries, TimeSeriesCollection}
import scala.io.Source
import java.io.File
val dow = new TimeSeries("dow")
Source.fromFile("./DJI.csv").getLines.toStream.tail.foreach{
x:String =>
val arr = x.split(",")
val day = Day.parseDay(arr(0))
@krishnanraman
krishnanraman / testmultiproc.py
Created December 13, 2017 01:43
multi processing with python
import multiprocessing as mp
import numpy as np
import sys
def printf(format, *args):
sys.stdout.write(format % args)
def f(x):
sum = 0
for i in np.arange(x, 2*x,1):
@krishnanraman
krishnanraman / TalebDart.scala
Last active November 3, 2017 23:43
taleb dart problem
import util.Random
object TalebDart extends App {
case class Dart(x:Double, y:Double)
case class Box(minx:Double, miny:Double) {
var count = 0
def inBox(dart:Dart):Boolean = {
val isin = dart.x >= minx && dart.x <= minx + 0.25 &&
@krishnanraman
krishnanraman / factorial.cpp
Last active September 11, 2017 21:56
simple c++ example
#include "factorial.h"
int factorial(int x) {
if (x==1)
return 1;
else
return x * factorial(x-1);
}
@krishnanraman
krishnanraman / ucb1.scala
Created August 24, 2017 22:05
ucb1 algo.
object ucb1 extends App {
printf("Min Bid:")
val min = readDouble
printf("Max Bid:")
val max = readDouble
printf("Number of arms:")
val arms = readInt
printf("Trials (say 1000):")
val trials = readInt
@krishnanraman
krishnanraman / variableBid.scala
Created August 23, 2017 23:35
Using the epsilon greedy algorithm to figure out the true optimal bid
object variableBid extends App {
printf("Min Bid:")
val min = readDouble
printf("Max Bid:")
val max = readDouble
printf("Number of arms:")
val arms = readInt
printf("Trials (say 1000):")
val trials = readInt
printf("Epsilon (say 0.01):")
@krishnanraman
krishnanraman / epsilongreedy.scala
Last active August 22, 2017 00:53
Epsilon Greedy explore-exploit strategy. Always beats random!
object epsilongreedy extends App {
// cmd-line inits
print("Run 10,000 trials with Arm Probabilities (eg. 0.3,0.7): ")
val s = readLine
val armprob:List[Double] = s.split(",").toList.map{ x => x.toDouble }
val arms = armprob.size
val trials = 10000
val epsAll = List(0.01,0.1,0.5,0.9,0.99).map{ eps => Eps(eps, arms)}
val bestArm:Int = armprob.zipWithIndex.maxBy{ x=> x._1}._2
@krishnanraman
krishnanraman / stitchfix.scala
Created July 12, 2017 20:12
stitchfix coding exercise
/*
Given an unsorted array/list of integers, write a function that:
- returns True iff there are duplicate integers in the list
- returns False otherwise
[-12, 32, 56, -2] -> False
[32, 23, 23, 1, 5 ] -> True
*/
// To execute Scala code, please define an object named Solution that extends App
@krishnanraman
krishnanraman / selu.py
Last active July 7, 2017 00:27
Simpler SELU on the unit interval, using linear regression
# This is selu, the best ever breakthrough activation function since sliced bread
import numpy as np
def selu(x):
alpha = 1.67326
scale = 1.0507
return scale*np.where(x>=0.0, x, alpha*np.exp(x) - alpha)
# Plot it in matplot, stare at it for a while
# I figure 3 straight lines can do a decent job of interpolating it on unit interval
from scipy import stats
@krishnanraman
krishnanraman / foo.scala
Last active June 26, 2017 00:33
AL qualifier
object foo extends App {
case class C(n:String, id:Int, pid:Int)
val l = List(C("mh", 3,6), C("sf", 1,0), C("ny", 6,0), C("ba",2,1))
def printChildren(pid:Int, l:List[C]):Unit =
l
.filter{ x=> x.pid == pid}
.sortBy{ x=> x.n}
.foreach {x => println(x.n); printChildren(x.id, l)}
printChildren(0, l)
}