Created
May 17, 2014 08:25
-
-
Save Alrecenk/6fffeca5e05259a6fe33 to your computer and use it in GitHub Desktop.
A simple initial guess for a logistic regression. P and N are the average inputs of the positives and negatives respectively.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for(int k=0;k<pos.length;k++){ | |
pp += pos[k]*pos[k] ; | |
pn += pos[k]*neg[k] ;// calculate relevant dot products | |
nn += neg[k]*neg[k] ; | |
} | |
//assuming w = alpha * (pos- neg) with b in the last w slot | |
//solve for alpha and b so that pos returns 0.75 and neg returns 0.25 | |
double alphab[] = lineIntersection(pp-pn,1,sinv(0.75),pn-nn,1,sinv(0.25)) ; | |
w = new double[input[0].length] ; | |
for(int k=0;k<w.length-1;k++){ | |
w[k] = alphab[0] * (pos[k] - neg[k]) ; // alpha * (pos-neg) | |
} | |
w[w.length-1] = alphab[1]; // bias is on the end of w | |
... | |
//inverse of sigmoid/logistic function | |
public static double sinv(double y){ | |
return Math.log( y / (1-y) ) ; | |
} | |
... | |
//returns the intersection of 2D lines given in standard form | |
// a1*x + b1*y = c1 and a2*x + b2*y = c2 | |
public static double[] lineIntersection(double a1, double b1, double c1, double a2, double b2, double c2){ | |
double det = a1*b2 - a2*b1 ; | |
if( det == 0){//the lines are parellel | |
return null ; | |
}else{ | |
return new double[]{ (c1*b2 - b1*c2)/det, (a1*c2-c1*a2)/det} ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment