Пример #1
0
def tst_robust_glm():
    from scitbx.glmtbx import robust_glm
    from scitbx.array_family import flex
    from numpy.random import poisson, seed
    from math import exp

    seed(0)

    n_obs = 100

    for c in range(1, 100):

        # Test for a constant value
        X = flex.double([1 for i in range(n_obs)])
        X.reshape(flex.grid(n_obs, 1))
        Y = flex.double(list(poisson(c, n_obs)))
        B = flex.double([0])
        result = robust_glm(X, Y, B, family="poisson", max_iter=100)
        assert (abs(c - exp(result.parameters()[0])) < 0.1 * c)

    # Now test with a massive outlier
    for c in range(1, 100):

        # Test for a constant value
        X = flex.double([1 for i in range(n_obs)])
        X.reshape(flex.grid(n_obs, 1))
        Y = flex.double(list(poisson(c, n_obs)))
        Y[n_obs // 2] = c * 100
        B = flex.double([0])
        result = robust_glm(X, Y, B, family="poisson", max_iter=100)
        assert (abs(c - exp(result.parameters()[0])) < 0.1 * c)

    print('OK')
Пример #2
0
def tst_robust_glm():
  from scitbx.glmtbx import robust_glm
  from scitbx.array_family import flex
  from numpy.random import poisson, seed
  from math import exp

  seed(0)

  n_obs = 100

  for c in range(1, 100):

    # Test for a constant value
    X = flex.double([1 for i in range(n_obs)])
    X.reshape(flex.grid(n_obs, 1))
    Y = flex.double(list(poisson(c, n_obs)))
    B = flex.double([0])
    result = robust_glm(X, Y, B, family="poisson", max_iter=100)
    assert(abs(c - exp(result.parameters()[0])) < 0.1*c)

  # Now test with a massive outlier
  for c in range(1, 100):

    # Test for a constant value
    X = flex.double([1 for i in range(n_obs)])
    X.reshape(flex.grid(n_obs, 1))
    Y = flex.double(list(poisson(c, n_obs)))
    Y[n_obs//2] = c * 100
    B = flex.double([0])
    result = robust_glm(X, Y, B, family="poisson", max_iter=100)
    assert(abs(c - exp(result.parameters()[0])) < 0.1*c)

  print 'OK'
Пример #3
0
 0,
 4,
 4,
 3,
 0,
 1,
 4,
 1,
 1,
 1,
 6]
    # a[4] = 10
    # a[50] = 100

    X = flex.double([1] * len(a))
    X.reshape(flex.grid(len(a), 1))

    Y = flex.double(a)
    B = flex.double([0])

    result = robust_glm(X, Y, B, family="poisson")
    print list(result.parameters())


  from matplotlib import pylab
  print "MOM1: ", sum(means1) / len(means1)
  print "MOM2: ", sum(means2) / len(means2)
  pylab.plot(means1, color='black')
  pylab.plot(means2, color='blue')
  pylab.show()
Пример #4
0
    # Y = [exp(known[0]*x[0] + known[1]*x[1]) for x in X]

    # Y1 = [int(poisson(exp(known[0]*x[0] + known[1]*x[1]),1)[0]) for x in X]
    Y1 = [int(poisson(known[0]*x[0] + known[1]*x[1],1)[0]) for x in X]
    # Y1 = [int(exp(known[0]*x[0] + known[1]*x[1])) for x in X]

    initial = median(Y1)
    if initial > 0:
      initial = log(initial)

    initial = [initial, 0]

    st = time()
    result = robust_glm(
      flex.double(X),
      flex.double(Y1),
      flex.double(initial),
      max_iter=1000)
    assert result.converged()
    tt += time() - st

    beta = list(result.parameters())

    print beta

    Y2 = [exp(beta[0]*x[0] + beta[1]*x[1]) for x in X]

    P = [1.0 for x in X]

    result = glm(
      flex.double(X),
Пример #5
0
    seed(0)
    means1 = []
    means2 = []

    for k in range(10):
        print(k)
        a = list(poisson(100, 100))
        # a[4] = 10
        # a[50] = 100
        means1.append(sum(a) / len(a))
        # means2.append(glm2(a))

        X = flex.double([1] * len(a))
        X.reshape(flex.grid(len(a), 1))

        Y = flex.double(a)
        B = flex.double([0])

        result = robust_glm(X, Y, B, family="poisson")
        print(list(result.parameters()))
        print(list(glm2(a)))

    from matplotlib import pylab

    print("MOM1: ", sum(means1) / len(means1))
    print("MOM2: ", sum(means2) / len(means2))
    pylab.plot(means1, color="black")
    pylab.plot(means2, color="blue")
    pylab.show()
Пример #6
0
        # Y = [exp(known[0]*x[0] + known[1]*x[1]) for x in X]

        # Y1 = [int(poisson(exp(known[0]*x[0] + known[1]*x[1]),1)[0]) for x in X]
        Y1 = [int(poisson(known[0] * x[0] + known[1] * x[1], 1)[0]) for x in X]
        # Y1 = [int(exp(known[0]*x[0] + known[1]*x[1])) for x in X]

        initial = median(Y1)
        if initial > 0:
            initial = log(initial)

        initial = [initial, 0]

        st = time()
        result = robust_glm(flex.double(X),
                            flex.double(Y1),
                            flex.double(initial),
                            max_iter=1000)
        assert result.converged()
        tt += time() - st

        beta = list(result.parameters())

        print(beta)

        Y2 = [exp(beta[0] * x[0] + beta[1] * x[1]) for x in X]

        P = [1.0 for x in X]

        result = glm(
            flex.double(X),
            flex.double(Y1),