def simulate(c_switch): data = [] in_house = 1 third_party = in_house * 0.80 switching = in_house * c_switch weights = np.linspace(0.1, 1, 100) for weight in weights: g = Graph() g.from_dict({ 1: {'payoff': 0, 'after': []}, 2: {'payoff': 0, 'after': [{'node_id': 1, 'cost': in_house}]}, 3: {'payoff': 0, 'after': [{'node_id': 2, 'cost': in_house}]}, 4: {'payoff': 0, 'after': [{'node_id': 1, 'cost': third_party}]}, 5: {'payoff': 0, 'after': [{'node_id': 4, 'cost': third_party}]}, 6: {'payoff': 0, 'after': [{'node_id': 4, 'cost': switching, 'weight': weight}]}, 7: {'payoff': 0, 'after': [{'node_id': 6, 'cost': in_house}, {'node_id': 5, 'cost': 2 * switching, 'weight': weight}]}, 8: {'payoff': 0, 'after': [{'node_id': 3, 'cost': in_house}]}, 9: {'payoff': 0, 'after': [{'node_id': 5, 'cost': third_party}]}, 10: {'payoff': 0, 'after': [{'node_id': 7, 'cost': in_house}, {'node_id': 5, 'cost': 3 * switching, 'weight': weight}]}, 11: {'payoff': 0, 'after': [{'node_id': 8, 'cost': 0}]}, 12: {'payoff': 0, 'after': [{'node_id': 9, 'cost': 0}]}, 13: {'payoff': 0, 'after': [{'node_id': 10, 'cost': 0}]}, }) options = g.get_options(iters=1000) data.append([weight / (1.0 + weight), options[2] - options[4]]) return data
def build_graph(node_id=None): """ returns a list of petersburg graph objects with a certain object perturbed """ # weights w1, w2, w3, w4, w5, w6 = 10, 10, 10, 10, 10, 10 w7 = random.randint(1, 10) w8 = random.randint(1, 10) w9 = random.randint(1, 10) w10 = random.randint(1, 10) w11 =random.randint(1, 10) # costs c1, c2, c3, c4, c5, c6 = 0, 0, 0, 0, 0, 0 c7 = random.randint(1, 10) c8 = random.randint(1, 10) c9 = random.randint(1, 10) c10 = random.randint(1, 10) c11 = random.randint(1, 10) template = { 1: {'payoff': 0, 'after': []}, 2: {'payoff': 0, 'after': [{'node_id': 1, 'cost': c1, 'weight': w1}]}, 3: {'payoff': 0, 'after': [{'node_id': 1, 'cost': c2, 'weight': w2}]}, 4: {'payoff': 0, 'after': [{'node_id': 3, 'cost': c3, 'weight': w3}]}, 5: {'payoff': 0, 'after': [{'node_id': 3, 'cost': c4, 'weight': w4}]}, 6: {'payoff': 0, 'after': [{'node_id': 3, 'cost': c5, 'weight': w5}]}, 7: {'payoff': 0, 'after': [{'node_id': 4, 'cost': c6, 'weight': w6}]}, 8: {'payoff': 0, 'after': [{'node_id': 4, 'cost': c7, 'weight': w7}]}, 9: {'payoff': 0, 'after': [{'node_id': 5, 'cost': c8, 'weight': w8}]}, 10: {'payoff': 0, 'after': [{'node_id': 5, 'cost': c9, 'weight': w9}]}, 11: {'payoff': 0, 'after': [{'node_id': 6, 'cost': c10, 'weight': w10}]}, 12: {'payoff': 0, 'after': [{'node_id': 6, 'cost': c11, 'weight': w11}]}, } if node_id is None: return [(None, Graph().from_dict(template))] out = [] default = template[node_id]['after'][0]['weight'] for x in np.linspace(0.1, default + 1, 25): template[node_id]['after'][0]['weight'] = x g = Graph() g.from_dict(template) out.append((x, g)) return out
from petersburg import Graph import json __author__ = 'willmcginnis' if __name__ == '__main__': data = [] c_switch = 2 in_house = 1 third_party = in_house * 0.5 switching = in_house * c_switch weight = 0.67 g = Graph() g.from_dict({ 1: {'payoff': 0, 'after': []}, 2: {'payoff': 0, 'after': [{'node_id': 1, 'cost': in_house}]}, 3: {'payoff': 0, 'after': [{'node_id': 2, 'cost': in_house}]}, 4: {'payoff': 0, 'after': [{'node_id': 1, 'cost': third_party}]}, 5: {'payoff': 0, 'after': [{'node_id': 4, 'cost': third_party}]}, 6: {'payoff': 0, 'after': [{'node_id': 4, 'cost': switching, 'weight': weight}]}, 7: {'payoff': 0, 'after': [{'node_id': 6, 'cost': in_house}, {'node_id': 5, 'cost': 2 * switching, 'weight': weight}]}, 8: {'payoff': 0, 'after': [{'node_id': 3, 'cost': in_house}]}, 9: {'payoff': 0, 'after': [{'node_id': 5, 'cost': third_party}]}, 10: {'payoff': 0, 'after': [{'node_id': 7, 'cost': in_house}, {'node_id': 5, 'cost': 3 * switching, 'weight': weight}]}, 11: {'payoff': 0, 'after': [{'node_id': 8, 'cost': 0}]}, 12: {'payoff': 0, 'after': [{'node_id': 9, 'cost': 0}]}, 13: {'payoff': 0, 'after': [{'node_id': 10, 'cost': 0}]}, }) print(g.to_dict_of_dicts())
""" A variation of the st petersburg game where the bank has a limited bankroll, and therefore cannot payout an infinite amount of money on any given hand. """ from petersburg import Graph __author__ = 'willmcginnis' if __name__ == '__main__': for bankroll in [100, 10e6, 10e9, 79200000000]: g = Graph() entrance_fee = 0 gd = {1: {'payoff': 0, 'after': []}, 2: {'payoff': 0, 'after': [{'node_id': 1, 'cost': entrance_fee}]}} nn = 3 idx = 0 payoff = 2 ** (idx + 1) while payoff <= bankroll: node_id = 2 * (idx + 1) gd[nn] = {'payoff': payoff, 'after': [{'node_id': node_id, 'cost': 0, 'weight': 1}]} nn += 1 gd[nn] = {'payoff': 0, 'after': [{'node_id': node_id, 'cost': 0, 'weight': 1}]} nn += 1 idx += 1 payoff = 2 ** (idx + 1) g.from_dict(gd) outcomes = []
from petersburg import Graph __author__ = 'willmcginnis' if __name__ == '__main__': g = Graph() # st petersburg paradox w/ $10 entrance fee, a 1000 dollar bankroll and only 100 possible flips entrance_fee = 10 gd = {1: {'payoff': 0, 'after': []}, 2: {'payoff': 0, 'after': [{'node_id': 1, 'cost': entrance_fee}]}} nn = 3 for idx in range(10): node_id = 2 * (idx + 1) payoff = 2 ** (idx + 1) gd[nn] = {'payoff': payoff, 'after': [{'node_id': node_id, 'cost': 0, 'weight': 1}]} nn += 1 gd[nn] = {'payoff': 0, 'after': [{'node_id': node_id, 'cost': 0, 'weight': 1}]} nn += 1 g.from_dict(gd) outcomes = [] for _ in range(1000): outcome = g.get_outcome(iters=1000, ruin=True, starting_bank=1000) outcomes.append(outcome) print(float(sum(outcomes))/len(outcomes)) print(min(outcomes)) print(max(outcomes))
from petersburg import Graph __author__ = 'willmcginnis' if __name__ == '__main__': g = Graph() # decide or research then decide (w/o switching) g.from_dict({ 1: {'payoff': 0, 'after': []}, 2: {'payoff': 0, 'after': [{'node_id': 1, 'cost': 10, 'weight': 2}]}, 3: {'payoff': 0, 'after': [{'node_id': 1, 'cost': 10}]}, 4: {'payoff': 0, 'after': [{'node_id': 1, 'cost': 10, 'weight': 1.5}]}, 5: {'payoff': 0, 'after': [{'node_id': 2, 'cost': 5}, {'node_id': 3, 'cost': 10}]}, 6: {'payoff': 0, 'after': [{'node_id': 2, 'cost': 5}, {'node_id': 4, 'cost': 10}]}, 7: {'payoff': 25, 'after': [{'node_id': 5, 'cost': 0}]}, 8: {'payoff': 15, 'after': [{'node_id': 5, 'cost': 0}]}, 9: {'payoff': 25, 'after': [{'node_id': 6, 'cost': 0}]}, 10: {'payoff': 15, 'after': [{'node_id': 6, 'cost': 0}]}, }) print(g.adjacency_matrix()) # # outcomes = [] # for _ in range(100000): # outcomes.append(g.get_outcome()) # # print('\n\nSimulated Output With Random Start') # print(float(sum(outcomes))/len(outcomes)) # # print('\n\nSimulated Profit of Each Starting Move')
import numpy as np import matplotlib.pyplot as plt import matplotlib.style matplotlib.style.use('ggplot') import pandas as pd from petersburg import Graph __author__ = 'willmcginnis' if __name__ == '__main__': g = Graph() # necktie paradox g.from_dict({ 1: {'payoff': 0, 'after': []}, 2: {'payoff': 0, 'after': [{'node_id': 1}]}, 3: {'payoff': 0, 'after': [{'node_id': 1}]}, 4: {'payoff': 100, 'after': [{'node_id': 2, 'cost': 0}, {'node_id': 3, 'cost': 0}]}, 5: {'payoff': 50, 'after': [{'node_id': 3, 'cost': 0}, {'node_id': 2, 'cost': 0}]}, }) data_2 = [] data_3 = [] for iter in [5, 10, 50, 100, 500, 1000, 5000, 10000, 50000, 1000000]: outcomes = g.get_options(iters=iter, extended_stats=True) data_2.append([ outcomes[2]['count'], outcomes[2]['mean'], outcomes[2]['min'], outcomes[2]['max'] ])
import pandas as pd from petersburg import Graph import matplotlib.pyplot as plt import matplotlib.style matplotlib.style.use('ggplot') __author__ = 'willmcginnis' if __name__ == '__main__': g = Graph() # necktie paradox g.from_dict({ 1: {'payoff': 0, 'after': []}, 2: {'payoff': 0, 'after': [{'node_id': 1, 'cost': 40}]}, 3: {'payoff': 0, 'after': [{'node_id': 1, 'cost': 50}]}, 4: {'payoff': 50, 'after': [{'node_id': 2, 'cost': 0}]}, 5: {'payoff': 40, 'after': [{'node_id': 3, 'cost': 0}]}, }) data = [] for iter in [5, 10, 50, 100, 500, 1000, 5000, 10000, 50000, 1000000]: outcomes = [] for _ in range(iter): outcomes.append(g.get_outcome()) data.append([iter, float(sum(outcomes))/len(outcomes), min(outcomes), max(outcomes) ])