public void EigenValsTest() { var matrix1 = new NRealMatrix(2, 2); matrix1[0, 0] = 1; matrix1[0, 1] = 2; matrix1[1, 0] = 3; matrix1[1, 1] = 4; var w = new MCJCMatrix(2, 2); var vl = new MCJCMatrix(2, 2); var vr = new MCJCMatrix(2, 2); var lib = new MLapackLib(); lib.eigenVals(matrix1.ManagedMatrix, w, vl, vr); Console.WriteLine("w"); PrintComplexMatrix(w); Console.WriteLine("vl"); PrintComplexMatrix(vl); Console.WriteLine("vr"); PrintComplexMatrix(vr); }
public void MultiplicationAndInverseTest() { var matrix = new NRealMatrix(2, 2); matrix[0, 0] = 1; matrix[0, 1] = 2; matrix[1, 0] = 3; matrix[1, 1] = 4; var unitMatrix = new NRealMatrix(2, 2); //unitMatrix.one(); unitMatrix[0, 0] = 1; unitMatrix[0, 1] = 0; unitMatrix[1, 0] = 0; unitMatrix[1, 1] = 1; var inverse = !matrix; var result = matrix * inverse; for (int i = 0; i < matrix.Rows; i++) { for (int j = 0; j < matrix.Columns; j++) { Assert.AreEqual(unitMatrix[i, j], result[i, j], Epsilon); } } }
private static void SvdTest() { var a = new NRealMatrix(4, 2); a[0, 0] = 2; a[0, 1] = 4; a[1, 0] = 1; a[1, 1] = 3; a[2, 0] = 0; a[2, 1] = 0; a[3, 0] = 0; a[3, 1] = 0; var lib = new NLapackLib(); var s = new NRealMatrix(); var u = new NRealMatrix(); var vt = new NRealMatrix(); lib.SvdDecomposition(a, s, u, vt); Console.WriteLine(a); Console.WriteLine(s); Console.WriteLine(u); Console.WriteLine(vt); }
private static void SleTest() { var a = new NRealMatrix(2, 2); a[0, 0] = 2; a[0, 1] = 4; a[1, 0] = 1; a[1, 1] = 3; var b = new NRealMatrix(2, 2); b[0, 0] = 1; b[0, 1] = 0; b[1, 0] = 0; b[1, 1] = 1; var lib = new NLapackLib(); var x = new NRealMatrix(); lib.SolveSle(a, b, x); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(x); }
public decimal? linearCoeff(DateTime updateTime) { List<KeyValuePair<DateTime, Price>> values = _mktData[0].TimeSeries.Values(updateTime, _interval, false); if (values == null) return null; if (values.Count < 2) return null; var V = new NRealMatrix(values.Count, 2); var Vt = new NRealMatrix(values.Count, 2); var Y = new NRealMatrix(values.Count, 1); int idxRow = 0; DateTime startTime = updateTime - _interval; foreach (var keyVal in values) { V[idxRow, 0] = 1; V[idxRow, 1] = (double)(keyVal.Key - startTime).TotalMilliseconds / 1000.0; Vt[idxRow, 0] = V[idxRow, 0]; Vt[idxRow, 1] = V[idxRow, 1]; Y[idxRow, 0] = Convert.ToDouble(keyVal.Value.Mid() - values[0].Value.Mid()) * _interval.TotalMinutes; idxRow += 1; } Vt.Transpose(); var VtV = Vt * V; var VtY = Vt * Y; var X = new NRealMatrix(2, 1); //solve VtV * X = VtY LapackLib.Instance.SolveSle(VtV, VtY, X); if (X[1, 0] == double.NaN) { Log.Instance.WriteEntry("Invalid linear regression " + _mktData[0].Name, EventLogEntryType.Error); return null; } return Convert.ToDecimal(X[1, 0]); }
void nextStep() { // compute jacobian if (_retry == 0) { _jac = _jac_func(_inputs, _weights); } // compute hessian approximation with tykhonov damping coefficient var jacT = new NRealMatrix(_jac.Rows, _jac.Columns); jacT.SetArray(_jac.ToArray()); jacT.Transpose(); var dampedHessian = new NRealMatrix(_jac.Columns, _jac.Columns); dampedHessian = jacT * _jac; for (int idxRow = 0; idxRow < dampedHessian.Rows; idxRow++) { dampedHessian.SetAt(idxRow, idxRow, new NDouble(dampedHessian[idxRow, idxRow] * (1.0 + _lambda) + 1e-10)); } var adj = new NRealMatrix(dampedHessian.Rows, 1); var y = new NRealMatrix(dampedHessian.Rows, 1); y = jacT * _error; // solve dampedHessian * adj = y LapackLib.Instance.SolveSle(dampedHessian, y, adj); var nextWeights = new NRealMatrix(1, _weights.Columns); for (int idxWeight = 0; idxWeight < nextWeights.Columns; idxWeight++) { nextWeights.SetAt(0, idxWeight, new NDouble(_weights[0, idxWeight] - adj[idxWeight, 0])); } // compute errors var error = calcError(nextWeights); var totalError = calcTotalError(error); if (totalError > _totalError) { // revert step and increase damping factor if (_retry < 100) { _lambda *= 11.0; _retry++; } else { updateWeights(); throw new StallException(); } } else { // accept step and decrease damping factor _lambda /= 9.0; _weights.SetArray(nextWeights.ToArray()); _error = error; _totalError = totalError; _retry = 0; } }
decimal calcDirCoeff(IEnumerable <KeyValuePair <decimal, DateTime> > values, DateTime updateTime) { if (values.Count() < 2) { return(0m); } if (values.Count() > _nbPeaks) { values = values.Take(_nbPeaks); } /* * if (values.Count() > 2) * { * if (values.ElementAt(0).Value > values.ElementAt(2).Value && values.ElementAt(1).Value > values.ElementAt(2).Value && Math.Abs((values.ElementAt(0).Value - values.ElementAt(1).Value).TotalSeconds) > 120) * values = values.Take(2); * }*/ var V = new NRealMatrix(values.Count(), 2); var Vt = new NRealMatrix(values.Count(), 2); var Y = new NRealMatrix(values.Count(), 1); int idxRow = 0; var startTime = DateTime.MaxValue; var startValue = 0m; foreach (var kvp in values) { if (kvp.Value < startTime) { startTime = kvp.Value; startValue = kvp.Key; } } var interval = (updateTime - startTime); foreach (var keyVal in values) { V[idxRow, 0] = 1; V[idxRow, 1] = (double)(keyVal.Value - startTime).TotalMilliseconds / 1000.0; Vt[idxRow, 0] = V[idxRow, 0]; Vt[idxRow, 1] = V[idxRow, 1]; Y[idxRow, 0] = Convert.ToDouble(keyVal.Key - startValue) * interval.TotalSeconds; idxRow += 1; } Vt.Transpose(); var VtV = Vt * V; var VtY = Vt * Y; var X = new NRealMatrix(2, 1); //solve VtV * X = VtY LapackLib.Instance.SolveSle(VtV, VtY, X); if (X[1, 0] == double.NaN) { Log.Instance.WriteEntry("IndicatorTrend: Invalid linear regression " + _mktData[0].Name, EventLogEntryType.Error); return(0m); } return(Convert.ToDecimal(X[1, 0])); }
public LevenbergMarquardt(objective_func obj_func, List <double> inputs, List <Value> modelParams, model_func model, model_func model_jac, double lambda = 0.001, double obj_error = 0.00001, int max_iter = 10000, int rnd_seed = 0) { if (inputs.Count == 0) { throw new ApplicationException("Number of input data must be > 0"); } if (modelParams.Count == 0) { throw new ApplicationException("Number of model parameters must be > 0"); } _obj_func = obj_func; _model = model; _jac_func = model_jac; _lambda = lambda; _max_iter = max_iter; _inputs = new NRealMatrix(inputs.Count, 1); _inputs.SetArray((from input in inputs select new NDouble[] { new NDouble(input) }).ToArray()); _outputs = _obj_func(_inputs); _modelParams = modelParams; // initalize the weights with normal random distibution var seed = new MLapack.MCJIMatrix(4, 1); var rndSeed = rnd_seed == 0 ? 321 : rnd_seed; seed.setAt(0, 0, rndSeed); seed.setAt(1, 0, rndSeed); seed.setAt(2, 0, rndSeed); seed.setAt(3, 0, rndSeed); // check if a guess has been provided bool modelParamInitialized = false; foreach (var weight in modelParams) { if (weight.X != 0) { modelParamInitialized = true; } } if (modelParamInitialized) { _weights = new NRealMatrix(1, modelParams.Count); _weights.SetArray(new NDouble[][] { (from param in modelParams select new NDouble(param.X)).ToArray() }); } else { _weights = LapackLib.Instance.RandomMatrix(RandomDistributionType.Uniform_0_1, seed, 1, modelParams.Count); for (int idxWeight = 0; idxWeight < _weights.Columns; idxWeight++) { _weights[0, idxWeight] = (_weights[0, idxWeight] * 2.0 - 1.0) / Math.Sqrt(inputs.Count); } } _obj_error = obj_error; _error = calcError(_weights); _totalError = calcTotalError(_error); _startError = _totalError; }
public void LupDecompositionTest() { var a = new NRealMatrix(3, 3); a[0, 0] = 1; a[0, 1] = 2; a[0, 2] = 3; a[1, 0] = 4; a[1, 1] = 5; a[1, 2] = 6; a[2, 0] = 7; a[2, 1] = 8; a[2, 2] = 9; var p = new NRealMatrix(3, 3); p[0, 0] = 1; p[0, 1] = 0; p[0, 2] = 0; p[1, 0] = 0; p[1, 1] = 0; p[1, 2] = 1; p[2, 0] = 0; p[2, 1] = 1; p[2, 2] = 0; Console.WriteLine(p * a); var l = new NRealMatrix(3, 3); var u = new NRealMatrix(3, 3); Console.WriteLine(a); Console.WriteLine(p); var mlib = new NLapackLib(); mlib.LupDecompostion(a, p, l, u); //var lib = new MLapackLib(); //lib.LUPDecomposition(a.ManagedMatrix, p.ManagedMatrix, l.ManagedMatrix, u.ManagedMatrix); Console.WriteLine(l); Console.WriteLine(u); var result = l * u; Console.WriteLine(result); Console.WriteLine(p * a); }
double calcTotalError(NRealMatrix error) { double sumSquare = 0; for (int idxError = 0; idxError < error.Rows; idxError++) { sumSquare += error[idxError, 0] * error[idxError, 0]; } return(Math.Sqrt(sumSquare)); }
private static void InverseTest() { var a = new NRealMatrix(2, 2); a[0, 0] = 2; a[0, 1] = 4; a[1, 0] = 1; a[1, 1] = 3; Console.WriteLine(a); Console.WriteLine(!a); }
NRealMatrix calcError(NRealMatrix weights) { var error = new NRealMatrix(_inputs.Rows, 1); NRealMatrix modelOutput = _model(_inputs, weights); for (int idxError = 0; idxError < error.Rows; idxError++) { error.SetAt(idxError, 0, new NDouble(_outputs[idxError, 0] - modelOutput[idxError, 0])); } return(error); }
/// <summary> /// /// </summary> /// <param name="a"></param> /// <returns></returns> public NRealMatrix Chol(NRealMatrix a) { var result = new NRealMatrix(); _lib.chol(a.ManagedMatrix, result.ManagedMatrix); int rows = result.Rows; for (int i = 0; i < rows; i++) for (int j = 0; j < i; j++) result.ManagedMatrix.setAt(i, j, 0); return result; }
public LevenbergMarquardt(objective_func obj_func, List<double> inputs, List<Value> modelParams, model_func model, model_func model_jac, double lambda = 0.001, double obj_error = 0.00001, int max_iter = 10000, int rnd_seed = 0) { if (inputs.Count == 0) throw new ApplicationException("Number of input data must be > 0"); if (modelParams.Count == 0) throw new ApplicationException("Number of model parameters must be > 0"); _obj_func = obj_func; _model = model; _jac_func = model_jac; _lambda = lambda; _max_iter = max_iter; _inputs = new NRealMatrix(inputs.Count, 1); _inputs.SetArray((from input in inputs select new NDouble[] { new NDouble(input) } ).ToArray()); _outputs = _obj_func(_inputs); _modelParams = modelParams; // initalize the weights with normal random distibution var seed = new MLapack.MCJIMatrix(4,1); var rndSeed = rnd_seed == 0 ? 321 : rnd_seed; seed.setAt(0, 0, rndSeed); seed.setAt(1, 0, rndSeed); seed.setAt(2, 0, rndSeed); seed.setAt(3, 0, rndSeed); // check if a guess has been provided bool modelParamInitialized = false; foreach (var weight in modelParams) { if (weight.X != 0) modelParamInitialized = true; } if (modelParamInitialized) { _weights = new NRealMatrix(1, modelParams.Count); _weights.SetArray(new NDouble[][] {(from param in modelParams select new NDouble(param.X)).ToArray() }); } else { _weights = LapackLib.Instance.RandomMatrix(RandomDistributionType.Uniform_0_1, seed, 1, modelParams.Count); for (int idxWeight = 0; idxWeight < _weights.Columns; idxWeight++) _weights[0, idxWeight] = (_weights[0, idxWeight] * 2.0 - 1.0) / Math.Sqrt(inputs.Count); } _obj_error = obj_error; _error = calcError(_weights); _totalError = calcTotalError(_error); _startError = _totalError; }
/// <summary> /// /// </summary> /// <param name="a"></param> /// <returns></returns> public NRealMatrix Chol(NRealMatrix a) { var result = new NRealMatrix(); _lib.chol(a.ManagedMatrix, result.ManagedMatrix); int rows = result.Rows; for (int i = 0; i < rows; i++) { for (int j = 0; j < i; j++) { result.ManagedMatrix.setAt(i, j, 0); } } return(result); }
public void JMatrixTest() { var matrix1 = new NRealMatrix(2, 2); matrix1[0, 0] = 1; matrix1[0, 1] = 2; matrix1[1, 0] = 3; matrix1[1, 1] = 4; var matrix2 = new NRealMatrix(2, 2); matrix2[0, 0] = 5; matrix2[0, 1] = 6; matrix2[1, 0] = 7; matrix2[1, 1] = 8; var matrix3 = matrix1 * matrix2; }
public void ConditionNumerTest() { var matrix1 = new NRealMatrix(2, 2); matrix1[0, 0] = 1; matrix1[0, 1] = 2; matrix1[1, 0] = 3; matrix1[1, 1] = 4; var lib = new MLapackLib(); var conditionNumber = lib.matrixRConditionNumber(Convert.ToSByte(MatrixNormType.OneNorm), matrix1.ManagedMatrix); Console.WriteLine("condNum = " + conditionNumber); /*var norm = (double)matrix1; var normInv = (double)(!matrix1); Console.WriteLine(norm + " " + normInv); Console.WriteLine(1/(norm * normInv));*/ }
public void MatrixNormTest() { const int maxN = 10; for (int n = 2; n < maxN; n++) { var unit = new NRealMatrix(n, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { unit[i, j] = (i == j) ? 1 : 0; } } double norm = (double)unit; Assert.AreEqual(1, norm, Epsilon); } }
public decimal?linearCoeff(DateTime updateTime) { List <KeyValuePair <DateTime, Price> > values = _mktData[0].TimeSeries.Values(updateTime, _interval, false); if (values == null) { return(null); } if (values.Count < 2) { return(null); } var V = new NRealMatrix(values.Count, 2); var Vt = new NRealMatrix(values.Count, 2); var Y = new NRealMatrix(values.Count, 1); int idxRow = 0; DateTime startTime = updateTime - _interval; foreach (var keyVal in values) { V[idxRow, 0] = 1; V[idxRow, 1] = (double)(keyVal.Key - startTime).TotalMilliseconds / 1000.0; Vt[idxRow, 0] = V[idxRow, 0]; Vt[idxRow, 1] = V[idxRow, 1]; Y[idxRow, 0] = Convert.ToDouble(keyVal.Value.Mid() - values[0].Value.Mid()) * _interval.TotalMinutes; idxRow += 1; } Vt.Transpose(); var VtV = Vt * V; var VtY = Vt * Y; var X = new NRealMatrix(2, 1); //solve VtV * X = VtY LapackLib.Instance.SolveSle(VtV, VtY, X); if (X[1, 0] == double.NaN) { Log.Instance.WriteEntry("Invalid linear regression " + _mktData[0].Name, EventLogEntryType.Error); return(null); } return(Convert.ToDecimal(X[1, 0])); }
public void ZeroTest() { const int maxN = 10; for (int r = 2; r < maxN; r++) { for (int c = 2; c < maxN; c++) { var matrix = new NRealMatrix(r, c); matrix.Zero(); for (int i = 0; i < matrix.Rows; i++) { for (int j = 0; j < matrix.Columns; j++) { Assert.AreEqual(Math.Abs(matrix[i, j]), 0, Epsilon); } } } } }
public void EigenValsTestManaged() { var w = new NComplexMatrix(2, 2); var vl = new NComplexMatrix(2, 2); var vr = new NComplexMatrix(2, 2); var lib = new NLapackLib(); var matrix1 = new NRealMatrix(2, 2); matrix1[0, 0] = 1; matrix1[0, 1] = 2; matrix1[1, 0] = 3; matrix1[1, 1] = 4; lib.EigenVals(matrix1, w, vl, vr); Console.WriteLine("w=\n" + w); Console.WriteLine("vl=\n" + vl); Console.WriteLine("vr=\n" + vl); }
public void ConditionNumerTest() { var matrix1 = new NRealMatrix(2, 2); matrix1[0, 0] = 1; matrix1[0, 1] = 2; matrix1[1, 0] = 3; matrix1[1, 1] = 4; var lib = new MLapackLib(); var conditionNumber = lib.matrixRConditionNumber(Convert.ToSByte(MatrixNormType.OneNorm), matrix1.ManagedMatrix); Console.WriteLine("condNum = " + conditionNumber); /*var norm = (double)matrix1; * var normInv = (double)(!matrix1); * * Console.WriteLine(norm + " " + normInv); * Console.WriteLine(1/(norm * normInv));*/ }
public void MultiplicationTest() { var matrix = new NRealMatrix(2, 2); matrix[0, 0] = 1; matrix[0, 1] = 2; matrix[1, 0] = 3; matrix[1, 1] = 4; var inverse = !matrix; var sameMatrix = !inverse; for (int i = 0; i < matrix.Rows; i++) { for (int j = 0; j < matrix.Columns; j++) { Assert.AreEqual(sameMatrix[i, j], matrix[i, j], Epsilon); } } }
public void SolveSle() { const int maxN = 10; for (int n = 2; n < maxN; n++) { var random = new Random(); double mult = random.NextDouble(); var a = new NRealMatrix(n, n); var b = new NRealMatrix(n, n); var unit = new NRealMatrix(n, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { var value = random.NextDouble(); a[i, j] = value; b[i, j] = value * mult; unit[i, j] = (i == j) ? 1 : 0; } } var x = new NRealMatrix(n, n); var lib = new NLapackLib(); lib.SolveSle(a, b, x); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Assert.AreEqual(x[i, j], unit[i, j] * mult, Epsilon); } } } }
public void InverseTest() { const int maxN = 10; var random = new Random(); for (int n = 2; n < maxN; n++) { var matrix = new NRealMatrix(n, n); var unit = new NRealMatrix(n, n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrix[i, j] = random.NextDouble(); unit[i, j] = (i == j) ? 1 : 0; } } var result = matrix * unit; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Assert.AreEqual(matrix[i, j], result[i, j], Epsilon); } } result = unit * matrix; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Assert.AreEqual(matrix[i, j], result[i, j], Epsilon); } } } }
public void SvdDecompositionTest() { var matrix1 = new NRealMatrix(2, 2); matrix1[0, 0] = 1; matrix1[0, 1] = 2; matrix1[1, 0] = 3; matrix1[1, 1] = 4; var s = new NRealMatrix(); var u = new NRealMatrix(); var vt = new NRealMatrix(); var lib = new MLapackLib(); lib.SVDDecomposition(matrix1.ManagedMatrix, s.ManagedMatrix, u.ManagedMatrix, vt.ManagedMatrix); Console.WriteLine(matrix1); Console.WriteLine(s); Console.WriteLine(u); Console.WriteLine(vt); }
public void CholTest() { var a = new NRealMatrix(3, 3); a[0, 0] = 2; a[0, 1] = 1; a[0, 2] = 1; a[1, 0] = 1; a[1, 1] = 2; a[1, 2] = 1; a[2, 0] = 1; a[2, 1] = 1; a[2, 2] = 2; var lib = new NLapackLib(); var result = lib.Chol(a); Console.WriteLine(result); }
public void EigenValsXTest() { var matrix1 = new NRealMatrix(2, 2); matrix1[0, 0] = 1; matrix1[0, 1] = 2; matrix1[1, 0] = 3; matrix1[1, 1] = 4; var w = new NComplexMatrix(2, 2); var vl = new NComplexMatrix(2, 2); var vr = new NComplexMatrix(2, 2); var rconde = new NRealMatrix(2, 2); var rcondv = new NRealMatrix(2, 2); //var lib = new MLapackLib(); //lib.eigenValsX(Convert.ToInt16(BalanceType.None), matrix1, w, vl, vr, rconde, rcondv); var nlib = new NLapackLib(); nlib.EigenValsX(BalanceType.None, matrix1, w, vl, vr, rconde, rcondv); Console.WriteLine("w="); Console.WriteLine(w); Console.WriteLine("vl="); Console.WriteLine(vl); Console.WriteLine("vr="); Console.WriteLine(vr); Console.WriteLine("rconde="); Console.WriteLine(rconde); Console.WriteLine("rcondv="); Console.WriteLine(rcondv); }
/// <summary> /// /// </summary> /// <param name="a"></param> /// <param name="p"></param> /// <param name="l"></param> /// <param name="u"></param> public void LupDecompostion(NRealMatrix a, NRealMatrix p, NRealMatrix l, NRealMatrix u) { _lib.LUPDecomposition(a.ManagedMatrix, p.ManagedMatrix, l.ManagedMatrix, u.ManagedMatrix); }
/// <summary> /// /// </summary> /// <param name="matrix"></param> /// <param name="w"></param> /// <param name="vl"></param> /// <param name="vr"></param> public void EigenVals(NRealMatrix matrix, NComplexMatrix w, NComplexMatrix vl, NComplexMatrix vr) { _lib.eigenVals(matrix.ManagedMatrix, w.ManagedMatrix, vl.ManagedMatrix, vr.ManagedMatrix); }
/// <summary> /// /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <param name="x"></param> public void SolveSle(NRealMatrix a, NRealMatrix b, NRealMatrix x) { _lib.solveSLE(a.ManagedMatrix, b.ManagedMatrix, x.ManagedMatrix); }
/// <summary> /// /// </summary> /// <param name="balancec"></param> /// <param name="a"></param> /// <param name="w"></param> /// <param name="vl"></param> /// <param name="vr"></param> /// <param name="rconde"></param> /// <param name="rcondv"></param> public void EigenValsX(BalanceType balancec, NRealMatrix a, NComplexMatrix w, NComplexMatrix vl, NComplexMatrix vr, NRealMatrix rconde, NRealMatrix rcondv) { _lib.eigenValsX(Convert.ToInt16(balancec), a.ManagedMatrix, w.ManagedMatrix, vl.ManagedMatrix, vr.ManagedMatrix, rconde.ManagedMatrix, rcondv.ManagedMatrix); }
void nextStep() { // compute jacobian if (_retry == 0) _jac = _jac_func(_inputs, _weights); // compute hessian approximation with tykhonov damping coefficient var jacT = new NRealMatrix(_jac.Rows, _jac.Columns); jacT.SetArray(_jac.ToArray()); jacT.Transpose(); var dampedHessian = new NRealMatrix(_jac.Columns, _jac.Columns); dampedHessian = jacT * _jac; for (int idxRow = 0; idxRow < dampedHessian.Rows; idxRow++) dampedHessian.SetAt(idxRow, idxRow, new NDouble(dampedHessian[idxRow, idxRow] * (1.0 + _lambda) + 1e-10)); var adj = new NRealMatrix(dampedHessian.Rows, 1); var y = new NRealMatrix(dampedHessian.Rows, 1); y = jacT * _error; // solve dampedHessian * adj = y LapackLib.Instance.SolveSle(dampedHessian, y, adj); var nextWeights = new NRealMatrix(1, _weights.Columns); for (int idxWeight = 0; idxWeight < nextWeights.Columns; idxWeight++) nextWeights.SetAt(0, idxWeight, new NDouble(_weights[0, idxWeight] - adj[idxWeight, 0])); // compute errors var error = calcError(nextWeights); var totalError = calcTotalError(error); if (totalError > _totalError) { // revert step and increase damping factor if (_retry < 100) { _lambda *= 11.0; _retry++; } else { updateWeights(); throw new StallException(); } } else { // accept step and decrease damping factor _lambda /= 9.0; _weights.SetArray(nextWeights.ToArray()); _error = error; _totalError = totalError; _retry = 0; } }
double calcTotalError(NRealMatrix error) { double sumSquare = 0; for (int idxError = 0; idxError < error.Rows; idxError++) sumSquare += error[idxError, 0] * error[idxError, 0]; return Math.Sqrt(sumSquare); }
public static void Run(bool generate = false, bool generate_from_db = false) { Dictionary<string, string> dicSettings = new Dictionary<string, string>(); dicSettings["APP_NAME"] = "Midax"; dicSettings["PUBLISHING_START_TIME"] = "2016-01-22 08:00:00"; dicSettings["PUBLISHING_STOP_TIME"] = "2016-01-22 09:00:00"; dicSettings["REPLAY_MODE"] = "CSV"; dicSettings["REPLAY_POPUP"] = "1"; dicSettings["TRADING_START_TIME"] = "2016-01-22 08:45:00"; dicSettings["TRADING_STOP_TIME"] = "2016-01-22 08:59:00"; dicSettings["TRADING_CLOSING_TIME"] = "2016-01-22 08:57:00"; dicSettings["TRADING_MODE"] = "REPLAY"; dicSettings["TRADING_SIGNAL"] = "MacD_1_5_IX.D.DAX.DAILY.IP"; dicSettings["TRADING_LIMIT_PER_BP"] = "10"; dicSettings["TRADING_CURRENCY"] = "GBP"; Config.Settings = dicSettings; string action = generate ? "Generating" : "Testing"; var dax = new MarketData("DAX:IX.D.DAX.DAILY.IP"); List<string> tests = new List<string>(); Console.WriteLine(action + " WMA..."); // Test weighted moving average with long intervals tests.Add(@"..\..\expected_results\testWMA.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(tests); if (generate) dicSettings["PUBLISHING_CSV"] = string.Format("..\\..\\expected_results\\testWMAgen.csv"); var macDTestWMA = new ModelMacDTest(dax, 1, 2, 3); MarketDataConnection.Instance.Connect(null); macDTestWMA.StartSignals(); macDTestWMA.StopSignals(); // Test weighted moving average with short intervals tests = new List<string>(); tests.Add(@"..\..\expected_results\testWMA2.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(tests); if (generate) dicSettings["PUBLISHING_CSV"] = string.Format("..\\..\\expected_results\\testWMA2gen.csv"); dax.Clear(); macDTestWMA = new ModelMacDTest(dax, 1, 2, 3); MarketDataConnection.Instance.Connect(null); macDTestWMA.StartSignals(); macDTestWMA.StopSignals(); // Test weighted moving average with linear time decay tests = new List<string>(); tests.Add(@"..\..\expected_results\testWMA3.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(tests); dicSettings["TIME_DECAY_FACTOR"] = "3"; if (generate) dicSettings["PUBLISHING_CSV"] = string.Format("..\\..\\expected_results\\testWMA3gen.csv"); dax.Clear(); macDTestWMA = new ModelMacDTest(dax, 1, 2, 3); MarketDataConnection.Instance.Connect(null); macDTestWMA.StartSignals(); macDTestWMA.StopSignals(); // Test volume weighted moving average with linear time decay /* tests = new List<string>(); tests.Add(@"..\..\expected_results\testWMA4.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(tests); if (generate) dicSettings["PUBLISHING_CSV"] = string.Format("..\\..\\expected_results\\testWMA4gen.csv"); var macDVTest = new ModelMacDVTest(dax, 1, 2, 3); MarketDataConnection.Instance.Connect(null); macDVTest.StartSignals(); macDVTest.StopSignals();*/ dicSettings.Remove("TIME_DECAY_FACTOR"); // Test RSI and Correlation indicators tests = new List<string>(); tests.Add(@"..\..\expected_results\testRsiCorrel.csv"); dicSettings["INDEX_ICEDOW"] = "DOW:IceConnection_DOW"; dicSettings["INDEX_DOW"] = "DOW:IX.D.DOW.DAILY.IP"; dicSettings["INDEX_DAX"] = "DAX:IX.D.DAX.DAILY.IP"; dicSettings["REPLAY_CSV"] = Config.TestList(tests); if (generate) dicSettings["PUBLISHING_CSV"] = string.Format("..\\..\\expected_results\\testRsiCorrelgen.csv"); dax.Clear(); var icedow = new MarketData(dicSettings["INDEX_ICEDOW"]); var dow = new MarketData(dicSettings["INDEX_DOW"]); var macD = new ModelMacDTest(dax, 1, 2, 3); //var macDV = new ModelMacDVTest(icedow, 1, 2, 3, dow); var moleTest = new ModelMoleTest(macD); MarketDataConnection.Instance.Connect(null); macD.StartSignals(false); //macDV.StartSignals(false); moleTest.StartSignals(false); MarketDataConnection.Instance.StartListening(); moleTest.StopSignals(false); //macDV.StartSignals(false); macD.StopSignals(false); MarketDataConnection.Instance.StopListening(); Console.WriteLine(action + " calibration..."); // Test a 1mn linear regression var mktData = new MarketData("testLRMktData"); var updateTime = Config.ParseDateTimeLocal(dicSettings["TRADING_START_TIME"]); mktData.TimeSeries.Add(updateTime, new Price(100)); mktData.TimeSeries.Add(updateTime.AddSeconds(20), new Price(120)); mktData.TimeSeries.Add(updateTime.AddSeconds(40), new Price(140)); mktData.TimeSeries.Add(updateTime.AddSeconds(60), new Price(130)); mktData.TimeSeries.Add(updateTime.AddSeconds(80), new Price(145)); mktData.TimeSeries.Add(updateTime.AddSeconds(100), new Price(165)); mktData.TimeSeries.Add(updateTime.AddSeconds(120), new Price(145)); var linReg = new IndicatorLinearRegression(mktData, new TimeSpan(0, 2, 0)); var linRegCoeff = linReg.linearCoeff(updateTime.AddSeconds(120)); if (Math.Abs(linRegCoeff.Value - 0.821428571428573m) > 1e-8m) throw new ApplicationException("Linear regression error"); // Test the optimization of function a * cos(b * x) + b * sin(a * x) using Levenberg Marquardt LevenbergMarquardt.objective_func objFunc = (NRealMatrix x) => { NRealMatrix y = new NRealMatrix(x.Rows, 1); for (int idxRow = 0; idxRow < y.Rows; idxRow++) y.SetAt(idxRow, 0, new NDouble(2 * Math.Cos(x[idxRow, 0]) + Math.Sin(2 * x[idxRow, 0]))); return y; }; List<double> inputs = new List<double>(); Random rnd = new Random(155); for (int idxPt = 0; idxPt < 10; idxPt++) inputs.Add(rnd.NextDouble() * 2); List<Value> modelParams = new List<Value>(); modelParams.Add(new Value(-0.2)); modelParams.Add(new Value(0.3)); LevenbergMarquardt.model_func modelFunc = (NRealMatrix x, NRealMatrix weights) => { NRealMatrix y = new NRealMatrix(x.Rows, 1); double a = weights[0, 0]; double b = weights[0, 1]; for (int idxRow = 0; idxRow < y.Rows; idxRow++) y.SetAt(idxRow, 0, new NDouble(a * Math.Cos(b * x[idxRow, 0]) + b * Math.Sin(a * x[idxRow, 0]))); return y; }; Func<double,double,double,double> derA = (double a, double b, double x) => Math.Cos(b * x) + b * x * Math.Cos(a * x); Func<double,double,double,double> derB = (double a, double b, double x) => - a * x * Math.Sin(b * x) + Math.Sin(a * x); LevenbergMarquardt.model_func jacFunc = (NRealMatrix x, NRealMatrix weights) => { NRealMatrix jac = new NRealMatrix(x.Rows, 2); double a = weights[0, 0]; double b = weights[0, 1]; for (int idxRow = 0; idxRow < jac.Rows; idxRow++) { jac.SetAt(idxRow, 0, new NDouble(-derA(a, b, x[idxRow, 0]))); jac.SetAt(idxRow, 1, new NDouble(-derB(a, b, x[idxRow, 0]))); } return jac; }; LevenbergMarquardt calibModel = new LevenbergMarquardt(objFunc, inputs, modelParams, modelFunc, jacFunc); calibModel.Solve(); if (Math.Abs(modelParams[0].X - 2) > calibModel.ObjectiveError || Math.Abs(modelParams[1].X - 1) > calibModel.ObjectiveError) throw new ApplicationException("LevenbergMarquardt calibration error"); // Parity-2 problem NeuralNetwork ann = new NeuralNetwork(2, 1, new List<int>() { 2 }); List<List<double>> annInputs = new List<List<double>>(); annInputs.Add(new List<double>() { -1, -1 }); annInputs.Add(new List<double>() { -1, 1 }); annInputs.Add(new List<double>() { 1, -1 }); annInputs.Add(new List<double>() { 1, 1 }); List<List<double>> annOutputs = new List<List<double>>(); annOutputs.Add(new List<double>() { 1 }); annOutputs.Add(new List<double>() { -1 }); annOutputs.Add(new List<double>() { -1 }); annOutputs.Add(new List<double>() { 1 }); // test forward propagation ann._outputs.Neurons[0].Weights[0].X = 1; ann._outputs.Neurons[0].Weights[1].X = -1; ann._outputs.Neurons[0].Weights[2].X = -1; ann._innerLayers[0].Neurons[0].Weights[0].X = 1; ann._innerLayers[0].Neurons[0].Weights[1].X = 1; ann._innerLayers[0].Neurons[0].Weights[2].X = 1; ann._innerLayers[0].Neurons[1].Weights[0].X = 1; ann._innerLayers[0].Neurons[1].Weights[1].X = 1; ann._innerLayers[0].Neurons[1].Weights[2].X = -1; ann._inputs.Neurons[0].Value.X = -1; ann._inputs.Neurons[1].Value.X = -1; if (Math.Abs(ann._outputs.Neurons[0].Activation() - -0.38873457229297215) > calibModel.ObjectiveError) throw new ApplicationException("Neural network forward propagation error"); // Test neural network training for parity-2 problem ann = new NeuralNetwork(2, 1, new List<int>() { 2 }); ann.Train(annInputs, annOutputs); // Test neural network training for parity-3 problem ann = new NeuralNetwork(3, 1, new List<int>() { 2 }); annInputs = new List<List<double>>(); annInputs.Add(new List<double>() {-1,-1,-1}); annInputs.Add(new List<double>() {-1,-1, 1}); annInputs.Add(new List<double>() {-1, 1,-1}); annInputs.Add(new List<double>() {-1, 1, 1}); annInputs.Add(new List<double>() { 1,-1,-1}); annInputs.Add(new List<double>() { 1,-1, 1}); annInputs.Add(new List<double>() { 1, 1,-1}); annInputs.Add(new List<double>() { 1, 1, 1}); annOutputs = new List<List<double>>(); annOutputs.Add(new List<double>() { -1 }); annOutputs.Add(new List<double>() { 1 }); annOutputs.Add(new List<double>() { 1 }); annOutputs.Add(new List<double>() { -1 }); annOutputs.Add(new List<double>() { 1 }); annOutputs.Add(new List<double>() { -1 }); annOutputs.Add(new List<double>() { -1 }); annOutputs.Add(new List<double>() { 1 }); ann.Train(annInputs, annOutputs); Console.WriteLine(action + " live indicators and signals..."); tests = new List<string>(); tests.Add(@"..\..\expected_results\core_22_1_2016.csv"); if (generate_from_db) dicSettings["DB_CONTACTPOINT"] = "192.168.1.26"; dicSettings["REPLAY_MODE"] = generate_from_db ? "DB" : "CSV"; dicSettings["REPLAY_CSV"] = Config.TestList(tests); if (generate) dicSettings["PUBLISHING_CSV"] = string.Format("..\\..\\expected_results\\coregen_22_1_2016.csv"); MarketDataConnection.Instance.Connect(null); dax.Clear(); var model = new ModelMacDTest(dax); model.StartSignals(); Console.WriteLine(action + " daily indicators..."); model.StopSignals(); Thread.Sleep(1000); if (!dicSettings.ContainsKey("PUBLISHING_CSV")) { // the program is expected to throw exceptions in this scope, just press continue if you are debugging // all exceptions should be handled, and the program should terminate with a success message box // test that the right numer of trades was placed. this is an extra sanity check to make sure the program is not idle if (ReplayTester.Instance.NbProducedTrades != ReplayTester.Instance.NbExpectedTrades) model.ProcessError(string.Format("the model did not produced the expected number of trades. It produced {0} trades instead of {1} expected", ReplayTester.Instance.NbProducedTrades, ReplayTester.Instance.NbExpectedTrades)); // test trade booking MarketDataConnection.Instance = new ReplayConnection(); model = new ModelMacDTest(dax); MarketDataConnection.Instance.Connect(null); Console.WriteLine(action + " trade booking..."); var tradeTime = Config.ParseDateTimeLocal(dicSettings["TRADING_CLOSING_TIME"]).AddSeconds(-1); var tradeTest = new Trade(tradeTime, dax.Id, SIGNAL_CODE.SELL, 10, 10000m); var expectedTrades = new Dictionary<KeyValuePair<string, DateTime>, Trade>(); expectedTrades[new KeyValuePair<string, DateTime>("###DUMMY_TRADE_REF1###", tradeTime)] = tradeTest; ReplayTester.Instance.SetExpectedResults(null, null, expectedTrades, null); model.PTF.Subscribe(); model.PTF.BookTrade(tradeTest); Thread.Sleep(1000); if (model.PTF.GetPosition(tradeTest.Epic).Quantity != -10) throw new ApplicationException("SELL Trade booking error"); var expectedTrade = new Trade(tradeTime, dax.Id, SIGNAL_CODE.BUY, 10, 10000m); expectedTrade.Reference = "###CLOSE_DUMMY_TRADE_REF2###"; expectedTrade.Id = "###DUMMY_TRADE_ID1###"; expectedTrades[new KeyValuePair<string, DateTime>(expectedTrade.Reference, tradeTime)] = expectedTrade; model.PTF.ClosePosition(tradeTest, tradeTime); Thread.Sleep(1000); if (model.PTF.GetPosition(tradeTest.Epic).Quantity != 0) throw new ApplicationException("Trade position closing error"); expectedTrade.Reference = "###DUMMY_TRADE_REF3###"; expectedTrade.Id = "###DUMMY_TRADE_ID2###"; expectedTrades[new KeyValuePair<string, DateTime>(expectedTrade.Reference, tradeTime)] = expectedTrade; model.PTF.BookTrade(new Trade(tradeTest, true, tradeTime)); Thread.Sleep(1000); if (model.PTF.GetPosition(tradeTest.Epic).Quantity != 10) throw new ApplicationException("BUY Trade booking error"); expectedTrade = new Trade(tradeTime, dax.Id, SIGNAL_CODE.SELL, 10, 0m); expectedTrade.Reference = "###CLOSE_DUMMY_TRADE_REF4###"; expectedTrade.Id = "###DUMMY_TRADE_ID2###"; expectedTrades[new KeyValuePair<string, DateTime>(expectedTrade.Reference, tradeTime)] = expectedTrade; Portfolio.Instance.CloseAllPositions(tradeTest.TradingTime); Thread.Sleep(1000); // test synchronization issues with the broker List<string> testsSync = new List<string>(); testsSync.Add(@"..\..\expected_results\sync.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(testsSync); MarketDataConnection.Instance = new ReplayCrazySeller(); model = new ModelMacDTest(dax); Console.WriteLine(action + " synchronization..."); MarketDataConnection.Instance.Connect(null); model.StartSignals(); model.StopSignals(); testsSync = new List<string>(); testsSync.Add(@"..\..\expected_results\sync2.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(testsSync); MarketDataConnection.Instance = new ReplayCrazyBuyer(); model = new ModelMacDTest(dax); MarketDataConnection.Instance.Connect(null); model.StartSignals(); model.StopSignals(); Console.WriteLine(action + " expected exceptions..."); dicSettings["REPLAY_CSV"] = Config.TestList(tests); MarketDataConnection.Instance = new ReplayConnection(); MarketDataConnection.Instance.Connect(null); List<string> testError = new List<string>(); testError.Add(@"..\..\expected_results\error.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(testError); var modelErr = new ModelMacDTest(dax); string expected; bool success = false; try { MarketDataConnection.Instance.Connect(null); modelErr.StartSignals(); } catch (Exception exc) { expected = "Test failed: indicator EMA_1_IX.D.DAX.DAILY.IP time 08:30 expected value 9740.300000000000000000000000 != 9739.8"; success = (exc.Message.Replace(" AM", "") == expected); if (!success) model.ProcessError(exc.Message, expected); } if (!success) model.ProcessError("An expected exception has not been thrown"); success = false; try { modelErr.StopSignals(); } catch (Exception exc) { model.ProcessError(exc.Message + " - Wrong daily mean exception removed"); } success = false; try { model.StopSignals(); } catch (Exception exc) { model.ProcessError(exc.Message + " - Double EOD publishing exception removed"); } success = false; try { MarketDataConnection.Instance = new ReplayConnection(); MarketDataConnection.Instance.Connect(null); model = new ModelMacDTest(new MarketData(dax.Id)); model.StartSignals(); } catch (Exception exc) { expected = "Test failed: indicator EMA_1_IX.D.DAX.DAILY.IP time 08:30 expected value 9740.300000000000000000000000 != 9739.8"; success = (exc.Message.Replace(" AM", "") == expected); if (!success) model.ProcessError(exc.Message, expected); } if (!success) model.ProcessError("An expected exception has not been thrown"); success = false; try { MarketDataConnection.Instance.Resume(); } catch (Exception exc) { expected = "Time series do not accept values in the past"; success = (exc.Message.Replace(" AM", "") == expected); if (!success) model.ProcessError(exc.Message, expected); } if (!success) model.ProcessError("An expected exception has not been thrown"); model.StopSignals(); success = false; } }
NRealMatrix calcError(NRealMatrix weights) { var error = new NRealMatrix(_inputs.Rows, 1); NRealMatrix modelOutput = _model(_inputs, weights); for (int idxError = 0; idxError < error.Rows; idxError++) error.SetAt(idxError, 0, new NDouble(_outputs[idxError, 0] - modelOutput[idxError, 0])); return error; }
public void Train(List <List <double> > inputValues, List <List <double> > outputValues, double obj_error = 1e-5, double max_error = 1e-5, int rnd_seed = 0) { if (inputValues.Count != outputValues.Count) { throw new ApplicationException("Training set inputs and outputs must have the same size"); } // input normalization int nbInputs = inputValues[0].Count; NRealMatrix inputTable = new NRealMatrix(inputValues.Count, nbInputs); var maxValue = 0.0; for (int idxInputList = 0; idxInputList < inputValues.Count; idxInputList++) { for (int idxInput = 0; idxInput < inputValues[idxInputList].Count; idxInput++) { if (Math.Abs(inputValues[idxInputList][idxInput]) > maxValue) { maxValue = Math.Abs(inputValues[idxInputList][idxInput]); } } } for (int idxInputList = 0; idxInputList < inputValues.Count; idxInputList++) { for (int idxInput = 0; idxInput < inputValues[idxInputList].Count; idxInput++) { inputTable[idxInputList, idxInput] = inputValues[idxInputList][idxInput] / maxValue; } } // output normalization int nbOutputs = outputValues[0].Count; NRealMatrix objectiveTable = new NRealMatrix(outputValues.Count, nbOutputs); maxValue = 0.0; for (int idxOutputList = 0; idxOutputList < outputValues.Count; idxOutputList++) { for (int idxOutput = 0; idxOutput < outputValues[idxOutputList].Count; idxOutput++) { if (Math.Abs(outputValues[idxOutputList][idxOutput]) > maxValue) { maxValue = Math.Abs(outputValues[idxOutputList][idxOutput]); } } } for (int idxOutputList = 0; idxOutputList < outputValues.Count; idxOutputList++) { for (int idxOutput = 0; idxOutput < outputValues[idxOutputList].Count; idxOutput++) { objectiveTable[idxOutputList, idxOutput] = outputValues[idxOutputList][idxOutput] / maxValue; } } LevenbergMarquardt.objective_func objFunc = (NRealMatrix x) => { NRealMatrix y = new NRealMatrix(x.Rows, nbOutputs); for (int idxRow = 0; idxRow < y.Rows; idxRow++) { for (int idxCol = 0; idxCol < nbOutputs; idxCol++) { y[idxRow, idxCol] = objectiveTable[Convert.ToInt32(x[idxRow, 0]), idxCol]; } } return(y); }; List <double> inputs = new List <double>(); for (int idxOutputList = 0; idxOutputList < outputValues.Count; idxOutputList++) { inputs.Add((double)idxOutputList); } List <Value> modelParams = getModelParams(); List <Value> modelValues = new List <Value>(); foreach (var layer in _innerLayers) { modelValues.AddRange(layer.GetValues()); } modelValues.AddRange(_outputs.GetValues()); List <Value> modelDeltas = new List <Value>(); foreach (var layer in _innerLayers) { modelDeltas.AddRange(layer.GetDeltas()); } modelDeltas.AddRange(_outputs.GetDeltas()); LevenbergMarquardt.model_func modelFunc = (NRealMatrix x, NRealMatrix weights) => { // apply new weights for (int idxWeight = 0; idxWeight < weights.Columns; idxWeight++) { modelParams[idxWeight].X = weights[0, idxWeight]; } NRealMatrix y = new NRealMatrix(x.Rows, nbOutputs); // foreach set of input data for (int idxRow = 0; idxRow < x.Rows; idxRow++) { // compute the ouput results CalculateOutput(inputValues[Convert.ToInt32(x[idxRow, 0])]); List <double> modelOutputs = GetOutput(); for (int idxCol = 0; idxCol < nbOutputs; idxCol++) { y[idxRow, idxCol] = modelOutputs[idxCol]; } } return(y); }; LevenbergMarquardt.model_func jacFunc = (NRealMatrix x, NRealMatrix weights) => { // apply new weights for (int idxWeight = 0; idxWeight < weights.Columns; idxWeight++) { modelParams[idxWeight].X = weights[0, idxWeight]; } // compute the jacobian matrix NRealMatrix jac = new NRealMatrix(x.Rows, weights.Columns); for (int idxRow = 0; idxRow < x.Rows; idxRow++) { // compute the ouput results CalculateOutput(inputValues[Convert.ToInt32(x[idxRow, 0])]); // backpropagate the delta BackPropagate(); for (int idxVal = 0; idxVal < modelValues.Count; idxVal++) { jac[idxRow, idxVal] = -modelValues[idxVal].X * modelDeltas[idxVal].X; } } return(jac); }; var error = 100.0; int trials = 10; LevenbergMarquardt optimizerOpt = null; _learningRate = 0.0; while (trials-- > 0) { LevenbergMarquardt optimizer = new LevenbergMarquardt(objFunc, inputs, modelParams, modelFunc, jacFunc, 0.001, obj_error, 200, rnd_seed); try { optimizer.Solve(); } catch (StallException) { } if (optimizer.Error < error) { error = optimizer.Error; optimizerOpt = optimizer; _learningRate = Math.Max(_learningRate, (optimizerOpt.StartError - optimizerOpt.Error) / optimizerOpt.StartError); } var rnd = new Random(rnd_seed); rnd_seed = (int)(rnd.NextDouble() * 100.0); for (int idxParam = 0; idxParam < modelParams.Count; idxParam++) { modelParams[idxParam].X = rnd.NextDouble() - 0.5; } } if (optimizerOpt.Error > max_error) { throw new StallException(); } _totalError = optimizerOpt.Error; }
public void Train(List<List<double>> inputValues, List<List<double>> outputValues, double obj_error = 1e-5, double max_error = 1e-5, int rnd_seed = 0) { if (inputValues.Count != outputValues.Count) throw new ApplicationException("Training set inputs and outputs must have the same size"); // input normalization int nbInputs = inputValues[0].Count; NRealMatrix inputTable = new NRealMatrix(inputValues.Count, nbInputs); var maxValue = 0.0; for (int idxInputList = 0; idxInputList < inputValues.Count; idxInputList++) { for (int idxInput = 0; idxInput < inputValues[idxInputList].Count; idxInput++) { if (Math.Abs(inputValues[idxInputList][idxInput]) > maxValue) maxValue = Math.Abs(inputValues[idxInputList][idxInput]); } } for(int idxInputList = 0; idxInputList < inputValues.Count; idxInputList++){ for(int idxInput = 0; idxInput < inputValues[idxInputList].Count; idxInput++) inputTable[idxInputList, idxInput] = inputValues[idxInputList][idxInput] / maxValue; } // output normalization int nbOutputs = outputValues[0].Count; NRealMatrix objectiveTable = new NRealMatrix(outputValues.Count, nbOutputs); maxValue = 0.0; for (int idxOutputList = 0; idxOutputList < outputValues.Count; idxOutputList++) { for (int idxOutput = 0; idxOutput < outputValues[idxOutputList].Count; idxOutput++) { if (Math.Abs(outputValues[idxOutputList][idxOutput]) > maxValue) maxValue = Math.Abs(outputValues[idxOutputList][idxOutput]); } } for(int idxOutputList = 0; idxOutputList < outputValues.Count; idxOutputList++){ for(int idxOutput = 0; idxOutput < outputValues[idxOutputList].Count; idxOutput++) objectiveTable[idxOutputList, idxOutput] = outputValues[idxOutputList][idxOutput] / maxValue; } LevenbergMarquardt.objective_func objFunc = (NRealMatrix x) => { NRealMatrix y = new NRealMatrix(x.Rows, nbOutputs); for (int idxRow = 0; idxRow < y.Rows; idxRow++){ for (int idxCol = 0; idxCol < nbOutputs; idxCol++) y[idxRow, idxCol] = objectiveTable[Convert.ToInt32(x[idxRow,0]), idxCol]; } return y; }; List<double> inputs = new List<double>(); for(int idxOutputList = 0; idxOutputList < outputValues.Count; idxOutputList++) inputs.Add((double)idxOutputList); List<Value> modelParams = getModelParams(); List<Value> modelValues = new List<Value>(); foreach (var layer in _innerLayers) modelValues.AddRange(layer.GetValues()); modelValues.AddRange(_outputs.GetValues()); List<Value> modelDeltas = new List<Value>(); foreach (var layer in _innerLayers) modelDeltas.AddRange(layer.GetDeltas()); modelDeltas.AddRange(_outputs.GetDeltas()); LevenbergMarquardt.model_func modelFunc = (NRealMatrix x, NRealMatrix weights) => { // apply new weights for (int idxWeight = 0; idxWeight < weights.Columns; idxWeight++) modelParams[idxWeight].X = weights[0, idxWeight]; NRealMatrix y = new NRealMatrix(x.Rows, nbOutputs); // foreach set of input data for (int idxRow = 0; idxRow < x.Rows; idxRow++) { // compute the ouput results CalculateOutput(inputValues[Convert.ToInt32(x[idxRow, 0])]); List<double> modelOutputs = GetOutput(); for (int idxCol = 0; idxCol < nbOutputs; idxCol++) y[idxRow, idxCol] = modelOutputs[idxCol]; } return y; }; LevenbergMarquardt.model_func jacFunc = (NRealMatrix x, NRealMatrix weights) => { // apply new weights for (int idxWeight = 0; idxWeight < weights.Columns; idxWeight++) modelParams[idxWeight].X = weights[0, idxWeight]; // compute the jacobian matrix NRealMatrix jac = new NRealMatrix(x.Rows, weights.Columns); for (int idxRow = 0; idxRow < x.Rows; idxRow++) { // compute the ouput results CalculateOutput(inputValues[Convert.ToInt32(x[idxRow, 0])]); // backpropagate the delta BackPropagate(); for (int idxVal = 0; idxVal < modelValues.Count; idxVal++) jac[idxRow, idxVal] = -modelValues[idxVal].X * modelDeltas[idxVal].X; } return jac; }; var error = 100.0; int trials = 10; LevenbergMarquardt optimizerOpt = null; _learningRate = 0.0; while (trials-- > 0) { LevenbergMarquardt optimizer = new LevenbergMarquardt(objFunc, inputs, modelParams, modelFunc, jacFunc, 0.001, obj_error, 200, rnd_seed); try { optimizer.Solve(); } catch (StallException) { } if (optimizer.Error < error) { error = optimizer.Error; optimizerOpt = optimizer; _learningRate = Math.Max(_learningRate, (optimizerOpt.StartError - optimizerOpt.Error) / optimizerOpt.StartError); } var rnd = new Random(rnd_seed); rnd_seed = (int)(rnd.NextDouble() * 100.0); for (int idxParam = 0; idxParam < modelParams.Count; idxParam++) modelParams[idxParam].X = rnd.NextDouble() - 0.5; } if (optimizerOpt.Error > max_error) throw new StallException(); _totalError = optimizerOpt.Error; }
/// <summary> /// /// </summary> /// <param name="a"></param> /// <param name="s"></param> /// <param name="u"></param> /// <param name="vt"></param> public void SvdDecomposition(NRealMatrix a, NRealMatrix s, NRealMatrix u, NRealMatrix vt) { _lib.SVDDecomposition(a.ManagedMatrix, s.ManagedMatrix, u.ManagedMatrix, vt.ManagedMatrix); }
public static void Run(bool generate = false, bool generate_from_db = false) { Dictionary <string, string> dicSettings = new Dictionary <string, string>(); dicSettings["APP_NAME"] = "Midax"; dicSettings["PUBLISHING_START_TIME"] = "2016-01-22 08:00:00"; dicSettings["PUBLISHING_STOP_TIME"] = "2016-01-22 09:00:00"; dicSettings["REPLAY_MODE"] = "CSV"; dicSettings["REPLAY_POPUP"] = "1"; dicSettings["TRADING_START_TIME"] = "2016-01-22 08:45:00"; dicSettings["TRADING_STOP_TIME"] = "2016-01-22 08:59:00"; dicSettings["TRADING_CLOSING_TIME"] = "2016-01-22 08:57:00"; dicSettings["TRADING_MODE"] = "REPLAY"; dicSettings["TRADING_SIGNAL"] = "MacD_1_5_IX.D.DAX.DAILY.IP"; dicSettings["TRADING_LIMIT_PER_BP"] = "10"; dicSettings["TRADING_CURRENCY"] = "GBP"; Config.Settings = dicSettings; string action = generate ? "Generating" : "Testing"; var dax = new MarketData("DAX:IX.D.DAX.DAILY.IP"); List <string> tests = new List <string>(); Console.WriteLine(action + " WMA..."); // Test weighted moving average with long intervals tests.Add(@"..\..\expected_results\testWMA.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(tests); if (generate) { dicSettings["PUBLISHING_CSV"] = string.Format("..\\..\\expected_results\\testWMAgen.csv"); } var macDTestWMA = new ModelMacDTest(dax, 1, 2, 3); MarketDataConnection.Instance.Connect(null); macDTestWMA.StartSignals(); macDTestWMA.StopSignals(); // Test weighted moving average with short intervals tests = new List <string>(); tests.Add(@"..\..\expected_results\testWMA2.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(tests); if (generate) { dicSettings["PUBLISHING_CSV"] = string.Format("..\\..\\expected_results\\testWMA2gen.csv"); } dax.Clear(); macDTestWMA = new ModelMacDTest(dax, 1, 2, 3); MarketDataConnection.Instance.Connect(null); macDTestWMA.StartSignals(); macDTestWMA.StopSignals(); // Test weighted moving average with linear time decay tests = new List <string>(); tests.Add(@"..\..\expected_results\testWMA3.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(tests); dicSettings["TIME_DECAY_FACTOR"] = "3"; if (generate) { dicSettings["PUBLISHING_CSV"] = string.Format("..\\..\\expected_results\\testWMA3gen.csv"); } dax.Clear(); macDTestWMA = new ModelMacDTest(dax, 1, 2, 3); MarketDataConnection.Instance.Connect(null); macDTestWMA.StartSignals(); macDTestWMA.StopSignals(); // Test volume weighted moving average with linear time decay /* * tests = new List<string>(); * tests.Add(@"..\..\expected_results\testWMA4.csv"); * dicSettings["REPLAY_CSV"] = Config.TestList(tests); * if (generate) * dicSettings["PUBLISHING_CSV"] = string.Format("..\\..\\expected_results\\testWMA4gen.csv"); * var macDVTest = new ModelMacDVTest(dax, 1, 2, 3); * MarketDataConnection.Instance.Connect(null); * macDVTest.StartSignals(); * macDVTest.StopSignals();*/ dicSettings.Remove("TIME_DECAY_FACTOR"); // Test RSI and Correlation indicators tests = new List <string>(); tests.Add(@"..\..\expected_results\testRsiCorrel.csv"); dicSettings["INDEX_ICEDOW"] = "DOW:IceConnection_DOW"; dicSettings["INDEX_DOW"] = "DOW:IX.D.DOW.DAILY.IP"; dicSettings["INDEX_DAX"] = "DAX:IX.D.DAX.DAILY.IP"; dicSettings["REPLAY_CSV"] = Config.TestList(tests); if (generate) { dicSettings["PUBLISHING_CSV"] = string.Format("..\\..\\expected_results\\testRsiCorrelgen.csv"); } dax.Clear(); var icedow = new MarketData(dicSettings["INDEX_ICEDOW"]); var dow = new MarketData(dicSettings["INDEX_DOW"]); var macD = new ModelMacDTest(dax, 1, 2, 3); //var macDV = new ModelMacDVTest(icedow, 1, 2, 3, dow); var moleTest = new ModelMoleTest(macD); MarketDataConnection.Instance.Connect(null); macD.StartSignals(false); //macDV.StartSignals(false); moleTest.StartSignals(false); MarketDataConnection.Instance.StartListening(); moleTest.StopSignals(false); //macDV.StartSignals(false); macD.StopSignals(false); MarketDataConnection.Instance.StopListening(); Console.WriteLine(action + " calibration..."); // Test a 1mn linear regression var mktData = new MarketData("testLRMktData"); var updateTime = Config.ParseDateTimeLocal(dicSettings["TRADING_START_TIME"]); mktData.TimeSeries.Add(updateTime, new Price(100)); mktData.TimeSeries.Add(updateTime.AddSeconds(20), new Price(120)); mktData.TimeSeries.Add(updateTime.AddSeconds(40), new Price(140)); mktData.TimeSeries.Add(updateTime.AddSeconds(60), new Price(130)); mktData.TimeSeries.Add(updateTime.AddSeconds(80), new Price(145)); mktData.TimeSeries.Add(updateTime.AddSeconds(100), new Price(165)); mktData.TimeSeries.Add(updateTime.AddSeconds(120), new Price(145)); var linReg = new IndicatorLinearRegression(mktData, new TimeSpan(0, 2, 0)); var linRegCoeff = linReg.linearCoeff(updateTime.AddSeconds(120)); if (Math.Abs(linRegCoeff.Value - 0.821428571428573m) > 1e-8m) { throw new ApplicationException("Linear regression error"); } // Test the optimization of function a * cos(b * x) + b * sin(a * x) using Levenberg Marquardt LevenbergMarquardt.objective_func objFunc = (NRealMatrix x) => { NRealMatrix y = new NRealMatrix(x.Rows, 1); for (int idxRow = 0; idxRow < y.Rows; idxRow++) { y.SetAt(idxRow, 0, new NDouble(2 * Math.Cos(x[idxRow, 0]) + Math.Sin(2 * x[idxRow, 0]))); } return(y); }; List <double> inputs = new List <double>(); Random rnd = new Random(155); for (int idxPt = 0; idxPt < 10; idxPt++) { inputs.Add(rnd.NextDouble() * 2); } List <Value> modelParams = new List <Value>(); modelParams.Add(new Value(-0.2)); modelParams.Add(new Value(0.3)); LevenbergMarquardt.model_func modelFunc = (NRealMatrix x, NRealMatrix weights) => { NRealMatrix y = new NRealMatrix(x.Rows, 1); double a = weights[0, 0]; double b = weights[0, 1]; for (int idxRow = 0; idxRow < y.Rows; idxRow++) { y.SetAt(idxRow, 0, new NDouble(a * Math.Cos(b * x[idxRow, 0]) + b * Math.Sin(a * x[idxRow, 0]))); } return(y); }; Func <double, double, double, double> derA = (double a, double b, double x) => Math.Cos(b * x) + b * x * Math.Cos(a * x); Func <double, double, double, double> derB = (double a, double b, double x) => - a * x * Math.Sin(b * x) + Math.Sin(a * x); LevenbergMarquardt.model_func jacFunc = (NRealMatrix x, NRealMatrix weights) => { NRealMatrix jac = new NRealMatrix(x.Rows, 2); double a = weights[0, 0]; double b = weights[0, 1]; for (int idxRow = 0; idxRow < jac.Rows; idxRow++) { jac.SetAt(idxRow, 0, new NDouble(-derA(a, b, x[idxRow, 0]))); jac.SetAt(idxRow, 1, new NDouble(-derB(a, b, x[idxRow, 0]))); } return(jac); }; LevenbergMarquardt calibModel = new LevenbergMarquardt(objFunc, inputs, modelParams, modelFunc, jacFunc); calibModel.Solve(); if (Math.Abs(modelParams[0].X - 2) > calibModel.ObjectiveError || Math.Abs(modelParams[1].X - 1) > calibModel.ObjectiveError) { throw new ApplicationException("LevenbergMarquardt calibration error"); } // Parity-2 problem NeuralNetwork ann = new NeuralNetwork(2, 1, new List <int>() { 2 }); List <List <double> > annInputs = new List <List <double> >(); annInputs.Add(new List <double>() { -1, -1 }); annInputs.Add(new List <double>() { -1, 1 }); annInputs.Add(new List <double>() { 1, -1 }); annInputs.Add(new List <double>() { 1, 1 }); List <List <double> > annOutputs = new List <List <double> >(); annOutputs.Add(new List <double>() { 1 }); annOutputs.Add(new List <double>() { -1 }); annOutputs.Add(new List <double>() { -1 }); annOutputs.Add(new List <double>() { 1 }); // test forward propagation ann._outputs.Neurons[0].Weights[0].X = 1; ann._outputs.Neurons[0].Weights[1].X = -1; ann._outputs.Neurons[0].Weights[2].X = -1; ann._innerLayers[0].Neurons[0].Weights[0].X = 1; ann._innerLayers[0].Neurons[0].Weights[1].X = 1; ann._innerLayers[0].Neurons[0].Weights[2].X = 1; ann._innerLayers[0].Neurons[1].Weights[0].X = 1; ann._innerLayers[0].Neurons[1].Weights[1].X = 1; ann._innerLayers[0].Neurons[1].Weights[2].X = -1; ann._inputs.Neurons[0].Value.X = -1; ann._inputs.Neurons[1].Value.X = -1; if (Math.Abs(ann._outputs.Neurons[0].Activation() - -0.38873457229297215) > calibModel.ObjectiveError) { throw new ApplicationException("Neural network forward propagation error"); } // Test neural network training for parity-2 problem ann = new NeuralNetwork(2, 1, new List <int>() { 2 }); ann.Train(annInputs, annOutputs); // Test neural network training for parity-3 problem ann = new NeuralNetwork(3, 1, new List <int>() { 2 }); annInputs = new List <List <double> >(); annInputs.Add(new List <double>() { -1, -1, -1 }); annInputs.Add(new List <double>() { -1, -1, 1 }); annInputs.Add(new List <double>() { -1, 1, -1 }); annInputs.Add(new List <double>() { -1, 1, 1 }); annInputs.Add(new List <double>() { 1, -1, -1 }); annInputs.Add(new List <double>() { 1, -1, 1 }); annInputs.Add(new List <double>() { 1, 1, -1 }); annInputs.Add(new List <double>() { 1, 1, 1 }); annOutputs = new List <List <double> >(); annOutputs.Add(new List <double>() { -1 }); annOutputs.Add(new List <double>() { 1 }); annOutputs.Add(new List <double>() { 1 }); annOutputs.Add(new List <double>() { -1 }); annOutputs.Add(new List <double>() { 1 }); annOutputs.Add(new List <double>() { -1 }); annOutputs.Add(new List <double>() { -1 }); annOutputs.Add(new List <double>() { 1 }); ann.Train(annInputs, annOutputs); Console.WriteLine(action + " live indicators and signals..."); tests = new List <string>(); tests.Add(@"..\..\expected_results\core_22_1_2016.csv"); if (generate_from_db) { dicSettings["DB_CONTACTPOINT"] = "192.168.1.26"; } dicSettings["REPLAY_MODE"] = generate_from_db ? "DB" : "CSV"; dicSettings["REPLAY_CSV"] = Config.TestList(tests); if (generate) { dicSettings["PUBLISHING_CSV"] = string.Format("..\\..\\expected_results\\coregen_22_1_2016.csv"); } MarketDataConnection.Instance.Connect(null); dax.Clear(); var model = new ModelMacDTest(dax); model.StartSignals(); Console.WriteLine(action + " daily indicators..."); model.StopSignals(); Thread.Sleep(1000); if (!dicSettings.ContainsKey("PUBLISHING_CSV")) { // the program is expected to throw exceptions in this scope, just press continue if you are debugging // all exceptions should be handled, and the program should terminate with a success message box // test that the right numer of trades was placed. this is an extra sanity check to make sure the program is not idle if (ReplayTester.Instance.NbProducedTrades != ReplayTester.Instance.NbExpectedTrades) { model.ProcessError(string.Format("the model did not produced the expected number of trades. It produced {0} trades instead of {1} expected", ReplayTester.Instance.NbProducedTrades, ReplayTester.Instance.NbExpectedTrades)); } // test trade booking MarketDataConnection.Instance = new ReplayConnection(true); model = new ModelMacDTest(dax); MarketDataConnection.Instance.Connect(null); Console.WriteLine(action + " trade booking..."); var tradeTime = Config.ParseDateTimeLocal(dicSettings["TRADING_CLOSING_TIME"]).AddSeconds(-1); var tradeTest = new Trade(tradeTime, dax.Id, SIGNAL_CODE.SELL, 10, 10000m); var expectedTrades = new Dictionary <KeyValuePair <string, DateTime>, Trade>(); expectedTrades[new KeyValuePair <string, DateTime>("###DUMMY_TRADE_REF1###", tradeTime)] = tradeTest; ReplayTester.Instance.SetExpectedResults(null, null, expectedTrades, null); var task = model.PTF.Subscribe(); task.Wait(); model.PTF.BookTrade(tradeTest, dax.Name); Thread.Sleep(5000); if (model.PTF.GetPosition(tradeTest.Epic).Quantity != -10) { throw new ApplicationException("SELL Trade booking error"); } var expectedTrade = new Trade(tradeTime, dax.Id, SIGNAL_CODE.BUY, 10, 10000m); expectedTrade.Reference = "###CLOSE_DUMMY_TRADE_REF2###"; expectedTrade.Id = "###DUMMY_TRADE_ID1###"; expectedTrades[new KeyValuePair <string, DateTime>(expectedTrade.Reference, tradeTime)] = expectedTrade; model.PTF.ClosePosition(tradeTest, tradeTime); Thread.Sleep(5000); if (model.PTF.GetPosition(tradeTest.Epic).Quantity != 0) { throw new ApplicationException("Trade position closing error"); } expectedTrade.Reference = "###DUMMY_TRADE_REF3###"; expectedTrade.Id = "###DUMMY_TRADE_ID2###"; expectedTrades[new KeyValuePair <string, DateTime>(expectedTrade.Reference, tradeTime)] = expectedTrade; model.PTF.BookTrade(new Trade(tradeTest, true, tradeTime), dax.Name); Thread.Sleep(5000); if (model.PTF.GetPosition(tradeTest.Epic).Quantity != 10) { throw new ApplicationException("BUY Trade booking error"); } expectedTrade = new Trade(tradeTime, dax.Id, SIGNAL_CODE.SELL, 10, 0m); expectedTrade.Reference = "###CLOSE_DUMMY_TRADE_REF4###"; expectedTrade.Id = "###DUMMY_TRADE_ID2###"; expectedTrades[new KeyValuePair <string, DateTime>(expectedTrade.Reference, tradeTime)] = expectedTrade; Portfolio.Instance.CloseAllPositions(tradeTest.TradingTime); Thread.Sleep(5000); // test synchronization issues with the broker List <string> testsSync = new List <string>(); testsSync.Add(@"..\..\expected_results\sync.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(testsSync); MarketDataConnection.Instance = new ReplayCrazySeller(); model = new ModelMacDTest(dax); Console.WriteLine(action + " synchronization..."); MarketDataConnection.Instance.Connect(null); model.StartSignals(); model.StopSignals(); testsSync = new List <string>(); testsSync.Add(@"..\..\expected_results\sync2.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(testsSync); MarketDataConnection.Instance = new ReplayCrazyBuyer(); model = new ModelMacDTest(dax); MarketDataConnection.Instance.Connect(null); model.StartSignals(); model.StopSignals(); Console.WriteLine(action + " expected exceptions..."); dicSettings["REPLAY_CSV"] = Config.TestList(tests); MarketDataConnection.Instance = new ReplayConnection(true); MarketDataConnection.Instance.Connect(null); List <string> testError = new List <string>(); testError.Add(@"..\..\expected_results\error.csv"); dicSettings["REPLAY_CSV"] = Config.TestList(testError); var modelErr = new ModelMacDTest(dax); string expected; bool success = false; try { MarketDataConnection.Instance.Connect(null); modelErr.StartSignals(); } catch (Exception exc) { expected = "Test failed: indicator EMA_1_IX.D.DAX.DAILY.IP time 08:30 expected value 9740.791666666666666666666667 != 9740.3"; success = (exc.Message.Replace(" AM", "") == expected); if (!success) { model.ProcessError(exc.Message, expected); } } if (!success) { model.ProcessError("An expected exception has not been thrown"); } success = false; try { modelErr.StopSignals(); } catch (Exception exc) { model.ProcessError(exc.Message + " - Wrong daily mean exception removed"); } success = false; try { model.StopSignals(); } catch (Exception exc) { model.ProcessError(exc.Message + " - Double EOD publishing exception removed"); } success = false; try { MarketDataConnection.Instance = new ReplayConnection(true); MarketDataConnection.Instance.Connect(null); model = new ModelMacDTest(new MarketData(dax.Id)); model.StartSignals(); } catch (Exception exc) { expected = "Test failed: indicator EMA_1_IX.D.DAX.DAILY.IP time 08:30 expected value 9740.791666666666666666666667 != 9740.3"; success = (exc.Message.Replace(" AM", "") == expected); if (!success) { model.ProcessError(exc.Message, expected); } } if (!success) { model.ProcessError("An expected exception has not been thrown"); } success = false; try { MarketDataConnection.Instance.Resume(); } catch (Exception exc) { expected = "Time series do not accept values in the past"; success = (exc.Message.Replace(" AM", "") == expected); if (!success) { model.ProcessError(exc.Message, expected); } } if (!success) { model.ProcessError("An expected exception has not been thrown"); } model.StopSignals(); success = false; } }