I created a simple class that works without @jitclass. When I try to improve it with @jitclass it stops working. What is happening here?

Viewed 17

Following example 12.4 from the following website https://python-programming.quantecon.org/numba.html#id4 i constructed a simple class to model an AR(1) process.

Although the code works fine without the use of @jitclass, the code stops working after I remove ("#").

import numpy as np
import numba
import matplotlib.pyplot as plt
from numba import float64
from numba import int32
from numba.experimental import jitclass

#ar_1_data = [('ρ', float64), ('z_0', float64), ('μ', float64), ('σ', float64)]

#@jitclass(ar_1_data)

class ar_1:
    
    def __init__(self, ρ = 0.5, z_0 = 1, μ = 0, σ = 1):
        self.ρ = ρ
        self.z = z_0
        self.lnz = np.log(z_0)
        self.μ = μ
        self.σ = σ
        
    def update(self):
        self.z = self.z**(self.ρ) * np.e**(np.random.normal(self.μ,self.σ))
        
    def sequence(self, n):
        path = []
        path_log = []
        for i in range(n):
            path.append(self.z)
            path_log.append(np.log(self.z))
            self.update()
        self.sequence = path 
        self.sequence_log = path_log

a = ar_1()
a.sequence(100)

Here is the Error im getting after removing the "#":


---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
Input In [83], in <cell line: 1>()
----> 1 a = ar_1()
      2 a.sequence(100)

File ~\anaconda3\lib\site-packages\numba\experimental\jitclass\base.py:124, in JitClassType.__call__(cls, *args, **kwargs)
    122 bind = cls._ctor_sig.bind(None, *args, **kwargs)
    123 bind.apply_defaults()
--> 124 return cls._ctor(*bind.args[1:], **bind.kwargs)

File ~\anaconda3\lib\site-packages\numba\core\dispatcher.py:468, in _DispatcherBase._compile_for_args(self, *args, **kws)
    464         msg = (f"{str(e).rstrip()} \n\nThis error may have been caused "
    465                f"by the following argument(s):\n{args_str}\n")
    466         e.patch_message(msg)
--> 468     error_rewrite(e, 'typing')
    469 except errors.UnsupportedError as e:
    470     # Something unsupported is present in the user code, add help info
    471     error_rewrite(e, 'unsupported_error')

File ~\anaconda3\lib\site-packages\numba\core\dispatcher.py:409, in _DispatcherBase._compile_for_args.<locals>.error_rewrite(e, issue_type)
    407     raise e
    408 else:
--> 409     raise e.with_traceback(None)

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Failed in nopython mode pipeline (step: nopython frontend)
Cannot resolve setattr: (instance.jitclass.ar_1#2658a3e4d30<ρ:float64,z_0:float64,μ:float64,σ:float64>).z = int64

File "..\..\..\..\..\AppData\Local\Temp\ipykernel_17336\4275445632.py", line 9:
<source missing, REPL/exec in use?>

During: typing of set attribute 'z' at C:\Users\Hogar\AppData\Local\Temp\ipykernel_17336\4275445632.py (9)

File "..\..\..\..\..\AppData\Local\Temp\ipykernel_17336\4275445632.py", line 9:
<source missing, REPL/exec in use?>

During: resolving callee type: jitclass.ar_1#2658a3e4d30<ρ:float64,z_0:float64,μ:float64,σ:float64>
During: typing of call at <string> (3)

During: resolving callee type: jitclass.ar_1#2658a3e4d30<ρ:float64,z_0:float64,μ:float64,σ:float64>
During: typing of call at <string> (3)


File "<string>", line 3:
<source missing, REPL/exec in use?>
0 Answers
Related