6
6
r .sort ()
7
7
8
8
def daily_return (prices ):
9
+ 'an array of daily returns from price array'
9
10
g = np .zeros_like (prices )
10
11
g [1 :] = (prices [1 :]- prices [:- 1 ])/ prices [:- 1 ]
11
12
return g
12
13
13
14
def volume_code (volume ):
15
+ 'code the continuous volume data categorically'
14
16
ind = np .searchsorted ([1e5 ,1e6 , 5e6 ,10e6 , 1e7 ], volume )
15
17
return ind
16
18
19
+ # a list of (dtype_name, summary_function, output_dtype_name).
20
+ # rec_summarize will call on each function on the indicated recarray
21
+ # attribute, and the result assigned to output name in the return
22
+ # record array.
17
23
summaryfuncs = (
18
24
('date' , lambda x : [thisdate .year for thisdate in x ], 'years' ),
19
25
('date' , lambda x : [thisdate .month for thisdate in x ], 'months' ),
@@ -24,13 +30,18 @@ def volume_code(volume):
24
30
25
31
rsum = mlab .rec_summarize (r , summaryfuncs )
26
32
33
+ # stats is a list of (dtype_name, function, output_dtype_name).
34
+ # rec_groupby will summarize the attribute identified by the
35
+ # dtype_name over the groups in the groupby list, and assign the
36
+ # result to the output_dtype_name
27
37
stats = (
28
38
('dreturn' , len , 'rcnt' ),
29
39
('dreturn' , np .mean , 'rmean' ),
30
40
('dreturn' , np .median , 'rmedian' ),
31
41
('dreturn' , np .std , 'rsigma' ),
32
42
)
33
43
44
+ # you can summarize over a single variable, like years or months
34
45
print 'summary by years'
35
46
ry = mlab .rec_groupby (rsum , ('years' ,), stats )
36
47
print mlab . rec2txt (ry )
@@ -39,6 +50,7 @@ def volume_code(volume):
39
50
rm = mlab .rec_groupby (rsum , ('months' ,), stats )
40
51
print mlab .rec2txt (rm )
41
52
53
+ # or over multiple variables like years and months
42
54
print 'summary by year and month'
43
55
rym = mlab .rec_groupby (rsum , ('years' ,'months' ), stats )
44
56
print mlab .rec2txt (rym )
0 commit comments