/// <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); }
public App() { int i = 3; var RawData = DataPreparer.Splicer(i); Constants.Item item = Constants.Item.GetItem(RawData, 4000); Constants.RawData raw = Constants.Item.GetRawData(); //series.InitializeComponent(); MainWindow window = new MainWindow(); window.InitializeComponent(); }
/// <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); }
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(); }
/// <summary> /// Trims the raw data object length to desired length, leaves original length - n /// </summary> /// <param name="rawData"></param> /// <param name="ListLength">Datapoints to remove</param> /// <returns></returns> public static void Trimmer(Constants.RawData rawData, int ListLength) { int BQcount = rawData.BuyQuantity.Count - ListLength; rawData.BuyQuantity.RemoveRange(ListLength, BQcount); int BPcount = rawData.BuyPrice.Count - ListLength; rawData.BuyPrice.RemoveRange(ListLength, BPcount); int SQcount = rawData.SellQuantity.Count - ListLength; rawData.SellQuantity.RemoveRange(ListLength, SQcount); int SPcount = rawData.SellPrice.Count - ListLength; rawData.SellPrice.RemoveRange(ListLength, SPcount); }
/// <summary> /// Raw data file reader of item /// </summary> /// <param name="a">Takes input of which item from Constants.Id[]</param> /// <returns>returns list of each of the four parameters</returns> public static Constants.RawData Splicer(int a) { var sprice = new List <int>(); var squant = new List <int>(); var bprice = new List <int>(); var bquant = new List <int>(); Constants.RawData rawdata = new Constants.RawData(sprice, squant, bprice, bquant); int id = Constants.id[a]; string file = @"C:\Users\iTzEinstein118Xx\Documents\ItemDataOutput\" + id + ".txt"; using (TextReader tr = File.OpenText(file)) { int linecount = File.ReadLines(file).Count(); for (int y = 0; y < linecount; y++) { string line = tr.ReadLine(); //{ "buy price", "buy quantity", "sell price", "sell quantity" }; foreach (string match in Constants.StringToMatch) { if (line.Contains(match)) { string[] bits = line.Split(' '); int j = bits.Length; try { //parsing bits, the number is always at the end of the line int num = Int32.Parse(bits[j - 1]); //add the number to dataset //use GetRawData method, Constants.RawData //list title as string, returning the list itself List <int> workingdata = rawdata.GetRawData(match); workingdata.Add(num); } catch (FormatException) { Console.WriteLine("Bad Format"); } } } } return(rawdata); } }
/// <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); }
public TimeSeries() { InitializeComponent(); Constants.RawData data = Constants.Item.GetRawData(); int n = Constants.Item.GetCount(); var Ewma = DataPreparer.Weighter(data, 0.5); YFormatter = value => value.ToString("C"); List <double> BuyQ = data.BuyQuantity.Select <int, double>(i => i).ToList(); List <double> BuyP = data.BuyPrice.Select <int, double>(i => i).ToList(); List <double> SellQ = data.SellQuantity.Select <int, double>(i => i).ToList(); List <double> SellP = data.SellPrice.Select <int, double>(i => i).ToList(); for (int i = 0; i < BuyQ.Count(); i++) { EWMAPrice.Add(new ObservablePoint(i, Ewma._strdBuyPrice.ElementAt(i))); Price.Add(new ObservablePoint(i, BuyP.ElementAt(i))); } DataContext = this; }
/// <summary> /// Fits a line in a univariate analysis to data /// </summary> /// <param name="data">Input</param> /// <param name="n">Range of Input</param> /// <returns>Fit Param's, first column is gradients</returns> public static double[,] FitLin(Constants.RawData data, int n) { //S(p)=BuyQuantity = b + m*SellPrice //D(p)=SellQuantity = b - m*BuyPrice double[,] Params = new double[3, 2]; var BQuant = data.BuyQuantity; var BPrice = data.BuyPrice; var SQuant = data.SellQuantity; var SPrice = data.SellPrice; //array location 0, 0 is mean value var InfoObject = DataPreparer.DataInfo(); 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 = GradSolver(BPrice, SQuant, n, BPMean, SQMean); var GradSup = GradSolver(SPrice, BQuant, n, SPMean, BQMean); //return B0/intercept for demand and supply var DemInt = IntSolver(GradDem, SQMean, BPMean); var SupInt = IntSolver(GradSup, BQMean, SPMean); //create predictions of data and add to predictions lists var PredDem = Predictor(BPrice, GradDem, DemInt); var PredSup = Predictor(SPrice, GradSup, SupInt); //return error on predicitons var DemErr = ErrorSolver(SQuant, PredDem); var SupErr = ErrorSolver(BQuant, PredSup); //add to Params and return Params[0, 0] = Math.Round(GradDem); Params[0, 1] = Math.Round(GradSup); Params[1, 0] = Math.Round(DemInt); Params[1, 1] = Math.Round(SupInt); Params[2, 0] = Math.Round(DemErr); Params[2, 1] = Math.Round(SupErr); Console.WriteLine(Params[2, 1]); return(Params); }
/// <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); }