1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
package statmodel
import (
"math"
"testing"
"gonum.org/v1/gonum/floats"
"github.com/kshedden/dstream/dstream"
)
func TestKO1(t *testing.T) {
x1 := []float64{3, 1, 5, 4, 2, 7, 5}
x2 := []float64{4, 1, 2, 3, 8, 6, 2}
x3 := []float64{2, 1, 5, 4, 9, 3, 1}
x4 := []float64{1, 0, 3, 2, 5, 2, 4} // Don't knockoff this one
da := dstream.NewFromArrays(
[][]interface{}{{x1}, {x2}, {x3}, {x4}},
[]string{"x1", "x2", "x3", "x4"})
ko, err := NewKnockoff(da, []string{"x1", "x2", "x3"})
if err != nil {
panic(err)
}
lmin := ko.lmin
da = dstream.Dstream(ko)
for r := 0; r < 3; r++ {
if len(da.Names()) != 7 {
t.Fail()
}
if da.NumVar() != 7 {
t.Fail()
}
da.Reset()
scale := make([]float64, 7)
for da.Next() {
for j := 0; j < 7; j++ {
z := da.GetPos(j).([]float64)
scale[j] += floats.Dot(z, z)
}
for i := 0; i < 3; i++ {
for j := 0; j <= i; j++ {
a1 := da.GetPos(i).([]float64)
a2 := da.GetPos(j).([]float64)
b1 := da.GetPos(4 + i).([]float64)
b2 := da.GetPos(4 + j).([]float64)
for _, x := range [][]float64{a1, a2, b1, b2} {
u := floats.Dot(x, x)
if math.Abs(u-1) > 1e-6 {
t.Fail()
}
}
if math.Abs(floats.Dot(a1, a2)-floats.Dot(b1, b2)) > 1e-6 {
t.Fail()
}
d := math.Abs(floats.Dot(a1, b2) - floats.Dot(a1, a2))
if i != j && d > 1e-6 {
t.Fail()
}
if i == j && math.Abs(d-2*lmin) > 1e-6 {
t.Fail()
}
}
}
}
for _, j := range []int{0, 1, 2, 4, 5, 6} {
if math.Abs(scale[j]-1) > 1e-6 {
t.Fail()
}
}
// Make sure it still works after copying out to
// static arrays.
if r == 1 {
da.Reset()
da = dstream.MemCopy(dstream.Dstream(da), false)
}
}
}
func TestKO2(t *testing.T) {
x1 := []interface{}{[]float64{3, 1, 5, 4, 2, 7, 9}, []float64{6, 2, 1, 3, 4, 8, 5, 2}}
x2 := []interface{}{[]float64{4, 1, 2, 3, 8, 6, 5}, []float64{6, 2, 3, 1, 5, 9, 3, 1}}
x3 := []interface{}{[]float64{2, 1, 5, 4, 9, 3, 2}, []float64{9, 3, 5, 2, 1, 7, 5, 2}}
x4 := []interface{}{[]float64{1, 0, 3, 2, 5, 2, 1}, []float64{3, 2, 1, 4, 2, 3, 4, 0}} // Don't knockoff this one
da := dstream.NewFromArrays(
[][]interface{}{x1, x2, x3, x4},
[]string{"x1", "x2", "x3", "x4"})
names := []string{"x1", "x2", "x3"}
ko, err := NewKnockoff(da, names)
if err != nil {
panic(err)
}
lmin := ko.lmin
da = dstream.Dstream(ko)
// Need to copy this out because the results are random.
da = dstream.MemCopy(da, true)
for r := 0; r < 3; r++ {
da.Reset()
if len(da.Names()) != 7 {
t.Fail()
}
if da.NumVar() != 7 {
t.Fail()
}
scale := make([]float64, 7)
for j := 0; j < 7; j++ {
da.Reset()
z := dstream.GetColPos(da, j).([]float64)
scale[j] = floats.Dot(z, z)
}
for i := 0; i < 3; i++ {
da.Reset()
a1 := dstream.GetColPos(da, i).([]float64)
da.Reset()
b1 := dstream.GetColPos(da, 4+i).([]float64)
for j := 0; j <= i; j++ {
da.Reset()
a2 := dstream.GetColPos(da, j).([]float64)
da.Reset()
b2 := dstream.GetColPos(da, 4+j).([]float64)
if math.Abs(floats.Dot(a1, a2)-floats.Dot(b1, b2)) > 1e-6 {
t.Fail()
}
d := math.Abs(floats.Dot(a1, b2) - floats.Dot(a1, a2))
if i != j && d > 1e-6 {
t.Fail()
}
if i == j && math.Abs(d-2*lmin) > 1e-6 {
t.Fail()
}
}
}
for _, j := range []int{0, 1, 2, 4, 5, 6} {
if math.Abs(scale[j]-1) > 1e-6 {
t.Fail()
}
}
}
}
|