/// <summary>
 /// Assign every account with monthly balances that fit randomly assigned trend
 /// </summary>
 public void AssignRandomTrends(Trend goodBalance, Trend badBalance, double variation, Random random)
 {
     foreach (AccountRecord record in accounts)
     {
         var account = record.Value;
         account.AssignRandomTrend(goodBalance, badBalance, variation, random);
     }
 }
Exemple #2
0
        /// <summary>
        /// Assign balance history to vary randomly around randomly assigned trend
        /// </summary>
        public void AssignRandomTrend(Trend goodBalance, Trend badBalance, double variation, Random random)
        {
            // choose random trend
            const double rateScale = 100.0;
            const double balanceScale = 100.0;
            double rateMean = (goodBalance.Slope + badBalance.Slope) / 2;
            double initialBalanceMean = (goodBalance.Intercept + badBalance.Intercept) / 2;
            double rate = rateMean + rateScale * random.NextDouble();
            double initialBalance = initialBalanceMean + balanceScale * random.NextDouble();
            Trend trend = new Trend { Slope = rate, Intercept = initialBalance };

            // balance history is trend plus noise
            for (int i = 0; i < Balance.Length; i++)
            {
            	Balance[i] = trend.Predict(i) + variation * random.NextDouble();
            }
        }
        /// <summary>
        /// Usage: CreditReview n, optional n is number of customers, use 100,000+ for meaningful timings
        /// </summary>
        static void Main(string[] args)
        {
            Console.WriteLine("Credit Review Sample\n");
#if DEBUG
            Console.WriteLine("For most accurate timing results, use Release build.\n");
#endif

            Random random = new Random(1);  // seed 1 makes runs reproducible

            // Defaults for data generation, may override some on command line
            int months = 36;
            int customerCount = 1000000; // for data runs make big enough for significant timing measurements
            Trend goodBalance = new Trend { Slope = 0.0, Intercept = 0.0 };
            Trend badBalance = new Trend { Slope = -150.0, Intercept = 0.0 };
            const double variation = 100.0;
            const double overdraft = -1000.0; // Default overdraft limit

            // Printed table of results
            const int rows = 8;
            const int cols = 4;

            // Optionally override some defaults on command line
            if (args.Length > 0) 
                customerCount = Int32.Parse(args[0], CultureInfo.CurrentCulture);
            if (args.Length > 1) 
                months = Int32.Parse(args[1], CultureInfo.CurrentCulture);
            if (months < 4) 
                months = 4; // PrintBalance requires at least 4 months

            // Force JIT compilation before timing tests
            const int fewCustomers = 10;
            const int fewMonths = 3;
            AccountRepository smallAccounts = new AccountRepository(fewCustomers, fewMonths, overdraft);
            smallAccounts.AssignRandomTrends(goodBalance, badBalance, variation, random);
            UpdatePredictionsSequential(smallAccounts);
            UpdatePredictionsParallel(smallAccounts);
            UpdatePredictionsPlinq(smallAccounts);

            // Create accounts for timing tests
            AccountRepository accounts = new AccountRepository(customerCount, months, overdraft);
            accounts.AssignRandomTrends(goodBalance, badBalance, variation, random);

            // Print summary of accounts  
            Console.WriteLine();
            Console.WriteLine("{0} customers, {1} months in each account", customerCount, months);

            // Execute sequential and parallel versions, print timings
            Console.WriteLine();
            SampleUtilities.TimedRun(
                () => { UpdatePredictionsSequential(accounts); return customerCount; }, "Sequential");
            SampleUtilities.TimedRun(
                () => { UpdatePredictionsParallel(accounts); return customerCount; }, "  Parallel");
            SampleUtilities.TimedRun(
                () => { UpdatePredictionsPlinq(accounts); return customerCount; }, "     PLINQ");

            // Print a few accounts including predictions and warnings
            accounts.Print(rows, months - cols, cols); // print the last few months

            Console.WriteLine("\nRun complete... press enter to finish.");
            Console.ReadLine();
        }