mirror of
https://github.com/gonum/gonum.git
synced 2025-10-06 23:52:47 +08:00
stat/spatial: use mat.RowNonZeroDoer
This commit is contained in:
@@ -13,7 +13,7 @@ import (
|
||||
"gonum.org/v1/gonum/mat"
|
||||
)
|
||||
|
||||
func simpleAdjacency(n, wide int, diag bool) *mat.Dense {
|
||||
func simpleAdjacency(n, wide int, diag bool) mat.Matrix {
|
||||
m := mat.NewDense(n, n, nil)
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 1; j <= wide; j++ {
|
||||
@@ -30,11 +30,28 @@ func simpleAdjacency(n, wide int, diag bool) *mat.Dense {
|
||||
return m
|
||||
}
|
||||
|
||||
func simpleAdjacencyBand(n, wide int, diag bool) mat.Matrix {
|
||||
m := mat.NewBandDense(n, n, wide, wide, nil)
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 1; j <= wide; j++ {
|
||||
if j > i {
|
||||
continue
|
||||
}
|
||||
m.SetBand(i-j, i, 1)
|
||||
m.SetBand(i, i-j, 1)
|
||||
}
|
||||
if diag {
|
||||
m.SetBand(i, i, 1)
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
var spatialTests = []struct {
|
||||
from, to float64
|
||||
n, wide int
|
||||
fn func(float64, int, *rand.Rand) float64
|
||||
locality func(n, wide int, diag bool) *mat.Dense
|
||||
locality func(n, wide int, diag bool) mat.Matrix
|
||||
|
||||
// Values for MoranI and z-score are obtained from
|
||||
// an R reference implementation.
|
||||
@@ -46,6 +63,7 @@ var spatialTests = []struct {
|
||||
// of the plotted data.
|
||||
wantSegs int
|
||||
}{
|
||||
// Dense matrix locality.
|
||||
{
|
||||
from: 0, to: 1, n: 1000, wide: 1,
|
||||
fn: func(_ float64, _ int, rnd *rand.Rand) float64 {
|
||||
@@ -122,6 +140,84 @@ var spatialTests = []struct {
|
||||
wantZ: -31.559531064275987,
|
||||
wantSegs: 0,
|
||||
},
|
||||
|
||||
// Band matrix locality.
|
||||
{
|
||||
from: 0, to: 1, n: 1000, wide: 1,
|
||||
fn: func(_ float64, _ int, rnd *rand.Rand) float64 {
|
||||
return rnd.Float64()
|
||||
},
|
||||
locality: simpleAdjacencyBand,
|
||||
|
||||
wantMoranI: -0.0019631298955953233,
|
||||
wantZ: -0.03039477405151108,
|
||||
wantSegs: 0,
|
||||
},
|
||||
{
|
||||
from: -math.Pi / 2, to: 3 * math.Pi / 2, n: 1000, wide: 1,
|
||||
fn: func(x float64, _ int, _ *rand.Rand) float64 {
|
||||
y := math.Sin(x)
|
||||
if math.Abs(y) > 0.5 {
|
||||
y *= 1/math.Abs(y) - 1
|
||||
}
|
||||
return y * math.Sin(x*2)
|
||||
},
|
||||
locality: simpleAdjacencyBand,
|
||||
|
||||
wantMoranI: 1.0008149537991464,
|
||||
wantZ: 31.648547078779092,
|
||||
wantSegs: 4,
|
||||
},
|
||||
{
|
||||
from: 0, to: 1, n: 1000, wide: 1,
|
||||
fn: func(_ float64, _ int, rnd *rand.Rand) float64 {
|
||||
return rnd.NormFloat64()
|
||||
},
|
||||
locality: simpleAdjacencyBand,
|
||||
|
||||
wantMoranI: 0.031195199553564902,
|
||||
wantZ: 1.0171161514080056,
|
||||
wantSegs: 0,
|
||||
},
|
||||
{
|
||||
from: 0, to: 1, n: 1000, wide: 1,
|
||||
fn: func(x float64, _ int, rnd *rand.Rand) float64 {
|
||||
if rnd.Float64() < 0.5 {
|
||||
return rnd.NormFloat64() + 5
|
||||
}
|
||||
return rnd.NormFloat64()
|
||||
},
|
||||
locality: simpleAdjacencyBand,
|
||||
|
||||
wantMoranI: -0.016245135637562223,
|
||||
wantZ: -0.48157993864993476,
|
||||
wantSegs: 0,
|
||||
},
|
||||
{
|
||||
from: 0, to: 1, n: 1000, wide: 1,
|
||||
fn: func(x float64, i int, rnd *rand.Rand) float64 {
|
||||
if i%2 == 0 {
|
||||
return rnd.NormFloat64() + 5
|
||||
}
|
||||
return rnd.NormFloat64()
|
||||
},
|
||||
locality: simpleAdjacencyBand,
|
||||
|
||||
wantMoranI: -0.8565268969272998,
|
||||
wantZ: -27.027057520918113,
|
||||
wantSegs: 0,
|
||||
},
|
||||
{
|
||||
from: 0, to: 1, n: 1000, wide: 1,
|
||||
fn: func(_ float64, i int, _ *rand.Rand) float64 {
|
||||
return float64(i % 2)
|
||||
},
|
||||
locality: simpleAdjacencyBand,
|
||||
|
||||
wantMoranI: -1,
|
||||
wantZ: -31.559531064275987,
|
||||
wantSegs: 0,
|
||||
},
|
||||
}
|
||||
|
||||
func TestGetisOrd(t *testing.T) {
|
||||
|
Reference in New Issue
Block a user