Skip to content

Instantly share code, notes, and snippets.

View lucidjargon's full-sized avatar

lucidjargon

View GitHub Profile
@lucidjargon
lucidjargon / helper functions.fs
Created November 23, 2011 03:59
Simple naive bayes in F#
type Probability = Map<string , Map<string,float> * float>
let mapAddGeneric m a f i =
if m |> Map.containsKey a then
Map.add a (f m.[a]) m
else Map.add a i m
let inline pairAdd (a,b) (c,d) = a + c , b + d
let inline div (a,b) = a / b
@lucidjargon
lucidjargon / minhash.fs
Created March 17, 2012 17:52
Simple minhash in F#
let third (a,b,c) = c
let fst3 (a,b,c) = a
let split op c (data: 'a []) =
data.[1..data.Length - 1] |> Array.fold (fun (bset, curTransform, i) curbit ->
if i = c then
bset |> Set.add (hash curTransform), curbit, 1
else bset, op curTransform curbit, i + 1) (Set.empty , data.[0], 1)
let minhash vset = vset |> Set.minElement
@lucidjargon
lucidjargon / damerauLevenshteinDistance.fs
Created March 22, 2012 07:31
Damerau-LevenshteinDistance algorithm in O(m) Space
//based on http://mwh.geek.nz/2009/04/26/python-damerau-levenshtein-distance/
let damerauLevenshteinDistance (arr1:'a []) (arr2:'a []) =
let wrap j k = if j = k then arr2.Length else j - 1 - k
let rec outer (oneback:int[]) (twoback:int[]) s = function
| i when i = arr1.Length || arr2.Length = 0 -> s
| i -> let thisrow = Array.zeroCreate (arr2.Length+1) in thisrow.[thisrow.Length - 1] <- i + 1
for j in 0..arr2.Length - 1 do
let delcost, addcost, subcost = oneback.[j] + 1, thisrow.[wrap j 0] + 1,
oneback.[wrap j 0] + if arr1.[i] <> arr2.[j] then 1 else 0
@lucidjargon
lucidjargon / distCovar.fs
Created May 26, 2012 23:18
Brownian Distance Covariance as described in http://arxiv.org/pdf/0803.4101.pdf for scalar values. Extensible to vector valued variables with a few character strokes
let foldRow2D, foldCol2D = 1, 0
let inline (@@) (m: 'a [,]) index = Array.Parallel.init (m.GetLength(1)) (fun i -> m.[index, i])
let inline (@@@) (m: 'a [,]) index = Array.Parallel.init (m.GetLength(0)) (fun i -> m.[i, index])
let inline cIndex ind k i (m:'a [,]) = if ind = 1 then m.[k,i] else m.[i,k]
let arr2DFoldAt r k (m : 'a [,]) f seed =
let top = m.GetLength(r)
let rec fold state = function
| i when i = top -> state
| i -> fold (f state (cIndex r k i m)) (i + 1)