Skip to content

Commit

Permalink
orbital mcmc
Browse files Browse the repository at this point in the history
  • Loading branch information
SHoltzen committed Mar 1, 2019
1 parent d2453e2 commit 8f14e42
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
def flip():
return numpy.random.binomial(1, 0.5)

### returns a random element from a group g using product replacement
def fast_random_element(g):
stab = libgap(g)
return (libgap.PseudoRandom(stab).sage(parent=g)).cycle_tuples(singletons=True)


class MarkovModel:
### graph: a sage graph
### variables: a list of graph vertices which correspond to variables in the factor graph
Expand Down Expand Up @@ -107,13 +113,8 @@ def burnside(self, state, n):
var_part = self.state_to_partition(state)
partition = var_part
stab = self.graph.automorphism_group(partition=partition)
p = stab.random_element().cycle_tuples(singletons=True)
# stab = libgap(stab)
# p = (libgap.PseudoRandom(stab).sage()).to_cycles(singletons=True)
# if len(p) == 0:
# # this is the identity; for some reason, Sage doesn't do this one correctly
# for v in self.variables:
# p += [(v,)]
p = fast_random_element(stab)
#p = stab.random_element().cycle_tuples(singletons=True)

# now convert p into a state
# print(p)
Expand Down Expand Up @@ -157,6 +158,15 @@ def orbitgibbs(self, state):

new_v = numpy.random.binomial(1, prob)
state[v] = new_v

# now walk along the orbit
g = fast_random_element(self.graph_aut)
# apply g to the state
for cyc in g:
fst = state[cyc[0]]
for var in cyc:
state[var] = state[(var + 1) % len(cyc)]
state[cyc[-1]] = fst
return state


Expand Down

0 comments on commit 8f14e42

Please sign in to comment.