6fca822a |
Tutorial `~adaptive.AverageLearner`
-----------------------------------
.. note::
Because this documentation consists of static html, the ``live_plot``
and ``live_info`` widget is not live. Download the notebook
in order to see the real behaviour.
.. seealso::
The complete source code of this tutorial can be found in
|
d9aaa498 |
:jupyter-download:notebook:`tutorial.AverageLearner`
|
6fca822a |
|
d9aaa498 |
.. jupyter-execute::
|
6fca822a |
:hide-code:
import adaptive
adaptive.notebook_extension()
The next type of learner averages a function until the uncertainty in
the average meets some condition.
This is useful for sampling a random variable. The function passed to
the learner must formally take a single parameter, which should be used
like a “seed” for the (pseudo-) random variable (although in the current
implementation the seed parameter can be ignored by the function).
|
d9aaa498 |
.. jupyter-execute::
|
6fca822a |
def g(n):
import random
from time import sleep
sleep(random.random() / 1000)
# Properly save and restore the RNG state
state = random.getstate()
random.seed(n)
val = random.gauss(0.5, 1)
random.setstate(state)
return val
|
d9aaa498 |
.. jupyter-execute::
|
6fca822a |
learner = adaptive.AverageLearner(g, atol=None, rtol=0.01)
|
a9f6349c |
# `loss < 1` means that we reached the `rtol` or `atol`
runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 1)
|
6fca822a |
|
d9aaa498 |
.. jupyter-execute::
|
6fca822a |
:hide-code:
await runner.task # This is not needed in a notebook environment!
|
d9aaa498 |
.. jupyter-execute::
|
6fca822a |
runner.live_info()
|
d9aaa498 |
.. jupyter-execute::
|
6fca822a |
runner.live_plot(update_interval=0.1)
|