-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_lag_correlation.py
156 lines (136 loc) · 6.38 KB
/
plot_lag_correlation.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import joblib
import argparse
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.regression.linear_model import OLS
def lag_correlation(kelp_metrics):
# find lat limits
lower = np.min(kelp_metrics['lat'])
upper = np.max(kelp_metrics['lat'])
# find unique times, compute mean + stdev of bins, and plot
unique_times = np.unique(kelp_metrics['time'])
mean_temp = []
mean_kelp = []
std_temp = []
mean_temp_lag = []
std_temp_lag = []
mean_temp_lag2 = []
std_temp_lag2 = []
# compute mean and std of temperature and kelp area
for t in unique_times:
mask = kelp_metrics['time'] == t
mean_temp.append(np.mean(kelp_metrics['temp'][mask]))
mean_kelp.append(np.mean(kelp_metrics['kelp'][mask]))
std_temp.append(np.std(kelp_metrics['temp'][mask]))
mean_temp_lag.append(np.mean(kelp_metrics['temp_lag'][mask]))
std_temp_lag.append(np.std(kelp_metrics['temp_lag'][mask]))
mean_temp_lag2.append(np.mean(kelp_metrics['temp_lag2'][mask]))
std_temp_lag2.append(np.std(kelp_metrics['temp_lag2'][mask]))
# convert to numpy arrays
mean_temp = np.array(mean_temp)
mean_kelp = np.array(mean_kelp)
std_temp = np.array(std_temp)
mean_temp_lag = np.array(mean_temp_lag)
std_temp_lag = np.array(std_temp_lag)
mean_temp_lag2 = np.array(mean_temp_lag2)
std_temp_lag2 = np.array(std_temp_lag2)
fig, ax = plt.subplots(figsize=(7,6))
ax.plot(mean_temp-273.15, mean_kelp, 'o', color='black', markersize=4)
# plot x-axis errorbar
#ax.errorbar(mean_temp-273.15, mean_kelp, xerr=std_temp, color='black', marker='o', ls='none', ecolor='black', alpha=0.5)
# measure trend line
A = np.vstack([mean_temp-273.15, np.ones(len(mean_temp))]).T
res = OLS(mean_kelp, A).fit()
m,b = res.params[0], res.params[1]
x = np.linspace(np.min(mean_temp-273.15), np.max(mean_temp-273.15), 100)
corrcoeff = np.corrcoef(mean_temp-273.15, mean_kelp)[0,1]
#print(f"Correlation coefficient: {corrcoeff:.2f}")
ax.plot(x, m*x+b, 'k-',alpha=0.75,lw=2,label=f'Temperature ($r={corrcoeff:.2f}$)')
#print(f"Slope of trend line: {m:.2f} +- {res.bse[0]:.2f} m^2/C")
# estimate x when y = 0
x0 = -b/m
# perform monte carlo simulation to estimate error in x0
x0s = []
for i in range(1000):
# sample from normal distribution
m_sample = np.random.normal(m, res.bse[0])
b_sample = np.random.normal(b, res.bse[1])
# estimate x when y = 0
x0_sample = -b_sample/m_sample
x0s.append(x0_sample)
#print(f"Temperature when kelp area is zero: {x0:.2f} +- {np.std(x0s):.2f}C")
# temp lagged by one quarter
ax.plot(mean_temp_lag[1:]-273.15, mean_kelp[1:], 'o', color='red', markersize=4)
# plot x-axis errorbar
#ax.errorbar(mean_temp_lag[1:]-273.15, mean_kelp[1:], xerr=std_temp_lag[1:], color='red', marker='o', ls='none', ecolor='red', alpha=0.5)
# measure trend line
A = np.vstack([mean_temp_lag[1:]-273.15, np.ones(len(mean_temp_lag[1:]))]).T
res = OLS(mean_kelp[1:], A).fit()
m,b = res.params[0], res.params[1]
x = np.linspace(np.min(mean_temp_lag[1:]-273.15), np.max(mean_temp_lag[1:]-273.15), 100)
corrcoeff = np.corrcoef(mean_temp_lag[1:]-273.15, mean_kelp[1:])[0,1]
print(f"Correlation coefficient: {corrcoeff:.2f}")
ax.plot(x, m*x+b, 'r-',alpha=0.75,lw=2,label=r'Temperature Lagged by One Quarter ($r=%.2f$)'%corrcoeff)
print(f"Slope of trend line: {m:.2f} +- {res.bse[0]:.2f} m^2/C")
# estimate x when y = 0
x0 = -b/m
# perform monte carlo simulation to estimate error in x0
x0s = []
for i in range(1000):
# sample from normal distribution
m_sample = np.random.normal(m, res.bse[0])
b_sample = np.random.normal(b, res.bse[1])
# estimate x when y = 0
x0_sample = -b_sample/m_sample
x0s.append(x0_sample)
print(f"One Quarter lagged temperature when kelp area is zero: {x0:.2f} +- {np.std(x0s):.2f}C")
# temp lagged by two quarters
ax.plot(mean_temp_lag2[2:]-273.15, mean_kelp[2:], 'o', color='blue', markersize=4)
# plot x-axis errorbar
#ax.errorbar(mean_temp_lag2[2:]-273.15, mean_kelp[2:], xerr=std_temp_lag2[2:], color='blue', marker='o', ls='none', ecolor='blue', alpha=0.5)
# measure trend line
A = np.vstack([mean_temp_lag2[2:]-273.15, np.ones(len(mean_temp_lag2[2:]))]).T
res = OLS(mean_kelp[2:], A).fit()
m,b = res.params[0], res.params[1]
x = np.linspace(np.min(mean_temp_lag2[2:]-273.15), np.max(mean_temp_lag2[2:]-273.15), 100)
corrcoeff = np.corrcoef(mean_temp_lag2[2:]-273.15, mean_kelp[2:])[0,1]
print(f"Correlation coefficient: {corrcoeff:.2f}")
ax.plot(x, m*x+b, 'b-',alpha=0.75,lw=2,label=r'Temperature Lagged by Two Quarters ($r=%.2f$)'%corrcoeff)
print(f"Slope of trend line: {m:.2f} +- {res.bse[0]:.2f} m^2/C")
# estimate x when y = 0
x0 = -b/m
# perform monte carlo simulation to estimate error in x0
x0s = []
for i in range(1000):
# sample from normal distribution
m_sample = np.random.normal(m, res.bse[0])
b_sample = np.random.normal(b, res.bse[1])
# estimate x when y = 0
x0_sample = -b_sample/m_sample
x0s.append(x0_sample)
#print(f"Two Quarter lagged temperature when kelp area is zero: {x0:.2f} +- {np.std(x0s):.2f}C")
ax.set_ylabel(r'Kelp Area $[m^2]$', fontsize=14)
ax.set_xlabel('Sea Surface Temperature [C]', fontsize=14)
ax.set_title(f"Lag Correlation between ({lower:.1f} - {upper:.1f}N)", fontsize=16)
ax.grid(True,ls='--')
ax.legend(loc='best')
plt.tight_layout()
return fig, ax
if __name__ == "__main__":
# argparse for input filepath
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file_path', type=str,
help='path to input metrics file',
default="Data/kelp_metrics_27_37.pkl")
args = parser.parse_args()
# load data from disk
with open(args.file_path, 'rb') as f:
data = joblib.load(f)
# print lat values from file name
print(f"Latitude Range: {args.file_path.split('_')[-2].split('.')[0]} - {args.file_path.split('_')[-1].split('.')[0]}")
# plot time series
fig, ax = lag_correlation(data)
plt.savefig(args.file_path.replace('.pkl', '_lag_correlation.png'))
plt.show()
# TODO monte carlo the results to get a best-fit line and error bars