// (c) December 2009 Frank Nielsen
//	Shows that beta divergences are (representational) Bregman divergences
//  (up to numerical imprecisions)
//
// See paper
// The dual Voronoi diagrams with respect to representational Bregman divergences
// Frank Nielsen and Richard Nock
// International Symposium on Voronoi Diagrams, ISVD 2009 (IEEE)
//
// Beta divergence is a Bregman divergence//
//
// Beta=1 Kullback-Leibler
// Beta=0 Itakura-Saito
// Beta=2 squared Euclidean distance 


class RepresentationalBetaBregman
{
final static double beta=2.0*Math.random();

public static double betadivergence(double p, double q)
{double result;
result= (1.0/(beta+1.0))*(Math.pow(p,beta+1.0)-Math.pow(q,beta+1.0)) - (1.0/beta)*q*(Math.pow(p,beta)-Math.pow(q,beta));

return result;
}

public static double F(double x)
{
return (1.0/(beta+1.0))*Math.pow(1.0+beta*x,(1.0+beta)/beta);

}

public static double gradF(double x)
{
return Math.pow(1.0+beta*x,1.0/beta); 	
}	
	
public static double k(double x)
{
return 	(Math.pow(x,beta)-1.0)/beta;
}

public static double BF(double p, double q)
{
return F(p)-F(q)-(p-q)*gradF(q);
}


public static double BFRep(double p, double q)
{
return BF(k(p),k(q));	
}
	
	
public static void main(String[] args)
{
System.out.println("Shows that beta divergences can be obtained from representational Bregman divergences");

double p,q,betadiv, bregrep;


for(int i=0;i<10;i++)
{
p=Math.random();
q=Math.random();	

betadiv=betadivergence(p,q);
bregrep=BFRep(p,q);

System.out.println("beta-div="+betadiv+"\tequals Bregman rep. div="+bregrep);
}
}

	
}