In [68]:
import pandas as pd
from fbprophet import Prophet 
df = pd.read_csv('https://raw.githubusercontent.com/facebook/prophet/master/examples/example_wp_log_peyton_manning.csv')
df.head()
Out[68]:
ds y
0 2007-12-10 9.590761
1 2007-12-11 8.519590
2 2007-12-12 8.183677
3 2007-12-13 8.072467
4 2007-12-14 7.893572

PRACTICING WITH PEYTON

In [69]:
m = Prophet()
m.fit(df)
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
Out[69]:
<fbprophet.forecaster.Prophet at 0x7f4f3ad41e80>
In [70]:
future = m.make_future_dataframe(periods=365)
future.tail()
Out[70]:
ds
3265 2017-01-15
3266 2017-01-16
3267 2017-01-17
3268 2017-01-18
3269 2017-01-19
In [71]:
forecast = m.predict(future)
forecast.tail()
Out[71]:
ds trend yhat_lower yhat_upper trend_lower trend_upper additive_terms additive_terms_lower additive_terms_upper weekly weekly_lower weekly_upper yearly yearly_lower yearly_upper multiplicative_terms multiplicative_terms_lower multiplicative_terms_upper yhat
3265 2017-01-15 7.180847 7.484119 8.912103 6.837290 7.526652 1.018428 1.018428 1.018428 0.048295 0.048295 0.048295 0.970133 0.970133 0.970133 0.0 0.0 0.0 8.199274
3266 2017-01-16 7.179809 7.890993 9.241308 6.834516 7.526953 1.344435 1.344435 1.344435 0.352287 0.352287 0.352287 0.992148 0.992148 0.992148 0.0 0.0 0.0 8.524244
3267 2017-01-17 7.178771 7.619836 9.010513 6.831743 7.527199 1.132844 1.132844 1.132844 0.119624 0.119624 0.119624 1.013220 1.013220 1.013220 0.0 0.0 0.0 8.311615
3268 2017-01-18 7.177733 7.420357 8.872825 6.828543 7.527256 0.966499 0.966499 0.966499 -0.066647 -0.066647 -0.066647 1.033146 1.033146 1.033146 0.0 0.0 0.0 8.144232
3269 2017-01-19 7.176695 7.412110 8.864867 6.825897 7.527090 0.979396 0.979396 0.979396 -0.072284 -0.072284 -0.072284 1.051680 1.051680 1.051680 0.0 0.0 0.0 8.156091
In [72]:
fig1 = m.plot(forecast)
In [73]:
fig2 = m.plot_components(forecast)

PRACTICING WITH ZILLOW

In [0]:
df = pd.read_csv('https://raw.githubusercontent.com/danielcaraway/data/master/Zip_Zhvi_SingleFamilyResidence.csv', encoding='latin')
In [0]:
CA = df[df.State == 'CA']
LA = CA[CA.CountyName == 'Los Angeles County']
mine = LA[LA.RegionName == 90039]
In [0]:
# df = mine.copy()
# df_t = df.loc[:, '1997-01':'2018-12'].T
In [77]:
df = mine.copy()
df_t = df.loc[:, '1997-01':'2018-12'].T
df = df_t.copy()
df.reset_index(inplace=True)
# df['ds'] = df[df.columns[1]] + '-01'
df.columns = ['ds','y']
# df['ds'] = df['ds'] + '-01'
df.head()
Out[77]:
ds y
0 1997-01 147935.0
1 1997-02 148799.0
2 1997-03 149280.0
3 1997-04 149552.0
4 1997-05 149320.0
In [78]:
df.tail()
Out[78]:
ds y
259 2018-08 868462.0
260 2018-09 879218.0
261 2018-10 888896.0
262 2018-11 903051.0
263 2018-12 914058.0
In [79]:
m = Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=365)
future.tail()
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
Out[79]:
ds
624 2019-11-27
625 2019-11-28
626 2019-11-29
627 2019-11-30
628 2019-12-01
In [80]:
forecast = m.predict(future)
forecast.tail()

fig1 = m.plot(forecast)
In [81]:
# df = pd.read_csv('../examples/example_retail_sales.csv')
m = Prophet(seasonality_mode='multiplicative').fit(df)
future = m.make_future_dataframe(periods=48)
fcst = m.predict(future)
fig = m.plot(fcst)
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
In [82]:
fcst
Out[82]:
ds trend yhat_lower yhat_upper trend_lower trend_upper multiplicative_terms multiplicative_terms_lower multiplicative_terms_upper yearly yearly_lower yearly_upper additive_terms additive_terms_lower additive_terms_upper yhat
0 1997-01-01 140047.491970 122187.251125 156589.993286 140047.491970 140047.491970 -0.001828 -0.001828 -0.001828 -0.001828 -0.001828 -0.001828 0.0 0.0 0.0 139791.432687
1 1997-02-01 141835.867074 124226.306882 157792.323187 141835.867074 141835.867074 -0.000150 -0.000150 -0.000150 -0.000150 -0.000150 -0.000150 0.0 0.0 0.0 141814.531781
2 1997-03-01 143451.173619 128994.542548 160912.284827 143451.173619 143451.173619 0.001271 0.001271 0.001271 0.001271 0.001271 0.001271 0.0 0.0 0.0 143633.507654
3 1997-04-01 145239.548723 127215.506933 161422.985381 145239.548723 145239.548723 0.002450 0.002450 0.002450 0.002450 0.002450 0.002450 0.0 0.0 0.0 145595.329110
4 1997-05-01 146970.234308 130267.104554 164323.678412 146970.234308 146970.234308 0.003117 0.003117 0.003117 0.003117 0.003117 0.003117 0.0 0.0 0.0 147428.370071
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
307 2019-01-14 884823.065837 851367.278144 884940.626978 884823.065837 884823.065837 -0.018989 -0.018989 -0.018989 -0.018989 -0.018989 -0.018989 0.0 0.0 0.0 868020.766237
308 2019-01-15 884994.214243 851579.017831 885911.379416 884994.214243 884994.214243 -0.019171 -0.019171 -0.019171 -0.019171 -0.019171 -0.019171 0.0 0.0 0.0 868027.822692
309 2019-01-16 885165.362649 851019.984804 884712.932237 885165.362649 885165.362649 -0.019036 -0.019036 -0.019036 -0.019036 -0.019036 -0.019036 0.0 0.0 0.0 868315.223924
310 2019-01-17 885336.511055 851721.103683 887087.420135 885336.511055 885336.511055 -0.018594 -0.018594 -0.018594 -0.018594 -0.018594 -0.018594 0.0 0.0 0.0 868874.659050
311 2019-01-18 885507.659461 852089.311855 887635.061339 885507.659461 885507.659461 -0.017864 -0.017864 -0.017864 -0.017864 -0.017864 -0.017864 0.0 0.0 0.0 869689.121422

312 rows × 16 columns

In [83]:
len(df)
Out[83]:
264
In [97]:
m = Prophet(seasonality_mode='multiplicative', mcmc_samples=264).fit(df)
future = m.make_future_dataframe(periods=120, freq='M')
fcst = m.predict(future)
fig = m.plot(fcst)
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
WARNING:pystan:Rhat above 1.1 or below 0.9 indicates that the chains very likely have not mixed
WARNING:pystan:182 of 528 iterations saturated the maximum tree depth of 10 (34.5 %)
WARNING:pystan:Run again with max_treedepth larger than 10 to avoid saturation
In [85]:
m = Prophet(seasonality_mode='multiplicative', mcmc_samples=264).fit(df)
future = m.make_future_dataframe(periods=12, freq='M')
fcst = m.predict(future)
fig = m.plot(fcst)
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
WARNING:pystan:Rhat above 1.1 or below 0.9 indicates that the chains very likely have not mixed
WARNING:pystan:177 of 528 iterations saturated the maximum tree depth of 10 (33.5 %)
WARNING:pystan:Run again with max_treedepth larger than 10 to avoid saturation
In [86]:
df
Out[86]:
ds y
0 1997-01 147935.0
1 1997-02 148799.0
2 1997-03 149280.0
3 1997-04 149552.0
4 1997-05 149320.0
... ... ...
259 2018-08 868462.0
260 2018-09 879218.0
261 2018-10 888896.0
262 2018-11 903051.0
263 2018-12 914058.0

264 rows × 2 columns

In [87]:
m = Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=12, freq='M')
future.tail()
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
Out[87]:
ds
271 2019-07-31
272 2019-08-31
273 2019-09-30
274 2019-10-31
275 2019-11-30
In [88]:
forecast = m.predict(future)
forecast.tail()

fig1 = m.plot(forecast)
In [89]:
forecast.tail()
Out[89]:
ds trend yhat_lower yhat_upper trend_lower trend_upper additive_terms additive_terms_lower additive_terms_upper yearly yearly_lower yearly_upper multiplicative_terms multiplicative_terms_lower multiplicative_terms_upper yhat
271 2019-07-31 918473.369818 902613.865705 938495.782575 913448.589808 922065.797615 2323.096416 2323.096416 2323.096416 2323.096416 2323.096416 2323.096416 0.0 0.0 0.0 920796.466234
272 2019-08-31 923759.835346 906457.330437 942717.089958 917444.302157 928700.069581 316.944131 316.944131 316.944131 316.944131 316.944131 316.944131 0.0 0.0 0.0 924076.779476
273 2019-09-30 928875.769727 903878.037350 942229.923960 921240.232748 935426.789393 -5867.834013 -5867.834013 -5867.834013 -5867.834013 -5867.834013 -5867.834013 0.0 0.0 0.0 923007.935714
274 2019-10-31 934162.235255 912278.901934 950962.684107 924779.022588 942742.578093 -1841.329851 -1841.329851 -1841.329851 -1841.329851 -1841.329851 -1841.329851 0.0 0.0 0.0 932320.905404
275 2019-11-30 939278.169636 923329.638442 962380.444046 928995.886814 949423.632385 3670.706646 3670.706646 3670.706646 3670.706646 3670.706646 3670.706646 0.0 0.0 0.0 942948.876282
In [92]:
components_fig = m.plot_components(forecast)
axes = components_fig.get_axes()
axes[0].set_ylabel('TESTING TESTING')
Out[92]:
Text(32.375, 0.5, 'TESTING TESTING')
In [94]:
CA
Out[94]:
RegionID RegionName City State Metro CountyName SizeRank 1996-04 1996-05 1996-06 1996-07 1996-08 1996-09 1996-10 1996-11 1996-12 1997-01 1997-02 1997-03 1997-04 1997-05 1997-06 1997-07 1997-08 1997-09 1997-10 1997-11 1997-12 1998-01 1998-02 1998-03 1998-04 1998-05 1998-06 1998-07 1998-08 1998-09 1998-10 1998-11 1998-12 ... 2016-09 2016-10 2016-11 2016-12 2017-01 2017-02 2017-03 2017-04 2017-05 2017-06 2017-07 2017-08 2017-09 2017-10 2017-11 2017-12 2018-01 2018-02 2018-03 2018-04 2018-05 2018-06 2018-07 2018-08 2018-09 2018-10 2018-11 2018-12 2019-01 2019-02 2019-03 2019-04 2019-05 2019-06 2019-07 2019-08 2019-09 2019-10 2019-11 2019-12
13 97564 94109 San Francisco CA San Francisco-Oakland-Hayward San Francisco County 14 547676.0 547569.0 536539.0 536211.0 536288.0 544996.0 549733.0 548116.0 548056.0 556765.0 564078.0 568911.0 563733.0 567592.0 576927.0 590090.0 601843.0 607732.0 612399.0 618075.0 624078.0 619463.0 608251.0 598124.0 599915.0 605774.0 612060.0 619972.0 630063.0 639649.0 648839.0 660816.0 673743.0 ... 2076723.0 2089536.0 2116069.0 2126269.0 2136544.0 2130549.0 2125238.0 2136355.0 2157234.0 2183082.0 2201513.0 2223619.0 2242654.0 2262404.0 2273661 2288114 2296814 2319273 2340441 2367999 2377043 2406408 2437497 2468207 2497009 2527010 2560823 2571444 2588508 2602537 2603723 2596306 2594652 2619794 2639943 2653455 2659471 2673801 2688969 2717489
21 96107 90250 Hawthorne CA Los Angeles-Long Beach-Anaheim Los Angeles County 22 174520.0 174451.0 174208.0 174168.0 174272.0 174236.0 174011.0 173853.0 173880.0 173809.0 173430.0 172856.0 172239.0 171385.0 170904.0 171034.0 171937.0 172959.0 173708.0 174116.0 174672.0 176446.0 178775.0 180840.0 181787.0 182670.0 183373.0 183753.0 184170.0 185105.0 186263.0 187815.0 188620.0 ... 542639.0 549684.0 554883.0 559601.0 565991.0 572281.0 577192.0 580694.0 585780.0 590338.0 592356.0 593805.0 595088.0 598773.0 604365 610036 616579 623096 630327 635907 639595 642727 642779 642257 642941 647773 652424 654329 654836 654122 653035 654325 656639 660217 657600 656051 656649 661550 665524 670833
39 97771 94565 Pittsburg CA San Francisco-Oakland-Hayward Contra Costa County 40 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 519924.0 520437.0 520271.0 521608.0 526538.0 534053.0 540373.0 544590.0 546909.0 545765.0 545227.0 545969.0 550546.0 552338.0 555309 559167 570681 581688 589077 588331 588979 590588 593489 591277 592299 591549 590131 585682 587867 590192 591096 588183 586736 584946 584380 582565 581526 579674 579779 582825
43 96027 90046 Los Angeles CA Los Angeles-Long Beach-Anaheim Los Angeles County 44 158703.0 158615.0 158990.0 159599.0 160228.0 161176.0 162302.0 163102.0 163659.0 164005.0 164238.0 165215.0 167196.0 168901.0 170638.0 171418.0 173336.0 174781.0 176926.0 178643.0 180776.0 183543.0 186489.0 188650.0 190057.0 192063.0 194619.0 196076.0 197877.0 200594.0 203909.0 206660.0 208435.0 ... 1125483.0 1141019.0 1150330.0 1161896.0 1179904.0 1195337.0 1208427.0 1219575.0 1236950.0 1248887.0 1259457.0 1272009.0 1291359.0 1312526.0 1335419 1356609 1380975 1408153 1434319 1452009 1454944 1455943 1460135 1471859 1488243 1508333 1526004 1537515 1545734 1549924 1550051 1554922 1566579 1580344 1588563 1599202 1619582 1643242 1655745 1668029
87 97711 94501 Alameda CA San Francisco-Oakland-Hayward Alameda County 88 164594.0 164065.0 163554.0 163687.0 164207.0 164802.0 164966.0 164895.0 165257.0 166068.0 167170.0 167651.0 167925.0 168804.0 170949.0 173937.0 176381.0 178295.0 179198.0 179969.0 181228.0 183009.0 185088.0 186707.0 188432.0 189955.0 191916.0 194937.0 198030.0 201294.0 202926.0 204314.0 204491.0 ... 823270.0 832964.0 845602.0 855472.0 862741.0 869135.0 880611.0 888377.0 892645.0 898715.0 900458.0 901673.0 907587.0 917554.0 931512 947430 962747 973865 980028 988153 995688 997469 1003458 1013300 1019174 1023141 1024986 1021306 1013708 1011229 1019833 1029955 1038937 1043473 1051026 1051526 1054081 1050810 1050477 1058938
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
30278 98009 95141 San Jose CA San Jose-Sunnyvale-Santa Clara Santa Clara County 30279 539482.0 532297.0 529040.0 519340.0 509995.0 507596.0 513339.0 521067.0 525147.0 530192.0 533211.0 535826.0 534342.0 538771.0 548248.0 558472.0 563311.0 562934.0 559622.0 551968.0 541129.0 529191.0 523018.0 515872.0 519279.0 523226.0 530816.0 531179.0 531116.0 536700.0 544999.0 555692.0 563530.0 ... 1085551.0 1088423.0 1079231.0 1067071.0 1071790.0 1095731.0 1111667.0 1120839.0 1125443.0 1135403.0 1130279.0 1127394.0 1124116.0 1139792.0 1144735 1158909 1177372 1212086 1224549 1225179 1222547 1230885 1223823 1198951 1175508 1180208 1187717 1193934 1193500 1189134 1180906 1174429 1175094 1184333 1191616 1203807 1191099 1161763 1123331 1105216
30350 98059 95229 Vallecito CA NaN Calaveras County 30351 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 212889.0 214869.0 217488.0 217801.0 217860.0 220071.0 222218.0 222219.0 218200.0 214591.0 216916.0 220715.0 220659.0 217588.0 213734 213025 211969 212102 213038 214920 215538 215920 216355 215913 216553 217207 219360 222307 224479 226993 226685 224245 220362 219113 219777 221118 220298 220130 219565 219973
30420 98400 95721 Twin Bridges CA Sacramento--Roseville--Arden-Arcade El Dorado County 30421 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 221106.0 222405.0 224061.0 226694.0 229175.0 231062.0 231671.0 229550.0 229457.0 228371.0 231913.0 236513.0 241288.0 241506.0 241026 241606 242682 242755 243387 247102 250117 251559 251094 250609 251871 254712 260237 266705 272689 273374 274286 272306 277570 283606 293289 293416 288793 281784 277526 273191
30422 98152 95375 Strawberry CA Sonora Tuolumne County 30423 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN 229014.0 230258.0 232993.0 237994.0 242690.0 245030.0 246624.0 248738.0 251551.0 254167.0 256218 257939 257146 256979 255471 255027 254391 255432 258595 262077 266299 269269 270577 270238 271945 274057 276549 276636 276468 276629 274957 272186 267689 264456 263067 263347
30428 97209 93282 Tulare CA Visalia-Porterville Tulare County 30429 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 148858.0 150809.0 152622.0 155571 156944 157868 160666 163506 164605 162217 161130 160962 161256 160592 159573 159334 158590 158014 155349 153531 153059 155689 156909 157209 156855 156706 156429 155899 157523

1685 rows × 292 columns

In [95]:
LA
Out[95]:
RegionID RegionName City State Metro CountyName SizeRank 1996-04 1996-05 1996-06 1996-07 1996-08 1996-09 1996-10 1996-11 1996-12 1997-01 1997-02 1997-03 1997-04 1997-05 1997-06 1997-07 1997-08 1997-09 1997-10 1997-11 1997-12 1998-01 1998-02 1998-03 1998-04 1998-05 1998-06 1998-07 1998-08 1998-09 1998-10 1998-11 1998-12 ... 2016-09 2016-10 2016-11 2016-12 2017-01 2017-02 2017-03 2017-04 2017-05 2017-06 2017-07 2017-08 2017-09 2017-10 2017-11 2017-12 2018-01 2018-02 2018-03 2018-04 2018-05 2018-06 2018-07 2018-08 2018-09 2018-10 2018-11 2018-12 2019-01 2019-02 2019-03 2019-04 2019-05 2019-06 2019-07 2019-08 2019-09 2019-10 2019-11 2019-12
21 96107 90250 Hawthorne CA Los Angeles-Long Beach-Anaheim Los Angeles County 22 174520.0 174451.0 174208.0 174168.0 174272.0 174236.0 174011.0 173853.0 173880.0 173809.0 173430.0 172856.0 172239.0 171385.0 170904.0 171034.0 171937.0 172959.0 173708.0 174116.0 174672.0 176446.0 178775.0 180840.0 181787.0 182670.0 183373.0 183753.0 184170.0 185105.0 186263.0 187815.0 188620.0 ... 542639.0 549684.0 554883.0 559601.0 565991.0 572281.0 577192.0 580694.0 585780.0 590338.0 592356.0 593805.0 595088.0 598773.0 604365 610036 616579 623096 630327 635907 639595 642727 642779 642257 642941 647773 652424 654329 654836 654122 653035 654325 656639 660217 657600 656051 656649 661550 665524 670833
43 96027 90046 Los Angeles CA Los Angeles-Long Beach-Anaheim Los Angeles County 44 158703.0 158615.0 158990.0 159599.0 160228.0 161176.0 162302.0 163102.0 163659.0 164005.0 164238.0 165215.0 167196.0 168901.0 170638.0 171418.0 173336.0 174781.0 176926.0 178643.0 180776.0 183543.0 186489.0 188650.0 190057.0 192063.0 194619.0 196076.0 197877.0 200594.0 203909.0 206660.0 208435.0 ... 1125483.0 1141019.0 1150330.0 1161896.0 1179904.0 1195337.0 1208427.0 1219575.0 1236950.0 1248887.0 1259457.0 1272009.0 1291359.0 1312526.0 1335419 1356609 1380975 1408153 1434319 1452009 1454944 1455943 1460135 1471859 1488243 1508333 1526004 1537515 1545734 1549924 1550051 1554922 1566579 1580344 1588563 1599202 1619582 1643242 1655745 1668029
98 96015 90034 Los Angeles CA Los Angeles-Long Beach-Anaheim Los Angeles County 99 249808.0 249482.0 250401.0 251056.0 251514.0 251397.0 250283.0 249968.0 250338.0 251984.0 252901.0 254188.0 254578.0 255822.0 256382.0 257680.0 260300.0 263348.0 266764.0 268164.0 270148.0 273509.0 278206.0 282508.0 285693.0 289000.0 292101.0 295544.0 299422.0 303572.0 307245.0 311989.0 316186.0 ... 1098613.0 1116683.0 1129006.0 1141621.0 1155358.0 1168045.0 1176148.0 1183092.0 1192333.0 1199192.0 1190123.0 1184125.0 1188810.0 1214663.0 1247125 1270676 1288018 1305398 1324662 1344220 1351806 1356336 1349641 1345237 1341463 1349432 1353677 1355101 1356760 1357547 1354072 1354989 1360972 1371112 1357947 1349303 1345223 1364396 1377603 1394108
111 96239 90805 Long Beach CA Los Angeles-Long Beach-Anaheim Los Angeles County 112 137817.0 137415.0 137248.0 137198.0 137274.0 137207.0 137069.0 136945.0 136878.0 136589.0 136326.0 135893.0 135554.0 134987.0 134640.0 134405.0 134764.0 135245.0 135914.0 136466.0 137104.0 138896.0 140825.0 142597.0 143056.0 143338.0 143329.0 143061.0 142901.0 143393.0 143855.0 144632.0 144637.0 ... 401058.0 406339.0 409653.0 412212.0 415488.0 419026.0 421879.0 423376.0 426035.0 428631.0 430342.0 431923.0 433537.0 436756.0 442200 447966 452898 457703 462067 465877 467806 469763 470023 469687 468882 470758 472025 472819 472463 471784 470695 471657 472929 475373 474002 473754 474411 479144 483458 488025
113 96193 90650 Norwalk CA Los Angeles-Long Beach-Anaheim Los Angeles County 114 147481.0 147182.0 146908.0 146755.0 146450.0 146097.0 145692.0 145414.0 145158.0 144947.0 144763.0 144611.0 144451.0 144117.0 143890.0 143867.0 144347.0 144856.0 145225.0 145311.0 145568.0 146854.0 148378.0 149777.0 150252.0 150679.0 151164.0 151188.0 151347.0 151833.0 152567.0 153598.0 154239.0 ... 432422.0 436375.0 438859.0 440851.0 444237.0 447533.0 450426.0 452110.0 454994.0 457611.0 459918.0 461409.0 462772.0 464739.0 468851 473157 477343 482122 486469 489710 490805 492170 492651 492327 491356 492278 493275 493810 493767 492713 491619 492074 492861 493873 491980 491376 492376 495968 499044 503135
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
17380 96039 90058 Los Angeles CA Los Angeles-Long Beach-Anaheim Los Angeles County 17381 118410.0 117991.0 120237.0 123068.0 127823.0 131013.0 131861.0 131357.0 131468.0 132496.0 132925.0 133579.0 134290.0 134600.0 132810.0 129681.0 127333.0 127130.0 128622.0 129086.0 129405.0 129024.0 129748.0 130886.0 131032.0 131435.0 131179.0 131632.0 131597.0 129796.0 126555.0 124734.0 125675.0 ... 303186.0 309476.0 314694.0 317294.0 320170.0 321963.0 326344.0 327113.0 330430.0 330553.0 333203.0 335117.0 338396.0 342555.0 346213 350278 353972 359593 364425 370712 373992 377692 378990 380787 379822 378925 378148 379070 380114 380890 380543 381035 381737 384453 385104 386134 386642 389196 391344 395551
18217 97331 93553 Pearblossom CA Los Angeles-Long Beach-Anaheim Los Angeles County 18218 87664.0 86997.0 86637.0 86620.0 87675.0 88586.0 90216.0 90976.0 92204.0 91754.0 91262.0 91141.0 92380.0 92480.0 90945.0 89145.0 88096.0 88948.0 89429.0 89987.0 89813.0 90803.0 91127.0 91897.0 91799.0 91988.0 93062.0 94090.0 95298.0 95325.0 95357.0 96074.0 97123.0 ... 227607.0 231888.0 232896.0 232554.0 234151.0 235742.0 237003.0 238851.0 241674.0 246895.0 248675.0 249350.0 249220.0 251599.0 256143 258672 261423 262936 264584 267527 270417 274536 275100 275549 274737 274805 275723 276405 278666 278833 279837 280583 282099 284206 282677 281829 280265 281574 282796 283995
21032 97324 93544 Llano CA Los Angeles-Long Beach-Anaheim Los Angeles County 21033 128101.0 126755.0 125668.0 125506.0 125855.0 127261.0 128071.0 129294.0 129966.0 130057.0 129332.0 128861.0 130453.0 132076.0 132959.0 132990.0 133831.0 136108.0 139067.0 140720.0 141234.0 139489.0 138698.0 140070.0 142403.0 143346.0 142333.0 140317.0 139114.0 139062.0 139361.0 139690.0 140228.0 ... 226474.0 229057.0 230557.0 231687.0 234412.0 237353.0 240098.0 243184.0 245577.0 247074.0 245587.0 244377.0 245162.0 247782.0 252035 254843 256340 257642 259744 263473 267284 271525 272349 272932 272444 273310 274497 275870 279848 282579 284081 284885 285527 287123 285334 285154 285197 288154 289016 289770
22563 399675 91008 Bradbury CA Los Angeles-Long Beach-Anaheim Los Angeles County 22564 299236.0 299338.0 297435.0 295414.0 291656.0 290623.0 292183.0 294484.0 297310.0 298133.0 302957.0 306087.0 310376.0 310012.0 310798.0 310823.0 312884.0 314229.0 315463.0 314338.0 314685.0 315376.0 318706.0 321743.0 325285.0 326808.0 327076.0 327002.0 328575.0 332404.0 336472.0 343414.0 352301.0 ... 1184504.0 1193048.0 1198569.0 1203923.0 1209900.0 1215592.0 1217751.0 1223244.0 1228543.0 1234276.0 1220389.0 1209054.0 1199230.0 1210158.0 1221662 1232501 1242322 1258584 1272872 1285892 1286602 1289543 1292734 1298696 1304408 1306182 1313124 1321300 1333949 1340705 1340088 1339797 1341705 1347899 1336720 1322992 1310898 1316338 1319847 1322469
29616 97339 93563 Valyermo CA Los Angeles-Long Beach-Anaheim Los Angeles County 29617 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ... 210391.0 208975.0 207852.0 206735.0 208193.0 210037.0 212655.0 212863.0 213990.0 214615.0 214177.0 214457.0 214366.0 216703.0 219395 220129 220932 220555 222510 226429 230833 237241 243458 248587 250603 250190 249598 249665 251241 252341 253674 253702 253460 251749 246804 244706 244372 246366 244359 239410

274 rows × 292 columns

In [0]:
# for 
# m = Prophet(seasonality_mode='multiplicative', mcmc_samples=264).fit(df)
# future = m.make_future_dataframe(periods=120, freq='M')
# fcst = m.predict(future)
# fig = m.plot(fcst)


def prophet_predict_prices_for(df, location_name):
  df_t = df.loc[:, '1997-01':'2017-12'].T
  df_t['avg'] = df_t.mean(numeric_only=True, axis=1)
  df_t.reset_index(inplace=True)
  columns = ['index', 'avg']
  df = pd.DataFrame(df_t, columns = columns)
  df = df.rename(index=str, columns={"avg": "y", "index": "ds"})
  # m = Prophet()
  # m.fit(df)
  m = Prophet(seasonality_mode='multiplicative', mcmc_samples=264).fit(df)
  future = m.make_future_dataframe(periods=120, freq='M')
  # future = m.make_future_dataframe(periods=12, freq='M')
  forecast = m.predict(future)
  print("====",location_name,"====")
  forcast_fig = m.plot(forecast)
  f_axes = forcast_fig.get_axes()
  f_axes[0].set_xlabel('DATE')
  f_axes[0].set_ylabel(location_name)
In [0]:
glendale = LA[LA.City == 'Glendale']
In [109]:
df = glendale.copy()
for zipcode in set(df['RegionName']):
  region_df = df[df['RegionName'] == zipcode]
  prophet_predict_prices_for(region_df, str(zipcode))
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
WARNING:pystan:Rhat above 1.1 or below 0.9 indicates that the chains very likely have not mixed
WARNING:pystan:468 of 528 iterations saturated the maximum tree depth of 10 (88.6 %)
WARNING:pystan:Run again with max_treedepth larger than 10 to avoid saturation
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
==== 91201 ====
WARNING:pystan:Rhat above 1.1 or below 0.9 indicates that the chains very likely have not mixed
WARNING:pystan:496 of 528 iterations saturated the maximum tree depth of 10 (93.9 %)
WARNING:pystan:Run again with max_treedepth larger than 10 to avoid saturation
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
==== 91202 ====
WARNING:pystan:Rhat above 1.1 or below 0.9 indicates that the chains very likely have not mixed
WARNING:pystan:402 of 528 iterations saturated the maximum tree depth of 10 (76.1 %)
WARNING:pystan:Run again with max_treedepth larger than 10 to avoid saturation
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
==== 91203 ====
WARNING:pystan:Rhat above 1.1 or below 0.9 indicates that the chains very likely have not mixed
WARNING:pystan:444 of 528 iterations saturated the maximum tree depth of 10 (84.1 %)
WARNING:pystan:Run again with max_treedepth larger than 10 to avoid saturation
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
==== 91204 ====
WARNING:pystan:Rhat above 1.1 or below 0.9 indicates that the chains very likely have not mixed
WARNING:pystan:477 of 528 iterations saturated the maximum tree depth of 10 (90.3 %)
WARNING:pystan:Run again with max_treedepth larger than 10 to avoid saturation
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
==== 91205 ====
WARNING:pystan:Rhat above 1.1 or below 0.9 indicates that the chains very likely have not mixed
WARNING:pystan:471 of 528 iterations saturated the maximum tree depth of 10 (89.2 %)
WARNING:pystan:Run again with max_treedepth larger than 10 to avoid saturation
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
==== 91206 ====
WARNING:pystan:Rhat above 1.1 or below 0.9 indicates that the chains very likely have not mixed
WARNING:pystan:379 of 528 iterations saturated the maximum tree depth of 10 (71.8 %)
WARNING:pystan:Run again with max_treedepth larger than 10 to avoid saturation
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
==== 91207 ====
WARNING:pystan:Rhat above 1.1 or below 0.9 indicates that the chains very likely have not mixed
WARNING:pystan:459 of 528 iterations saturated the maximum tree depth of 10 (86.9 %)
WARNING:pystan:Run again with max_treedepth larger than 10 to avoid saturation
==== 91208 ====
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
WARNING:pystan:Rhat above 1.1 or below 0.9 indicates that the chains very likely have not mixed
WARNING:pystan:481 of 528 iterations saturated the maximum tree depth of 10 (91.1 %)
WARNING:pystan:Run again with max_treedepth larger than 10 to avoid saturation
==== 91214 ====
In [111]:
df = mine.copy()
df_t = df.loc[:, '1997-01':'2018-12'].T
df = df_t.copy()
df.reset_index(inplace=True)
# df['ds'] = df[df.columns[1]] + '-01'
df.columns = ['ds','y']
# df['ds'] = df['ds'] + '-01'
df.head()
Out[111]:
ds y
0 1997-01 147935.0
1 1997-02 148799.0
2 1997-03 149280.0
3 1997-04 149552.0
4 1997-05 149320.0
In [112]:
m = Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=120, freq='M')
fcst = m.predict(future)
fig = m.plot(fcst)
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
In [113]:
fcst
Out[113]:
ds trend yhat_lower yhat_upper trend_lower trend_upper additive_terms additive_terms_lower additive_terms_upper yearly yearly_lower yearly_upper multiplicative_terms multiplicative_terms_lower multiplicative_terms_upper yhat
0 1997-01-01 1.403879e+05 1.224807e+05 1.550072e+05 1.403879e+05 1.403879e+05 -863.332396 -863.332396 -863.332396 -863.332396 -863.332396 -863.332396 0.0 0.0 0.0 1.395246e+05
1 1997-02-01 1.421767e+05 1.247782e+05 1.592319e+05 1.421767e+05 1.421767e+05 -719.485451 -719.485451 -719.485451 -719.485451 -719.485451 -719.485451 0.0 0.0 0.0 1.414573e+05
2 1997-03-01 1.437924e+05 1.271982e+05 1.610423e+05 1.437924e+05 1.437924e+05 66.933588 66.933588 66.933588 66.933588 66.933588 66.933588 0.0 0.0 0.0 1.438594e+05
3 1997-04-01 1.455813e+05 1.295119e+05 1.623841e+05 1.455813e+05 1.455813e+05 314.960898 314.960898 314.960898 314.960898 314.960898 314.960898 0.0 0.0 0.0 1.458962e+05
4 1997-05-01 1.473124e+05 1.322133e+05 1.647389e+05 1.473124e+05 1.473124e+05 687.660365 687.660365 687.660365 687.660365 687.660365 687.660365 0.0 0.0 0.0 1.480000e+05
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
379 2028-07-31 1.479180e+06 1.080462e+06 1.917491e+06 1.082229e+06 1.906088e+06 880.118140 880.118140 880.118140 880.118140 880.118140 880.118140 0.0 0.0 0.0 1.480060e+06
380 2028-08-31 1.484466e+06 1.077554e+06 1.922085e+06 1.084508e+06 1.918488e+06 -253.024316 -253.024316 -253.024316 -253.024316 -253.024316 -253.024316 0.0 0.0 0.0 1.484213e+06
381 2028-09-30 1.489582e+06 1.074314e+06 1.929589e+06 1.085996e+06 1.932973e+06 -3027.946570 -3027.946570 -3027.946570 -3027.946570 -3027.946570 -3027.946570 0.0 0.0 0.0 1.486554e+06
382 2028-10-31 1.494869e+06 1.078185e+06 1.943399e+06 1.082751e+06 1.947499e+06 -1111.523930 -1111.523930 -1111.523930 -1111.523930 -1111.523930 -1111.523930 0.0 0.0 0.0 1.493757e+06
383 2028-11-30 1.499985e+06 1.083240e+06 1.950976e+06 1.080330e+06 1.958310e+06 1934.287311 1934.287311 1934.287311 1934.287311 1934.287311 1934.287311 0.0 0.0 0.0 1.501919e+06

384 rows × 16 columns

In [142]:
m = Prophet()
m.fit(df)
invest_price = df.tail(1)
future = m.make_future_dataframe(periods=60, freq='M')
fcst = m.predict(future)
sell_price = fcst.tail(1)
print(sell_price['trend'].values[0] - invest_price['y'].values[0])
fig = m.plot(fcst)
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
274366.1740152382
In [0]:
invest_price = df.tail(1)
In [0]:
sell_price = fcst.tail(1)
In [141]:
sell_price['trend'].values[0] - invest_price['y'].values[0]
Out[141]:
274366.1740152382
In [138]:
type(sell_price['trend'])
Out[138]:
pandas.core.series.Series
In [139]:
sell_price['trend'].values[0]
Out[139]:
1188424.1740152382
In [140]:
sell_price
Out[140]:
ds trend yhat_lower yhat_upper trend_lower trend_upper additive_terms additive_terms_lower additive_terms_upper yearly yearly_lower yearly_upper multiplicative_terms multiplicative_terms_lower multiplicative_terms_upper yhat
323 2023-11-30 1.188424e+06 1.041552e+06 1.332581e+06 1.039249e+06 1.325371e+06 3670.706646 3670.706646 3670.706646 3670.706646 3670.706646 3670.706646 0.0 0.0 0.0 1.192095e+06
In [0]:
def get_investment_difference(df, zip_name):
  df_t = df.loc[:, '1997-01':'2017-12'].T
  df_t['avg'] = df_t.mean(numeric_only=True, axis=1)
  df_t.reset_index(inplace=True)
  columns = ['index', 'avg']
  df = pd.DataFrame(df_t, columns = columns)
  df = df.rename(index=str, columns={"avg": "y", "index": "ds"})
  m = Prophet()
  m.fit(df)
  invest_price = df.tail(1)
  future = m.make_future_dataframe(periods=60, freq='M')
  fcst = m.predict(future)
  sell_price = fcst.tail(1)
  print('++++++++',zip_name)
  print(sell_price['trend'].values[0] - invest_price['y'].values[0])
  fig = m.plot(fcst)
In [149]:
df = glendale.copy()
for zipcode in set(df['RegionName']):
  region_df = df[df['RegionName'] == zipcode]
  get_investment_difference(region_df, str(zipcode))
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
++++++++ 91201
236843.65637025773
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
++++++++ 91202
244537.5453543407
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
++++++++ 91203
218796.04965441872
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
++++++++ 91204
254380.26474907843
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
++++++++ 91205
259213.92290523078
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
++++++++ 91206
287514.6916984883
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
++++++++ 91207
359584.92270615813
INFO:fbprophet:Disabling weekly seasonality. Run prophet with weekly_seasonality=True to override this.
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
++++++++ 91208
251286.40563258925
++++++++ 91214
245259.13723531668
In [0]: