回帰式 その8

 投稿者:しばっち  投稿日:2010年 3月27日(土)21時21分49秒
  !'最小2乗法により回帰式を求める。
LET N =15
RANDOMIZE
DIM X(N),Y(N)
LET A=INT(RND*5)+1
LET B=INT(RND*3)+1
LET C=INT(RND*1000)+500
LET MODE=INT(RND*8)+1
FOR I =1 TO N
   LET X(I)=I*2-1
   SELECT CASE MODE
   CASE 1
      LET Y(I)=C/(A+B*X(I))
   CASE 2
      LET Y(I)=C/(A*EXP(B*X(I)))
   CASE 3
      LET Y(I)=C/(A*X(I)^B)
   CASE 4
      LET Y(I)=C/(B*LOG(X(I))+A)
   CASE 5
      LET Y(I)=C/(A*B^X(I))
   CASE 6
      LET Y(I)=C/(A+B*SQR(X(I)))
   CASE 7
      LET Y(I)=C/(A+B/X(I))
   CASE 8
      LET Y(I)=C/(A+B*X(I)^(1/3))
   END SELECT
NEXT I
FOR I=1 TO N
   PRINT "X=";X(I);"Y=";Y(I);"^Y=";
   SELECT CASE MODE
   CASE 1
      PRINT FORECAST(X(I),N,Y,X)
   CASE 2
      PRINT FORECAST2(X(I),N,Y,X)
   CASE 3
      PRINT FORECAST3(X(I),N,Y,X)
   CASE 4
      PRINT FORECAST4(X(I),N,Y,X)
   CASE 5
      PRINT FORECAST5(X(I),N,Y,X)
   CASE 6
      PRINT FORECAST6(X(I),N,Y,X)
   CASE 7
      PRINT FORECAST7(X(I),N,Y,X)
   CASE 8
      PRINT FORECAST8(X(I),N,Y,X)
   END SELECT
NEXT I
END

EXTERNAL FUNCTION FORECAST(X,N,YA(),XA())
!'Y=C/(A+B*X)
DIM XX(2,2),YY(2),WW(2)
FOR I=1 TO N
   LET C=MAX(C,1/YA(I))
NEXT I
FOR I=1 TO N
   LET XX(1,1)=XX(1,1)+1/C
   LET XX(2,1)=XX(2,1)+XA(I)/C
   LET XX(1,2)=XX(1,2)+XA(I)/C
   LET XX(2,2)=XX(2,2)+XA(I)*XA(I)/C
   LET YY(1)=YY(1)+1/YA(I)
   LET YY(2)=YY(2)+1/YA(I)*XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST =C/(A+B*X)
END FUNCTION

EXTERNAL FUNCTION FORECAST2(X,N,YA(),XA())
!'Y=C/(A*EXP(B*X))
DIM XX(2,2),YY(2),WW(2)
LET C=-10000
FOR I=1 TO N
   LET C=MAX(C,LOG(1/YA(I)))
NEXT I
FOR I=1 TO N
   LET XX(1,1)=XX(1,1)+1
   LET XX(2,1)=XX(2,1)+XA(I)
   LET XX(1,2)=XX(1,2)+XA(I)
   LET XX(2,2)=XX(2,2)+XA(I)*XA(I)
   LET YY(1)=YY(1)+LOG(1/YA(I))
   LET YY(2)=YY(2)+LOG(1/YA(I))*XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=EXP(WW(1))*C
LET B=WW(2)
LET FORECAST2 =C/(A*EXP(B*X))
END FUNCTION

EXTERNAL FUNCTION FORECAST3(X,N,YA(),XA())
!'Y=C/(A*X^B)
DIM XX(2,2),YY(2),WW(2)
LET C=-10000
FOR I=1 TO N
   LET C=MAX(C,LOG(1/YA(I)))
NEXT I
FOR I=1 TO N
   LET XX(1,1)=XX(1,1)+1
   LET XX(2,1)=XX(2,1)+LOG(XA(I))
   LET XX(1,2)=XX(1,2)+LOG(XA(I))
   LET XX(2,2)=XX(2,2)+LOG(XA(I))*LOG(XA(I))
   LET YY(1)=YY(1)+LOG(1/YA(I))
   LET YY(2)=YY(2)+LOG(1/YA(I))*LOG(XA(I))
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=EXP(WW(1))*C
LET B=WW(2)
LET FORECAST3 =C/(A*X^B)
END FUNCTION

EXTERNAL FUNCTION FORECAST4(X,N,YA(),XA())
!'Y=C/(B*LOG(X)+A)
DIM XX(2,2),YY(2),WW(2)
FOR I=1 TO N
   LET C=MAX(C,1/YA(I))
NEXT I
FOR I=1 TO N
   LET XX(1,1)=XX(1,1)+1/C
   LET XX(2,1)=XX(2,1)+LOG(XA(I))/C
   LET XX(1,2)=XX(1,2)+LOG(XA(I))/C
   LET XX(2,2)=XX(2,2)+LOG(XA(I))*LOG(XA(I))/C
   LET YY(1)=YY(1)+1/YA(I)
   LET YY(2)=YY(2)+1/YA(I)*LOG(XA(I))
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST4 =C/(B*LOG(X)+A)
END FUNCTION

EXTERNAL FUNCTION FORECAST5(X,N,YA(),XA())
!'Y=C/(A*B^X)
DIM XX(2,2),YY(2),WW(2)
LET C=-100000
FOR I=1 TO N
   LET C=MAX(C,LOG(1/YA(I)))
NEXT I
FOR I=1 TO N
   LET XX(1,1)=XX(1,1)+1
   LET XX(2,1)=XX(2,1)+XA(I)
   LET XX(1,2)=XX(1,2)+XA(I)
   LET XX(2,2)=XX(2,2)+XA(I)*XA(I)
   LET YY(1)=YY(1)+LOG(1/YA(I))
   LET YY(2)=YY(2)+LOG(1/YA(I))*XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=EXP(WW(1))*C
LET B=EXP(WW(2))
LET FORECAST5 =C/(A*B^X)
END FUNCTION

EXTERNAL FUNCTION FORECAST6(X,N,YA(),XA())
!'Y=C/(A+B*SQR(X))
DIM XX(2,2),YY(2),WW(2)
FOR I=1 TO N
   LET C=MAX(C,1/YA(I))
NEXT I
FOR I=1 TO N
   LET XX(1,1)=XX(1,1)+1/C
   LET XX(2,1)=XX(2,1)+SQR(XA(I))/C
   LET XX(1,2)=XX(1,2)+SQR(XA(I))/C
   LET XX(2,2)=XX(2,2)+SQR(XA(I))*SQR(XA(I))/C
   LET YY(1)=YY(1)+1/YA(I)
   LET YY(2)=YY(2)+1/YA(I)*SQR(XA(I))
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST6 =C/(A+B*SQR(X))
END FUNCTION

EXTERNAL FUNCTION FORECAST7(X,N,YA(),XA())
!'Y=C/(A+B*(1/X))
DIM XX(2,2),YY(2),WW(2)
FOR I=1 TO N
   LET C=MAX(C,1/YA(I))
NEXT I
FOR I=1 TO N
   LET XX(1,1)=XX(1,1)+1/C
   LET XX(2,1)=XX(2,1)+1/XA(I)/C
   LET XX(1,2)=XX(1,2)+1/XA(I)/C
   LET XX(2,2)=XX(2,2)+1/XA(I)/XA(I)/C
   LET YY(1)=YY(1)+1/YA(I)
   LET YY(2)=YY(2)+1/YA(I)/XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST7 =C/(A+B/X)
END FUNCTION

EXTERNAL FUNCTION FORECAST8(X,N,YA(),XA())
!'Y=C/(A+B*X^(1/3))
DIM XX(2,2),YY(2),WW(2)
FOR I=1 TO N
   LET C=MAX(C,1/YA(I))
NEXT I
FOR I=1 TO N
   LET XX(1,1)=XX(1,1)+1/C
   LET XX(2,1)=XX(2,1)+(XA(I)^(1/3))/C
   LET XX(1,2)=XX(1,2)+(XA(I)^(1/3))/C
   LET XX(2,2)=XX(2,2)+(XA(I)^(2/3))/C
   LET YY(1)=YY(1)+1/YA(I)
   LET YY(2)=YY(2)+1/YA(I)*XA(I)^(1/3)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST8 =C/(A+B*X^(1/3))
END FUNCTION

EXTERNAL SUB CRAMER2(X(,),Y(),W()) !'クラーメル法
LET D=X(1,1)*X(2,2)-X(2,1)*X(1,2)
LET XX=Y(1)*X(2,2)-Y(2)*X(1,2)
LET YY=Y(2)*X(1,1)-Y(1)*X(2,1)
LET W(1)=XX/D
LET W(2)=YY/D
END SUB

!'EXTERNAL  SUB  CRAMER3 (X(,), Y(), W())
!'LET  D = X(1, 1) * X(2, 2) * X(3, 3) + X(2, 1) * X(3, 2) * X(1, 3) + X(3, 1) * X(1, 2) * X(2, 3) - X(3, 1) * X(2, 2) * X(1, 3) - X(2, 1) * X(1, 2) * X(3, 3) - X(1, 1) * X(3, 2) * X(2, 3)
!'LET  XX = Y(1) * X(2, 2) * X(3, 3) + Y(2) * X(3, 2) * X(1, 3) + Y(3) * X(1, 2) * X(2, 3) - Y(3) * X(2, 2) * X(1, 3) - Y(2) * X(1, 2) * X(3, 3) - Y(1) * X(3, 2) * X(2, 3)
!'LET  YY = X(1, 1) * Y(2) * X(3, 3) + X(2, 1) * Y(3) * X(1, 3) + X(3, 1) * Y(1) * X(2, 3) - X(3, 1) * Y(2) * X(1, 3) - X(2, 1) * Y(1) * X(3, 3) - X(1, 1) * Y(3) * X(2, 3)
!'LET  ZZ = X(1, 1) * X(2, 2) * Y(3) + X(2, 1) * X(3, 2) * Y(1) + X(3, 1) * X(1, 2) * Y(2) - X(3, 1) * X(2, 2) * Y(1) - X(2, 1) * X(1, 2) * Y(3) - X(1, 1) * X(3, 2) * Y(2)
!'LET  W(1) = XX / D
!'LET  W(2) = YY / D
!'LET  W(3) = ZZ / D
!'END SUB
 

戻る