def run(n, N, pmin, pmax): '''This function executes n iterations of the digital marketplace auctioning game. Keyword args: n -- number of iterations N -- number of bidders pmin -- minimum price/cost of the service pmax -- maximum price/cost of the service ''' # Run the simulation for n times #intersections = [simulate(N, pmin, pmax) for i in range(n)] intersections = [] no_intersection = 0 for i in range(n): sim = simulate(N, pmin, pmax) while(sim == 0.0): no_intersection += 1 sim = simulate(N, pmin, pmax) intersections.append(sim) # Compute the mean and std of the distribution avg_intersect = np.mean(intersections) std_intersect = np.std(intersections) med_intersect = np.median(intersections) # Compute the probability of an intersection occurring prob_intersect = 1 - no_intersection/(len(intersections)+no_intersection) print("Probability of an intersection occurring: {0}".format(prob_intersect)) # Plot the time series plt.figure() plt.plot(np.linspace(1, n, n), intersections, 'b*') plt.xlabel(r"Iterations, $n$") plt.ylabel(r"Intersections, $w$") plt.grid() plt.savefig("time_series_N_{0}.pdf".format(N)) # Plot the histogram fig = plt.figure() ax = fig.add_subplot(111) h = 2*(mstats.idealfourths(intersections)[1]-mstats.idealfourths(intersections)[0])*n**(-1/3) ax.hist(intersections, (max(intersections)-min(intersections))/h, normed=1, facecolor='green') plt.xlabel(r"Intersections, $w$") plt.ylabel(r"Empirical PDF") plt.text(0.65, 1.12, \ "Mean: {0}\nMedian: {1}\nStd: {2}".format(avg_intersect, med_intersect, std_intersect), \ fontsize=13, \ horizontalalignment='left', \ verticalalignment='top', \ transform=ax.transAxes) plt.grid() plt.savefig("hist_N_{0}.pdf".format(N)) # Plot the empirical CDF plt.figure() ecdf = sm.tools.ECDF(intersections) x_axis = np.linspace(min(intersections), max(intersections)) plt.step(x_axis, ecdf(x_axis)) plt.xlabel(r"Intersections, $w$") plt.ylabel(r"Empirical CDF") plt.grid() plt.savefig("ecdf_N_{0}.pdf".format(N))
def test_idealfourths(): # Tests ideal-fourths test = np.arange(100) assert_almost_equal(np.asarray(ms.idealfourths(test)), [24.416667,74.583333],6) test_2D = test.repeat(3).reshape(-1,3) assert_almost_equal(ms.idealfourths(test_2D, axis=0), [[24.416667,24.416667,24.416667], [74.583333,74.583333,74.583333]],6) assert_almost_equal(ms.idealfourths(test_2D, axis=1), test.repeat(2).reshape(-1,2)) test = [0, 0] _result = ms.idealfourths(test) assert_(np.isnan(_result).all())
def test_idealfourths(self): "Tests ideal-fourths" test = np.arange(100) assert_almost_equal(np.asarray(ms.idealfourths(test)), [24.416667,74.583333],6) test_2D = test.repeat(3).reshape(-1,3) assert_almost_equal(ms.idealfourths(test_2D, axis=0), [[24.416667,24.416667,24.416667], [74.583333,74.583333,74.583333]],6) assert_almost_equal(ms.idealfourths(test_2D, axis=1), test.repeat(2).reshape(-1,2)) test = [0,0] _result = ms.idealfourths(test) assert_(np.isnan(_result).all())
def IQR(x): import scipy.stats.mstats as st quarts = st.idealfourths(x) val = quarts[1] - quarts[0] return val
def IQR(x): import scipy.stats.mstats as st quarts = st.idealfourths(x) val = quarts[1]-quarts[0] return val