def test_crs_with_specific_constraint(): from patsy.highlevel import incr_dbuilder, build_design_matrices, dmatrix x = (-1.5)**np.arange(20) # Hard coded R values for smooth: s(x, bs="cr", k=5) # R> knots <- smooth$xp knots_R = np.array([ -2216.837820053100585937, -50.456909179687500000, -0.250000000000000000, 33.637939453125000000, 1477.891880035400390625 ]) # R> centering.constraint <- t(qr.X(attr(smooth, "qrc"))) centering_constraint_R = np.array([[ 0.064910676323168478574, 1.4519875239407085132, -2.1947446912471946234, 1.6129783104357671153, 0.064868180547550072235 ]]) # values for which we want a prediction new_x = np.array([-3000., -200., 300., 2000.]) result1 = dmatrix("cr(new_x, knots=knots_R[1:-1], " "lower_bound=knots_R[0], upper_bound=knots_R[-1], " "constraints=centering_constraint_R)") data_chunked = [{"x": x[:10]}, {"x": x[10:]}] new_data = {"x": new_x} builder = incr_dbuilder("cr(x, df=4, constraints='center')", lambda: iter(data_chunked)) result2 = build_design_matrices([builder], new_data)[0] assert np.allclose(result1, result2, rtol=1e-12, atol=0.)
def test_crs_with_specific_constraint(): from patsy.highlevel import incr_dbuilder, build_design_matrices, dmatrix x = (-1.5)**np.arange(20) # Hard coded R values for smooth: s(x, bs="cr", k=5) # R> knots <- smooth$xp knots_R = np.array([-2216.837820053100585937, -50.456909179687500000, -0.250000000000000000, 33.637939453125000000, 1477.891880035400390625]) # R> centering.constraint <- t(qr.X(attr(smooth, "qrc"))) centering_constraint_R = np.array([[0.064910676323168478574, 1.4519875239407085132, -2.1947446912471946234, 1.6129783104357671153, 0.064868180547550072235]]) # values for which we want a prediction new_x = np.array([-3000., -200., 300., 2000.]) result1 = dmatrix("cr(new_x, knots=knots_R[1:-1], " "lower_bound=knots_R[0], upper_bound=knots_R[-1], " "constraints=centering_constraint_R)") data_chunked = [{"x": x[:10]}, {"x": x[10:]}] new_data = {"x": new_x} builder = incr_dbuilder("cr(x, df=4, constraints='center')", lambda: iter(data_chunked)) result2 = build_design_matrices([builder], new_data)[0] assert np.allclose(result1, result2, rtol=1e-12, atol=0.)
def test_te_3smooths(): from patsy.highlevel import incr_dbuilder, build_design_matrices x1 = (-1.5)**np.arange(20) x2 = (1.6)**np.arange(20) x3 = (-1.2)**np.arange(20) # Hard coded R results for smooth: te(x1, x2, x3, bs=c("cr", "cs", "cc"), k=c(3,3,4)) design_matrix_R = \ np.array([[7.2077663709837084334e-05, 2.0648333344343273131e-03, -4.7934014082310591768e-04, 2.3923430783992746568e-04, 6.8534265421922660466e-03, -1.5909867344112936776e-03, -6.8057712777151204314e-09, -1.9496724335203412851e-07, 4.5260614658693259131e-08, 0.0101479754187435277507, 0.290712501531622591333, -0.067487370093906928759, 0.03368233306025386619709, 0.9649092451763204847381, -0.2239985793289433757547, -9.5819975394704535133e-07, -2.7449874082511405643e-05, 6.3723431275833230217e-06, -1.5205851762850489204e-04, -0.00435607204539782688624, 0.00101123909269346416370, -5.0470024059694933508e-04, -1.4458319360584082416e-02, 3.3564223914790921634e-03, 1.4357783514933466209e-08, 4.1131230514870551983e-07, -9.5483976834512651038e-08]]) new_data = {"x1": -38.443359375000000000, "x2": 68.719476736000032702, "x3": -5.1597803519999985156} data_chunked = [{"x1": x1[:10], "x2": x2[:10], "x3": x3[:10]}, {"x1": x1[10:], "x2": x2[10:], "x3": x3[10:]}] builder = incr_dbuilder("te(cr(x1, df=3), cr(x2, df=3), cc(x3, df=3)) - 1", lambda: iter(data_chunked)) design_matrix = build_design_matrices([builder], new_data)[0] assert np.allclose(design_matrix, design_matrix_R, rtol=1e-12, atol=0.)
def test_te_2smooths(): from patsy.highlevel import incr_dbuilder, build_design_matrices x1 = (-1.5)**np.arange(20) x2 = (1.6)**np.arange(20) # Hard coded R results for smooth: te(x1, x2, bs=c("cs", "cc"), k=c(5,7)) # Without centering constraint: dmatrix_R_nocons = \ np.array([[-4.4303024184609255207e-06, 7.9884438387230142235e-06, 9.7987758194797719025e-06, -7.2894213245475212959e-08, 1.5907686862964493897e-09, -3.2565884983072595159e-11, 0.0170749607855874667439, -3.0788499835965849050e-02, -3.7765754357352458725e-02, 2.8094376299826799787e-04, -6.1310290747349201414e-06, 1.2551314933193442915e-07, -0.26012671685838206770, 4.6904420337437874311e-01, 0.5753384627946153129230, -4.2800085814700449330e-03, 9.3402525733484874533e-05, -1.9121170389937518131e-06, -0.0904312240489447832781, 1.6305991924427923334e-01, 2.0001237112941641638e-01, -1.4879148887003382663e-03, 3.2470731316462736135e-05, -6.6473404365914134499e-07, 2.0447857920168824846e-05, -3.6870296695050991799e-05, -4.5225801045409022233e-05, 3.3643990293641665710e-07, -7.3421200200015877329e-09, 1.5030635073660743297e-10], [-9.4006130602653794302e-04, 7.8681398069163730347e-04, 2.4573006857381437217e-04, -1.4524712230452725106e-04, 7.8216741353106329551e-05, -3.1304283003914264551e-04, 3.6231183382798337611064, -3.0324832476174168328e+00, -9.4707559178211142559e-01, 5.5980126937492580286e-01, -3.0145747744342332730e-01, 1.2065077148806895302e+00, -35.17561267504181188315, 2.9441339255948005160e+01, 9.1948319320782125885216, -5.4349184288245195873e+00, 2.9267472035096449012e+00, -1.1713569391233907169e+01, 34.0275626863976370373166, -2.8480442582712722555e+01, -8.8947340548151565542e+00, 5.2575353623762932642e+00, -2.8312249982592527786e+00, 1.1331265795534763541e+01, 7.9462158845078978420e-01, -6.6508361863670617531e-01, -2.0771242914526857892e-01, 1.2277550230353953542e-01, -6.6115593588420035198e-02, 2.6461103043402139923e-01]]) # With centering constraint: dmatrix_R_cons = \ np.array([[0.00329998606323867252343, 1.6537431155796576600e-04, -1.2392262709790753433e-04, 6.5405304166706783407e-05, -6.6764045799537624095e-05, -0.1386431081763726258504, 0.124297283800864313830, -3.5487293655619825405e-02, -3.0527115315785902268e-03, 5.2009247643311604277e-04, -0.00384203992301702674378, -0.058901915802819435064, 0.266422358491648914036, 0.5739281693874087597607, -1.3171008503525844392e-03, 8.2573456631878912413e-04, 6.6730833453016958831e-03, -0.1467677784718444955470, 0.220757650934837484913, 0.1983127687880171796664, -1.6269930328365173316e-03, -1.7785892412241208812e-03, -3.2702835436351201243e-03, -4.3252183044300757109e-02, 4.3403766976235179376e-02, 3.5973406402893762387e-05, -5.4035858568225075046e-04, 2.9565209382794241247e-04, -2.2769990750264097637e-04], [0.41547954838956052681098, 1.9843570584107707994e-02, -1.5746590234791378593e-02, 8.3171184312221431434e-03, -8.7233014052017516377e-03, -15.9926770785086258541696, 16.503663226274017716833, -6.6005803955894726265e-01, 1.3986092022708346283e-01, -2.3516913533670955050e-01, 0.72251037497207359905360, -9.827337059999853963177, 3.917078117294827688255, 9.0171773596973618936090, -5.0616811270787671617e+00, 3.0189990249009683865e+00, -1.0872720629943064097e+01, 26.9308504460453121964747, -21.212262927009287949431, -9.1088328555582247503253, 5.2400156972500298025e+00, -3.0593641098325474736e+00, 1.0919392118399086300e+01, -4.6564290223265718538e+00, 4.8071307441606982991e+00, -1.9748377005689798924e-01, 5.4664183716965096538e-02, -2.8871392916916285148e-02, 2.3592766838010845176e-01]]) new_x1 = np.array([11.390625, 656.84083557128906250]) new_x2 = np.array([16.777216000000006346, 1844.6744073709567147]) new_data = {"x1": new_x1, "x2": new_x2} data_chunked = [{ "x1": x1[:10], "x2": x2[:10] }, { "x1": x1[10:], "x2": x2[10:] }] builder = incr_dbuilder("te(cr(x1, df=5), cc(x2, df=6)) - 1", lambda: iter(data_chunked)) dmatrix_nocons = build_design_matrices([builder], new_data)[0] assert np.allclose(dmatrix_nocons, dmatrix_R_nocons, rtol=1e-12, atol=0.) builder = incr_dbuilder( "te(cr(x1, df=5), cc(x2, df=6), " "constraints='center') - 1", lambda: iter(data_chunked)) dmatrix_cons = build_design_matrices([builder], new_data)[0] assert np.allclose(dmatrix_cons, dmatrix_R_cons, rtol=1e-12, atol=0.)
def test_te_2smooths(): from patsy.highlevel import incr_dbuilder, build_design_matrices x1 = (-1.5)**np.arange(20) x2 = (1.6)**np.arange(20) # Hard coded R results for smooth: te(x1, x2, bs=c("cs", "cc"), k=c(5,7)) # Without centering constraint: dmatrix_R_nocons = \ np.array([[-4.4303024184609255207e-06, 7.9884438387230142235e-06, 9.7987758194797719025e-06, -7.2894213245475212959e-08, 1.5907686862964493897e-09, -3.2565884983072595159e-11, 0.0170749607855874667439, -3.0788499835965849050e-02, -3.7765754357352458725e-02, 2.8094376299826799787e-04, -6.1310290747349201414e-06, 1.2551314933193442915e-07, -0.26012671685838206770, 4.6904420337437874311e-01, 0.5753384627946153129230, -4.2800085814700449330e-03, 9.3402525733484874533e-05, -1.9121170389937518131e-06, -0.0904312240489447832781, 1.6305991924427923334e-01, 2.0001237112941641638e-01, -1.4879148887003382663e-03, 3.2470731316462736135e-05, -6.6473404365914134499e-07, 2.0447857920168824846e-05, -3.6870296695050991799e-05, -4.5225801045409022233e-05, 3.3643990293641665710e-07, -7.3421200200015877329e-09, 1.5030635073660743297e-10], [-9.4006130602653794302e-04, 7.8681398069163730347e-04, 2.4573006857381437217e-04, -1.4524712230452725106e-04, 7.8216741353106329551e-05, -3.1304283003914264551e-04, 3.6231183382798337611064, -3.0324832476174168328e+00, -9.4707559178211142559e-01, 5.5980126937492580286e-01, -3.0145747744342332730e-01, 1.2065077148806895302e+00, -35.17561267504181188315, 2.9441339255948005160e+01, 9.1948319320782125885216, -5.4349184288245195873e+00, 2.9267472035096449012e+00, -1.1713569391233907169e+01, 34.0275626863976370373166, -2.8480442582712722555e+01, -8.8947340548151565542e+00, 5.2575353623762932642e+00, -2.8312249982592527786e+00, 1.1331265795534763541e+01, 7.9462158845078978420e-01, -6.6508361863670617531e-01, -2.0771242914526857892e-01, 1.2277550230353953542e-01, -6.6115593588420035198e-02, 2.6461103043402139923e-01]]) # With centering constraint: dmatrix_R_cons = \ np.array([[0.00329998606323867252343, 1.6537431155796576600e-04, -1.2392262709790753433e-04, 6.5405304166706783407e-05, -6.6764045799537624095e-05, -0.1386431081763726258504, 0.124297283800864313830, -3.5487293655619825405e-02, -3.0527115315785902268e-03, 5.2009247643311604277e-04, -0.00384203992301702674378, -0.058901915802819435064, 0.266422358491648914036, 0.5739281693874087597607, -1.3171008503525844392e-03, 8.2573456631878912413e-04, 6.6730833453016958831e-03, -0.1467677784718444955470, 0.220757650934837484913, 0.1983127687880171796664, -1.6269930328365173316e-03, -1.7785892412241208812e-03, -3.2702835436351201243e-03, -4.3252183044300757109e-02, 4.3403766976235179376e-02, 3.5973406402893762387e-05, -5.4035858568225075046e-04, 2.9565209382794241247e-04, -2.2769990750264097637e-04], [0.41547954838956052681098, 1.9843570584107707994e-02, -1.5746590234791378593e-02, 8.3171184312221431434e-03, -8.7233014052017516377e-03, -15.9926770785086258541696, 16.503663226274017716833, -6.6005803955894726265e-01, 1.3986092022708346283e-01, -2.3516913533670955050e-01, 0.72251037497207359905360, -9.827337059999853963177, 3.917078117294827688255, 9.0171773596973618936090, -5.0616811270787671617e+00, 3.0189990249009683865e+00, -1.0872720629943064097e+01, 26.9308504460453121964747, -21.212262927009287949431, -9.1088328555582247503253, 5.2400156972500298025e+00, -3.0593641098325474736e+00, 1.0919392118399086300e+01, -4.6564290223265718538e+00, 4.8071307441606982991e+00, -1.9748377005689798924e-01, 5.4664183716965096538e-02, -2.8871392916916285148e-02, 2.3592766838010845176e-01]]) new_x1 = np.array([11.390625, 656.84083557128906250]) new_x2 = np.array([16.777216000000006346, 1844.6744073709567147]) new_data = {"x1": new_x1, "x2": new_x2} data_chunked = [{"x1": x1[:10], "x2": x2[:10]}, {"x1": x1[10:], "x2": x2[10:]}] builder = incr_dbuilder("te(cr(x1, df=5), cc(x2, df=6)) - 1", lambda: iter(data_chunked)) dmatrix_nocons = build_design_matrices([builder], new_data)[0] assert np.allclose(dmatrix_nocons, dmatrix_R_nocons, rtol=1e-12, atol=0.) builder = incr_dbuilder("te(cr(x1, df=5), cc(x2, df=6), " "constraints='center') - 1", lambda: iter(data_chunked)) dmatrix_cons = build_design_matrices([builder], new_data)[0] assert np.allclose(dmatrix_cons, dmatrix_R_cons, rtol=1e-12, atol=0.)