Beispiel #1
0
        public static Uncertain <T[]> MarkovModel <T>(this Uncertain <T[]> observations, Uncertain <T> init, Func <T, Uncertain <T> > transition, Func <T, Uncertain <T> > emission) where T : IEquatable <T>
        {
            Func <IEnumerable <T>, Uncertain <T[]> > RunOne = obs =>
            {
                var initlst = from prior in init
                              select FunctionalList.Cons(prior, FunctionalList.Empty <T>());

                return(obs.Aggregate <T, Uncertain <FunctionalList <T> >, Uncertain <T[]> >(
                           initlst,
                           (list, obs_i) =>
                {
                    var program = from head in list
                                  let state = head.Head
                                              from next in transition(state)
                                              from emit in emission(next)
                                              where obs_i.Equals(emit)
                                              select FunctionalList.Cons(next, head);
                    return program;
                },
                           uncertainlst =>
                {
                    return from sample in uncertainlst
                    select sample.Reverse().Skip(1) /* skip prior */.ToArray();
                }));
            };

            return(from obs in observations
                   from result in RunOne(obs)
                   select result);

            //Uncertain < T[] > output = observations.Aggregate<Uncertain<T>, Uncertain<FunctionalList<T>>, Uncertain<T[]>>(
            //    FunctionalList.Empty<T>(),
            //    (i, j) =>
            //    {
            //        return from lst in i
            //               from sample in j
            //               select FunctionalList.Cons(sample, lst);
            //    },
            //    uncertainlst =>
            //    {
            //        return from sample in uncertainlst
            //               select sample.Reverse().ToArray();
            //    });
            //return output;
        }
Beispiel #2
0
        public static Uncertain <T[]> USeq <T>(this IEnumerable <Uncertain <T> > source, int num)
        {
            Uncertain <T[]> output = source.Take(num).Aggregate <Uncertain <T>, Uncertain <FunctionalList <T> >, Uncertain <T[]> >(
                FunctionalList.Empty <T>(),
                (i, j) =>
            {
                return(from lst in i
                       from sample in j
                       select FunctionalList.Cons(sample, lst));
            },
                uncertainlst =>
            {
                return(from sample in uncertainlst
                       select sample.Reverse().ToArray());
            });

            return(output);
        }
Beispiel #3
0
        public static Uncertain <R[]> USeq2 <T, R>(this IEnumerable <Uncertain <T> > source, Func <T[], R[]> selector)
        {
            Uncertain <R[]> output = source.Aggregate <Uncertain <T>, Uncertain <FunctionalList <T> >, Uncertain <R[]> >(
                FunctionalList.Empty <T>(),
                (i, j) =>
            {
                return(from lst in i
                       from sample in j
                       select FunctionalList.Cons(sample, lst));
            },
                uncertainlst =>
            {
                return(from sample in uncertainlst
                       let vec = sample.Reverse().ToArray()
                                 select selector(vec));
            });

            return(output);
        }