cauchy

package
v0.0.0-...-d740340 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cauchy implements a cauchy continuous random variable

Cauchy distribution is a probability distribution parameterized by two parameters, namely, location and scale.

References:

https://en.wikipedia.org/wiki/Cauchy_distribution

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cauchy

type Cauchy struct {
	// contains filtered or unexported fields
}

Cauchy is a continuous random variable for a lognormal probability distribution parameterized by location and scale

Example

Example - Cauchy Distribution

package main

import (
	"fmt"
	"strings"

	"github.com/shivakar/random/distribution/cauchy"
	"github.com/shivakar/random/distribution/normal"
	"github.com/shivakar/random/prng/xorshift128plus"
)

func main() {
	n := normal.New(xorshift128plus.New(20170611), 5.0, 0.5)
	l := cauchy.New(xorshift128plus.New(20170611), 5.0, 0.5)

	var np [10]int
	var lp [10]int

	nrolls := 10000
	nstars := 100
	for i := 0; i < nrolls; i++ {
		v := n.Float64()
		if v >= 0.0 && v < 10.0 {
			np[int(v)]++
		}

		v = l.Float64()
		if v >= 0.0 && v < 10.0 {
			lp[int(v)]++
		}
	}

	fmt.Println("Cauchy Distribution: mu=5.0, sigma=0.5")
	for i := 0; i < 10; i++ {
		v := lp[i] * nstars / nrolls
		fmt.Printf("%2d-%2d: %s (%d)\n", i, i+1,
			strings.Repeat("*", v), v)
	}
	fmt.Println("Normal Distribution: mu=5.0, sigma=0.5")
	for i := 0; i < 10; i++ {
		v := np[i] * nstars / nrolls

		fmt.Printf("%2d-%2d: %s (%d)\n", i, i+1,
			strings.Repeat("*", v), v)
	}

}
Output:
Cauchy Distribution: mu=5.0, sigma=0.5
 0- 1:  (0)
 1- 2: * (1)
 2- 3: ** (2)
 3- 4: ****** (6)
 4- 5: *********************************** (35)
 5- 6: *********************************** (35)
 6- 7: ****** (6)
 7- 8: ** (2)
 8- 9: * (1)
 9-10:  (0)
Normal Distribution: mu=5.0, sigma=0.5
 0- 1:  (0)
 1- 2:  (0)
 2- 3:  (0)
 3- 4: ** (2)
 4- 5: *********************************************** (47)
 5- 6: *********************************************** (47)
 6- 7: ** (2)
 7- 8:  (0)
 8- 9:  (0)
 9-10:  (0)

func New

func New(r prng.Engine, l float64, s float64) *Cauchy

New returns a new Cauchy Distribution

func (*Cauchy) CDF

func (c *Cauchy) CDF(x float64) float64

CDF or cumulative distribution function returns the probability that a real-valued random variable X of the probability distribution will be found to have a value less than or equal to x

For Cauchy distribution: CDF(x) = 0.5 + 1/pi * arctan((x-location)/scale)

func (*Cauchy) Float64

func (c *Cauchy) Float64() float64

Float64 returns the next random number satisfying the underlying probability distribution

For Cauchy distribution, Float64 return a float64 in [-Inf, Inf]

n = location + scale * tan(pi * U(0, 1) - 0.5)

Implemented using an internal Normal Distribution variable and returning exponentiation of the result.

func (*Cauchy) GetParams

func (c *Cauchy) GetParams() []float64

GetParams returns the current parameters of the Distribution

func (*Cauchy) Init

func (c *Cauchy) Init(r prng.Engine, params ...float64)

Init initializes the normal random variable with the supplied PRNG Engine and distribution parameters

Two float64 parameters are required for Cauchy distribution, namely, location and scale

where:

-inf < l < inf and
scale > 0

func (*Cauchy) PDF

func (c *Cauchy) PDF(x float64) float64

PDF or probability distribution function returns the relative likelihood for the random variable to take on the given value

For Cauchy distribution: PDF(x) = 1 / (pi*scale * [ 1 + ((x-location)/scale)^2 ])

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL