#!/usr/bin/python
# -*- coding: cp1252 -*-
#Author: Piero Toffanin
#BidPlaza Statistics 1.0
#Targeted analysis module
import os, string, dircache, sys, bprebuildindex, copy
def startTargetedAnalysis():
print "Targeted Analysis"
print "---------------------------"
productName = ""
while productName == "":
productName = raw_input("Insert product name: ")
filenames = getFilenamesFromFilter(productName)
if len(filenames) == 0:
print "No matches for " + productName
return
#We want to find:
# - Offerts never appeared
# - Offerts choosed rarely
# - Odd and even offerts ratio
# - Offerts never appeared in between winning offerts
# - Previous winning offerts
# - Offerts based on less choosen digits
c = 0
productName = []
totalOfferts = []
winningOfferts = []
heuristic = []
average = []
offerts = {}
oddcount = evencount = 0
offertsCounted = 0
neverAppearedOfferts = []
quotient = {}
for filename in filenames:
lines = open("odb/" + filename).read().split("\n")
print "Analyzing " + filename + " (" + lines[0] + ")... [" + str(len(lines)-5) + " lines]"
productName.append(lines[0])
totalOfferts.append(int(lines[1]))
winningOfferts.append(float(lines[2]))
heuristic.append(int(lines[3]))
average.append(float(lines[4]))
for d in range(5,len(lines)-1):
offert = lines[d].split(" ")
if offert[1] == "0" and not offert[0] in neverAppearedOfferts:
neverAppearedOfferts.append(offert[0])
if not offerts.has_key(offert[0]):
offerts[offert[0]] = int(offert[1])
else:
old = offerts[offert[0]]
del offerts[offert[0]]
offerts[offert[0]] = old + int(offert[1])
if not quotient.has_key(offert[0]):
quotient[offert[0]] = 1
else:
old = quotient[offert[0]]
del quotient[offert[0]]
quotient[offert[0]] = old + 1
if int(offert[0]) % 2 == 0:
evencount += int(offert[1])
else:
oddcount += int(offert[1])
offertsCounted += int(offert[1])
totOfferts = 0
for item in totalOfferts:
totOfferts += item
earnings = 0
for c in range(0,len(average)):
earnings += totalOfferts[c] * average[c] * 2
earnings /= len(productName)
print str(len(productName)) + " files analyzed"
print "---------------------------------------\n"
print "Total offerts analyzed: " + str(offertsCounted)
print "Guess of real offerts received: " + str(totOfferts)
print "Guess of BidPlaza's earnings for this item: " + str(round(earnings,2)) + " E\n"
buf = "Previous winning offerts: "
winningaverage = 0
#winningOfferts.sort(cmp=lambda x, y: int(x)-int(y))
for wo in winningOfferts:
buf += getPrintableCost(wo) + ", "
winningaverage += wo
winningaverage /= len(winningOfferts)
maximumever = max(winningOfferts)
buf += "\nAverage winning offert: " + getPrintableCost(winningaverage) + "\n"
buf += "Maximum winning offert ever: " + getPrintableCost(maximumever) + "\n"
print buf
#Offerts never appeared
print "(* = in winning average x 2 range)"
buf = "Offerts never appeared: \n"
for item in neverAppearedOfferts:
buf += getPrintableCost(item)
if item < int(winningaverage*2):
buf += " *"
buf += "\n"
print buf
#Offerts choosed rarely
rarelyAppearedOfferts = []
buf = "Offerts choosed rarely: \n"
for k,v in offerts.iteritems():
if v < quotient[k]*3 and int(k) < maximumever + 1:
rarelyAppearedOfferts.append(k)
rarelyAppearedOfferts.sort(cmp=lambda x, y: int(x)-int(y))
for item in rarelyAppearedOfferts:
buf += getPrintableCost(item) + ", "
print buf
#Odd/Even Ratio
print "\nGlobal odd/even ratio: "
percodd = round(float(100 * oddcount / offertsCounted),2)
perceven = round(float(100 * evencount / offertsCounted),2)
print "Odd on total: " + str(percodd) + "%"
print "Even on total: " + str(perceven) + "%\n"
print "Rare offerts odd/even ratio: "
oddcount = evencount = 0
for item in rarelyAppearedOfferts:
if int(item) % 2 == 0:
evencount += 1
else:
oddcount += 1
percodd = round(float(100 * oddcount / len(rarelyAppearedOfferts)),2)
perceven = round(float(100 * evencount / len(rarelyAppearedOfferts)),2)
print "Odd on rare offerts: " + str(percodd) + "%"
print "Even on rare offerts: " + str(perceven) + "%\n"
print "Less choosen digits:"
print "# Thousands Hundreds Decimals Units"
unitCount = [0] * 10
decCount = [0] * 10
hunCount = [0] * 10
thoCount = [0] * 10
for item in rarelyAppearedOfferts:
unitCount[int(item) % 10] += 1
decCount[(int(item) / 10) % 10] += 1
hunCount[(int(item) / 100) % 10] += 1
thoCount[(int(item) / 1000) % 10] += 1
decPerc = [0] * 10
uniPerc = [0] * 10
for c in range(0,10):
print "%d %8d %9d %11d %9d " % (c,int(100 * thoCount[c] / len(rarelyAppearedOfferts)),
int(100 * hunCount[c] / len(rarelyAppearedOfferts)),
int(100 * decCount[c] / len(rarelyAppearedOfferts)),
int(100 * unitCount[c] / len(rarelyAppearedOfferts)))
decPerc[c] = 100 * decCount[c] / len(rarelyAppearedOfferts)
uniPerc[c] = 100 * unitCount[c] / len(rarelyAppearedOfferts)
#Offerts based on less choosen digits
maxDec = max(decCount);
minDec = min(decCount);
averPercDec = ((maxDec + minDec) / 2) * 100 / len(rarelyAppearedOfferts)
correctPercDec = averPercDec
found = 0
inc = 0
while found == 0:
for c in range(0,10):
if correctPercDec in range(decPerc[c]-inc,decPerc[c]+inc):
found = 1
correctPercDec = decPerc[c]
if not found:
inc += 1
maxUni = max(unitCount);
unitCount.remove(maxUni)
maxUni2 = max(unitCount);
#minUni = min(unitCount);
#averPercUni = ((maxUni + minUni) / 2) * 100 / len(rarelyAppearedOfferts)
correctPercUni = maxUni * 100 / len(rarelyAppearedOfferts)
correctPercUni2 = maxUni2 * 100 / len(rarelyAppearedOfferts)
#found = 0
#inc = 0
#while found == 0:
# for c in range(0,10):
# if correctPercUni in range(uniPerc[c]-inc,uniPerc[c]+inc):
# found = 1
# correctPercUni = uniPerc[c]
# if not found:
# inc += 1
buf = "\n(* = in offerts choosed rarely as well)\nSuggested but more risky offerts: "
for hun in range(0,10):
for dec in range(0,10):
if decPerc[dec] == correctPercDec:
for uni in range(0,10):
if uniPerc[uni] in range(correctPercUni-1,correctPercUni+1) or uniPerc[uni] in range(correctPercUni2-1,correctPercUni2+1):
offert = hun*100 + dec*10 + uni
if offert == 0:
continue
buf += getPrintableCost(offert)
if str(offert) in rarelyAppearedOfferts:
buf += " *"
buf += ", "
if hunCount[hun] > 0:
break
print buf
print "\nChances of success with 1 offert: " + str(round(100 / float(totOfferts/len(productName)),5)) + "%\n"
def getPrintableCost(nativeCost):
return str(float(nativeCost)/100) + " E"
def getFilenamesFromFilter(strfilter):
fd = open("odb/" + bprebuildindex.databaseIndexFilename)
lines = fd.read().split("\n")
filesCount = lines[0]
ret = []
for c in range(1,int(filesCount)):
line = lines[c].split("^")
if line[0].find(strfilter) != -1:
ret.append(line[1])
return ret