-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyeo_johnson_transformed_distributions.py
168 lines (123 loc) · 4.83 KB
/
yeo_johnson_transformed_distributions.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
import torch
from torch.distributions import Normal, Transform, constraints
from torch.distributions.transforms import AffineTransform, PowerTransform, ExpTransform, ComposeTransform
from torch.distributions.transformed_distribution import TransformedDistribution
class YeoJohnsonTransform(Transform):
domain = constraints.real
codomain = constraints.real
def __init__(self, lbda):
super().__init__()
self.lbda = lbda
# Positive domain transforms
if self.lbda == 0:
self.positive_transform = ComposeTransform([
AffineTransform(loc=1., scale=1.),
ExpTransform().inv,
])
else:
self.positive_transform = ComposeTransform([
AffineTransform(loc=1., scale=1.),
PowerTransform(self.lbda),
AffineTransform(loc=-1., scale=1.),
AffineTransform(loc=0., scale=1. / self.lbda),
])
# Negative domain transforms
if self.lbda == 2:
self.negative_transform = ComposeTransform([
AffineTransform(loc=1., scale=-1.),
ExpTransform().inv,
AffineTransform(loc=1., scale=-1.),
])
else:
self.negative_transform = ComposeTransform([
AffineTransform(loc=1., scale=-1.),
PowerTransform(2. - self.lbda),
AffineTransform(loc=-1., scale=1.),
AffineTransform(loc=0., scale=-1. / (2. - self.lbda)),
])
def _call(self, x):
positive_mask = x >= 0.
negative_mask = ~positive_mask
y = torch.zeros_like(x)
y[positive_mask] = self.positive_transform(x[positive_mask])
y[negative_mask] = self.negative_transform(x[negative_mask])
return y
@property
def exponent(self):
return self.lbda
class YeoJohnsonNormal(TransformedDistribution):
arg_constraints = {
"loc": constraints.real,
"scale": constraints.positive,
"tloc": constraints.real,
"tscale": constraints.real,
"lbda": constraints.real
}
support = constraints.real
has_rsample = False
def __init__(self, loc, scale, lbda, tloc, tscale, validate_args=None):
base_distribution = Normal(loc, scale)
self.yj_transform = YeoJohnsonTransform(lbda=lbda)
self.scalar_transform = AffineTransform(loc=tloc, scale=tscale)
transforms = [
self.yj_transform,
self.scalar_transform,
]
super().__init__(base_distribution, transforms, validate_args=validate_args)
@property
def loc(self):
return self.base_dist.loc
@property
def scale(self):
return self.base_dist.scale
@property
def tloc(self):
return self.transforms[1].loc
@property
def tscale(self):
return self.transforms[1].scale
@property
def lbda(self):
return self.transforms[0].exponent
if __name__ == "__main__":
from sklearn.preprocessing import PowerTransformer
import matplotlib.pyplot as plt
n_samples = 1_000_000
dist = Normal(-5., 0.5)
dist_samples = dist.sample((n_samples,)).view(-1, 1)
power_transformer = PowerTransformer(method='yeo-johnson', standardize=True, )
power_transformer.fit(dist_samples.cpu().numpy())
pt_normal_samples = power_transformer.transform(dist_samples.cpu().numpy())
rec_pt_normal_samples = power_transformer.inverse_transform(pt_normal_samples)
tloc = torch.tensor(power_transformer._scaler.mean_)
tscale = torch.tensor(power_transformer._scaler.scale_)
lbda = torch.tensor(power_transformer.lambdas_)
yjnormal = YeoJohnsonNormal(
loc=torch.tensor([0.]),
scale=torch.tensor([1.]),
lbda=lbda,
tloc=tloc,
tscale=tscale
)
samples = yjnormal.sample((int(n_samples),))
fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(12, 6), constrained_layout=True, sharey=True)
ax0.hist(pt_normal_samples, bins=50, density=True,
label="sklearn YJT normalized", alpha=0.3)
ax0.hist(Normal(0., 1).sample((n_samples,)).view(-1, 1).cpu().numpy(), bins=50, density=True,
label="Standard normal", alpha=0.3)
ax0.legend()
ax0.set_ylabel("Density")
ax0.set_xlabel("X")
x = torch.linspace(-10, 10, 1000)
# ax1.hist(dist_samples.cpu().numpy(), bins=50, density=True,
# label="Data", alpha=0.3)
ax1.plot(x.cpu().numpy(),
dist.log_prob(x).exp().cpu().numpy(),
label="Data PDF", ls="dashed")
ax1.hist(samples.cpu().numpy(), bins=50, density=True,
label="Yeo-Johnson Transformed Normal", alpha=0.3)
ax1.hist(rec_pt_normal_samples, bins=50, density=True,
label="sklearn YJT reconstructed", alpha=0.3)
ax1.set_xlabel("X")
ax1.legend()
plt.show()