# PIN

`D11-F03-A05` · Market Microstructure → Order-Flow and Impact · archetype `record-transform` · difficulty 5/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/market-microstructure/order-flow-and-impact/pin/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

```ts
import { pin } from "fintech-algorithms/market-microstructure/order-flow-and-impact/pin";
```

## Signature

```ts
pin(inputRows, starts, balancedNoise, iterations)
```

Probability of informed trading, estimated by maximum likelihood on daily buy and sell counts. Notoriously hard to optimise — the likelihood has flat regions and local optima, which is why the starting points are a parameter.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `inputRows` | `Row[]` | yes | Daily buy and sell trade counts. |
| `starts` | `number[][]` | yes | Starting parameter vectors. Multiple starts are the standard defence against local optima; a single start frequently converges somewhere wrong. |
| `balancedNoise` | `number` | yes | Initial guess for the balanced uninformed arrival rate. · min: 0 |
| `iterations` | `number` | yes | Maximum optimiser iterations. · min: 1, integer: true |

## Returns

`{ pin, alpha, mu, epsilon_buy, epsilon_sell, converged, … }`

The PIN estimate with every structural parameter and a convergence flag — an unconverged PIN is not an estimate.

## Errors

- When no starting vector is supplied — throws

## Complexity

Time `O(days × iterations × starts)`, space `O(days)`.

## Worked example

Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

### Input

`inputRows`:

```json
[
  {
    "id": "D001",
    "day": 1,
    "buys": 12,
    "sells": 29
  },
  {
    "id": "D002",
    "day": 2,
    "buys": 14,
    "sells": 12
  },
  {
    "id": "D003",
    "day": 3,
    "buys": 16,
    "sells": 13
  }
]
```

Showing 3 of 60 elements.

`starts`:

```json
3
```

`balancedNoise`:

```json
false
```

`iterations`:

```json
120
```

### Call

```ts
pin(inputRows, starts, balancedNoise, iterations)
```

### Returns

object with 13 fields: model, state, observation_count, starts, balanced_noise, alpha, delta, mu, …

```json
{
  "model": "ekop-pin",
  "state": "estimated",
  "observation_count": 60,
  "starts": 3,
  "balanced_noise": false,
  "alpha": 0.3335769653,
  "delta": 0.3998646736,
  "mu": 19.2332579136,
  "epsilon_buy": 14.0464750051,
  "epsilon_sell": 13.0377527714,
  "log_likelihood": -335.6690937078,
  "pin": 0.1915155787,
  "trace": [
    {
      "id": "D001",
      "index": 0,
      "buys": 12,
      "sells": 29,
      "imbalance": -17,
      "side": "sell-heavy",
      "reason": "daily-count-input"
    },
    {
      "id": "D002",
      "index": 1,
      "buys": 14,
      "sells": 12,
      "imbalance": 2,
      "side": "buy-heavy",
      "reason": "daily-count-input"
    },
    {
      "id": "D003",
      "index": 2,
      "buys": 16,
      "sells": 13,
      "imbalance": 3,
      "side": "buy-heavy",
      "reason": "daily-count-input"
    }
  ]
}
```

## Other exports

`orderFlowImbalance`, `queueImbalance`, `kyleLambda`, `hasbrouckPriceImpact`, `vpin`, `runTopic`. Every module additionally exports `run` as an alias of its primary
function, and a `meta` object carrying its catalog id, domain, family, shape and article URL.

## Verification and provenance

Tier: **contract**.

The module loads, the entry point is callable and its declared signature matches the compiled code. The example below is real captured output, but no independently published figure asserts the numbers.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.1.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/market-microstructure/order-flow-and-impact/pin/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-microstructure/order-flow-and-impact/pin/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-microstructure/llms.txt
