ankane / prophet-ruby

Time series forecasting for Ruby
MIT License
405 stars 11 forks source link

Help: How to get components values? #19

Closed leoplct closed 6 months ago

leoplct commented 6 months ago

Hello,

I am looking for a way to get the components of the forecasted series.

I know I could use this but I need the numeric values, not the chart.

m.plot_components(forecast).savefig("components.png")

image

Inspecting the code I found this

model.predict(future_days(days: days))
puts model.seasonalities.inspect

but the output is this

{"weekly"=>{:period=>7, :fourier_order=>3, :prior_scale=>10.0, :mode=>"multiplicative", :condition_name=>nil}} {"weekly"=>{:period=>7, :fourier_order=>3, :prior_scale=>10.0, :mode=>"multiplicative", :condition_name=>nil}}

ankane commented 6 months ago

Hi @leoplct, the data frame returned from model.predict() should have it.

leoplct commented 6 months ago

@ankane The method .seasonalities doesn't exist.

r = model.predict(future_days(days: days))
puts r.seasonalities.inspect
/app/services/forecaster/forecaster.rb:8:in `results': undefined method `seasonalities' for                      ds               trend          yhat_lower          yhat_upper         trend_lower (NoMethodError)
2020-01-01 00:00:00 UTC          132.457668  102.54412310794025   160.7679458245698          132.457668
2020-01-02 00:00:00 UTC  135.52352290909093  106.29359243905199  163.72024324319386  135.52352290909093
2020-01-03 00:00:00 UTC  138.58937782025072   108.6678132212479  169.89344531863384  138.58937782025072
2020-01-04 00:00:00 UTC  141.65523271340436  110.02777864484085  171.52578446670833  141.65523271340436
2020-01-05 00:00:00 UTC   144.7210876055361  114.07941973515894  173.76046504415518   144.7210876055361
                    ...                 ...                 ...                 ...                 ...
2020-02-07 00:00:00 UTC  245.89429893658695  215.64588805741883   277.5255988074532  245.89427912161068
2020-02-08 00:00:00 UTC  248.96015382504646  219.92458120979668  279.48258544843526  248.96013285568122
2020-02-09 00:00:00 UTC   252.0260087135059  225.84879229851205   281.4402568223808  252.02598634948285
2020-02-10 00:00:00 UTC  255.09186360196537  226.73538315722791   287.3103036278492   255.0918399893096
2020-02-11 00:00:00 UTC   258.1577184904248   228.4267107916969   285.5872656861613  258.15769368316364

       trend_upper  Amazon events  Amazon events_lower  Amazon events_upper  holidays  holidays_lower  holidays_upper
        132.457668            0.0                  0.0                  0.0       0.0             0.0             0.0
135.52352290909093            0.0                  0.0                  0.0       0.0             0.0             0.0
138.58937782025072            0.0                  0.0                  0.0       0.0             0.0             0.0
141.65523271340436            0.0                  0.0                  0.0       0.0             0.0             0.0
 144.7210876055361            0.0                  0.0                  0.0       0.0             0.0             0.0
               ...            ...                  ...                  ...       ...             ...             ...
245.89431759518342            0.0                  0.0                  0.0       0.0             0.0             0.0
248.96017402538914            0.0                  0.0                  0.0       0.0             0.0             0.0
252.02602978164828            0.0                  0.0                  0.0       0.0             0.0             0.0
255.09188646235285            0.0                  0.0                  0.0       0.0             0.0             0.0
 258.1577422432756            0.0                  0.0                  0.0       0.0             0.0             0.0

multiplicative_terms  multiplicative_terms_lower  multiplicative_terms_upper  col  col_lower  col_upper  additive_terms
                 0.0                         0.0                         0.0  0.0        0.0        0.0             0.0
                 0.0                         0.0                         0.0  0.0        0.0        0.0             0.0
                 0.0                         0.0                         0.0  0.0        0.0        0.0             0.0
                 0.0                         0.0                         0.0  0.0        0.0        0.0             0.0
                 0.0                         0.0                         0.0  0.0        0.0        0.0             0.0
                 ...                         ...                         ...  ...        ...        ...             ...
                 0.0                         0.0                         0.0  0.0        0.0        0.0             0.0
                 0.0                         0.0                         0.0  0.0        0.0        0.0             0.0
                 0.0                         0.0                         0.0  0.0        0.0        0.0             0.0
                 0.0                         0.0                         0.0  0.0        0.0        0.0             0.0
                 0.0                         0.0                         0.0  0.0        0.0        0.0             0.0

additive_terms_lower  additive_terms_upper                yhat
                 0.0                   0.0          132.457668
                 0.0                   0.0  135.52352290909093
                 0.0                   0.0  138.58937782025072
                 0.0                   0.0  141.65523271340436
                 0.0                   0.0   144.7210876055361
                 ...                   ...                 ...
                 0.0                   0.0  245.89429893658695
                 0.0                   0.0  248.96015382504646
                 0.0                   0.0   252.0260087135059
                 0.0                   0.0  255.09186360196537
                 0.0                   0.0   258.1577184904248:Rover::DataFrame

    puts r.seasonalities.inspect
          ^^^^^^^^^^^^^^
leoplct commented 6 months ago

@ankane Any news on this?

ankane commented 6 months ago

If there's weekly seasonality, there will be weekly, weekly_lower, and weekly_upper columns in the data frame.

leoplct commented 2 weeks ago

Hello @ankane, I'm still trying to get the 7 values of weekly seasonality used in the matplot chart. As you suggested I tried with this code but I got 2905 elements. I understand that each point is the weekly value of each day, but I am interested in the exactly the same 7 points used to build the chart. I need to show the chart on the end user but I cannot use matplot.

 df = Rover.read_csv(URI.open("https://raw.githubusercontent.com/ankane/prophet-ruby/master/examples/example_wp_log_peyton_manning.csv"))

m = Prophet.new(seasonality_mode: "multiplicative")
m.fit(df)
m.predict
m.predict['weekly']
m.predict['weekly'].size
=> 2905
ankane commented 2 weeks ago

The weekly column should have 7 repeating values (repeating with very small differences).

leoplct commented 2 weeks ago

I found that rounding by 4th digits I got 7 values

df = Rover.read_csv(URI.open("https://raw.githubusercontent.com/ankane/prophet-ruby/master/examples/example_wp_log_peyton_manning.csv"))
m = Prophet.new(seasonality_mode: "multiplicative")
m.fit(df)
m.predict['weekly'].map{|w| w.round(4)}.tally
=> {0.0433=>413, 0.0146=>416, -0.0083=>417, -0.009=>413, -0.0086=>417, -0.0381=>415, 0.0062=>414}

So to get the coefficients for each day of the week I can use

days_of_week = %w[Monday Tuesday Wednesday Thursday Friday Saturday Sunday]
values = m.predict['weekly'].map{|w| w.round(4)}.to_a.tally.keys
mapped_days = days_of_week.zip(values).to_h
mapped_days

It looks not very robust. For example, the first value is Monday or Sunday? Any other better way to have coefficients for each day of the week?

ankane commented 2 weeks ago

Please use other resources for additional help with this (the current API provides everything that's needed to do this in a robust way).

leoplct commented 2 weeks ago

What are other resources? Could you just share me the right code to get the same coefficients used to generate the matplot chart? I think it could be useful for other people too.