-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
218 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sholtzen@wifi-131-179-3-172.host.ucla.edu.13952 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<instantiation date="Jun 4, 2005 7:07:21 AM"> | ||
<inst id="0" value="false"/> | ||
</instantiation> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from sage.all import * | ||
import numpy.random | ||
import random | ||
from my_graphs import * | ||
|
||
def flip(): | ||
return numpy.random.binomial(1, 0.5) | ||
|
||
class FactorGraph: | ||
### graph: a sage graph | ||
### variables: a list of graph vertices which correspond to variables in the factor graph | ||
### factors: a list of list of graph vertices which correspond to colored factors in the factor graph | ||
### potential: state -> real: a function which evaluates the potential on a particular state | ||
### a state is a dictionary assigning variables to Boolean values | ||
def __init__(self, graph, variables, factors, potential): | ||
self.graph = graph | ||
self.variables = variables | ||
self.factors = factors | ||
self.potential = potential | ||
|
||
### converts a variable state into a variable partition | ||
def state_to_partition(self, state): | ||
var_part_1 = [] | ||
var_part_2 = [] | ||
for v,val in state.iteritems(): | ||
if val == True: | ||
var_part_1.append(v) | ||
else: | ||
var_part_2.append(v) | ||
return [var_part_1, var_part_2] | ||
|
||
### computes a burnside process transition beginning from a state particular state | ||
### n: number of moves | ||
def burnside(self, state, n): | ||
print("state: %s" % state) | ||
for j in range(0,n): | ||
var_part = self.state_to_partition(state) | ||
partition = var_part + self.factors | ||
stab = self.graph.automorphism_group(partition=partition) | ||
stab = libgap(stab) | ||
# product replacement | ||
p = libgap.PseudoRandom(stab).sage() | ||
# now convert p into a state | ||
state = dict() | ||
for cyc in p.to_cycles(): | ||
# sample a value for cyc and fill in the new current value | ||
v = flip() | ||
for idx in cyc: | ||
if (idx - 1) in self.variables: | ||
#-1, zero indexed | ||
state[idx-1] = v | ||
return state | ||
|
||
### perform a single step of orbit jumping | ||
### returns: a pair, (the ratio of transition probabilities, new state) | ||
### n: number of burnside steps to take | ||
def orbitjump(self, state, n): | ||
hatx = self.burnside(state, n) | ||
probx = self.potential(state) | ||
probhatx = self.potential(hatx) | ||
orbx = self.graph.automorphism_group(partition=self.state_to_partition(state)).order() | ||
orbhatx = self.graph.automorphism_group(partition=self.state_to_partition(hatx)).order() | ||
transitionprob = (probhatx * orbx) / (probx * orbhatx) | ||
return (transitionprob, hatx) | ||
|
||
### draw n samples according to orbit jump MCMC with no orbital MCMC | ||
### burnsidesize: number of burnside steps to take | ||
### returns a list of samples | ||
def orbitjumpmcmc(self, burnsidesize, n): | ||
samples = [] | ||
# set up initial random state | ||
cur_state = dict() | ||
for v in self.variables: | ||
cur_state[v] = flip() | ||
|
||
for i in range(0, n): | ||
(ratio, new_state) = self.orbitjump(cur_state, burnsidesize) | ||
# accept with transition probability | ||
acceptprob = numpy.minimum(1, ratio) | ||
if numpy.random.binomial(1, acceptprob): | ||
samples.append(new_state) | ||
cur_state = new_state | ||
else: | ||
samples.append(cur_state) | ||
|
||
return samples | ||
|
||
|
||
|
||
def gen_complete_pairwise_factorgraph(n): | ||
(g, (v, factors)) = gen_complete_pairwise_factor(n) | ||
def potential(state): | ||
print("potential got state: %s" % state) | ||
p = 0.0 | ||
for v in state.itervalues(): | ||
if v: | ||
p += 1 | ||
return p | ||
return FactorGraph(g, v, [factors], potential) | ||
|
||
def main(): | ||
graph = gen_complete_pairwise_factorgraph(4) | ||
print(graph.orbitjumpmcmc(4, 10)) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import sys | ||
import itertools | ||
|
||
def findsubsets(S,m): | ||
return set(itertools.combinations(S, m)) | ||
|
||
|
||
n = 24 | ||
print("MARKOV") | ||
print(n) | ||
for i in range(0,n): | ||
sys.stdout.write("2 " ) | ||
print("") | ||
# print factor description | ||
subsets = findsubsets(range(0,n), 2) | ||
print(len(subsets)) | ||
for (a,b) in subsets: | ||
print("2 %s %s " % (a,b)) | ||
print("") | ||
# print tables | ||
for i in range(0, len(subsets)): | ||
print("4") | ||
print(" 0.2 0.3") | ||
print(" 0.3 0.2") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters