!cat showcases.2011.csv
,"Sep. 19","Sep. 20","Sep. 21","Sep. 22","Sep. 23","Sep. 26","Sep. 27","Sep. 28","Sep. 29","Sep. 30","Oct. 3","Oct. 4","Oct. 5","Oct. 7","Oct. 10","Oct. 11","Oct. 12","Oct. 13","Oct. 14","Oct. 17","Oct. 18","Oct. 19","Oct. 20","Oct. 21","Oct. 24","Oct. 25","Oct. 26","Oct. 27","Oct. 28","Oct. 31","Nov. 1","Nov. 2","Nov. 3","Nov. 4","Nov. 7","Nov. 8","Nov. 9","Nov. 10","Nov. 11","Nov. 14","Nov. 15","Nov. 16","Nov. 17","Nov. 18","Nov. 21","Nov. 22","Nov. 23","Nov. 28","Nov. 29","Nov. 30","Dec. 1","Dec. 2","Dec. 5","Dec. 6","Dec. 7","Dec. 8","Dec. 9","Dec. 12","Dec. 13","Dec. 14","Dec. 15","Dec. 16","Dec. 19","Dec. 20","Dec. 21","Dec. 22","Dec. 23","Dec. 26","Dec. 30","Jan. 2","Jan. 3","Jan. 4","Jan. 5","Jan. 6","Jan. 9","Jan. 10","Jan. 11","Jan. 12","Jan. 13","Jan. 16","Jan. 18","Jan. 19","Jan. 20","Jan. 23","Jan. 24","Jan. 25","Jan. 26","Jan. 27","Jan. 30","Jan. 31","Feb. 1","Feb. 2","Feb. 3","Feb. 6","Feb. 7","Feb. 8","Feb. 9","Feb. 10","Feb. 13","Feb. 14","Feb. 15","Feb. 16","Feb. 17","Feb. 20","Feb. 21","Feb. 22","Feb. 23","Feb. 24","Feb. 27","Feb. 28","Feb. 29","Mar. 1","Mar. 2","Mar. 5","Mar. 6","Mar. 7","Mar. 8","Mar. 9","Mar. 12","Mar. 13","Mar. 14","Mar. 19","Mar. 20","Mar. 21","Mar. 22","Mar. 23","Mar. 26","Mar. 27","Mar. 28","Mar. 29","Mar. 30","Apr. 2","Apr. 3","Apr. 4","Apr. 5","Apr. 6","Apr. 9","Apr. 10","Apr. 11","Apr. 12","Apr. 13","Apr. 16","Apr. 17","Apr. 18","Apr. 19","Apr. 20","Apr. 23","Apr. 24","Apr. 25","Apr. 26","Apr. 27","Apr. 30","May 1","May 2","May 3","May 4","May 7","May 8","May 9","May 10","May 11","May 14","May 15","May 16","May 17","May 18","May 21","May 22","May 23","May 24","May 25","May 28","May 29","May 30","May 31","Jun. 1","Jun. 4","Jun. 5","Jun. 6","Jun. 7","Jun. 8","Jun. 11","Jun. 12","Jun. 13","Jun. 14","Jun. 15","Jun. 18","Jun. 19","Jul. 4","Aug. 17","Sep. 4" ,"5631K","5632K","5633K","5634K","5635K","5641K","5642K","5643K","5644K","5645K","5681K","5682K","5683K","5754K","5651K","5652K","5653K","5654K","5655K","5671K","5672K","5673K","5674K","5675K","5781K","5783K","5782K","5784K","5785K","5691K","5692K","5693K","5694K","5695K","5701K","5702K","5703K","5704K","5705K","5711K","5712K","5713K","5714K","5715K","5721K","5722K","5723K","5731K","5732K","5733K","5734K","5735K","5741K","5742K","5743K","5744K","5745K","5751K","5752K","5753K","5684K","5755K","5761K","5762K","5763K","5764K","5765K","5802K","5775K","5662K","5665K","5663K","5664K","5661K","5791K","5792K","5793K","5795K","5794K","5821K","5803K","5804K","5805K","5811K","5812K","5813K","5814K","5815K","5801K","5822K","5823K","5824K","5825K","5831K","5832K","5833K","5834K","5835K","5841K","5842K","5843K","5844K","5845K","5852K","5853K","5851K","5854K","5855K","5861K","5862K","5863K","5864K","5865K","5871K","5872K","5873K","5874K","5875K","5881K","5882K","5883K","5891K","5892K","5893K","5894K","5895K","5901K","5902K","5903K","5904K","5905K","5911K","5912K","5913K","5915K","5914K","5941K","5942K","5943K","5944K","5945K","5931K","5932K","5933K","5934K","5935K","5921K","5922K","5923K","5924K","5925K","5951K","5952K","5953K","5954K","5955K","5961K","5962K","5963K","5964K","5965K","5983K","5972K","5995K","5974K","5975K","5971K","5982K","5981K","5984K","5985K","5991K","5992K","5993K","5994K","5973K","6001K","6002K","6003K","6004K","6005K","6012K","6011K","6013K","6014K","6015K","6024K","5685K","6021K","6022K","6023K" ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, "Showcase 1",50969,21901,32815,44432,24273,30554,20963,28941,25851,28800,37703,34774,25268,44044,32516,30827,40374,27698,23590,38625,24026,37449,34268,22281,22890,24226,25448,32525,36799,27380,25903,31769,22853,23202,20721,28451,26154,27019,51959,35811,24885,24648,30707,23744,45078,20773,30892,29971,37423,33593,24866,24628,24827,29248,24818,28762,31838,22268,38892,22599,31705,23299,21394,22589,30573,33009,19612,28550,49937,36915,33400,34516,31516,51108,23981,26383,39801,39201,19563,20418,28465,22356,24150,20425,23926,20860,35526,20912,27426,26393,25329,22434,31933,23986,24729,21747,27273,21681,29324,35874,30397,27310,28414,32535,22886,29842,35954,27883,31268,21563,39642,21862,33499,24313,26809,29674,21711,25618,35504,23327,34581,25918,20418,26511,28288,30348,34428,26732,29631,30253,30256,21399,30988,26536,39924,26254,29137,32666,30706,34805,33343,29671,20408,30406,20990,32391,20828,36439,31785,27663,30291,33480,28714,36803,24281,23841,33844,31809,28309,27332,28957,54579,28649,26854,20962,38787,26738,28958,28604,25269,30624,25366,31328,36813,31434,30321,36414,34736,21707,38297,28996,27690,29426,21711,29307,25263,26993,29390,34920,30323,46638 "Showcase 2",45429,34061,53186,31428,22320,24337,41373,45437,41125,36319,38752,28450,39745,40894,30608,30463,29405,31290,38597,33917,43036,36623,31681,29080,34137,24925,36174,23705,24897,37776,19792,20182,36070,32357,24054,21527,30623,34089,58458,69485,29349,23955,28244,19756,28336,34646,26712,24349,24366,23276,34144,28136,33508,38836,27654,23097,23255,33801,35658,42174,34238,29515,26949,22797,25211,29737,23084,24891,71628,29460,24974,32693,26937,30939,32252,29551,22193,29344,33551,29524,23504,36954,18349,19508,20068,27319,26055,25875,29831,30918,36442,28854,36664,24753,25264,30843,32968,28487,27806,22513,25585,23677,29286,29299,31501,21902,20320,25830,32687,30493,20043,28598,34432,27386,19693,25812,24631,30858,26993,27570,23566,20908,25541,32310,37393,21827,26934,28480,39620,39263,34607,26375,43998,25357,33251,42675,20267,32390,20513,32782,29488,27498,34453,36562,19827,49869,30602,28979,36419,29993,36548,24984,22407,37746,30764,21255,37125,23904,37776,29317,49419,27712,31352,34808,33302,30317,26784,25612,22438,23548,29445,26525,30571,25755,38715,29281,38505,33612,25820,20344,25938,41765,34304,26277,26617,32646,33337,26314,31278,31285,42319 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, "Bid 1",42000,14000,32000,27000,18750,27222,25000,35000,22500,21300,21567,35500,26000,38500,24500,27507,30000,36500,25000,36000,24000,37000,22575,20000,18200,25000,23000,36750,35000,38000,23045,22000,20000,31100,12500,29000,25400,22000,32750,32000,22486,29600,29000,23225,37000,17000,32450,28500,29100,24021,25000,24350,18500,25000,28750,25500,26000,21569,41000,34500,32614,22000,15000,14532,24000,28000,17500,24533,32000,33000,26132,27000,23000,50972,15500,27000,35000,27852,28500,22000,24500,19000,27000,25000,20000,18500,23000,20000,19000,34000,24000,22575,21995,27000,22500,26400,27000,20000,31250,38000,25000,26000,26000,23000,22100,29300,22500,22000,27000,18500,24240,16500,23000,19500,23000,24075,17500,5500,24000,18358,27000,16522,24622,21500,22475,25000,28000,18000,27000,32000,26550,20950,43200,22500,22135,21000,27237,24500,23000,33000,22000,22000,18200,18550,23990,27200,22000,15500,32500,18500,27000,27500,26576,27000,22000,28900,22253,27000,22000,26000,36000,40000,30450,25500,22000,23000,33000,24500,24000,35000,26121,21000,25700,30000,27000,31000,23723,28000,17500,27000,23500,23000,27000,19000,25300,25500,41000,23052,27800,27000,29900 "Bid 2",34000,59900,45000,38000,23000,18525,32000,45000,32000,27500,23800,18975,35555,35000,29765,34000,23000,25000,24000,42333,27000,24500,27500,29500,25000,28000,22000,27500,22000,37000,22500,19900,32100,27590,20000,26500,20000,28000,58000,48000,30100,21000,22750,26500,27000,27500,26000,24250,26250,31000,28500,25000,20500,34000,24250,33250,15000,29250,25700,30000,34000,28950,26420,22000,27500,23456,21000,30500,28000,25200,19401,25000,28000,32310,27500,30000,23000,28000,30000,22750,33000,24500,20400,27000,25000,25000,24859,30100,23500,24150,33500,36500,32000,24000,27890,22000,27717,31000,26400,21000,27100,24000,27500,23695,23200,21500,21000,26535,19500,26500,26000,31860,25000,25496,24500,31000,23000,17000,34800,29000,21000,24000,23000,25000,27500,22525,23500,21000,38000,30000,32000,22000,35000,23000,23374,28999,26000,27500,21100,30777,18000,24000,36100,24000,18000,33057,28000,25500,28000,23000,23100,18200,24500,35000,22000,18605,31229,20000,34000,28300,38675,18522,33500,22000,26262,24500,23000,25000,22013,18500,28000,24050,31000,31000,32000,30972,28543,38000,26000,22300,29972,36000,30000,24750,28000,26800,26888,16000,30022,21000,33000 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, "Difference 1",8969,7901,815,17432,5523,3332,-4037,-6059,3351,7500,16136,-726,-732,5544,8016,3320,10374,-8802,-1410,2625,26,449,11693,2281,4690,-774,2448,-4225,1799,-10620,2858,9769,2853,-7898,8221,-549,754,5019,19209,3811,2399,-4952,1707,519,8078,3773,-1558,1471,8323,9572,-134,278,6327,4248,-3932,3262,5838,699,-2108,-11901,-909,1299,6394,8057,6573,5009,2112,4017,17937,3915,7268,7516,8516,136,8481,-617,4801,11349,-8937,-1582,3965,3356,-2850,-4575,3926,2360,12526,912,8426,-7607,1329,-141,9938,-3014,2229,-4653,273,1681,-1926,-2126,5397,1310,2414,9535,786,542,13454,5883,4268,3063,15402,5362,10499,4813,3809,5599,4211,20118,11504,4969,7581,9396,-4204,5011,5813,5348,6428,8732,2631,-1747,3706,449,-12212,4036,17789,5254,1900,8166,7706,1805,11343,7671,2208,11856,-3000,5191,-1172,20939,-715,9163,3291,5980,2138,9803,2281,-5059,11591,4809,6309,1332,-7043,14579,-1801,1354,-1038,15787,-6262,4458,4604,-9731,4503,4366,5628,6813,4434,-679,12691,6736,4207,11297,5496,4690,2426,2711,4007,-237,-14007,6338,7120,3323,16738 "Difference 2",11429,-25839,8186,-6572,-680,5812,9373,437,9125,8819,14952,9475,4190,5894,843,-3537,6405,6290,14597,-8416,16036,12123,4181,-420,9137,-3075,14174,-3795,2897,776,-2708,282,3970,4767,4054,-4973,10623,6089,458,21485,-751,2955,5494,-6744,1336,7146,712,99,-1884,-7724,5644,3136,13008,4836,3404,-10153,8255,4551,9958,12174,238,565,529,797,-2289,6281,2084,-5609,43628,4260,5573,7693,-1063,-1371,4752,-449,-807,1344,3551,6774,-9496,12454,-2051,-7492,-4932,2319,1196,-4225,6331,6768,2942,-7646,4664,753,-2626,8843,5251,-2513,1406,1513,-1515,-323,1786,5604,8301,402,-680,-705,13187,3993,-5957,-3262,9432,1890,-4807,-5188,1631,13858,-7807,-1430,2566,-3092,2541,7310,9893,-698,3434,7480,1620,9263,2607,4375,8998,2357,9877,13676,-5733,4890,-587,2005,11488,3498,-1647,12562,1827,16812,2602,3479,8419,6993,13448,6784,-2093,2746,8764,2650,5896,3904,3776,1017,10744,9190,-2148,12808,7040,5817,3784,612,425,5048,1445,2475,-429,-5245,6715,-1691,9962,-4388,-180,-1956,-4034,5765,4304,1527,-1383,5846,6449,10314,1256,10285,9319
import price as pr
data = pr.ReadData('showcases.2011.csv')
data[:5]
[(50969, 45429, 42000, 34000, 8969, 11429), (21901, 34061, 14000, 59900, 7901, -25839), (32815, 53186, 32000, 45000, 815, 8186), (44432, 31428, 27000, 38000, 17432, -6572), (24273, 22320, 18750, 23000, 5523, -680)]
len(data)
191
class Pdf(object):
"""Represents a probability density function (PDF)."""
def Density(self, x):
"""Evaluates this Pdf at x.
Returns: float probability density
"""
raise UnimplementedMethodException()
def MakePmf(self, xs, name=''):
"""Makes a discrete version of this Pdf, evaluated at xs.
xs: equally-spaced sequence of values
Returns: new Pmf
"""
pmf = Pmf(name=name)
for x in xs:
pmf.Set(x, self.Density(x))
pmf.Normalize()
return pmf
class GaussianPdf(Pdf):
"""Represents the PDF of a Gaussian distribution."""
def __init__(self, mu, sigma):
"""Constructs a Gaussian Pdf with given mu and sigma.
mu: mean
sigma: standard deviation
"""
self.mu = mu
self.sigma = sigma
def Density(self, x):
"""Evaluates this Pdf at x.
Returns: float probability density
"""
return EvalGaussianPdf(x, self.mu, self.sigma)
class EstimatedPdf(Pdf):
"""Represents a PDF estimated by KDE."""
def __init__(self, sample):
"""Estimates the density function based on a sample.
sample: sequence of data
"""
self.kde = scipy.stats.gaussian_kde(sample)
def Density(self, x):
"""Evaluates this Pdf at x.
Returns: float probability density
"""
return self.kde.evaluate(x)
prices = ReadData()
pdf = thinkbayes.EstimatedPdf(prices)
low, high = 0, 75000
n = 101
xs = numpy.linspace(low, high, n)
pmf = pdf.MakePmf(xs)
guess = 참가자가 상품을 보고, 각 상품의 금액을 추정하고, 이렇게 구해진 금액을 모두 더한 값.
error = price - guess
diff = price - bid
class Player(object):
"""Represents a player on The Price is Right."""
def __init__(self, prices, bids, diffs):
"""Construct the Player.
prices: sequence of prices
bids: sequence of bids
diffs: sequence of underness (negative means over)
"""
self.pdf_price = thinkbayes.EstimatedPdf(prices)
self.cdf_diff = thinkbayes.MakeCdfFromList(diffs)
mu = 0
sigma = numpy.std(diffs)
self.pdf_error = thinkbayes.GaussianPdf(mu, sigma)
class Price(thinkbayes.Suite):
"""Represents hypotheses about the price of a showcase."""
def __init__(self, pmf, player, name=''):
"""Constructs the suite.
pmf: prior distribution of price
player: Player object
name: string
"""
thinkbayes.Suite.__init__(self, pmf, name=name)
self.player = player
# class Price :
def Likelihood(self, data, hypo):
"""Computes the likelihood of the data under the hypothesis.
hypo: actual price
data: the contestant's guess
"""
price = hypo
guess = data
error = price - guess
like = self.player.ErrorDensity(error)
return like
# class Player:
def ErrorDensity(self, error):
"""Density of the given error in the distribution of error.
error: how much the bid is under the actual price
"""
return self.pdf_error.Density(error)
# class Player:
def MakeBeliefs(self, guess):
"""Makes a posterior distribution based on estimated price.
Sets attributes prior and posterior.
guess: what the player thinks the showcase is worth
"""
pmf = self.PmfPrice()
self.prior = Price(pmf, self, name='prior')
self.posterior = self.prior.Copy(name='posterior')
self.posterior.Update(guess)
# class Player:
n = 101
price_xs = numpy.linspace(0, 75000, n)
def PmfPrice(self):
"""Returns a new Pmf of prices.
A discrete version of the estimated Pdf.
"""
return self.pdf_price.MakePmf(self.price_xs)
class GainCalculator(object):
"""Encapsulates computation of expected gain."""
def __init__(self, player, opponent):
"""Constructs the calculator.
player: Player
opponent: Player
"""
self.player = player
self.opponent = opponent
# class GainCalculator :
def ExpectedGains(self, low=0, high=75000, n=101):
"""Computes expected gains for a range of bids.
low: low bid
high: high bid
n: number of bids to evaluates
returns: tuple (sequence of bids, sequence of gains)
"""
bids = numpy.linspace(low, high, n)
gains = [self.ExpectedGain(bid) for bid in bids]
return bids, gains
# class GainCalculator :
def ExpectedGain(self, bid):
"""Computes the expected return of a given bid.
bid: your bid
"""
suite = self.player.posterior
total = 0
for price, prob in sorted(suite.Items()):
gain = self.Gain(bid, price)
total += prob * gain
return total
# class GainCalculator :
def Gain(self, bid, price):
"""Computes the return of a bid, given the actual price.
bid: number
price: actual price
"""
# if you overbid, you get nothing
if bid > price:
return 0
# otherwise compute the probability of winning
diff = price - bid
prob = self.ProbWin(diff)
# if you are within 250 dollars, you win both showcases
if diff <= 250:
return 2 * price * prob
else:
return price * prob
# class GainCalculator :
def ProbWin(self, diff):
"""Computes the probability of winning for a given diff.
diff: how much your bid was off by
"""
prob = (self.opponent.ProbOverbid() +
self.opponent.ProbWorseThan(diff))
return prob
# class Player :
def ProbOverbid(self):
"""Returns the probability this player overbids.
"""
return self.cdf_diff.Prob(-1)
def ProbWorseThan(self, diff):
"""Probability this player's diff is greater than the given diff.
diff: how much the oppenent is off by (always positive)
"""
return 1 - self.cdf_diff.Prob(diff)
# class Player :
def OptimalBid(self, guess, opponent):
"""Computes the bid that maximizes expected return.
guess: what the player thinks the showcase is worth
opponent: Player
Returns: (optimal bid, expected gain)
"""
self.MakeBeliefs(guess)
calc = GainCalculator(self, opponent)
bids, gains = calc.ExpectedGains()
gain, bid = max(zip(gains, bids))
return bid, gain
def PlotExpectedGains(guess1=20000, guess2=40000):
"""Plots expected gains as a function of bid.
guess1: player1's estimate of the price of showcase 1
guess2: player2's estimate of the price of showcase 2
"""
player1, player2 = MakePlayers()
MakePlots(player1, player2)
player1.MakeBeliefs(guess1)
player2.MakeBeliefs(guess2)
print 'Player 1 prior mle', player1.prior.MaximumLikelihood()
print 'Player 2 prior mle', player2.prior.MaximumLikelihood()
print 'Player 1 mean', player1.posterior.Mean()
print 'Player 2 mean', player2.posterior.Mean()
print 'Player 1 mle', player1.posterior.MaximumLikelihood()
print 'Player 2 mle', player2.posterior.MaximumLikelihood()
player1.PlotBeliefs('price3')
player2.PlotBeliefs('price4')
calc1 = GainCalculator(player1, player2)
calc2 = GainCalculator(player2, player1)
thinkplot.Clf()
thinkplot.PrePlot(num=2)
bids, gains = calc1.ExpectedGains()
thinkplot.Plot(bids, gains, label='Player 1')
print 'Player 1 optimal bid', max(zip(gains, bids))
bids, gains = calc2.ExpectedGains()
thinkplot.Plot(bids, gains, label='Player 2')
print 'Player 2 optimal bid', max(zip(gains, bids))
thinkplot.Save(root='price5',
xlabel='bid ($)',
ylabel='expected gain ($)',
formats=FORMATS)
def PlotOptimalBid():
"""Plots optimal bid vs estimated price.
"""
player1, player2 = MakePlayers()
guesses = numpy.linspace(15000, 60000, 21)
res = []
for guess in guesses:
player1.MakeBeliefs(guess)
mean = player1.posterior.Mean()
mle = player1.posterior.MaximumLikelihood()
calc = GainCalculator(player1, player2)
bids, gains = calc.ExpectedGains()
gain, bid = max(zip(gains, bids))
res.append((guess, mean, mle, gain, bid))
guesses, means, _mles, gains, bids = zip(*res)
thinkplot.PrePlot(num=3)
pyplot.plot([15000, 60000], [15000, 60000], color='gray')
thinkplot.Plot(guesses, means, label='mean')
#thinkplot.Plot(guesses, mles, label='MLE')
thinkplot.Plot(guesses, bids, label='bid')
thinkplot.Plot(guesses, gains, label='gain')
thinkplot.Save(root='price6',
xlabel='guessed price ($)',
formats=FORMATS)
import price as pr
pr.PlotExpectedGains()
Writing price1.png Writing price1.pdf Writing price1.eps Player median 3902 Player median 3136 Player 1 overbids 0.246006389776 Player 2 overbids 0.290734824281 Writing price2.png Writing price2.pdf Writing price2.eps Player 1 prior mle 27750.0 Player 2 prior mle 27750.0 Player 1 mean 25096.4118162 Player 2 mean 35641.2168697 Player 1 mle 24000.0 Player 2 mle 35250.0 Writing price3.png Writing price3.pdf Writing price3.eps Writing
pr.PlotOptimalBid()
Writing price6.png Writing price6.pdf Writing price6.eps