using Test, GpABC

@testset "RBF Kernel Tests" begin
    @test 2 == get_hyperparameters_size(SquaredExponentialIsoKernel(), zeros(0, 0))
    @test 5 == get_hyperparameters_size(SquaredExponentialArdKernel(), zeros(0, 4))

    # lifted from Matlab
    D2 = [
        1.67814152778737 1.20785927753982 0.624461685654236 1.46408315059547 8.48310688500901 1.45871370121154 6.42543323212624;
        1.14246534460365 1.85035457131647 1.19053740612926 0.611497365423743 6.99118646721199 2.43597535692027 5.07544002153986;
        7.23733046161335 3.16542101637104 2.72647762438244 8.92586928526950 17.1642767758545 1.17728913861911 14.7362524561371;
        19.0995257794008 4.56588070446465 5.99211031953106 11.1739924041597 34.8720573587766 15.7837441497418 30.3384444420246;
        0.466988806886986 3.56126964624601 2.47101254290117 2.18123029882355 4.99339934819349 1.06659412805776 3.56150053043329
    ]
    x1 = [
        0.459530675532078 0.485182659460782;
        0.332063667630806 0.464957557369470;
        0.994061870963342 0.541666708673195;
        0.197865280880530 0.792372670393611;
        0.521775759068213 0.407471305414788
    ]
    x2 = [
        0.370230106343622 0.358754987995492;
        0.457397317261154 0.595083240231072;
        0.476711208222765 0.564018563630068;
        0.0852766000131502 0.459998930552974;
        0.243816421015103 0.202025803831418;
        0.822588000829605 0.447684994641545;
        0.236529943177723 0.241704940442790
    ]
    log_theta = [-log(10)/2, -log(100)/2]
    D2D = [
        0 0.203383856493429 3.17628077043479 10.1212580692727 0.642649959010387;
        0.203383856493429 0 4.97084560152883 10.9001576937249 0.690373692755269;
        3.17628077043479 4.97084560152883 0 12.6246380248108 4.03138234045883;
        10.1212580692727 10.9001576937249 12.6246380248108 0 15.8640860550538;
        0.642649959010387 0.690373692755269 4.03138234045883 15.8640860550539 0
    ]

    @test 4 * ones(5, 1) ≈ covariance_diagonal(SquaredExponentialArdKernel(), [log(2), 0.0, 0.0], x1)
    @test 9 * ones(5, 1) ≈ covariance_diagonal(SquaredExponentialIsoKernel(), [log(3), 0], x1)

    log_theta1 = [log(2); log_theta]
    @test 4 * exp.(-D2D/2) ≈ covariance_training(SquaredExponentialArdKernel(), log_theta1, x1)
    @test 4 * exp.(-D2/2) ≈ covariance(SquaredExponentialArdKernel(), log_theta1, x1, x2)
    @test 4 * exp.(-D2D/2) ≈ covariance_training(SquaredExponentialArdKernel(), log_theta1, x1)
    @test 4 * exp.(-D2/2) ≈ covariance(SquaredExponentialArdKernel(), log_theta1, x1, x2)
    @test 4 * exp.(-D2D/2) ≈ covariance_training(SquaredExponentialArdKernel(), log_theta1, x1)
    @test 4 * exp.(-D2/2) ≈ covariance(SquaredExponentialArdKernel(), log_theta1, x1, x2)

    x1 = [
        0.853623959633347	0.0784714031398629;
        0.857889557656781	0.868872571675513;
        0.549795537511723	0.141312150350452;
        0.413598959526792	0.0333109400390233;
        0.357712766787835	0.856969235863988
    ]
    K = [
        403.428793492735	401.127219311054	403.073312456853	402.706563960842	400.293352208840;
        401.127219311054	403.428793492735	401.129005018406	400.133662383833	402.505043923819;
        403.073312456853	401.129005018406	403.428793492735	403.317183316184	401.405364130706;
        402.706563960842	400.133662383833	403.317183316184	403.428793492735	400.918680124180;
        400.293352208840	402.505043923819	401.405364130706	400.918680124180	403.428793492735
    ]
    @test K ≈ covariance_training(SquaredExponentialIsoKernel(), [3.0, 2.0], x1)
    @test K ≈ covariance(SquaredExponentialIsoKernel(), [3.0, 2.0], x1, x1)
    K = [
        401.977811520080	401.541155407376	402.523543965735	400.671895243459	402.732109940195	402.482565433579	401.263637928358;
        403.301972026813	399.081819073422	403.069411359787	400.973386644735	402.486964554685	400.132636562887	403.418742502755;
        402.078736824856	402.775457037114	402.568058976288	402.175937911770	403.104367431456	403.259534426607	401.152026633490;
        401.293095434847	403.147715014431	401.890001738463	402.191713363450	402.707451571754	403.407040991986	400.131332629428;
        402.684468987700	400.872263943178	402.472762274165	402.816140740862	402.544213669122	401.157890532104	402.329417475725
    ]
    x2 = [
        0.778909885221585	0.701258592634117;
        0.138440919173155	0.0531664015972830;
        0.776528543222942	0.567711645535990;
        0.115225947981340	0.529626523391170;
        0.602507722378945	0.432978635389814;
        0.347290022926074	0.0719249965957948;
        0.903415318533506	0.843417785523188;
    ]
    @test K ≈ covariance(SquaredExponentialIsoKernel(), [3.0, 2.0], x1, x2)

    K = [
        2980.95798704173	2978.65028672416	2978.42445044689	2975.66944742414	2972.01871140493;
        2978.65028672416	2980.95798704173	2976.41449546484	2973.00056583059	2974.13568651361;
        2978.42445044689	2976.41449546484	2980.95798704173	2980.40855933366	2978.05996749357;
        2975.66944742414	2973.00056583059	2980.40855933366	2980.95798704173	2978.36743523035;
        2972.01871140493	2974.13568651361	2978.05996749357	2978.36743523035	2980.95798704173
    ]
    @test K ≈ covariance_training(SquaredExponentialArdKernel(), [4.0, 2.0, 3.0], x1)
    K = [
        2979.37304583894	2967.02516765859	2979.91160562529	2965.36261803110	2978.77301466686	2973.96726404099	2978.72931416283;
        2980.68391806226	2964.41563041355	2980.44223635749	2965.51614192689	2978.47660458701	2971.50931134187	2980.89901384615;
        2978.36770986479	2976.31355555597	2978.88359595894	2975.25091626029	2980.56786900222	2979.82092270343	2975.72768863743;
        2975.67123379203	2978.89038176959	2976.31073840956	2977.61944811412	2979.39404570740	2980.83245035378	2971.99726674807;
        2976.02944167625	2977.26070750597	2975.86478266464	2978.95759612153	2978.65883077957	2978.67897590742	2972.83895895038
    ]
    @test K ≈ covariance(SquaredExponentialArdKernel(), [4.0, 2.0, 3.0], x1, x2)

    R = [
        0.0819471660451718	0.460473784459975	0.831553137317092	0.174000919877552	0.260121663222146;
        0.286019721201630	0.182895418807350	0.913578888361146	0.433460382015193	0.864793774608892;
        0.133454235798425	0.923303895128567	0.520376685783442	0.168640529349745	0.588969851652612;
        0.965341064131512	0.968565425820912	0.970957962316907	0.341376781156928	0.816296725448359;
        0.487264858320013	0.679111772067431	0.890472660255839	0.519560904484038	0.499064610572096;
    ]
    @test [5610.54080009540; 21.9062443257787] * 2 ≈
        covariance_grad(SquaredExponentialIsoKernel(), [3.0, 2.0], x1, R)
    @test [41563.6677709956; 38.4313787854587; 16.7982248189535] * 2 ≈
        covariance_grad(SquaredExponentialArdKernel(), [4.0, 2.0, 3.0], x1, R)

    # R2 = [
    #     0.322721288224087	0.758281509279405	0.220444597406520	0.463054206437922	0.659690255606403	0.473267965239132	0.313020285830377;
    #     0.976691182226265	0.437326458952076	0.0123486113792046	0.552047602158062	0.414125976811222	0.214691774088280	0.931943951142926;
    #     0.248699676826922	0.145795607541593	0.543111450291756	0.264049470547610	0.215292266204624	0.324647101724822	0.181986859153019;
    #     0.895468430079272	0.386775951546978	0.0741450535478746	0.879451493676496	0.417118101109340	0.343838689077570	0.653141784785847;
    #     0.137904342328154	0.543417650062320	0.155914979128127	0.941254220875564	0.503925344675745	0.896847326726116	0.353976895731863
    # ]
    # @test [6374.93996516720; 21.9355980134406] * 2 ≈
    #     covariance_grad(SquaredExponentialIsoKernel(), [3.0, 2.0], x1, x2, mode_regression, R2)
    # @test [4.719203177727137e+04;61.483491155964536;13.680112702542862] * 2 ≈
    #     covariance_grad(SquaredExponentialArdKernel(), [4.0, 2.0, 3.0], x1, x2, mode_regression, R2)
end;
