Пример #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);
        }
Пример #2
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;
        }
Пример #3
0
        public static void Analyze(
            List<Country> countries,
            List<Model> models,
            Portfolio portfolio,
            SweepMode sweepMode,
            SweepParameters[] sweeps,
            ConcurrentBag<ModelResult> modelResults,
            string perCountrySummaryFilename,
            string crossCountrySummaryFilename,
            Object printLock)
        {
            // Simple sorting of successful runs
            PerCountryAnalysis(modelResults, perCountrySummaryFilename, printLock);

            if (sweepMode == SweepMode.SweepAndCountry)
            {
                CrossCountryAnalysis(countries, models, portfolio,
                                     sweepMode, sweeps, modelResults,
                                     perCountrySummaryFilename, crossCountrySummaryFilename, printLock);
            }

        }
Пример #4
0
        static void Main(string[] args)
        {
            string    globalFileName, modelsFilename, countriesFileName;
            Portfolio portfolio = Portfolio.Single;
            SweepMode sweepMode = SweepMode.No;

            SweepParameters[] sweeps = new SweepParameters[1];
            int       nFactors       = 0;
            const int maxFactors     = 10;

            Factor[] factors = new Factor[maxFactors];
            int      cp      = 0;

            for (int f = 0; f < factors.Length; f++)
            {
                factors[f] = Factor.None;
            }

            // Global params file
            if (args.Length <= cp)
            {
                Console.Write("Usage: <globals.csv> <countries.csv> <models.csv> [single|dual] [sweep N factor-1 ... factor-n]");
                return;
            }

            globalFileName = args[cp++];
            Globals.ReadParams(globalFileName);
            string resultPrefix = "R_" + Globals.Singleton().Prefix;

            // Countries file
            if (args.Length <= cp)
            {
                Console.Write("Second parameter must point to the countries file");
                return;
            }
            countriesFileName = args[cp++];
            List <Country> countries = Country.ReadCountries(countriesFileName, false);

            // Models file
            if (args.Length <= cp)
            {
                Console.Write("Third parameter must point to the models file");
                return;
            }
            modelsFilename = args[cp++];
            List <Model> models = Model.ReadModels(modelsFilename);

            // Portfolio Composition Mode
            if (args.Length > cp)
            {
                if (args[cp].ToLower().Trim() == "single")
                {
                    Console.WriteLine("The whole portfolio is managed as one thing");
                    portfolio     = Portfolio.Single;
                    resultPrefix += "_Single";
                }
                else if (args[cp].ToLower().Trim() == "double")
                {
                    portfolio = Portfolio.Double;
                    Console.WriteLine("The portfolio is composed of 2 separate parts: all countries except last, and last");
                    resultPrefix += "_Double";
                }
                else
                {
                    Console.WriteLine("First parameter can be only 'single' or 'double'. It defines portfolio composition");
                    return;
                }
                cp++;
            }

            // Sweep mode
            if (args.Length > cp)
            {
                if (args[cp].ToLower().Trim() != "sweep")
                {
                    Console.WriteLine("This parameter can be only 'sweep'. It would request sweep by few listed parameters.");
                    return;
                }
                else
                {
                    sweepMode     = SweepMode.SweepNoCountry;
                    resultPrefix += "_Sweep";
                }
                cp++;
            }

            if (sweepMode != SweepMode.No)
            {
                // Sweep factors counter
                if (args.Length > cp)
                {
                    nFactors = int.Parse(args[cp]);
                    if (nFactors >= maxFactors)
                    {
                        Console.WriteLine("You cannot Sweep by more than {0} factors", maxFactors);
                        return;
                    }
                    Console.WriteLine("Sweep is requested for {0} factors", nFactors);
                    cp++;
                }
                else
                {
                    Console.WriteLine("This parameter can be only sweep factor count");
                    return;
                }
                for (int i = 0; i < nFactors; i++)
                {
                    //Country| Strategy | Withdrawal |  DualShare |  Eq | Bo
                    switch (args[cp].ToLower().Trim())
                    {
                    case "country":
                        sweepMode     = SweepMode.SweepAndCountry;
                        resultPrefix += "_Country";
                        break;

                    case "str":
                        factors[i]    = Factor.Strategy;
                        resultPrefix += "_Strategy";
                        break;

                    case "world":
                        factors[i]    = Factor.WorldShare;
                        resultPrefix += "_World";
                        break;

                    case "wd":
                        factors[i]    = Factor.Withdrawal;
                        resultPrefix += "_Withdrawal";
                        break;

                    case "eq":
                        factors[i]    = Factor.Equity;
                        resultPrefix += "_Equity";
                        break;

                    case "bo":
                        factors[i]    = Factor.Bonds;
                        resultPrefix += "_Bonds";
                        break;

                    default:
                        Console.Write("This parameter can be only Country| Strategy | Withdrawal |  WorldShare |  Equity | Bonds");
                        return;
                    }
                    cp++;
                }
            }


            // Prepare sweep parameters
            if (sweepMode != SweepMode.No)
            {
                sweeps = Utils.Factorize(factors, countries);
                Console.WriteLine("You requested to sweep across {0} combinations", sweeps.Length);
            }

            // Create results dir and copy controling files
            Utils.CreateResultDir(resultPrefix, globalFileName, countriesFileName, modelsFilename);

            Utils.SaveCommand(Utils.CommandFileName(resultPrefix), args);

            // Run simulations
            Execute(
                countries,
                models,
                portfolio,
                sweepMode,
                sweeps,
                Utils.ResultFileName(resultPrefix),
                Utils.PerCountryFileName(resultPrefix),
                Utils.CrossCountryFileName(resultPrefix));
        }
Пример #5
0
        public static void CrossCountryAnalysis(
            List <Country> countries,
            List <Model> models,
            Portfolio portfolio,
            SweepMode sweepMode,
            SweepParameters[] sweeps,
            ConcurrentBag <ModelResult> modelResults,
            string perCountrySummaryFilename,
            string crossCountrySummaryFilename,
            Object printLock)
        {
            Dictionary <SweepParameters, SweepStat> sorter = new Dictionary <SweepParameters, SweepStat>();

            foreach (ModelResult mr in modelResults)
            {
                SweepParameters swp = new SweepParameters();
                swp.Country        = -1;
                swp.WithdrawalRate = mr.model.YearlyWithdrawal;
                swp.WorldShare     = mr.model.WorldShare;
                swp.Equity         = mr.model.StartEq;
                swp.Bonds          = mr.model.StartBo;
                swp.Strategy       = mr.model.Strategy;
                int countryIndex = 0;
                foreach (Country c in countries)
                {
                    if (c.Filename == mr.model.CountryName)
                    {
                        break;
                    }
                    else
                    {
                        countryIndex++;
                    }
                }

                if (!sorter.ContainsKey(swp))
                {
                    SweepStat sws = new SweepStat();
                    sws.sweepResults = new Dictionary <int, List <ModelResult> >();
                    sorter.Add(swp, sws);
                }

                if (!sorter[swp].sweepResults.ContainsKey(countryIndex))
                {
                    sorter[swp].sweepResults.Add(countryIndex, new List <ModelResult>());
                }

                sorter[swp].sweepResults[countryIndex].Add(mr);
            }

            List <SweepResult> sweepResults = new List <SweepResult>();

            foreach (SweepParameters swp in sorter.Keys)
            {
                SweepStat sws = sorter[swp];
                foreach (int country in sorter[swp].sweepResults.Keys)
                {
                    foreach (ModelResult mr in sorter[swp].sweepResults[country])
                    {
                        sws.totalPop            += countries[country].Population;
                        sws.weightedProd        += mr.productivity * countries[country].Population;
                        sws.weightedSuccessRate += mr.overallSuccessRate * countries[country].Population;
                    }
                }
                sws.weightedProd        /= sws.totalPop;
                sws.weightedSuccessRate /= sws.totalPop;

                SweepResult swr = new SweepResult();
                swr.parameters = swp;
                swr.stat       = sws;
                sweepResults.Add(swr);
            }

            IEnumerable <SweepResult> sortedResults = sweepResults.OrderBy(
                sr => (sr.stat.weightedSuccessRate * 10000.0 + sr.stat.weightedProd));

            using (StreamWriter sw = new StreamWriter(crossCountrySummaryFilename))
            {
                sw.WriteLine("WdRate,Strategy,World,Eq,Bo,Prod,Success,");
                foreach (SweepResult sr in sortedResults)
                {
                    sw.WriteLine("{0:F2},{1},{2:F2},{3},{4},{5:F2},{6:F2},",
                                 sr.parameters.WithdrawalRate,
                                 sr.parameters.Strategy,
                                 sr.parameters.WorldShare,
                                 sr.parameters.Equity,
                                 sr.parameters.Bonds,
                                 sr.stat.weightedProd,
                                 sr.stat.weightedSuccessRate);
                }
            }
        }
Пример #6
0
        public static void CrossCountryAnalysis(
            List<Country> countries,
            List<Model> models,
            Portfolio portfolio,
            SweepMode sweepMode,
            SweepParameters[] sweeps,
            ConcurrentBag<ModelResult> modelResults,
            string perCountrySummaryFilename,
            string crossCountrySummaryFilename,
            Object printLock)
        {
            Dictionary<SweepParameters, SweepStat> sorter = new Dictionary<SweepParameters, SweepStat>();

            foreach (ModelResult mr in modelResults)
            {
                SweepParameters swp = new SweepParameters();
                swp.Country = -1;
                swp.WithdrawalRate = mr.model.YearlyWithdrawal;
                swp.WorldShare = mr.model.WorldShare;
                swp.Equity = mr.model.StartEq;
                swp.Bonds = mr.model.StartBo;
                swp.Strategy = mr.model.Strategy;
                int countryIndex = 0;
                foreach(Country c in countries)
                {
                    if (c.Filename == mr.model.CountryName)
                        break;
                    else
                        countryIndex++;
                }

                if (!sorter.ContainsKey(swp))
                {
                    SweepStat sws = new SweepStat();
                    sws.sweepResults = new Dictionary<int, List<ModelResult>>();
                    sorter.Add(swp, sws);
                }

                if (!sorter[swp].sweepResults.ContainsKey(countryIndex))
                    sorter[swp].sweepResults.Add(countryIndex, new List<ModelResult>());

                sorter[swp].sweepResults[countryIndex].Add(mr);
            }

            List<SweepResult> sweepResults = new List<SweepResult>();

            foreach(SweepParameters swp in sorter.Keys)
            {
                SweepStat sws = sorter[swp];
                foreach (int country in sorter[swp].sweepResults.Keys)
                {
                    foreach (ModelResult mr in sorter[swp].sweepResults[country])
                    {
                        sws.totalPop += countries[country].Population;
                        sws.weightedProd += mr.productivity * countries[country].Population;
                        sws.weightedSuccessRate += mr.overallSuccessRate * countries[country].Population;
                    }
                }
                sws.weightedProd /= sws.totalPop;
                sws.weightedSuccessRate /= sws.totalPop;
                
                SweepResult swr = new SweepResult();
                swr.parameters = swp;
                swr.stat = sws;
                sweepResults.Add(swr);
            }
            
            IEnumerable<SweepResult> sortedResults = sweepResults.OrderBy(
                sr => (sr.stat.weightedSuccessRate * 10000.0 + sr.stat.weightedProd));

            using (StreamWriter sw = new StreamWriter(crossCountrySummaryFilename))
            {
                sw.WriteLine("WdRate,Strategy,World,Eq,Bo,Prod,Success,");
                foreach (SweepResult sr in sortedResults)
                {
                    sw.WriteLine("{0:F2},{1},{2:F2},{3},{4},{5:F2},{6:F2},",
                        sr.parameters.WithdrawalRate,
                        sr.parameters.Strategy,
                        sr.parameters.WorldShare,
                        sr.parameters.Equity,
                        sr.parameters.Bonds,
                        sr.stat.weightedProd,
                        sr.stat.weightedSuccessRate);

                }
            }

        }
Пример #7
0
        // Sweep run for a double-part portfolio  by country
        public static void ExecuteSweepDoubleByCountry(
            List<Country> countries,
            List<Model> models,
            SweepParameters[] sweeps,
            ConcurrentBag<ModelResult> modelResults,
            StreamWriter swr,
            Object printlock)
        {
            // Group2 is just the World
            List<Country> countries2 = new List<Country>();
            foreach (Country c in countries)
            {
                if (Globals.IsWorld(c.Filename))
                {
                    countries2.Add(c);
                    countries2.Last().Weight = 1;
                }
            }

            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);

            // Now enumerate countries; Group1 each time will carry just one
            foreach (var c in countries)
            {
                if (Globals.IsWorld(c.Filename))
                    continue;
                List<Country> countries1 = new List<Country>();
                countries1.Add(c);
                countries1.Last().Weight = 1;

                // Group1 is just one country
                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) =>
                            {
                                Model mm = Model.SweepModel(m, sw, c);
                                if (mm.Validate())
                                {
                                    if (mm.StartEq + mm.StartBo <= 100)
                                    {
                                        List<SingleRunResult> result = Market.RunDoublePortfolioExperiment(
                                            mm,
                                            sw.WorldShare,
                                            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);
                                    }
                                }
                            });
                    });

                c.Weight = 0;
            }
        }
Пример #8
0
        // Sweep run for a single portfolio by country
        public static void ExecuteSweepSingleByCountry(
            List<Country> countries,
            List<Model> models,
            SweepParameters[] sweeps,
            ConcurrentBag<ModelResult> modelResults,
            StreamWriter swr,
            Object printlock)
        {
            foreach (var c in countries)
                c.Weight = 0;

            foreach (var c in countries)
            {
                c.Weight = 1;

                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())
                                {
                                    if (mm.StartEq + mm.StartBo <= 100)
                                    {
                                        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);
                                    }
                                }
                            });
                    });

                c.Weight = 0;
            }
        }
Пример #9
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);
                            }
                        });
                });
        }
Пример #10
0
        static void Execute(
            List<Country> countries,
            List<Model> models,
            Portfolio portfolio,
            SweepMode sweepMode,
            SweepParameters[] sweeps,
            string resFile,
            string perCountrySummaryFilename,
            string crossCountrySummaryFilename)
        {
            ConcurrentBag<ModelResult> modelResults = new ConcurrentBag<ModelResult>();
            Object printLock = new Object();

            using (StreamWriter swr = new StreamWriter(resFile))
            {
                swr.WriteLine(Utils.ResultHeader);

                if (sweepMode == SweepMode.No)
                {
                    if (portfolio == Portfolio.Single)
                    {
                        Experiments.ExecuteSingle(countries, models, modelResults, swr, printLock);
                    }
                    else if (portfolio == Portfolio.Double)
                    {
                        Experiments.ExecuteDouble(countries, models, modelResults, swr, printLock);
                    }
                }
                else if (sweepMode == SweepMode.SweepNoCountry)
                {
                    if (portfolio == Portfolio.Single)
                    {
                        Experiments.ExecuteSweepSingle(countries, models, sweeps, modelResults, swr, printLock);
                    }
                    else if (portfolio == Portfolio.Double)
                    {
                        Experiments.ExecuteSweepDouble(countries, models, sweeps, modelResults, swr, printLock);
                    }
                }
                else if (sweepMode == SweepMode.SweepAndCountry)
                {
                    if (portfolio == Portfolio.Single)
                    {
                        Experiments.ExecuteSweepSingleByCountry(countries, models, sweeps, modelResults, swr, printLock);
                    }
                    else if (portfolio == Portfolio.Double)
                    {
                        Experiments.ExecuteSweepDoubleByCountry(countries, models, sweeps, modelResults, swr, printLock);
                    }
                }
            }

            Analysis.Analyze(
                            countries,
                            models,
                            portfolio,
                            sweepMode,
                            sweeps,
                            modelResults, 
                            perCountrySummaryFilename, 
                            crossCountrySummaryFilename,
                            printLock);
        }
Пример #11
0
        static void Main(string[] args)
        {
            string globalFileName, modelsFilename, countriesFileName;
            Portfolio portfolio = Portfolio.Single;
            SweepMode sweepMode = SweepMode.No;
            SweepParameters[] sweeps = new SweepParameters[1];
            int nFactors = 0;
            const int maxFactors = 10;
            Factor[] factors = new Factor[maxFactors];
            int cp = 0;

            for (int f = 0; f < factors.Length; f++)
                factors[f] = Factor.None;

            // Global params file
            if (args.Length <= cp)
            {
                Console.Write("Usage: <globals.csv> <countries.csv> <models.csv> [single|dual] [sweep N factor-1 ... factor-n]");
                return;
            }

            globalFileName = args[cp++];
            Globals.ReadParams(globalFileName);
            string resultPrefix = "R_" + Globals.Singleton().Prefix;

            // Countries file
            if (args.Length <= cp)
            {
                Console.Write("Second parameter must point to the countries file");
                return;
            }
            countriesFileName = args[cp++];
            List<Country> countries = Country.ReadCountries(countriesFileName, false);

            // Models file
            if (args.Length <= cp)
            {
                Console.Write("Third parameter must point to the models file");
                return;
            }
            modelsFilename = args[cp++];
            List<Model> models = Model.ReadModels(modelsFilename);

            // Portfolio Composition Mode
            if (args.Length > cp)
            {
                if (args[cp].ToLower().Trim() == "single")
                {
                    Console.WriteLine("The whole portfolio is managed as one thing");
                    portfolio = Portfolio.Single;
                    resultPrefix += "_Single";
                }
                else if (args[cp].ToLower().Trim() == "double")
                {
                    portfolio = Portfolio.Double;
                    Console.WriteLine("The portfolio is composed of 2 separate parts: all countries except last, and last");
                    resultPrefix += "_Double";
                }
                else
                {
                    Console.WriteLine("First parameter can be only 'single' or 'double'. It defines portfolio composition");
                    return;
                }
                cp++;
            }

            // Sweep mode
            if (args.Length > cp)
            {
                if (args[cp].ToLower().Trim() != "sweep")
                {
                    Console.WriteLine("This parameter can be only 'sweep'. It would request sweep by few listed parameters.");
                    return;
                }
                else
                {
                    sweepMode = SweepMode.SweepNoCountry;
                    resultPrefix += "_Sweep";
                }
                cp++;
            }

            if (sweepMode != SweepMode.No)
            {
                // Sweep factors counter
                if (args.Length > cp)
                {
                    nFactors = int.Parse(args[cp]);
                    if (nFactors >= maxFactors)
                    {
                        Console.WriteLine("You cannot Sweep by more than {0} factors", maxFactors);
                        return;
                    }
                    Console.WriteLine("Sweep is requested for {0} factors", nFactors);
                    cp++;
                }
                else
                {
                    Console.WriteLine("This parameter can be only sweep factor count");
                    return;
                }
                for (int i = 0; i < nFactors; i++)
                {
                    //Country| Strategy | Withdrawal |  DualShare |  Eq | Bo 
                    switch (args[cp].ToLower().Trim())
                    {
                        case "country":
                            sweepMode = SweepMode.SweepAndCountry;
                            resultPrefix += "_Country";
                            break;
                        case "str":
                            factors[i] = Factor.Strategy;
                            resultPrefix += "_Strategy";
                            break;
                        case "world":
                            factors[i] = Factor.WorldShare;
                            resultPrefix += "_World";
                            break;
                        case "wd":
                            factors[i] = Factor.Withdrawal;
                            resultPrefix += "_Withdrawal";
                            break;
                        case "eq":
                            factors[i] = Factor.Equity;
                            resultPrefix += "_Equity";
                            break;
                        case "bo":
                            factors[i] = Factor.Bonds;
                            resultPrefix += "_Bonds";
                            break;
                        default:
                            Console.Write("This parameter can be only Country| Strategy | Withdrawal |  WorldShare |  Equity | Bonds");
                            return;
                    }
                    cp++;
                }
            }


            // Prepare sweep parameters
            if (sweepMode != SweepMode.No)
            {
                sweeps = Utils.Factorize(factors, countries);
                Console.WriteLine("You requested to sweep across {0} combinations", sweeps.Length);
            }

            // Create results dir and copy controling files
            Utils.CreateResultDir(resultPrefix, globalFileName, countriesFileName, modelsFilename);

            Utils.SaveCommand(Utils.CommandFileName(resultPrefix), args);

            // Run simulations
            Execute(
                countries,
                models,
                portfolio,
                sweepMode,
                sweeps,
                Utils.ResultFileName(resultPrefix),
                Utils.PerCountryFileName(resultPrefix),
                Utils.CrossCountryFileName(resultPrefix));
        }
Пример #12
0
        public static SweepParameters[] Factorize(Factor[] factors, List <Country> countries)
        {
            SweepParameters[] sweeps = new SweepParameters[1];
            sweeps[0].Strategy   = -1;
            sweeps[0].Equity     = -1;
            sweeps[0].Bonds      = -1;
            sweeps[0].WorldShare = (double)Globals.Singleton().DoubleWorldWeight /
                                   (double)(Globals.Singleton().DoubleWorldWeight + Globals.Singleton().DoubleCountryWeight);
            sweeps[0].WithdrawalRate = -1;
            sweeps[0].Country        = -1;

            int nCountries = countries.Count;

            for (int f = 0; f < factors.Length; f++)
            {
                switch (factors[f])
                {
                case Factor.Strategy:
                {
                    SweepParameters[] oldSweeps = sweeps;
                    sweeps = new SweepParameters[oldSweeps.Length * Globals.Singleton().SweepStrategies.Length];
                    int c = 0;
                    for (int o = 0; o < oldSweeps.Length; o++)
                    {
                        for (int n = 0; n < Globals.Singleton().SweepStrategies.Length; n++)
                        {
                            sweeps[c]          = oldSweeps[o];
                            sweeps[c].Strategy = Globals.Singleton().SweepStrategies[n];
                            c++;
                        }
                    }
                }
                break;

                case Factor.Withdrawal:
                {
                    SweepParameters[] oldSweeps = sweeps;
                    sweeps = new SweepParameters[oldSweeps.Length * Globals.Singleton().SweepWithdrawalRates.Length];
                    int c = 0;
                    for (int o = 0; o < oldSweeps.Length; o++)
                    {
                        for (int n = 0; n < Globals.Singleton().SweepWithdrawalRates.Length; n++)
                        {
                            sweeps[c] = oldSweeps[o];
                            sweeps[c].WithdrawalRate = Globals.Singleton().SweepWithdrawalRates[n];
                            c++;
                        }
                    }
                }
                break;

                case Factor.WorldShare:
                {
                    SweepParameters[] oldSweeps = sweeps;
                    sweeps = new SweepParameters[oldSweeps.Length * Globals.Singleton().SweepWorldShares.Length];
                    int c = 0;
                    for (int o = 0; o < oldSweeps.Length; o++)
                    {
                        for (int n = 0; n < Globals.Singleton().SweepWorldShares.Length; n++)
                        {
                            sweeps[c]            = oldSweeps[o];
                            sweeps[c].WorldShare = Globals.Singleton().SweepWorldShares[n];
                            c++;
                        }
                    }
                }
                break;

                case Factor.Equity:
                {
                    SweepParameters[] oldSweeps = sweeps;
                    sweeps = new SweepParameters[oldSweeps.Length * Globals.Singleton().SweepEquities.Length];
                    int c = 0;
                    for (int o = 0; o < oldSweeps.Length; o++)
                    {
                        for (int n = 0; n < Globals.Singleton().SweepEquities.Length; n++)
                        {
                            sweeps[c]        = oldSweeps[o];
                            sweeps[c].Equity = Globals.Singleton().SweepEquities[n];
                            c++;
                        }
                    }
                }
                break;

                case Factor.Bonds:
                {
                    SweepParameters[] oldSweeps = sweeps;
                    sweeps = new SweepParameters[oldSweeps.Length * Globals.Singleton().SweepBonds.Length];
                    int c = 0;
                    for (int o = 0; o < oldSweeps.Length; o++)
                    {
                        for (int n = 0; n < Globals.Singleton().SweepBonds.Length; n++)
                        {
                            sweeps[c]       = oldSweeps[o];
                            sweeps[c].Bonds = Globals.Singleton().SweepBonds[n];
                            c++;
                        }
                    }
                }
                break;

                case Factor.None:
                    break;
                }
            }

            List <SweepParameters> sweeps1 = new List <SweepParameters>();
            int count = 0;

            for (int i = 0; i < sweeps.Length; i++)
            {
                if (sweeps[i].Equity + sweeps[i].Bonds <= 100)
                {
                    sweeps1.Add(sweeps[i]);
                    count++;
                }
            }

            sweeps = new SweepParameters[count];
            int cn = 0;

            foreach (SweepParameters s in sweeps1)
            {
                sweeps[cn++] = s;
            }

            return(sweeps);
        }
Пример #13
0
        public static SweepParameters[] Factorize(Factor[] factors, List<Country> countries)
        {
            SweepParameters[] sweeps = new SweepParameters[1];
            sweeps[0].Strategy = -1;
            sweeps[0].Equity = -1;
            sweeps[0].Bonds = -1;
            sweeps[0].WorldShare = (double)Globals.Singleton().DoubleWorldWeight / 
                                   (double)(Globals.Singleton().DoubleWorldWeight + Globals.Singleton().DoubleCountryWeight);
            sweeps[0].WithdrawalRate = -1;
            sweeps[0].Country = -1;

            int nCountries = countries.Count;

            for (int f = 0; f < factors.Length; f++)
            {
                switch (factors[f])
                {
                    case Factor.Strategy:
                        {
                            SweepParameters[] oldSweeps = sweeps;
                            sweeps = new SweepParameters[oldSweeps.Length * Globals.Singleton().SweepStrategies.Length];
                            int c = 0;
                            for (int o = 0; o < oldSweeps.Length; o++)
                            {
                                for (int n = 0; n < Globals.Singleton().SweepStrategies.Length; n++)
                                {
                                    sweeps[c] = oldSweeps[o];
                                    sweeps[c].Strategy = Globals.Singleton().SweepStrategies[n];
                                    c++;
                                }
                            }
                        }
                        break;
                    case Factor.Withdrawal:
                        {
                            SweepParameters[] oldSweeps = sweeps;
                            sweeps = new SweepParameters[oldSweeps.Length * Globals.Singleton().SweepWithdrawalRates.Length];
                            int c = 0;
                            for (int o = 0; o < oldSweeps.Length; o++)
                            {
                                for (int n = 0; n < Globals.Singleton().SweepWithdrawalRates.Length; n++)
                                {
                                    sweeps[c] = oldSweeps[o];
                                    sweeps[c].WithdrawalRate = Globals.Singleton().SweepWithdrawalRates[n];
                                    c++;
                                }
                            }
                        }
                        break;
                    case Factor.WorldShare:
                        {
                            SweepParameters[] oldSweeps = sweeps;
                            sweeps = new SweepParameters[oldSweeps.Length * Globals.Singleton().SweepWorldShares.Length];
                            int c = 0;
                            for (int o = 0; o < oldSweeps.Length; o++)
                            {
                                for (int n = 0; n < Globals.Singleton().SweepWorldShares.Length; n++)
                                {
                                    sweeps[c] = oldSweeps[o];
                                    sweeps[c].WorldShare = Globals.Singleton().SweepWorldShares[n];
                                    c++;
                                }
                            }
                        }
                        break;
                    case Factor.Equity:
                        {
                            SweepParameters[] oldSweeps = sweeps;
                            sweeps = new SweepParameters[oldSweeps.Length * Globals.Singleton().SweepEquities.Length];
                            int c = 0;
                            for (int o = 0; o < oldSweeps.Length; o++)
                            {
                                for (int n = 0; n < Globals.Singleton().SweepEquities.Length; n++)
                                {
                                    sweeps[c] = oldSweeps[o];
                                    sweeps[c].Equity = Globals.Singleton().SweepEquities[n];
                                    c++;
                                }
                            }
                        }
                        break;
                    case Factor.Bonds:
                        {
                            SweepParameters[] oldSweeps = sweeps;
                            sweeps = new SweepParameters[oldSweeps.Length * Globals.Singleton().SweepBonds.Length];
                            int c = 0;
                            for (int o = 0; o < oldSweeps.Length; o++)
                            {
                                for (int n = 0; n < Globals.Singleton().SweepBonds.Length; n++)
                                {
                                    sweeps[c] = oldSweeps[o];
                                    sweeps[c].Bonds = Globals.Singleton().SweepBonds[n];
                                    c++;
                                }
                            }
                        }
                        break;
                    case Factor.None:
                        break;
                }
            }

            List<SweepParameters> sweeps1 = new List<SweepParameters>();
            int count = 0;
            for (int i = 0; i < sweeps.Length; i++)
            {
                if (sweeps[i].Equity + sweeps[i].Bonds <= 100)
                {
                    sweeps1.Add(sweeps[i]);
                    count++;
                }
            }

            sweeps = new SweepParameters[count];
            int cn = 0;
            foreach (SweepParameters s in sweeps1)
                sweeps[cn++] = s;

            return sweeps;
        }