-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnormalize_weights.py
56 lines (47 loc) · 2.15 KB
/
normalize_weights.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
'''
Created on Jun 1, 2017
@author: zwieback
'''
import numpy as np
from scipy.stats import gmean
# normalize explanatory variables such that their product is 1 (i.e. divide by their geometric mean)
def normalize_weight_multiplicative(explanatory):
# weight is assumed positive
assert np.count_nonzero(explanatory<0) == 0
multfactor = gmean(explanatory,axis=1)
nweight = explanatory/multfactor[:,np.newaxis]
return nweight, {'multiplicative':multfactor}
# normalize explanatory variables so that they have mean zero and standard deviation 1
def normalize_weight_additive(explanatory):
addfactor = np.mean(explanatory,axis=1)
weight = explanatory-addfactor[:,np.newaxis]
multfactor=np.std(weight,axis=1)
nweight = weight / multfactor[:,np.newaxis]
return nweight, {'multiplicative':multfactor,'additive':addfactor}
def prepare_weights(explan=None,n=None,multiplicative=False,additive=True):
assert multiplicative != additive #either mult. or add.
if explan is None:
assert n is not None
explan = np.zeros((0,n))
weight = explan
normfactor = np.zeros((0))
else:
if multiplicative:
weight,normfactor = normalize_weight_multiplicative(explan)
elif additive:
weight,normfactor = normalize_weight_additive(explan)
else:
raise
nfac=explan.shape[0]
return {'nfac':nfac,'normfactor':normfactor,'weight':weight}
def normalize_weights(explankappa=None,explanmu=None,explanlambda=None,explanalphabeta=None,n=None):
explanatoryweights={}
# prepare kappa weights
explanatoryweights['kappa'] = prepare_weights(explan=explankappa,n=n,multiplicative=True,additive=False)
# prepare mu weights
explanatoryweights['mu'] = prepare_weights(explan=explanmu,n=n,multiplicative=False,additive=True)
# prepare lambda weights
explanatoryweights['lambda'] = prepare_weights(explan=explanlambda,n=n,multiplicative=False,additive=True)
# prepare alpha/beta weights
explanatoryweights['alphabeta'] = prepare_weights(explan=explanalphabeta,n=n,multiplicative=False,additive=True)
return explanatoryweights