Skip to content

Instantly share code, notes, and snippets.

@Sciss
Last active December 11, 2017 10:33
Show Gist options
  • Save Sciss/f1ef0f51a1e688b7b180881674b4c6ea to your computer and use it in GitHub Desktop.
Save Sciss/f1ef0f51a1e688b7b180881674b4c6ea to your computer and use it in GitHub Desktop.
def cauchyCDF(gamma: Double = 1.0, x0: Double = 0.0) = (x: Double) =>
1.0/math.Pi * math.atan((x-x0) / gamma) + 0.5
val f = cauchyCDF()
(-6.0 to 6.0 by 0.1).map(f).plot()
// y - 0.5 = 1.0/math.Pi * math.atan((x-x0) / gamma)
//
// (y - 0.5) * math.Pi = math.atan((x-x0) / gamma)
//
// math.tan((y - 0.5) * math.Pi) = (x-x0) / gamma
//
// math.tan((y - 0.5) * math.Pi) * gamma = x - x0
//
// math.tan((y - 0.5) * math.Pi) * gamma + x0 = x
def genCauchy(gamma: Double = 1.0, x0: Double = 0.0) = {
val y = math.random()
math.tan((y - 0.5) * math.Pi) * gamma + x0
}
val xs = Vector.fill(10000)(genCauchy())
val histo = (-6.0 to 6.0 by 0.1).map { lo => xs.count(x => x >= lo && x < lo + 0.1) }
histo.plot()
val xs2 = Vector.fill(10000)(genCauchy(gamma = 2))
val histo2 = (-6.0 to 6.0 by 0.1).map { lo => xs2.count(x => x >= lo && x < lo + 0.1) }
histo2.plot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment