# x, y, rho and Phi(x, y, rho) computed using 10000 Gauss points
using StaticArrays

data = [
    1.4643071301680395 0.2608711310869101 -0.39340549998271723 0.5412822677479813
    1.1341188715556492 -1.5745869848370844 0.047636263318690336 0.05137668451285231
    -0.5638279972380127 -1.0279660845549863 -0.9024285135658796 6.758922980430737e-6
    0.40752339592449827 1.4121749577696892 0.599862776662886 0.6426102791769261
    -0.3112378143828986 0.09830439448059536 -0.27232272454795603 0.16195548268402457
    0.21327813956606173 -0.949087921746004 0.014741864645310599 0.10156803307865547
    0.7772983205313361 0.05747447390909049 0.2452968774301893 0.4377541290132034
    1.1675220009018015 0.28224129574653145 0.9163748809032705 0.6104378198196154
    0.10817469836005027 0.14109232234154198 0.22726503215215224 0.3379740085030564
    -1.8380592107548481 -1.5555742025386252 -0.31576708068649745 0.0002959449027418672
    0.643694737206525 1.2972628288018058 0.28647938907273673 0.6859654918890726
    -0.12308284078752839 0.3187970252733201 0.44882000014473206 0.35077850917848047
    0.4421427803274385 -1.0104910883715064 -0.3192501356898158 0.07502632133595051
    0.07200507807828677 -1.2444991825818057 -0.08607973357657019 0.050070937587043035
    -1.1034372169774 1.9233445877702353 0.5130398911643251 0.1347986750919063
    -1.867527226614093 0.47882320039568355 -0.06002767119617492 0.01961640082984873
    0.44972418526181324 0.05118485079801709 0.8076998575057721 0.4817208100419016
    0.24537526050312564 -1.2026725186704708 -0.9331611608750189 0.00015601307309313917
    -0.7361552384181024 -0.9948592466621384 -0.44632484102400966 0.009749883973644399
    -0.4450002990337082 0.9776749351851451 0.475920120611815 0.3118895797012318
    Inf -0.6733567529686127 0.9543028899648932 0.25036017750415995
    -Inf 0.12537172573685507 0.6490753182220962 0.0
    -Inf Inf 0.4691937784801415 0.0
    -Inf 1.6557106424466033 -0.21725994167383422 0.0
    Inf -1.4682072138174136 -0.02027841919955494 0.07102397267717517
    -0.2363119428322761 -Inf 0.5737718491966176 0.0
    Inf 0.21544610030974956 0.3517368660664412 0.58529023452831
    0.9993603424079786 Inf 0.17674031246464647 0.8411899181549679
#    -2.1081948831537214 0.03380065156887355 -0.9488037280958662 4.774982492738999e-13
    -Inf -0.08625166314905748 -0.30604233868164277 0.0
    1.4859295914108603 -Inf 0.199939784570541 0.0
    0.7475058996740315 -Inf 0.07531069470545042 0.0
    -Inf Inf -0.44055484032035086 0.0
    Inf -Inf -0.5163347434074138 0.0
    -0.6246291628524274 -1.6712393716866896 -0.2882232466347343 0.0048098181070054605
    -Inf -Inf -0.6626521561983009 0.0
    -Inf -Inf -0.21276586850580753 0.0
    Inf -Inf -0.22060273983396822 0.0
    Inf 1.1679218940355824 0.8628552904703347 0.8785808640718042
    Inf Inf 0.2047007663963864 1.0
    1.4643071301680395 0.2608711310869101 0.9018650879232706 0.602781040176274
    -1.5745869848370844 -0.8998931491797961 0.8968484585837353 0.05514962202849921
    -0.06462944484077995 0.40752339592449827 -0.9097039001685411 0.1522552330577691
    -0.3112378143828986 0.09830439448059536 -0.9511622546807661 0.018122797835366328
#    -0.949087921746004 -0.41839491618414215 -0.9832714583771583 3.3306690738754696e-16
    -0.5206684964371484 1.1675220009018015 -0.8937184403584206 0.18451550710692455
    0.10817469836005027 0.14109232234154198 0.9241414860485208 0.48746907008890855
    -1.5555742025386252 1.0980377024685246 -0.8922680775583764 0.0064844180972321586
    -1.6191589467053888 -0.12308284078752839 -0.9173323232250136 2.409223393735893e-7
    0.4421427803274385 -1.0104910883715064 -0.9723685127796805 0.00018629380796320039
    -1.2444991825818057 0.7321681826266581 0.9189212215747404 0.10665793625632225
    1.3775612444949075 -1.867527226614093 -0.9430317005654634 0.0011605534014627501
    0.44972418526181324 0.05118485079801709 -0.9793209397632535 0.19413558048329926
    -1.2026725186704708 -0.050779830784901565 0.9209700880204509 0.11447955295814552
    -0.27768412415298127 -0.4450002990337082 -0.9159636302721306 0.0025951526377828993
    0.8486637089024585 -1.4446783561804906 0.8965098114508927 0.07427411843427388
    1.9444589221380262 -0.7083489702163178 0.9799915616744654 0.2393642867830409
    0.1450805244965497 0.8437651107489547 0.9696991409001233 0.5576179126178168
    -0.35191886502552666 0.6718101538102055 0.9680571279920187 0.3624489827303736
    -0.5410899301286479 1.6557106424466033 0.9509727243269613 0.29422279933864653
]
for i in 1:size(data, 1)
    local x, y, ρ, pr = data[i, :]
    @test GaussianDistributions.Phi(x, y, ρ) ≈ pr
end

P = GaussianDistributions.BiNormal(0.6)
@test cdf(P, [0,0]) ≈ 1/4 + asin(P.ρ)/2pi

x, y = data[2, 1:2]
dx = dy = 1e-5
@test isapprox(((cdf(P, [x + dx, y + dx]) - cdf(P, [x+dx, y]))/dy + (cdf(P, [x, y])-cdf(P, [x, y+dx]))/dy)/dx,  pdf(P, [x,y]), atol=1e-6)


L = rand(2,2)
μ = rand(2)
P = Gaussian(μ, L*L')
σ1 = sqrt(P.Σ[1,1])
σ2 = sqrt(P.Σ[2,2])
ρ = P.Σ[1,2]/(σ1*σ2)
P2 = GaussianDistributions.BiNormal(ρ)
z = [rand(P2)...]

@test cdf(P2, z) ≈ cdf(P, μ + z.*[σ1, σ2])
