/*public double Predict(DateTime date) { * InitData(); * InitNeuralNetwork(Data.First().Count); * var currentData = DataForNeuralNetworkCollector.Get(date, TimeSpan.FromDays(1)); * currentData.Data = DataForNeuralNetworkCollector.Normalize(currentData.Data, ExpectedValuesLocal, DispersionsLocal); * var result = Lstm.Run(new Vector[] { currentData.Data }); * return DataForNeuralNetworkCollector * .Denormalize(result.Last(), * new double[] { ExpectedValuesLocal.Values.Last() }, * new double[] { DispersionsLocal.Values.Last() }) * .Values.First(); * }*/ public List <PredictionOfCurrencyLearnResult> Test() { InitData(); InitLstm(); var result = new List <PredictionOfCurrencyLearnResult>(); for (var i = 0; i < DataManager.TestData.Data.Count - 1; i += Chunk) { var chunk = Chunk; if (i + Chunk > DataManager.TestData.Data.Count - 1) { chunk = DataManager.TestData.Data.Count - 1 - i; } var input = new Vector[chunk]; var ideal = new Vector[chunk]; for (var j = i; j < i + chunk && j < DataManager.TestData.Data.Count - 1; j++) { input[j - i] = DataManager.TestData[j].Vector; ideal[j - i] = new double[] { DataManager.TestData[j + 1].Vector.Values.Last() }; } var output = Lstm.Run(input); for (var j = 0; j < chunk; j++) { var error = ((output[j] - ideal[j]) ^ 2) * 0.5; result.Add(new PredictionOfCurrencyLearnResult { Date = DataManager.LearnData.Data[i + j].Date, Error = error, Output = DataManager.ConvertOutput(output[j]), Ideal = DataManager.ConvertOutput(ideal[j]), Input = DataManager.ConvertInput(input[j]), }); } } return(result); }
protected void InitLstm() { if (Lstm == null) { Lstm = new Lstm(Collectors.Count, 1, LearnParameters.Parameters, LearnParameters.CellParameters); if (LoadNetwork) { Lstm.Load(NeuralNetworkName); } } }
protected List <PredictionOfCurrencyLearnResult> FitLstm(bool saveLearnResult = false) { InitData(); InitLstm(); var result = new List <PredictionOfCurrencyLearnResult>(); for (var i = 0; i < DataManager.LearnData.Data.Count - 1; i += Chunk) { var chunk = Chunk; if (i + Chunk > DataManager.LearnData.Data.Count - 1) { chunk = DataManager.LearnData.Data.Count - 1 - i; } var input = new Vector[chunk]; var ideal = new Vector[chunk]; for (var j = i; j < i + chunk && j < DataManager.LearnData.Data.Count - 1; j++) { input[j - i] = DataManager.LearnData[j].Vector; ideal[j - i] = new double[] { DataManager.LearnData[j + 1].Vector.Values.Last() }; } var(output, error) = Lstm.Learn(input, ideal); for (var j = 0; j < chunk; j++) { result.Add(new PredictionOfCurrencyLearnResult { Date = DataManager.LearnData.Data[i + j].Date, Error = error[j], Output = DataManager.ConvertOutput(output[j]), Ideal = DataManager.ConvertOutput(ideal[j]), Input = DataManager.ConvertInput(input[j]), }); } } Lstm.Save(NeuralNetworkName); if (saveLearnResult) { SaveLearnProgress(result); } return(result); }
static async Task Main(string[] args) { IStockDataService yahooService = new YahooService(); _priceManager = new PriceManager(yahooService); var prices = CsvExtensions.ReadCsv( @"Data\\all_stocks_5yr.csv", new Dictionary <string, string> { { "date", "StartTime" }, { "open", "Open" }, { "close", "Close" }, { "high", "High" }, { "low", "Low" }, { "volume", "Volume" }, { "Name", "Symbol" } }); var aalPrice = prices.Where(c => c.Symbol.Equals("AAL")).SortByStartTime(); var open = aalPrice.Select(c => (double)c.Open).ToArray(); var high = aalPrice.Select(c => (double)c.High).ToArray(); var low = aalPrice.Select(c => (double)c.Low).ToArray(); var close = aalPrice.Select(c => (double)c.Close).ToArray(); var volume = aalPrice.Select(c => c.Volume).ToArray(); var lstm = new Lstm(open, high, low, close, volume); lstm.Print(); var scale = lstm.CalculateScale(); var min = lstm.CalculateMin(); var stochasticOsc = new StochasticOsc(); var sData = stochasticOsc.Run(GetData()); var plt = new Plot(600, 400); var slow = sData.Select(c => c.SlowValue).ToArray(); var vals = sData.Select(c => c.Value).ToArray(); //var xs = sData.Select(p => p.StartTime.ToOADate()).ToArray(); plt.PlotSignalXY(sData.Select(p => p.StartTime.ToOADate()).ToArray(), slow, label: "slow", color: Color.Red, lineWidth: 2, lineStyle: LineStyle.Solid, markerSize: 0); plt.PlotSignalXY(sData.Select(p => p.StartTime.ToOADate()).ToArray(), vals, label: "fast", color: Color.Black, lineWidth: 2, lineStyle: LineStyle.Solid); plt.Title("IBM Stochastic"); plt.YLabel("Stochastic Unit"); plt.XLabel("Date"); plt.Ticks(dateTimeX: true); //plt.Legend(); plt.AxisBounds(minY: 0, maxY: 100); plt.AxisAuto(verticalMargin: 0.01); plt.Add(new PlottableHLine(20, Color.Black, 1, "", false, 20, 20, LineStyle.Solid)); plt.Add(new PlottableHLine(80, Color.Black, 1, "", false, 80, 80, LineStyle.Solid)); plt.SaveFig("IBM Slow Stochastic Chart.png"); return; //var list = stochasticService.Run(sData); //await TargilAsync(); //var tickerManager = new TickerManager(yahooService, _priceManager); //var tickers = CsvExtensions.ReadConstituentsAsync("Data\\constituents.csv", new Dictionary<string, string> //{ // {"Symbol", "Symbol"}, // {"Name", "Name"}, // {"Sector", "Sector"}, //}).Result; //var msftTicker = tickerManager.GetTickerBySymbol(tickers, "MSFT"); //var prices = await _priceManager.GetPricesAsync( // msftTicker, // new DateTime(2020, 4, 13), // new DateTime(2020, 6, 26), // Interval.OneDay, // false); //var offsetPercent = 1; //var vals = stochasticService.Run(prices); //var plt = new ScottPlot.Plot(600, 400); //plt.PlotSignal(vals.Select(c => (double)c.Value).ToArray()); //plt.Title("Signal Plot Quickstart (5 million points)"); //plt.YLabel("Vertical Units"); //plt.XLabel("Horizontal Units"); //plt.SaveFig("Quickstart_Quickstart_Signal_5MillionPoints.png"); return; //var offsetPercent = 0.5; //var supportPoints = _priceManager.GetSupportExtremaGroups(prices, ExtremaType.Minimum, offsetPercent); //var p = prices.Last(); //Console.WriteLine($"Curret value: {p.Close}"); //Console.WriteLine("Support Points"); //supportPoints.Print(ExtremaType.Minimum); //Console.WriteLine(); //Console.WriteLine("Reject Points"); //rejectPoints.Print(ExtremaType.Maximum); //var daysMomentum = _priceManager.GetDaysMomentum(prices); //daysMomentum.Print(); }
public override void Run() { var rnd = new Random(); var lstm = new Lstm(2, 1, new RecurentParameters(0.5, 1, 0.5), new RecurentCellParameters(2, 4), new RecurentCellParameters(4, 1)); /*new RecurentParameters { * ActivationCoefficient = 0.5, * LearnSpeed = 1, * LengthOfInput = 2, * LengthOfOutput = 1, * LengthOfOutputSequence = 3, * LayerCount = 3, * Cells = new RecurentCellParameters[] { * new RecurentCellParameters(2, 4), * new RecurentCellParameters(4, 1) * } * }*/ /*lstm.GatesForLayers.ForgetLayer = new double[][] { * new [] { 0.0097627, 0.04303787, 0.02055268, 0.00897664, -0.01526904 }, * new [] { 0.02917882, -0.01248256, 0.0783546, 0.09273255, -0.0233117 } * };s * lstm.GatesForLayers.InputLayer = new double[][] { * new [] { 0.0097627, 0.04303787, 0.02055268, 0.00897664, -0.01526904 }, * new [] { 0.02917882, -0.01248256, 0.0783546, 0.09273255, -0.0233117 } * }; * lstm.GatesForLayers.OutputLayer = new double[][] { * new [] { 0.0097627, 0.04303787, 0.02055268, 0.00897664, -0.01526904 }, * new [] { 0.02917882, -0.01248256, 0.0783546, 0.09273255, -0.0233117 } * }; * lstm.GatesForLayers.TanhLayer = new double[][] { * new [] { 0.0097627, 0.04303787, 0.02055268, 0.00897664, -0.01526904 }, * new [] { 0.02917882, -0.01248256, 0.0783546, 0.09273255, -0.0233117 } * }; * * lstm.GatesForLayers.BiasForgetLayer = new [] { 0.0097627, 0.04303787 }; * lstm.GatesForLayers.BiasInputLayer = new [] { 0.0097627, 0.04303787 }; * lstm.GatesForLayers.BiasOutputLayer = new [] { 0.0097627, 0.04303787 }; * lstm.GatesForLayers.BiasTanhLayer = new [] { 0.0097627, 0.04303787 };*/ //lstm.BaseLayerLstm.Forget = new Vector(2, () => rnd.NextDouble()); //lstm.BaseLayerLstm.ForgetFromPreviousLayer = new Vector(2, () => rnd.NextDouble()); //lstm.BaseLayerLstm.OutputFromPreviousLayer = new Vector(2, () => rnd.NextDouble()); var learn = new Vector[] { new [] { 0.018088, 0.01591 }, new [] { 0.0248, -0.00912 }, new [] { -0.013727, 0.00502 }, new [] { -0.023491, 0.007678 }, new [] { -0.011982, 0.025521 }, new [] { 0.00835, -0.0316 }, new [] { 0.041049, -0.041505 }, new [] { 0.050914, -0.046292 }, /*new [] { 0.076138, -0.106684 }, * new [] { 0.131035, -0.092031 }, * new [] { 0.206694, -0.209201 },*/ }; var ideal1 = new Vector[] { new [] { 0.0248, -0.00912 }, new [] { -0.013727, 0.00502 }, new [] { -0.023491, 0.007678 }, new [] { -0.011982, 0.025521 }, new [] { 0.00835, -0.0316 }, new [] { 0.041049, -0.041505 }, new [] { 0.050914, -0.046292 }, new [] { 0.076138, -0.106684 }, /*new [] { 0.131035, -0.092031 }, * new [] { 0.206694, -0.209201 }, * new [] { 0.168238, -0.211099 }*/ }; var ideal = new Vector[] { new [] { -0.4 }, new [] { 0.1 }, new [] { 0.4 } }; var input = new Vector[] { new [] { -0.3, -0.1 }, new [] { 0.7, 0.8 }, new [] { 0.2, 0.1 } }; var errorCommon = 0.0; var minError = double.MaxValue; var t = new TimeSpan(0); for (var i = 0; i < 10000000; i++) { var perf = new Stopwatch(); perf.Start(); var(outputs, errors) = lstm.Learn(input, ideal); perf.Stop(); t += perf.Elapsed; if (outputs.ToList().Any(x => double.IsNaN(x[0]))) { Console.WriteLine("NaN"); for (var h = 0; h < 5; h++) { Console.Beep(1000, 700); Thread.Sleep(100); } Console.ReadLine(); Console.ReadLine(); Console.ReadLine(); } var error = errors.Sum(x => x[0]); if (error <= minError) { minError = error; } if (error < 0.000000000000000000001) { Console.WriteLine("End \t" + lstm.Epoch); Console.ReadLine(); } /*else { * Console.WriteLine("Increath"); * Console.ReadLine(); * }*/ errorCommon += error; /*Console.WriteLine("Learn:\t" + i + "\t time = " + (t / (i + 1))); * for (var j = 0; j < outputs.Length; j++) { * Console.WriteLine("\tOutput:\t" + j); * Console.WriteLine("\t\tI:\t" + ideal[j]); * Console.WriteLine("\t\tO:\t" + outputs[j]); * Console.WriteLine("\t\tE:\t" + errors[j]); * } * Console.WriteLine("Common error:\t" + errorCommon / lstm.Epoch); * Console.WriteLine("Min error:\t" + minError);*/ Console.WriteLine("Error:\t" + error); //Console.ReadKey(); } }