Skip to content

Instantly share code, notes, and snippets.

View actsasgeek's full-sized avatar

Stephyn Butcher actsasgeek

View GitHub Profile
@actsasgeek
actsasgeek / poincare_bread.R
Created August 30, 2013 01:52
Poincare's Bread Supposedly, Poincaré   suspected that his local bakery was selling loaves of bread that were lighter than the advertised weight of 1 kg, so every day for a year he bought a loaf of bread, brought it home and weighed it. At the end of the year, he plotted the distribution of his measurements and showed that it fit a normal distri…
pick_heaviest_loaf <- function( loaves=100) {
bread <- rnorm( loaves, 950, 50)
return( max( bread))
}
loaves_over_a_year <- function( loaves=100) {
days <- 365
poincare_loaves <- vector()
for (i in 1:days ) {
heaviest_loaf <- pick_heaviest_loaf(loaves)
@actsasgeek
actsasgeek / monty_hall.R
Created August 30, 2013 01:47
R implementation of the Monty Hall Problem.
evaluate_a_monty_hall_scenario <- function(switch=FALSE) {
options <- c(1,2,3)
car <- sample( 1:3, 1)
pick <- sample( 1:3, 1)
opened <- options[!(options %in% c( car, pick))]
closed <- options[!(options %in% c( pick, opened[ 1]))]
if (switch == TRUE) { pick = closed[ 1]}
return( car == pick)
}
@actsasgeek
actsasgeek / monty_hall.py
Created August 29, 2013 02:09
Monty Hall problem in Python.
from random import randint
def evaluate_a_monty_hall_scenario(switch=False):
options = set([1, 2, 3])
car = randint( 1, 3)
pick = randint( 1, 3)
opened = list( options.difference( set([car])).difference( set([pick])))[0]
closed = list( options.difference( set([pick])).difference( set([opened])))[0]
if switch:
pick = closed
(def sales-data [
{:person_id 1
:deal_id 2
:date "2013-01-04"}
{:person_id 1
:deal_id 3
:date "2013-01-05"}
{:person_id 1
:deal_id 4
:date "2013-01-05"}
@actsasgeek
actsasgeek / line.rb
Created July 20, 2012 21:11
ruby implementation of gradient descent linear regression
require 'generator'
samples = [
{ :xs => [ 1.0, 0.25], :y => 0.98},
{ :xs => [ 1.0, 0.49], :y => 0.82},
{ :xs => [ 1.0, 0.60], :y => 0.41},
{ :xs => [ 1.0, 0.89], :y => 0.31}
]
# line is the sum of the dot product of the weight (thetas)
@actsasgeek
actsasgeek / osx.clj
Created May 17, 2012 17:10
A thin Clojure wrapper for com.apple.eawt.Application(Listener)
(ns osx
(:use seesaw.core)
(:import
[com.apple.eawt Application ApplicationListener]
[java.awt.image BufferedImage]))
;; mostly for use with Seesaw
;; https://github.com/daveray/seesaw
(defn event-not-handled [e] (.setHandled e false))
@actsasgeek
actsasgeek / mars_rovers_threaded.clj
Created February 16, 2012 01:34
Threaded Mars Rovers in Clojure
(ns codelesson.mars-rovers-threaded)
;; Mars Rovers
;; Steve Butcher ([email protected])
;; Code Lesson - Week 3
;;
;; Utility functions
;; This function will take a map and switch the keys and values
;; The input map should have unique value as well as keys.
@actsasgeek
actsasgeek / banking.clj
Created February 10, 2012 18:12
fanstasy banking assignment with multimethods
(ns banking)
(defn zip [& rest] (apply map vector rest))
(defn rand-in-range [x y]
"returns a random int in the range x (inclusive) and y (exclusive)"
(+ x (rand-int (- y x))))
(defn rand-date [start-date end-date]
"generates a random date between the start-date and the end-date. Each should be a string
@actsasgeek
actsasgeek / gist:1655048
Created January 22, 2012 01:52
evaluating a string
(defn get-user-command [game-state]
(let [ command (read-line)
[function & args] (conj (into [] (.split command " ")) "game-state")]
(do
(println function args)
(apply (symbol function) args))))
@actsasgeek
actsasgeek / instance_v03.scala
Created July 8, 2011 17:37
Instance object...scalafied!
object Instance {
def parseCSV( instanceAsCSVString: String): Instance = {
def extractFeatureValues( tokenizedFeatures: List[ String]): List[ Option[Double]] = {
tokenizedFeatures.map { token =>
try {
Some( token.toDouble)
} catch {
case nfe: NumberFormatException => {
if ( token == "?") {
None