<jupyter_start><jupyter_text>Numba -  an overview Numba is a just-in-time compiler for Python that works best on code that uses NumPy arrays and functions, and loops.ArviZ includes Numba as an optional dependency and a number of functions have been included in **utils.py** for systems in which Numba is pre-installed. An additional functionality of disabling/re-enabling numba for systems which have numba installed has also been included.  A simple example to display the effectiveness of Numba<jupyter_code>import arviz as az
import numpy as np
import timeit

from arviz.utils import conditional_jit, Numba
from arviz.stats import geweke
from arviz.stats.diagnostics import ks_summary
data = np.random.randn(1000000)
def variance(data, ddof=0):  # Method to calculate variance without using numba
    a_a, b_b = 0, 0
    for i in data:
        a_a = a_a + i
        b_b = b_b + i * i
    var = b_b / (len(data)) - ((a_a / (len(data))) ** 2)
    var = var * (len(data) / (len(data) - ddof))
    return var
%timeit variance(data, ddof=1)
@conditional_jit
def variance_jit(data, ddof=0):  # Calculating variance with numba
    a_a, b_b = 0, 0
    for i in data:
        a_a = a_a + i
        b_b = b_b + i * i
    var = b_b / (len(data)) - ((a_a / (len(data))) ** 2)
    var = var * (len(data) / (len(data) - ddof))
    return var
%timeit variance_jit(data, ddof=1)<jupyter_output>1.23 ms ± 68.9 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)<jupyter_text>**That is almost 300 times faster!! Let's compare this to numpy**<jupyter_code>%timeit np.var(data, ddof=1)<jupyter_output>2.13 ms ± 196 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)<jupyter_text>**In certain scenarios, Numba outperforms numpy!** **Let's see Numba's effect on a few of ArviZ functions**<jupyter_code>Numba.disable_numba()  # This disables numba
Numba.numba_flag
data = np.random.randn(1000000)
smaller_data = np.random.randn(1000)
%timeit geweke(data)
%timeit geweke(smaller_data)
Numba.enable_numba()  # This will re-enable numba
Numba.numba_flag  # This indicates the status of Numba
%timeit geweke(data)
%timeit geweke(smaller_data)
Numba.enable_numba()
Numba.numba_flag<jupyter_output><empty_output><jupyter_text>**Numba speeds up the code by a factor of two approximately. Let's check some other method**<jupyter_code>summary_data = np.random.randn(1000, 100, 10)
school = az.load_arviz_data("centered_eight").posterior["mu"].values
Numba.disable_numba()
Numba.numba_flag
%timeit ks_summary(summary_data)
%timeit ks_summary(school)
Numba.enable_numba()
Numba.numba_flag
%timeit ks_summary(summary_data)
%timeit ks_summary(school)<jupyter_output>1.18 ms ± 79.2 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
{'fraction_non_alphanumeric': 0.10631229235880399, 'fraction_numerical': 0.024732373569582872, 'mean_word_length': 4.508130081300813, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '49440', 'license': 'Apache-2.0', 'n_tokens_mistral': 1069, 'n_tokens_neox': 1004, 'n_words': 312, 'path': 'doc/notebooks/Numba.ipynb', 'repo_name': 'neha-shah99/arviz', 'size': '2709'}