コード例 #1
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);
                }
            }
        }
コード例 #2
0
ファイル: Analysis.cs プロジェクト: alexdad/MarketModel
        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);

                }
            }

        }