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

[DEP][DOC}: 696 fix random seed documentation #25

Closed
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
12 changes: 7 additions & 5 deletions bilby/core/sampler/dynesty_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,9 @@ def propose_differetial_evolution(
The number of dimensions to run the differential evolution over, the
first :code:`n_cluster` dimensions are used. The rest are randomly
sampled from the prior.
rstate: np.random.RandomState
The random state to use to generate random numbers.
rstate: numpy.random.Generator
The numpy generator instance used for random number generation.
Consider using built in `bilby.core.utils.random.rng`.
bretsnev marked this conversation as resolved.
Show resolved Hide resolved
mix: float
The fraction of proposed points that should follow the specified scale
rather than mode hopping. :code:`default=0.5`
Expand Down Expand Up @@ -656,9 +657,10 @@ def propose_volumetric(
The number of dimensions to run the differential evolution over, the
first :code:`n_cluster` dimensions are used. The rest are randomly
sampled from the prior.
rstate: np.random.RandomState
The random state to use to generate random numbers.

rstate: numpy.random.Generator
The numpy generator instance used for random number generation.
Consider using built in `bilby.core.utils.random.rng`.

Returns
-------
u_prop: np.ndarray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import bilby
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.utils.random import seed, rng
bretsnev marked this conversation as resolved.
Show resolved Hide resolved

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression_bilby_mcmc"
Expand All @@ -31,8 +35,8 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
sigma = np.random.normal(1, 0.01, N)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
sigma = rng.normal(1, 0.01, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.likelihood import GaussianLikelihood
from bilby.core.utils.random import seed, rng

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression_pymc"
Expand All @@ -33,7 +37,7 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import matplotlib.pyplot as plt
import numpy as np
import pymc as pm
from bilby.core.utils.random import seed, rng

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression_pymc_custom_likelihood"
Expand All @@ -36,7 +40,7 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
8 changes: 5 additions & 3 deletions examples/core_examples/gaussian_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"""
import bilby
import numpy as np
from bilby.core.utils.random import seed, rng

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "gaussian_example"
Expand All @@ -16,9 +20,7 @@
# waveform_generator to make the signal.

# Making simulated data: in this case, we consider just a Gaussian

data = np.random.normal(3, 4, 100)

data = rng.normal(3, 4, 100)

class SimpleGaussianLikelihood(bilby.Likelihood):
def __init__(self, data):
Expand Down
11 changes: 7 additions & 4 deletions examples/core_examples/gaussian_process_celerite_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.prior import Uniform
from bilby.core.utils.random import seed, rng

#sets seed of bilby's generator "rng" to "123"
seed(123)

# In this example we show how we can use the `celerite` package within `bilby`.
# We begin by synthesizing some data and then use a simple Gaussian Process
Expand Down Expand Up @@ -44,9 +48,8 @@ def linear_function(x, a, b):

# For the data creation, we leave a gap in the middle of the time series
# to see how the Gaussian Process model can interpolate the data.
# We fix the seed to ensure reproducibility.
# The seed has been fixed to ensure reproducibility (see line 11)

np.random.seed(42)
times = np.linspace(0, 40, 100)
times = np.append(times, np.linspace(60, 100, 100))
dt = times[1] - times[0]
Expand All @@ -56,7 +59,7 @@ def linear_function(x, a, b):
amplitude
* np.sin(2 * np.pi * times / period)
* np.exp(-((times - 50) ** 2) / 2 / width**2)
+ np.random.normal(scale=jitter, size=len(times))
+ rng.normal(scale=jitter, size=len(times))
+ linear_function(x=times, a=slope, b=offset)
)

Expand Down Expand Up @@ -184,7 +187,7 @@ def linear_function(x, a, b):

# Plot the mean model for ten other posterior samples.
samples = [
result.posterior.iloc[np.random.randint(len(result.posterior))] for _ in range(10)
result.posterior.iloc[rng.integers(len(result.posterior))] for _ in range(10)
]
for sample in samples:
likelihood.set_parameters(sample)
Expand Down
11 changes: 7 additions & 4 deletions examples/core_examples/gaussian_process_george_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.prior import Uniform
from bilby.core.utils.random import seed, rng

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# In this example we show how we can use the `george` package within
# `bilby`. We begin by synthesizing some data and then use a simple Gaussian
Expand Down Expand Up @@ -42,9 +46,8 @@ def linear_function(x, a, b):

# For the data creation, we leave a gap in the middle of the time series to
# see how the Gaussian Process model can interpolate the data. We fix the
# seed to ensure reproducibility.

np.random.seed(42)

times = np.linspace(0, 40, 100)
times = np.append(times, np.linspace(60, 100, 100))
dt = times[1] - times[0]
Expand All @@ -54,7 +57,7 @@ def linear_function(x, a, b):
amplitude
* np.sin(2 * np.pi * times / period)
* np.exp(-((times - 50) ** 2) / 2 / width**2)
+ np.random.normal(scale=jitter, size=len(times))
+ rng.normal(scale=jitter, size=len(times))
+ linear_function(x=times, a=slope, b=offset)
)

Expand Down Expand Up @@ -163,7 +166,7 @@ def linear_function(x, a, b):

# Plot the mean model for ten other posterior samples.
samples = [
result.posterior.iloc[np.random.randint(len(result.posterior))] for _ in range(10)
result.posterior.iloc[rng.integer(len(result.posterior))] for _ in range(10)
]
for sample in samples:
likelihood.set_parameters(sample)
Expand Down
8 changes: 6 additions & 2 deletions examples/core_examples/grid_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import bilby
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.utils.random import seed, rng

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression_grid"
Expand All @@ -31,8 +35,8 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
sigma = np.random.normal(1, 0.01, N)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
sigma = rng.normal(1, 0.01, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
10 changes: 7 additions & 3 deletions examples/core_examples/hyper_parameter_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
from bilby.core.sampler import run_sampler
from bilby.core.utils import check_directory_exists_and_if_not_mkdir
from bilby.hyper.likelihood import HyperparameterLikelihood
from bilby.core.utils.random import seed, rng

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

outdir = "outdir"
check_directory_exists_and_if_not_mkdir(outdir)
Expand All @@ -35,11 +39,11 @@ def model(x, c0, c1):
# Make the sample sets
results = list()
for i in range(Nevents):
c0 = np.random.normal(true_mu_c0, true_sigma_c0)
c1 = np.random.uniform(-1, 1)
c0 = rng.normal(true_mu_c0, true_sigma_c0)
c1 = rng.uniform(-1, 1)
injection_parameters = dict(c0=c0, c1=c1)

data = model(x, **injection_parameters) + np.random.normal(0, sigma, N)
data = model(x, **injection_parameters) + rng.normal(0, sigma, N)
line = ax1.plot(x, data, "-x", label=labels[i])

likelihood = GaussianLikelihood(x, data, model, sigma)
Expand Down
8 changes: 6 additions & 2 deletions examples/core_examples/linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import bilby
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.utils.random import seed, rng

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression"
Expand All @@ -31,8 +35,8 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
sigma = np.random.normal(1, 0.01, N)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
sigma = rng.normal(1, 0.01, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
6 changes: 5 additions & 1 deletion examples/core_examples/linear_regression_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import bilby
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.utils.random import seed, rng

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression_grid"
Expand All @@ -34,7 +38,7 @@ def model(time, m, c):
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
sigma = 3.0
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
6 changes: 5 additions & 1 deletion examples/core_examples/linear_regression_unknown_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import bilby
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.utils.random import seed, rng

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "linear_regression_unknown_noise"
Expand All @@ -32,7 +36,7 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# We quickly plot the data to check it looks sensible
fig, ax = plt.subplots()
Expand Down
13 changes: 10 additions & 3 deletions examples/core_examples/linear_regression_with_Fisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@
import copy

import bilby
from bilby.core.utils.random import seed, rng
#sets seed of bilby's generator "rng" to "123"
seed(123)

import numpy as np




# A few simple setup steps
outdir = "outdir"

np.random.seed(123)



# First, we define our "signal model", in this case a simple linear function
Expand All @@ -33,8 +40,8 @@ def model(time, m, c):
time_duration = 10
time = np.arange(0, time_duration, 1 / sampling_frequency)
N = len(time)
sigma = np.random.normal(1, 0.01, N)
data = model(time, **injection_parameters) + np.random.normal(0, sigma, N)
sigma = rng.normal(1, 0.01, N)
data = model(time, **injection_parameters) + rng.normal(0, sigma, N)

# Now lets instantiate a version of our GaussianLikelihood, giving it
# the time, data and signal model
Expand Down
6 changes: 5 additions & 1 deletion examples/core_examples/occam_factor_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
import bilby
import matplotlib.pyplot as plt
import numpy as np
from bilby.core.utils.random import seed, rng

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "occam_factor"
Expand All @@ -44,7 +48,7 @@
N = 100
time = np.linspace(0, 1, N)
coeffs = [1, 2, 3]
data = np.polyval(coeffs, time) + np.random.normal(0, sigma, N)
data = np.polyval(coeffs, time) + rng.normal(0, sigma, N)

fig, ax = plt.subplots()
ax.plot(time, data, "o", label="data", color="C0")
Expand Down
6 changes: 5 additions & 1 deletion examples/core_examples/radioactive_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import numpy as np
from bilby.core.likelihood import PoissonLikelihood
from bilby.core.prior import LogUniform
from bilby.core.utils.random import seed, rng

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# A few simple setup steps
label = "radioactive_decay"
Expand Down Expand Up @@ -61,7 +65,7 @@ def decay_rate(delta_t, halflife, n_init):

rates = decay_rate(delta_t, **injection_parameters)
# get radioactive counts
counts = np.random.poisson(rates)
counts = rng.poisson(rates)
theoretical = decay_rate(delta_t, **injection_parameters)

# We quickly plot the data to check it looks sensible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import bilby
import gwinc
import numpy as np
from bilby.core.utils.random import seed

# Sets seed of bilby's generator "rng" to "123" to ensure reproducibility
seed(123)

# Set the duration and sampling frequency of the data segment that we're going
# to inject the signal into
Expand All @@ -22,9 +26,6 @@
label = "australian_detector"
bilby.core.utils.setup_logger(outdir=outdir, label=label)

# Set up a random seed for result reproducibility. This is optional!
np.random.seed(88170232)

# create a new detector using a PyGwinc sensitivity curve
curve = gwinc.load_budget("Aplus").run()

Expand Down
Loading
Loading