コード例 #1
0
        /// <summary>
        /// Calculates the Correlation Coefficient (pearson's)
        /// </summary>
        /// <param name="x">independent variable</param>
        /// <param name="x_avg">mean</param>
        /// <param name="y">dependent variable</param>
        /// <param name="y_avg">mean</param>
        /// <returns>Correlation Coefficient</returns>
        public static double[] CorrCoeff(Constants.RawData data, Constants.DataInfo info)
        {
            List <int> demquant     = data.SellQuantity;
            List <int> supquant     = data.BuyQuantity;
            List <int> demprice     = data.BuyPrice;
            List <int> supprice     = data.SellPrice;
            double     demprice_avg = (double)info.BuyPriceInfo[0, 0];
            double     supprice_avg = (double)info.SellPriceInfo[0, 0];
            double     demquant_avg = (double)info.SellQuantInfo[0, 0];
            double     supquant_avg = (double)info.BuyQuantInfo[0, 0];

            double[] Corrs       = new double[2];
            double   nominator   = 0;
            double   denominator = 0;
            double   Denomxx     = 0;
            double   Denomyy     = 0;

            for (int i = 0; i < demquant.Count(); i++)
            {
                nominator += (demprice.ElementAt(i) - demprice_avg) * (demquant.ElementAt(i) - demquant_avg);
                Denomxx   += Math.Pow(demprice.ElementAt(i) - demprice_avg, 2);
                Denomyy   += Math.Pow(demquant.ElementAt(i) - demquant_avg, 2);
            }
            denominator = Math.Sqrt(Denomxx * Denomyy);
            Corrs[0]    = nominator / denominator;
            for (int i = 0; i < demquant.Count(); i++)
            {
                nominator += (supprice.ElementAt(i) - supprice_avg) * (supquant.ElementAt(i) - supquant_avg);
                Denomxx   += Math.Pow(supprice.ElementAt(i) - supprice_avg, 2);
                Denomyy   += Math.Pow(supquant.ElementAt(i) - supquant_avg, 2);
            }
            denominator = Math.Sqrt(Denomxx * Denomyy);
            Corrs[1]    = nominator / denominator;
            return(Corrs);
        }
コード例 #2
0
        public static void Main(string[] args)
        {
            int i = 3;

            Console.WriteLine("Operation on dataset: Item ({0})...", Constants.id[i]);
            //get initial data from files
            var RawData = DataPreparer.Splicer(i);

            Console.WriteLine("Getting data from files");
            //subset length, total set size is 4*n (buy/sell quant, buy/sell price)
            //trim dataset to n before singleton implemented!
            int n = 4000;

            Console.WriteLine("trimmng datset...");
            DataPreparer.Trimmer(RawData, n);
            //get item specific data for raw dataset as a singleton class
            Console.WriteLine("Getting item...");
            Constants.Item    item = Constants.Item.GetItem(RawData, n);
            Constants.RawData Data = Constants.Item.GetRawData();
            //standardised dataset as well as weighted and standardised weighted data
            Console.WriteLine("Standardising dataset");
            Constants.StrdData strdData = DataPreparer.Z_Score(Data);
            //LSR fitting solutions to each set (standardised & non-standardised)
            Console.WriteLine("Fitting function parameters");
            double[,] FitEq     = FittingFunctions.FitLin(Data, n);
            double[,] StrdFitEq = FittingFunctions.StrdFitLin(strdData, n);
            Constants.DataInfo Info = DataPreparer.DataInfo();
            Console.WriteLine("Getting model information");
            double[] Corrs = ModelInfo.CorrCoeff(Data, Info);
            //Get prediction lists and convert to List<int> for StrdDev function
            var        _DemPreds = FittingFunctions.Predictor(Data.BuyPrice, FitEq[0, 0], FitEq[1, 0]);
            List <int> DemPreds  = _DemPreds.Select(x => (int)x).ToList();
            var        _SupPreds = FittingFunctions.Predictor(Data.SellPrice, FitEq[0, 1], FitEq[1, 1]);
            List <int> SupPreds  = _SupPreds.Select(x => (int)x).ToList();

            //Standard deviations of data, standard deviations of predicted data & correlations
            double[] FitData =
            {
                ModelInfo.StrdDev(Data.SellQuantity, Info.SellQuantInfo, n),
                ModelInfo.StrdDev(Data.BuyQuantity,  Info.BuyQuantInfo,  n),
                ModelInfo.StrdDev(DemPreds,          Info.BuyQuantInfo,  n),
                ModelInfo.StrdDev(SupPreds,          Info.BuyQuantInfo,  n),
                ModelInfo.RMSE(FitEq[2,                              0], n),ModelInfo.RMSE(FitEq[2,1], n),
                Corrs[0],                            Corrs[1]
            };
            Console.WriteLine("Printing results to files...");
            DataWriter.FileMaker(Info, i, FitEq, StrdFitEq, FitData);
            Console.Write("Check the desktop!");
            Console.ReadLine();
        }
コード例 #3
0
        /// <summary>
        /// Item data, Mean, Min, Max, Median and Set Length
        /// </summary>
        /// <param name="rawdata">Input standard data object</param>
        /// <returns>DataInfo object, Mean, Min, Max, Median and Set Length</returns>
        public static Constants.DataInfo StrdDataInfo(Constants.StrdData strdData)
        {
            Object[,] spriceinfo = new Object[, ] {
                { 0, "Mean" }, { 0, "Min" }, { 0, "Max" }, { 0, "Median" }, { 0, "Set Length" }
            };
            Object[,] squantinfo = new Object[, ] {
                { 0, "Mean" }, { 0, "Min" }, { 0, "Max" }, { 0, "Median" }, { 0, "Set Length" }
            };
            Object[,] bpriceinfo = new Object[, ] {
                { 0, "Mean" }, { 0, "Min" }, { 0, "Max" }, { 0, "Median" }, { 0, "Set Length" }
            };
            Object[,] bquantinfo = new Object[, ] {
                { 0, "Mean" }, { 0, "Min" }, { 0, "Max" }, { 0, "Median" }, { 0, "Set Length" }
            };
            List <double> sprice = new List <double>();
            List <double> squant = new List <double>();
            List <double> bprice = new List <double>();
            List <double> bquant = new List <double>();

            Constants.DataInfo datainfo = new Constants.DataInfo(spriceinfo, squantinfo, bpriceinfo, bquantinfo);

            bpriceinfo[0, 0] = strdData._strdBuyPrice.Average();
            bpriceinfo[1, 0] = strdData._strdBuyPrice.Min();
            bpriceinfo[2, 0] = strdData._strdBuyPrice.Max();
            bpriceinfo[4, 0] = strdData._strdBuyPrice.Count();
            bprice.AddRange(strdData._strdBuyPrice);
            bprice.Sort();
            int i = (bprice.Count() + 1) / 2;

            bpriceinfo[3, 0] = bprice.ElementAt(i);

            spriceinfo[0, 0] = strdData._strdSellPrice.Average();
            spriceinfo[1, 0] = strdData._strdSellPrice.Min();
            spriceinfo[2, 0] = strdData._strdSellPrice.Max();
            spriceinfo[4, 0] = strdData._strdSellPrice.Count();
            sprice.AddRange(strdData._strdSellPrice);
            sprice.Sort();
            int j = (sprice.Count() + 1) / 2;

            spriceinfo[3, 0] = sprice.ElementAt(j);

            bquantinfo[0, 0] = strdData._strdBuyQuant.Average();
            bquantinfo[1, 0] = strdData._strdBuyQuant.Min();
            bquantinfo[2, 0] = strdData._strdBuyQuant.Max();
            bquantinfo[4, 0] = strdData._strdBuyQuant.Count();
            bquant.AddRange(strdData._strdBuyQuant);
            bquant.Sort();
            int k = (bquant.Count() + 1) / 2;

            bquantinfo[3, 0] = bquant.ElementAt(k);

            squantinfo[0, 0] = strdData._strdSellQuant.Average();
            squantinfo[1, 0] = strdData._strdSellQuant.Min();
            squantinfo[2, 0] = strdData._strdSellQuant.Max();
            squantinfo[4, 0] = strdData._strdSellQuant.Count();
            squant.AddRange(strdData._strdSellQuant);
            squant.Sort();
            int l = (squant.Count() + 1) / 2;

            squantinfo[3, 0]       = squant.ElementAt(l);
            datainfo.BuyPriceInfo  = bpriceinfo;
            datainfo.BuyQuantInfo  = bquantinfo;
            datainfo.SellQuantInfo = squantinfo;
            datainfo.SellPriceInfo = spriceinfo;

            return(datainfo);
        }
コード例 #4
0
        /// <summary>
        /// Item data, Mean, Min, Max, Median and Set Length
        /// </summary>
        /// <param name="rawdata">Input data object, with four lists</param>
        /// <returns>DataInfo object, Mean, Min, Max, Median and Set Length</returns>
        public static Constants.DataInfo DataInfo()
        {
            Constants.RawData rawdata = Constants.Item.GetRawData();
            Object[,] spriceinfo = new Object[, ] {
                { 0, "Mean" }, { 0, "Min" }, { 0, "Max" }, { 0, "Median" }, { 0, "Set Length" }
            };
            Object[,] squantinfo = new Object[, ] {
                { 0, "Mean" }, { 0, "Min" }, { 0, "Max" }, { 0, "Median" }, { 0, "Set Length" }
            };
            Object[,] bpriceinfo = new Object[, ] {
                { 0, "Mean" }, { 0, "Min" }, { 0, "Max" }, { 0, "Median" }, { 0, "Set Length" }
            };
            Object[,] bquantinfo = new Object[, ] {
                { 0, "Mean" }, { 0, "Min" }, { 0, "Max" }, { 0, "Median" }, { 0, "Set Length" }
            };
            List <int> sprice = new List <int>();
            List <int> squant = new List <int>();
            List <int> bprice = new List <int>();
            List <int> bquant = new List <int>();

            Constants.DataInfo datainfo = new Constants.DataInfo(spriceinfo, squantinfo, bpriceinfo, bquantinfo);

            bpriceinfo[0, 0] = rawdata.BuyPrice.Average();
            bpriceinfo[1, 0] = rawdata.BuyPrice.Min();
            bpriceinfo[2, 0] = rawdata.BuyPrice.Max();
            bpriceinfo[4, 0] = rawdata.BuyPrice.Count();
            bprice.AddRange(rawdata.BuyPrice);
            bprice.Sort();
            int i = (bprice.Count() + 1) / 2;

            bpriceinfo[3, 0] = bprice.ElementAt(i);

            spriceinfo[0, 0] = rawdata.SellPrice.Average();
            spriceinfo[1, 0] = rawdata.SellPrice.Min();
            spriceinfo[2, 0] = rawdata.SellPrice.Max();
            spriceinfo[4, 0] = rawdata.SellPrice.Count();
            sprice.AddRange(rawdata.SellPrice);
            sprice.Sort();
            int j = (sprice.Count() + 1) / 2;

            spriceinfo[3, 0] = sprice.ElementAt(j);

            bquantinfo[0, 0] = rawdata.BuyQuantity.Average();
            bquantinfo[1, 0] = rawdata.BuyQuantity.Min();
            bquantinfo[2, 0] = rawdata.BuyQuantity.Max();
            bquantinfo[4, 0] = rawdata.BuyQuantity.Count();
            bquant.AddRange(rawdata.BuyQuantity);
            bquant.Sort();
            int k = (bquant.Count() + 1) / 2;

            bquantinfo[3, 0] = bquant.ElementAt(k);

            squantinfo[0, 0] = rawdata.SellQuantity.Average();
            squantinfo[1, 0] = rawdata.SellQuantity.Min();
            squantinfo[2, 0] = rawdata.SellQuantity.Max();
            squantinfo[4, 0] = rawdata.SellQuantity.Count();
            squant.AddRange(rawdata.SellQuantity);
            squant.Sort();
            int l = (squant.Count() + 1) / 2;

            squantinfo[3, 0]       = squant.ElementAt(l);
            datainfo.BuyPriceInfo  = bpriceinfo;
            datainfo.BuyQuantInfo  = bquantinfo;
            datainfo.SellQuantInfo = squantinfo;
            datainfo.SellPriceInfo = spriceinfo;

            return(datainfo);
        }
コード例 #5
0
        /// <summary>
        /// Create a Human Readable File on the desktop. Including analysis of instances and fitted functions.
        /// </summary>
        /// <param name="info">RawData info object</param>
        /// <param name="n">item ID</param>
        /// <param name="RawFit">Fitted Parameters for rawdata</param>
        /// <param name="StrdFit">Fitted Parameters for Standardised Data</param>
        /// <param name="WeightedFit">Fitted Parameters for EWMA data</param>
        /// <param name="WeightedFit">Fitted Parameters for standardised EWMA data</param>
        public static void FileMaker(Constants.DataInfo info, int n, double[,] RawFit, double[,] StrdFit, double[] ModelInfo)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }
            if (RawFit == null)
            {
                throw new ArgumentNullException(nameof(RawFit));
            }
            if (StrdFit == null)
            {
                throw new ArgumentNullException(nameof(StrdFit));
            }
            if (ModelInfo == null)
            {
                throw new ArgumentNullException(nameof(ModelInfo));
            }

            string FileName = @"c: \Users\iTzEinstein118Xx\Desktop\AnalysisData" + Constants.id[n] + ".txt";

            try {
                using (StreamWriter sw = new StreamWriter(FileName, false))
                {
                    sw.WriteLine("Human Readable File: {0}\n", Constants.id[n]);
                    sw.WriteLine("Info for BuyQuant\n");
                    sw.WriteLine("Analysis of Instances:\n");
                    foreach (object inf in info.BuyQuantInfo)
                    {
                        string bqinf = Convert.ToString(inf);
                        sw.WriteLine(" " + bqinf + "\n");
                        sw.Write("\n");
                    }

                    sw.WriteLine("Info for BuyPrice");
                    sw.WriteLine("Analysis of Instances:\n");
                    foreach (object inf in info.BuyPriceInfo)
                    {
                        string binf = Convert.ToString(inf);
                        sw.WriteLine(" " + binf + "\n");
                    }

                    sw.WriteLine("Info for SellQuant");
                    sw.WriteLine("Analysis of Instances:\n");
                    foreach (object inf in info.SellQuantInfo)
                    {
                        string pqinfo = Convert.ToString(inf);
                        sw.WriteLine(" " + pqinfo + "\n");
                    }

                    sw.WriteLine("Info for SellPrice");
                    sw.WriteLine("Analysis of Instances:\n");
                    foreach (object inf in info.SellPriceInfo)
                    {
                        string sinf = Convert.ToString(inf);
                        sw.WriteLine(" " + sinf + "\n");
                    }

                    sw.WriteLine("Demand Function: {0} + {1}p \r R-Squared: {2}\n", RawFit[1, 0], RawFit[0, 0], RawFit[2, 0]);
                    sw.WriteLine("Supply Function: {0} + {1}p \r R-Squared: {2}\n", RawFit[1, 1], RawFit[0, 1], RawFit[2, 1]);
                    sw.WriteLine("\n");
                    sw.WriteLine("Standard Demand Function: {0} + {1}p \r R-Squared: {2}\n", StrdFit[1, 0], StrdFit[0, 0], StrdFit[2, 0]);
                    sw.WriteLine("Standard Demand Function: {0} + {1}p \r R-Squared: {2}\n", StrdFit[1, 1], StrdFit[0, 1], StrdFit[2, 1]);
                    sw.WriteLine("Raw Data Info: \n");
                    sw.WriteLine("Standard Dev, Demand: {0}", ModelInfo[0]);
                    sw.WriteLine("Standard Dev, Supply: {0}", ModelInfo[1]);
                    sw.WriteLine("Correlation, Demand: {0}", ModelInfo[6]);
                    sw.WriteLine("Correlation, Supply: {0}", ModelInfo[7]);
                    sw.WriteLine("Fitted Data Info: \n");
                    sw.WriteLine("Standard Dev, Fitted Demand: {0}", ModelInfo[2]);
                    sw.WriteLine("Standard Dev, Fitted Supply: {0}", ModelInfo[3]);
                    sw.WriteLine("RMSE, Demand: {0}", ModelInfo[4]);
                    sw.WriteLine("RMSE, Supply: {0}", ModelInfo[5]);
                    Console.Write("File Printed!\n");
                }
            }
            finally
            {
            }
        }