示例#1
0
        /// <summary>
        /// モンテカルロ積分を行う。
        /// </summary>
        /// <param name="result"></param>
        /// <param name="numerator"></param>
        /// <param name="denominator"></param>
        /// <param name="calculation_count"></param>
        /// <param name="iscalar"></param>
        /// <param name="range_max"></param>
        /// <param name="range_min"></param>
        /// <param name="seeds"></param>
        public static void Monte_Carlo_Integration
            (ref decimal result, ref decimal numerator, ref decimal denominator
            , uint calculation_count
            , IScalarFunction iscalar, decimal[] range_max, decimal[] range_min
            , uint[] seeds)
        {
            //積分区間の最大値と最小値の次元をそろえる
            if (range_max.Length != range_min.Length)
            {
                throw new FormatException("length of " + nameof(range_max) + "(" + range_max.Length + ")"
                                          + " with that of " + nameof(range_min) + "(" + range_min.Length + ")");
            }
            //乱数の種の次元もそろえる
            if (range_max.Length != seeds.Length)
            {
                throw new FormatException("length of " + nameof(range_max) + "(" + range_max.Length + ")"
                                          + " with that of " + nameof(seeds) + "(" + seeds.Length + ")");
            }

            //一様乱数のclassを生成する
            List <UniformDistribution> list_ud = new List <UniformDistribution>();

            for (int j = 0; j < range_min.Length; j++)
            {
                list_ud.Add(new UniformDistribution(seeds[j]));
            }


            //積分を行う
            decimal[] xs = new decimal[range_min.Length];
            for (uint j = 0; j < calculation_count; j++)
            {
                //乱数を生成する
                for (int k = 0; k < list_ud.Count; k++)
                {
                    xs[k] = list_ud[k].NextDecimal(range_max[k], range_min[k]);
                }
                numerator += iscalar.Calculate_f_u(xs);
                denominator++;
            }

            result = numerator / denominator;
        }
示例#2
0
        public static void MetropolisHastings
            (ref decimal result, ref decimal numerator, ref decimal denominator
            , ref decimal partition_function
            , uint calculation_count_epoch
            , decimal[] initial_x, ref decimal[] final_x
            , IAction iaction, decimal[] step_half_width, uint[] seeds_for_step
            , uint seed_for_judge
            , IScalarFunction iscalar
            )
        {
            //ジャンプの幅の乱数の種の次元をそろえる
            if (step_half_width.Length != seeds_for_step.Length)
            {
                throw new FormatException("Length of " + nameof(step_half_width) + "(" + step_half_width.Length + ")"
                                          + " with that of " + nameof(seeds_for_step) + "(" + seeds_for_step.Length + ")");
            }
            //初期位置と乱数の種の次元をそろえる
            if (initial_x.Length != seeds_for_step.Length)
            {
                throw new FormatException("Length of " + nameof(initial_x) + "(" + initial_x.Length + ")"
                                          + " with that of " + nameof(seeds_for_step) + "(" + seeds_for_step.Length + ")");
            }

            //ジャンプの幅を正の数にしておく
            decimal[] abs_step_half_width = new decimal[step_half_width.Length];
            for (int j = 0; j < step_half_width.Length; j++)
            {
                abs_step_half_width[j] = Math.Abs(step_half_width[j]);
            }

            //一様乱数のclassを生成する
            List <UniformDistribution> list_ud = new List <UniformDistribution>();

            for (int j = 0; j < step_half_width.Length; j++)
            {
                list_ud.Add(new UniformDistribution(seeds_for_step[j]));
            }
            UniformDistribution judge = new UniformDistribution(seed_for_judge);


            //初期設定を行う
            decimal[] xs           = new decimal[step_half_width.Length];
            decimal[] xs_candidate = new decimal[step_half_width.Length];
            for (int k = 0; k < step_half_width.Length; k++)
            {
                xs[k]           = initial_x[k];
                xs_candidate[k] = xs[k];
            }

            decimal action           = 0m;
            decimal action_candidate = 0m;

            action           = iaction.Calculate_f_u(xs);
            action_candidate = action;


            //計算を行う
            for (int j = 0; j < calculation_count_epoch; j++)
            {
                //新しいxの候補を計算する。
                for (int k = 0; k < step_half_width.Length; k++)
                {
                    //xの値を1次元だけ動かす
                    xs_candidate[k] = xs[k] + list_ud[k].NextDecimal(abs_step_half_width[k], -abs_step_half_width[k]);
                }
                action_candidate = iaction.Calculate_f_u(xs_candidate);


                //更新できる場合
                if (judge.NextDecimal() < TaylorSeriesDecimal.Exponential(action - action_candidate))
                {
                    //被積分関数の値を足す
                    numerator += iscalar.Calculate_f_u(xs_candidate);

                    //分配関数に確率値を加える。
                    partition_function += TaylorSeriesDecimal.Exponential(-action_candidate);

                    for (int k = 0; k < step_half_width.Length; k++)
                    {
                        //xの値を更新する。
                        xs[k] = xs_candidate[k];
                    }

                    //作用を更新する。
                    action = action_candidate;
                }
                else
                {
                }

                denominator++;
            }

            result = numerator / denominator;

            for (int j = 0; j < initial_x.Length; j++)
            {
                final_x[j] = xs[j];
            }
        }