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
|
import numpy as N
import scipy.stats
class Link:
def initialize(self, Y):
return N.asarray(Y).mean() * N.ones(Y.shape)
class Logit(Link):
"""
The logit transform as a link function:
g(x) = log(x / (1 - x))
"""
tol = 1.0e-10
def clean(self, p):
return N.clip(p, Logit.tol, 1. - Logit.tol)
def __call__(self, p):
p = self.clean(p)
return N.log(p / (1. - p))
def inverse(self, z):
t = N.exp(z)
return t / (1. + t)
def deriv(self, p):
p = self.clean(p)
return 1. / (p * (1 - p))
logit = Logit()
class Power(Link):
"""
The power transform as a link function:
g(x) = x**power
"""
def __init__(self, power=1.):
self.power = power
def __call__(self, x):
return N.power(x, self.power)
def inverse(self, x):
return N.power(x, 1. / self.power)
def deriv(self, x):
return self.power * N.power(x, self.power - 1)
inverse = Power(power=-1.)
inverse.__doc__ = """
The inverse transform as a link function:
g(x) = 1 / x
"""
sqrt = Power(power=0.5)
sqrt.__doc__ = """
The square-root transform as a link function:
g(x) = sqrt(x)
"""
inverse_squared = Power(power=-2.)
inverse_squared.__doc__ = """
The inverse squared transform as a link function:
g(x) = 1 / x**2
"""
identity = Power(power=1.)
identity.__doc__ = """
The identity transform as a link function:
g(x) = x
"""
class Log(Link):
"""
The log transform as a link function:
g(x) = log(x)
"""
tol = 1.0e-10
def clean(self, x):
return N.clip(x, Logit.tol, N.inf)
def __call__(self, x, **extra):
x = self.clean(x)
return N.log(x)
def inverse(self, z):
return N.exp(z)
def deriv(self, x):
x = self.clean(x)
return 1. / x
log = Log()
class CDFLink(Logit):
"""
The use the CDF of a scipy.stats distribution as a link function:
g(x) = dbn.ppf(x)
"""
def __init__(self, dbn=scipy.stats.norm):
self.dbn = dbn
def __call__(self, p):
p = self.clean(p)
return self.dbn.ppf(p)
def inverse(self, z):
return self.dbn.cdf(z)
def deriv(self, p):
p = self.clean(p)
return 1. / self.dbn.pdf(self(p))
probit = CDFLink()
probit.__doc__ = """
The probit (standard normal CDF) transform as a link function:
g(x) = scipy.stats.norm.ppf(x)
"""
cauchy = CDFLink(dbn=scipy.stats.cauchy)
cauchy.__doc__ = """
The Cauchy (standard Cauchy CDF) transform as a link function:
g(x) = scipy.stats.cauchy.ppf(x)
"""
class CLogLog(Logit):
"""
The complementary log-log transform as a link function:
g(x) = log(-log(x))
"""
def __call__(self, p):
p = self.clean(p)
return N.log(-N.log(p))
def inverse(self, z):
return N.exp(-N.exp(z))
def deriv(self, p):
p = self.clean(p)
return -1. / (N.log(p) * p)
cloglog = CLogLog()
|