Skip to content

Instantly share code, notes, and snippets.

@einblicker
einblicker / gist:3390476
Created August 19, 2012 00:09
naive bayes
//こちらのプログラムを移植してみた。解説も詳しくて勉強になる。
//http://d.hatena.ne.jp/aidiary/20100613/1276389337
object NaiveBayes {
type Category = String
type Word = String
case class Data(category: Category, words: List[Word])
def train(data: List[Data]) = new NBClassifier(data)
@einblicker
einblicker / HANSEI.fs
Created August 3, 2012 07:50
HANSEI in F#
//HANSEIとはprobabilistic programmingを行うためのtaglessなインタプリタのこと。
//確率モデルをプログラムの形で書くとパラメータの推論を自動で行なってくれる。
//oleg氏の論文だとreify/reflectを使ったdirect styleのモナドで記述されている。
//http://okmij.org/ftp/kakuritu
//要はこちらのスライドで紹介されているものをmonadやHOASを使って内部DSLとして実装したもの。
//http://www.slideshare.net/tsubosaka/infernetlda
//こちらの方の記事も参考になる。
//http://d.hatena.ne.jp/rst76/20100706/1278430517
@einblicker
einblicker / gist:3187433
Created July 27, 2012 11:07
MCMCっぽいやつ
(* 何かがおかしい。要検証。 *)
module MCMC =
let random = new Random()
let select x = x + (random.NextDouble() - 0.5) * 10.0
let prob x = 1.0 / (1.0 + System.Math.Exp(-x))
let nextStatus x =
let xt = select x
let p = prob xt / prob x
@einblicker
einblicker / NeuralNetwork.fs
Created July 26, 2012 14:00
neural network
open System
type Class = Virus | Noise
type Datum = { Image : float[]; Class : Class }
type NeuralNetwork(inputSize, hiddenSize, outputSize, patternSize, maxT, eps, alpha, beta, W0, high, low) =
let mutable eta = 0.005
let inputLayer = Array.init (inputSize+1) (fun _ -> 0.0)
let hiddenLayer = Array.init (hiddenSize+1) (fun _ -> 0.0)
@einblicker
einblicker / ねじれヒープ.fs
Created July 23, 2012 16:42
the fun of programming chap1
type Tree<'T when 'T : comparison> = Null | Fork of 'T * Tree<'T> * Tree<'T>
let isEmpty = function
| Null -> true
| _ -> false
let minElem (Fork(x,_,_)) = x
let rec merge a b =
@einblicker
einblicker / gist:3136147
Created July 18, 2012 13:13
レーベンシュタイン距離
let levenshteinDistance (str1 : string) (str2 : string) =
let d = Array.init (str1.Length+1) (fun _ -> Array.init (str2.Length+1) (fun _ -> 0))
let mutable cost = 0
for i1 = 0 to str1.Length do
d.[i1].[0] <- i1
for i2 = 0 to str2.Length do
d.[0].[i2] <- i2
for i1 = 1 to str1.Length do
for i2 = 1 to str2.Length do
if str1.[i1-1] = str2.[i2-1] then
@einblicker
einblicker / gist:3135433
Created July 18, 2012 10:23
System Fのインタプリタ(未完成)
type ty =
| TyVar of int * int
| TyArr of ty * ty
| TyAll of string * ty
| TySome of string * ty
type binding =
| NameBind
| VarBind of ty
| TyVarBind
@einblicker
einblicker / gist:3117567
Created July 15, 2012 15:57
POJ 3187 Backward Digit Sums
let [| n; sum |] = System.Console.ReadLine().Split([|' '|]) |> Array.map int
let rec permutations list taken =
seq { if Set.count taken = List.length list then yield [] else
for l in list do
if not (Set.contains l taken) then
for perm in permutations list (Set.add l taken) do
yield l::perm }
let step xs = seq {
@einblicker
einblicker / gist:3117471
Created July 15, 2012 15:32
PKU 2718 Smallest Difference
let size = System.Console.ReadLine() |> int
let input = [|
for i = 0 to size - 1 do
yield System.Console.ReadLine().Split([|' '|]) |> Array.map int
|]
let rec permutations list taken =
seq { if Set.count taken = List.length list then yield [] else
for l in list do
if not (Set.contains l taken) then
@einblicker
einblicker / gist:3116215
Created July 15, 2012 10:37
AOJ 0121 Seven Puzzle
module Util =
let toStr x = Array.fold (fun x y -> x + string y) "" x
let fordir (arr : int[]) x f =
for (i, d) in List.mapi (fun a b -> (a, b)) [ 1; -1; 4; -4 ] do
if not(x%4=0 && i=1) && not(x%4=3 && i=0) && x+d<8 && x+d>=0 then
let arr = arr.Clone() :?> int[]
let t = arr.[x]
arr.[x] <- arr.[x + d]
arr.[x + d] <- t
f arr