Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Bidplaza Statistics - bptargetedanalysis.py

bptargetedanalysis.py

Caricato da: Piero Tofy
Scarica il programma completo

  1. #!/usr/bin/python
  2. # -*- coding: cp1252 -*-
  3. #Author: Piero Toffanin
  4.  
  5. #BidPlaza Statistics 1.0
  6.  
  7. #Targeted analysis module
  8.  
  9. import os, string, dircache, sys, bprebuildindex, copy
  10.  
  11.  
  12. def startTargetedAnalysis():
  13.     print "Targeted Analysis"
  14.     print "---------------------------"
  15.     productName = ""
  16.     while productName == "":
  17.         productName = raw_input("Insert product name: ")
  18.     filenames = getFilenamesFromFilter(productName)
  19.     if len(filenames) == 0:
  20.         print "No matches for " + productName
  21.         return
  22.  
  23.     #We want to find:
  24.     # - Offerts never appeared
  25.     # - Offerts choosed rarely
  26.     # - Odd and even offerts ratio
  27.     # - Offerts never appeared in between winning offerts
  28.     # - Previous winning offerts
  29.     # - Offerts based on less choosen digits
  30.  
  31.     c = 0
  32.     productName = []
  33.     totalOfferts = []
  34.     winningOfferts = []
  35.     heuristic = []
  36.     average = []
  37.     offerts = {}
  38.     oddcount = evencount = 0
  39.     offertsCounted = 0
  40.     neverAppearedOfferts = []
  41.     quotient = {}
  42.     for filename in filenames:
  43.         lines = open("odb/" + filename).read().split("\n")
  44.         print "Analyzing " + filename + " (" + lines[0] + ")... [" + str(len(lines)-5) + " lines]"
  45.         productName.append(lines[0])
  46.         totalOfferts.append(int(lines[1]))
  47.         winningOfferts.append(float(lines[2]))
  48.         heuristic.append(int(lines[3]))
  49.         average.append(float(lines[4]))
  50.  
  51.         for d in range(5,len(lines)-1):
  52.             offert = lines[d].split(" ")
  53.             if offert[1] == "0" and not offert[0] in neverAppearedOfferts:
  54.                 neverAppearedOfferts.append(offert[0])
  55.  
  56.             if not offerts.has_key(offert[0]):
  57.                 offerts[offert[0]] = int(offert[1])
  58.             else:
  59.                 old = offerts[offert[0]]
  60.                 del offerts[offert[0]]
  61.                 offerts[offert[0]] = old + int(offert[1])
  62.  
  63.             if not quotient.has_key(offert[0]):
  64.                 quotient[offert[0]] = 1
  65.             else:
  66.                 old = quotient[offert[0]]
  67.                 del quotient[offert[0]]
  68.                 quotient[offert[0]] = old + 1
  69.              
  70.             if int(offert[0]) % 2 == 0:
  71.                 evencount += int(offert[1])
  72.             else:
  73.                 oddcount += int(offert[1])
  74.             offertsCounted += int(offert[1])
  75.  
  76.     totOfferts = 0
  77.     for item in totalOfferts:
  78.         totOfferts += item
  79.     earnings = 0
  80.     for c in range(0,len(average)):
  81.         earnings += totalOfferts[c] * average[c] * 2
  82.     earnings /= len(productName)
  83.    
  84.     print str(len(productName)) + " files analyzed"
  85.     print "---------------------------------------\n"
  86.    
  87.     print "Total offerts analyzed: " + str(offertsCounted)
  88.     print "Guess of real offerts received: " + str(totOfferts)
  89.     print "Guess of BidPlaza's earnings for this item: " + str(round(earnings,2)) + " E\n"
  90.     buf = "Previous winning offerts: "
  91.     winningaverage = 0
  92.     #winningOfferts.sort(cmp=lambda x, y: int(x)-int(y))
  93.     for wo in winningOfferts:
  94.         buf += getPrintableCost(wo) + ", "
  95.         winningaverage += wo
  96.     winningaverage /= len(winningOfferts)
  97.     maximumever = max(winningOfferts)
  98.     buf += "\nAverage winning offert: " + getPrintableCost(winningaverage) + "\n"
  99.     buf += "Maximum winning offert ever: " + getPrintableCost(maximumever) + "\n"
  100.     print buf
  101.    
  102.  
  103.     #Offerts never appeared
  104.     print "(* = in winning average x 2 range)"
  105.     buf = "Offerts never appeared: \n"
  106.     for item in neverAppearedOfferts:
  107.         buf += getPrintableCost(item)
  108.         if item < int(winningaverage*2):
  109.             buf += " *"
  110.         buf += "\n"
  111.     print buf
  112.  
  113.    
  114.  
  115.     #Offerts choosed rarely
  116.     rarelyAppearedOfferts = []
  117.     buf = "Offerts choosed rarely: \n"
  118.     for k,v in offerts.iteritems():
  119.         if v < quotient[k]*3 and int(k) < maximumever + 1:
  120.             rarelyAppearedOfferts.append(k)
  121.     rarelyAppearedOfferts.sort(cmp=lambda x, y: int(x)-int(y))
  122.     for item in rarelyAppearedOfferts:
  123.         buf += getPrintableCost(item) + ", "
  124.     print buf
  125.  
  126.    
  127.  
  128.     #Odd/Even Ratio
  129.     print "\nGlobal odd/even ratio: "
  130.     percodd = round(float(100 * oddcount / offertsCounted),2)
  131.     perceven = round(float(100 * evencount / offertsCounted),2)
  132.  
  133.     print "Odd on total: " + str(percodd) + "%"
  134.     print "Even on total: " + str(perceven) + "%\n"
  135.  
  136.  
  137.     print "Rare offerts odd/even ratio: "
  138.     oddcount = evencount = 0
  139.     for item in rarelyAppearedOfferts:
  140.         if int(item) % 2 == 0:
  141.             evencount += 1
  142.         else:
  143.             oddcount += 1
  144.     percodd = round(float(100 * oddcount / len(rarelyAppearedOfferts)),2)
  145.     perceven = round(float(100 * evencount / len(rarelyAppearedOfferts)),2)
  146.  
  147.     print "Odd on rare offerts: " + str(percodd) + "%"
  148.     print "Even on rare offerts: " + str(perceven) + "%\n"
  149.  
  150.     print "Less choosen digits:"
  151.     print "#   Thousands   Hundreds   Decimals   Units"
  152.     unitCount = [0] * 10
  153.     decCount = [0] * 10
  154.     hunCount = [0] * 10
  155.     thoCount = [0] * 10
  156.     for item in rarelyAppearedOfferts:
  157.         unitCount[int(item) % 10] += 1
  158.         decCount[(int(item) / 10) % 10] += 1
  159.         hunCount[(int(item) / 100) % 10] += 1
  160.         thoCount[(int(item) / 1000) % 10] += 1
  161.  
  162.     decPerc = [0] * 10
  163.     uniPerc = [0] * 10
  164.    
  165.     for c in range(0,10):
  166.         print "%d %8d %9d %11d %9d " % (c,int(100 * thoCount[c] / len(rarelyAppearedOfferts)),
  167.               int(100 * hunCount[c] / len(rarelyAppearedOfferts)),
  168.               int(100 * decCount[c] / len(rarelyAppearedOfferts)),
  169.               int(100 * unitCount[c] / len(rarelyAppearedOfferts)))
  170.         decPerc[c] = 100 * decCount[c] / len(rarelyAppearedOfferts)
  171.         uniPerc[c] = 100 * unitCount[c] / len(rarelyAppearedOfferts)
  172.  
  173.     #Offerts based on less choosen digits
  174.     maxDec = max(decCount);
  175.     minDec = min(decCount);
  176.     averPercDec = ((maxDec + minDec) / 2) * 100 / len(rarelyAppearedOfferts)
  177.        
  178.     correctPercDec = averPercDec
  179.     found = 0
  180.     inc = 0
  181.     while found == 0:
  182.         for c in range(0,10):
  183.             if correctPercDec in range(decPerc[c]-inc,decPerc[c]+inc):
  184.                 found = 1
  185.                 correctPercDec = decPerc[c]
  186.         if not found:
  187.            inc += 1
  188.  
  189.     maxUni = max(unitCount);
  190.     unitCount.remove(maxUni)
  191.     maxUni2 = max(unitCount);
  192.    
  193.     #minUni = min(unitCount);
  194.     #averPercUni = ((maxUni + minUni) / 2) * 100 / len(rarelyAppearedOfferts)
  195.     correctPercUni = maxUni * 100 / len(rarelyAppearedOfferts)  
  196.     correctPercUni2 = maxUni2 * 100 / len(rarelyAppearedOfferts)
  197.     #found = 0
  198.     #inc = 0
  199.     #while found == 0:
  200.     #    for c in range(0,10):
  201.     #        if correctPercUni in range(uniPerc[c]-inc,uniPerc[c]+inc):
  202.     #            found = 1
  203.     #            correctPercUni = uniPerc[c]
  204.     #        if not found:
  205.     #            inc += 1
  206.  
  207.  
  208.  
  209.     buf = "\n(* = in offerts choosed rarely as well)\nSuggested but more risky offerts: "
  210.     for hun in range(0,10):
  211.  
  212.         for dec in range(0,10):
  213.             if decPerc[dec] == correctPercDec:
  214.                for uni in range(0,10):
  215.                    if uniPerc[uni] in range(correctPercUni-1,correctPercUni+1) or uniPerc[uni] in range(correctPercUni2-1,correctPercUni2+1):
  216.                         offert = hun*100 + dec*10 + uni
  217.                         if offert == 0:
  218.                             continue
  219.                         buf += getPrintableCost(offert)
  220.                         if str(offert) in rarelyAppearedOfferts:
  221.                             buf += " *"
  222.                         buf += ", "
  223.  
  224.  
  225.                
  226.        
  227.         if hunCount[hun] > 0:
  228.             break
  229.     print buf
  230.    
  231.  
  232.     print "\nChances of success with 1 offert: " + str(round(100 / float(totOfferts/len(productName)),5)) + "%\n"
  233.                                                                
  234.      
  235.  
  236.    
  237. def getPrintableCost(nativeCost):
  238.     return str(float(nativeCost)/100) + " E"
  239.    
  240.    
  241. def getFilenamesFromFilter(strfilter):
  242.     fd = open("odb/" + bprebuildindex.databaseIndexFilename)
  243.     lines = fd.read().split("\n")
  244.     filesCount = lines[0]
  245.     ret = []
  246.     for c in range(1,int(filesCount)):
  247.         line = lines[c].split("^")
  248.         if line[0].find(strfilter) != -1:
  249.             ret.append(line[1])
  250.     return ret