title: how to vectorize forecasting when using polynomials in numpy
body: <p>essentially would like to find the most efficient solution (<code>numpy</code>) that essentially allows me to extend <code>np.poly1d</code> to K dimensions.</p>
  
  <p>test case:</p>
  
  <pre><code>import numpy as np
  import pandas as pd
  import matplotlib.pyplot as plt
  
  
  class Polyfit:
  
      @staticmethod
      def from_fit_to_forecast(df, forecast_values, dates_forward, x_data, y_data, order=2):
          # nice vectorized params estimation
          all_params = np.polyfit(x_data, y_data, order)
  
          # terrible fit of data as I loop over them
          new_df = pd.DataFrame([np.poly1d(i)(x_data) for i in all_params.T], columns=df.index, index=df.columns).T
          forecast_df_second = pd.DataFrame(
              [np.poly1d(i)(forecast_values) for i in all_params.T], columns=dates_forward, index=df.columns).T
  
          return new_df, forecast_df_second
  
      @staticmethod
      def gen_data(k_steps):
          data = 1 + np.random.rand(100, 4) / 300 - (np.random.rand(100, 4) / 10) ** 2
          dates = pd.date_range('2010-1-1', freq='D', periods=100)
          dates_forward = pd.date_range(max(dates) + pd.Timedelta(1, unit='d'), freq='D', periods=k_steps)
          return pd.DataFrame(data, columns=list('ABCD'), index=dates).cumprod(), dates_forward
  
      def __init__(self, k_steps_forward=20):
          self.original_data, dates_forward = self.gen_data(k_steps_forward)
  
          x_data = list(range(len(self.original_data.index)))
          max_x_data = max(x_data)
          forecast_values = list(range(max_x_data + 1, max_x_data + 1 + k_steps_forward, 1))
          y_data = self.original_data.values
  
          self.fit_df_2, self.forecast_2 = self.from_fit_to_forecast(
              self.original_data, forecast_values, dates_forward, x_data, y_data, order=2)
  
  cls = Polyfit(k_steps_forward=30)
  
  print(cls.fit_df_2)
  print(cls.forecast_2)
  </code></pre>
  
  <p>the critical point is in the <code>from_fit_to_forecast</code> where I do this:</p>
  
  <pre><code>[np.poly1d(i)(forecast_values) for i in all_params.T]
  </code></pre>
  
  <p>which slows things down considerably. Also, since I would also be using the 2nd order polynomial, I tried playing around with <code>np.dot</code> or other things that work with matrixes but no avail.</p>
  
  <p>any suggestions?</p>