MersenneTwister serializer
This commit is contained in:
Родитель
3184f84834
Коммит
89f771f681
|
@ -1,10 +1,8 @@
|
|||
[![Issue Stats](http://issuestats.com/github/microsoft/Angara.Statistics/badge/issue)](http://issuestats.com/github/microsoft/Angara.Statistics)
|
||||
[![Issue Stats](http://issuestats.com/github/microsoft/Angara.Statistics/badge/pr)](http://issuestats.com/github/microsoft/Angara.Statistics)
|
||||
|
||||
# Angara.Statistics
|
||||
|
||||
Angara.Statistics
|
||||
======================
|
||||
=================
|
||||
|
||||
A collection of essential algorithms for Bayesian data constrained modelling.
|
||||
Includes Mersenne twister random number generator, common probability distributions,
|
||||
|
|
|
@ -253,10 +253,13 @@ and Sampler private (// utilities
|
|||
pall.[index+offset] <- pdef
|
||||
pall
|
||||
|
||||
static member Create(pp: Parameters, rng: MT19937, logl: Parameters -> float) =
|
||||
static member Restore(pp: Parameters, rng: MT19937, detlas, ltotold, ptotold, accept, runalt, runacc) =
|
||||
// init_chains
|
||||
let pall = make_pall pp
|
||||
let paramcount = pall.Length
|
||||
Sampler(pall, metr_k, rng, pp, deltas, ltotold, ptotold, accept, runalt, runacc)
|
||||
|
||||
static member Create(pp: Parameters, rng: MT19937, logl: Parameters -> float) =
|
||||
// initRandomValues
|
||||
let values = pall |> Array.mapi (fun i def ->
|
||||
if def.delay<1
|
||||
|
@ -273,8 +276,7 @@ and Sampler private (// utilities
|
|||
let ltotold = pp.SetValues values |> logl
|
||||
let ptotold = log_prior pall values
|
||||
// initialize iteration number
|
||||
let metr_k = 1
|
||||
Sampler(pall, metr_k, rng, pp.SetValues values, deltas, ltotold, ptotold, false, runalt, runacc)
|
||||
Restore(1, rng, pp.SetValues values, deltas, ltotold, ptotold, false, runalt, runacc)
|
||||
|
||||
member x.Probe(isBurnIn:bool, logl: Parameters -> float) =
|
||||
let paramcount = pall.Length
|
||||
|
|
|
@ -958,3 +958,20 @@ module Serialization =
|
|||
member x.TypeId = "ProbDist"
|
||||
member x.Serialize _ d = serializeDistribution d
|
||||
member x.Deserialize _ is = deserializeDistribution is
|
||||
|
||||
type MersenneTwisterSerializer() =
|
||||
interface ISerializer<MT19937> with
|
||||
member x.TypeId = "MeresenneTwister"
|
||||
member x.Serialize _ g =
|
||||
let seed = g.get_seed()
|
||||
use buffer = System.IO.MemoryStream (seed.Length*4)
|
||||
use writer = System.IO.BinaryWriter buffer
|
||||
seed |> Array.iter writer.Write
|
||||
ByteArray(buffer.GetBuffer())
|
||||
member x.Deserialize _ is =
|
||||
match InfoSet.TryGetByteArray is with
|
||||
| None -> invalidArg "is" "Invalid InfoSet"
|
||||
| Some buffer ->
|
||||
use reader = System.IO.BinaryReader(new System.IO.MemoryStream buffer)
|
||||
let seed = Array.init (buffer.Length/4) (fun _ -> reader.ReadUInt32())
|
||||
MT19937 seed
|
|
@ -35,6 +35,16 @@ let DistributionSerialization() =
|
|||
raises<System.ArgumentException> <@ deserializeDistribution (InfoSet.String "") @>
|
||||
raises<System.ArgumentException> <@ deserializeDistribution (InfoSet.Double 1.) @>
|
||||
|
||||
[<Test>]
|
||||
let MersenneTwisterSerialization() =
|
||||
let lib = SerializerLibrary.CreateDefault()
|
||||
lib.Register(MersenneTwisterSerializer())
|
||||
let check d =
|
||||
let is = serializeDistribution d
|
||||
d =! deserializeDistribution is
|
||||
let json = Json.FromObject(lib,d).ToString()
|
||||
d =! Json.ToObject(Newtonsoft.Json.Linq.JObject.Parse json,lib)
|
||||
|
||||
[<Test>]
|
||||
let ParametersSerialization() =
|
||||
let lib = SerializerLibrary.CreateDefault()
|
||||
|
|
Загрузка…
Ссылка в новой задаче