In [1]:
#-*- coding: utf8
from __future__ import division, print_function

from phoenix.models import WavePhoenixR

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline


:0: FutureWarning: IPython widgets are experimental and may change in the future.

Gangnam Style Data


In [2]:
data = [520578, 429666, 397952, 505910, 405054, 256381, 649459, 535209, 524792, 462833, 565211, 687343, 636699, 603342, 813458, 832641, 924225, 970433, 1058284, 1789240, 1467022, 1361674, 1580239, 1585645, 1649960, 1581240, 1763891, 1505520, 1341269, 1544174, 1874544, 2031506, 2077288, 2214882, 2111487, 2087280, 2300856, 2672590, 3057718, 3212088, 3594824, 3607928, 3295138, 3589528, 3929102, 4219268, 4768364, 5018013, 5357443, 5019866, 5173064, 5444334, 5581247, 6202701, 7006687, 7032648, 6919060, 6719197, 6842544, 7212990, 7910952, 9664447, 9108651, 8820094, 9146978, 8418257, 8473949, 9550893, 11018902, 11736860, 10416584, 9738119, 9483200, 9321228, 8909267, 11481444, 12784802, 10849805, 9454005, 8803530, 8835570, 9069084, 11005339, 11615236, 9848207, 8449565, 8017559, 8126286, 8133914, 9467998, 10859777, 9832122, 7273529, 7614319, 7739443, 7633689, 9214745, 10957737, 9175074, 7587595, 7764514, 8484355, 8414463, 9836827, 11189149, 9377162, 7906863, 8065552, 8051642, 8601761, 9574078, 10454058, 8760752, 6828075, 6940728, 6758396, 7475981, 9105561, 11206549, 9367574, 7667185, 7120974, 7332373, 7178476, 8518937, 10511076, 8755494, 6932099, 7023241, 7389004, 7887420, 8565469, 11487589, 10209032, 7512689, 6682767, 6494478, 6549704, 7957156, 9311429, 7476852, 5808087, 5671298, 5951764, 6036078, 7156204, 8888429, 6897801, 5419944, 5476154, 5673707, 6040946, 7060284, 8138955, 6665434, 5369141, 8624024, 6962725, 8956125, 14924298, 11541865, 9492325, 9921559, 9019929, 7682540, 7052114, 7316434, 7819302, 7296451, 11085546, 7287532, 6389752, 5914194, 6215161, 7246611, 5928877, 5080581, 4849609, 5043994, 5336680, 5874136, 6878945, 5797948, 4385026, 4584479, 4541108, 4655647, 5267185, 6306349, 5376396, 4308772, 3681413, 4489439, 4271413, 5032795, 5953710, 5049273, 3962326, 4012856, 4048600, 4162806, 4786452, 5786704, 4960040, 3794802, 3764029, 3957399, 4014468, 4707340, 5617656, 4848504, 3889920, 3797241, 3733985, 3762549, 4454666, 5103411, 4363097, 3582828, 3470084, 3544961, 3638580, 4401248, 5426250, 4224641, 3164109, 3468998, 3306348, 3157505, 3746716, 5144933, 4329939, 3242996, 3108167, 3206673, 3408942, 3726897, 4575528, 3766935, 3022835, 3021680, 3015885, 3118800, 3735088, 4648755, 3934828, 2959366, 2901021, 2873277, 2889656, 3390074, 4060047, 3522396, 2772557, 2779615, 2880541, 2942589, 3232238, 3370352, 3114496, 2736286, 2486267, 2574656, 2719661, 2746360, 3346764, 2809162, 2240907, 2218082, 2329051, 2445492, 2926574, 3990257, 4220378, 3631999, 3322043, 2839298, 2688346, 3550368, 3893333, 2973921, 2137509, 2307979, 2349896, 2240535, 2501047, 2853962, 2509205, 2009597, 2143262, 2423911, 2316451, 2310104, 2656082, 2238667, 1823209, 1759979, 1847124, 1946184, 2144452, 2439730, 2003199, 1606718, 1598226, 1661170, 1667831, 1899119, 2238454, 2116519, 1752631, 1645825, 1642035, 1742668, 1989617, 2290706, 2013891, 1692240, 1625839, 1669896, 1740956, 1834506, 2139404, 1856432, 1614242, 1539186, 1503506, 1565075, 1729917, 1887569, 1660447, 1394235, 1444947, 1416540, 1390298, 1505303, 1669974, 1479979, 1287720, 1299237, 1276124, 1300069, 1381207, 1522580, 1305555, 1215816, 1235000, 1272230, 1374422, 1394487, 1529056, 1346645, 1260346, 1252824, 1255996, 1295045, 1403945, 1477193, 2191564, 1395833, 1372207, 2463147, 1341864, 1927184, 1650021, 2623358, 2947237, 1684224, 1189390, 1213568, 1326633, 1329957, 1171079, 1127822, 1124372, 1113470, 1100733, 1205090, 1270241, 1127974, 1094596, 1087548, 1053101, 1054306, 1214791, 1217006, 1090662, 1034406, 1039773, 1047610, 1068348, 1121260, 1174848, 1085700, 1033750, 1024127, 1062159, 1037714, 1082550, 1125624, 1042973, 1009021, 1076690, 1062314, 1069452, 1131626, 1201404, 1308454, 939452, 951912, 940037, 959168, 1080650, 1154721, 1055020, 919882, 880427, 905276, 896710, 998658, 1163679, 984427, 840402, 833984, 848721, 837183, 975047, 1205779, 1008897, 767616, 771686, 756693, 780916, 929299, 1073402, 912294, 709954, 705218, 705834, 718649, 850786, 1014891, 892357, 720660, 725652, 748325, 741509, 868118, 1066284, 905775, 719367, 735545, 803910, 803072, 943124, 1105894, 975401, 818138, 791833, 812249, 835844, 961647, 1109471, 935630, 768193, 793144, 818822, 829571, 962540, 1124023, 1005348, 844987, 839410, 785031, 873256, 1025542, 1168894, 1053445, 812220, 796252, 821785, 811054, 947204, 1111561, 1034968, 853981, 810984, 823659, 819858, 938461, 1152921, 969425, 763053, 772884, 799418, 820273, 971502, 1152222, 966040, 786763, 810022, 863079, 915460, 999834, 1155809, 970919, 792316, 801611, 854061, 860599, 969760, 1147332, 970812, 785217, 776486, 860520, 795485, 962033, 1146396, 959024, 767085, 766319, 800528, 854124, 935753, 1070980, 974719, 932175, 1135712, 1034452, 983544, 1062462, 1099100, 1042753, 1056354, 1687223, 1085504, 1021200, 1059459, 1114790, 1001356, 883433, 891419, 876013, 873431, 974177, 1136040, 970624, 792575, 806275, 845833, 821561, 940386, 1131729, 973537, 816732, 815515, 827487, 821041, 940088, 1095140, 959687, 779763, 804907, 833333, 841005, 912807, 1072332, 922285, 742791, 740753, 808256, 794750, 895617]

In [3]:
data = np.array(data, dtype='d')

In [4]:
plt.semilogy(data, 'g-')
plt.ylabel('Views')
plt.xlabel('Days after upload')


Out[4]:
<matplotlib.text.Text at 0x100734210>

Fitting the model with MDL


In [5]:
model = WavePhoenixR(score_func='mdl', threshold=0.001).fit(data)

In [6]:
results = model(len(data))

In [7]:
plt.semilogy(data, 'g-')
plt.plot(results, 'r--')
plt.ylabel('Views')
plt.xlabel('Days after upload')


Out[7]:
<matplotlib.text.Text at 0x11370fb10>

In [8]:
model.parameters


Out[8]:
Parameters([('period', <Parameter 'period', value=7 (fixed), bounds=[None:None]>), ('amp', <Parameter 'amp', value=0.27249344369267037 +/- 0, bounds=[0:1]>), ('phase', <Parameter 'phase', value=6.328720179475944 +/- 0, bounds=[0:7]>), ('i0_0', <Parameter 'i0_0', value=1 (fixed), bounds=[None:None]>), ('sp_0', <Parameter 'sp_0', value=0 (fixed), bounds=[None:None]>), ('beta_0', <Parameter 'beta_0', value=0.00095379884648721935 +/- 0, bounds=[0:1]>), ('r_0', <Parameter 'r_0', value=153489.90964115571 +/- 0, bounds=[0:inf]>), ('gamma_0', <Parameter 'gamma_0', value=0.0069657063901719107 +/- 0, bounds=[0:1]>), ('start_points', <Parameter 'start_points', value=[0] (fixed), bounds=[None:None]>), ('num_models', <Parameter 'num_models', value=1 (fixed), bounds=[None:None]>), ('s0_0', <Parameter 's0_0', value=100.0 +/- 0, bounds=[0:inf]>)])

Fitting the model with no penalization (mean square error minimization)


In [9]:
model = WavePhoenixR(score_func='msq', threshold=np.inf).fit(data)
results = model(len(data))
plt.semilogy(data, 'g-')
plt.plot(results, 'r--')
plt.ylabel('Views')
plt.xlabel('Days after upload')


Out[9]:
<matplotlib.text.Text at 0x113d6ad90>

In [10]:
model.parameters


Out[10]:
Parameters([('period', <Parameter 'period', value=7 (fixed), bounds=[None:None]>), ('amp', <Parameter 'amp', value=0.27918490056540701 +/- 0, bounds=[0:1]>), ('phase', <Parameter 'phase', value=6.2430254828986085 +/- 0, bounds=[0:7]>), ('i0_0', <Parameter 'i0_0', value=1 (fixed), bounds=[None:None]>), ('sp_0', <Parameter 'sp_0', value=0 (fixed), bounds=[None:None]>), ('beta_0', <Parameter 'beta_0', value=0.00067911587368146842 +/- 0, bounds=[0:1]>), ('r_0', <Parameter 'r_0', value=91549.062548751011 +/- 0, bounds=[0:inf]>), ('gamma_0', <Parameter 'gamma_0', value=0.0069789987570351153 +/- 0, bounds=[0:1]>), ('s0_0', <Parameter 's0_0', value=158.48931924611142 +/- 0, bounds=[0:inf]>), ('s0_65', <Parameter 's0_65', value=3485748.5311761815 +/- 0, bounds=[0:inf]>), ('i0_65', <Parameter 'i0_65', value=1 (fixed), bounds=[None:None]>), ('sp_65', <Parameter 'sp_65', value=65 (fixed), bounds=[None:None]>), ('beta_65', <Parameter 'beta_65', value=0.78857738185419546 +/- 0, bounds=[0:1]>), ('r_65', <Parameter 'r_65', value=0.44692802238289708 +/- 0, bounds=[0:inf]>), ('gamma_65', <Parameter 'gamma_65', value=0.11284587282730824 +/- 0, bounds=[0:1]>), ('s0_156', <Parameter 's0_156', value=2917841.0 +/- 0, bounds=[0:inf]>), ('i0_156', <Parameter 'i0_156', value=1 (fixed), bounds=[None:None]>), ('sp_156', <Parameter 'sp_156', value=156 (fixed), bounds=[None:None]>), ('beta_156', <Parameter 'beta_156', value=0.71217590454252522 +/- 0, bounds=[0:1]>), ('r_156', <Parameter 'r_156', value=2.2370893391952795 +/- 0, bounds=[0:inf]>), ('gamma_156', <Parameter 'gamma_156', value=0.092518397267422614 +/- 0, bounds=[0:1]>), ('s0_129', <Parameter 's0_129', value=2820028.0 +/- 0, bounds=[0:inf]>), ('i0_129', <Parameter 'i0_129', value=1 (fixed), bounds=[None:None]>), ('sp_129', <Parameter 'sp_129', value=129 (fixed), bounds=[None:None]>), ('beta_129', <Parameter 'beta_129', value=0.70130481320768057 +/- 0, bounds=[0:1]>), ('r_129', <Parameter 'r_129', value=0.79509612070186719 +/- 0, bounds=[0:inf]>), ('gamma_129', <Parameter 'gamma_129', value=0.13891635210281317 +/- 0, bounds=[0:1]>), ('s0_219', <Parameter 's0_219', value=1956166.0 +/- 0, bounds=[0:inf]>), ('i0_219', <Parameter 'i0_219', value=1 (fixed), bounds=[None:None]>), ('sp_219', <Parameter 'sp_219', value=219 (fixed), bounds=[None:None]>), ('beta_219', <Parameter 'beta_219', value=0.61482302948948453 +/- 0, bounds=[0:1]>), ('r_219', <Parameter 'r_219', value=0.13611762236950042 +/- 0, bounds=[0:inf]>), ('gamma_219', <Parameter 'gamma_219', value=0.038246418590853093 +/- 0, bounds=[0:1]>), ('s0_348', <Parameter 's0_348', value=1228871.0 +/- 0, bounds=[0:inf]>), ('i0_348', <Parameter 'i0_348', value=1 (fixed), bounds=[None:None]>), ('sp_348', <Parameter 'sp_348', value=348 (fixed), bounds=[None:None]>), ('beta_348', <Parameter 'beta_348', value=0.82128086668491063 +/- 0, bounds=[0:1]>), ('r_348', <Parameter 'r_348', value=1.2269962823552305e-11 +/- 0, bounds=[0:inf]>), ('gamma_348', <Parameter 'gamma_348', value=0.99710097644143847 +/- 0, bounds=[0:1]>), ('s0_311', <Parameter 's0_311', value=371856.0 +/- 0, bounds=[0:inf]>), ('i0_311', <Parameter 'i0_311', value=1 (fixed), bounds=[None:None]>), ('sp_311', <Parameter 'sp_311', value=311 (fixed), bounds=[None:None]>), ('beta_311', <Parameter 'beta_311', value=0.79803005281350226 +/- 0, bounds=[0:1]>), ('r_311', <Parameter 'r_311', value=0.0075232590677991595 +/- 0, bounds=[0:inf]>), ('gamma_311', <Parameter 'gamma_311', value=0.31450117665397653 +/- 0, bounds=[0:1]>), ('s0_324', <Parameter 's0_324', value=348383.0 +/- 0, bounds=[0:inf]>), ('i0_324', <Parameter 'i0_324', value=1 (fixed), bounds=[None:None]>), ('sp_324', <Parameter 'sp_324', value=324 (fixed), bounds=[None:None]>), ('beta_324', <Parameter 'beta_324', value=0.91824920059566639 +/- 0, bounds=[0:1]>), ('r_324', <Parameter 'r_324', value=0.01531292262802908 +/- 0, bounds=[0:inf]>), ('gamma_324', <Parameter 'gamma_324', value=0.85098332950131605 +/- 0, bounds=[0:1]>), ('s0_380', <Parameter 's0_380', value=129458.0 +/- 0, bounds=[0:inf]>), ('i0_380', <Parameter 'i0_380', value=1 (fixed), bounds=[None:None]>), ('sp_380', <Parameter 'sp_380', value=380 (fixed), bounds=[None:None]>), ('beta_380', <Parameter 'beta_380', value=0.64955143240652746 +/- 0, bounds=[0:1]>), ('r_380', <Parameter 'r_380', value=0.3770640327561896 +/- 0, bounds=[0:inf]>), ('gamma_380', <Parameter 'gamma_380', value=0.99826846320629681 +/- 0, bounds=[0:1]>), ('s0_459', <Parameter 's0_459', value=333050.0 +/- 0, bounds=[0:inf]>), ('i0_459', <Parameter 'i0_459', value=1 (fixed), bounds=[None:None]>), ('sp_459', <Parameter 'sp_459', value=459 (fixed), bounds=[None:None]>), ('beta_459', <Parameter 'beta_459', value=0.36667178526813859 +/- 0, bounds=[0:1]>), ('r_459', <Parameter 'r_459', value=1.3065009519539035 +/- 0, bounds=[0:inf]>), ('gamma_459', <Parameter 'gamma_459', value=0.00065155671965966278 +/- 0, bounds=[0:1]>), ('start_points', <Parameter 'start_points', value=[0, 65, 156, 129, 219, 348, 311, 324, 380, 459] (fixed), bounds=[None:None]>), ('num_models', <Parameter 'num_models', value=10 (fixed), bounds=[None:None]>)])

In [ ]: