-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGibbs-Sampling.py
174 lines (149 loc) · 6.29 KB
/
Gibbs-Sampling.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
# -*- coding: utf-8 -*-
"""Assignment6.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/19Bsvrgl7FgtC-8Iq3Tw78wNp9WOScpoT
"""
#!/usr/bin/env python3
# coding: utf-8
# Gibbs-Sampling procedure to compute the Probability Matrix of a Discrete-Time Markov
# Chain whose states are the d-dimensional cartesian product of the form
# {0,1,...n-1} x {0,1,...n-1} x ... X {0,1,...n-1} (i.e. d-many products)
#
# The target stationary distribution is expressed over the n**d many states
#
# Written by Prof. R.S. Sreenivas for
# IE531: Algorithms for Data Analytics
#
import sys
import argparse
import random
import numpy as np
import time
import math
import matplotlib.pyplot as plt
import itertools as it
# need this to keep the matrix print statements to 4 decimal places
np.set_printoptions(formatter={'float': lambda x: "{0:0.4f}".format(x)})
# This function computes a random n-dimensional probability vector (whose entries sum to 1)
def generate_a_random_probability_vector(n) :
x = []
for i in range(n-1):
x.extend([np.random.uniform()])
x = np.sort(x)
y = [x[0]]
for i in range(1, n-1):
y.extend([x[i]-x[i-1]])
y.extend([1.0-x[n-2]])
return y
# Two d-tuples x and y are Gibbs-Neighbors if they are identical, or they differ in value at just
# one coordinate
def check_if_these_states_are_gibbs_neighbors(x, y) :
# x and y are dim-tuples -- we will assume they are not equal
# count the number of coordinates where they differ
if (x == y):
return True
diff = 0
for a, b in zip(x, y):
if (a!=b):
diff = diff+1
if(diff > 1):
return False
return True
# Given two Gibbs-Neighbors -- that are not identical -- find the coordinate where they differ in value
# this is the "free-coordinate" for this pair
def free_coordinates_of_gibbs_neighbors(x, y) :
# we assume x and y are gibbs neighbors, then the must agree on at least (dim-1)-many coordinates
# or, they will disagree on just one of the (dim)-many coordinates... we have to figure out which
# coordinate/axes is free
count = 0
for a, b in zip(x, y):
count = count +1
if (a!=b):
return(count -1)
free_index = -1
return free_index
# x in a dim-tuple (i.e. if dim = 2, it is a 2-tuple; if dim = 4, it is a 4-tuple) state of the Gibbs MC
# each of the dim-many variables in the dim-tuple take on values over range(n)... this function returns
# the lexicographic_index (i.e. dictionary-index) of the state x
def get_lexicographic_index(x, n, dim) :
number = 0
for cnt,i in enumerate(x):
number = number + n**(dim-cnt-1)*i
return number
# This is an implementaton of the Gibbs-Sampling procedure
# The MC has n**dim many states; the target stationary distribution is pi
# The third_variable_is when set to True, prints the various items involved in the procedure
# (not advisable to print for large MCs)
def create_gibbs_MC(n, dim, pi, do_want_to_print) :
if (do_want_to_print) :
print ("Generating the Probability Matrix using Gibbs-Sampling")
print ("Target Stationary Distribution:")
for x in it.product(range(n), repeat = dim) :
number = get_lexicographic_index(x, n, dim)
print ("\u03C0", x, " = \u03C0(", number , ") = ", pi[number])
# the probability matrix will be (n**dim) x (n**dim)
probability_matrix = [[0 for x in range(n**dim)] for y in range(n**dim)]
# the state of the MC is a dim-tuple (i.e. if dim = 2, it is a 2-tuple; if dim = 4, it is a 4-tuple)
# got this from https://stackoverflow.com/questions/7186518/function-with-varying-number-of-for-loops-python
for x in it.product(range(n), repeat = dim) :
# x is a dim-tuple where each variable ranges over 0,1,...,n-1
for y in it.product(range(n), repeat = dim) :
if (check_if_these_states_are_gibbs_neighbors(x, y) == False):
continue;
if(x == y):
continue;
ind_x = get_lexicographic_index(x, n, dim)
ind_y = get_lexicographic_index(y, n, dim)
free_index = free_coordinates_of_gibbs_neighbors(x, y)
z = list(x)
sum = 0
for i in range(n):
z[free_index] = i
ind_z = get_lexicographic_index(z, n, dim)
sum = sum + pi[ind_z]
probability_matrix[ind_x][ind_y] = 1/dim*(pi[ind_y]/sum)
#completing diagonal entries for self-loops
for i in range(n**dim):
total = 0
for j in range(n**dim):
total = total + probability_matrix[i][j]
probability_matrix[i][i] = 1 - total
return probability_matrix
# Trial 1... States: {(0,0), (0,1), (1,0), (1,1)} (i.e. 4 states)
n = 2
dim = 2
a = generate_a_random_probability_vector(n**dim)
print("(Random) Target Stationary Distribution\n", a)
p = create_gibbs_MC(n, dim, a, True)
print ("Probability Matrix:")
print (np.matrix(p))
print ("Does the Probability Matrix have the desired Stationary Distribution?", np.allclose(np.matrix(a), np.matrix(a)* np.matrix(p)))
# Trial 2... States{(0,0), (0,1),.. (0,9), (1,0), (1,1), ... (9.9)} (i.e. 100 states)
n = 10
dim = 2
a = generate_a_random_probability_vector(n**dim)
p = create_gibbs_MC(n, dim, a, False)
print ("Does the Probability Matrix have the desired Stationary Distribution?", np.allclose(np.matrix(a), np.matrix(a)* np.matrix(p)))
# Trial 3... 1000 states
n = 10
dim = 3
t1 = time.time()
a = generate_a_random_probability_vector(n**dim)
p = create_gibbs_MC(n, dim, a, False)
t2 = time.time()
hours, rem = divmod(t2-t1, 3600)
minutes, seconds = divmod(rem, 60)
print ("It took ", hours, "hours, ", minutes, "minutes, ", seconds, "seconds to finish this task")
print ("Does the Probability Matrix have the desired Stationary Distribution?", np.allclose(np.matrix(a), np.matrix(a)* np.matrix(p)))
# Trial 4... 10000 states
n = 10
dim = 4
t1 = time.time()
a = generate_a_random_probability_vector(n**dim)
p = create_gibbs_MC(n, dim, a, False)
t2 = time.time()
hours, rem = divmod(t2-t1, 3600)
minutes, seconds = divmod(rem, 60)
print ("It took ", hours, "hours, ", minutes, "minutes, ", seconds, "seconds to finish this task")
print ("Does the Probability Matrix have the desired Stationary Distribution?", np.allclose(np.matrix(a), np.matrix(a)* np.matrix(p)))