-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexample.py
62 lines (49 loc) · 1.63 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Examples to help illustrate the module BPL.PY
=============================================
Created: Mon Apr 17, 2017 01:19PM
Last modified: Tue Apr 18, 2017 10:48AM
Copyright: Bedartha Goswami <goswami@uni-potsdam.de>
"""
import numpy as np
import matplotlib.pyplot as pl
import mkt
def show_examples():
"""
Returns the MK test results for artificial data.
"""
# create artificial time series with trend
n = 1000
C = [0.01, 0.001, -0.001, -0.01]
e = 1.00
t = np.linspace(0., 500, n)
# set up figure
fig, axes = pl.subplots(nrows=2, ncols=2, figsize=[16.00, 9.00])
# loop through various values of correlation
ALPHA = 0.01
for c, ax in zip(C, axes.flatten()):
# estimate the measurements 'x'
x = c * t + e * np.random.randn(n)
x = np.round(x, 2)
# get the slope, intercept and pvalues from the mklt module
MK, m, c, p = mkt.test(t, x, eps=1E-3, alpha=ALPHA, Ha="upordown")
# plot results
ax.plot(t, x, "k.-", label="Sampled time series")
ax.plot(t, m * t + c, "r-", label="Linear fit")
ax.set_title(MK.upper() + "\np=%.3f, alpha = %.2f" % (p, ALPHA),
fontweight="bold", fontsize=10)
# prettify
if ax.is_last_row():
ax.legend(loc="upper right")
ax.set_xlabel("Time")
if ax.is_first_col():
ax.set_ylabel(r"Measurements $x$")
if ax.is_first_row():
ax.legend(loc="upper left")
# save/show plot
pl.show(fig)
return None
if __name__ == "__main__":
print("running example...")
show_examples()
print("done.")