> ## Documentation Index
> Fetch the complete documentation index at: https://docs.quantspace.limex.pro/llms.txt
> Use this file to discover all available pages before exploring further.

# Statistical Factor Models Server

> Stock-Watson, complete-panel, and dynamic statistical factor extraction.

## Overview

The Statistical Factor Models Server fits statistical factor models from numeric panel matrices. It covers Stock-Watson EM, complete-panel Bai-Ng style extraction, and Banbura-Modugno dynamic factor extraction.

## Connection

Add this server to your MCP client configuration.

<Tabs>
  <Tab title="Cursor">
    ```json theme={null}
    {
      "mcpServers": {
        "statistical-factor-models": {
          "url": "https://backend-production-c775c.up.railway.app/mcp/statistical-factor-models"
        }
      }
    }
    ```
  </Tab>

  <Tab title="Bearer auth">
    ```json theme={null}
    {
      "mcpServers": {
        "statistical-factor-models": {
          "url": "https://backend-production-c775c.up.railway.app/mcp/statistical-factor-models",
          "headers": {
            "Authorization": "Bearer ${env:STATISTICAL_FACTOR_MODELS_MCP_TOKEN}"
          }
        }
      }
    }
    ```
  </Tab>
</Tabs>

Restart the client after changing MCP configuration. The server tools appear automatically after the connection is established.

## Transport

| Property         | Value                                                                           |
| ---------------- | ------------------------------------------------------------------------------- |
| Protocol         | MCP over Streamable HTTP                                                        |
| MCP URL          | `https://backend-production-c775c.up.railway.app/mcp/statistical-factor-models` |
| Health URL       | `https://backend-production-c775c.up.railway.app/health`                        |
| MCP path         | `/mcp/statistical-factor-models`                                                |
| Request envelope | `{"request": {...}}`                                                            |
| Auth             | Optional bearer token when enabled for the endpoint                             |

## Best-Fit Workflows

* Fit a balanced panel with no missing values.
* Fit a missing-value panel with Stock-Watson EM.
* Fit a missing-value panel with a dynamic/state-space factor model.
* Use explicit mixed-frequency mappings for Stock-Watson runs.

## Recommended Tools

* complete-panel-factor-extractor-fit
* swfactor-extractor-create -> swfactor-extractor-fit
* bmfactor-extractor-fit

## Tools

| Tool                                  | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    | Returns |
| ------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `swfactor-extractor-fit`              | Fit the Stock-Watson EM factor model on a 2D panel `X` with shape `(N, T)`, where rows are series and columns are time points. Missing observations may be passed as `null`/NaN. Every row of `X` must already be stationary / `I(0)`; if the underlying series are `I(1)`, transform them first, for example with first differences or log differences. Preferred agent path: first create a handle with `swfactor-extractor-create`, pass exactly one of `n_factors` or `n_factors_max` in `init_kwargs`, and always pass explicit `flow_series` and `flow_series_diff` there, even when both are empty dicts. Then call `swfactor-extractor-fit` with that `handle`. Do not pass `n_factors_max` directly to `swfactor-extractor-fit`, and do not rely on direct/singleton SW calls to fill `flow_series` or `flow_series_diff` for you. For mixed-frequency setup, ordinary MAR rows and end-of-period stock-style rows should not go into `flow_series` or `flow_series_diff`; use `flow_series[i]=M` for Type C / Type D rows that decompose into `M` higher-frequency `I(0)` pieces, and use `flow_series_diff[i]=M` only for Type E rows built as stationary transforms of averaged `I(1)` levels. Classify rows by the stationary series actually passed into `X`, not by the raw source column. Example: if `X` rows are adjusted log-returns for `[SPY_adj_close, QQQ_adj_close, IWM_adj_close, ...]` and only `SPY` and `QQQ` are quarter-end rows on a monthly grid, use `flow_series=&#123;0:3,1:3&#125;` and `flow_series_diff=&#123;&#125;`. When the subtask is only to prepare SW mixed-frequency mappings, return only those two arguments. | `Any`   |
| `complete-panel-factor-extractor-fit` | Fit the complete-panel Bai-Ng style factor model on a balanced 2D numeric panel `X` with shape `(N, T)`. Missing values are not allowed. Every row of `X` must already be stationary / `I(0)`; if the underlying series are `I(1)`, transform them first, for example with first differences or log differences. Fast happy path: call this tool directly with `&#123;"request":&#123;"X":[[1.0,2.0,3.0],[1.5,2.5,3.5]]&#125;&#125;`. If you need custom rank settings, first create a handle with `complete-panel-factor-extractor-create` and pass exactly one of `n_factors` or `n_factors_max` in `init_kwargs`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           | `Any`   |
| `bmfactor-extractor-fit`              | Fit the Banbura-Modugno dynamic factor model on a 2D panel `X` with shape `(N, T)`, where rows are series and columns are time points. Missing observations may be passed as `null`/NaN. Every row of `X` must already be stationary / `I(0)`; if the underlying series are `I(1)`, transform them first, for example with first differences or log differences. Use this when you want a dynamic/state-space model rather than the simpler SW extractor. Fast happy path: call this tool directly with `&#123;"request":&#123;"X":[[1.0,null,3.0],[2.0,2.5,3.5]]&#125;&#125;`. If you need custom rank settings, first create a handle with `bmfactor-extractor-create` and pass exactly one of `n_factors` or `n_factors_max` in `init_kwargs`.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | `Any`   |

## Examples

### Fit a balanced panel directly

```json theme={null}
{
  "request": {
    "X": [
      [
        1.0,
        2.0,
        3.0,
        4.0
      ],
      [
        1.5,
        2.5,
        3.5,
        4.5
      ],
      [
        2.0,
        3.0,
        4.0,
        5.0
      ]
    ]
  }
}
```

### Create a Stock-Watson extractor

```json theme={null}
{
  "request": {
    "init_kwargs": {
      "n_factors_max": 2,
      "flow_series": {},
      "flow_series_diff": {}
    }
  }
}
```

### Fit the created Stock-Watson extractor

```json theme={null}
{
  "request": {
    "handle": "<handle-from-create>",
    "X": [
      [
        1.0,
        null,
        3.0,
        4.0
      ],
      [
        2.0,
        2.5,
        null,
        4.5
      ],
      [
        0.5,
        1.0,
        1.5,
        2.0
      ]
    ]
  }
}
```

## n8n Workflow

This server can be exposed as an n8n Webhook for panel factor extraction. The workflow can accept JSON or uploaded CSV, validate rank settings, build the numeric panel `X`, route by algorithm, call the MCP tool, and respond with a compact fit summary.

Recommended n8n MCP chain:

1. Normalize webhook body or uploaded CSV into a numeric panel with rows as series and columns as observations.
2. Validate the selected algorithm: `complete-panel`, `stock-watson`, or `banbura-modugno`.
3. For complete-panel runs, call `complete-panel-factor-extractor-fit` directly or create a handle first with `complete-panel-factor-extractor-create`.
4. For Stock-Watson, call `swfactor-extractor-create` with explicit `flow_series` and `flow_series_diff`, then `swfactor-extractor-fit`.
5. For Banbura-Modugno, call `bmfactor-extractor-create` / `bmfactor-extractor-fit` when a configured handle is needed, or call `bmfactor-extractor-fit` directly for the simple path.
6. If the fit returns `result_id`, call `result_get` before responding from n8n.

Keep panel data and model parameters in the webhook body or Code-node output. Keep credentials in n8n Credentials, not in exported workflow JSON.

See [n8n Workflows](/quantx/servers/deployed/n8n-workflows) for the shared webhook and MCP session patterns.

Available n8n template:

* [Statistical Factor Models User Workflow JSON](/quantx/servers/deployed/workflows/statistical-factor-models-user-workflow.json) - public panel-fit webhook at `POST https://n8n.limex.pro/webhook/stat-factor-model-fit`.

## Notes

* Primary fitting tools expect `X` as a JSON array of arrays with shape `(N, T)`: rows are series, columns are time points.
* Input rows should already be stationary / `I(0)`.
* Use `null` for missing observations in the Stock-Watson and dynamic factor tools.
* For Stock-Watson mixed-frequency work, create a handle with explicit `flow_series` and `flow_series_diff` first.

<CardGroup cols={3}>
  <Card title="Client setup" icon="plug" href="/quantx/servers/deployed/client-setup">
    Configure this endpoint in Cursor, Claude Desktop, or a generic MCP client.
  </Card>

  <Card title="n8n workflows" icon="workflow" href="/quantx/servers/deployed/n8n-workflows">
    Build webhook and scheduled factor-model flows around this MCP endpoint.
  </Card>

  <Card title="Shared tools" icon="wrench" href="/quantx/servers/deployed/shared-tools">
    Use health, result, artifact, environment, and table helper tools.
  </Card>
</CardGroup>

## Other Servers

<CardGroup cols={3}>
  <Card title="Dataloader" icon="server" href="/quantx/servers/dataloader">
    Market-data retrieval, TA-Lib indicators, and dataframe exports.
  </Card>

  <Card title="Fama-French Replicate" icon="server" href="/quantx/servers/fama-french-replicate">
    Official and replicated Fama-French factors plus loadings and alpha estimation.
  </Card>

  <Card title="Jump Models" icon="server" href="/quantx/servers/jump-models">
    JumpModel and SparseJumpModel regime fitting, online prediction, and backtesting.
  </Card>

  <Card title="Wavelet Mean Reversion" icon="server" href="/quantx/servers/wavelet-mean-reversion">
    Wavelet-based mean reversion analysis for financial time series.
  </Card>

  <Card title="Parallax ExtremeHurst" icon="server" href="/quantx/servers/parallax-extreme-hurst">
    ExtremeHurst signal generation from OHLCV data.
  </Card>

  <Card title="EP Ratio Screener" icon="server" href="/quantx/servers/ep-ratio-screener">
    Fundamental stock screening based on earnings yield and balance-sheet quality.
  </Card>

  <Card title="Volatility Scaling Lab" icon="server" href="/quantx/servers/volatility-scaling-lab">
    Volatility targeting, EWMA volatility, Monte Carlo bands, and risk diagnostics.
  </Card>
</CardGroup>
