Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
dml-mm
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Descartes Research
dml-mm
Commits
ce928ed7
Commit
ce928ed7
authored
7 years ago
by
Long Bui
Browse files
Options
Downloads
Patches
Plain Diff
implement untested variance calculation for RandomVariables
parent
9ce35f2f
No related branches found
No related tags found
1 merge request
!1
Randomvarhelper
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tools.descartes.dml.mm.applicationlevel/src/tools/descartes/dml/mm/applicationlevel/functions/util/RandomVariableHelper.java
+116
-1
116 additions, 1 deletion
...applicationlevel/functions/util/RandomVariableHelper.java
with
116 additions
and
1 deletion
tools.descartes.dml.mm.applicationlevel/src/tools/descartes/dml/mm/applicationlevel/functions/util/RandomVariableHelper.java
+
116
−
1
View file @
ce928ed7
...
...
@@ -16,6 +16,7 @@ import tools.descartes.dml.mm.applicationlevel.functions.IntSampleList;
import
tools.descartes.dml.mm.applicationlevel.functions.NormalDistribution
;
import
tools.descartes.dml.mm.applicationlevel.functions.ProbabilityFunction
;
import
tools.descartes.dml.mm.applicationlevel.functions.ProbabilityMassFunction
;
import
tools.descartes.dml.mm.applicationlevel.functions.ReplayFile
;
import
tools.descartes.dml.mm.applicationlevel.functions.SampleList
;
/**
...
...
@@ -45,11 +46,125 @@ public class RandomVariableHelper {
return
_getNumericalMean
((
BoxedPDF
)
probfn
);
if
(
probfn
instanceof
ProbabilityMassFunction
)
return
_getNumericalMean
((
ProbabilityMassFunction
)
probfn
);
if
(
probfn
instanceof
ReplayFile
)
return
_getNumericalMean
((
ReplayFile
)
probfn
);
return
_getNumericalMean
(
probfn
);
}
public
static
Double
getNumericalVariance
(
ProbabilityFunction
probfn
)
{
if
(
probfn
instanceof
IntLiteral
)
return
_getNumericalVariance
((
IntLiteral
)
probfn
);
if
(
probfn
instanceof
DoubleLiteral
)
return
_getNumericalVariance
((
DoubleLiteral
)
probfn
);
if
(
probfn
instanceof
BooleanLiteral
)
return
_getNumericalVariance
((
BooleanLiteral
)
probfn
);
if
(
probfn
instanceof
ExponentialDistribution
)
return
_getNumericalVariance
((
ExponentialDistribution
)
probfn
);
if
(
probfn
instanceof
NormalDistribution
)
return
_getNumericalVariance
((
NormalDistribution
)
probfn
);
if
(
probfn
instanceof
BoxedPDF
)
return
_getNumericalVariance
((
BoxedPDF
)
probfn
);
if
(
probfn
instanceof
ProbabilityMassFunction
)
return
_getNumericalVariance
((
ProbabilityMassFunction
)
probfn
);
if
(
probfn
instanceof
ReplayFile
)
return
_getNumericalVariance
((
ReplayFile
)
probfn
);
return
_getNumericalVariance
(
probfn
);
}
private
static
Double
_getNumericalVariance
(
EnumSampleList
enumSl
)
{
throw
new
UnsupportedOperationException
(
"Enums do not have a numerical variance."
);
}
private
static
Double
_getNumericalVariance
(
DoubleSampleList
doubleSl
)
{
double
[]
singletons
=
new
double
[
doubleSl
.
getItems
().
size
()];
double
[]
probabilities
=
new
double
[
doubleSl
.
getItems
().
size
()];
for
(
int
i
=
0
;
i
<
doubleSl
.
getItems
().
size
();
i
++)
{
singletons
[
i
]
=
doubleSl
.
getItems
().
get
(
i
).
getValue
().
doubleValue
();
probabilities
[
i
]
=
doubleSl
.
getItems
().
get
(
i
).
getProbability
().
doubleValue
();
}
EnumeratedRealDistribution
dist
=
new
EnumeratedRealDistribution
(
singletons
,
probabilities
);
return
dist
.
getNumericalVariance
();
}
private
static
Double
_getNumericalVariance
(
BoolSampleList
boolSl
)
{
throw
new
UnsupportedOperationException
(
"Booleans do not have a numerical variance."
);
}
private
static
Double
_getNumericalVariance
(
IntSampleList
intSl
)
{
int
[]
singletons
=
new
int
[
intSl
.
getItems
().
size
()];
double
[]
probabilities
=
new
double
[
intSl
.
getItems
().
size
()];
for
(
int
i
=
0
;
i
<
intSl
.
getItems
().
size
();
i
++)
{
singletons
[
i
]
=
intSl
.
getItems
().
get
(
i
).
getValue
().
intValue
();
probabilities
[
i
]
=
intSl
.
getItems
().
get
(
i
).
getProbability
().
doubleValue
();
}
EnumeratedIntegerDistribution
dist
=
new
EnumeratedIntegerDistribution
(
singletons
,
probabilities
);
return
dist
.
getNumericalVariance
();
}
private
static
Double
_getNumericalVariance
(
SampleList
sl
)
{
if
(
sl
instanceof
IntSampleList
)
return
_getNumericalVariance
((
IntSampleList
)
sl
);
else
if
(
sl
instanceof
BoolSampleList
)
return
_getNumericalVariance
((
BoolSampleList
)
sl
);
else
if
(
sl
instanceof
DoubleSampleList
)
return
_getNumericalVariance
((
DoubleSampleList
)
sl
);
else
if
(
sl
instanceof
EnumSampleList
)
return
_getNumericalVariance
((
EnumSampleList
)
sl
);
throw
new
IllegalStateException
(
"Unknown type of SampleList: "
+
sl
.
getClass
().
getName
());
}
private
static
Double
_getNumericalVariance
(
ProbabilityMassFunction
pmf
)
{
return
_getNumericalVariance
(
pmf
.
getSamples
());
}
private
static
Double
_getNumericalVariance
(
BoxedPDF
bpdf
)
{
double
lastValue
=
0
;
double
meanSquaredSamples
=
0.0
;
double
numericalMean
=
_getNumericalMean
(
bpdf
);
for
(
ContinuousSample
cs
:
bpdf
.
getSample
())
{
meanSquaredSamples
+=
cs
.
getProbability
().
doubleValue
()
*
Math
.
pow
((
lastValue
+
(
cs
.
getValue
().
doubleValue
()
-
lastValue
)
/
2
),
2
);
lastValue
=
cs
.
getValue
().
doubleValue
();
}
return
meanSquaredSamples
-
Math
.
pow
(
numericalMean
,
2
);
}
private
static
Double
_getNumericalVariance
(
NormalDistribution
nd
)
{
return
Math
.
pow
(
nd
.
getSigma
().
doubleValue
(),
2
);
}
private
static
Double
_getNumericalVariance
(
ExponentialDistribution
expd
)
{
double
rate
=
expd
.
getRate
().
doubleValue
();
if
(
rate
>
0
)
{
return
1
/
Math
.
pow
(
rate
,
2
);
}
throw
new
IllegalArgumentException
(
"ExponentialDistribution rate must be greater than 0."
);
}
private
static
Double
_getNumericalVariance
(
BooleanLiteral
l
)
{
return
0.0
;
}
private
static
Double
_getNumericalVariance
(
DoubleLiteral
l
)
{
return
0.0
;
}
private
static
Double
_getNumericalVariance
(
IntLiteral
l
)
{
return
0.0
;
}
private
static
Double
_getNumericalVariance
(
ProbabilityFunction
probfn
)
{
throw
new
IllegalStateException
(
"Unknown type of ProbabilityFunction: "
+
probfn
.
getClass
().
getName
());
}
private
static
Double
_getNumericalMean
(
ReplayFile
replay
)
{
double
sum
=
0
;
throw
new
UnsupportedOperationException
(
"File Path: "
+
replay
.
getFilepath
());
}
private
static
Double
_getNumericalMean
(
ProbabilityMassFunction
pmf
)
{
return
RandomVariableHelper
.
_getNumericalMean
(
pmf
.
getSamples
());
return
_getNumericalMean
(
pmf
.
getSamples
());
}
private
static
Double
_getNumericalMean
(
IntSampleList
intsl
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment