-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
181 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# numint | ||
One-dimensional numerical quadrature | ||
|
||
## Description | ||
|
||
Numint is a package for one-dimensional numerical quadrature. | ||
It provides several Newton-Cotes and Gauss Legendre quadrature rules and an algorithm for successive approximations. | ||
|
||
Numint was developed for use with the [mab](http://github.com/stitchfix/mab) Thompson sampling multi-armed bandit strategy, | ||
but works as a standalone library for numerical integration. | ||
|
||
Numint can be extended by implementing the `Rule` and/or `Subdivider` interfaces. | ||
## Installation | ||
|
||
```go | ||
go get -u github.com/stitchfix/mab/numint | ||
``` | ||
|
||
## Usage | ||
|
||
```go | ||
package main | ||
import ( | ||
"fmt" | ||
|
||
"github.com/stitchfix/mab/numint" | ||
) | ||
|
||
func main() { | ||
q := numint.NewQuadrature(numint.WithAbsTol(1E-6)) | ||
res, _ := q.Integrate(math.Cos, 0, 1) | ||
fmt.Println(res) | ||
} | ||
``` | ||
|
||
## Documentation | ||
|
||
More detailed refence docs can be found on [pkg.go.dev](https://pkg.go.dev/github.com/stitchfix/mab/numint) | ||
|
||
## License | ||
|
||
Mab and Numint are licensed under the Apache 2.0 license. See the LICENSE file for terms and conditions for use, reproduction, and | ||
distribution. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package numint_test | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
|
||
"github.com/stitchfix/mab" | ||
"github.com/stitchfix/mab/numint" | ||
) | ||
|
||
func TestQuadrature_Integrate(t *testing.T) { | ||
tests := []struct { | ||
f func(float64) float64 | ||
a, b, expected float64 | ||
}{ | ||
{ | ||
func(x float64) float64 { return x }, | ||
0, 0, | ||
0, | ||
}, | ||
{ | ||
func(x float64) float64 { return x }, | ||
0, 1, | ||
0.5, | ||
}, | ||
{ | ||
func(x float64) float64 { return 1.0 / (1 + x*x) }, | ||
0, 1, | ||
0.785398, | ||
}, | ||
{ | ||
mab.Beta(10, 20).Prob, | ||
0, 1, | ||
1, | ||
}, | ||
{ | ||
mab.Normal(10, 20).Prob, | ||
-700, 900, | ||
1, | ||
}, | ||
{ | ||
math.Asinh, | ||
-.5, 1, | ||
0.344588, | ||
}, | ||
{ | ||
func(x float64) float64 { return x * math.Cos(x*x) }, | ||
1, 5, | ||
-0.486911, | ||
}, | ||
} | ||
tol := 1E-6 | ||
q := numint.NewQuadrature(numint.WithAbsTol(tol)) | ||
|
||
for _, test := range tests { | ||
t.Run("", func(t *testing.T) { | ||
actual, err := q.Integrate(test.f, test.a, test.b) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !closeEnough(test.expected, actual, tol) { | ||
t.Errorf("actual not %f, got=%f", test.expected, actual) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func closeEnough(a, b, tol float64) bool { | ||
return math.Abs(a-b) < tol | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters