bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 1 | ''' |
| 2 | Created on May 19, 2011 |
| 3 | |
| 4 | @author: bungeman |
| 5 | ''' |
| 6 | |
| 7 | import re |
| 8 | import math |
| 9 | |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 10 | # bench representation algorithm constant names |
| 11 | ALGORITHM_AVERAGE = 'avg' |
| 12 | ALGORITHM_MEDIAN = 'med' |
| 13 | ALGORITHM_MINIMUM = 'min' |
| 14 | ALGORITHM_25TH_PERCENTILE = '25th' |
| 15 | |
bensong@google.com | 967b258 | 2013-12-05 01:31:56 +0000 | [diff] [blame] | 16 | # Regular expressions used throughout. |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 17 | PER_SETTING_RE = '([^\s=]+)(?:=(\S+))?' |
| 18 | SETTINGS_RE = 'skia bench:((?:\s+' + PER_SETTING_RE + ')*)' |
| 19 | BENCH_RE = 'running bench (?:\[\d+ \d+\] )?\s*(\S+)' |
epoger@google.com | e657a25 | 2013-08-13 15:12:33 +0000 | [diff] [blame] | 20 | TIME_RE = '(?:(\w*)msecs = )?\s*((?:\d+\.\d+)(?:,\s*\d+\.\d+)*)' |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 21 | # non-per-tile benches have configs that don't end with ']' or '>' |
bensong@google.com | 967b258 | 2013-12-05 01:31:56 +0000 | [diff] [blame] | 22 | CONFIG_RE = '(\S+[^\]>]):\s+((?:' + TIME_RE + '\s+)+)' |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 23 | # per-tile bench lines are in the following format. Note that there are |
| 24 | # non-averaged bench numbers in separate lines, which we ignore now due to |
| 25 | # their inaccuracy. |
| 26 | TILE_RE = (' tile_(\S+): tile \[\d+,\d+\] out of \[\d+,\d+\] <averaged>:' |
| 27 | ' ((?:' + TIME_RE + '\s+)+)') |
| 28 | # for extracting tile layout |
| 29 | TILE_LAYOUT_RE = ' out of \[(\d+),(\d+)\] <averaged>: ' |
| 30 | |
| 31 | PER_SETTING_RE_COMPILED = re.compile(PER_SETTING_RE) |
| 32 | SETTINGS_RE_COMPILED = re.compile(SETTINGS_RE) |
| 33 | BENCH_RE_COMPILED = re.compile(BENCH_RE) |
| 34 | TIME_RE_COMPILED = re.compile(TIME_RE) |
| 35 | CONFIG_RE_COMPILED = re.compile(CONFIG_RE) |
| 36 | TILE_RE_COMPILED = re.compile(TILE_RE) |
| 37 | TILE_LAYOUT_RE_COMPILED = re.compile(TILE_LAYOUT_RE) |
| 38 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 39 | class BenchDataPoint: |
| 40 | """A single data point produced by bench. |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 41 | """ |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 42 | def __init__(self, bench, config, time_type, time, settings, |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 43 | tile_layout='', per_tile_values=[], per_iter_time=[]): |
| 44 | # string name of the benchmark to measure |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 45 | self.bench = bench |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 46 | # string name of the configurations to run |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 47 | self.config = config |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 48 | # type of the timer in string: '' (walltime), 'c' (cpu) or 'g' (gpu) |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 49 | self.time_type = time_type |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 50 | # float number of the bench time value |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 51 | self.time = time |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 52 | # dictionary of the run settings |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 53 | self.settings = settings |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 54 | # how tiles cover the whole picture: '5x3' means 5 columns and 3 rows |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 55 | self.tile_layout = tile_layout |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 56 | # list of float for per_tile bench values, if applicable |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 57 | self.per_tile_values = per_tile_values |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 58 | # list of float for per-iteration bench time, if applicable |
| 59 | self.per_iter_time = per_iter_time |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 60 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 61 | def __repr__(self): |
| 62 | return "BenchDataPoint(%s, %s, %s, %s, %s)" % ( |
| 63 | str(self.bench), |
| 64 | str(self.config), |
| 65 | str(self.time_type), |
| 66 | str(self.time), |
| 67 | str(self.settings), |
| 68 | ) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 69 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 70 | class _ExtremeType(object): |
| 71 | """Instances of this class compare greater or less than other objects.""" |
| 72 | def __init__(self, cmpr, rep): |
| 73 | object.__init__(self) |
| 74 | self._cmpr = cmpr |
| 75 | self._rep = rep |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 76 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 77 | def __cmp__(self, other): |
| 78 | if isinstance(other, self.__class__) and other._cmpr == self._cmpr: |
| 79 | return 0 |
| 80 | return self._cmpr |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 81 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 82 | def __repr__(self): |
| 83 | return self._rep |
| 84 | |
| 85 | Max = _ExtremeType(1, "Max") |
| 86 | Min = _ExtremeType(-1, "Min") |
| 87 | |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 88 | class _ListAlgorithm(object): |
| 89 | """Algorithm for selecting the representation value from a given list. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 90 | representation is one of the ALGORITHM_XXX representation types.""" |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 91 | def __init__(self, data, representation=None): |
| 92 | if not representation: |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 93 | representation = ALGORITHM_AVERAGE # default algorithm |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 94 | self._data = data |
| 95 | self._len = len(data) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 96 | if representation == ALGORITHM_AVERAGE: |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 97 | self._rep = sum(self._data) / self._len |
| 98 | else: |
| 99 | self._data.sort() |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 100 | if representation == ALGORITHM_MINIMUM: |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 101 | self._rep = self._data[0] |
| 102 | else: |
| 103 | # for percentiles, we use the value below which x% of values are |
| 104 | # found, which allows for better detection of quantum behaviors. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 105 | if representation == ALGORITHM_MEDIAN: |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 106 | x = int(round(0.5 * self._len + 0.5)) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 107 | elif representation == ALGORITHM_25TH_PERCENTILE: |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 108 | x = int(round(0.25 * self._len + 0.5)) |
| 109 | else: |
| 110 | raise Exception("invalid representation algorithm %s!" % |
| 111 | representation) |
| 112 | self._rep = self._data[x - 1] |
| 113 | |
| 114 | def compute(self): |
| 115 | return self._rep |
| 116 | |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 117 | def _ParseAndStoreTimes(config_re_compiled, is_per_tile, line, bench, |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 118 | value_dic, layout_dic): |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 119 | """Parses given bench time line with regex and adds data to value_dic. |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 120 | |
| 121 | config_re_compiled: precompiled regular expression for parsing the config |
| 122 | line. |
| 123 | is_per_tile: boolean indicating whether this is a per-tile bench. |
| 124 | If so, we add tile layout into layout_dic as well. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 125 | line: input string line to parse. |
| 126 | bench: name of bench for the time values. |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 127 | value_dic: dictionary to store bench values. See bench_dic in parse() below. |
| 128 | layout_dic: dictionary to store tile layouts. See parse() for descriptions. |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 129 | """ |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 130 | |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 131 | for config in config_re_compiled.finditer(line): |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 132 | current_config = config.group(1) |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 133 | tile_layout = '' |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 134 | if is_per_tile: # per-tile bench, add name prefix |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 135 | current_config = 'tile_' + current_config |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 136 | layouts = TILE_LAYOUT_RE_COMPILED.search(line) |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 137 | if layouts and len(layouts.groups()) == 2: |
| 138 | tile_layout = '%sx%s' % layouts.groups() |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 139 | times = config.group(2) |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 140 | for new_time in TIME_RE_COMPILED.finditer(times): |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 141 | current_time_type = new_time.group(1) |
| 142 | iters = [float(i) for i in |
| 143 | new_time.group(2).strip().split(',')] |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 144 | value_dic.setdefault(bench, {}).setdefault( |
| 145 | current_config, {}).setdefault(current_time_type, []).append( |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 146 | iters) |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 147 | layout_dic.setdefault(bench, {}).setdefault( |
| 148 | current_config, {}).setdefault(current_time_type, tile_layout) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 149 | |
bensong@google.com | 967b258 | 2013-12-05 01:31:56 +0000 | [diff] [blame] | 150 | # TODO(bensong): switch to reading JSON output when available. This way we don't |
| 151 | # need the RE complexities. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 152 | def parse(settings, lines, representation=None): |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 153 | """Parses bench output into a useful data structure. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 154 | |
bensong@google.com | 8734816 | 2012-08-15 17:31:46 +0000 | [diff] [blame] | 155 | ({str:str}, __iter__ -> str) -> [BenchDataPoint] |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 156 | representation is one of the ALGORITHM_XXX types.""" |
| 157 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 158 | benches = [] |
| 159 | current_bench = None |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 160 | # [bench][config][time_type] -> [[per-iter values]] where per-tile config |
| 161 | # has per-iter value list for each tile [[<tile1_iter1>,<tile1_iter2>,...], |
| 162 | # [<tile2_iter1>,<tile2_iter2>,...],...], while non-per-tile config only |
| 163 | # contains one list of iterations [[iter1, iter2, ...]]. |
| 164 | bench_dic = {} |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 165 | # [bench][config][time_type] -> tile_layout |
| 166 | layout_dic = {} |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 167 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 168 | for line in lines: |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 169 | |
| 170 | # see if this line is a settings line |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 171 | settingsMatch = SETTINGS_RE_COMPILED.search(line) |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 172 | if (settingsMatch): |
| 173 | settings = dict(settings) |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 174 | for settingMatch in PER_SETTING_RE_COMPILED.finditer(settingsMatch.group(1)): |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 175 | if (settingMatch.group(2)): |
| 176 | settings[settingMatch.group(1)] = settingMatch.group(2) |
| 177 | else: |
| 178 | settings[settingMatch.group(1)] = True |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 179 | |
| 180 | # see if this line starts a new bench |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 181 | new_bench = BENCH_RE_COMPILED.search(line) |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 182 | if new_bench: |
| 183 | current_bench = new_bench.group(1) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 184 | |
| 185 | # add configs on this line to the bench_dic |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 186 | if current_bench: |
epoger@google.com | edb711b | 2013-02-17 08:59:56 +0000 | [diff] [blame] | 187 | if line.startswith(' tile_') : |
| 188 | _ParseAndStoreTimes(TILE_RE_COMPILED, True, line, current_bench, |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 189 | bench_dic, layout_dic) |
epoger@google.com | edb711b | 2013-02-17 08:59:56 +0000 | [diff] [blame] | 190 | else: |
| 191 | _ParseAndStoreTimes(CONFIG_RE_COMPILED, False, line, |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 192 | current_bench, bench_dic, layout_dic) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 193 | |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 194 | # append benches to list |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 195 | for bench in bench_dic: |
| 196 | for config in bench_dic[bench]: |
| 197 | for time_type in bench_dic[bench][config]: |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 198 | tile_layout = '' |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 199 | per_tile_values = [] # empty for non-per-tile configs |
| 200 | per_iter_time = [] # empty for per-tile configs |
| 201 | bench_summary = None # a single final bench value |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 202 | if len(bench_dic[bench][config][time_type]) > 1: |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 203 | # per-tile config; compute representation for each tile |
| 204 | per_tile_values = [ |
| 205 | _ListAlgorithm(iters, representation).compute() |
| 206 | for iters in bench_dic[bench][config][time_type]] |
| 207 | # use sum of each tile representation for total bench value |
| 208 | bench_summary = sum(per_tile_values) |
| 209 | # extract tile layout |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 210 | tile_layout = layout_dic[bench][config][time_type] |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 211 | else: |
| 212 | # get the list of per-iteration values |
| 213 | per_iter_time = bench_dic[bench][config][time_type][0] |
| 214 | bench_summary = _ListAlgorithm( |
| 215 | per_iter_time, representation).compute() |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 216 | benches.append(BenchDataPoint( |
| 217 | bench, |
| 218 | config, |
| 219 | time_type, |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 220 | bench_summary, |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 221 | settings, |
| 222 | tile_layout, |
commit-bot@chromium.org | 758bc7a | 2014-03-12 16:23:33 +0000 | [diff] [blame] | 223 | per_tile_values, |
| 224 | per_iter_time)) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 225 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 226 | return benches |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 227 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 228 | class LinearRegression: |
| 229 | """Linear regression data based on a set of data points. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 230 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 231 | ([(Number,Number)]) |
| 232 | There must be at least two points for this to make sense.""" |
| 233 | def __init__(self, points): |
| 234 | n = len(points) |
| 235 | max_x = Min |
| 236 | min_x = Max |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 237 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 238 | Sx = 0.0 |
| 239 | Sy = 0.0 |
| 240 | Sxx = 0.0 |
| 241 | Sxy = 0.0 |
| 242 | Syy = 0.0 |
| 243 | for point in points: |
| 244 | x = point[0] |
| 245 | y = point[1] |
| 246 | max_x = max(max_x, x) |
| 247 | min_x = min(min_x, x) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 248 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 249 | Sx += x |
| 250 | Sy += y |
| 251 | Sxx += x*x |
| 252 | Sxy += x*y |
| 253 | Syy += y*y |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 254 | |
senorblanco@chromium.org | c5e1ed8 | 2012-09-20 19:05:33 +0000 | [diff] [blame] | 255 | denom = n*Sxx - Sx*Sx |
| 256 | if (denom != 0.0): |
| 257 | B = (n*Sxy - Sx*Sy) / denom |
| 258 | else: |
| 259 | B = 0.0 |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 260 | a = (1.0/n)*(Sy - B*Sx) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 261 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 262 | se2 = 0 |
| 263 | sB2 = 0 |
| 264 | sa2 = 0 |
senorblanco@chromium.org | c5e1ed8 | 2012-09-20 19:05:33 +0000 | [diff] [blame] | 265 | if (n >= 3 and denom != 0.0): |
| 266 | se2 = (1.0/(n*(n-2)) * (n*Syy - Sy*Sy - B*B*denom)) |
| 267 | sB2 = (n*se2) / denom |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 268 | sa2 = sB2 * (1.0/n) * Sxx |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 269 | |
| 270 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 271 | self.slope = B |
| 272 | self.intercept = a |
| 273 | self.serror = math.sqrt(max(0, se2)) |
| 274 | self.serror_slope = math.sqrt(max(0, sB2)) |
| 275 | self.serror_intercept = math.sqrt(max(0, sa2)) |
| 276 | self.max_x = max_x |
| 277 | self.min_x = min_x |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 278 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 279 | def __repr__(self): |
| 280 | return "LinearRegression(%s, %s, %s, %s, %s)" % ( |
| 281 | str(self.slope), |
| 282 | str(self.intercept), |
| 283 | str(self.serror), |
| 284 | str(self.serror_slope), |
| 285 | str(self.serror_intercept), |
| 286 | ) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 287 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 288 | def find_min_slope(self): |
| 289 | """Finds the minimal slope given one standard deviation.""" |
| 290 | slope = self.slope |
| 291 | intercept = self.intercept |
| 292 | error = self.serror |
| 293 | regr_start = self.min_x |
| 294 | regr_end = self.max_x |
| 295 | regr_width = regr_end - regr_start |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 296 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 297 | if slope < 0: |
| 298 | lower_left_y = slope*regr_start + intercept - error |
| 299 | upper_right_y = slope*regr_end + intercept + error |
| 300 | return min(0, (upper_right_y - lower_left_y) / regr_width) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 301 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 302 | elif slope > 0: |
| 303 | upper_left_y = slope*regr_start + intercept + error |
| 304 | lower_right_y = slope*regr_end + intercept - error |
| 305 | return max(0, (lower_right_y - upper_left_y) / regr_width) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 306 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 307 | return 0 |
epoger@google.com | c71174d | 2011-08-08 17:19:23 +0000 | [diff] [blame] | 308 | |
| 309 | def CreateRevisionLink(revision_number): |
| 310 | """Returns HTML displaying the given revision number and linking to |
| 311 | that revision's change page at code.google.com, e.g. |
| 312 | http://code.google.com/p/skia/source/detail?r=2056 |
| 313 | """ |
| 314 | return '<a href="http://code.google.com/p/skia/source/detail?r=%s">%s</a>'%( |
| 315 | revision_number, revision_number) |
senorblanco@chromium.org | c5e1ed8 | 2012-09-20 19:05:33 +0000 | [diff] [blame] | 316 | |
| 317 | def main(): |
| 318 | foo = [[0.0, 0.0], [0.0, 1.0], [0.0, 2.0], [0.0, 3.0]] |
| 319 | LinearRegression(foo) |
| 320 | |
| 321 | if __name__ == "__main__": |
| 322 | main() |