Exemplo n.º 1
0
def train(lowerBidLimit=200, upperBidLimit=400, bidIncrement=1):
    print('Training linear bidding strategy model:')

    advertisers = list(set(trainingDF['advertiser']))

    n = len(advertisers) * len(range(lowerBidLimit, upperBidLimit, bidIncrement))
    i = 0
    j = 1

    optimalBaseBidPrices = []
    optimalBaseBids = {}

    for advertiser in advertisers:
        highest = (advertiser, 0, 0)  # tuple containing highest ctr and corresponding bid prices
        for bid in range(lowerBidLimit, upperBidLimit, bidIncrement):
            i += 1
            progress(i, n, '('+str(j)+'/'+str(len(advertisers))+')')
            rows = trainingDF[(trainingDF['advertiser']==advertiser) & (trainingDF['bidprice']<=bid)]
            imps = len(rows)  # imps = number of impressions
            clicks = len(rows[rows['click']==1])
            if imps > 0:
                ctr = clicks / imps  # ctr = click through rate
            else:
                ctr = 0
            if ctr > highest[1]:
                highest = (advertiser, ctr, bid)
        optimalBaseBidPrices.append(highest)
        optimalBaseBids[advertiser] = highest[2]
        j += 1

    for optimalBaseBidPrice in optimalBaseBidPrices:
        print(optimalBaseBidPrice)

    return optimalBaseBids
Exemplo n.º 2
0
def testFunc(trainResults, csv, silent):
	if silent == 1:
		print 'Testing:'

	sums = {}

	bidprices = {}
	data = {}
	data['bidid'] = []
	data['bidprice'] = []

	n = len(csv)

	for i in range(0, n):
		if silent == 1:
			progress(i + 1, n)

		slotprice = csv.slotprice.values[i]
		advertiser = csv.advertiser.values[i]
		bidid = csv.bidid.values[i]

		bidprice = trainResults[advertiser][slotprice]

		data['bidid'].append(bidid)
		data['bidprice'].append(bidprice)

		bidprices[bidid] = bidprice

	return data, bidprices
Exemplo n.º 3
0
def predict(constant_bidprices, validation_set):
    print('Predicting bid prices using constant bidding strategy model:')
    bids = {}
    validationDF = pd.read_csv(validation_set)

    n = len(validationDF)

    for i in range(0, n):
        progress(i + 1, n)
        bidid = validationDF.bidid.values[i]
        advertiser = validationDF.advertiser.values[i]
        constBidPrice = constant_bidprices[advertiser]
        bids[bidid] = constBidPrice

    return bids
Exemplo n.º 4
0
def predict(advertiser_bidrange, validation_set):
    print('Predicting bid prices using random bidding strategy model:')
    bids = {}
    validationDF = pd.read_csv(validation_set)

    n = len(validationDF)

    for i in range(0, n):
        progress(i + 1, n)
        bidid = validationDF.bidid.values[i]
        advertiser = validationDF.advertiser.values[i]
        minBidPrice = advertiser_bidrange[advertiser][0]
        maxBidPrice = advertiser_bidrange[advertiser][1]
        diff = maxBidPrice - minBidPrice
        constBidPrice = random.randint(minBidPrice, maxBidPrice + diff)
        bids[bidid] = constBidPrice

    return bids
Exemplo n.º 5
0
def train(training_set, lowerBidLimit=200, upperBidLimit=400, bidIncrement=1):
    print('Training constant bidding strategy model:')
    trainingDF = pd.read_csv(training_set)

    advertisers = list(set(trainingDF['advertiser']))

    n = len(advertisers) * len(
        range(lowerBidLimit, upperBidLimit, bidIncrement))
    i = 0
    j = 1

    optimalConstBidPrices = []
    optimalConstBids = {}

    for advertiser in advertisers:
        highest = (
            advertiser, 0, 0, 1
        )  # tuple containing highest ctr and corresponding bid prices
        for bid in range(lowerBidLimit, upperBidLimit, bidIncrement):
            i += 1
            progress(i, n, '(' + str(j) + '/' + str(len(advertisers)) + ')')
            rows = trainingDF[(trainingDF['advertiser'] == advertiser)
                              & (trainingDF['bidprice'] <= bid)]
            imps = len(rows)  # imps = number of impressions
            clicks = len(rows[rows['click'] == 1])
            price = sum(rows.payprice.values)
            if imps > 0:
                ctr = clicks / imps  # ctr = click through rate
                cpc = price / clicks  # cost per click
            else:
                ctr = 0
                cpc = 1
            if (ctr > 0) and (ctr / cpc) > (highest[1] / highest[3]):
                highest = (advertiser, ctr, bid, cpc)
        optimalConstBidPrices.append(highest)
        optimalConstBids[advertiser] = highest[2]
        j += 1

    for optimalConstBidPrice in optimalConstBidPrices:
        print(optimalConstBidPrice)

    return optimalConstBids
Exemplo n.º 6
0
def predict(base_bidprices):
    bids = {}

    n = len(validationDF)

    print('Calculating pCTRs using logistic regression:')
    pctrs = getPCTRS(trainingDF, validationDF)

    print('Calculating avgCTR:')
    imps = len(validationDF)  # imps = number of impressions
    clicks = len(validationDF[validationDF['click']==1])
    avgCTR = clicks / imps  # ctr = click through rate

    print('Predicting bid prices using linear bidding strategy model:')
    for i in range(0, n):
        progress(i+1, n)
        bidid = validationDF.bidid.values[i]
        advertiser = validationDF.advertiser.values[i]
        baseBidPrice = int(base_bidprices[advertiser] * pctrs[i] / avgCTR)
        bids[bidid] = baseBidPrice

    return bids
Exemplo n.º 7
0
def train(training_set):
    print('Training random bidding strategy model:')
    trainingDF = pd.read_csv(training_set)

    advertisers = list(set(trainingDF['advertiser']))

    n = len(advertisers)
    i = 0

    advertiserBidRange = {}

    for advertiser in advertisers:
        i += 1
        progress(i, n, '(' + str(i) + '/' + str(len(advertisers)) + ')')
        rows = trainingDF[(trainingDF.advertiser == advertiser)]
        bids = rows.bidprice.values
        minBidPrice = min(bids)
        maxBidPrice = max(bids)
        advertiserBidRange[advertiser] = (minBidPrice, maxBidPrice)

    for advertiser in advertiserBidRange:
        print(advertiser, advertiserBidRange[advertiser])

    return advertiserBidRange
Exemplo n.º 8
0
def training(csv, silent):
	if silent == 1:
		print 'Training:'
	advertisers = list(set(csv['advertiser']))

	data = {}

	for advertiser in advertisers:
		#if silent == 1:
		print advertiser

		data[advertiser] = {}
		advertiserRows = csv[(csv['advertiser'] == advertiser)]

		for slotprice in range(0, 400):
			if silent == 1:
				progress (slotprice + 1, 400)

			rows = advertiserRows[(advertiserRows['slotprice'] == slotprice)]
			clicks = rows[(rows['click'] == 1)]
			
			if len(rows) > 0:
				difference = float(len(rows)) - float(len(clicks))

				avgbid = float(sum(rows['bidprice'].values)) / float(len(rows))
				bidprice = avgbid - difference * float(0.01001)
				# bidprice = avgbid - difference * float(0.01001) + float(len(clicks))

				if bidprice <= float(0):
					# if difference < float(100000):
					# 	bidprice = avgbid - difference * float(0.0000425)
					# 	# bidprice = avgbid - difference * float(0.000043)
					# else:
					# 	bidprice = avgbid - difference * float(0.00000425)
					# 	# bidprice = avgbid - difference * float(0.0000043)

					# data[advertiser][slotprice] = round(bidprice)
					data[advertiser][slotprice] = 0
				else:
					won = rows[(rows['bidprice'] <= round(bidprice))]
					wonClicks = won[(won['click'] == 1)]
					if len(won) > 0:
						ctr = float(len(wonClicks)) / float(len(won))
					else:
						ctr = 0

					newbidprice = round(bidprice) + 1

					new = rows[(rows['bidprice'] <= newbidprice)]
					newClicks = new[(new['click'] == 1)]
					if len(new) > 0:
						ctrMore = float(len(newClicks)) / float(len(new))
					else:
						ctrMore = 0

					# if len(new) > len(won):
					# 	bidprice = bidprice + 1
					if ctr < ctrMore:
						bidprice = bidprice + 1

					data[advertiser][slotprice] = round(bidprice)
			else:
				data[advertiser][slotprice] = 0

	return data