# Univariate HP with Exponential Delay

In this notebook, we present an example use case of fitting (calibrating) and sampling from a univariate Hawkes (self-exciting) process with exponential delay density. Concretely, the HP is determined by the conditional intensity function

$$
\begin{align}
\lambda^*(t) &= \mu + \alpha \sum_{t_j < t} g_\theta(t - t_j) \\
g_\theta(x) &= -\theta \exp (-\theta x )
\end{align}
$$


```python
%matplotlib inline
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from hawkeslib.model.uv_exp import UnivariateExpHawkesProcess as UVHP
```

## Example 1: FX Price Jump Data

Fit to real data and get Monte Carlo estimates of the distribution of number of occurrences via Ogata's modified thinning method.


```python
df = pd.read_csv("example_data.csv", header=None)
df.columns = ["mark", "time"]
```


```python
# take all mark 21 and fit a univariate exp HP
td = np.array(df.loc[df.mark == 21, :].get("time")).astype(np.float64)

proc = UVHP()
proc.fit(td, method="em")
```




    -58574.97098100516




```python
# verify that the unconditional mean makes sense
mu, alpha, theta = proc.get_params()

N_approx = mu * td[-1] / (1 - alpha)

print(N_approx)
print(len(td))
```

    8005.669015748907
    8003



```python
%%time
# take unconditioned samples from this HP, and estimate the distribution of 
# number of occurrences in a ten second period

counts = []
for j in range(100000):
    n = len(proc.sample(T=10000))  # time in millis
    counts.append(n)
```

    CPU times: user 5.65 s, sys: 100 ms, total: 5.75 s
    Wall time: 1.52 s



```python
plt.title("Number of Occurrences in 10secs")
plt.hist(counts, bins=30)
plt.show()
```

## Example 2: Simulated Data

We simulate HPs and try to fit their parameters.


```python
param_pairs = []
for j in range(100):
    # priors
    pars = np.random.gamma(3.), np.random.rand() * .3, 1/np.random.gamma(1.)

    hp = UVHP()
    hp.set_params(*pars)
    smp = hp.sample(10000, method="branching")
    
    # fit with gd
    hp2 = UVHP()
    hp2.fit(smp, method="gd")
    
    # fit with em
    hp3 = UVHP()
    hp3.fit(smp, method="em", reltol=1e-8)
    
    comb_pars = hp.get_params() + hp2.get_params() + hp3.get_params()
    param_pairs.append(comb_pars)
```


```python
Z = np.array(param_pairs)

f, ax = plt.subplots(2, 3, figsize=(15,10))
f.suptitle("Fitted vs Actual Parameters")
for k in range(3):
    ax[0][k].plot(Z[:, k], Z[:, k+3], "k.")
    ax[0][k].set_title(["mu", "alpha", "theta"][k])
    
    ax[1][k].plot(Z[:, k], Z[:, k+6], "k.")

ax[0,2].loglog()
ax[1,2].loglog()

ax[0,0].set_ylabel("Grad. Descent")
ax[1,0].set_ylabel("Expectation-Maximization")

```


```python

```

84397d273bc569d9ef5282dc6e1422897510db88
ipynb
Jupyter Notebook
examples/UV Exp Example.ipynb
changlan/hawkeslib
6837948f6379711ae27332bfd1faea6bc5230da8
MIT
2018-08-15T11:10:22.000Z
2022-03-20T09:26:17.000Z
examples/UV Exp Example.ipynb
changlan/hawkeslib
6837948f6379711ae27332bfd1faea6bc5230da8
MIT
2018-08-08T19:31:50.000Z
2022-01-16T09:34:41.000Z
examples/UV Exp Example.ipynb
canerturkmen/fasthawkes
6837948f6379711ae27332bfd1faea6bc5230da8
MIT
2019-03-07T02:45:50.000Z
2022-01-13T17:18:30.000Z
Qwen/Qwen-72B
1. YES
2. YES