回帰式 その9

 投稿者:しばっち  投稿日:2010年 3月27日(土)21時23分2秒
  !'最小2乗法により回帰式を求める。
LET N=15
RANDOMIZE
DIM X(N),Y(N)
LET A=INT(RND*3)+2
LET B=INT(RND*3)+2
LET MODE=INT(RND*10)+1
FOR I =1 TO N
   LET X(I)=I*2
   SELECT CASE MODE
   CASE 1
      LET Y(I)=SQR(X(I))/(A+B*X(I))
   CASE 2
      LET Y(I)=LOG(X(I))/(A+B*X(I))
   CASE 3
      LET Y(I)=SQR(X(I))/LOG(A+B*X(I))
   CASE 4
      LET Y(I)=SQR(X(I))/EXP(A+B*X(I))
   CASE 5
      LET Y(I)=EXP(X(I))/SQR(A+B*X(I))
   CASE 6
      LET Y(I)=LOG(X(I))/(A+B*X(I))^(1/3)
   CASE 7
      LET Y(I)=X(I)^2/(A+B*X(I))
   CASE 8
      LET Y(I)=EXP(X(I))/(A+B*X(I))
   CASE 9
      LET Y(I)=1/(1+1/(A+B*X(I)))
   CASE 10
      LET Y(I)=X(I)/(A+B*(1/X(I)))
   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)
   CASE 9
      PRINT FORECAST9(X(I),N,Y,X)
   CASE 10
      PRINT FORECAST10(X(I),N,Y,X)
   END SELECT
NEXT I
END

EXTERNAL FUNCTION FORECAST(X,N,YA(),XA())
DIM XX(2,2),YY(2),WW(2)
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)+SQR(XA(I))/YA(I)
   LET YY(2)=YY(2)+SQR(XA(I))*XA(I)/YA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST =SQR(X)/(A+B*X)
END FUNCTION

EXTERNAL FUNCTION FORECAST2(X,N,YA(),XA())
DIM XX(2,2),YY(2),WW(2)
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(XA(I))/YA(I)
   LET YY(2)=YY(2)+LOG(XA(I))/YA(I)*XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST2 =LOG(X)/(A+B*X)
END FUNCTION

EXTERNAL FUNCTION FORECAST3(X,N,YA(),XA())
DIM XX(2,2),YY(2),WW(2)
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)+EXP(SQR(XA(I))/YA(I))
   LET YY(2)=YY(2)+EXP(SQR(XA(I))/YA(I))*XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST3 =SQR(X)/LOG(A+B*X)
END FUNCTION

EXTERNAL FUNCTION FORECAST4(X,N,YA(),XA())
DIM XX(2,2),YY(2),WW(2)
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(SQR(XA(I))/YA(I))
   LET YY(2)=YY(2)+LOG(SQR(XA(I))/YA(I))*XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST4=SQR(X)/EXP(A+B*X)
END FUNCTION

EXTERNAL FUNCTION FORECAST5(X,N,YA(),XA())
DIM XX(2,2),YY(2),WW(2)
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)+(EXP(XA(I))/YA(I))^2
   LET YY(2)=YY(2)+(EXP(XA(I))/YA(I))^2*XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST5 =EXP(X)/SQR(A+B*X)
END FUNCTION

EXTERNAL FUNCTION FORECAST6(X,N,YA(),XA())
DIM XX(2,2),YY(2),WW(2)
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(XA(I))/YA(I))^3
   LET YY(2)=YY(2)+(LOG(XA(I))/YA(I))^3*XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST6 =LOG(X)/(A+B*X)^(1/3)
END FUNCTION

EXTERNAL FUNCTION FORECAST7(X,N,YA(),XA())
DIM XX(2,2),YY(2),WW(2)
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)^2
   LET YY(1)=YY(1)+XA(I)^2/YA(I)
   LET YY(2)=YY(2)+XA(I)^2/YA(I)*XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST7=X^2/(A+B*X)
END FUNCTION

EXTERNAL FUNCTION FORECAST8(X,N,YA(),XA())
DIM XX(2,2),YY(2),WW(2)
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)^2
   LET YY(1)=YY(1)+EXP(XA(I))/YA(I)
   LET YY(2)=YY(2)+EXP(XA(I))/YA(I)*XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST8 =EXP(X)/(A+B*X)
END FUNCTION

EXTERNAL  FUNCTION FORECAST9(X,N,YA(),XA())
DIM XX(2,2),YY(2),WW(2)
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)+YA(I)/(1-YA(I))
   LET  YY(2)=YY(2)+YA(I)/(1-YA(I))*XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET  A=WW(1)
LET  B=WW(2)
LET  FORECAST9= 1/(1+1/(A+B*X))
END FUNCTION

EXTERNAL FUNCTION FORECAST10(X,N,YA(),XA())
DIM XX(2,2),YY(2),WW(2)
FOR I=1 TO N
   LET XX(1,1)=XX(1,1)+1
   LET XX(2,1)=XX(2,1)+1/XA(I)
   LET XX(1,2)=XX(1,2)+1/XA(I)
   LET XX(2,2)=XX(2,2)+1/XA(I)^2
   LET YY(1)=YY(1)+XA(I)/YA(I)
   LET YY(2)=YY(2)+XA(I)/YA(I)/XA(I)
NEXT I
CALL CRAMER2(XX,YY,WW)
LET A=WW(1)
LET B=WW(2)
LET FORECAST10=X/(A+B/X)
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
 

戻る