File: varfuncs.py

package info (click to toggle)
python-scipy 0.5.2-0.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 33,888 kB
  • ctags: 44,231
  • sloc: ansic: 156,256; cpp: 90,347; python: 89,604; fortran: 73,083; sh: 1,318; objc: 424; makefile: 342
file content (53 lines) | stat: -rw-r--r-- 935 bytes parent folder | download
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
import numpy as N

class VarianceFunction:
    """
    Variance function that relates the variance of a random variable
    to its mean. Defaults to 1.

    """

    def __call__(self, mu):
        return N.ones(mu.shape, N.float64)

constant = VarianceFunction()

class Power:

    """
    Variance function:

    V(mu) = fabs(mu)**power
    """

    def __init__(self, power=1.):
        self.power = power

    def __call__(self, mu):
        return N.power(N.fabs(mu), self.power)

class Binomial:

    """
    Binomial variance function

    p = mu / n; V(mu) = p * (1 - p) * n
    """

    tol = 1.0e-10

    def __init__(self, n=1):
        self.n = n

    def clean(self, p):
        return N.clip(p, Binomial.tol, 1 - Binomial.tol)

    def __call__(self, mu):
        p = self.clean(mu / self.n)
        return p * (1 - p) * self.n

mu = Power()
mu_squared = Power(power=2)
mu_cubed = Power(power=3)
binary = Binomial()