-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Push forward of random variables with defined density
Added classes : 1. DifferentiableMap 2. PushforwardMap
- Loading branch information
1 parent
53a36c5
commit ef42700
Showing
11 changed files
with
126 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
dynaml-core/src/main/scala-2.11/io/github/mandar2812/dynaml/analysis/DifferentiableMap.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.github.mandar2812.dynaml.analysis | ||
|
||
import io.github.mandar2812.dynaml.pipes.DataPipe | ||
|
||
/** | ||
* A [[DataPipe]] which represents a differentiable transformation. | ||
* */ | ||
trait DifferentiableMap[S, D, J] extends DataPipe[S, D] { | ||
|
||
/** | ||
* Returns the Jacobian of the transformation | ||
* at the point x. | ||
* */ | ||
def J(x: S): J | ||
} | ||
|
||
object DifferentiableMap { | ||
def apply[S, D, J](f: (S) => D, j: (S) => J): DifferentiableMap[S, D, J] = | ||
new DifferentiableMap[S, D, J] { | ||
/** | ||
* Returns the Jacobian of the transformation | ||
* at the point x. | ||
**/ | ||
override def J(x: S) = j(x) | ||
|
||
override def run(data: S) = f(data) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
dynaml-core/src/main/scala-2.11/io/github/mandar2812/dynaml/analysis/PushforwardMap.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.github.mandar2812.dynaml.analysis | ||
|
||
import breeze.numerics.log | ||
import breeze.stats.distributions.ContinuousDistr | ||
import io.github.mandar2812.dynaml.pipes.{DataPipe, Encoder} | ||
import io.github.mandar2812.dynaml.probability.{ContinuousDistrRV, RandomVarWithDistr, RandomVariable} | ||
import spire.algebra.Field | ||
|
||
/** | ||
* @author mandar2812 on 22/12/2016. | ||
* | ||
* Push forward map is a function that has a well defined inverse | ||
* as well as Jacobian of the inverse. | ||
*/ | ||
abstract class PushforwardMap[ | ||
@specialized(Double) Source, | ||
@specialized(Double) Destination, | ||
@specialized(Double) Jacobian]( | ||
implicit detImpl: DataPipe[Jacobian, Double], field: Field[Destination]) | ||
extends Encoder[Source, Destination] { self => | ||
/** | ||
* Represents the decoding/inverse operation. | ||
*/ | ||
override val i: DifferentiableMap[Destination, Source, Jacobian] | ||
|
||
def ->[R <: ContinuousDistrRV[Source]](r: R) | ||
: RandomVarWithDistr[Destination, ContinuousDistr[Destination]] = | ||
RandomVariable(new ContinuousDistr[Destination] { | ||
override def unnormalizedLogPdf(x: Destination) = | ||
r.underlyingDist.unnormalizedLogPdf(i(x)) + log(detImpl(i.J(x))) | ||
|
||
override def logNormalizer = r.underlyingDist.logNormalizer | ||
|
||
override def draw() = self.run(r.underlyingDist.draw()) | ||
}) | ||
|
||
} | ||
|
||
object PushforwardMap { | ||
def apply[S, D, J](forward: DataPipe[S, D], reverse: DifferentiableMap[D, S, J])( | ||
implicit detImpl: DataPipe[J, Double], field: Field[D]) = | ||
new PushforwardMap[S, D, J] { | ||
/** | ||
* Represents the decoding/inverse operation. | ||
*/ | ||
override val i = reverse | ||
|
||
override def run(data: S) = forward(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import io.github.mandar2812.dynaml.DynaMLPipe._ | ||
import io.github.mandar2812.dynaml.analysis.{DifferentiableMap, PushforwardMap} | ||
import io.github.mandar2812.dynaml.pipes.DataPipe | ||
import io.github.mandar2812.dynaml.probability.GaussianRV | ||
import spire.implicits._ | ||
import com.quantifind.charts.Highcharts._ | ||
/** | ||
* @author mandar date 22/12/2016. | ||
*/ | ||
|
||
val g = GaussianRV(0.0, 0.25) | ||
|
||
implicit val detImpl = identityPipe[Double] | ||
|
||
val h: PushforwardMap[Double, Double, Double] = PushforwardMap( | ||
DataPipe((x: Double) => math.exp(x)), | ||
DifferentiableMap( | ||
(x: Double) => math.log(x), | ||
(x: Double) => 1.0/x) | ||
) | ||
|
||
val p = h->g | ||
|
||
val x = Array.tabulate[(Double, Double)](100)(n => (n*0.03, p.underlyingDist.pdf(n*0.03))) | ||
spline(x.toIterable) |