You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Original interval calculation
def _calculate_intervals(res, level, h, sigmah):
quantiles = [(1 + (l / 100)) / 2 for l in level]
out = {}
for l, q in zip(reversed(level), quantiles):
out[f'lo-{l}'] = res['mean'] - np.sqrt(2) * erfinv(2 * q - 1) * sigmah
for l, q in zip(level, quantiles):
out[f'hi-{l}'] = res['mean'] + np.sqrt(2) * erfinv(2 * q - 1) * sigmah
return out
# Optimized interval calculation
@njit(nogil=True, cache=True) # Key optimization 1: Numba compilation
def _calculate_intervals_optimized(res, level, h, sigmah):
# Key optimization 2: Pre-allocate arrays
quantiles = np.empty(len(level))
lo = np.empty((len(level), h))
hi = np.empty((len(level), h))
for i, l in enumerate(level):
quantiles[i] = (1 + (l / 100)) / 2
mean = res['mean']
for i, q in enumerate(quantiles):
norm_quantile = np.sqrt(2) * erfinv(2 * q - 1)
lo[i] = mean - norm_quantile * sigmah
hi[i] = mean + norm_quantile * sigmah
result = {}
for i, l in enumerate(reversed(level)):
result[f'lo-{l}'] = lo[i]
for i, l in enumerate(level):
result[f'hi-{l}'] = hi[i]
return result
# Original SES calculation
def _ses_forecast(x, alpha):
smoothed = x[0]
n = len(x)
fitted = np.full(n, np.nan, dtype=x.dtype)
for i in range(1, n):
smoothed = (alpha * x[i - 1] + (1 - alpha) * smoothed).item()
fitted[i] = smoothed
forecast = alpha * x[-1] + (1 - alpha) * smoothed
return forecast, fitted
# Optimized SES calculation
@njit(nogil=True, cache=True) # Key optimization 1: Numba compilation
def _ses_forecast_optimized(x: np.ndarray, alpha: float) -> tuple:
n = len(x)
# Key optimization 2: Pre-allocate with correct type
fitted = np.empty(n, dtype=x.dtype)
fitted[0] = x[0]
for i in range(1, n):
fitted[i] = alpha * x[i-1] + (1 - alpha) * fitted[i-1]
forecast = alpha * x[-1] + (1 - alpha) * fitted[-1]
return forecast, fitted
Key Optimizations Explained:
Numba JIT Compilation (@njit decorator):
Compiles the Python code to machine code
Removes Python interpreter overhead
Especially effective for numerical computations and loops
Pre-allocation of Arrays:
Original: Creates new arrays in loops or uses np.full with NaN
Optimized: Pre-allocates exact size arrays with np.empty
Avoids memory reallocation during computation
Loop Optimization:
Original: Multiple loops and list comprehensions
Optimized: Single loops with direct array indexing
Reduces iteration overhead
Vectorized Operations:
Original: Item-by-item calculations
Optimized: Uses NumPy's vectorized operations where possible
Takes advantage of SIMD instructions
Memory Access Patterns:
Original: Multiple dictionary accesses and array copies
Optimized: Direct array access and minimized copies
Better cache utilization
Type Stability:
Original: Mixed types and .item() conversions
Optimized: Consistent types throughout computation
Helps Numba generate more efficient machine code
Cache Usage (cache=True):
Reuses compiled code between calls
Avoids recompilation overhead
The text was updated successfully, but these errors were encountered:
Description
Key Optimizations Explained:
Numba JIT Compilation (@njit decorator):
Compiles the Python code to machine code
Removes Python interpreter overhead
Especially effective for numerical computations and loops
Pre-allocation of Arrays:
Original: Creates new arrays in loops or uses np.full with NaN
Optimized: Pre-allocates exact size arrays with np.empty
Avoids memory reallocation during computation
Loop Optimization:
Original: Multiple loops and list comprehensions
Optimized: Single loops with direct array indexing
Reduces iteration overhead
Vectorized Operations:
Original: Item-by-item calculations
Optimized: Uses NumPy's vectorized operations where possible
Takes advantage of SIMD instructions
Memory Access Patterns:
Original: Multiple dictionary accesses and array copies
Optimized: Direct array access and minimized copies
Better cache utilization
Type Stability:
Original: Mixed types and .item() conversions
Optimized: Consistent types throughout computation
Helps Numba generate more efficient machine code
Cache Usage (cache=True):
Reuses compiled code between calls
Avoids recompilation overhead
The text was updated successfully, but these errors were encountered: