-
Notifications
You must be signed in to change notification settings - Fork 153
/
ModelParameters.scala
235 lines (215 loc) · 6.92 KB
/
ModelParameters.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
* ModelParameters.scala
* Collections for defining prior and posterior parameters.
*
* Created By: Michael Howard (mhoward@cra.com)
* Creation Date: Oct 29, 2014
*
* Copyright 2017 Avrom J. Pfeffer and Charles River Analytics, Inc.
* See http://www.cra.com or email figaro@cra.com for information.
*
* See http://www.github.com/p2t2/figaro for a copy of the software license.
*/
/*
* Additional Updates from our community
*
* Paul Philips May 23, 2017
*/
package com.cra.figaro.patterns.learning
import com.cra.figaro.language.Element
import com.cra.figaro.language.ElementCollection
import com.cra.figaro.language.Reference.stringToReference
import com.cra.figaro.library.atomic.continuous.AtomicBeta
import com.cra.figaro.library.atomic.continuous.AtomicDirichlet
import scala.collection.mutable.ListBuffer
import com.cra.figaro.language.DoubleParameter
import com.cra.figaro.language.ArrayParameter
import com.cra.figaro.language.Parameter
import argonaut._, Argonaut._
import com.cra.figaro.library.atomic.continuous.Dirichlet
import com.cra.figaro.library.atomic.continuous.Beta
/**
* Case classes defining type parameters of parameter elements.
* These are used for matching on return types of parameter collections, and for
* correctly instantiating elements from the posterior and prior parameters.
*/
abstract class ParameterType
/**
* Parameters whose MAP value is a double
*/
case class PrimitiveDouble(val d: Double) extends ParameterType {
override def toString = d.toString()
}
/**
* Learnable parameters whose MAP value is a double
*/
case class ParameterDouble(val p: Parameter[Double]) extends ParameterType {
override def toString = p.toString()
}
/**
* Parameters whose MAP value is an array of doubles
*/
case class PrimitiveArray(val a: Array[Double]) extends ParameterType {
override def toString = a.toString()
}
/**
* Learnable parameters whose MAP value is an array of doubles
*/
case class ParameterArray(val p: Parameter[Array[Double]]) extends ParameterType {
override def toString = p.toString()
}
object ParameterType {
def apply(d: Double) = new PrimitiveDouble(d)
def apply(p: Parameter[_]) = {
p match {
case b: AtomicBeta => new ParameterDouble(b)
case d: AtomicDirichlet => new ParameterArray(d)
}
}
def apply(a: Array[Double]) = new PrimitiveArray(a)
}
/**
* Defines a collection of prior or posterior parameters, obtained from a set of ModelParameters
*/
trait ParameterCollection {
def get(s: String): ParameterType
}
/**
* A class representing the prior and posterior parameters of a model
*/
class ModelParameters extends ElementCollection {
/**
* Add a parameter to the collection
*/
override def add[T](element: Element[T]) = {
element match {
case p: Parameter[T] => super.add(p)
case default => {
//Do not add non-parameter elements to the set of model parameters.
}
}
}
/**
* Convert the contents of to a list of parameter elements
*/
def convertToParameterList: List[Parameter[_]] = {
val l = ListBuffer.empty[Parameter[_]]
for (p <- this.namedElements) {
p match {
case a: Parameter[_] => {
l += a
}
case default => {
//Do not add non-parameter elements to the result list. This should never be reached.
}
}
}
l.toList
}
private object PriorParameterCollection extends ParameterCollection {
def get(s: String): ParameterType = {
val p = getElementByReference(s)
val result = p match {
case p: Parameter[_] => ParameterType(p)
case default => throw new IllegalArgumentException("Cannot retrieve non-parameter elements from parameter collection.")
}
result
}
def apply(s: String): ParameterType = {
val p = getElementByReference(s)
val result = p match {
case p: Parameter[_] => ParameterType(p)
case default => throw new IllegalArgumentException("Cannot retrieve non-parameter elements from parameter collection.")
}
result
}
}
private object PosteriorParameterCollection extends ParameterCollection {
def get(s: String): ParameterType = {
val p = getElementByReference(s)
val result = p match {
case x: Parameter[_] => {
x match {
case d: DoubleParameter => ParameterType(d.MAPValue)
case a: ArrayParameter => ParameterType(a.MAPValue)
case default => throw new IllegalArgumentException("Cannot retrieve non-parameter elements from parameter collection.")
}
}
}
result
}
/**
* @param The name of a parameter to retrieve from the set of model parameters
*/
def apply(s: String): ParameterType = {
val p = getElementByReference(s)
val result = p match {
case x: Parameter[_] => {
x match {
case d: DoubleParameter => ParameterType(d.MAPValue)
case a: ArrayParameter => ParameterType(a.MAPValue)
case default => throw new IllegalArgumentException("Cannot retrieve non-parameter elements from parameter collection.")
}
}
}
result
}
}
/**
* Get the collection of prior parameters
*/
def priorParameters: ParameterCollection = {
val p = PriorParameterCollection
p
}
/**
* Get the collection of posterior parameters (after learning)
*/
def posteriorParameters: ParameterCollection = {
val p = PosteriorParameterCollection
p
}
}
object ModelParameters {
/**
* Decode JSON into a parameter element
*/
implicit val decodeJson: DecodeJson[Parameter[_]] = DecodeJson { c =>
c.downField("Beta").as[AtomicBeta] |||
c.downField("Dirichlet").as[Parameter[_]]
}
/**
* Encode a set of model parameters into JSON
*/
implicit def ModelParametersEncodeJson: EncodeJson[ModelParameters] = {
EncodeJson((params: ModelParameters) => {
val encodedParameters = for (p <- params.convertToParameterList) yield {
p match {
case b: Beta => ("Beta" := Beta.BetaEncodeJson(b)) ->: jEmptyObject
case d: Dirichlet => ("Dirichlet" := Dirichlet.DirichletEncodeJson(d)) ->: jEmptyObject
case default => throw new IllegalArgumentException("Unserializable parameter type.")
}
}
("allParameters" := jArray(encodedParameters)) ->: jEmptyObject
})
}
/**
* Decode JSON into a set of model parameters
*/
implicit def ModelParametersDecodeJson(implicit collection: ElementCollection): DecodeJson[ModelParameters] =
DecodeJson(c => for {
jsonParameters <- (c --\ "allParameters").as[List[Parameter[_]]]
} yield ModelParameters(jsonParameters))
/**
* Create a new set of model parameters.
*/
def apply() = new ModelParameters()
/**
* Create a new set of model parameters containing the list of parameters provided
*/
def apply(l: List[Parameter[_]]) = {
val m = new ModelParameters()
l.foreach(m.add(_))
m
}
}