Esempio n. 1
0
        public Markov <T> ToDistribution()
        {
            var initial     = _initial.ToDistribution();
            var transitions = _transitions.ToDictionary(
                kv => kv.Key,
                kv => kv.Value.ToDistribution());

            IDistribution <T> Transition(T state) => transitions.GetValueOrDefault(state, Empty <T> .Distribution());

            return(Markov <T> .Distribution(initial, Transition));
        }
Esempio n. 2
0
        public static Metropolis <T> Distribution(
            Func <T, float> target,
            IDistribution <T> initial,
            Func <T, IDistribution <T> > proposal,
            IRNG random)
        {
            var markov = Markov <T> .Distribution(initial, Transition);

            var chain = markov.Sample(random);

            return(new Metropolis <T>(target, chain.GetEnumerator()));

            IDistribution <T> Transition(T item)
            {
                T     candidate   = proposal(item).Sample(random);
                float probability = target(candidate) / target(candidate);

                return(Flip <T> .Distribution(candidate, item, probability));
            }
        }