Beispiel #1
0
        /// <summary>
        /// Raw Data Z-Score standardiser
        /// </summary>
        /// <param name="rawData">Raw data from item</param>
        /// <returns>Population standardised score of each data</returns>
        public static Constants.StrdData Z_Score(Constants.RawData rawData)
        {
            var strdbquant = new List <double>();
            var strdbprice = new List <double>();
            var strdsprice = new List <double>();
            var strdsquant = new List <double>();

            List <double>[] AccessList = new List <double>[] { strdbprice, strdbquant, strdsprice, strdsquant };
            var             DataLists  = new List <List <int> > {
                rawData.BuyPrice, rawData.BuyQuantity, rawData.SellPrice, rawData.SellQuantity
            };

            Constants.StrdData z_score = new Constants.StrdData(strdbprice, strdbquant, strdsprice, strdsquant);

            for (int j = 0; j < AccessList.Count(); j++)
            {
                var WorkingList = new List <int>();
                WorkingList.AddRange(DataLists.ElementAt(j));
                double Mean  = WorkingList.Average();
                int    Count = WorkingList.Count();
                double Prev  = 0;
                double Sum   = 0;

                for (int i = 0; i < Count; i++)
                {
                    //current is always the next element in the list
                    double Current = WorkingList.ElementAt(i);

                    double Working = (Current - Mean);
                    double Ans     = Math.Pow(Working, 2);
                    Sum  = Prev + Ans;
                    Prev = Ans;
                }
                //gets the standard deviation
                double StrdDev = Math.Sqrt(Sum / Count);
                double Strd    = 0;

                //standardises the input list
                foreach (int i in WorkingList)
                {
                    Strd = (i - Mean) / StrdDev;
                    AccessList.ElementAt(j).Add(Strd);
                }
                WorkingList.Clear();
            }
            z_score._strdBuyPrice  = strdbprice;
            z_score._strdBuyQuant  = strdbquant;
            z_score._strdSellPrice = strdsprice;
            z_score._strdSellQuant = strdsquant;
            return(z_score);
        }
Beispiel #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();
        }
Beispiel #3
0
        /// <summary>
        /// weighting function for non-standardised data (raw data)
        /// </summary>
        /// <param name="rawdata">Raw data object</param>
        /// <param name="Alpha">higher alpha discards quicker</param>
        /// <returns>Pure weighted data</returns>
        public static Constants.StrdData Weighter(Constants.RawData rawdata, double Alpha)
        {
            var strdbquant = new List <double>();
            var strdbprice = new List <double>();
            var strdsprice = new List <double>();
            var strdsquant = new List <double>();

            List <double> BuyQ  = rawdata.BuyQuantity.Select <int, double>(i => i).ToList();
            List <double> BuyP  = rawdata.BuyPrice.Select <int, double>(i => i).ToList();
            List <double> SellQ = rawdata.SellQuantity.Select <int, double>(i => i).ToList();
            List <double> SellP = rawdata.SellPrice.Select <int, double>(i => i).ToList();

            List <double>[]    AccessList = new List <double>[] { strdbprice, strdbquant, strdsprice, strdsquant };
            List <double>[]    DataLists  = new List <double>[] { BuyP, BuyQ, SellP, SellQ };
            Constants.StrdData ewma       = new Constants.StrdData(strdbprice, strdbquant, strdsprice, strdsquant);

            for (int j = 0; j < DataLists.Count(); j++)
            {
                double[]      WeightedArray = new double[DataLists.ElementAt(j).Count()];
                List <double> CurrentList   = DataLists.ElementAt(j);

                double X_init = CurrentList.ElementAt(0);
                //first X value is not weighted, used to initiallise weighting
                WeightedArray[0] = X_init;
                double Weight_prev    = X_init;
                double Weight_Current = 0;

                for (int i = 0; i < CurrentList.Count(); i++)
                {
                    Weight_Current   = Alpha * CurrentList.ElementAt(i) + (1 - Alpha) * Weight_prev;
                    WeightedArray[i] = Weight_Current;
                    Weight_prev      = Weight_Current;
                }

                AccessList[j].AddRange(WeightedArray);
            }

            ewma._strdBuyPrice  = strdbprice;
            ewma._strdBuyQuant  = strdbquant;
            ewma._strdSellPrice = strdsprice;
            ewma._strdSellQuant = strdsquant;

            return(ewma);
        }
Beispiel #4
0
        /// <summary>
        ///Weight standardised data
        /// </summary>
        /// <param name="strdData">Standard Data object</param>
        /// <param name="Alpha">Higher alpha discards data faster</param>
        /// <returns>Weighted, Standardised data</returns>
        public static Constants.StrdData EWMA(Constants.StrdData strdData, double Alpha)
        {
            var strdbquant = new List <double>();
            var strdbprice = new List <double>();
            var strdsprice = new List <double>();
            var strdsquant = new List <double>();

            List <double>[]    AccessList = new List <double>[] { strdbprice, strdbquant, strdsprice, strdsquant };
            List <double>[]    DataLists  = new List <double>[] { strdData._strdBuyPrice, strdData._strdBuyQuant, strdData._strdSellPrice, strdData._strdSellQuant };
            Constants.StrdData ewma       = new Constants.StrdData(strdbprice, strdbquant, strdsprice, strdsquant);

            for (int j = 0; j < AccessList.Count(); j++)
            {
                double[]      WeightedList = new double[DataLists.ElementAt(j).Count()];
                List <double> CurrentList  = DataLists.ElementAt(j);

                double X_init = CurrentList.ElementAt(0);
                //first X value is not weighted, used to initiallise weighting
                WeightedList[0] = X_init;
                double Weight_prev    = X_init;
                double Weight_Current = 0;

                for (int i = 0; i < CurrentList.Count(); i++)
                {
                    Weight_Current  = Alpha * CurrentList.ElementAt(i) + (1 - Alpha) * Weight_prev;
                    WeightedList[i] = Weight_Current;
                    Weight_prev     = Weight_Current;
                }

                AccessList[j].AddRange(WeightedList);
            }

            ewma._strdBuyPrice  = strdbprice;
            ewma._strdBuyQuant  = strdbquant;
            ewma._strdSellPrice = strdsprice;
            ewma._strdSellQuant = strdsquant;

            return(ewma);
        }
Beispiel #5
0
        /// <summary>
        /// Fit to a standardised data set then return de-standardized values
        /// </summary>
        /// <param name="strdData"></param>
        /// <param name="n">Dataset length</param>
        /// <returns></returns>
        public static double[,] StrdFitLin(Constants.StrdData strdData, int n)
        {
            //S(p)=SellQuantity = b + m*BuyPrice
            //D(p)=BuyQuantity = b + m*SellPrice
            double[,] Params = new double[3, 2];
            var BQuant = strdData._strdBuyQuant;
            var BPrice = strdData._strdBuyPrice;
            var SQuant = strdData._strdSellQuant;
            var SPrice = strdData._strdSellPrice;

            var InfoObject = DataPreparer.StrdDataInfo(strdData);
            //array location 0, 0 is mean value
            var BPMean = (double)InfoObject.BuyPriceInfo[0, 0];
            var BQMean = (double)InfoObject.BuyQuantInfo[0, 0];
            var SPMean = (double)InfoObject.SellPriceInfo[0, 0];
            var SQMean = (double)InfoObject.SellQuantInfo[0, 0];

            //return B1/Grad for demand and supply
            var GradDem = StrdGradSolver(SPrice, BQuant, n, SPMean, BQMean);
            var GradSup = StrdGradSolver(BPrice, SQuant, n, BPMean, SQMean);
            //return B0/intercept for demand and supply
            var DemInt = IntSolver(GradDem, BQMean, SPMean);
            var SupInt = IntSolver(GradSup, SQMean, BPMean);
            //create predictions of data and add to predictions lists
            var PredDem = StrdPredictor(SPrice, GradSup, SupInt);
            var PredSup = StrdPredictor(BPrice, GradDem, DemInt);
            //return error on predicitons
            var DemErr = StrdErrorSolver(BQuant, PredDem);
            var SupErr = StrdErrorSolver(SQuant, PredSup);

            //add to Params and return
            Params[0, 0] = GradDem;
            Params[0, 1] = GradSup;
            Params[1, 0] = DemInt;
            Params[1, 1] = SupInt;
            Params[2, 0] = DemErr;
            Params[2, 1] = SupErr;
            return(Params);
        }
Beispiel #6
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);
        }