REM *----------------------------------------------*
REM *            BETA REGRESSION ROUTINE	   *
REM *		 -----------------------	   *
REM *	UPDATED: February 5, 1983		   *
REM *	COPYRIGHT: E. A. Valenzuela		   *
REM *	VERSION: 1.31				   * 
REM *----------------------------------------------*

rem ... requires following subprograms ...
rem     STKgraph.BAS  ... MAIN program
rem     STKPLOT.BAS
rem     LINREG.BAS
rem     SCRNIN.BAS
 
9001 rem ...program to regress price history data to obtain alphas and
rem      ...betas for the portfolio...

: rem ... read in # of stocks
open "B:STKPRICE.DAT" as 8
 read #8; nstk%, newdate$, olddate$
close 8

if end #4 then 9003

print clear$; fn.down$(2); \
  fn.right$(4); "ALPHA AND BETA CALCULATION FOR PORTFOLIO"
print down$; fn.right$(15); "option list" ; down$

print fn.right$(4); " 0 = RETURN to main menu"
 print
print fn.right$(4); " 1 = DAILY PRICE FILE"
print fn.right$(4); " 2 = WEEKLY PRICE FILE"
print fn.right$(4); " 3 = MONTHLY PRICE FILE"
print fn.right$(4); " 4 = QUARTERLY PRICE FILE"
print fn.right$(4); " 5 = YEARLY PRICE FILE"

print down$; fn.line$(17); fn.right$(10); bell$ ;
  input "SELECT FILE TO ANALYZE: "; option%

print up$; fn.line$(17); fn.clear$(2,line%); bell$ 
  input "FOR PRICE PLOTS ONLY, ENTER 'P': "; plot$
  plot$ = ucase$( plot$ )  

print up$; fn.line$(17); fn.clear$(2,line%); bell$ 
  input "FOR AUTO-SCALING OF PLOTS, ENTER 'A': "; auto.scale$
  auto.scale$ = ucase$( auto.scale$ )

if option% <= 0 or option% > 5 then RETURN
 ON option% goto 9011,9012,9013,9014,9015

9011 filename$ = "B:DAYHST.DAT"
     period = 260 : rem ... weekdays in a year
     goto 9016
9012 filename$ = "B:WEEKHST.DAT"
     period = 52
     goto 9016
9013 filename$ = "B:MONTHST.DAT"
     period = 12
     goto 9016
9014 filename$ = "B:QUARTHST.DAT"
     period = 4
     goto 9016
9015 filename$ = "B:YEARHST.DAT"
     period = 1
     
rem ... set up, print report heading ...
9016 print$="F"
title1$= "  BETA ANALYIS OF PORTFOLIO on " 
title2$= "  DATE: "
if plot$="P" then goto 9017 else GOSUB 9023

9017 rem ... pick up the S&P 500 prices ...
OPEN filename$ recl 8 as 4
    if end #4 then 9003
   namstk$ = "S&P 500"
   nitems% = nstk% + 3
   j% = 0
   i% = 1
   n1% = 3
 while -1 
   rem ... pointer points at record # 3 ...
   read #4, n1%; x0(i%)
   x(i%) = x0(i%)   
   y(i%) = x0(i%)
   i% = i% + 1
   j% = j% + 1    
   n1% = n1% + nitems% 
   rem ... pointer points at record # 3 + number of stocks + 1 ...
 wend 

9019 rem ... set up summation counters for the portfolio ...
gx=0
gy=0
gx2=0
gy2=0
gxy=0
nport%=0
sump=1.0

open "B:STKFIL01.DAT" recl 80 as 9
rem ... pick up the prices of all the individual stocks ...
 
for istock%=1 to nstk%
n1%=3
j%=0

read #9; nshrs%,namstk$,SYMBOL$,XCHNG$,PURDATE$,COMMISSION, \
   TOTPURPRICE,KEY%(ISTOCK%),DIVIDEND

open FILENAME$ recl 8 as 4
if end #4 then 9003
i%=1
 while -1 
   rem ... pointer points at record # 3 + stock number ...
   read #4, n1% + istock%; price1 
   if price1 = 0 then goto 9004
   j% = j% + 1    : \ 
   x(j%) = x0(i%) : \
   y(j%) = price1 
9004 n1% = n1% + nitems% 
   rem ... pointer points at record # 3 + number of stocks + 1 ...
   i%=i%+1
wend
  
9003 rem ...end-of-file on #4...
 nr% = j%-1
 close 4
rem ... if less than 3 points, skip plots, analysis ...
 if nr% < 3 then goto 9031

rem ... PLOT STOCK PRICES ...
console
 GOSUB 298 : rem ... SUBROUTINE STKPLOT.BAS
 if plot$="P" then goto 9031

rem ... if an "X" has been entered at the console, EXIT
 if CONSTAT% THEN ANS% = CONCHAR%
 IF CHR$( ANS% ) = "X" THEN RETURN

rem ... calculate % price change ...
sum5=1.0
  j% = nr% + 1
  for i%=2 to j%
   ij% = i%-1
   xr(ij%) = (x(i%) - x(ij%)) / x(ij%)	
   yr(ij%) = (y(i%) - y(ij%)) / y(ij%)
   sum5=sum5*(1.0+yr(ij%))
  next i%

rem ... calculate geometric annual return ...
rem ... assume prices are weekly prices ...

nperiods = float(nr%)
nyear = nperiods / period
yield = (sum5-1.0) / nyear
if yield >  9.99 then yield =  9.99
if yield < -0.99 then yield = -0.99
ygavg=(sum5-1.0) / nperiods

rem ... find maximum and minimum % increases ...

 min = yr(1)
 max = yr(1)
for i%=2 to nr%
 if yr(i%) > max then max = yr(i%)
 if yr(i%) < min then min = yr(i%)
next i%
 max=max
 min=min

rem ... CALL LINEAR REGRESSION ROUTINE ...
gosub 9100 : rem ... SUBROUTINE LINREG.BAS

rem ... reward-risk ratio ...
ratio = yield/YSD
ab=(a/ABS(b))*100.

REM ... PRINT STOCK STATISTICS ...
gosub 9024

REM ... STORE PORTFOLIO SUMS ...
gx=gx+sx
gx2=gx2+sx2
gy=gy+sy
gy2=gy2+sy2
gxy=gxy+sxy
nport%=nport%+nr%
sump=sump*sum5

9031 if istock% = 0 then goto 9019
     next istock%

CLOSE 9
  if plot$="P" then RETURN

rem ... calculate portfolio statistics ...
namstk$ = "portfolio"
sxy=gxy
sx=gx
sx2=gx2
sy=gy
sy2=gy2
nr%=nport%
  nperiods = float(nr%)
nyear = nperiods / period
yield = (sump-1.0) / nyear
ygavg = (sump-1.0) / nperiods

print 
rem ... CALL LINEAR REGRESSION ROUTINE AT ENTRY POINT

GOSUB 9110 : rem ... SUBROUTINE LINREG.BAS

ratio=yield/YSD
ab=(a/ABS(b))*100.

gosub 9025
return

rem ------ SUBROUTINES -----

9023 rem ... SET UP PRINT FORMATS ...

lprinter width 136
print small.print$
print newpage$

rem ... PRINT HEADING FOR REPORT ...

print title1$; filename$
print title2$; newdate$

print "                                      stand  stand  stand -correlation"\
 +"-  ------- RETURNS PER PERIOD--------------      reward" 
print "     STOCK      no.of  alpha   beta   error  error  error r-square   r"\
 +"  geometric  arith. standard             annual  to-risk   a/b"
print " no.    name    points               intrcpt slope estimate           "\
 +"  average   average deviatn  min    max  yield   ratio   ratio"
line$="--------- --------- --------- --------- --------- --------- " + \
      "--------- --------- --------- --------- --------- --------- " + \
      "---------"

print line$

if print$="T" then return
rem ... CREATE FILE FOR BETA STORAGE ...
 CREATE "B:BETA.DAT" as 10
return 

9024 rem ... print statistics for each stock ...
lprinter width 136

for9$=" ## /........../ ###  ##.###  ##.###  #.###  #.###  #.###  #.###  #.##"\
 +"#  ##.###  ##.###  ##.### ##.### ##.### ##.###  ###.##  ###.##"  
print using for9$; istock%,namstk$,nr%,a,b,  SA,    SB,    see,   r2,    r,  \
       ygavg,   yavg,    YSD,     min, max, yield,ratio,ab
 if fn.mod%(istock%,5) eq 5 then print

rem ... WRITE DATA TO BETA FILE ... 
print #10; istock%,namstk$,nr%,a,b,  SA,    SB,    see,   r2,    r,  \
       ygavg,   yavg,    YSD,     min, max, yield,ratio,ab
return

9025 rem ... print portfolio statistics ...
lprinter width 136

for8$=" ## /......../ #####  ##.###  ##.###  #.###  #.###  #.###  #.### ##.##"\
 +"#  ##.###  ##.###  ##.###               ##.###  ###.##  ###.##"  
print using for8$; istock%,namstk$,nr%,a,b,SA,SB,see,r2,r,ygavg,yavg,YSD, \
  yield,ratio,AB

rem ... FINISH WRITING DATA TO FILE...
 min=0
 max=0
print #10; istock%,namstk$,nr%,a,b,SA,SB,see,r2,r,ygavg,yavg,YSD, \
  min, max, yield, ratio, AB
CLOSE 10

 console
return
 
REM ---------------- END OF REGRESSION ---------------------
 