Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A simple initial guess for a logistic regression. P and N are the average inputs of the positives and negatives respectively.
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 ;
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
You can’t perform that action at this time.