-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings_util.py
186 lines (141 loc) · 6.21 KB
/
settings_util.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Import Python libraries
import numpy as np
from numpy import sqrt, log, exp, mean, cumsum, sum, zeros, ones, argsort, argmin, argmax, array, maximum, concatenate
from numpy.random import randn, rand
np.set_printoptions(precision = 4)
import os
import scipy.optimize as optim
from scipy.stats import norm
from scipy.stats import bernoulli
import time
from datetime import datetime
# Import own utilities
from toimport import *
def get_hyp(pi_max, pi1c, num_hyp):
# Read hyp from file
filename_pre = "H_PM%.2f_PIC%d_NH%d_" % (pi_max, pi1c, num_hyp)
hypo_filename = [filename for filename in os.listdir('./expsettings') if filename.startswith(filename_pre)]
if len(hypo_filename) > 0:
# Just take the first sample
hyp_mat = np.loadtxt('./expsettings/%s' % hypo_filename[0])
else:
print("Hyp file doesn't exist, thus generating the file now ...")
# Generate 100 draws of num_hyp hypotheses with given pi_1 setting
hyp_mat = generate_hyp(pi1c, pi_max, num_hyp, 100)
# Choose some Hypvector could choose a different sample
Hypo = hyp_mat[0]
return Hypo
def generate_hyp(pi1c, pi_max, max_hyp, samples):
pi_filename = 'PIC.dat'
# ---- Get pi1 progression ---- #
if pi_max == 0:
#%%% If want a homogeneous progression of pi1
f = open('./expsettings/%s' % pi_filename)
pi_list = f.read().split('\n')
# Convert to number array
cache1 = pi_list[pi1c].split(' ')
pi1_vec = np.array([float(cache1[i]) for i in range(len(cache1))])
# Determine vector of lengths for constant pieces (with length hyp-step)
hyp_steps = len(set(pi1_vec)) # number of piecewise constant
len_per = max_hyp / hyp_steps # lengths of pcw constant
len_last = max_hyp - hyp_steps*len_per + len_per
length_vec = np.concatenate((len_per*np.ones(hyp_steps-1), np.array([len_last])))
else:
#%%% If want constant pi1
pi1_vec = np.ones(max_hyp)*pi_max
# Caculate lengths of constant pieces using pi1_vec and max_hyp
hyp_steps = 1
length_vec = [max_hyp]
hyp_mat = np.zeros([samples, max_hyp])
# ---- Sample hypotheses vectors using the pi1 progression ------ #
for i in range(samples):
Hyp = np.array([])
for j in range(hyp_steps):
Hyp = np.concatenate((Hyp, bernoulli.rvs(pi1_vec[j], size = length_vec[j])))
hyp_mat[i] = Hyp
# ----- Save sample hypotheses vectors ----- #
dirname = './expsettings'
filename = "H_PM%.2f_PIC%d_NH%d_" % (pi_max, pi1c, max_hyp)
saveres(dirname, filename, hyp_mat)
return hyp_mat
def get_absvec(abs_style, abs_prob, num_hyp):
# Read abs vec from file
filename_pre = "A_S%d_P%.2f_NH%d_" % (abs_style, abs_prob, num_hyp)
abs_filename = [filename for filename in os.listdir('./expsettings') if filename.startswith(filename_pre)]
if len(abs_filename) > 0:
# Just take the first sample
abs_mat = np.loadtxt('./expsettings/%s' % abs_filename[0])
else:
print("Absvec file doesn't exist, thus generating the file now ...")
# Generate 100 draws of num_hyp hypotheses with given pi_1 setting
abs_mat = generate_abs(abs_style, abs_prob, num_hyp,100)
# Choose some Hypvector could choose a different sample
abs_vec = abs_mat[0]
return abs_vec
def generate_absvec(abs_style, abs_prob, num_hyp):
pi_filename = 'PIC.dat'
# abs_style is essentially pi1c for hyp
pi1c = abs_style
absvec = np.zeros(length)
# ---- Get pi1 progression ---- #
if abs_prob == 0:
#%%% If want a homogeneous progression of pi1
f = open('./expsettings/%s' % pi_filename)
pi_list = f.read().split('\n')
# Convert to number array
cache1 = pi_list[pi1c].split(' ')
pi1_vec = np.array([float(cache1[i]) for i in range(len(cache1))])
# Determine vector of lengths for constant pieces (with length hyp-step)
abs_steps = len(set(pi1_vec)) # number of piecewise constant
len_per = max_hyp / abs_steps # lengths of pcw constant
len_last = max_hyp - abs_steps*len_per + len_per
length_vec = np.concatenate((len_per*np.ones(abs_steps-1), np.array([len_last])))
abs_mat = np.zeros([samples, max_hyp])
# ---- Sample hypotheses vectors using the pi1 progression ------ #
for i in range(samples):
Abs = np.array([])
for j in range(hyp_steps):
Abs = np.concatenate((Abs, bernoulli.rvs(pi1_vec[j], size = length_vec[j])))
abs_mat[i] = Abs
# ----- Save sample hypotheses vectors ----- #
dirname = './expsettings'
filename = "A_P%.2f_S%d_NH%d_" % (abs_prob, abs_style, num_hyp)
saveres(dirname, filename, abs_mat)
return abs_mat
def create_pen(penw_style, penw_const, prw_vec, NUMHYP):
# Constant weights
if penw_style == 1:
penw_vec = np.ones(NUMHYP)*penw_const
# Linear Decreasing
elif penw_style == 2:
penw_vec = np.linspace(penw_const, 1, num = NUMHYP)
# Linear increasing
elif penw_style == 3:
penw_vec = np.linspace(1, penw_const, num = NUMHYP)
# Correlated
elif penw_style == 4:
penw_vec = prw_vec
return penw_vec
def create_pr(prw_style, prw_const, m_corr, Hypo, NUMHYP):
prw_vec = Hypo.astype(float)
diff = m_corr - 1
zero_indices= np.where(np.array(Hypo == 0))[0]
# Correlated
if prw_style == 1:
# If Hyp=1 set 2
prw_vec = prw_vec*m_corr
# In the additive model
prw_vec[zero_indices] = (1-diff)*np.ones(len(zero_indices))
# # In the ratio model
# prw_vec[zero_indices] = np.true_divide(np.ones(len(zero_indices)),m_corr)
# Anti correlated
elif prw_style == 2:
# In the additive model
prw_vec = (1-diff)*np.ones(len(zero_indices))
# # In the ratio model
# prw_vec = np.true_divide(np.ones(NUMHYP),m_corr)
prw_vec[zero_indices] = np.ones(len(zero_indices))*m_corr
# Constant
elif prw_style == 3:
prw_vec = np.ones(NUMHYP)*prw_const
return prw_vec