      PROGRAM FishOil ! Bernd Berg, Sep 17 2008.
C Purpose: Bootstrap confidence intervals of a medical study.
C See Yahoo article linked on the ISC 5228 2008 HW website.
      include '../../ForLib/implicit.sta'
      include '../../ForLib/constants.par'
      PARAMETER(NTRY=10 000, NDAT=3500, NCH=2)
      PARAMETER(IUO=6,IUD=10,ISD1=1,ISD2=0,M1=-1,I1=1)
      PARAMETER(PROB1=0.27D00,PROB2=0.29D00)           ! Death.
C     PARAMETER(PROB1=1981.D0/NDAT,PROB2=2053.D0/NDAT) ! Heart Attack.
      DIMENSION PROB(NCH),SCORE(NCH),RESULT(NTRY,NCH),COUNT(3)
      DIMENSION DIFF(NTRY)
c     DATA PROB/0.27D0,0.29D0/ ! Ordinary versus Fish oil: Deaths.
      DATA PROB/PROB1,PROB2/   ! Heart failures (death or not).
      LTEST=.FALSE.
      LTEST=.TRUE.
C
      WRITE(IUO,'(/," Program FishOil:",/,/,
     &  " Probabilities (Ordinary oil, fishoil):")')
      WRITE(IUO,'(/,1X,5F10.3)') PROB
C
      CALL RMASET(M1,IUD,ISD1,ISD2,"not there")
      OPEN(IUD,file="data.d",form='formatted',status='unknown')
      DO ITRY=1,NTRY
        CALL RAZERO(SCORE,I1,NCH)
        DO IDAT=1,NDAT
          DO I=1,NCH
            CALL RANMAR(XR)
            IF(XR.LT.PROB(I)) SCORE(I)=SCORE(I)+ONE
          ENDDO
1         CONTINUE
        ENDDO
        IF(SCORE(1).GT.SCORE(2)) THEN
          COUNT(1)=COUNT(1)+ONE
        ELSEIF(NINT(SCORE(1)).EQ.NINT(SCORE(2))) THEN
          COUNT(2)=COUNT(2)+ONE
        ELSE
          COUNT(3)=COUNT(3)+ONE
        ENDIF
        DO I=1,NCH
          RESULT(ITRY,I)=SCORE(I)/NDAT
        ENDDO
        DIFF(ITRY)=SCORE(2)-SCORE(1)
        WRITE(IUD,'(I10,F12.1)') ITRY,DIFF(ITRY)
      ENDDO  
      CLOSE(IUD)
C
      DO I=1,3
        COUNT(I)=COUNT(I)/NTRY
      ENDDO
      WRITE(IUO,'(/," Ordinary oil, tie, fishoil:",/)')
      WRITE(IUO,'(1X,3F10.3)') COUNT
C
      DO I=1,NCH
        CALL HEAPSORT(NTRY,RESULT(1,I))
        CALL QDF_GNU(IUD,NTRY,RESULT(1,I))
      ENDDO
      WRITE(IUO,'(/," All done: qdf dat and plot files created.")')
C
      CALL HEAPSORT(NTRY,DIFF)
      CALL QDF_GNU(IUD,NTRY,DIFF)
C
      IF(LTEST) STOP "FishOil: LTEST."
      STOP
      END

      include '../../ForLib/heapsort.f'
      include '../../ForLib/qdf_gnu.f'
      include '../../ForLib/ranmar.f'
      include '../../ForLib/razero.f'
      include '../../ForLib/rmafun.f'
      include '../../ForLib/rmaset.f'
