io.siddhi.extension.execution.streamingml.bayesian.model

## Class NormalDistribution

- java.lang.Object
- - io.siddhi.extension.execution.streamingml.bayesian.model.NormalDistribution

- All Implemented Interfaces:
    Serializable

      public class NormalDistribution
      extends Object

  implements Gaussian distribution.

  sampling uses reparameterization trick [1] to reduce the variance of the gradients reparameterization trick follows q(z) = loc + scale * epsilon, here epsilon ~ N(0, 1)

  [1] https://arxiv.org/abs/1312.6114

  See Also:
    Serialized Form

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `NormalDistribution(org.nd4j.autodiff.samediff.SDVariable loc, org.nd4j.autodiff.samediff.SDVariable scale, org.nd4j.autodiff.samediff.SameDiff sd)` constructs a normal distribution.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `org.nd4j.autodiff.samediff.SDVariable` | `getLoc()`
    `org.nd4j.autodiff.samediff.SDVariable` | `getScale()`
    `org.nd4j.autodiff.samediff.SDVariable` | `klDivergence(io.siddhi.extension.execution.streamingml.bayesian.model.Distribution distribution)` kullback leibler divergence between two normal densities.
    `org.nd4j.autodiff.samediff.SDVariable` | `logProbability(org.nd4j.autodiff.samediff.SDVariable value)` computes the log-probability of normal distribution.
    `org.nd4j.autodiff.samediff.SDVariable` | `sample()` returns a random sample from the distribution.
    `org.nd4j.autodiff.samediff.SDVariable` | `sample(int n)` returns random samples from the distribution.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### NormalDistribution

          public NormalDistribution(org.nd4j.autodiff.samediff.SDVariable loc,
                                    org.nd4j.autodiff.samediff.SDVariable scale,
                                    org.nd4j.autodiff.samediff.SameDiff sd)

      constructs a normal distribution.
      Parameters:
        `loc` - location/mean. Expected a 2-dimensional variable s,t (input_size, output_size)
        `scale` - standard deviation
        `sd` - SameDiff context

  - ### Method Detail

    - #### logProbability

          public org.nd4j.autodiff.samediff.SDVariable logProbability(org.nd4j.autodiff.samediff.SDVariable value)

      computes the log-probability of normal distribution. uses following simplified formula

      log(p(x)) = -log(sqrt[2*PI]*scale)-([x-loc]/[2*scale])^2

      Parameters:
        `value` - x values
      Returns:
        log probability SameDiff var

    - #### sample

          public org.nd4j.autodiff.samediff.SDVariable sample()

      returns a random sample from the distribution.
      Returns:
        a differentiable SameDiff variable

    - #### sample

          public org.nd4j.autodiff.samediff.SDVariable sample(int n)

      returns random samples from the distribution.
      Parameters:
        `n` - number of samples
      Returns:
        a differentiable SameDiff variable

    - #### klDivergence

          public org.nd4j.autodiff.samediff.SDVariable klDivergence(io.siddhi.extension.execution.streamingml.bayesian.model.Distribution distribution)
                                                             throws io.siddhi.core.exception.SiddhiAppCreationException

      kullback leibler divergence between two normal densities. based on following expression

      KL(q(loc_1,scale_1)||p(loc_2,scale_2) = log(scale_2) - log(scale_1) + (scale_1^2 + [loc_1-loc_2]^2)/(2*scale_2^2) + 0.5

      Parameters:
        `distribution` - reference distribution p(x)
      Returns:
        SameDiff variable
      Throws:
        `io.siddhi.core.exception.SiddhiAppCreationException` - if distribution is not Gaussian

    - #### getLoc

          public org.nd4j.autodiff.samediff.SDVariable getLoc()

    - #### getScale

          public org.nd4j.autodiff.samediff.SDVariable getScale()

Copyright © 2025 WSO2. All rights reserved.