Beispiel #1
0
        public static Model SweepModel(Model mp, SweepParameters sw, Country c)
        {
            Model m = new Model(mp.Strategy, mp.StartEq, mp.StartBo, mp.YearlyWithdrawal, mp.RebalanceEvery, c.Filename);
            if (sw.Strategy >= 0)
                m.Strategy = sw.Strategy;
            if (sw.Equity >= 0)
                m.StartEq = sw.Equity;
            if (sw.Bonds >= 0)
                m.StartBo = sw.Bonds;
            if (sw.WithdrawalRate >= 0)
                m.YearlyWithdrawal = sw.WithdrawalRate;
            if (sw.WorldShare >= 0)
                m.WorldShare = sw.WorldShare;

            return m;
        }
Beispiel #2
0
        // Sweep run for a double-part portfolio 
        public static void ExecuteSweepDouble(
            List<Country> countries,
            List<Model> models,
            SweepParameters[] sweeps,
            ConcurrentBag<ModelResult> modelResults,
            StreamWriter swr,
            Object printlock)
        {
            // Separate countries into 2 groups: 2=WORLD, 1=all others
            double weight1 = 0.0, weight2 = 0.0;
            List<Country> countries1 = new List<Country>();
            List<Country> countries2 = new List<Country>();
            foreach (Country c in countries)
            {
                if (Globals.IsWorld(c.Filename))
                {
                    countries2.Add(c);
                    weight2 += c.Weight;
                }
                else
                {
                    countries1.Add(c);
                    weight1 += c.Weight;
                }
            }
            if (weight1 <= 0 || weight2 <= 0)
                throw new Exception("Cannot find the world or others");

            // Group2 is just the World
            List<int> equityChanges2 = new List<int>();
            List<int> bondChanges2 = new List<int>();
            List<int> billChanges2 = new List<int>();

            GraphAcquierer.Acquire(countries2, equityChanges2, bondChanges2, billChanges2, printlock);

            Distro distroEquities2 = new Distro(Globals.Singleton().Bins);
            Distro distroBonds2 = new Distro(Globals.Singleton().Bins);
            Distro distroBills2 = new Distro(Globals.Singleton().Bins);

            Distro.Prepare(
                equityChanges2, bondChanges2, billChanges2,
                distroEquities2, distroBonds2, distroBills2,
                printlock);

            // Group1 is all except World
            List<int> equityChanges1 = new List<int>();
            List<int> bondChanges1 = new List<int>();
            List<int> billChanges1 = new List<int>();

            GraphAcquierer.Acquire(countries1, equityChanges1, bondChanges1, billChanges1, printlock);

            Distro distroEquities1 = new Distro(Globals.Singleton().Bins);
            Distro distroBonds1 = new Distro(Globals.Singleton().Bins);
            Distro distroBills1 = new Distro(Globals.Singleton().Bins);

            Distro.Prepare(
                equityChanges1, bondChanges1, billChanges1,
                distroEquities1, distroBonds1, distroBills1,
                printlock);

            lock (printlock)
            {
                Console.WriteLine(Utils.ResultHeader);
            }

            var res2 = Parallel.ForEach(
                models,
                (m) =>
                {
                    var res1 = Parallel.ForEach(
                        sweeps,
                        (sw) =>
                        {
                            Country nullCountry = new Country();
                            Model mm = Model.SweepModel(m, sw, nullCountry);
                            if (mm.Validate())
                            {
                                List<SingleRunResult> result = Market.RunDoublePortfolioExperiment(
                                    mm,
                                    weight2 / (weight1 + weight2),
                                    distroEquities1, distroBonds1, distroBills1,
                                    distroEquities2, distroBonds2, distroBills2);

                                ModelResult mr = new ModelResult(mm, result);
                                modelResults.Add(mr);
                                Utils.WriteResult(null, mm, mr, printlock);
                                Utils.WriteResult(swr, mm, mr, printlock);
                            }
                        });
                });
        }
Beispiel #3
0
        // Sweep run for a single portfolio
        public static void ExecuteSweepSingle(
            List<Country> countries,
            List<Model> models,
            SweepParameters[] sweeps,
            ConcurrentBag<ModelResult> modelResults,
            StreamWriter swr,
            Object printlock)
        {
            Country c = new Country("", 0, 0, 0.0, 0.0, 0.0, 0.0, 0);

            List<int> equityChanges = new List<int>();
            List<int> bondChanges = new List<int>();
            List<int> billChanges = new List<int>();

            GraphAcquierer.Acquire(countries, equityChanges, bondChanges, billChanges, printlock);

            Distro distroEquities = new Distro(Globals.Singleton().Bins);
            Distro distroBonds = new Distro(Globals.Singleton().Bins);
            Distro distroBills = new Distro(Globals.Singleton().Bins);

            Distro.Prepare(
                equityChanges, bondChanges, billChanges,
                distroEquities, distroBonds, distroBills,
                printlock);

            Distro.Test(
                distroEquities, distroBonds, distroBills,
                printlock);

            lock (printlock)
            {
                Console.WriteLine(Utils.ResultHeader);
            }

            var res2 = Parallel.ForEach(
                models,
                (m) =>
                {
                    var res1 = Parallel.ForEach(
                        sweeps,
                        (sw) =>
                        {
                            Model mm = Model.SweepModel(m, sw, c);
                            if (mm.Validate())
                            {
                                List<SingleRunResult> result = Market.RunSinglePortfolioExperiment(
                                    mm,
                                    distroEquities, distroBonds, distroBills);

                                ModelResult mr = new ModelResult(mm, result);
                                modelResults.Add(mr);
                                Utils.WriteResult(null, mm, mr, printlock);
                                Utils.WriteResult(swr, mm, mr, printlock);
                            }
                        });
                });
        }