-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathPMErrorOfParameterFunction.class.st
More file actions
167 lines (147 loc) · 5.66 KB
/
Copy pathPMErrorOfParameterFunction.class.st
File metadata and controls
167 lines (147 loc) · 5.66 KB
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
"
ErrorOfParameterFunction wants a function with parameters as a block and a Collection of x@f(x) points. The independent variable has to be declared first in the block, then the parameters.
ErrorOfParameterFunction>>value: anArrayOfParameters returns a sum of squared errors or similar error measures. it can be used in MultiVariableOptimizers to calculate parameters.
"
Class {
#name : #PMErrorOfParameterFunction,
#superclass : #Object,
#instVars : [
'data',
'function',
'errorType',
'varArray',
'quartile',
'qPosition',
'relative'
],
#category : 'Math-FunctionFit'
}
{ #category : #'instance creation' }
PMErrorOfParameterFunction class >> function: aBlock data: aCollectionOfPoints [
^self new function: aBlock; data: aCollectionOfPoints; yourself
]
{ #category : #accessing }
PMErrorOfParameterFunction >> data [
^data
]
{ #category : #accessing }
PMErrorOfParameterFunction >> data: aCollectionOfPoints [
"a collection of points x@f(x)"
(aCollectionOfPoints isCollection and: [aCollectionOfPoints allSatisfy: [:aPoint| aPoint isPoint]]) ifFalse:
[self error: 'data must be a Collection of Points'].
aCollectionOfPoints ifEmpty: [CollectionIsEmpty signalWith: aCollectionOfPoints].
data := aCollectionOfPoints .
quartile ifNotNil:[ self quartile: quartile] .
^data
]
{ #category : #accessing }
PMErrorOfParameterFunction >> errorCollection: parameters [
"returns a collection of squared errors or of abs errors"
"this is used often and should and could be a bit optimized"
|y y1|
parameters withIndexDo: [ :p :i | varArray at: i + 1 put: p ].
^data
collect: [ :point |
| err|
varArray at: 1 put: point x.
y := point y.
y1 :=function valueWithArguments: varArray.
err :=y1-y.
relative
ifTrue: [
err := ((y1 = 0)or:[y=0])
ifTrue: [ (y=y1) ifTrue: [0] ifFalse: [1]]
ifFalse: [ errorType = #squared
ifTrue: [ err squared / y1 abs + (err squared /y abs) / 2] "this looks even more stupid than the rest of the 'relative ifTrue:' code, but it is necessary to do it this way because of the possible bias"
ifFalse: [(err / y)abs +(err /y1)abs /2 ] ] ]
ifFalse:[ errorType = #squared
ifTrue: [ err squared ]
ifFalse: [ err abs ] ]].
]
{ #category : #accessing }
PMErrorOfParameterFunction >> errorType [
^errorType
]
{ #category : #accessing }
PMErrorOfParameterFunction >> errorType: aSymbol [
"valid errorTypes are: #squared, #abs, #insensitive, #quartile, #median. Default is #squared"
(aSymbol isSymbol and:[#(#squared #abs #insensitive #quartile #median) includes: aSymbol]) ifFalse:
[^MessageNotUnderstood signal: 'errorType must be one of the following Symbols: #squared, #abs, #insensitive, #quartile, #median'].
(#(#quartile #insensitive) includes: aSymbol)ifFalse: [self quartile: 1/2]. "reset quartile to default, if it is not used"
aSymbol =#median ifTrue: [^errorType :=#quartile] .
^errorType :=aSymbol
]
{ #category : #accessing }
PMErrorOfParameterFunction >> function [
^function
]
{ #category : #accessing }
PMErrorOfParameterFunction >> function: aBlock [
function :=aBlock .
varArray :=Array new: aBlock numArgs.
]
{ #category : #initialization }
PMErrorOfParameterFunction >> initialize [
super initialize .
relative :=false.
errorType :=#squared.
quartile:= 1/2.
]
{ #category : #accessing }
PMErrorOfParameterFunction >> parameterNames [
^(function argumentNames collect:[:s| s asString ])allButFirst
]
{ #category : #accessing }
PMErrorOfParameterFunction >> parameterSize [
^varArray size -1
]
{ #category : #printing }
PMErrorOfParameterFunction >> printOn: aStream [
super printOn: aStream.
aStream
nextPutAll: '( function: '; print: function;
nextPutAll: ' relativeError: '; print: relative;
nextPutAll: ' errorType: '; print: errorType.
(#(#quartile #insensitive) includes: errorType)ifTrue:[
aStream nextPutAll: ' withQuartile: '; print: quartile].
aStream nextPut: $).
]
{ #category : #accessing }
PMErrorOfParameterFunction >> quartile [
^quartile
]
{ #category : #accessing }
PMErrorOfParameterFunction >> quartile: aFloat [
"quartile: is used by errortypes #quartile and #insensitive."
aFloat >1 | (aFloat <0) ifTrue:[^DomainError signal:'quartile must be between 0 and 1'].
data ifNotNil: [qPosition :=(data size -1.00001 * aFloat )rounded+1]. "-1.00001 because x.5 is rounded up to x+1 and pharos median rounds the position down"
^quartile := aFloat .
]
{ #category : #evaluating }
PMErrorOfParameterFunction >> realValue: parameters [
"returns the sqrt of the mean of the sum of squared errors, or the mean abs error, or the quartile error, or the insensitive error"
|e|
e:=self value: parameters .
(#(#insensitive #quartile) includes: errorType) ifFalse: [e :=e / data size] .
errorType = #squared ifTrue: [e := e sqrt] . "if relativeError=true this is not really correct at the moment because of the way value: is calculated, but value: cant be changed! if anything, the calc here could be changed."
^e
]
{ #category : #accessing }
PMErrorOfParameterFunction >> relativeError: aBoolean [
" default is false"
^relative :=aBoolean
]
{ #category : #evaluating }
PMErrorOfParameterFunction >> value: parameters [
"returns the sum of squared errors, or the sum of abs errors, or the quartile error. insensitive is an experimental quartile error that centers a tube around its quartile and returns the (estimated) radius of the tube like quartile"
| e|
e := self errorCollection: parameters .
errorType = #quartile
ifTrue: [ ^ e asSortedCollection at: qPosition ]
ifFalse: [
errorType = #insensitive
ifTrue: [
e := e asSortedCollection copyFrom: 1 to: (qPosition + 1 min: e size).
e := e asOrderedCollection reverse withIndexCollect: [ :v :i | v / (2 raisedToInteger: i) ] ] ].
^ e sum
]