In [18]:
from test import test_pivots
import constraints as C
import numpy as np

In [56]:
%load_ext rmagic
P = [test_pivots()[-1] for _ in range(5000)]
%R -i P


constraints.py:155: UserWarning: constraints not satisfied: array([ 1.71090646,  3.19314075,  6.09021381, -0.00490885])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 0.16368884,  4.92639745, -0.00287809,  6.7552196 ])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([  0.31635766,  12.32874275,   0.7982411 ,  -0.01141226])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 3.53466421,  1.47008024,  8.37356051, -0.00577252])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 0.87204499,  4.4566665 ,  0.0806998 , -0.00413267])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 1.4227518 ,  2.48865267, -0.00109452,  1.69055639])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 5.2438446 ,  1.25795476,  3.07560632, -0.00089158])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 2.38448543,  3.60504443,  7.9751358 , -0.00197738])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 2.60839568,  2.46304396,  5.88267607, -0.00556586])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 2.27340477, -0.00327003,  4.17737521,  3.65110569])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 0.05066552,  2.42830614,  9.29294178, -0.00898939])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 6.42573816,  0.68559473,  4.32405194, -0.00450235])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 0.25522147,  2.8301038 ,  3.79861842, -0.00101724])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 12.83799862,   2.32861551,   8.45574143,  -0.00244308])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 1.05263305,  2.83419871,  6.92444217, -0.00127581])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 3.65442503,  0.93429408,  1.2477361 , -0.00063576])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 2.27485248, -0.00257864,  5.34866713,  5.48619045])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 8.97454545, -0.00814869,  0.46562482,  0.85755932])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 3.13283917, -0.00505617,  4.24635069,  5.81938168])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([  7.02836122,   0.15326967,  -0.01414688,  14.1579635 ])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 6.94028589,  1.3232656 , -0.0035688 ,  1.78345989])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 5.90910993, -0.00089397,  6.64288162,  6.26222152])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([  0.28488089,  11.73298937,   5.89010126,  -0.01166778])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([  1.17115047,   0.64320035,  14.84978943,  -0.00293713])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([  1.38685368,   7.16464632,  -0.01114959,  12.51132167])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([-0.00351811,  5.16234425,  2.2914652 ,  3.55109154])
  warn('constraints not satisfied: %s' % `U`)
The rmagic extension is already loaded. To reload it, use:
  %reload_ext rmagic

In [57]:
%%R
plot(ecdf(P))
abline(0,1)


Now let's try with a constraint from a linear model?


In [98]:
X = np.random.standard_normal((30,4))
A, b = np.random.standard_normal((6,30)), np.random.standard_normal(6)
E, f = np.random.standard_normal((2,30)), np.random.standard_normal(2)

con = C.constraint((A,b), (E, f)).impose_equality()
Y = C.simulate_from_constraints(con)
Xinv = np.linalg.pinv(X)
beta = np.dot(Xinv, Y)
mu = np.dot(X, beta)
mu /= np.linalg.norm(mu)

In [99]:
tangent = []
for i in range(3):
    tangent.append(X[:,i] - (X[:,i] * mu).sum() * mu)
np.array(tangent).shape


Out[99]:
(3, 30)

In [100]:
con_lm = C.constraint((mu.reshape((1,30)), np.zeros(1)), (np.array(tangent), np.zeros(3)))

In [101]:
new_con = C.stack(con, con_lm)

In [104]:
P = []
u = np.random.standard_normal(30)
u[3] = 1
for i in range(5000):
    Z = C.simulate_from_constraints(con)
    mu = np.dot(X, np.dot(Xinv, Z))
    P.append(con.pivots(u, Z)[-1])


constraints.py:155: UserWarning: constraints not satisfied: array([  0.34561246,   4.34209407,  -0.00939385,   5.08385469,
         3.85511702,  13.42009605])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 4.06363533,  1.99642848,  6.92190195, -0.00299238,  6.76914631,
        1.34102024])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([-0.00075745,  2.57440192,  1.00963718,  3.17885414,  2.97564029,
        0.33199159])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 2.53934087,  0.0692945 , -0.00436632,  2.34309817,  3.41601645,
        5.15952293])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([-0.00456842,  0.16129662,  8.65552546,  1.55020415,  9.99015319,
        6.1542846 ])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 1.09990247,  5.0749894 , -0.00498162,  1.77838864,  1.24161077,
        0.32424454])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 1.35285206,  6.49377271,  7.0897859 , -0.00317562,  8.49670122,
        3.3070897 ])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([  2.91518981,  -0.00395694,   2.13436467,   3.73548534,
         6.33356427,  11.24323496])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([  6.65837178,  14.69388343,  -0.01050323,   1.65436601,
         3.67980264,   1.95324727])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([-0.004124  ,  5.86451985,  1.03075273,  3.51489093,  7.70863828,
        3.21520707])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([  3.57561391,  -0.00453177,   1.79000502,   4.7467756 ,
         5.34276354,  12.84091403])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([-0.00146699,  4.67035099,  3.53106208,  4.31041077,  2.04350419,
        1.27966161])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([-0.00172043,  0.84510551,  7.27096068,  9.73531992,  3.34655101,
        5.18038479])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 2.79101901,  8.68465331,  0.25637352, -0.00427623,  3.5079212 ,
        2.95462604])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 0.87673899, -0.00745473,  7.55394117,  6.1547747 ,  4.65262091,
        3.14188866])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 1.30881649,  2.03070155, -0.00552695,  7.81541343,  9.31114117,
        1.83876923])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([  1.36860024,  -0.00625517,   0.20757683,   1.60026019,
         4.82821491,  10.21284401])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ -0.00517406,  10.57228612,   1.91780438,   5.04191331,
         6.57372956,   4.12174548])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 0.94733132,  3.16773995,  4.38478271,  3.95817394,  2.20618632,
       -0.00088683])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 0.15889682, -0.00444985,  0.16550975,  2.39928332,  0.62852612,
        4.69575606])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 3.89266234,  6.50664823,  1.85656092, -0.00328624,  5.90808802,
        3.32175731])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 2.02344033, -0.00368281,  6.25123962,  7.75554523,  0.03486594,
        0.49905092])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 0.69436823,  4.90857526,  2.7019054 ,  1.38263285,  8.41810556,
       -0.00217016])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([  3.49549927,  -0.00695681,  14.65471546,   4.25460392,
         1.8693866 ,   8.16607242])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 2.12709053,  2.91555712,  1.82124989,  4.73323517,  4.50915641,
       -0.00206903])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 1.28439466,  3.14223849, -0.0014612 ,  1.72674581,  0.12141365,
        0.91541159])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([-0.00515208,  0.74434073,  6.35048055,  3.53075725,  2.15465935,
        7.09502878])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([-0.0078561 ,  4.9470493 ,  0.97531309,  0.44673254,  0.70419064,
        8.52831105])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 0.70962154,  3.40363211,  9.92145887,  2.82527059, -0.00321754,
        3.65601536])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ 2.75790025,  1.22558709,  3.62579995,  6.35858009, -0.00562122,
        0.09694815])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([ -0.00309538,   6.6141941 ,   9.45689998,   0.23804186,
        12.93697903,   7.75948318])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([  1.06217687,  -0.00463081,  12.37618927,   0.35171344,
         9.16008446,   9.40116033])
  warn('constraints not satisfied: %s' % `U`)
constraints.py:155: UserWarning: constraints not satisfied: array([  1.56412332,   5.88093475,   2.36514138,   0.74812274,
        13.29668188,  -0.01219053])
  warn('constraints not satisfied: %s' % `U`)

In [105]:
%%R -i P
P = P[P>0]
P = P[P<1]
plot(ecdf(P))
abline(0,1)



In [105]:


In [ ]: