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

# Calculate Kullback-Leibler Divergence

> Calculates the Kullback-Leibler (KL) divergence from distribution Q to P, measuring how one probability distribution differs from a reference distribution. KL divergence is asymmetric and always non-negative. Essential for model comparison, portfolio rebalancing decisions, and detecting distribution shifts in market conditions.

**Use Cases:**
- Compare forecast distributions to realized outcomes
- Detect regime changes in market conditions
- Evaluate model performance and goodness-of-fit
- Measure portfolio drift from target allocation
- Assess information gain from new data

**Formula:** D_KL(P||Q) = Σ p(x) log(p(x)/q(x))

**Credits:** 5 credits per request (Pro Tier) [Tier: ENTERPRISE, Credits: 10]



## OpenAPI

````yaml api-specs/physics.json post /quantlib/physics/divergence/kl
openapi: 3.1.0
info:
  title: FinceptQuantLib API - Physics Module
  description: >-
    Physics and Information Theory module for FinceptQuantLib API. Includes
    Shannon/Renyi/Tsallis entropy, KL/JS divergence, mutual information,
    transfer entropy, Fisher information, Boltzmann distribution, Ising model,
    maximum entropy, thermodynamics (free energy, Carnot cycle, van der Waals
    equation), and Maxwell relations. **Pro Tier required. 5 credits per
    request.**
  version: 3.0.0
  contact:
    name: Fincept API Support
    url: https://fincept.in
servers:
  - url: https://api.fincept.in
    description: Fincept API Production Server
security:
  - APIKeyHeader: []
tags:
  - name: quantlib-physics
    description: Physics and Information Theory module for FinceptQuantLib API
    x-displayName: Physics
paths:
  /quantlib/physics/divergence/kl:
    post:
      tags:
        - quantlib-physics
      summary: Calculate Kullback-Leibler Divergence
      description: >-
        Calculates the Kullback-Leibler (KL) divergence from distribution Q to
        P, measuring how one probability distribution differs from a reference
        distribution. KL divergence is asymmetric and always non-negative.
        Essential for model comparison, portfolio rebalancing decisions, and
        detecting distribution shifts in market conditions.


        **Use Cases:**

        - Compare forecast distributions to realized outcomes

        - Detect regime changes in market conditions

        - Evaluate model performance and goodness-of-fit

        - Measure portfolio drift from target allocation

        - Assess information gain from new data


        **Formula:** D_KL(P||Q) = Σ p(x) log(p(x)/q(x))


        **Credits:** 5 credits per request (Pro Tier) [Tier: ENTERPRISE,
        Credits: 10]
      operationId: kl_divergence
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - p
                - q
              properties:
                p:
                  type: array
                  items:
                    type: number
                  description: Target probability distribution (must sum to 1)
                  example:
                    - 0.4
                    - 0.3
                    - 0.2
                    - 0.1
                q:
                  type: array
                  items:
                    type: number
                  description: Reference probability distribution (must sum to 1)
                  example:
                    - 0.25
                    - 0.25
                    - 0.25
                    - 0.25
                base:
                  type: number
                  description: Logarithm base for divergence calculation
                  default: 2.718281828459045
                  example: 2.718281828459045
            examples:
              portfolio_drift:
                summary: Portfolio allocation drift
                value:
                  p:
                    - 0.35
                    - 0.3
                    - 0.2
                    - 0.15
                  q:
                    - 0.25
                    - 0.25
                    - 0.25
                    - 0.25
                  base: 2.718281828459045
              regime_change:
                summary: Market regime shift detection
                value:
                  p:
                    - 0.7
                    - 0.2
                    - 0.08
                    - 0.02
                  q:
                    - 0.3
                    - 0.3
                    - 0.3
                    - 0.1
                  base: 2
      responses:
        '200':
          description: KL divergence calculated successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    type: object
                    properties:
                      kl_divergence:
                        type: number
                        description: >-
                          KL divergence value (0 = identical distributions,
                          higher = more different)
                        example: 0.1438
        '401':
          $ref: '#/components/responses/UnauthorizedError'
        '402':
          $ref: '#/components/responses/InsufficientCreditsError'
        '422':
          $ref: '#/components/responses/ValidationError'
components:
  responses:
    UnauthorizedError:
      description: Authentication information is missing or invalid
      content:
        application/json:
          schema:
            type: object
            properties:
              detail:
                type: string
                example: Invalid API key
    InsufficientCreditsError:
      description: Insufficient API credits
      content:
        application/json:
          schema:
            type: object
            properties:
              detail:
                type: string
                example: Insufficient credits. This endpoint requires 5 credits.
    ValidationError:
      description: Request validation error
      content:
        application/json:
          schema:
            type: object
            properties:
              detail:
                type: array
                items:
                  type: object
                  properties:
                    loc:
                      type: array
                      items:
                        type: string
                    msg:
                      type: string
                    type:
                      type: string
  securitySchemes:
    APIKeyHeader:
      type: apiKey
      in: header
      name: X-API-Key
      description: >-
        API key for authentication. Get your key at
        https://api.fincept.in/auth/register

````