Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bilby
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
lscsoft
bilby
Commits
7bf26c23
There was a problem fetching the pipeline summary.
Commit
7bf26c23
authored
6 years ago
by
Gregory Ashton
Browse files
Options
Downloads
Patches
Plain Diff
Add example on occam factor
parent
91d541db
No related branches found
No related tags found
1 merge request
!86
Add Occam factor
Pipeline
#
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/other_examples/occam_factor_example.py
+93
-0
93 additions, 0 deletions
examples/other_examples/occam_factor_example.py
with
93 additions
and
0 deletions
examples/other_examples/occam_factor_example.py
0 → 100644
+
93
−
0
View file @
7bf26c23
#!/bin/python
"""
"""
from
__future__
import
division
import
tupak
import
numpy
as
np
import
matplotlib.pyplot
as
plt
# A few simple setup steps
label
=
'
occam_factor
'
outdir
=
'
outdir
'
tupak
.
utils
.
check_directory_exists_and_if_not_mkdir
(
outdir
)
sigma
=
1
N
=
100
time
=
np
.
linspace
(
0
,
1
,
N
)
coeffs
=
[
1
,
2
,
3
]
data
=
np
.
polyval
(
coeffs
,
time
)
+
np
.
random
.
normal
(
0
,
sigma
,
N
)
fig
,
ax
=
plt
.
subplots
()
ax
.
plot
(
time
,
data
,
'
o
'
,
label
=
'
data
'
,
color
=
'
C0
'
)
ax
.
plot
(
time
,
np
.
polyval
(
coeffs
,
time
),
label
=
'
true signal
'
,
color
=
'
C1
'
)
ax
.
set_xlabel
(
'
time
'
)
ax
.
set_ylabel
(
'
y
'
)
ax
.
legend
()
fig
.
savefig
(
'
{}/{}_data.png
'
.
format
(
outdir
,
label
))
class
Polynomial
(
tupak
.
Likelihood
):
def
__init__
(
self
,
x
,
y
,
sigma
,
n
):
"""
A Gaussian likelihood for polynomial of degree `n`.
Parameters
----------
x, y: array_like
The data to analyse.
sigma: float
The standard deviation of the noise.
n: int
The degree of the polynomial to fit.
"""
self
.
x
=
x
self
.
y
=
y
self
.
sigma
=
sigma
self
.
N
=
len
(
x
)
self
.
n
=
n
self
.
keys
=
[
'
c{}
'
.
format
(
k
)
for
k
in
range
(
n
)]
self
.
parameters
=
{
k
:
None
for
k
in
self
.
keys
}
def
polynomial
(
self
,
x
,
parameters
):
coeffs
=
[
parameters
[
k
]
for
k
in
self
.
keys
]
return
np
.
polyval
(
coeffs
,
x
)
def
log_likelihood
(
self
):
res
=
self
.
y
-
self
.
polynomial
(
self
.
x
,
self
.
parameters
)
return
-
0.5
*
(
np
.
sum
((
res
/
self
.
sigma
)
**
2
)
+
self
.
N
*
np
.
log
(
2
*
np
.
pi
*
self
.
sigma
**
2
))
def
fit
(
n
):
likelihood
=
Polynomial
(
time
,
data
,
sigma
,
n
)
priors
=
{}
for
i
in
range
(
n
):
k
=
'
c{}
'
.
format
(
i
)
priors
[
k
]
=
tupak
.
core
.
prior
.
Uniform
(
0
,
10
,
k
)
result
=
tupak
.
run_sampler
(
likelihood
=
likelihood
,
priors
=
priors
,
npoints
=
100
,
outdir
=
outdir
,
label
=
label
)
return
result
.
log_evidence
,
result
.
log_evidence_err
,
np
.
log
(
result
.
occam_factor
(
priors
))
fig
,
ax
=
plt
.
subplots
()
log_evidences
=
[]
log_evidences_err
=
[]
log_occam_factors
=
[]
ns
=
range
(
1
,
10
)
for
l
in
ns
:
e
,
e_err
,
o
=
fit
(
l
)
log_evidences
.
append
(
e
)
log_evidences_err
.
append
(
e_err
)
log_occam_factors
.
append
(
o
)
ax
.
errorbar
(
ns
,
log_evidences
-
np
.
max
(
log_evidences
),
yerr
=
log_evidences_err
,
fmt
=
'
-o
'
,
color
=
'
C0
'
)
ax
.
plot
(
ns
,
log_occam_factors
-
np
.
max
(
log_occam_factors
),
'
-o
'
,
color
=
'
C1
'
,
alpha
=
0.5
)
fig
.
savefig
(
'
{}/{}_test
'
.
format
(
outdir
,
label
))
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