Skip to content

Commit 83fc0cf

Browse files
committed
Santa Fe Laser: Added script for mcmc sampling of GP hyper parameters
1 parent 7277e5b commit 83fc0cf

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

scripts/gp_mcmc_santafe.sc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import breeze.linalg.{DenseMatrix, DenseVector}
2+
import breeze.stats.distributions.{ContinuousDistr, Gamma}
3+
import io.github.mandar2812.dynaml.DynaMLPipe._
4+
import io.github.mandar2812.dynaml.analysis.VectorField
5+
import io.github.mandar2812.dynaml.kernels.{DiracKernel, SEKernel}
6+
import io.github.mandar2812.dynaml.modelpipe.GPRegressionPipe
7+
import io.github.mandar2812.dynaml.pipes.{DataPipe, StreamDataPipe}
8+
import io.github.mandar2812.dynaml.probability.MultGaussianRV
9+
import io.github.mandar2812.dynaml.probability.mcmc.HyperParameterMCMC
10+
import io.github.mandar2812.dynaml.utils.GaussianScaler
11+
import com.quantifind.charts.Highcharts._
12+
13+
14+
val deltaT = 4
15+
16+
type Features = DenseVector[Double]
17+
type Data = Stream[(Features, Features)]
18+
19+
implicit val f = VectorField(deltaT)
20+
21+
val kernel = new SEKernel(1.5, 1.5)
22+
val noise_kernel = new DiracKernel(1.0)
23+
24+
val data_size = 500
25+
26+
val scales_flow_stub = identityPipe[(GaussianScaler, GaussianScaler)]
27+
28+
val prepare_data = {
29+
fileToStream >
30+
trimLines >
31+
extractTrainingFeatures(
32+
List(0), Map()
33+
) >
34+
DataPipe((lines: Stream[String]) =>
35+
lines.zipWithIndex.map(couple => (couple._2.toDouble, couple._1.toDouble))
36+
) >
37+
deltaOperation(deltaT, 0) >
38+
StreamDataPipe((r: (Features, Double)) => (r._1, DenseVector(r._2))) >
39+
gaussianScaling >
40+
DataPipe(DataPipe((d: Data) => d.take(data_size)), scales_flow_stub)
41+
}
42+
43+
val create_gp_model = GPRegressionPipe(
44+
(d: Data) => d.toSeq.map(p => (p._1, p._2(0))),
45+
kernel, noise_kernel
46+
)
47+
48+
val model_flow = DataPipe(create_gp_model, scales_flow_stub)
49+
50+
val workflow = prepare_data > model_flow
51+
52+
val (model, scales) = workflow("data/santafelaser.csv")
53+
54+
val num_hyp = model._hyper_parameters.length
55+
val proposal = MultGaussianRV(num_hyp)(DenseVector.zeros[Double](num_hyp), DenseMatrix.eye[Double](num_hyp))
56+
57+
val mcmc = HyperParameterMCMC[model.type, ContinuousDistr[Double]](
58+
model, model._hyper_parameters.map(h => (h, new Gamma(1.0, 1.0))).toMap,
59+
proposal)
60+
61+
val samples = mcmc.iid(500).draw
62+
63+
val samples_se = samples.map(h => (h("bandwidth"), h("amplitude")))
64+
65+
scatter(samples_se)
66+
title("x,y ~ P(sigma, a | Data)")
67+
xAxis("sigma")
68+
yAxis("a")

0 commit comments

Comments
 (0)