Newer
Older
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
"""
Contents
--------
- `fit <#wetb.wind.weibull.fit>`_: Fit a weibull distribution, in terms of the parameters k and A, to the provided wind speeds
- `pdf <#wetb.wind.weibull.pdf>`_: Create Weibull pdf function
- `random <#wetb.wind.weibull.random>`_: Create a list of n random Weibull distributed values
"""
import numpy as np
import math
gamma = math.gamma
def pdf(A, k):
"""Create Weibull pdf function
Parameters
----------
A : float
Scale parameter
k : float
Shape parameter
Returns
-------
pdf-function
Examples
--------
>>> from wetb.wind import weibull
>>> pdf_func = weibull.pdf(3,4)
>>> pdf_func(5)
0.131007116969
>>> from pylab import arange, plot, show
>>> wsp = arange(20)
>>> plot(wsp, pdf_func(wsp))
>>> show()
"""
return lambda x: k * x ** (k - 1) / A ** k * np.exp(-(x / A) ** k)
def cdf(A,k):
return lambda x: 1-np.exp(-(x/A)**k)
def random(A, k, n):
"""Create a list of n random Weibull distributed values
Parameters
----------
A : float
Scale parameter
k : float
Shape parameter
n : int
Number of values
Returns
-------
x : array_like, shape (n,)
n random Weibull distributed values
Examples
--------
>>> from wetb.wind import weibull
>>> from pylab import hist, show
>>> hist(weibull.random(4,2,1000), 20)
>>> show()
"""
return A * np.random.weibull(k, n)
def fit(wsp):
"""Fit a weibull distribution, in terms of the parameters k and A, to the provided wind speeds
Parameters
----------
wsp : array_like
Wind speeds
Returns
-------
A : float
Scale parameter
k : float
Shape parameter
Examples
--------
>>> from wetb.wind import weibull
>>> A,k = weibull.fit(wsp_lst)
"""
res_pr_ms = 2 # number of wind speed bins pr m/s
pdf, x = np.histogram(wsp, bins=np.arange(0, np.ceil(np.nanmax(wsp)), 1 / res_pr_ms))
x = (x[1:] + x[:-1]) / 2
N = np.sum(~np.isnan(wsp))
pdf = pdf / N * res_pr_ms
m = lambda n : np.sum(pdf * x ** n / res_pr_ms)
from scipy.optimize import newton
func = lambda k : gamma(1 / k + 1) ** 2 / gamma(2 / k + 1) - m(1) ** 2 / m(2)
k = newton(func, 1)
A = m(1) / gamma(1 / k + 1)
return A, k
if __name__ == "__main__":
from wetb.wind import weibull
from pylab import hist, show, plot
hist(weibull.random(10, 2, 10000), 20, normed=True)
wsp = np.arange(0, 20, .5)
plot(wsp, weibull.pdf(10, 2)(wsp))
show()