Skip to content

Instantly share code, notes, and snippets.

@chris-taylor
chris-taylor / t.hs
Created May 1, 2012 14:43
alternative implementation of floating point abs and num
data NewFloat a = F a deriving (Eq,Ord,Show)
instance RealFloat a => Num (NewFloat a) where
F x + F y = F (x + y)
F x - F y = F (x - y)
F x * F y = F (x * y)
fromInteger n = F (fromInteger n)
abs = newAbs
signum = newSignum
@chris-taylor
chris-taylor / trib.hs
Created May 22, 2012 17:26
Finding tribonacci pseudoprimes
-- A semi-fast isPrime algorithm
noDivs factors n = foldr (\f r -> f*f > n || (rem n f /= 0 && r))
True factors
primesTD = 2 : 3 : filter (noDivs $ tail primesTD) [5,7..]
isPrime n = n > 1 && noDivs primesTD n
divides a b = b `mod` a == 0
@chris-taylor
chris-taylor / 2cat.hs
Created May 22, 2012 23:34
Playing around with ways of defining a group as a 2-category in Haskell
{-# LANGUAGE MultiParamTypeClasses #-}
import Data.Maybe
import Data.List as List
-- Group stuff
class Group g where
eye :: g
(%) :: g -> g -> g
@chris-taylor
chris-taylor / evolution.m
Created June 12, 2012 13:02
Relative entropy as applied to an evolutionary competition between two species
function [T Y] = relent(Tspan)
%% Configuration
leg = {'Species 1','Species 2'};
opts = odeset();
opts.AbsTol = 1e-8;
opts.RelTol = 1e-4;
@chris-taylor
chris-taylor / Module1.hs
Created June 13, 2012 13:44
Reader monad example
module Module1 where
import Control.Monad.Reader
data Config = Config { arg :: Int }
initialize :: IO Config
initialize = do
n <- getLine
return $ Config (read n)
@chris-taylor
chris-taylor / unfold.hs
Created June 14, 2012 23:05
Generic unfolds (partial solution)
{-# LANGUAGE ViewPatterns, GADTs, ScopedTypeVariables #-}
-- Functions to fold and unfold. We are using ViewPatterns to make the symmetry between fold and unfold explicity.
foldr2 :: (Either (a,b) () -> b) -> [a] -> b
foldr2 f [] = f $ Right ()
foldr2 f (x:xs) = f $ Left (x, foldr2 f xs)
unfoldr2 :: (b -> Either (a,b) ()) -> b -> [a]
unfoldr2 f (f -> Right () ) = []
@chris-taylor
chris-taylor / simCancer.m
Created June 22, 2012 15:56
Simulation of cancer cells
function [plate cellLocs] = simCancer(sz,pDiv,maxCells)
directions = [1 0; -1 0; 0 1; 0 -1]; % Possible grid directions
plate = zeros(sz); % Initial state of the plate
cellLocs = zeros(sz*sz,2); % (x,y) location of cells
numCells = 1; % Initial number of cells
generation = 1; % Initial generation
@chris-taylor
chris-taylor / Central Limit Theorem Demo
Created September 27, 2012 10:12
centrallimittheorem.octave
%Uniform distribution
x1 = randi(10,1000,1);
[n1 xout1] = hist(x1);
%Sum of uniform distribution
x2 = sum(randi(10,1000,12),2);
m = mean(x2);
s = std(x2);
[n2 xout2] = hist(x2);
@chris-taylor
chris-taylor / US2012ElectionPredictions.txt
Created November 4, 2012 18:00
US 2012 Election predictions
State Winner Confidence
California DEM 100%
Connecticut DEM 100%
District of Columbia DEM 100%
Massachusetts DEM 100%
New York DEM 100%
Vermont DEM 100%
Maryland DEM 100%
Washington DEM 100%
Hawaii DEM 100%
@chris-taylor
chris-taylor / em.matlab
Created December 5, 2012 10:07
Expectation Maximization
function em(X,theta)
% Expectation maximization, P coins
%
% X is TxN matrix of coin flip results (1 = heads, 0 = tails)
% theta is 1xP vector of probabilities (0 < theta < 1)
% Convergence criterion (relative difference)
tol = 1e-6;
% Compute parameters of distribution assigning coins to outputs