Price Forecasting (kadi.market.forecasting)¶
The Forecasting class predicts future crop prices based on historical price data. The model uses linear regression enriched with seasonal features (Fourier harmonics), trained on demand using the provided historical dataset. If historical data is insufficient (fewer than 20 observations), a clearly marked simulated fallback is returned.
Direct Importing¶
Initialization¶
Forecasting is created automatically by Market and is accessible via market.forecast. For direct instantiation:
The module has no persistent state: the model is trained on each call to predict() using the provided data.
Methods¶
predict(crop, market, ahead, ci, hist)¶
Predicts future price for a crop at a given market location.
Internal Pipeline:
- History validation (minimum 20 observations required). If insufficient, switches to simulated fallback.
- Feature engineering: linear trend + 2 Fourier harmonics (annual and semi-annual cycles over 365 / 182.5 days).
- Linear regression training on complete history.
- RMSE calculation via time series cross-validation (TimeSeriesSplit, 3 folds).
- Prediction interval calculation based on real RMSE, expanding with square root of time horizon.
import kadi as kd
market = kd.Market(lat=9.3, lon=2.3, location="Parakou")
# Fetching price history
df = market.pricing.fetch("maize", "parakou", days=365)
# 30-day forecast with 90% confidence interval
prediction = market.forecast.predict(
crop="maize",
market="parakou",
ahead=30,
ci=0.9,
hist=df,
)
print(f"Predicted price : {prediction['predicted_price']:.2f} XOF/kg")
print(f"Interval : [{prediction['low_90']:.2f}, {prediction['high_90']:.2f}]")
print(f"RMSE : {prediction['rmse']} XOF/kg")
print(f"Model : {prediction['model_used']}")
print(f"History points : {prediction['nb_history_pts']}")
print(f"Simulated : {prediction['is_simulated']}")
print(f"Confidence : {prediction['confidence_score']:.2f}")
Via the Market facade (full pipeline in a single call):
# Market.predict() handles fetching history automatically
prediction = market.predict("maize", ahead=30, confidence_interval=0.9, days=365)
print(f"Predicted price in 30d : {prediction['predicted_price']} XOF/kg")
print(f"Horizon used : {prediction['ahead']} days")
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
crop |
str |
required | Crop code (e.g. 'maize') |
market |
str |
required | Normalized market name (e.g. 'cotonou') |
ahead |
int |
7 |
Forecast horizon in days |
ci |
float |
0.9 |
Confidence level: 0.9 (90%) or 0.95 (95%) |
hist |
pd.DataFrame |
None |
History DataFrame with 'date' and 'price' columns |
Returns: dict
| Key | Type | Description |
|---|---|---|
predicted_price |
float |
Predicted price in XOF/kg |
low_90 |
float |
Lower bound of confidence interval |
high_90 |
float |
Upper bound of confidence interval |
confidence |
float |
Confidence level used (0.9 or 0.95) |
model_used |
str |
'linear_regression_fourier' or 'fallback_simule' |
rmse |
float |
RMSE in XOF/kg (None if simulated fallback) |
is_simulated |
bool |
True if source data is simulated |
confidence_score |
float |
Reliability score (0.0 to 1.0) |
nb_history_pts |
int |
Number of historical data points used |
days_ahead |
int |
Forecast horizon used |
Confidence Score:
The confidence score is calculated as follows:
source_score = 0.10 if is_simulated else 0.85
volume_factor = min(1.0, nb_pts / 100.0)
confidence_score = source_score * (0.5 + 0.5 * volume_factor)
A score of 0.85 is the maximum achievable with real data.
A score of 0.0 indicates a fully simulated fallback.
Simulated Fallback:
Activated if hist is missing or contains fewer than 20 valid observations. Returns predicted_price=300.0 XOF/kg with is_simulated=True and confidence_score=0.0. Must not be used for actual commercial decisions.
Deprecated Method¶
| Legacy Method | Replaced By | Since |
|---|---|---|
predict_price(crop, market, days_ahead, historique) |
predict(crop, market, ahead, hist) |
v1.2.0 |
Deprecated Class¶
| Legacy Class | Replaced By | Since |
|---|---|---|
MarketForecasting |
Forecasting |
v1.2.0 |
Forecasting ¶
Classe gérant la prévision de prix agricoles à partir de l'historique réel.
Le modèle principal est une régression linéaire enrichie de features saisonnières (harmoniques de Fourier). Il est entraîné à chaque appel de predict() sur l'historique fourni, sans état persistant entre les appels. Cela garantit que les prévisions sont toujours basées sur les données les plus récentes disponibles.
Quand l'historique est insuffisant (< 20 points) ou absent, la méthode bascule sur un fallback simulé clairement signalé par is_simulated=True.
Source code in kadi/market/forecasting.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
__init__ ¶
Initialise le module de prévision.
Aucun état persistant n'est conservé entre les appels : le modèle est entraîné à la demande dans predict().
Source code in kadi/market/forecasting.py
__getattr__ ¶
Intercepte les accès aux anciens noms de méthodes publiques.
Délègue vers le nouveau nom et émet un DeprecationWarning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Nom de l'attribut ou méthode demandé. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
callable |
La méthode correspondante sous son nouveau nom. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
Si le nom n'est ni nouveau ni ancien. |
Source code in kadi/market/forecasting.py
predict ¶
predict(crop: str, market: str, ahead: int = 7, ci: float = 0.9, hist: DataFrame = None, **kwargs) -> dict
Prédit le prix futur d'une culture sur un marché donné.
La méthode utilise une régression linéaire avec features temporelles et saisonnières (harmoniques de Fourier), entraînée sur l'historique fourni en paramètre. Le RMSE est calculé par validation croisée temporelle (TimeSeriesSplit).
Si l'historique est absent ou contient moins de 20 observations valides, la méthode bascule sur un fallback simulé et signale clairement is_simulated=True avec confidence_score=0.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crop
|
str
|
Code de la culture (ex: 'maize', 'rice'). |
required |
market
|
str
|
Nom normalisé du marché (ex: 'cotonou'). |
required |
ahead
|
int
|
Horizon de prévision en jours. Défaut : 7. La précision décroît avec l'horizon. |
7
|
ci
|
float
|
Niveau de confiance pour l'intervalle de prévision. Valeurs supportées : 0.9 (90%) et 0.95 (95%). Défaut : 0.9. |
0.9
|
hist
|
DataFrame
|
DataFrame avec au moins les colonnes 'date' (datetime) et 'price' (float en XOF/kg). Typiquement fourni par Pricing.fetch(). Si None ou insuffisant, le fallback simulé est activé. |
None
|
**kwargs
|
Anciens arguments (days_ahead, historique) pour rétrocompatibilité. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Dictionnaire contenant : - 'predicted_price' (float) : prix prédit en XOF/kg - 'low_90' (float) : borne inférieure de l'intervalle - 'high_90' (float) : borne supérieure de l'intervalle - 'confidence' (float) : niveau de confiance (0.9 ou 0.95) - 'model_used' (str) : identifiant du modèle utilisé - 'rmse' (float ou None) : RMSE réel en XOF/kg (None si simulé) - 'is_simulated' (bool) : True si les données source sont simulées - 'confidence_score' (float): score de fiabilité de la prévision (0-1) - 'nb_history_pts' (int) : nombre de points d'historique utilisés - 'days_ahead' (int) : horizon de prévision utilisé |
Source code in kadi/market/forecasting.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |