type Node =
val DF : double
val Branch : (int * double) []
new (df, branch) = {
DF = df
Branch = branch
}
let induceBackward (nodes : Node []) (values : double []) : double [] =
let n = values.Length / 2
let folder acc (k, p) = acc + p * values.[n + k]
let mapping (node : Node) = node.DF * Array.fold folder 0.0 node.Branch
Array.map mapping nodes
let ITERATION = 1000
let lastValues i = Array.create 201 (double i)
let testTree =
[| for i in 0..99 ->
[| for j in -i..i ->
Node(1.0, [|(j-1, 1.0/6.0); (j, 2.0/3.0); (j+1, 1.0/6.0)|]) |] |]
let value i = (Array.foldBack induceBackward testTree (lastValues i)).[0]
stdout.WriteLine (Array.max (Array.init ITERATION value))
dHlwZSBOb2RlID0gCiAgdmFsIERGIDogZG91YmxlCiAgdmFsIEJyYW5jaCA6IChpbnQgKiBkb3VibGUpIFtdCiAgCiAgbmV3IChkZiwgYnJhbmNoKSA9IHsKICAgIERGID0gZGYKICAgIEJyYW5jaCA9IGJyYW5jaAogICAgfQoKbGV0IGluZHVjZUJhY2t3YXJkIChub2RlcyA6IE5vZGUgW10pICh2YWx1ZXMgOiBkb3VibGUgW10pIDogZG91YmxlIFtdID0KICAgIGxldCBuID0gdmFsdWVzLkxlbmd0aCAvIDIKICAgIGxldCBmb2xkZXIgYWNjIChrLCBwKSA9IGFjYyArIHAgKiB2YWx1ZXMuW24gKyBrXQogICAgbGV0IG1hcHBpbmcgKG5vZGUgOiBOb2RlKSA9IG5vZGUuREYgKiBBcnJheS5mb2xkIGZvbGRlciAwLjAgbm9kZS5CcmFuY2gKICAgIEFycmF5Lm1hcCBtYXBwaW5nIG5vZGVzCgpsZXQgSVRFUkFUSU9OID0gMTAwMAoKbGV0IGxhc3RWYWx1ZXMgaSA9IEFycmF5LmNyZWF0ZSAyMDEgKGRvdWJsZSBpKQpsZXQgdGVzdFRyZWUgPQogICAgW3wgZm9yIGkgaW4gMC4uOTkgLT4KICAgICAgICBbfCBmb3IgaiBpbiAtaS4uaSAtPgogICAgICAgICAgICBOb2RlKDEuMCwgW3woai0xLCAxLjAvNi4wKTsgKGosIDIuMC8zLjApOyAoaisxLCAxLjAvNi4wKXxdKSB8XSB8XQpsZXQgdmFsdWUgaSA9IChBcnJheS5mb2xkQmFjayBpbmR1Y2VCYWNrd2FyZCB0ZXN0VHJlZSAobGFzdFZhbHVlcyBpKSkuWzBdCnN0ZG91dC5Xcml0ZUxpbmUgKEFycmF5Lm1heCAoQXJyYXkuaW5pdCBJVEVSQVRJT04gdmFsdWUpKQ==