1#!/usr/bin/python2.7
2# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import numpy
7
8
9def LinearRegression(x, y):
10    """Perform a linear regression using numpy.
11
12    @param x: Array of x-coordinates of the samples
13    @param y: Array of y-coordinates of the samples
14    @return: ((slope, intercept), r-squared)
15    """
16    # p(x) = p[0]*x**1 + p[1]
17    p, (residual,) = numpy.polyfit(x, y, 1, full=True)[:2]
18    # Calculate the coefficient of determination (R-squared) from the
19    # "residual sum of squares"
20    # Reference:
21    # http://en.wikipedia.org/wiki/Coefficient_of_determination
22    r2 = 1 - (residual / (y.size*y.var()))
23
24    # Alternate calculation for R-squared:
25    #
26    # Calculate the coefficient of determination (R-squared) as the
27    # square of the  sample correlation coefficient,
28    # which can be calculated from the variances and covariances.
29    # Reference:
30    # http://en.wikipedia.org/wiki/Correlation#Pearson.27s_product-moment_coefficient
31    #V = numpy.cov(x, y, ddof=0)
32    #r2 = (V[0,1]*V[1,0]) / (V[0,0]*V[1,1])
33
34    return p, r2
35
36
37def FactsToNumpyArray(facts, dtype):
38    """Convert "facts" (list of dicts) to a numpy array.
39
40    @param facts: A list of dicts. Each dict must have keys matching the field
41            names in dtype.
42    @param dtype: A numpy.dtype used to fill the array from facts. The dtype
43            must be a "structured array". ie:
44            numpy.dtype([('loops', numpy.int), ('cycles', numpy.int)])
45    @returns: A numpy.ndarray with dtype=dtype filled with facts.
46    """
47    a = numpy.zeros(len(facts), dtype=dtype)
48    for i, f in enumerate(facts):
49        a[i] = tuple(f[n] for n in dtype.names)
50    return a
51