QuantEcon / QuantEcon.py

A community based Python library for quantitative economics
https://quantecon.org/quantecon-py/
MIT License
1.89k stars 2.25k forks source link

Error Raised in `qe.random.draw` #704

Closed HumphreyYang closed 1 year ago

HumphreyYang commented 1 year ago

Hi @Smit-create,

I encountered this error in the intermediate lecture (full error report here. It is related to the draw function in random.utilities.

The error appears in this function and is temporarily resolved by changing qe.random.draw(cdf) to qe.random.draw(cdf, 1).

cdf = np.cumsum(q_default)

@jit(nopython=True)
def compute_stopping_time(w_bar, seed=1234):

    np.random.seed(seed)
    t = 1
    while True:
        # Generate a wage draw
        w = w_default[qe.random.draw(cdf)]
        # Stop when the draw is above the reservation wage
        if w >= w_bar:
            stopping_time = t
            break
        else:
            t += 1
    return stopping_time

@jit(nopython=True)
def compute_mean_stopping_time(w_bar, num_reps=100000):
    obs = np.empty(num_reps)
    for i in range(num_reps):
        obs[i] = compute_stopping_time(w_bar, seed=i)
    return obs.mean()

c_vals = np.linspace(10, 40, 25)
stop_times = np.empty_like(c_vals)
for i, c in enumerate(c_vals):
    mcm = McCallModel(c=c)
    w_bar = compute_reservation_wage_two(mcm)
    stop_times[i] = compute_mean_stopping_time(w_bar)

fig, ax = plt.subplots()

ax.plot(c_vals, stop_times, label="mean unemployment duration")
ax.set(xlabel="unemployment compensation", ylabel="months")
ax.legend()

plt.show()

The error message is

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function draw at 0x7f88556a4af0>) found for signature:

 >>> draw(readonly array(float64, 1d, C))

There are 2 candidate implementations:
   - Of which 2 did not match due to:
   Overload in function 'ol_draw': File: quantecon/random/utilities.py: Line 213.
     With argument(s): '(readonly array(float64, 1d, C))':
    Rejected as the implementation raised a specific error:
      TypingError: missing a required argument: 'size'
  raised from /opt/conda/envs/quantecon/lib/python3.10/site-packages/numba/core/typing/templates.py:791

During: resolving callee type: Function(<function draw at 0x7f88556a4af0>)
During: typing of call at /tmp/ipykernel_4408/474854781.py (10)


File "../../../../tmp/ipykernel_4408/474854781.py", line 10:
<source missing, REPL/exec in use?>

During: resolving callee type: type(CPUDispatcher(<function compute_stopping_time at 0x7f884d71c280>))
During: typing of call at /tmp/ipykernel_4408/474854781.py (23)

During: resolving callee type: type(CPUDispatcher(<function compute_stopping_time at 0x7f884d71c280>))
During: typing of call at /tmp/ipykernel_4408/474854781.py (23)


File "../../../../tmp/ipykernel_4408/474854781.py", line 23:
<source missing, REPL/exec in use?>

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<function draw at 0x7f88556a4af0>) found for signature:

 >>> draw(readonly array(float64, 1d, C))

There are 2 candidate implementations:
   - Of which 2 did not match due to:
   Overload in function 'ol_draw': File: quantecon/random/utilities.py: Line 213.
     With argument(s): '(readonly array(float64, 1d, C))':
    Rejected as the implementation raised a specific error:
      TypingError: missing a required argument: 'size'
  raised from /opt/conda/envs/quantecon/lib/python3.10/site-packages/numba/core/typing/templates.py:791

During: resolving callee type: Function(<function draw at 0x7f88556a4af0>)
During: typing of call at /tmp/ipykernel_4408/474854781.py (10)


File "../../../../tmp/ipykernel_4408/474854781.py", line 10:
<source missing, REPL/exec in use?>

During: resolving callee type: type(CPUDispatcher(<function compute_stopping_time at 0x7f884d71c280>))
During: typing of call at /tmp/ipykernel_4408/474854781.py (23)

During: resolving callee type: type(CPUDispatcher(<function compute_stopping_time at 0x7f884d71c280>))
During: typing of call at /tmp/ipykernel_4408/474854781.py (23)


File "../../../../tmp/ipykernel_4408/474854781.py", line 23:
<source missing, REPL/exec in use?>

Would you mind having a look into this error?

Many thanks in advance.