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 void Create(RegressionArguments[] settings_)
    {
      m_allSettings = settings_;
      tbRegWindowLength.Bind(settings_[0], "RegressionWindowLength", new Validators.IntValidator());

      m_allSettings[0].PropertyChanged += (x, y) =>
      {
        if (String.Compare("RegressionWindowLength", y.PropertyName, StringComparison.OrdinalIgnoreCase) == 0)
          sync();
      };
    }
    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;
    }