# Wiener Filter

> "The signal is the truth. The noise is just an opinion."

The Wiener Filter is an optimal linear filter that attempts to minimize the mean square error between the estimated random process and the desired process. In the context of technical analysis, it acts as an adaptive smoothing filter that adjusts its responsiveness based on the local statistical properties of the data (signal-to-noise ratio). When the signal variance is high relative to noise variance, the filter follows the input closely. When noise dominates, it smooths aggressively.

## Historical Context

Developed by Norbert Wiener in the 1940s, this filter is a cornerstone of statistical signal processing. While originally designed for stationary signals, its adaptive nature makes it effective for financial time series where volatility (noise) and trends (signal) fluctuate constantly. This implementation uses a localized approach to estimate the signal and noise statistics dynamically.

## Architecture & Physics

The Wiener Filter operates on two time scales:

1. **Noise Estimation (`period`)**: Analyzes the variance of high-frequency fluctuations (diffs) to estimate the noise floor.
2. **Signal Estimation (`smoothPeriod`)**: Analyzes the variance of the price around a local mean to estimate the total power (signal + noise).

It dynamically calculates a gain factor $k$:

* $k \approx 1$: Signal dominates → Output follows input (less smoothing).
* $k \approx 0$: Noise dominates → Output follows local mean (more smoothing).

### Architecture

* **Zero-Allocation**: Uses `RingBuffer` and `stackalloc` internally (implied via scalar loop logic) to avoid heap pressure.
* **Constant Time**: Updates are $O(P)$ where $P$ is the lookback period (due to variance calculations), but optimized for linear access.

## Mathematical Foundation

1. **Noise Variance ($\sigma_n^2$)**
    Estimated from the sum of squared differences of consecutive prices over `period`.
    $$ \sigma_n^2 = \frac{1}{2N} \sum_{i=0}^{N-1} (x_i - x_{i+1})^2 $$
    Where $N$ is `period`.

2. **Total Variance ($\sigma_x^2$)**
    Variance of the input signal around its local mean ($\mu$) over `smoothPeriod`.
    $$ \mu = \frac{1}{M} \sum_{i=0}^{M-1} x_i $$
    $$ \sigma_x^2 = \frac{1}{M} \sum_{i=0}^{M-1} (x_i - \mu)^2 $$
    Where $M$ is `smoothPeriod`.

3. **Signal Variance ($\sigma_s^2$)**
    $$ \sigma_s^2 = \max(\sigma_x^2 - \sigma_n^2, 0) $$

4. **Optimal Gain ($k$)**
    $$ k = \frac{\sigma_s^2}{\sigma_s^2 + \sigma_n^2} $$

5. **Output ($y$)**
    $$ y_t = \mu + k \cdot (x_t - \mu) $$

## Performance Profile

| Metric | Score | Notes |
| :--- | :--- | :--- |
| **Throughput** | 🟢 High | O(N) where N is period, but N is small. |
| **Allocations** | 🟢 Zero | No heap allocations in hot path. |
| **Complexity** | O(N) | Requires iteration for variance calc (SoA loops). |
| **Accuracy** | 🟢 High | Statistically optimal for stationary noise. |
| **Timeliness** | 🟡 Medium | Adapts to volatility; can lag in sudden trends if `smoothPeriod` is long. |
| **Smoothness** | 🟢 High | Filters noise aggressively when volatility is low. |

## Validation

Validated against a reference implementation of the Wiener algorithm logic.

| Library | Status | Notes |
| :--- | :--- | :--- |
| **Reference** | ✅ | Matches internal statistical reference implementation. |

## Usage

```csharp
using QuanTAlib;

// 1. Initialize
var wiener = new Wiener(period: 20, smoothPeriod: 10);

// 2. Update with new data
var result = wiener.Update(new TValue(DateTime.UtcNow, 100.0));

// 3. Access results
Console.WriteLine($"Filter Value: {result.Value}");
Console.WriteLine($"Is Hot: {wiener.IsHot}");
