# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# %load_ext autoreload
# %autoreload 2
# %load_ext Cython

import sys,time,datetime,copy,subprocess,itertools,pickle,warnings,json,gc,numbers

import numpy as np
import scipy as sp
from matplotlib import pyplot as plt
import matplotlib as mpl

# +
import Hawkes as hk

para = {"mu":0.1, "alpha":0.5, "beta":10.0}
itv = [0,10000]

h1 = hk.simulator().set_kernel('exp').set_baseline('const').set_parameter(para)
T = h1.simulate(itv)
print(len(T))

h2 = hk.estimator().set_kernel('exp').set_baseline('const')
h2.fit(T,itv)
print(h2.para)

print()
# %timeit -n1 -r1 h2.fit(T,itv)

# +
import Hawkes as hk

para = {"mu":0.1, "alpha":[0.25,0.25], "beta":[1.0,20.0]}
itv = [0,10000]

h1 = hk.simulator().set_kernel('exp',num_exp=2).set_baseline('const').set_parameter(para)
T = h1.simulate(itv)
print(len(T))

h2 = hk.estimator().set_kernel('exp',num_exp=2).set_baseline('const')
h2.fit(T,itv)
print(h2.para)

print()
# %timeit -n1 -r1 h2.fit(T,itv)

# +
import Hawkes as hk

para = {"mu":0.1, "k":0.5,"p":2.0,"c":1.0}
itv = [0,10000]

h1 = hk.simulator().set_kernel('pow').set_baseline('const').set_parameter(para)
T = h1.simulate(itv)
print(len(T))

h2 = hk.estimator().set_kernel('pow').set_baseline('const')
h2.fit(T,itv)
print(h2.para)

print()
# %timeit -n1 -r1 h2.fit(T,itv)

# +
import Hawkes as hk

para = {"alpha":0.2,"beta":10.0}
mu_t = lambda x: 10.0 + 9.0*np.sin(2*np.pi*x/100)
itv = [0,100]
h1 = hk.simulator().set_kernel("exp").set_baseline('custom',l_custom=mu_t).set_parameter(para)
T = h1.simulate(itv)
print(len(T))

# piecewise constant model
h2 = hk.estimator().set_kernel("exp").set_baseline('pconst',num_basis=12)
h2.fit(T,itv)
print(h2.para)

# piecewise lienar model
h3 = hk.estimator().set_kernel("exp").set_baseline('plinear',num_basis=6)
h3.fit(T,itv)
print(h3.para)

# log-linear model
h4 = hk.estimator().set_kernel("exp").set_baseline('loglinear',num_basis=6)
h4.fit(T,itv)
print(h4.para)

h1.plot_l()
plt.plot(T,h2.baseline.l(T),"g-",label='p-const')
plt.plot(T,h3.baseline.l(T),"r-",label='p-linear')
plt.plot(T,h4.baseline.l(T),"b-",label='log-linear')
plt.legend()

pass

# +
########################## non-parametric model
import Hawkes as hk

para = {"mu":0.2, "alpha":0.8, "beta":1.0}
itv = [0,10000]

h1 = hk.simulator().set_kernel('exp').set_baseline('const').set_parameter(para)
T = h1.simulate(itv)
print(len(T))

support = 10.0
num_bin = 32

###
h2 = hk.estimator().set_kernel('nonpara',support=support,num_bin=num_bin).set_baseline('const')
h2.fit(T,itv)

h2.kernel.plot()
x = np.linspace(0,support,100)
y = np.exp(-1.0*x)*0.8*1.0
plt.plot(x,y,'r-')
# -



StarcoderdataJupyterScriptsDedupFiltered