def main(random): """ Buy on the dips run <num_runs> simulations tracking output over 20 years plot a return distribution """ # parameters specific to this continuous purchase model title = ("Random" if random else "Real sequence") + " simulations of " monthly = True # monthly simulations # mappings from parameters into point formats colors = { 0.10:"r", 0.15:"y", 0.20:"g", 0.25:"c" } symbols = [ "x", ".", "o", "+", "*" ] legends = [] simulator = Market(monthly=monthly) # for a range of plausible dip thresholds for max_dip in (0.10, 0.15, 0.20, 0.25): # are we willing to buy part on smaller dips for buy_points in [1, 2, 3]: results = [] # a statistically interesting number of runs for runs in range(num_runs * 2 if random else num_runs): sequence = simulator.rates(length=num_years*12, random=random) results.append( strat_dips(sequence, max_dip, buy_points, monthly) ) # summarize the results mean = sum(results) / len(results) sigma = statistics.stdev(results) report = "{}({}%/{}) over {} years: mean={:.2f}, sigma={:.2f}" print(report.format(my_name, int(100*max_dip), buy_points, num_years, mean, sigma)) # bucketize and display the results granularity = bucketwidth(results) buckets = bucketize(results, granularity) (x_values, y_values) = distribution(buckets, granularity) plt.plot(x_values, y_values, colors[max_dip] + symbols[buy_points]) legends.append(str(int(max_dip*100)) + "% dip/" + str(buy_points)) print("") # blank line between changes in threshold # put up the title, axes, and data plt.title(title + my_name) plt.xlabel(str(num_years) + "-year return") plt.ylabel("probability") plt.legend(legends) if output is None: plt.show() else: print("saving distribution plot as " + output) plt.savefig(output) plt.close()
def main(random): """ For all-in and all-out run <num_runs> simulations tracking output over 20 years plot a return distribution """ # parameters specific to this continuous purchase model title = ("Random" if random else "Real sequence") + " simulations of " monthly = False # annual simulations legends = [] simulator = Market(monthly=monthly) # purchases spread out over 1-5 years for in_cds in [True, False]: results = [] # a statistically interesting number of runs for runs in range(num_runs * 2 if random else num_runs): sequence = simulator.rates(length=num_years, random=random) results.append(strat_all(sequence, in_cds, monthly)) # summarize the results mean = sum(results) / len(results) sigma = statistics.stdev(results) report = "{} {}, {} years: mean={:.2f}, sigma={:.2f}" print( report.format(my_name, "CDs" if in_cds else "market", num_years, mean, sigma)) # bucketize and display the results granularity = bucketwidth(results) buckets = bucketize(results, granularity) (x_values, y_values) = distribution(buckets, granularity) plt.plot(x_values, y_values, "go" if in_cds else "b*") legends.append("CDs" if in_cds else "market") # put up the title, axes, and data plt.title(title + my_name) plt.xlabel(str(num_years) + "-year return") plt.ylabel("probability") plt.legend(legends) if output is None: plt.show() else: print("saving distribution plot as " + output) plt.savefig(output) plt.close()
def main(random): """ Only buy in at lows run <num_runs> simulations tracking output over 20 years plot a return distribution """ # parameters specific to this continuous purchase model title = ("Random" if random else "Real sequence") + " simulations of " monthly = True # monthly simulations formats = ["w.", "r.", "y+", "g*", "co"] legends = [] simulator = Market(monthly=monthly) for fractions in [1, 2, 3, 4]: results = [] # a statistically interesting number of runs for runs in range(num_runs * 2 if random else num_runs): sequence = simulator.rates(length=num_years * 12, random=random) results.append(strat_bottom(sequence, fractions, monthly)) # summarize the results mean = sum(results) / len(results) sigma = statistics.stdev(results) report = "{} over {} years in {} pieces: mean={:.2f}, sigma={:.2f}" print(report.format(my_name, num_years, fractions, mean, sigma)) # bucketize and display the results granularity = bucketwidth(results) buckets = bucketize(results, granularity) (x_values, y_values) = distribution(buckets, granularity) plt.plot(x_values, y_values, formats[fractions]) legends.append("fractions=" + str(fractions)) # put up the title, axes, and data plt.title(title + my_name) plt.xlabel(str(num_years) + "-year return") plt.ylabel("probability") plt.legend(legends) if output is None: plt.show() else: print("saving distribution plot as " + output) plt.savefig(output) plt.close()