/*------------------------------------------------------------- Copyright (C) 2000 Peter Clote. All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation for NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies. THE AUTHOR AND PUBLISHER MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. -------------------------------------------------------------*/ /****************************************** Program: gradientDescent.c P. Clote, 12 Feb 1997 Assume 2N coin flips where outcome is 10101010 ... 10 L(p) = p^N (1-p)^N E = -log L(p) = - N log p - N log(1-p) Want \delta p = - k dE/dp = - k ( - N/p + N/(1-p) ) = k (N/p - N/(1-p)) USAGE: gradientDecent k is the constant for delta p, e.g. k=0.00001 yields convergence ******************************************/ #include #include #define N 20 /* 2N coin flips */ main(int argc, char *argv[]) { float p = 0.001; /* initial probability */ float deltaP; /* delta p */ float k; /* constant in gradient descent, eg 0.01 */ int i,n=0; /* count number of iterations */ float grad; /* gradient */ /********** check of input errors ***********/ if (argc != 2) { fprintf(stderr,"%s float.\n",argv[0]); exit(1); } k = atof(argv[1]); /********** repeatedly update value of p ********/ do { for (i=0;i<50;i++) { grad = ((float)N)/p - ((float) N)/(1-p); deltaP = k * grad; //printf("n:%d p:%f grad:%f\n",n,p,grad); p = p + deltaP; n++; } printf("n = %d\t p = %2.4f\n",n,p); } while ( getchar() != 'q' ); }