Skip to content

Instantly share code, notes, and snippets.

View krishnanraman's full-sized avatar

Krishnan Raman krishnanraman

View GitHub Profile
@krishnanraman
krishnanraman / output.txt
Created February 8, 2018 02:57
Create Spark DataFrame From List
+---+---+
| x| y|
+---+---+
| 1| 0|
| 2| 0|
| 3| 0|
| 4| 0|
| 5| 0|
| 6| 0|
| 7| 0|
// Given 2 points in first quadrant
// Point p1 (a,b)
// Point p2 (c,d)
// a,b,c,d > 0
//
// Find point X(x,0) on x axis
// such that
// ||p1-X|| + ||p2-X|| is smallest
//
>spark-shell
import org.apache.spark.sql.SQLContext
val sql = new SQLContext(sc)
sql.read.format("orc").load("./test.orc").schema.foreach(println)
@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