Skip to content

Commit

Permalink
Merge pull request #656 from Smit-create/random_sample1
Browse files Browse the repository at this point in the history
[Refactor]: Make random methods compatible to RandomGenerator
  • Loading branch information
Smit-create committed Nov 26, 2022
2 parents 6cc1759 + 1ec966e commit c20ea25
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion quantecon/arma.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def simulation(self, ts_length=90, random_state=None):
random_state = check_random_state(random_state)

sys = self.ma_poly, self.ar_poly, 1
u = random_state.randn(ts_length, 1) * self.sigma
u = random_state.standard_normal((ts_length, 1)) * self.sigma
vals = dlsim(sys, u)[1]

return vals.flatten()
2 changes: 1 addition & 1 deletion quantecon/graph_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def random_tournament_graph(n, random_state=None):
"""
random_state = check_random_state(random_state)
num_edges = n * (n-1) // 2
r = random_state.random_sample(num_edges)
r = random_state.random(num_edges)
row = np.empty(num_edges, dtype=int)
col = np.empty(num_edges, dtype=int)
_populate_random_tournament_row_col(n, r, row, col)
Expand Down
4 changes: 2 additions & 2 deletions quantecon/lqcontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def compute_sequence(self, x0, ts_length=None, method='doubling',
x0 = x0.reshape(self.n, 1) # Make sure x0 is a column vector
x_path = np.empty((self.n, T+1))
u_path = np.empty((self.k, T))
w_path = random_state.randn(self.j, T+1)
w_path = random_state.standard_normal((self.j, T+1))
Cw_path = np.dot(C, w_path)

# == Compute and record the sequence of policies == #
Expand Down Expand Up @@ -604,7 +604,7 @@ def compute_sequence(self, x0, ts_length=None, random_state=None):
# == Prepare storage arrays == #
x_path = np.empty((self.n, T+1))
u_path = np.empty((self.k, T))
w_path = random_state.randn(self.j, T+1)
w_path = random_state.standard_normal((self.j, T+1))
Cw_path = np.empty((self.n, T+1))
for i in range(T+1):
Cw_path[:, i] = Cs[state[i]] @ w_path[:, i]
Expand Down
4 changes: 2 additions & 2 deletions quantecon/lqnash.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ def nnash(A, B1, B2, R1, R2, Q1, Q2, S1, S2, W1, W2, M1, M2,
v2 = np.eye(k_2)
P1 = np.zeros((n, n))
P2 = np.zeros((n, n))
F1 = random_state.randn(k_1, n)
F2 = random_state.randn(k_2, n)
F1 = random_state.standard_normal((k_1, n))
F2 = random_state.standard_normal((k_2, n))

for it in range(max_iter):
# update
Expand Down
6 changes: 3 additions & 3 deletions quantecon/lss.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ def simulate(self, ts_length=100, random_state=None):

x0 = random_state.multivariate_normal(self.mu_0.flatten(),
self.Sigma_0)
w = random_state.randn(self.m, ts_length-1)
w = random_state.standard_normal((self.m, ts_length-1))
v = self.C.dot(w) # Multiply each w_t by C to get v_t = C w_t
# == simulate time series == #
x = simulate_linear_model(self.A, x0, v, ts_length)

if self.H is not None:
v = random_state.randn(self.l, ts_length)
v = random_state.standard_normal((self.l, ts_length))
y = self.G.dot(x) + self.H.dot(v)
else:
y = self.G.dot(x)
Expand Down Expand Up @@ -235,7 +235,7 @@ def replicate(self, T=10, num_reps=100, random_state=None):
x_T, _ = self.simulate(ts_length=T+1, random_state=random_state)
x[:, j] = x_T[:, -1]
if self.H is not None:
v = random_state.randn(self.l, num_reps)
v = random_state.standard_normal((self.l, num_reps))
y = self.G.dot(x) + self.H.dot(v)
else:
y = self.G.dot(x)
Expand Down
4 changes: 2 additions & 2 deletions quantecon/markov/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def simulate_indices(self, ts_length, init=None, num_reps=None,
X = np.empty((k, ts_length), dtype=int)

# Random values, uniformly sampled from [0, 1)
random_values = random_state.random_sample(size=(k, ts_length-1))
random_values = random_state.random(size=(k, ts_length-1))

# Generate sample paths and store in X
if not self.is_sparse: # Dense
Expand Down Expand Up @@ -705,7 +705,7 @@ def mc_sample_path(P, init=0, sample_size=1000, random_state=None):
X_0 = init
else:
cdf0 = np.cumsum(init)
u_0 = random_state.random_sample()
u_0 = random_state.random()
X_0 = searchsorted(cdf0, u_0)

mc = MarkovChain(P)
Expand Down
4 changes: 2 additions & 2 deletions quantecon/markov/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ def random_discrete_dp(num_states, num_actions, beta=None,
L = num_states * num_actions

random_state = check_random_state(random_state)
R = scale * random_state.randn(L)
R = scale * random_state.standard_normal(L)
Q = _random_stochastic_matrix(L, num_states, k=k,
sparse=sparse, format='csr',
random_state=random_state)
if beta is None:
beta = random_state.random_sample()
beta = random_state.random()

if sa_pair:
s_indices, a_indices = sa_indices(num_states, num_actions)
Expand Down
2 changes: 1 addition & 1 deletion quantecon/quad.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def qnwequi(n, a, b, kind="N", equidist_pp=None, random_state=None):
nodes = np.outer(i * (i+1) / 2, j)
nodes = (nodes - fix(nodes)).squeeze()
elif kind.upper() == "R": # pseudo-random
nodes = random_state.rand(n, d).squeeze()
nodes = random_state.random((n, d)).squeeze()
else:
raise ValueError("Unknown sequence requested")

Expand Down
4 changes: 2 additions & 2 deletions quantecon/random/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def probvec(m, k, random_state=None, parallel=True):

# if k >= 2
random_state = check_random_state(random_state)
r = random_state.random_sample(size=(m, k-1))
r = random_state.random(size=(m, k-1))
x = np.empty((m, k))

# Parse Parallel Option #
Expand Down Expand Up @@ -146,7 +146,7 @@ def sample_without_replacement(n, k, num_trials=None, random_state=None):
size = k if num_trials is None else (num_trials, k)

random_state = check_random_state(random_state)
r = random_state.random_sample(size=size)
r = random_state.random(size=size)
result = _sample_without_replacement(n, r)

return result
Expand Down

0 comments on commit c20ea25

Please sign in to comment.