回帰式 その4

 投稿者:しばっち  投稿日:2010年 3月27日(土)21時17分26秒
  !'和の公式により回帰式を求める
LET N =24
DIM Y(N)
RANDOMIZE
LET A=RND*5
LET B=RND*5
!'LET C=RND*5
!'LET D=RND*5
!'LET E=RND*500+500
FOR I=1 TO N
   LET Y(I)=A+B*I
   !' LET Y(I)=E/(A+B*I)
   !' LET Y(I)=E/(A+B*I+C*I^2)
   !' LET Y(I)=E/(A+B*I+C*I^2+D*I^3)
NEXT I
FOR I=1 TO N
   PRINT "X=";I;"Y=";Y(I);"^Y=";FORECAST(I,N,Y)
   !' PRINT "X=";I;"Y=";Y(I);"^Y=";FORECAST2(I,N,Y)
   !' PRINT "X=";I;"Y=";Y(I);"^Y=";FORECAST3(I,N,Y)
   !' PRINT "X=";I;"Y=";Y(I);"^Y=";FORECAST4(I,N,Y)
NEXT I
END

EXTERNAL FUNCTION FORECAST(X,N,YA())
!'Y=A+B*X
!'S=A*Σ1+B*ΣX
!'S1=(A+B)+(A+2*B)+(A+3*B)+(A+4*B)+...(A+N*B)
!'S2=(A+(N+1)*B)+(A+(N+2)*B)+(A+(N+3)*B)+...(A+2*N*B)
!'S1=N*A+B*N/2*((1)+(N))
!'S2=N*A+B*N/2*((N+1)+(2*N))
!'S2-S1=B*N/2*(2*N)
!'B=(S2-S1)/N^2
!'A=(S1-B*N/2*(N+1))/N
LET NN=INT(N/2) !'データ数は2の倍数
FOR I=1 TO NN
   LET S1=S1+YA(I)
   LET S2=S2+YA(NN+I)
NEXT I
LET B=(S2-S1)/NN/NN
LET A=(S1-B*NN/2*(NN+1))/NN
LET FORECAST =A+B*X
END FUNCTION

EXTERNAL FUNCTION FORECAST2(X,N,YA())
LET NN=INT(N/2) !'データ数は2の倍数
FOR I=1 TO NN
   LET S1=S1+1/YA(I)
   LET S2=S2+1/YA(NN+I)
   LET C=MAX(1/YA(I),C)
   LET C=MAX(1/YA(I+NN),C)
NEXT I
LET B=C*(S2-S1)/NN/NN
LET A=C*S1/NN-B/2*(NN+1)
LET FORECAST2 =C/(A+B*X)
END FUNCTION

EXTERNAL FUNCTION FORECAST3(X,N,YA())
LET NN=INT(N/3) !'データ数は3の倍数
FOR I=1 TO NN
   LET S1=S1+1/YA(I)
   LET S2=S2+1/YA(NN+I)
   LET S3=S3+1/YA(2*NN+I)
   LET D=MAX(1/YA(I),D)
   LET D=MAX(1/YA(I+NN),D)
   LET D=MAX(1/YA(I+2*NN),D)
NEXT I
LET C=D*(S3+S1-2*S2)/(2*NN^3)
LET B=D/NN^2*(S2-S1)-C*(2*NN+1)
LET A=D/NN*S1-B/2*(NN+1)-C/6*(2*NN^2+3*NN+1)
LET FORECAST3 =D/(A+B*X+C*X^2)
END FUNCTION

EXTERNAL FUNCTION FORECAST4(X,N,YA())
!'1/Y=A/E Σ1+B/E ΣX+C/E Σ(X^2)+D/E Σ(X^3)
!'S1=E/A*N+B/N*(N^2+N)/2+C/E*(2*N^3+3*N^2+N)/6+D/E*(N^4+2*N^3+N^2)/4
!'S2=E/A*N+B/N*(3*N^2+N)/2+C/E*(14*N^3+9*N^2+N)/6+D/E*(15*N^4+14*N^3+3*N^2)/4
!'S3=E/A*N+B/N*(5*N^2+N)/2+C/E*(38*N^3+15*N^2+N)/6+D/E*(65*N^4+38*N^3+5*N^2)/4
!'S4=E/A*N+B/N*(7*N^2+N)/2+C/E*(74*N^3+21*N^2+N)/6+D/E*(175*N^4+74*N^3+7*N^2)/4
!'S2-S1=B/E*N^2+C/E*(2*N^3+N^2)+D/E*(14*N^4+12*N^3+2*N^2)/4
!'S3-S2=B/E*N^2+C/E*(4*N^3+N^2)+D/E*(50*N^4+24*N^3+2*N^2)/4
!'S4-S3=B/E*N^2+C/E*(6*N^3+N^2)+D/E*(110*N^4+36*N^3+2*N^2)/4
!'S3+S1-2*S2=C/E*2*N^3+D/E*(36*N^4+12*N^3)/4
!'S4+S2-2*S3=C/E*2*N^3+D/E*(60*N^4+12*N^3)/4
LET NN=INT(N/4) !'データ数は4の倍数
FOR I=1 TO NN
   LET S1=S1+1/YA(I)
   LET S2=S2+1/YA(NN+I)
   LET S3=S3+1/YA(2*NN+I)
   LET S4=S4+1/YA(3*NN+I)
   LET E=MAX(1/YA(I),E)
   LET E=MAX(1/YA(I+NN),E)
   LET E=MAX(1/YA(I+2*NN),E)
   LET E=MAX(1/YA(I+3*NN),E)
NEXT I
LET D=E*(S4+3*S2-3*S3-S1)/(6*NN^4)
LET C=E*(S3+S1-2*S2-D/E*(9*NN^4+3*NN^3))/(2*NN^3)
LET B=E*(S2-S1-C/E*(2*NN^3+NN^2)-D/E*(14*NN^4+12*NN^3+2*NN^2)/4)/(NN^2)
LET A=E*S1/NN-B*(NN+1)/2-C*(2*NN^2+3*NN+1)/6-D*(NN^3+2*NN^2+NN)/4
LET FORECAST4 =E/(A+B*X+C*X^2+D*X^3)
END FUNCTION
 

戻る