예제 #1
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));
        }
예제 #2
0
        public static void ReadParams(string fname)
        {
            int cycles = 400;
            int repeats = 1000;
            int startsize = 4000000;

            int doublerebalance = cycles;
            int doubleCountryWeight = 1;
            int doubleWorldWeight = 1;
            double stepsInYear = 10.8684;
            int bins = 200;
            int wdBins = 5;
            double[] qtops = { 0.5, 0.8, 1.2, 2.0 };  // wdBins length
            int cutoff = 95;
            int essentialsPercent = 80;
            int allowedInsufficientRate = 5;
            string doubleWorldName = "world.jpg";
            Double[] sweepWithdrawalRates = { 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5 };
            double[] sweepWorldShares = { 0, 0.25, 0.5, 0.75 };
            int[] sweepEquities = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
            int[] sweepBonds = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
            int[] sweepStrategies = { 1, 2, 3 };

            string resultLocation = ".";
            string prefix = "R";

            var sr = new StreamReader(File.OpenRead(fname));
            while (!sr.EndOfStream)
            {
                var line = sr.ReadLine().Trim();
                if (line.Length == 0 || line.StartsWith("#"))
                    continue;
                var values = line.Split(',');
                switch (values[0].ToLower().Trim())
                {
                    case "prefix":
                        prefix = values[1];
                        break;
                    case "location":
                        resultLocation = values[1];
                        break;
                    case "cycles":
                        cycles = int.Parse(values[1]);
                        break;
                    case "repeats":
                        repeats = int.Parse(values[1]);
                        break;
                    case "startsum":
                        startsize = int.Parse(values[1]);
                        break;
                    case "doubleworldname":
                        doubleWorldName = values[1];
                        break;
                    case "doublerebalance":
                        doublerebalance = int.Parse(values[1]);
                        break;
                    case "doubleworldweight":
                        doubleWorldWeight = int.Parse(values[1]);
                        break;
                    case "doublecountryweight":
                        doubleCountryWeight = int.Parse(values[1]);
                        break;
                    case "bins":
                        bins = int.Parse(values[1]);
                        break;
                    case "wdbins":
                        wdBins = int.Parse(values[1]);
                        break;
                    case "qtops":
                        qtops = new double[wdBins-1];
                        for (int i = 1; i < wdBins; i++)
                            qtops[i - 1] = double.Parse(values[i]);
                        break;
                    case "cutoff":
                        cutoff = int.Parse(values[1]);
                        break;
                    case "essentialspercent":
                        essentialsPercent = int.Parse(values[1]);
                        break;
                    case "allowedinsufficientrate":
                        allowedInsufficientRate = int.Parse(values[1]);
                        break;
                    case "stepsinyear":
                        stepsInYear = double.Parse(values[1]);
                        break;
                    case "sweepwithdrawalrates":
                        sweepWithdrawalRates = new double[values.Length - 2];
                        for (int i = 1; i < values.Length - 1; i++)
                            sweepWithdrawalRates[i - 1] = double.Parse(values[i]);
                        break;
                    case "sweepworld":
                        sweepWorldShares = new double[values.Length - 2];
                        for (int i = 1; i < values.Length - 1; i++)
                            sweepWorldShares[i - 1] = double.Parse(values[i]);
                        break;
                    case "sweepequities":
                        sweepEquities = new int[values.Length - 2];
                        for (int i = 1; i < values.Length - 1; i++)
                            sweepEquities[i - 1] = int.Parse(values[i]);
                        break;
                    case "sweepbonds":
                        sweepBonds = new int[values.Length - 2];
                        for (int i = 1; i < values.Length - 1; i++)
                            sweepBonds[i - 1] = int.Parse(values[i]);
                        break;
                    case "sweepstr":
                        sweepStrategies = new int[values.Length - 2];
                        for (int i = 1; i < values.Length - 1; i++)
                            sweepStrategies[i - 1] = int.Parse(values[i]);
                        break;
                }
            }

            s_globals = new Globals(
                cycles,
                repeats,
                startsize,
                doublerebalance,
                doubleCountryWeight,
                doubleWorldWeight,
                doubleWorldName,
                bins,
                wdBins,
                cutoff,
                essentialsPercent,
                allowedInsufficientRate,
                stepsInYear,
                prefix,
                resultLocation,
                sweepWithdrawalRates,
                sweepWorldShares,
                sweepEquities,
                sweepBonds,
                sweepStrategies,
                qtops);
        }