예제 #1
0
 public RegressionResult(ILevels independent_, DatedDataCollectionGen<double[]> regDataInput_, NMathStats.LinearRegression lin_, NMathStats.LinearRegressionAnova anova_)
 {
   m_independent = independent_;
   m_lin = lin_;
   m_regressionData = regDataInput_;
   m_anova = anova_;
 }
예제 #2
0
		public void Evaluate(bool addIntercept_)
		{
			NMathCore.DoubleMatrix matrix = new CenterSpace.NMath.Core.DoubleMatrix(m_regressionMatrix);
			NMathCore.DoubleVector obs = new CenterSpace.NMath.Core.DoubleVector(m_observations);

			m_lin = new NMathStats.LinearRegression(matrix, obs, addIntercept_);
			m_anova = new CenterSpace.NMath.Stats.LinearRegressionAnova(m_lin);
			m_params = null;

			NotifyAll();
		}
예제 #3
0
    public static NMathStats.LinearRegression DoRegression(SwapsSummaryRow summaryRow_, Instrument regressAgainstThis_, int numDays_ = 252, bool withIntercept_=true)
    {
      var ys = summaryRow_.GetRawStatSeries(StatCols.Values);

      if (ys.Length < numDays_)
        return null;

      ys = ys.GetEndValues(numDays_);

      var xs = regressAgainstThis_.A_Prices.Data.Slice(regressAgainstThis_.A_Prices.Length - numDays_, numDays_);

      NMathCore.DoubleVector yV = new NMathCore.DoubleVector(ys.Data);
      NMathCore.DoubleMatrix matrix = new NMathCore.DoubleMatrix(xs.ToColumn());

      var lin = new NMathStats.LinearRegression(obs: yV, A: matrix, addIntercept: withIntercept_);

      return lin;
    }
    internal static RegressionResult DoRegression(this ILevels levels_, RegressionArguments args_)
    {
      try
      {
        var myLevels = levels_.Levels;
        var independentLevels = args_.Independent.Levels;

        if (args_.RegressOn == RegressOn.Differences)
        {
          myLevels = myLevels.ToDifferences();
          independentLevels = independentLevels.ToDifferences();
        }

        var m_regressionData =
          myLevels.GetEndValues(args_.RegressionWindowLength).IntersectDates(independentLevels);

        NMathCore.DoubleVector yV = new NMathCore.DoubleVector(m_regressionData.Data.Select(x => x[0]).ToArray());
        NMathCore.DoubleMatrix matrix = new NMathCore.DoubleMatrix(m_regressionData.Data.Select(x => x[1]).ToArray().ToColumn());

        var lin = new NMathStats.LinearRegression(obs: yV, A: matrix, addIntercept: true);
        var anova = new NMathStats.LinearRegressionAnova(lin);

        return new RegressionResult(args_.Independent, m_regressionData, lin, anova);
      }
      catch (Exception ex_)
      {
        Logger.Error(string.Format("Error recalculating regression for {0}", levels_.Name), typeof(RegressionExtensionMethods), ex_);

        return null;
      }
    }
    internal static DatedDataCollectionGen<double> GetRollingRegressionField(this ILevels levels_,  RegressionArguments args_, RegressionField field_)
    {
      DatedDataCollectionGen<double> ret = null;
      try
      {
        var myLevels = levels_.Levels;
        var independentLevels = args_.Independent.Levels;

        if (args_.RegressOn == RegressOn.Differences)
        {
          myLevels = myLevels.ToDifferences();
          independentLevels = independentLevels.ToDifferences();
        }

        var m_regressionData =
          myLevels.IntersectDates(independentLevels);

        if (m_regressionData.Length < args_.RegressionWindowLength)
          return null;

        var myBucket = new List<double>();
        var indepBucket = new List<double>();
        var betas = new List<double>();
        var dates = new List<DateTime>();

        for (int i = 0; i < m_regressionData.Length; ++i)
        {
          myBucket.Add(m_regressionData.Data[i][0]);
          indepBucket.Add(m_regressionData.Data[i][1]);

          if (myBucket.Count < args_.RegressionWindowLength)
            continue;

          while (myBucket.Count > args_.RegressionWindowLength)
          {
            myBucket.RemoveAt(0);
            indepBucket.RemoveAt(0);
          }

          var yV = new NMathCore.DoubleVector(myBucket.ToArray());
          var matrix = new NMathCore.DoubleMatrix(indepBucket.ToArray().ToColumn());

          try
          {
            var lin = new NMathStats.LinearRegression(obs: yV, A: matrix, addIntercept: true);
            var anova = new NMathStats.LinearRegressionAnova(lin);

            var result = new RegressionResult(args_.Independent, m_regressionData, lin, anova);

            betas.Add(result[field_]);
            dates.Add(m_regressionData.Dates[i]);
          }
          catch { }
        }

        ret = new DatedDataCollectionGen<double>(dates.ToArray(), betas.ToArray());
      }
      catch (Exception ex_)
      {
        Logger.Error("Error generated doing rolling beta", typeof(RegressionExtensionMethods), ex_);
        return null;
      }
      return ret;
    }