view tvii/cost.py @ 85:d705f6384e8b

i hate namespace conflicts
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 14:03:02 -0800
parents cecea2334eef
children
line wrap: on
line source

#!/usr/bin/env python

"""
cost function for e.g. linear regression:

1./(2.*m))*sum(h(x^i) - y^i)**2

where `m` is the number of examples
"""

def cost(sigma, X, y):
    """
    compute the cost function for a (linear) regression

    sigma -- vector of the sigmas; what you are solving for
    X -- matrix of parameters;  let's make this include x_0 = 1 for now
    y -- vector of training examples
    """

    # TODO: sanity (e.g. dimension) checking
    m = len(_y)

    return (0.5/m)*sum([
    (sum([s*x for s, x in zip(sigma, X[index])]) - _y)**2
        for index, _y in enumerate(y)])


if __name__ == '__main__':
    main()