mirror of
https://github.com/gonum/gonum.git
synced 2025-10-29 01:33:14 +08:00
bound: add Bound package and type (#845)
* bound: add Bound package and type Updates 833.
This commit is contained in:
10
bound/bound.go
Normal file
10
bound/bound.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
// Copyright ©2019 The Gonum Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package bound
|
||||||
|
|
||||||
|
// Bound represents [Min, Max] bounds.
|
||||||
|
type Bound struct {
|
||||||
|
Min, Max float64
|
||||||
|
}
|
||||||
6
bound/doc.go
Normal file
6
bound/doc.go
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
// Copyright ©2019 The Gonum Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package bound provides types for bounded data.
|
||||||
|
package bound // import "gonum.org/v1/gonum/bound"
|
||||||
@@ -7,6 +7,7 @@ package distmv
|
|||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
|
"gonum.org/v1/gonum/bound"
|
||||||
"gonum.org/v1/gonum/floats"
|
"gonum.org/v1/gonum/floats"
|
||||||
"gonum.org/v1/gonum/mat"
|
"gonum.org/v1/gonum/mat"
|
||||||
"gonum.org/v1/gonum/mathext"
|
"gonum.org/v1/gonum/mathext"
|
||||||
@@ -75,7 +76,7 @@ func (Bhattacharyya) DistUniform(l, r *Uniform) float64 {
|
|||||||
|
|
||||||
// unifLogVolOverlap computes the log of the volume of the hyper-rectangle where
|
// unifLogVolOverlap computes the log of the volume of the hyper-rectangle where
|
||||||
// both uniform distributions have positive probability.
|
// both uniform distributions have positive probability.
|
||||||
func unifLogVolOverlap(b1, b2 []Bound) float64 {
|
func unifLogVolOverlap(b1, b2 []bound.Bound) float64 {
|
||||||
var logVolOverlap float64
|
var logVolOverlap float64
|
||||||
for dim, v1 := range b1 {
|
for dim, v1 := range b1 {
|
||||||
v2 := b2[dim]
|
v2 := b2[dim]
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"golang.org/x/exp/rand"
|
"golang.org/x/exp/rand"
|
||||||
|
|
||||||
|
"gonum.org/v1/gonum/bound"
|
||||||
"gonum.org/v1/gonum/floats"
|
"gonum.org/v1/gonum/floats"
|
||||||
"gonum.org/v1/gonum/mat"
|
"gonum.org/v1/gonum/mat"
|
||||||
)
|
)
|
||||||
@@ -61,14 +62,14 @@ func TestBhattacharyyaUniform(t *testing.T) {
|
|||||||
tol float64
|
tol float64
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
a: NewUniform([]Bound{{-3, 2}, {-5, 8}}, rnd),
|
a: NewUniform([]bound.Bound{{-3, 2}, {-5, 8}}, rnd),
|
||||||
b: NewUniform([]Bound{{-4, 1}, {-7, 10}}, rnd),
|
b: NewUniform([]bound.Bound{{-4, 1}, {-7, 10}}, rnd),
|
||||||
samples: 100000,
|
samples: 100000,
|
||||||
tol: 1e-2,
|
tol: 1e-2,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
a: NewUniform([]Bound{{-3, 2}, {-5, 8}}, rnd),
|
a: NewUniform([]bound.Bound{{-3, 2}, {-5, 8}}, rnd),
|
||||||
b: NewUniform([]Bound{{-5, -4}, {-7, 10}}, rnd),
|
b: NewUniform([]bound.Bound{{-5, -4}, {-7, 10}}, rnd),
|
||||||
samples: 100000,
|
samples: 100000,
|
||||||
tol: 1e-2,
|
tol: 1e-2,
|
||||||
},
|
},
|
||||||
@@ -256,14 +257,14 @@ func TestKullbackLeiblerUniform(t *testing.T) {
|
|||||||
tol float64
|
tol float64
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
a: NewUniform([]Bound{{-5, 2}, {-7, 12}}, rnd),
|
a: NewUniform([]bound.Bound{{-5, 2}, {-7, 12}}, rnd),
|
||||||
b: NewUniform([]Bound{{-4, 1}, {-7, 10}}, rnd),
|
b: NewUniform([]bound.Bound{{-4, 1}, {-7, 10}}, rnd),
|
||||||
samples: 100000,
|
samples: 100000,
|
||||||
tol: 1e-2,
|
tol: 1e-2,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
a: NewUniform([]Bound{{-5, 2}, {-7, 12}}, rnd),
|
a: NewUniform([]bound.Bound{{-5, 2}, {-7, 12}}, rnd),
|
||||||
b: NewUniform([]Bound{{-9, -6}, {-7, 10}}, rnd),
|
b: NewUniform([]bound.Bound{{-9, -6}, {-7, 10}}, rnd),
|
||||||
samples: 100000,
|
samples: 100000,
|
||||||
tol: 1e-2,
|
tol: 1e-2,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -8,23 +8,18 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
|
|
||||||
"golang.org/x/exp/rand"
|
"golang.org/x/exp/rand"
|
||||||
|
"gonum.org/v1/gonum/bound"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Bound represents [Min, Max] bounds.
|
|
||||||
type Bound struct {
|
|
||||||
Min float64
|
|
||||||
Max float64
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uniform represents a multivariate uniform distribution.
|
// Uniform represents a multivariate uniform distribution.
|
||||||
type Uniform struct {
|
type Uniform struct {
|
||||||
bounds []Bound
|
bounds []bound.Bound
|
||||||
dim int
|
dim int
|
||||||
rnd *rand.Rand
|
rnd *rand.Rand
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUniform creates a new uniform distribution with the given bounds.
|
// NewUniform creates a new uniform distribution with the given bounds.
|
||||||
func NewUniform(bnds []Bound, src rand.Source) *Uniform {
|
func NewUniform(bnds []bound.Bound, src rand.Source) *Uniform {
|
||||||
dim := len(bnds)
|
dim := len(bnds)
|
||||||
if dim == 0 {
|
if dim == 0 {
|
||||||
panic(badZeroDimension)
|
panic(badZeroDimension)
|
||||||
@@ -35,7 +30,7 @@ func NewUniform(bnds []Bound, src rand.Source) *Uniform {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
u := &Uniform{
|
u := &Uniform{
|
||||||
bounds: make([]Bound, dim),
|
bounds: make([]bound.Bound, dim),
|
||||||
dim: dim,
|
dim: dim,
|
||||||
}
|
}
|
||||||
if src != nil {
|
if src != nil {
|
||||||
@@ -55,7 +50,7 @@ func NewUnitUniform(dim int, src rand.Source) *Uniform {
|
|||||||
if dim <= 0 {
|
if dim <= 0 {
|
||||||
panic(nonPosDimension)
|
panic(nonPosDimension)
|
||||||
}
|
}
|
||||||
bounds := make([]Bound, dim)
|
bounds := make([]bound.Bound, dim)
|
||||||
for i := range bounds {
|
for i := range bounds {
|
||||||
bounds[i].Min = 0
|
bounds[i].Min = 0
|
||||||
bounds[i].Max = 1
|
bounds[i].Max = 1
|
||||||
@@ -74,9 +69,9 @@ func NewUnitUniform(dim int, src rand.Source) *Uniform {
|
|||||||
// is nil, a new slice is allocated and returned. If the input is non-nil, then
|
// is nil, a new slice is allocated and returned. If the input is non-nil, then
|
||||||
// the bounds are stored in-place into the input argument, and Bounds will panic
|
// the bounds are stored in-place into the input argument, and Bounds will panic
|
||||||
// if len(bounds) != u.Dim().
|
// if len(bounds) != u.Dim().
|
||||||
func (u *Uniform) Bounds(bounds []Bound) []Bound {
|
func (u *Uniform) Bounds(bounds []bound.Bound) []bound.Bound {
|
||||||
if bounds == nil {
|
if bounds == nil {
|
||||||
bounds = make([]Bound, u.Dim())
|
bounds = make([]bound.Bound, u.Dim())
|
||||||
}
|
}
|
||||||
if len(bounds) != u.Dim() {
|
if len(bounds) != u.Dim() {
|
||||||
panic(badInputLength)
|
panic(badInputLength)
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ package distmv
|
|||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gonum.org/v1/gonum/bound"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUniformEntropy(t *testing.T) {
|
func TestUniformEntropy(t *testing.T) {
|
||||||
@@ -15,11 +17,11 @@ func TestUniformEntropy(t *testing.T) {
|
|||||||
Entropy float64
|
Entropy float64
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
NewUniform([]Bound{{0, 1}, {0, 1}}, nil),
|
NewUniform([]bound.Bound{{0, 1}, {0, 1}}, nil),
|
||||||
0,
|
0,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
NewUniform([]Bound{{-1, 3}, {2, 8}, {-5, -3}}, nil),
|
NewUniform([]bound.Bound{{-1, 3}, {2, 8}, {-5, -3}}, nil),
|
||||||
math.Log(48),
|
math.Log(48),
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
"golang.org/x/exp/rand"
|
"golang.org/x/exp/rand"
|
||||||
|
|
||||||
|
"gonum.org/v1/gonum/bound"
|
||||||
"gonum.org/v1/gonum/floats"
|
"gonum.org/v1/gonum/floats"
|
||||||
"gonum.org/v1/gonum/mat"
|
"gonum.org/v1/gonum/mat"
|
||||||
"gonum.org/v1/gonum/stat"
|
"gonum.org/v1/gonum/stat"
|
||||||
@@ -26,8 +27,8 @@ func TestLatinHypercube(t *testing.T) {
|
|||||||
src := rand.New(rand.NewSource(1))
|
src := rand.New(rand.NewSource(1))
|
||||||
for _, nSamples := range []int{1, 2, 5, 10, 20} {
|
for _, nSamples := range []int{1, 2, 5, 10, 20} {
|
||||||
for _, dist := range []lhDist{
|
for _, dist := range []lhDist{
|
||||||
distmv.NewUniform([]distmv.Bound{{Min: 0, Max: 3}}, src),
|
distmv.NewUniform([]bound.Bound{{Min: 0, Max: 3}}, src),
|
||||||
distmv.NewUniform([]distmv.Bound{{Min: 0, Max: 3}, {Min: -1, Max: 5}, {Min: -4, Max: -1}}, src),
|
distmv.NewUniform([]bound.Bound{{Min: 0, Max: 3}, {Min: -1, Max: 5}, {Min: -4, Max: -1}}, src),
|
||||||
} {
|
} {
|
||||||
dim := dist.Dim()
|
dim := dist.Dim()
|
||||||
batch := mat.NewDense(nSamples, dim, nil)
|
batch := mat.NewDense(nSamples, dim, nil)
|
||||||
@@ -92,7 +93,7 @@ func TestRejection(t *testing.T) {
|
|||||||
src := rand.New(rand.NewSource(1))
|
src := rand.New(rand.NewSource(1))
|
||||||
// Test by finding the expected value of a uniform.
|
// Test by finding the expected value of a uniform.
|
||||||
dim := 3
|
dim := 3
|
||||||
bounds := make([]distmv.Bound, dim)
|
bounds := make([]bound.Bound, dim)
|
||||||
for i := 0; i < dim; i++ {
|
for i := 0; i < dim; i++ {
|
||||||
min := src.NormFloat64()
|
min := src.NormFloat64()
|
||||||
max := src.NormFloat64()
|
max := src.NormFloat64()
|
||||||
|
|||||||
Reference in New Issue
Block a user