C Version, Listing




/* Start of file BBallC.C */

char name[]="BBallC - Baseball probability (if each game is a 50-50 chance).";
char version[]="C Version 2.04, last revised: 1994-02-13, 0600 hour";
char author[] ="Copyright (c) 1981-1994 by author: Harry J. Smith,";
char address[]="19628 Via Monte Dr., Saratoga CA 95070.  All rights reserved.";

/* Computes the probability that the 1st place team will beat the 2nd place
*  team for the division title, assuming each has a 50-50 chance of winning
*  any given future game. Uses a bivariate binomial distribution as a model.
*/

/* Developed in Turbo Pascal 5.0, converted to
*  C subset of Borland C++ Version 4.0
*/

#include <stdio.h>   /* Definitions for stream input/output */
#include <math.h>    /* Definitions for the math floating point package */
#include <conio.h>   /* Direct MS-DOS console input/output, getch() */
#include <stdlib.h>  /* Definition of exit() etc. */
#include <string.h>  /* Definition of srtcpy() etc. */
#define DEBUG
#undef DEBUG

/* Global constants and variables */
const char ESC = 27;

char   TN1[11]; /* Team's name, 1st place team */
char   TN2[11]; /* Team's name, 2nd place team */
int    GL1;     /* Games Left to play, 1st place team */
int    GL2;     /* Games Left to play, 2nd place team */
int    GE;      /* Games to play each other */
double GA;      /* Games 1st place team is ahead. 0, 0.5, ... */
int    GA2;     /* Twice games ahead = 2 * GA, 0, 1, ... */
int    MNT1;    /* Magic Number to tie for 1st place team */
int    MNW1;    /* Magic Number to win for 1st place team */
int    MNT2;    /* Magic Number to tie for 2nd place team */
int    MNW2;    /* Magic Number to win for 2nd place team */
double P;       /* Probability that 1st place team beats 2nd place team */
double Q;       /* Probability that 2nd place team beats 1st place team */
char   Ch;

/* Procedure prototypes */
void init( void);        /* Initialize */
void GetCase( void);     /* Get data for a case to compute */
void ExpandCase( void);  /* Compute related data */
void DisplayCase( void);
void ComputeProb( void); /* Compute probability using a bivariate binomial
			    distribution as a model */
void DisplayProb( void); /* Display probability */
void ReadInt( char *Mess, int Min, int Max, int Nom,
	      int *I);   /* Read in an integer from keyboard */
void ReadReal( char *Mess, double Min, double Max, double Nom,
	     double *I); /* Read in a Real from keyboard */

/* -------------------------------------- */
int main( void) {
  do {
    init();
    GetCase();
    ExpandCase();
    ComputeProb();
    init();
    DisplayCase();
    DisplayProb();
    printf("Press any key to continue... (or ESC to exit)\n");
    Ch = getch();
  } while (Ch != ESC);
  return 0;
} /* End of main */

/* -------------------------------------- */
void init( void)  /* Initialize */
{
  printf(" [1;33;44m [2J"); /* Ansi.Sys ESC seq. for YELLOW on BLUE, clrscr */
  printf("\n%s\n%s\n%s\n%s\n\n", name, version, author, address);
} /* End of init */

/* -------------------------------------- */
void GetCase( void) /* Get data for a case to compute */
{
  int I;

/*  Giants, Braves 1993 */
  strcpy( TN1, "Braves");  strcpy( TN2, "Giants");
  GL1 = 6;  GL2 = 7;  GE = 0;  GA = 1.5;  /* 1993-09-27, 1 / Q = 4.7175 */
  ReadInt("Games Left to play, 1st place team (-2 => Exit, -1 => Test case)",
    -2, 162, -1, &I);
  if (I == -2)  exit(0); /* Exit main */
  if (I >= 0) {
    strcpy( TN1, "        ");  strcpy( TN2, "        ");
    GL1 = I;
    ReadInt("Games Left to play, 2nd place team", 0, 162, GL1, &GL2);
    ReadInt("Games to play each other", 0, 24, 0, &GE);
    do
      ReadReal("Games ahead, 0, 0.5, ...", 0, 162, 0, &GA);
    while (floor( GA + GA) != GA + GA);
  }
} /* End of GetCase */

/* -------------------------------------- */
void ExpandCase( void) /* Compute related data */
{
  int I;
  GA2 = 2 * GA + 0.5;
  I = GL1 + GL2 - GA2;
  if (I % 2 == 1) { /* if I is odd */
    printf("Error in data\n");
    printf("Press any key to continue...\n");  Ch = getch();
  }
  MNT1 = I / 2;             MNW1 = MNT1 + 1;
  MNT2 = GL1 + GL2 - MNT1;  MNW2 = MNT2 + 1;
} /* End of ExpandCase */

/* -------------------------------------- */
void DisplayCase( void)
{
  printf("%8d = Games Left to play, 1st place team (%s)\n", GL1, TN1);
  printf("%8d = Games Left to play, 2nd place team (%s)\n", GL2, TN2);
  printf("%8d = Games to play each other\n", GE);
  printf("%#8.1f = Games 1st place team is ahead.  0, 0.5, ...\n", GA);
  printf("%8d = Magic Number to tie for 1st place team\n", MNT1);
  printf("%8d = Magic Number to win for 1st place team\n", MNW1);
  printf("%8d = Magic Number to tie for 2nd place team\n", MNT2);
  printf("%8d = Magic Number to win for 2nd place team\n\n", MNW2);
} /* End of DisplayCase */

/* -------------------------------------- */
void ComputeProb( void) /* Compute probability using a bivariate binomial
			   distribution as a model */
{
  int I, J;
  double A, B;
  double E[25]; /* Binomial coefficients, games to play each other */
  double S[25]; /* Running sum of 2 * E[I] */
  double F[163]; /* B. C., games not played with each other */
  double G[163]; /* Sum of 2 * E[I] for 2nd place team win, E[I] for tie */

  A = GL1 + GL2 - GE - GE; /* A = not played with each other games */
  B = 1.0;
  F[0] = 1.0;
  for (I = 1; I <= MNT1; I++) { /* Compute binomial coefficients */
    F[I] = F[I - 1] * A / B;
    A = A - 1.0;
    B = B + 1.0;
  }
  A = GE;
  B = 1.0;
  E[0] = 1.0;
  S[0] = 2.0;
  for (I = 1; I <= GE; I++) { /* Compute binomial coefficients */
    E[I] = E[I - 1] * A / B;
    A = A - 1.0;
    B = B + 1.0;
    S[I] = S[I - 1] + 2 * E[I];
  }
  for (I = 0; I <= MNT1; I++) { /* Compute G[I] */
    J = (MNT1 - I) / 2;
    if (J <= GE) {
      G[I] = S[J];
      if ((J + J) == (MNT1 - I))  G[I] = G[I] - E[J]; /* Adjust for tie */
    }
    else
      G[I] = S[GE];
  }
#ifdef DEBUG
  DisplayCase();
  for (I = 0; I <= MNT1; I++) {
    printf("F[%d] = %.0f\n", I, F[I]);
  }
  printf("\nPress any key to continue...\n");  Ch = getch();
  printf("\n");
  for (I = 0; I <= GE; I++) {
    printf("E[%d] = %.0f\n", I, E[I]);
  }
  printf("\nPress any key to continue...\n");  Ch = getch();
  printf("\n");
  for (I = 0; I <= GE; I++) {
    printf("S[%d] = %.0f\n", I, S[I]);
  }
  printf("\nPress any key to continue...\n");  Ch = getch();
  printf("\n");
  for (I = 0; I <= MNT1; I++) {
    printf("G[%d] = %.0f\n", I, G[I]);
  }
  printf("\nPress any key to continue...\n");  Ch = getch();
  printf("\n");
#endif
  Q = 0.0;   /* Compute probability that 2nd place team beats 1st place team */
  for (I = 0; I <= MNT1; I++) {
    Q = Q + F[I] * G[I];
  }
  A = GL1 + GL2 - GE + 1;
  B = pow( 2.0, A); /* 2 ** Flips */
  Q = Q / B;
  P = 1.0 - Q;
} /* End of ComputeProb */

/* -------------------------------------- */
void DisplayProb( void) /* Display probability */
{
  printf(
  "%#10.4f = P = Probability that 1st place team beats 2nd place team\n", P);
  printf(
  "%#10.4f = Q = Probability that 2nd place team beats 1st place team\n", Q);
  if (Q > 0.0)
    printf("%#10.4f = 1 / Q, (Odds = %1.4f : 1)\n", 1.0 / Q, P / Q);
  printf("\n");
} /* End of DisplayProb */

/* -------------------------------------- */
void ReadInt( char *Mess, int Min, int Max, int Nom, int *I)  /* Read in an
						     integer from keyboard */
{
  char St[129];
  char *ior;
  int Stat;
  long int LI;

  do {
	 do {
		printf("%s\n", Mess);
		printf("  [%d, %d] (ENTER => %d): ", Min, Max, Nom);
		ior = gets( St);
	 } while (ior == NULL);
	 Stat = sscanf( St, "%ld", &LI);
  } while (!(((Stat == 1) && (LI >= Min) && (LI <= Max)) || (St[0] == 0)));
  if (St[0] == 0) LI = Nom;
  *I = (int) LI;
  printf("Input = %d\n\n", *I);
} /* End of ReadInt */

/* -------------------------------------- */
void ReadReal( char *Mess, double Min, double Max, double Nom,
			double *R)  /* Read in a Real from keyboard */
{
  char St[129];
  char *ior;
  int Stat;

  do {
	 do {
		printf("%s\n", Mess);
		printf("  [%.1lf, %.1lf] (ENTER => %.1lf): ", Min, Max, Nom);
		ior = gets( St);
	 } while (ior == NULL);
	 Stat = sscanf( St, "%lf", R);
  } while (!(((Stat == 1) && (*R >= Min) && (*R <= Max)) || (St[0] == 0)));
  if (St[0] == 0) *R = Nom;
  printf("Input = %.1lf\n\n", *R);
} /* End of ReadReal */

/* End of file BBallC.C */

Return to Baseball Pennant Race Odds
Return to Harry's Home Page


This page accessed times since JOctober 20, 2004.
Page created by: hjsmithh@sbcglobal.net
Changes last made on Saturday, 14-May-05 12:42:43 PDT

1