Skip to content

Commit 78f3dff

Browse files
committed
Added Multi task GP regression model, Laplacian Co Regionalization kernel
1 parent 1f257cd commit 78f3dff

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

dynaml-core/scripts/probModels.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ val post = c_model.posterior(350)
1717
histogram((1 to 2000).map(_ => p.sample()))
1818
histogram((1 to 2000).map(_ => post.sample()))
1919

20-
val data = new Gaussian(-2.0, 3.25).sample(3000).toStream
20+
val data = new Gaussian(-2.0, 3.25).sample(2000).toStream
2121
histogram(data)
2222
title("Histogram of data")
2323

@@ -32,7 +32,7 @@ val iidPrior = IIDRandomVarDistr(prior) _
3232
scatter(iidPrior(1000).sample())
3333
hold()
3434
val likelihood = DataPipe((params: (Double, Double)) =>
35-
IIDRandomVarDistr(RandomVariable(new Gaussian(params._1, params._2)))(3000))
35+
IIDRandomVarDistr(RandomVariable(new Gaussian(params._1, params._2)))(2000))
3636

3737
val gModel = ProbabilityModel(prior, likelihood)
3838

dynaml-core/src/main/scala-2.11/io/github/mandar2812/dynaml/kernels/LaplacianKernel.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,17 @@ class LaplaceCovFunc(private var beta: Double)
4545
override def gradient(x: Double, y: Double): Map[String, Double] =
4646
Map("beta" -> 1.0*evaluate(x,y)*math.abs(x-y)/math.pow(state("beta"), 2))
4747
}
48+
49+
class CoRegLaplaceKernel(bandwidth: Double) extends LocalSVMKernel[Int] {
50+
51+
override val hyper_parameters: List[String] = List("coRegLB")
52+
53+
state = Map("coRegLB" -> bandwidth)
54+
55+
override def gradient(x: Int, y: Int): Map[String, Double] =
56+
Map("coRegLB" -> 1.0*evaluate(x,y)*math.abs(x-y)/math.pow(state("coRegLB"), 2))
57+
58+
override def evaluate(x: Int, y: Int): Double = {
59+
math.exp(-1.0*math.abs(x-y)/state("coRegLB"))
60+
}
61+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package io.github.mandar2812.dynaml.models.gp
2+
3+
import breeze.linalg.{DenseMatrix, DenseVector}
4+
import io.github.mandar2812.dynaml.kernels.CovarianceFunction
5+
import org.apache.log4j.Logger
6+
7+
/**
8+
* @author mandar2812 date: 11/8/16.
9+
*
10+
* Abstract implementation of multi-task gaussian process
11+
* as outlined in Lawrence et. al 2012 on arxiv
12+
* @tparam I The index set of the GP.
13+
*/
14+
class MTGPRegressionModel[I](
15+
cov: CovarianceFunction[(I, Int), Double, DenseMatrix[Double]],
16+
n: CovarianceFunction[(I, Int), Double, DenseMatrix[Double]],
17+
data: Seq[Stream[(I, Double)]],
18+
num: Int, numOutputs: Int) extends
19+
AbstractGPRegressionModel[
20+
Seq[Stream[(I, Double)]],
21+
(I, Int)](cov, n, data, num*numOutputs) {
22+
23+
assert(
24+
data.length == numOutputs,
25+
"Number of outputs in data should match numOutputs constructor variable"
26+
)
27+
28+
private val logger = Logger.getLogger(this.getClass)
29+
30+
val noutputs = numOutputs
31+
32+
/**
33+
* Convert from the underlying data structure to
34+
* Seq[(I, Y)] where I is the index set of the GP
35+
* and Y is the value/label type.
36+
**/
37+
override def dataAsSeq(data: Seq[Stream[(I, Double)]]): Seq[((I, Int), Double)] =
38+
data.zipWithIndex.map((patternSet) =>
39+
patternSet._1.map(patternAndLabel => ((patternAndLabel._1, patternSet ._2), patternAndLabel._2))
40+
).reduceLeft((s1, s2) => s1 ++ s2)
41+
}

dynaml-core/src/main/scala-2.11/io/github/mandar2812/dynaml/probability/ProbabilityModel.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ DistL <: Density[Domain]](
2121

2222
val likelihood: DataPipe[ConditioningSet, RandomVarWithDistr[Domain, DistL]] = c
2323

24-
var Max_Candidates: Int = 10000
24+
var Max_Candidates: Int = 1000
25+
26+
var Max_Estimations: Int = 100000
2527

2628
override val underlyingDist = new Density[(ConditioningSet, Domain)] {
2729
override def apply(x: (ConditioningSet, Domain)): Double =
@@ -38,9 +40,9 @@ DistL <: Density[Domain]](
3840
val sampl = this.prior.sample
3941
val q = this.prior.underlyingDist
4042

41-
val M = (1 to Max_Candidates).map(i => {
43+
val M = (1 to Max_Estimations).map(i => {
4244
likelihood(sampl()).underlyingDist(data)
43-
}).sum/Max_Candidates.toDouble
45+
}).sum/Max_Estimations.toDouble
4446

4547
val postD = new Density[ConditioningSet] {
4648
override def apply(x: ConditioningSet): Double =

0 commit comments

Comments
 (0)