Theil-Sen Median斜率估计和Mann-Kendall趋势分析的python代码
时间: 2023-06-09 17:05:35 浏览: 264
前端开源库-ml-regression-theil-sen.zip
下面是Theil-Sen Median斜率估计的Python代码:
```python
import numpy as np
def theilsen(x, y):
n = len(x)
slopes = []
for i in range(n):
for j in range(i+1, n):
if x[i] != x[j]:
slope = (y[j] - y[i])/(x[j] - x[i])
slopes.append(slope)
median_slope = np.median(slopes)
return median_slope
```
下面是Mann-Kendall趋势分析的Python代码:
```python
from itertools import combinations
def mannkendall(x):
n = len(x)
s = 0
for i, j in combinations(range(n), 2):
s += np.sign(x[j] - x[i])
kendall_tau = s/((n*(n-1))/2)
var_s = n*(n-1)*(2*n+5)/18
if s > 0:
z = (s - 1)/np.sqrt(var_s)
elif s < 0:
z = (s + 1)/np.sqrt(var_s)
else:
z = 0
return kendall_tau, z
```
阅读全文