-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwave_lite.py
More file actions
150 lines (115 loc) · 3.29 KB
/
wave_lite.py
File metadata and controls
150 lines (115 loc) · 3.29 KB
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
import pywt
#import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
#import scipy.stats as sci
import os
def upArrow_op(li, j):
if j == 0:
return [1]
N = len(li)
li_n = np.zeros(2 ** (j - 1) * (N - 1) + 1)
for i in range(N):
li_n[2 ** (j - 1) * i] = li[i]
return li_n
def period_list(li, N):
n = len(li)
# append [0 0 ...]
n_app = N - np.mod(n, N)
li = list(li)
li = li + [0] * n_app
if len(li) < 2 * N:
return np.array(li)
else:
li = np.array(li)
li = np.reshape(li, [-1, N])
li = np.sum(li, axis=0)
return li
def circular_convolve_mra(h_j_o, w_j):
# ''' calculate the mra D_j'''
N = len(w_j)
l = np.arange(N)
D_j = np.zeros(N)
for t in range(N):
index = np.mod(t + l, N)
w_j_p = np.array([w_j[ind] for ind in index])
D_j[t] = (np.array(h_j_o) * w_j_p).sum()
return D_j
def circular_convolve_d(h_t, v_j_1, j):
# '''
# jth level decomposition
# h_t: \tilde{h} = h / sqrt(2)
# v_j_1: v_{j-1}, the (j-1)th scale coefficients
# return: w_j (or v_j)
# '''
N = len(v_j_1)
L = len(h_t)
w_j = np.zeros(N)
l = np.arange(L)
for t in range(N):
index = np.mod(t - 2 ** (j - 1) * l, N)
v_p = np.array([v_j_1[ind] for ind in index])
w_j[t] = (np.array(h_t) * v_p).sum()
return w_j
def circular_convolve_s(h_t, g_t, w_j, v_j, j):
# '''
# (j-1)th level synthesis from w_j, w_j
# see function circular_convolve_d
# '''
N = len(v_j)
L = len(h_t)
v_j_1 = np.zeros(N)
l = np.arange(L)
for t in range(N):
index = np.mod(t + 2 ** (j - 1) * l, N)
w_p = np.array([w_j[ind] for ind in index])
v_p = np.array([v_j[ind] for ind in index])
v_j_1[t] = (np.array(h_t) * w_p).sum()
v_j_1[t] = v_j_1[t] + (np.array(g_t) * v_p).sum()
return v_j_1
def modwt(x, filters, level):
# '''
# filters: 'db1', 'db2', 'haar', ...
# return: see matlab
# '''
# filter
wavelet = pywt.Wavelet(filters)
h = wavelet.dec_hi
g = wavelet.dec_lo
h_t = np.array(h) / np.sqrt(2)
g_t = np.array(g) / np.sqrt(2)
wavecoeff = []
v_j_1 = x
#==============================================================================
#
# Importante: j é o nível de frequência que se deseja
#
#
#==============================================================================
for j in range(level):
w = circular_convolve_d(h_t, v_j_1, j + 1)
v_j_1 = circular_convolve_d(g_t, v_j_1, j + 1)
wavecoeff.append(w)
wavecoeff.append(v_j_1)
return np.vstack(wavecoeff)
######################################################################
#inicio da minha parte
preco = pd.read_excel('Precos.xlsx',index_col=0)
ret = preco.pct_change(1)
ret = ret.dropna()
ret_ary = np.array(ret)[-3001:]
D = {}
col = 0
for k in ret.columns:
D[k] = modwt(ret_ary[0:-1, list(ret.columns).index(k)] , 'haar' , 7)
#%%
#writer_list = []
for x in D.keys():
nme= str.split(str(x), '.')[0]
writer = pd.ExcelWriter(os.path.join(os.getcwd(),'dados\\%s.xlsx' % nme))
pd.DataFrame(D[x]).T.to_excel(writer, 'sheet1')
# writer_list.append(writer)
writer.save()
#for x in writer_list:
# writer.save()
#