Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: Configure JupyterLite's TryExamples directive for UNU-RNS tutorial #5

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions doc/source/dev/toolchain.rst
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,18 @@ asv (airspeed velocity) Recent https://asv.readthedocs.io/
Building the Documentation
--------------------------

==================== =================================================
Tool Version
==================== =================================================
Sphinx Whatever recent versions work. >= 5.0.
PyData Sphinx theme Whatever recent versions work. >= 0.15.2.
Sphinx-Design Whatever recent versions work. >= 0.4.0.
numpydoc Whatever recent versions work. >= 1.5.0.
matplotlib Generally suggest >= 3.5.
MyST-NB Whatever recent versions work. >= 0.17.1
jupyterlite-sphinx Whatever recent versions work. >= 0.12.0
==================== =================================================
============================ =================================================
Tool Version
============================ =================================================
melissawm marked this conversation as resolved.
Show resolved Hide resolved
Sphinx Whatever recent versions work. >= 5.0.
PyData Sphinx theme Whatever recent versions work. >= 0.15.2.
Sphinx-Design Whatever recent versions work. >= 0.4.0.
numpydoc Whatever recent versions work. >= 1.5.0.
matplotlib Generally suggest >= 3.5.
MyST-NB Whatever recent versions work. >= 0.17.1
jupyterlite-sphinx Whatever recent versions work. >= 0.13.1
jupyterlite-pyodide-kernel Whatever recent versions work. >= 0.1.0
============================ =================================================

.. note::

Expand Down
3 changes: 1 addition & 2 deletions doc/source/try_examples.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"min_height": "400px",
"ignore_patterns": [".*"]
"min_height": "400px"
}
29 changes: 19 additions & 10 deletions doc/source/tutorial/stats/sampling.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,21 @@ samples from the given distribution.

An example of this interface is shown below:

```python
```{eval-rst}
.. try_examples::

>>> from scipy.stats.sampling import TransformedDensityRejection
>>> from math import exp
>>>
...
>>> class StandardNormal:
... def pdf(self, x: float) -> float:
... # note that the normalization constant isn't required
... return exp(-0.5 * x*x)
... def dpdf(self, x: float) -> float:
... return -x * exp(-0.5 * x*x)
...
...
>>> dist = StandardNormal()
>>>
...
>>> urng = np.random.default_rng()
>>> rng = TransformedDensityRejection(dist, random_state=urng)
```
Expand Down Expand Up @@ -188,7 +190,11 @@ In the above example, we have set up an object of the
standard normal distribution. Now, we can start sampling from our
distribution by calling the `rvs` method:

```python
```{eval-rst}
.. try_examples::

>>> from scipy.stats.sampling import TransformedDensityRejection
>>> rng = TransformedDensityRejection(dist, domain=(-1, 1), random_state=urng)
Comment on lines +196 to +197
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these import statements will be required in these code snippets to make sure that the button works and the examples run without issues for users. I'm not sure if there is a better or cleaner way to do this, because it will create repeated imports (though running them won't be a problem because imports are cached)

>>> rng.rvs()
-1.526829048388144
>>> rng.rvs((5, 3))
Expand All @@ -213,15 +219,15 @@ mystnb:
>>> from scipy.stats import norm
>>> from scipy.stats.sampling import TransformedDensityRejection
>>> from math import exp
>>>
>>>
>>> class StandardNormal:
... def pdf(self, x: float) -> float:
... # note that the normalization constant isn't required
... return exp(-0.5 * x*x)
... def dpdf(self, x: float) -> float:
... return -x * exp(-0.5 * x*x)
...
>>>
...
>>>
>>> dist = StandardNormal()
>>> urng = np.random.default_rng()
>>> rng = TransformedDensityRejection(dist, random_state=urng)
Expand Down Expand Up @@ -258,7 +264,7 @@ mystnb:
{class}`~.stats.sampling.TransformedDensityRejection` would not be the same
even for the same `random_state`:

```python
```{eval-rst}
>>> from scipy.stats.sampling import norm, TransformedDensityRejection
>>> from copy import copy
>>> dist = StandardNormal()
Expand All @@ -274,7 +280,10 @@ mystnb:

We can pass a `domain` parameter to truncate the distribution:

```python
```{eval-rst}
.. try_examples::

>>> from scipy.stats.sampling import TransformedDensityRejection
>>> rng = TransformedDensityRejection(dist, domain=(-1, 1), random_state=urng)
>>> rng.rvs((5, 3))
array([[-0.99865691, 0.38104014, 0.31633526],
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dependencies:
- jupytext
- myst-nb
- jupyterlite-sphinx>=0.13.1
- jupyterlite-pyodide-kernel
# Some optional test dependencies
- mpmath
- gmpy2
Expand Down