-
Notifications
You must be signed in to change notification settings - Fork 0
/
CensoredData.py
48 lines (40 loc) · 1.6 KB
/
CensoredData.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
import numpy as np
import statsmodels.api as sm
import logging
from matplotlib.pyplot import cm
import matplotlib.pyplot as plt
import os
logging.basicConfig(level=logging.DEBUG)
class CensoredData(object):
def __init__(self, dirname, coeff=3, variance=1, l1=0.0):
self.logger = logging.getLogger(self.__class__.__name__)
x = np.arange(-5, 5, 0.01)
sdev = np.sqrt(variance)
epsilon = sdev * np.random.standard_normal(x.shape[0])
ystar = 1 + coeff*x + epsilon
y = np.where(ystar < l1, l1, ystar)
self.x = x
self.y = y
self.dirname = dirname
def fitOLS(self):
x = sm.add_constant(self.x[:, np.newaxis], has_constant="add")
olsModel = sm.OLS(self.y, x).fit()
self.logger.info(olsModel.summary())
predicted = olsModel.predict(x)
fig, axs = plt.subplots(1, 1, figsize=(10, 10))
colors = cm.rainbow(np.linspace(0, 1, 2))
axs.scatter(self.x, self.y, c=colors[0], label="Observed")
axs.scatter(self.x, predicted, c=colors[1], label="OLS Predicted")
axs.grid()
axs.legend()
axs.set_xlabel("X")
axs.set_ylabel("y")
axs.set(title="Spurious Fitting of Censored Data Using OLS")
fig.tight_layout()
plt.savefig(os.path.join(self.dirname, f"plot_{self.__class__.__name__}.jpeg"),
dpi=500)
plt.show()
if __name__ == "__main__":
dirname = r"C:\prog\cygwin\home\samit_000\latex\book_stats\code\data"
censoredData = CensoredData(dirname)
censoredData.fitOLS()