Advertisement
Guest User

Wheel of time

a guest
Sep 11th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. # Calcualte stats for Wheel of Time series
  2. # Each row is the number of 5,4,3,2,1 rations for a book in the series
  3. # Collected from goodreads.com, amazon.com and amazon.co.uk
  4.  
  5. goodreads = [
  6.     [45132, 32768, 15394, 4462, 2287],  # 1
  7.     [-1,    -1,    -1,    -1,   -1],   # 2
  8.     [31992, 26753, 11780, 2265,  795],  # 3
  9.     [24523, 21922, 10282, 1747,  430],  # 4
  10.     [16119, 16903,  9131, 1749,  371],  # 5
  11.     [12290, 12332,  7866, 1750,  399],  # 6
  12.     [9925, 11312,  8258, 2068,  451],  # 7
  13.     [8841,  9805,  8043, 2511,  542],  # 8
  14.     [8891,  9205,  7464, 2438,  622],  # 9
  15.     [7411,  7549,  6040, 2272,  864],  # 10
  16.     [8959,  8456,  4699, 1202,  321],  # 11
  17.     [14569,  9046,  3169,  676,  363],  # 12
  18.     [14147,  7671,  2337,  451,  292]]  # 13
  19.  
  20. amazoncom = [
  21.     [1279, 404, 188, 138, 157],  # 1
  22.     [393, 139, 37, 13, 15],     # 2
  23.     [292, 122, 35, 23, 17],     # 3
  24.     [275,  93, 39, 17, 19],     # 4
  25.     [204,  97, 54, 27, 24],     # 5
  26.     [227, 83, 55, 35, 35],      # 6
  27.     [335, 187, 116, 73, 54],    # 7
  28.     [361, 330, 379, 352, 466],   # 8
  29.     [331, 302, 191, 184, 185],  # 9
  30.     [199, 146, 253, 396, 1581],  # 10
  31.     [184, 155, 108, 81, 111],   # 11
  32.     [540, 149, 31, 22, 14],     # 12
  33.     [1628, 346, 150, 62, 358]]  # 13
  34.  
  35. amazoncouk = [
  36.     [144, 47, 20, 19, 20],  # 1
  37.     [33, 25, 4, 0, 1],     # 2
  38.     [46, 25, 8, 0, 5],     # 3
  39.     [32, 12, 7, 0, 1],     # 4
  40.     [21, 17, 5, 5, 4],     # 5
  41.     [25, 10, 10, 1, 2],    # 6
  42.     [19, 12, 2, 0, 0],     # 7
  43.     [20, 21, 22, 16, 19],  # 8
  44.     [44, 33, 26, 9, 10],   # 9
  45.     [20, 18, 32, 60, 76],  # 10
  46.     [38, 42, 17, 6, 5],    # 11
  47.     [125, 22, 6, 3, 1],    # 12
  48.     [96, 31, 8, 8, 5]]     # 13
  49.  
  50. import math
  51. import numpy as np
  52. from matplotlib import pyplot as plot
  53.  
  54.  
  55. def histo_stat(haxis, histo):
  56.     """
  57.    Parameters: haxis - x axis, histo - weight for each entry in axis
  58.    Return: mean, standard devation and bin fractions
  59.    """
  60.     sumw = sum(h for h in histo)
  61.     if sumw < 0:
  62.         return 0, 0, []
  63.     ihisto = zip(histo, haxis)
  64.     mean = 1. / sumw * sum(w * x for w, x in ihisto)
  65.     std = math.sqrt(1. / sumw * sum(i[0] * (i[1] - mean) ** 2 for i in ihisto))
  66.     frac = [float(h) / sumw for h in histo]
  67.     return mean, std, frac
  68.  
  69.  
  70. def run(name, data, color):
  71.     """
  72.    Paramters: name - datasetname, data - table with histgrams
  73.    """
  74.     print("----{}----".format(name))
  75.     haxis = range(5, 0, -1)
  76.     stats = []
  77.     for i, histo in enumerate(data):
  78.         mean, std, frac = histo_stat(haxis, histo)
  79.         # Special case with no data
  80.         if name == "Goodreads.com" and i == 1:
  81.             mean = 4.15
  82.         stats += [(mean, std)]
  83.         perc = ",".join("{:>6.1%}".format(fi) for fi in frac)
  84.         print "{:>2}: {:.2f} +- {:.2f} [{}]".format(i + 1, mean, std, perc)
  85.     stats = np.array(stats)
  86.  
  87.     xaxis = range(1, len(data)+1)
  88.     plot.errorbar(xaxis, stats[:, 0], xerr=0.5, yerr=stats[:, 1], color=color, fmt='-o')
  89.     plot.fill_between(xaxis, stats[:, 0] - stats[:, 1],  stats[:, 0] + stats[:, 1], facecolor=color, alpha=0.3)
  90.  
  91. run("Amazon.com", amazoncom, "red")
  92. run("Amazon.co.uk", amazoncouk, "blue")
  93. run("Goodreads.com", goodreads, "green")
  94.  
  95. plot.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement