Ejemplo n.º 1
0
        public decimal Predict(PredictorOption option)
        {
            // step 0: create a value table of X(ordinal month value) and Y(rate value for given month)
            int[]     xValues = Enumerable.Range(option.FromDate.Month, option.ToDate.Month).ToArray();
            decimal[] yValues = GetY(option.FromDate, option.ToDate, xValues, option.FromCurrency, option.ToCurrency);

            // step 1: number of values
            int n = xValues.Length;

            // step 2: X*X, X*Y
            decimal[] xx = CalXX(xValues);
            decimal[] xy = CalXY(xValues, yValues);

            // step 3: ΣX, ΣY, Σ(X*X) and Σ(X*Y)
            int     sumX  = xValues.Sum();
            decimal sumY  = yValues.Sum();
            decimal sumXX = xx.Sum();
            decimal sumXY = xy.Sum();

            // step 4
            decimal slope = CalSlope(n, sumX, sumY, sumXX, sumXY);

            // step 5
            decimal intercept = CalIntercept(sumY, slope, sumX, n);

            // step 6
            // 15/1/2017 => x = 13
            decimal rate = CalRate(intercept, slope, xValues.Length + 1);

            return(rate);
        }
Ejemplo n.º 2
0
        public void Test1()
        {
            Random randNum = new Random();

            int[] yValues = Enumerable.Repeat(0, 12).Select(i => randNum.Next(1, 30)).ToArray();

            Queue <ExchangeRate> rateQueue = new Queue <ExchangeRate>();

            foreach (int yValue in yValues)
            {
                ExchangeRate r = new ExchangeRate();
                r.rates = new JObject(new JProperty("VND", yValue));

                rateQueue.Enqueue(r);
            }

            Mock <IExchangeRateDataReader> readerMock = new Mock <IExchangeRateDataReader>();

            readerMock.Setup(reader => reader.Read(It.IsAny <DateTime>(), It.IsAny <string>())).Returns(rateQueue.Dequeue);

            PredictorOption predictorOption = new PredictorOption
            {
                FromCurrency = "USD",
                ToCurrency   = "VND",
                FromDate     = new DateTime(year: 2016, month: 1, day: 15),
                ToDate       = new DateTime(year: 2016, month: 12, day: 15),
                PredictDate  = new DateTime(year: 2017, month: 1, day: 15)
            };

            IPredictor predictor = new Predictor(readerMock.Object);
            decimal    rate      = predictor.Predict(predictorOption);

            Assert.True(rate > 0);
        }
Ejemplo n.º 3
0
        private string HandlePredictCommand(PredictOption opts)
        {
            JObject       currencies    = _reader.ReadCurrencies();
            StringBuilder stringBuilder = new StringBuilder();

            if (!currencies.ContainsKey(opts.From))
            {
                stringBuilder.AppendLine($"Currency code {opts.From} is invalid.");
            }

            if (!currencies.ContainsKey(opts.To))
            {
                stringBuilder.AppendLine($"Currency code {opts.To} is invalid.");
            }

            if (stringBuilder.Length > 0)
            {
                throw new ArgumentException(stringBuilder.ToString());
            }

            PredictorOption option = new PredictorOption
            {
                FromDate     = DateTime.ParseExact("15/01/2016", "dd/MM/yyyy", CultureInfo.InvariantCulture),
                ToDate       = DateTime.ParseExact("15/12/2016", "dd/MM/yyyy", CultureInfo.InvariantCulture),
                PredictDate  = DateTime.ParseExact("15/01/2017", "dd/MM/yyyy", CultureInfo.InvariantCulture),
                FromCurrency = opts.From,
                ToCurrency   = opts.To
            };

            decimal rate = _predictor.Predict(option);

            string result = $"The predicted currency exchange from {opts.From} to {opts.To} for 15/1/2017 is {rate}";

            return(result);
        }