Exemplo n.º 1
0
        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]);
        }
Exemplo n.º 2
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;
            }
        }
Exemplo n.º 3
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]));
        }
Exemplo n.º 4
0
        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]));
        }
Exemplo n.º 5
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;
     }
 }