|
| 1 | +# Test corr.jl |
| 2 | +# |
| 3 | +# Many test cases are migrated from test/01.jl in the old version |
| 4 | +# The reference results are generated from R. |
| 5 | +# |
| 6 | + |
| 7 | +using StatsBase |
| 8 | +using Test |
| 9 | + |
| 10 | +# random data for testing |
| 11 | + |
| 12 | +x = [-2.133252557240862 -.7445937365828654; |
| 13 | + .1775816414485478 -.5834801838041446; |
| 14 | + -.6264517920318317 -.68444205333293; |
| 15 | + -.8809042583216906 .9071671734302398; |
| 16 | + .09251017186697393 -1.0404476733379926; |
| 17 | + -.9271887119115569 -.620728578941385; |
| 18 | + 3.355819743178915 -.8325051361909978; |
| 19 | + -.2834039258495755 -.22394811874731657; |
| 20 | + .5354280026977677 .7481337671592626; |
| 21 | + .39182285417742585 .3085762550821047] |
| 22 | + |
| 23 | +x1 = view(x, :, 1) |
| 24 | +x2 = view(x, :, 2) |
| 25 | +realx = convert(AbstractMatrix{Real}, x) |
| 26 | +realx1 = convert(AbstractVector{Real}, x1) |
| 27 | +realx2 = convert(AbstractVector{Real}, x2) |
| 28 | + |
| 29 | +# autocov & autocorr |
| 30 | + |
| 31 | +@test autocov([1:5;]) ≈ [2.0, 0.8, -0.2, -0.8, -0.8] |
| 32 | +@test autocor([1, 2, 3, 4, 5]) ≈ [1.0, 0.4, -0.1, -0.4, -0.4] |
| 33 | + |
| 34 | +racovx1 = [1.839214242630635709475, |
| 35 | + -0.406784553146903871124, |
| 36 | + 0.421772254824993531042, |
| 37 | + 0.035874943792884653182, |
| 38 | + -0.255679775928512320604, |
| 39 | + 0.231154400105831353551, |
| 40 | + -0.787016960267425180753, |
| 41 | + 0.039909287349160660341, |
| 42 | + -0.110149697877911914579, |
| 43 | + -0.088687020167434751916] |
| 44 | + |
| 45 | +@test autocov(x1) ≈ racovx1 |
| 46 | +@test autocov(realx1) ≈ racovx1 |
| 47 | +@test autocov(x) ≈ [autocov(x1) autocov(x2)] |
| 48 | +@test autocov(realx) ≈ [autocov(realx1) autocov(realx2)] |
| 49 | + |
| 50 | +racorx1 = [0.999999999999999888978, |
| 51 | + -0.221173011668873431557, |
| 52 | + 0.229321981664153962122, |
| 53 | + 0.019505581764945757045, |
| 54 | + -0.139015765538446717242, |
| 55 | + 0.125681062460244019618, |
| 56 | + -0.427909344123907742219, |
| 57 | + 0.021699096507690283225, |
| 58 | + -0.059889541590524189574, |
| 59 | + -0.048220059475281865091] |
| 60 | + |
| 61 | +@test autocor(x1) ≈ racorx1 |
| 62 | +@test autocor(realx1) ≈ racorx1 |
| 63 | +@test autocor(x) ≈ [autocor(x1) autocor(x2)] |
| 64 | +@test autocor(realx) ≈ [autocor(realx1) autocor(realx2)] |
| 65 | + |
| 66 | + |
| 67 | +# crosscov & crosscor |
| 68 | + |
| 69 | +rcov0 = [0.320000000000000006661, |
| 70 | + -0.319999999999999951150, |
| 71 | + 0.080000000000000029421, |
| 72 | + -0.479999999999999982236, |
| 73 | + 0.000000000000000000000, |
| 74 | + 0.479999999999999982236, |
| 75 | + -0.080000000000000029421, |
| 76 | + 0.319999999999999951150, |
| 77 | + -0.320000000000000006661] |
| 78 | + |
| 79 | +@test crosscov([1, 2, 3, 4, 5], [1, -1, 1, -1, 1]) ≈ rcov0 |
| 80 | +@test crosscov([1:5;], [1:5;]) ≈ [-0.8, -0.8, -0.2, 0.8, 2.0, 0.8, -0.2, -0.8, -0.8] |
| 81 | + |
| 82 | +c11 = crosscov(x1, x1) |
| 83 | +c12 = crosscov(x1, x2) |
| 84 | +c21 = crosscov(x2, x1) |
| 85 | +c22 = crosscov(x2, x2) |
| 86 | +@test crosscov(realx1, realx2) ≈ c12 |
| 87 | + |
| 88 | +@test crosscov(x, x1) ≈ [c11 c21] |
| 89 | +@test crosscov(realx, realx1) ≈ [c11 c21] |
| 90 | +@test crosscov(x1, x) ≈ [c11 c12] |
| 91 | +@test crosscov(realx1, realx) ≈ [c11 c12] |
| 92 | +@test crosscov(x, x) ≈ cat([c11 c21], [c12 c22], dims=3) |
| 93 | +@test crosscov(realx, realx) ≈ cat([c11 c21], [c12 c22], dims=3) |
| 94 | + |
| 95 | +rcor0 = [0.230940107675850, |
| 96 | + -0.230940107675850, |
| 97 | + 0.057735026918963, |
| 98 | + -0.346410161513775, |
| 99 | + 0.000000000000000, |
| 100 | + 0.346410161513775, |
| 101 | + -0.057735026918963, |
| 102 | + 0.230940107675850, |
| 103 | + -0.230940107675850] |
| 104 | + |
| 105 | +@test crosscor([1, 2, 3, 4, 5], [1, -1, 1, -1, 1]) ≈ rcor0 |
| 106 | +@test crosscor([1:5;], [1:5;]) ≈ [-0.4, -0.4, -0.1, 0.4, 1.0, 0.4, -0.1, -0.4, -0.4] |
| 107 | + |
| 108 | +c11 = crosscor(x1, x1) |
| 109 | +c12 = crosscor(x1, x2) |
| 110 | +c21 = crosscor(x2, x1) |
| 111 | +c22 = crosscor(x2, x2) |
| 112 | +@test crosscor(realx1, realx2) ≈ c12 |
| 113 | + |
| 114 | +@test crosscor(x, x1) ≈ [c11 c21] |
| 115 | +@test crosscor(realx, realx1) ≈ [c11 c21] |
| 116 | +@test crosscor(x1, x) ≈ [c11 c12] |
| 117 | +@test crosscor(realx1, realx) ≈ [c11 c12] |
| 118 | +@test crosscor(x, x) ≈ cat([c11 c21], [c12 c22], dims=3) |
| 119 | +@test crosscor(realx, realx) ≈ cat([c11 c21], [c12 c22], dims=3) |
| 120 | + |
| 121 | + |
| 122 | +## pacf |
| 123 | + |
| 124 | +rpacfr = [-0.218158122381419, |
| 125 | + 0.195015316828711, |
| 126 | + 0.144315804606139, |
| 127 | + -0.199791229449779] |
| 128 | + |
| 129 | +@test pacf(x[:,1], 1:4) ≈ rpacfr |
| 130 | + |
| 131 | +rpacfy = [-0.221173011668873, |
| 132 | + 0.189683314308021, |
| 133 | + 0.111857020733719, |
| 134 | + -0.175020669835420] |
| 135 | + |
| 136 | +@test pacf(x[:,1], 1:4, method=:yulewalker) ≈ rpacfy |
0 commit comments