# HG changeset patch # User Jeff Hammel # Date 1513546829 28800 # Node ID cecea2334eefab8e8d13f2409ef67a93ad0dbc91 # Parent 4f197c057e2624fb866b12b00387cd66a77fc4cb notes to self re cost function diff -r 4f197c057e26 -r cecea2334eef tvii/cost.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tvii/cost.py Sun Dec 17 13:40:29 2017 -0800 @@ -0,0 +1,29 @@ +#!/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()