Skip to content

Commit b341c0c

Browse files
committed
Added Expectation operator for random variables
1 parent a066f07 commit b341c0c

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.mandar2812.dynaml
2+
3+
import scalaxy.streams.optimize
4+
import spire.algebra.InnerProductSpace
5+
6+
/**
7+
* Created by mandar on 11/01/2017.
8+
*/
9+
package object probability {
10+
11+
var candidates: Int = 10000
12+
13+
def E[@specialized(Double) I](rv: RandomVariable[I])(implicit f: InnerProductSpace[I, Double]): I = optimize {
14+
f.divr(
15+
rv.iid(candidates)
16+
.sample()
17+
.reduce(
18+
(x, y) => f.plus(x,y)
19+
),
20+
candidates.toDouble)
21+
}
22+
23+
24+
def E(rv: RandomVariable[Double]): Double = optimize {
25+
rv.iid(candidates).sample().sum/candidates.toDouble
26+
}
27+
28+
29+
}

scripts/randomvariables.sc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import io.github.mandar2812.dynaml.DynaMLPipe._
22
import io.github.mandar2812.dynaml.analysis.{DifferentiableMap, PushforwardMap}
33
import io.github.mandar2812.dynaml.pipes.DataPipe
4-
import io.github.mandar2812.dynaml.probability.{GaussianRV, RandomVariable}
4+
import io.github.mandar2812.dynaml.probability.{E, GaussianRV, RandomVariable}
55
import spire.implicits._
66
import com.quantifind.charts.Highcharts._
77
import io.github.mandar2812.dynaml.probability.distributions.SkewGaussian
@@ -34,3 +34,5 @@ spline(x.toIterable)
3434
unhold()
3535
legend(List("Log Skew Gaussian", "Log Gaussian"))
3636
title("Probability Density Functions")
37+
38+
println("E[Q] = "+E(q))

0 commit comments

Comments
 (0)