Commit ff33f9a3 authored by Barthelet Thibault's avatar Barthelet Thibault
Browse files

raports

parent 244364f8
Loading
Loading
Loading
Loading
+670 KiB

File added.

No diff preview for this file type.

raport/sim.pdf

0 → 100644
+132 KiB

File added.

No diff preview for this file type.

+758 KiB
Loading image diff...
+150 −0
Original line number Diff line number Diff line
# Zero-Shot Time Series Forecasting for Educational Supply Chain Simulation

## Abstract

A supply chain simulation system was developed for educational purposes, combining customer behavior-driven demand generation with an interactive forecasting web application. Multiple forecasting strategies were evaluated, including traditional statistical methods and the TiRex zero-shot neural model. Results demonstrate that foundation models can outperform classical approaches on limited synthetic data without requiring retraining.

---

## 1. Context

### 1.1 Overview

Teaching supply chain forecasting requires realistic datasets, but commercial data is often restricted. Synthetic data generation is commonly employed, though producing authentic demand patterns with trend, seasonality, and natural variance remains challenging.

An additional concern in educational settings is the integration of machine learning models. Traditional approaches require retraining when simulation parameters change, creating friction in classroom environments where data may be regenerated frequently.

This work presents a simulation system with three components: a bottom-up demand generator, a student-facing web application for forecasting exercises, and an evaluation framework comparing multiple forecasting strategies including zero-shot neural methods.

### 1.2 Simulation

A customer-level simulation approach was implemented rather than top-down time series generation. Aggregate demand emerges from individual purchase decisions, producing more naturalistic patterns.

**Customer Segments:**

| Segment | Size | Purchase Frequency | Peak Months |
|---------|------|-------------------|-------------|
| Luxury Buyers | 30,000 | 18 months | Nov-Jan (holidays) |
| Sport Enthusiasts | 50,000 | 14 months | Apr-Jul (summer) |
| Casual Shoppers | 80,000 | 10 months | Sep-Oct (back-to-school) |

**Behavioral Parameters:**
- Brand affinity: Beta-distributed preference scores per product
- Price sensitivity: Affects watch selection probability
- Seasonality: Month-specific purchase probability multipliers

**Growth Dynamics:**
- Customer base: +0.2% growth monthly
- Purchase probability: +0.2% monthly increase
- Combined effect: 15-25% year-over-year demand growth

This approach ensures that trend, seasonality, and variance emerge organically from behavioral parameters rather than being mathematically imposed.

---

## 2. Experimentation

### 2.1 Dataset

The simulation generated 11 years of monthly sales data for three watch models:
- **Training set:** Years 1-10 (120 observations per product)
- **Test set:** Year 11 (12 observations per product)

### 2.2 Forecasting Strategies

Six strategies were evaluated:

| Strategy | Description |
|----------|-------------|
| **Naive** | Year 10 demand used as-is (baseline) |
| **Trend** | Year 10 + average monthly year-over-year growth |
| **AR(12)** | Autoregressive model with 12 lags |
| **Prophet** | Facebook's forecasting model with automatic seasonality |
| **TiRex** | Zero-shot xLSTM-based model (35M parameters, pre-trained on 47.5M series) |
| **Ensemble** | Simple average of all five methods |

The naive baseline was included as a reference point; any useful forecasting method should substantially outperform it.

### 2.3 Evaluation Metrics

- **MAE:** Mean Absolute Error (units)
- **RMSE:** Root Mean Square Error (penalizes large errors)
- **MAPE:** Mean Absolute Percentage Error (scale-independent)

---

## 3. Implementation

The simulation system was implemented in Python using NumPy for stochastic customer behavior modeling. The web application was built with Flask and provides students with historical data visualization, prediction input forms, and immediate feedback on forecasting accuracy and financial impact.

TiRex integration was achieved through the `tirex-ts` library with PyTorch backend. The model accepts historical context as a tensor and produces 12-month-ahead predictions without fine-tuning:

```python
context = torch.tensor(demands, dtype=torch.float32).unsqueeze(0)
quantiles, mean = model.forecast(context=context, prediction_length=12)
```

Prophet and AR models were fitted using standard implementations from `prophet` and `statsmodels` respectively.

---

## 4. Results

### 4.1 Overall Performance

| Strategy | MAE | RMSE | MAPE % |
|----------|-----|------|--------|
| **TiRex** | **24.89** | 33.39 | **3.05** |
| Prophet | 25.58 | **32.30** | 3.35 |
| Ensemble | 26.78 | 36.36 | 3.30 |
| AR(12) | 27.67 | 37.52 | 3.51 |
| Trend | 30.78 | 39.34 | 4.06 |
| Naive | 42.67 | 55.21 | 5.24 |

![Forecast Strategy Comparison](./forecast_comparison.png)

### 4.2 Per-Product Performance

**Luxury Classic:**
- TiRex achieved lowest errors across all metrics (MAPE: 3.0%)
- Prophet second-best (MAPE: 4.15%)

**Sport Pro:**
- AR(12) slightly outperformed TiRex on RMSE
- All advanced methods achieved 100% accuracy within 10% threshold

**Casual Style:**
- Prophet performed best (MAPE: 3.0%)
- TiRex second-best (MAPE: 3.11%)

### 4.3 Summary

TiRex won or tied on 2 of 4 overall metrics, with Prophet winning the remaining 2. Both substantially outperformed traditional methods. The naive baseline was outperformed by all strategies, confirming the presence of learnable patterns in the simulated data.

---

## 5. Discussion

### 5.1 Performance Analysis

The results demonstrate that TiRex, a zero-shot foundation model, outperformed Prophet and traditional statistical methods on overall MAE and MAPE despite having no exposure to this specific dataset during training. This finding is notable given that Prophet was specifically designed for business time series forecasting.

The strong performance of TiRex on only 120 training observations per product suggests effective transfer learning from its pre-training corpus of 47.5 million time series. The model successfully captured both seasonal patterns and trend components without explicit configuration.

### 5.2 Educational Applicability

The zero-shot nature of TiRex presents significant advantages for educational applications:

1. **No retraining required:** When simulation parameters are modified or new datasets are generated, TiRex can be applied immediately without computational overhead.

2. **Consistent baseline:** Students can compare their predictions against a fixed AI benchmark that does not change between simulation runs.

3. **Simplified deployment:** The absence of training pipelines reduces technical complexity in classroom environments.

4. **Reproducibility:** Results are deterministic given the same input context, facilitating grading and discussion.

---

## 6. Conclusion

A supply chain simulation system was developed and evaluated for educational forecasting exercises. The TiRex zero-shot foundation model demonstrated competitive or superior performance compared to traditional methods and Prophet, while requiring no training or fine-tuning. These characteristics make foundation models particularly suitable for educational and simulation contexts where data may change frequently and retraining is impractical.
 No newline at end of file

raport/source/sim.md

0 → 100644
+193 −0
Original line number Diff line number Diff line
# Supply Chain Simulation: Technical Documentation

## Overview

The simulation system generates realistic supply chain demand data through customer behavior modeling. Rather than imposing mathematical patterns (trend, seasonality) directly onto time series, aggregate demand emerges from the stochastic purchase decisions of individual customers. This bottom-up approach produces more naturalistic variance and coherent patterns across product categories.

---

## 1. Initialization

### 1.1 Watch Models

Three watch models are defined with distinct characteristics:

| Model | Category | Base Cost | Sell Price | Peak Months |
|-------|----------|-----------|------------|-------------|
| Luxury Classic | luxury | CHF 150 | CHF 500 | Nov, Dec, Jan |
| Sport Pro | sport | CHF 80 | CHF 220 | Apr, May, Jun, Jul |
| Casual Style | casual | CHF 40 | CHF 120 | Sep, Oct |

Peak months reflect real-world purchasing patterns: luxury items during holidays, sport watches in summer, casual watches during back-to-school season.

### 1.2 Customer Segments

Three customer segments are generated, each with distinct behavioral parameters:

| Segment | Size | Avg. Purchase Frequency | Price Sensitivity | Primary Affinity |
|---------|------|------------------------|-------------------|------------------|
| Luxury Buyers | 30,000 | 18 months | Low (β: 2,8) | Luxury Classic |
| Sport Enthusiasts | 50,000 | 14 months | Medium (β: 4,6) | Sport Pro |
| Casual Shoppers | 80,000 | 10 months | High (β: 6,4) | Casual Style |

**Total initial customer base:** 160,000 customers

### 1.3 Individual Customer Attributes

Each customer is instantiated with the following stochastic attributes:

- **Purchase frequency:** Sampled from normal distribution (segment mean ± std)
- **Brand affinity:** Beta-distributed preference score (0-1) for each watch model
- **Price sensitivity:** Beta-distributed value affecting watch selection
- **Seasonality factors:** Month-specific purchase probability multipliers inherited from segment

---

## 2. Monthly Simulation Loop

The simulation iterates through 132 months (11 years). Each month consists of three phases: customer base update, demand generation, and financial calculation.

### 2.1 Customer Base Update

At each timestep, the customer base is modified:

1. **Churn:** 0.3% of customers are removed (random selection)
2. **Growth:** New customers equal to 0.5% of current base are added
3. **Segment distribution:** New customers are allocated proportionally to original segment sizes

**Net effect:** ~0.2% monthly customer base growth

### 2.2 Demand Generation

Demand is generated through individual purchase decisions:

```
For each active customer:
    1. Calculate base purchase probability
       base_prob = 0.08 × trend_factor
       trend_factor = 1.0 + (0.002 × month_index)
    
    2. Adjust for purchase frequency
       monthly_prob = base_prob / customer.purchase_frequency
    
    3. Apply seasonality
       final_prob = monthly_prob × seasonality_factor[current_month]
    
    4. Make purchase decision
       if random() < final_prob → customer will purchase
    
    5. Select watch (if purchasing)
       weight[watch] = brand_affinity[watch] × price_factor[watch]
       chosen_watch = weighted_random_choice(weights)
    
    6. Increment demand counter for chosen watch
```

**Price factor calculation:**
```
price_factor = 1.0 - (price_sensitivity × (sell_price / 1000))
price_factor = max(0.1, price_factor)
```

This ensures price-sensitive customers are less likely to select expensive watches.

### 2.3 Production and Inventory

For each watch model, a simple production strategy is applied:

```
production = int(demand × 1.05)  # 5% buffer
available = inventory_start + production
units_sold = min(demand, available)
inventory_end = available - units_sold
stockout = max(0, demand - units_sold)
```

### 2.4 Financial Calculations

The following costs and revenues are computed:

| Metric | Formula |
|--------|---------|
| Revenue | `units_sold × sell_price` |
| Production Cost | `production × base_cost` |
| Labor Cost | `production × 20` |
| Holding Cost | `inventory_end × base_cost × 0.02` |
| Stockout Cost | `stockout × sell_price × 0.30` |
| Profit | `revenue - total_costs` |

---

## 3. Output

### 3.1 Dataset Structure

The simulation produces JSON files with the following structure:

```json
{
  "metadata": {
    "generator_type": "smart_customer_simulation",
    "years": 11,
    "total_months": 132,
    "initial_customers": 160000,
    "final_customers": 195000,
    "watches": [...]
  },
  "historical_data": [
    {
      "month_index": 0,
      "year": 1,
      "month": 1,
      "date": "2014-01",
      "active_customers": 160000,
      "watches": [
        {
          "watch_id": 1,
          "demand": 85,
          "production": 89,
          "inventory_start": 100,
          "inventory_end": 104,
          "units_sold": 85,
          "revenue": 42500.00,
          "profit": 28750.00,
          ...
        },
        ...
      ]
    },
    ...
  ]
}
```

### 3.2 Dataset Split

| Set | Period | Months | Purpose |
|-----|--------|--------|---------|
| Training | Years 1-10 | 0-119 | Historical data for analysis |
| Test | Year 11 | 120-131 | Ground truth for forecast evaluation |

---

## 4. Emergent Properties

The bottom-up approach produces the following characteristics without explicit mathematical formulation:

**Trend:** Emerges from:
- Customer base growth (net +0.2%/month)
- Increasing purchase probability (+0.2%/month)
- Combined effect: ~25% demand growth over 10 years

**Seasonality:** Emerges from:
- Segment-specific seasonality factors
- Higher purchase probability during peak months
- Different products peak at different times

**Variance:** Emerges from:
- Stochastic individual purchase decisions
- Random customer churn and acquisition
- Beta-distributed behavioral parameters


![Simulation Flow Diagram](./simpler_sim.png)
Loading