view tvii/cost.py @ 79:cecea2334eef

notes to self re cost function
author Jeff Hammel <k0scist@gmail.com>
date Sun, 17 Dec 2017 13:40:29 -0800
parents
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()