Mercurial > hg > tvii
changeset 79:cecea2334eef
notes to self re cost function
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 17 Dec 2017 13:40:29 -0800 |
parents | 4f197c057e26 |
children | 3c7927f59b05 |
files | tvii/cost.py |
diffstat | 1 files changed, 29 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /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()