Skip to content

Commit

Permalink
Merge pull request #3 from tomaskourim/presentation
Browse files Browse the repository at this point in the history
Presentation
  • Loading branch information
tomaskourim authored May 15, 2020
2 parents 7027d22 + faccc90 commit 2569b33
Show file tree
Hide file tree
Showing 15 changed files with 871 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ venv/
paper/aux_files
simulations_test
out/
paper/A model of random walk with varying transition probabilities.synctex.gz
*.synctex.gz
70 changes: 30 additions & 40 deletions common.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,46 +82,6 @@ def expected_p_t(step: int, p0: float, c_lambda: float, model_type: str) -> floa
return e


# def support_k(i: int, p0: float, c_lambda: float):
# return (expected_p_t(i, p0, c_lambda, "success_punished") * (-3 * c_lambda ** 2 + 4 * c_lambda - 1) + (
# 1 - c_lambda) ** 2)


# def expected_p_t_squared_support_sum(step: int, p0: float, c_lambda: float, model_type: str) -> float:
# e = 0
# for i in range(0, step):
# if model_type == 'success_punished':
# summand = support_k(i, p0, c_lambda) * (3 * c_lambda ** 2 - 2 * c_lambda) ** (step - 1 - i)
# else:
# raise Exception(f'Unexpected walk type: {model_type}')
# e = e + summand
# return e


# def expected_p_t_squared(step: int, p0: float, c_lambda: float, model_type: str) -> float:
# """
# Support function to get the variance
# :param step:
# :param p0:
# :param c_lambda:
# :param model_type:
# :return:
# """
#
# if model_type == 'success_punished':
# support_sum = expected_p_t_squared_support_sum(step, p0, c_lambda, model_type)
# e = p0 ** 2 * (3 * c_lambda ** 2 - 2 * c_lambda) ** step + support_sum if c_lambda != 2 / 3 else 0
# elif model_type == 'success_rewarded':
# e = p0 * ((2 * c_lambda - c_lambda ** 2) ** step * (p0 - 1) + 1)
# elif model_type == 'success_punished_two_lambdas':
# e = 0
# elif model_type == 'success_rewarded_two_lambdas':
# e = 0
# else:
# raise Exception(f'Unexpected walk type: {model_type}')
# return e


def expected_p_t_squared_support_sum(step: int, c_lambda: float, model_type: str) -> float:
support_sum = 0
for i in range(0, step):
Expand Down Expand Up @@ -255,6 +215,20 @@ def exp_s_t_squared(step: int, p0: float, c_lambda: float, s0: int, model_type:
return e


def exp_x_t(step: int, p0: float, c_lambda: float, model_type: str) -> float:
if model_type == 'success_punished':
e = (2 * c_lambda - 1) ** (step - 1) * (2 * p0 - 1)
elif model_type == 'success_rewarded':
e = 2 * p0 - 1
elif model_type == 'success_punished_two_lambdas':
e = 0
elif model_type == 'success_rewarded_two_lambdas':
e = 0
else:
raise Exception(f'Unexpected walk type: {model_type}')
return e


def exp_s_t_array(step_count: int, p0: float, c_lambda: float, s0: int, model_type: str) -> List[float]:
e_array = []
for step in range(0, step_count + 1):
Expand All @@ -278,6 +252,22 @@ def var_s_t_array(step_count: int, p0: float, c_lambda: float, s0: int, model_ty
return var_array


def exp_x_t_array(step_count: int, p0: float, c_lambda: float, model_type: str) -> List[float]:
e_array = []
for step in range(0, step_count + 1):
e_array.append(exp_x_t(step, p0, c_lambda, model_type))
return e_array


def var_x_t_array(step_count: int, p0: float, c_lambda: float, model_type: str) -> List[float]:
var_array = [0] # Var(X_0) = 0
for step in range(1, step_count + 1):
es2 = 1
es = exp_x_t(step, p0, c_lambda, model_type)
var_array.append(es2 - es ** 2)
return var_array


def create_logger():
start_time = datetime.now()

Expand Down
10 changes: 5 additions & 5 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
C_LAMBDA_PAIRS = [[0.5, 0.8], [0.1, 0.5], [0.5, 0.99], [0.99, 0.9]]
START_PROBABILITIES = [0.5, 0.8, 0.9, 0.99]
STEP_COUNTS = [5, 10, 50, 100]
REPETITIONS_OF_WALK_S = [10000]
REPETITIONS_OF_WALK_S = [1000]
REPETITIONS_OF_WALK_SERIES = 100

C_LAMBDAS_TESTING = [0.2, 0.75, 0.95]
C_LAMBDA_PAIRS_TESTING = [[0.5, 0.8], [0.75, 0.9], [0.2, 0.6]]
C_LAMBDAS_TESTING = [0.2, 0.75, 0.95] # , 1
C_LAMBDA_PAIRS_TESTING = [[0.5, 0.8], [0.75, 0.9], [0.2, 0.6]] # , [1, 1]
START_PROBABILITIES_TESTING = [0.99, 0.8, 0.4]
STEP_COUNTS_TESTING = [200]
STEP_COUNTS_TESTING = [1000]

MODEL_TYPES = ['success_punished', 'success_rewarded', 'success_punished_two_lambdas', 'success_rewarded_two_lambdas']
DATA_DIRNAME = "generated_walks"
PREDICTION_TYPES = ["only_lambda", "only_p0", "all_parameters", "everything"]\
PREDICTION_TYPES = ["only_lambda", "only_p0", "all_parameters", "everything"]

OPTIMIZATION_ALGORITHM = 'Nelder-Mead'

Expand Down
13 changes: 13 additions & 0 deletions data_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
REPETITIONS_OF_WALK_S, REPETITIONS_OF_WALK_SERIES


def generate_random_walk_from_rand_array(rand_numbers: np.ndarray, model_type: str, starting_probability: float,
c_lambdas: List[float], walk_steps: int) -> CompleteWalk:
steps = [0] # in the model, probabilities start with p0, but steps with x1
development = [0]
probabilities = [starting_probability]
for i in range(1, walk_steps + 1):
# next step using actual probability
steps.append(-1 if probabilities[i - 1] <= rand_numbers[i - 1] else 1)
development.append(development[i - 1] + steps[i])
probabilities.append(get_current_probability(c_lambdas, probabilities[i - 1], steps[i], model_type))
return CompleteWalk(probabilities, steps, development)


def generate_random_walk(model_type: str, starting_probability: float, c_lambdas: List[float], walk_steps: int) -> \
CompleteWalk:
steps = [0] # in the model, probabilities start with p0, but steps with x1
Expand Down
67 changes: 59 additions & 8 deletions graphs_generation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# used to generate useful graphics

import matplotlib.pyplot as plt
import numpy as np

from common import exp_p_t_array, var_p_t_array, log_time, create_logger, exp_s_t_array, exp_p_s_t_array, var_s_t_array
from common import exp_p_t_array, var_p_t_array, log_time, create_logger, exp_s_t_array, exp_p_s_t_array, var_s_t_array, \
exp_x_t_array, var_x_t_array
from config import MODEL_TYPES, REPETITIONS_OF_WALK_S, \
C_LAMBDAS_TESTING, START_PROBABILITIES_TESTING, STEP_COUNTS_TESTING, C_LAMBDA_PAIRS_TESTING
from data_generation import generate_random_walks, list_walks2list_lists
from data_generation import generate_random_walks, list_walks2list_lists, generate_random_walk_from_rand_array


def main(simulated_property="probability"):
Expand All @@ -24,8 +26,10 @@ def main(simulated_property="probability"):
else:
two_lambda = False
# TODO handle with dignity
min_y = 0 if simulated_property == "probability" else model_min_y[model_index]
max_y = 1 if simulated_property == "probability" else model_max_y[model_index]
min_y = 0 if simulated_property == "probability" else -1.05 if simulated_property == "step" else \
model_min_y[model_index]
max_y = 1 if simulated_property == "probability" else 1.05 if simulated_property == "step" else \
model_max_y[model_index]
for p_index, starting_probability in enumerate(START_PROBABILITIES_TESTING):
plt.subplot(plt_rows, plt_columns, p_index + 1)
plt.title(r'$p_{0}=%.2f$' % starting_probability, fontsize=20)
Expand Down Expand Up @@ -60,6 +64,9 @@ def main(simulated_property="probability"):
ps_all.append(ps_single)
mean = np.mean(ps_all, axis=0)
variance = np.var(ps_all, axis=0)
elif simulated_property == "step":
mean = np.mean(steps, axis=0)
variance = np.var(steps, axis=0)
else:
raise Exception("unexpected property type")
plt.plot(mean, mean_styles[index], label=label)
Expand All @@ -80,6 +87,11 @@ def main(simulated_property="probability"):
s0 = 0
plt.plot(exp_p_s_t_array(step_count, starting_probability, c_lambda, s0, model_type),
expected_styles[index], linewidth=0.7)
elif simulated_property == "step":
plt.plot(exp_x_t_array(step_count, starting_probability, c_lambda, model_type),
expected_styles[index], linewidth=0.7)
plt.plot(var_x_t_array(step_count, starting_probability, c_lambda, model_type),
expected_styles[index], linewidth=0.7)
plt.legend(loc='best', fontsize='xx-large', markerscale=3)
logger.info(
f"Type: {simulated_property}, model {model_type}, steps {step_count}, reps {repetitions}, p0 {starting_probability}, lambda {c_lambda}")
Expand All @@ -88,13 +100,52 @@ def main(simulated_property="probability"):
fig.set_size_inches(18.5, 10.5)
fig.show()
fig.savefig(
f'e_{simulated_property}_{repetitions}_walks_{step_count}_steps_type_{model_type}.pdf',
dpi=100)
f'e_{simulated_property}_{repetitions}_walks_{step_count}_steps_type_{model_type}.pdf', dpi=100)


def single_walk_simulation(model_type):
plt_columns = len(START_PROBABILITIES_TESTING)
plt_rows = 1
step_count = STEP_COUNTS_TESTING[0]
styles = ['g-', 'r-', 'b-', 'k-']
if 'two_lambdas' in model_type:
two_lambda = True
else:
two_lambda = False
# TODO handle with dignity
max_y = 80
min_y = -20

for p_index, starting_probability in enumerate(START_PROBABILITIES_TESTING):
rand_numbers = np.random.uniform(size=step_count)
plt.subplot(plt_rows, plt_columns, p_index + 1)
plt.title(r'$p_{0}=%.2f$' % starting_probability, fontsize=20)
plt.axis([1, step_count, min_y, max_y])
plt.xlabel('steps', fontsize=18)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
for index, c_lambda in enumerate(C_LAMBDAS_TESTING):
if two_lambda:
c_lambdas = C_LAMBDA_PAIRS_TESTING[index]
label = r'$\bar{\lambda}=[%.2f,%.2f]$' % (c_lambdas[0], c_lambdas[1])
else:
c_lambdas = [c_lambda]
label = r'$\lambda=%.2f$' % c_lambda
development = generate_random_walk_from_rand_array(rand_numbers, model_type, starting_probability,
c_lambdas, step_count).development
plt.plot(development, styles[index], label=label)
plt.legend(loc='best', fontsize='xx-large', markerscale=3)
fig = plt.gcf()
fig.set_size_inches(18.5, 10.5)
fig.show()
fig.savefig(f'single_walk_{step_count}_steps_type_{model_type}.pdf', dpi=100)


if __name__ == '__main__':
start_time, logger = create_logger()
main(simulated_property="position")
main(simulated_property="probability")
# main(simulated_property="position")
# main(simulated_property="probability")
# main(simulated_property="step")
# main(simulated_property="p_s")
single_walk_simulation("success_punished")
log_time(start_time, logger)
Loading

0 comments on commit 2569b33

Please sign in to comment.