回帰式

 投稿者:しばっち  投稿日:2010年 3月27日(土)21時13分39秒
  !'最小2乗法により回帰式を求める。
LET  N = 15 !'データ数
RANDOMIZE
DIM X(N), Y(N)
LET  A = RND !'乱数で係数を決める
LET  B = RND
LET MODE=INT(RND*8)+1
FOR I =1 TO N
   LET X(I)=I
   SELECT CASE MODE !'仮データ作成
   CASE 1
      LET Y(I)=A+B*X(I)
   CASE 2
      LET Y(I)=EXP(A*X(I))*B
   CASE 3
      LET Y(I)=A*X(I)^B
   CASE 4
      LET Y(I)=A*LOG(X(I))+B
   CASE 5
      LET Y(I)=A*B^X(I)
   CASE 6
      LET Y(I)=A*SQR(X(I))+B
   CASE 7
      LET Y(I)=A/X(I)+B
   CASE 8
      LET Y(I)=A*X(I)^(1/3)+B
   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
!'PRINT "R2=";R2(N,Y,X)
END

EXTERNAL  FUNCTION R2(N,YA(),XA()) !'決定係数(寄与率) 0<=r^2<=1
LET  YN=MEAN(N,YA)
FOR I=1 TO N
   LET  YY=FORECAST(XA(I),N,YA,XA)
   !' LET  YY=FORECAST2(XA(I),N,YA,XA)
   !' LET  YY=FORECAST3(XA(I),N,YA,XA)
   !' LET  YY=FORECAST4(XA(I),N,YA,XA)
   !' LET  YY=FORECAST5(XA(I),N,YA,XA)
   !' LET  YY=FORECAST6(XA(I),N,YA,XA)
   !' LET  YY=FORECAST7(XA(I),N,YA,XA)
   !' LET  YY=FORECAST8(XA(I),N,YA,XA)
   LET  SY=SY+(YA(I)-YN)^2
   LET  SE=SE+(YA(I)-YY)^2
NEXT I
LET  R2=1-SE/SY
END FUNCTION

EXTERNAL  FUNCTION MEAN(N,X()) !'平均
FOR I=1 TO N
   LET  S=S+X(I)
NEXT I
LET  MEAN=S/N
END FUNCTION

EXTERNAL  FUNCTION SLOPEA(N,YA(),XA())
LET  XN=MEAN(N,XA)
LET  YN=MEAN(N,YA)
FOR I=1 TO N
   LET  X=X+(XA(I)-XN)^2
   LET  XY=XY+(XA(I)-XN)*(YA(I)-YN)
NEXT I
LET  SLOPEA=XY/X
END FUNCTION

EXTERNAL  FUNCTION INTERCEPTA(N, YA(), XA())
LET  B = SLOPEA(N, YA, XA)
FOR I = 1 TO N
   LET  A = A + YA(I) - B * XA(I)
NEXT I
LET  INTERCEPTA = A / N
END FUNCTION

EXTERNAL  FUNCTION FORECASTA(X, N, YA(), XA())
!'Y=B+A*X
LET  A = SLOPEA(N, YA, XA)
LET  B = INTERCEPTA(N, YA, XA)
LET  FORECASTA = B + A * X
END FUNCTION

EXTERNAL  FUNCTION SLOPE(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + XA(I) * YA(I)
   LET   X = X + XA(I)
   LET   Y = Y + YA(I)
   LET  XX = XX + XA(I) * XA(I)
NEXT I
LET  SLOPE = (N * XY - X * Y) / (N * XX - X * X)
END FUNCTION

EXTERNAL  FUNCTION INTERCEPT(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + XA(I) * YA(I)
   LET   X = X + XA(I)
   LET   Y = Y + YA(I)
   LET  XX = XX + XA(I) * XA(I)
NEXT I
LET  INTERCEPT = (Y * XX - XY * X) / (N * XX - X * X)
END FUNCTION

EXTERNAL  FUNCTION FORECAST(X, N, YA(), XA())
!'Y=B+A*X
LET  A = SLOPE(N, YA, XA)
LET  B = INTERCEPT(N, YA, XA)
LET  FORECAST = B + A * X
END FUNCTION

EXTERNAL  FUNCTION SLOPE2(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + XA(I) * LOG(YA(I))
   LET   X = X + XA(I)
   LET   Y = Y + LOG(YA(I))
   LET  XX = XX + XA(I) * XA(I)
NEXT I
LET  SLOPE2 = (N * XY - X * Y) / (N * XX - X * X)
END FUNCTION

EXTERNAL  FUNCTION INTERCEPT2(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + XA(I) * LOG(YA(I))
   LET   X = X + XA(I)
   LET   Y = Y + LOG(YA(I))
   LET  XX = XX + XA(I) * XA(I)
NEXT I
LET  INTERCEPT2 = EXP((Y * XX - XY * X) / (N * XX - X * X))
END FUNCTION

EXTERNAL  FUNCTION FORECAST2(X, N, YA(), XA())
!'y=b*EXP(a*x)
!'LOG(y)=LOG(b)+a*x
!'Y=LOG(y) B=LOG(b) A=a X=x
!'Y=B+A*X
!'b=EXP(B)
LET  A = SLOPE2(N, YA, XA)
LET  B = INTERCEPT2(N, YA, XA)
LET  FORECAST2 = B * EXP(A * X)
END FUNCTION

EXTERNAL  FUNCTION SLOPE3(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + LOG(XA(I)) * LOG(YA(I))
   LET   X = X + LOG(XA(I))
   LET   Y = Y + LOG(YA(I))
   LET  XX = XX + LOG(XA(I)) * LOG(XA(I))
NEXT I
LET  SLOPE3 = (N * XY - X * Y) / (N * XX - X * X)
END FUNCTION

EXTERNAL  FUNCTION INTERCEPT3(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + LOG(XA(I)) * LOG(YA(I))
   LET   X = X + LOG(XA(I))
   LET   Y = Y + LOG(YA(I))
   LET  XX = XX + LOG(XA(I)) * LOG(XA(I))
NEXT I
LET  INTERCEPT3 = EXP((Y * XX - XY * X) / (N * XX - X * X))
END FUNCTION

EXTERNAL  FUNCTION FORECAST3(X, N, YA(), XA())
!'y=a*x^b
!'LOG(y)=LOG(a)+b*LOG(x)
!'Y=LOG(y) A=b B=LOG(a) X=LOG(x)
!'Y=B+A*X
!'a=EXP(B)
LET  B = SLOPE3(N, YA, XA)
LET  A = INTERCEPT3(N, YA, XA)
LET  FORECAST3 = A * X ^ B
END FUNCTION

EXTERNAL  FUNCTION SLOPE4(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + LOG(XA(I)) * YA(I)
   LET   X = X + LOG(XA(I))
   LET   Y = Y + YA(I)
   LET  XX = XX + LOG(XA(I)) * LOG(XA(I))
NEXT I
LET  SLOPE4 = (N * XY - X * Y) / (N * XX - X * X)
END FUNCTION

EXTERNAL  FUNCTION INTERCEPT4(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + LOG(XA(I)) * YA(I)
   LET   X = X + LOG(XA(I))
   LET   Y = Y + YA(I)
   LET  XX = XX + LOG(XA(I)) * LOG(XA(I))
NEXT I
LET  INTERCEPT4 = (Y * XX - XY * X) / (N * XX - X * X)
END FUNCTION

EXTERNAL  FUNCTION FORECAST4(X, N, YA(), XA())
!'y=b+a*LOG(x)
!'Y=y A=a X=LOG(x) B=b
!'Y=B+A*X
LET  A = SLOPE4(N, YA, XA)
LET  B = INTERCEPT4(N, YA, XA)
LET  FORECAST4 = A * LOG(X) + B
END FUNCTION

EXTERNAL  FUNCTION SLOPE5(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + XA(I) * LOG(YA(I))
   LET   X = X + XA(I)
   LET   Y = Y + LOG(YA(I))
   LET  XX = XX + XA(I) * XA(I)
NEXT I
LET  SLOPE5 = EXP((N * XY - X * Y) / (N * XX - X * X))
END FUNCTION

EXTERNAL  FUNCTION INTERCEPT5(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + XA(I) * LOG(YA(I))
   LET   X = X + XA(I)
   LET   Y = Y + LOG(YA(I))
   LET  XX = XX + XA(I) * XA(I)
NEXT I
LET  INTERCEPT5 = EXP((Y * XX - XY * X) / (N * XX - X * X))
END FUNCTION

EXTERNAL  FUNCTION FORECAST5(X, N, YA(), XA())
!'y=a*b^x
!'LOG(y)=LOG(a)+LOG(b)*x
!'Y=LOG(y) B=LOG(a) X=x A=LOG(b)
!'Y=B+A*X
!'a=EXP(B) b=EXP(A)
LET  B = SLOPE5(N, YA, XA)
LET  A = INTERCEPT5(N, YA, XA)
LET  FORECAST5 = A * B ^ X
END FUNCTION
 

Re: 回帰式

 投稿者:しばっち  投稿日:2010年 3月27日(土)21時14分27秒
  > No.1111[元記事へ]

続き

EXTERNAL  FUNCTION SLOPE6(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + SQR(XA(I)) * YA(I)
   LET   X = X + SQR(XA(I))
   LET   Y = Y + YA(I)
   LET  XX = XX + SQR(XA(I)) * SQR(XA(I))
NEXT I
LET  SLOPE6 = (N * XY - X * Y) / (N * XX - X * X)
END FUNCTION

EXTERNAL  FUNCTION INTERCEPT6(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + SQR(XA(I)) * YA(I)
   LET   X = X + SQR(XA(I))
   LET   Y = Y + YA(I)
   LET  XX = XX + SQR(XA(I)) * SQR(XA(I))
NEXT I
LET  INTERCEPT6 = (Y * XX - XY * X) / (N * XX - X * X)
END FUNCTION

EXTERNAL  FUNCTION FORECAST6(X, N, YA(), XA())
!'y=a*SQR(x)+b
!'X=SQR(x)
!'Y=B+A*X
LET  A = SLOPE6(N, YA, XA)
LET  B = INTERCEPT6(N, YA, XA)
LET  FORECAST6 = B + A * SQR(X)
END FUNCTION

EXTERNAL  FUNCTION SLOPE7(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + 1 / XA(I) * YA(I)
   LET   X = X + 1 / XA(I)
   LET   Y = Y + YA(I)
   LET  XX = XX + 1 / XA(I) * 1 / XA(I)
NEXT I
LET  SLOPE7 = (N * XY - X * Y) / (N * XX - X * X)
END FUNCTION

EXTERNAL  FUNCTION INTERCEPT7(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + 1 / XA(I) * YA(I)
   LET   X = X + 1 / XA(I)
   LET   Y = Y + YA(I)
   LET  XX = XX + 1 / XA(I) * 1 / XA(I)
NEXT I
LET  INTERCEPT7 = (Y * XX - XY * X) / (N * XX - X * X)
END FUNCTION

EXTERNAL  FUNCTION FORECAST7(X, N, YA(), XA())
!'y=a*(1/x)+b
!'X=1/x
!'Y=B+A*X
LET  A = SLOPE7(N, YA, XA)
LET  B = INTERCEPT7(N, YA, XA)
LET  FORECAST7 = B + A * (1/X)
END FUNCTION

EXTERNAL  FUNCTION SLOPE8(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + XA(I)^(1/3) * YA(I)
   LET   X = X + XA(I)^(1/3)
   LET   Y = Y + YA(I)
   LET  XX = XX + XA(I)^(1/3) * XA(I)^(1/3)
NEXT I
LET  SLOPE8 = (N * XY - X * Y) / (N * XX - X * X)
END FUNCTION

EXTERNAL  FUNCTION INTERCEPT8(N, YA(), XA())
FOR I = 1 TO N
   LET  XY = XY + XA(I)^(1/3) * YA(I)
   LET   X = X + XA(I)^(1/3)
   LET   Y = Y + YA(I)
   LET  XX = XX + XA(I)^(1/3) * XA(I)^(1/3)
NEXT I
LET  INTERCEPT8 = (Y * XX - XY * X) / (N * XX - X * X)
END FUNCTION

EXTERNAL  FUNCTION FORECAST8(X, N, YA(), XA())
!'y=a*x^(1/3)+b
!'X=x^(1/3)
!'Y=B+A*X
LET  A = SLOPE8(N, YA, XA)
LET  B = INTERCEPT8(N, YA, XA)
LET  FORECAST8 = B + A * X ^ (1/3)
END FUNCTION
 

戻る