> ## 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 Jensen-Shannon Divergence

> Calculates the Jensen-Shannon (JS) divergence between two probability distributions. JS divergence is symmetric, bounded (0 to 1), and based on the KL divergence. It's particularly useful when you need a symmetric distance measure between distributions and is more numerically stable than KL divergence.

**Use Cases:**
- Compare similarity between return distributions
- Symmetric portfolio comparison metrics
- Clustering market regimes
- Model selection with symmetric criteria
- Distribution matching in risk modeling

**Formula:** JS(P||Q) = 0.5 * D_KL(P||M) + 0.5 * D_KL(Q||M), where M = 0.5(P+Q)

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



## OpenAPI

````yaml api-specs/physics.json post /quantlib/physics/divergence/js
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/js:
    post:
      tags:
        - quantlib-physics
      summary: Calculate Jensen-Shannon Divergence
      description: >-
        Calculates the Jensen-Shannon (JS) divergence between two probability
        distributions. JS divergence is symmetric, bounded (0 to 1), and based
        on the KL divergence. It's particularly useful when you need a symmetric
        distance measure between distributions and is more numerically stable
        than KL divergence.


        **Use Cases:**

        - Compare similarity between return distributions

        - Symmetric portfolio comparison metrics

        - Clustering market regimes

        - Model selection with symmetric criteria

        - Distribution matching in risk modeling


        **Formula:** JS(P||Q) = 0.5 * D_KL(P||M) + 0.5 * D_KL(Q||M), where M =
        0.5(P+Q)


        **Credits:** 5 credits per request (Pro Tier) [Tier: ENTERPRISE,
        Credits: 10]
      operationId: js_divergence
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - p
                - q
              properties:
                p:
                  type: array
                  items:
                    type: number
                  description: First probability distribution (must sum to 1)
                  example:
                    - 0.4
                    - 0.3
                    - 0.2
                    - 0.1
                q:
                  type: array
                  items:
                    type: number
                  description: Second probability distribution (must sum to 1)
                  example:
                    - 0.3
                    - 0.3
                    - 0.25
                    - 0.15
                base:
                  type: number
                  description: Logarithm base for divergence calculation
                  default: 2.718281828459045
                  example: 2.718281828459045
            examples:
              portfolio_comparison:
                summary: Compare two portfolio allocations
                value:
                  p:
                    - 0.4
                    - 0.3
                    - 0.2
                    - 0.1
                  q:
                    - 0.35
                    - 0.35
                    - 0.2
                    - 0.1
                  base: 2.718281828459045
              strategy_similarity:
                summary: Compare trading strategy outcomes
                value:
                  p:
                    - 0.5
                    - 0.3
                    - 0.15
                    - 0.05
                  q:
                    - 0.45
                    - 0.35
                    - 0.15
                    - 0.05
                  base: 2
      responses:
        '200':
          description: JS divergence calculated successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  success:
                    type: boolean
                    example: true
                  data:
                    type: object
                    properties:
                      js_divergence:
                        type: number
                        description: >-
                          JS divergence value (0 = identical, 1 = maximally
                          different for base 2)
                        example: 0.0234
        '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

````