Skip to content

Commit

Permalink
Merge pull request #150 from mrandri19/master
Browse files Browse the repository at this point in the history
Use `@example` tags in documentation's examples
  • Loading branch information
ludoro committed Jun 13, 2020
2 parents 398a516 + e626df8 commit 630daa6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ You can obtain the current master with:
```

# Quick example
```julia
```@example
using Surrogates
num_samples = 10
lb = 0.0
Expand Down
30 changes: 13 additions & 17 deletions docs/src/tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,28 @@
Let's start with something easy to get our hands dirty.
I want to build a surrogate for ``f(x) = log(x)*x^2+x^3``.
Let's choose the radial basis surrogate.
```
```@example
using Surrogates
f = x -> log(x)*x^2+x^3
lb = 1.0
ub = 10.0
x = sample(50,lb,ub,SobolSample())
y = f.(x)
thin_plate_spline = x -> x^2*log(x)
q = 2
my_radial_basis = RadialBasis(x,y,lb,ub,thin_plate_spline,q)
my_radial_basis = RadialBasis(x,y,lb,ub,rad=thinplateRadial)
#I want an approximation at 5.4
approx = my_radial_basis(5.4)
```
Let's now see an example in 2D.
```
```@example
using Surrogates
using LinearAlgebra
f = x -> x[1]*x[2]
lb = [1.0,2.0]
ub = [10.0,8.5]
x = sample(50,lb,ub,SobolSample())
y = f.(x)
linear = x -> norm(x)
q = 1
my_radial_basis = RadialBasis(x,y,[lb,ub],linear,q)
my_radial_basis = RadialBasis(x,y,lb,ub)
#I want an approximation at (1.0,1.4)
approx = my_radial_basis((1.0,1.4))
Expand All @@ -38,15 +34,15 @@ Let's now use the Kriging surrogate, which is a single-output Gaussian process.
This surrogate has a nice feature: not only does it approximate the solution at a
point, it also calculates the standard error at such point.
Let's see an example:
```
```@example kriging
using Surrogates
f = x -> exp(x)*x^2+x^3
lb = 0.0
ub = 10.0
x = sample(100,lb,ub,UniformSample())
y = f.(x)
p = 1.9
my_krig = Kriging(x,y,p)
my_krig = Kriging(x,y,lb,ub,p=p)
#I want an approximation at 5.4
approx = my_krig(5.4)
Expand All @@ -56,14 +52,14 @@ std_err = std_error_at_point(my_krig,5.4)
```

Let's now optimize the Kriging surrogate using Lower confidence bound method, this is just a one-liner:
```
surrogate_optimize(f,LCBS(),a,b,my_krig,UniformSample())
```@example kriging
surrogate_optimize(f,LCBS(),lb,ub,my_krig,UniformSample())
```
## Lobachesky integral
The Lobachesky surrogate has the nice feature of having a closed formula for its
integral, which is something that other surrogates are missing.
Let's compare it with QuadGK:
```
```@examples
using Surrogates
using QuadGK
obj = x -> 3*x + log(x)
Expand All @@ -73,19 +69,19 @@ x = sample(2000,a,b,SobolSample())
y = obj.(x)
alpha = 2.0
n = 6
my_loba = LobacheskySurrogate(x,y,alpha,n,a,b)
my_loba = LobacheskySurrogate(x,y,a,b,alpha=alpha,n=n)
#1D integral
int_1D = lobachesky_integral(my_loba,a,b)
int = quadgk(obj,a,b)
int_val_true = int[1]-int[2]
@test abs(int_1D - int_val_true) < 10^-5
@assert int_1D int_val_true
```


## Example of NeuralSurrogate
Basic example of fitting a neural network on a simple function of two variables.
```
```@example
using Surrogates
using Flux
using Statistics
Expand All @@ -106,7 +102,7 @@ loss(x, y) = Flux.mse(model(x), y)
learning_rate = 0.1
optimizer = Descent(learning_rate) # Simple gradient descent. See Flux documentation for other options.
n_epochs = 50
sgt = NeuralSurrogate(x_train, y_train, bounds..., model, loss, optimizer, n_epochs)
sgt = NeuralSurrogate(x_train, y_train, bounds..., model=model, loss=loss, opt=optimizer, n_echos=n_epochs)
# Testing the new model
x_test = sample(30, bounds..., SobolSample())
Expand Down
5 changes: 4 additions & 1 deletion src/Radials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ end
linearRadial = RadialFunction(0,z->norm(z))
cubicRadial = RadialFunction(1,z->norm(z)^3)
multiquadricRadial = RadialFunction(1,z->sqrt(norm(z)^2+1))
thinplateRadial = RadialFunction(2,z->norm(z)^2*log(norm(z)))
thinplateRadial = RadialFunction(2, z->begin
result = norm(z)^2 * log(norm(z))
ifelse(iszero(z), zero(result), result)
end)

"""
RadialBasis(x,y,lb::Number,ub::Number; rad::RadialFunction = linearRadial,scale::Real=1.0)
Expand Down

0 comments on commit 630daa6

Please sign in to comment.