Skip to content

Instantly share code, notes, and snippets.

%% Read in files and extrapolate mortality rate
x = csv.read('life.csv','format', '%f %f');
age = x{1};
prob = x{2};
model = stats.lm(log(prob(61:end)), log(age(61:100)));
pfun = @(a) util.if_(a<60, @()prob(a), util.if_(a>121, 1, exp(model.beta(1) + model.beta(2) * log(a))));
@chris-taylor
chris-taylor / IOAction.hs
Last active September 28, 2020 12:32
Code for my blog post about pure I/O
data IOAction a = Return a
| Put String (IOAction a)
| Get (String -> IOAction a)
get = Get Return
put s = Put s (Return ())
seqio :: IOAction a -> (a -> IOAction b) -> IOAction b
seqio (Return a) f = f a
seqio (Put s io) f = Put s (seqio io f)
@chris-taylor
chris-taylor / Dual.m
Created March 9, 2012 10:19
Matlab dual number class (for automatic differentiation)
classdef Dual
% DUAL Dual numbers class for automatic differentiation.
%
% To do automatic differentiation we introduce a number d such that d^2 == 0.
% Then by a simple application of Taylor's theorem we have
%
% f(x + d) = f(x) + f'(x) d
%
% with all higher-order terms vanishing. Therefore applying a function to a dual
% number x + d generates both the value f(x) *and* the derivative f'(x).
module AI.Search.Examples.Graph where
import Control.Monad
import Control.Monad.ST
import Control.Applicative
import Data.STRef
import Data.Map (Map, (!))
import qualified Data.Map as Map
import Data.List (nub)
import Data.Graph.Inductive (LNode, LEdge, Gr)
@chris-taylor
chris-taylor / search.matlab
Created April 30, 2013 09:18
Download images from Google image search
function search(query,path)
% Download full size images from Google image search. Saves image files
% locally. Do not print or republish images without permission.
%
% Based on Python code by Craig Quiter at https://gist.github.com/crizCraig/2816295
%
% Requires the JSONLab package, available from
% http://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab-a-toolbox-to-encodedecode-json-files-in-matlaboctave
%
% USAGE
@chris-taylor
chris-taylor / golfsort.hs
Last active January 4, 2016 19:39
Golf'd mergesort
sortBy c=m.map(:[]) -- O(n log n)
where m[]=[];m[x]=x;m x=m(n x);n[]=[];n[x]=[x];n(x:y:z)=p x y:n z
p[]y=y;p x[]=x;p (w:x)(y:z)=case c w y of GT->y:p(w:x)z;_->w:p x(y:z)
sort x=m(map(:[])x)
where m[]=[];m[x]=x;m x=m(n x);n[]=[];n[x]=[x];n(x:y:z)=p x y:n z
p[]y=y;p x[]=x;p (w:x)(y:z)=if w>y then y:p(w:x)z else w:p x(y:z)
@chris-taylor
chris-taylor / wordgame.hs
Last active January 2, 2016 19:09
Given two words *from* and *to*, transform *from* into *to* by either adding, removing or altering one letter at a time, so that each step in the transformation is a valid English word.
import Control.Applicative
import qualified Data.Set as Set
import AI.Search.Uninformed
step :: Set.Set String -> String -> [] String
step wordlist xs = Set.toList . Set.fromList . filter f $ remove1 xs ++ swap1 xs ++ add1 xs
where
f w = Set.member w wordlist
len = length xs
letters = ['a'..'z']
@chris-taylor
chris-taylor / dicegame.hs
Created January 9, 2014 23:49
You roll a dice up to three times. After each of the first two rolls you can choose to accept the number showing on the dice in dollars, or roll again. What is your expected winnings if you play optimally?
import Control.Probability
expected p = expectation (runProb p)
die = uniform [1..6] :: Bayes Double Integer
game n
| n == 0 = die
| otherwise = do
x <- die
@chris-taylor
chris-taylor / FieldExtension.hs
Created December 23, 2013 17:19
Simple field extensions in Haskell
module FieldExtension where
{-
This module lets you define the exact Fibonacci function using the mathematically
elegant definition in terms of phi = (1 + sqrt 5) / 2, without losing precision
due to calculations being carried out in floating point.
phi :: Q5
phi = 0.5 :+ 0.5
@chris-taylor
chris-taylor / gist:5868299
Last active December 19, 2015 00:20
Testing GFM

Heading 1

Some Q code

fib: {[n]
  $[n<2; n; fib[n-1]+fib[n-2]]
}