-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyhmc.py
executable file
·192 lines (157 loc) · 4.27 KB
/
pyhmc.py
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
#! /usr/bin/env python2
import argparse
import math
import numpy as np
import sys
import os.path
import bivariatenormal
import binom
import normal
from iterate import sample
# {{{ parse
parser = argparse.ArgumentParser()
# specifying the sampler
## demo -- just sample from a bivariate normal
parser.add_argument("--demo",
action="store_true",
help="Sample from a bivariate normal in demo mode")
## likelihood
parser.add_argument("likelihood",
nargs="?",
choices=["binomial", "normal"],
metavar="likelihood",
help="Likelihood to use")
## prior
# parser.add_argument("prior",
# nargs="?",
# choices=["flat1", "flat2"],
# metavar="prior",
# help="prior to use")
# sampler parameters
## number of samples
parser.add_argument("-S",
type=int,
default=1000,
help="number of samples")
## number of itegrator steps
parser.add_argument("-L",
type=int,
default=10,
help="number of integrator steps between samples")
## time step
parser.add_argument("-E",
"--epsilon",
type = float,
default=0.1,
help="time step")
## initialize the chain
parser.add_argument("-I",
type=float,
nargs="+",
help="Chain beginning for canonical parameters")
# inputs
## the data specific to the sampler type
parser.add_argument("-Y",
type = float,
nargs="+",
help="Data. Structure depends on likelihood")
parser.add_argument("--infile",
nargs="+",
help="Data. Structure depends on likelihood")
# outputs
## output file
parser.add_argument("-O",
default="output.txt",
help="Output file")
# additional options
## verbosity (for trouble shooting)
parser.add_argument("-V",
action="store_true",
help="Be verbose")
args = parser.parse_args()
try:
if not (args.demo or args.likelihood):
raise Exception
except:
print("Either demo needs to be turned on" +
" or likelihood needs to be set\n")
parser.print_help()
sys.exit(1)
######################################
# Only demo is currently available #
######################################
try:
if not args.demo:
if args.likelihood == "binomial":
sampler = binom.binomial()
elif args.likelihood == "normal":
sampler = normal.normal()
else:
raise Exception
else:
sampler = bivariatenormal.bivariateNormal()
except:
print("Currently only binomial likelihood is available\n" +
"you selected %s" % args.likelihood)
parser.print_help()
sys.exit(1)
# set run parameters
## number of samples
S = args.S
L = args.L
epsilon = args.epsilon
# set data
if args.Y:
y = np.array(args.Y)
else:
y = sampler.defaulty
if args.Y and args.likelihood=="normal":
y = sampler.defaulty
print("using default data, cannot input normal data via command line")
if args.infile:
try:
y = sampler.readdata(args.infile[0])
except:
print("I cannot read the input file %s" % args.infile[0])
sys.exit(1)
# initialize chain
if args.I:
init = args.I
else:
init = sampler.init
# verbosity
verbose = args.V
# output file
outfile = args.O
try:
if os.path.exists(outfile):
raise Exception
else:
pass
except:
print("output file %s already exists" % outfile)
sys.exit(1)
# }}}
# {{{ print options
print("\n")
if not args.demo=="demo":
print("Likelihood: %s" % args.likelihood)
# print("Prior: %s" % args.prior)
if args.demo=="demo":
print("Demo mode: Sampling from a bivariate normal \
with mean %s and standard deviation 1" % y)
print(" Number of samples: %s" % S)
print(" Steps between each sample: %s" % L)
print(" Time step size: %s" % epsilon)
print("\nSampling...")
# }}}
######################
# Perform sampling #
######################
# init should be on familiar scale
qm = sample(y, S, L, epsilon, init, sampler, verbose, sampler.ll)
#################
# save output #
#################
np.savetxt(outfile, qm, delimiter=" ")
# vim:foldmethod=marker