-
Notifications
You must be signed in to change notification settings - Fork 1
/
mallows_model.py
77 lines (70 loc) · 2.42 KB
/
mallows_model.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
from imp import reload
def select_mm(dist_name):
if dist_name == 'hamming':
import mallows_hamming as mh
reload(mh)
return mh
elif dist_name == 'kendall':
import mallows_kendall as mk
reload(mk)
return mk
else:
raise
import numpy as np
def check_theta_phi(theta, phi):
"""This function automatically converts theta to phi or phi to theta as
list or float depending on the values and value types given as input
Parameters
----------
theta: float or list
Dispersion parameter theta to convert to phi (can be None)
phi: float or list
Dispersion parameter phi to convert to theta (can be None)
Returns
-------
tuple
tuple containing both theta and phi (of list or float type depending on the input type)
"""
# MANUEL: If we do not give an error, the code continues and who knows what may happen.
# if not ((phi is None) ^ (theta is None)):
# print("Set valid values for phi or theta")
assert (phi is None) ^ (theta is None), "check_theta_phi: you need to provide either theta or phi but not both"
# MANUEL: theta_to_phi and phi_to_theta work on numpy arrays, so they will do the right thing independently if the input is a scalar, a list or a numpy array.
# if phi is None and type(theta)!=list:
# phi = theta_to_phi(theta)
# if theta is None and type(phi)!=list:
# theta = phi_to_theta(phi)
# if phi is None and type(theta)==list:
# phi = [theta_to_phi(t) for t in theta]
# if theta is None and type(phi)==list:
# theta = [phi_to_theta(p) for p in phi]
# return np.array(theta), np.array(phi)
if phi is None:
phi = theta_to_phi(theta)
else:
theta = phi_to_theta(phi)
return theta, phi
def theta_to_phi(theta):
"""This functions converts theta dispersion parameter into phi
Parameters
----------
theta: float
Real dispersion parameter
Returns
-------
float
phi real dispersion parameter
"""
return np.exp(-theta)
def phi_to_theta(phi):
"""This functions converts phi dispersion parameter into theta
Parameters
----------
phi: float
Real dispersion parameter
Returns
-------
float
theta real dispersion parameter
"""
return -np.log(phi)