The matrix extension adds a new matrix data structure to NetLogo. A matrix is a mutable 2-dimensional array containing only numbers.
行列拡張機能はNetLogoで行列データ構造を使えるようにします。行列は数値のみを含むミュータブルな2次元配列です。
Although matrices store numbers, much like a list of lists, or an array of arrays, the primary reason to use the matrix data type is to take advantage of special mathematical operations associated with matrices. For instance, matrix multiplication is a convenient way to perform geometric transformations, and the repeated application of matrix multiplication can also be used to simulate other dynamic processes (for instance, processes on graph/network structures).
行列は、リストのリストや配列の配列と同様に数値を格納しますが、行列データ形式を用いる最大の理由は、行列に関連する特別な数学的演算を活用することにあります。たとえば、行列の操作は幾何学的変換を行ううえで便利な方法ですし、行列の掛け算の反復適用は他の動的過程(たとえばグラフ/ネットワーク構造の処理)をシミュレートするためにも用いられます。
If you'd like to know more about matrices and how they can be used, you might consider a course on linear algebra, or search the web for tutorials. The matrix extension also allows you to solve linear algebraic equations (specified in a matrix format), and even to identify trends in your data and perform linear (ordinary least squares) regressions on data sets with multiple explanatory variables.
行列がどのように使われるのかをもっとよく知りたいのであれば、線形代数学の授業を受講するか、ウェブのチュートリアルを探してください。行列拡張機能を用いれば線形代数方程式(行列の形式で記載されたもの)を解くこともできますし、複数の説明変数を用いたデータセットへの線形回帰(最小二乗法)を行ってデータのトレンドを特定することもできます。
The matrix extension comes preinstalled.
行列拡張機能はプリインストールされています。
To use the matrix extension in your model, add a line to the top of your Code tab:
モデルで行列拡張機能を使用するには、コードタブの最初の行に次のように書き加えます:
extensions [matrix]
extensions [matrix]
If your model already uses other extensions, then it already has an extensions line in it, so just add matrix to the list.
モデルですでに他の拡張機能を使用しているときは、extensions行がすでにありますので、単にリストにmatrixを加えます。
For more information on using NetLogo extensions, see the Extensions Guide.
NetLogoの拡張機能についてもっとよく知りたいのであれば、拡張機能ガイドを参照してください。
let m matrix:from-row-list [[1 2 3] [4 5 6]] print m => {{matrix: [ [ 1 2 3 ][ 4 5 6 ] ]}} print matrix:pretty-print-text m => [[ 1 2 3 ] [ 4 5 6 ]] print matrix:dimensions m => [2 3] ;;(NOTE: row & column indexing starts at 0, not 1) print matrix:get m 1 2 ;; what number is in row 1, column 2? => 6 matrix:set m 1 2 10 ;; change the 6 to a 10 print m => {{matrix: [ [ 1 2 3 ][ 4 5 10 ] ]}} let m2 matrix:make-identity 3 print m2 => {{matrix: [ [ 1 0 0 ][ 0 1 0 ][ 0 0 1 ] ]}} print matrix:times m m2 ;; multiplying by the identity changes nothing => {{matrix: [ [ 1 2 3 ][ 4 5 10 ] ]}} ;; make a new matrix with the middle 1 changed to -1 let m3 (matrix:set-and-report m2 1 1 -1) print m3 => {{matrix: [ [ 1 0 0 ][ 0 -1 0 ][ 0 0 1 ] ]}} print matrix:times m m3 => {{matrix: [ [ 1 -2 3 ][ 4 -5 10 ] ]}} print matrix:to-row-list (matrix:plus m2 m3) => [[2 0 0] [0 0 0] [0 0 2]]
let m matrix:from-row-list [[1 2 3] [4 5 6]] print m => {{matrix: [ [ 1 2 3 ][ 4 5 6 ] ]}} print matrix:pretty-print-text m => [[ 1 2 3 ] [ 4 5 6 ]] print matrix:dimensions m => [2 3] ;;(注意: 行と列のインデックスは0から始まる数である。1からではない) print matrix:get m 1 2 ;; 第1行第2列の数値は何か? => 6 matrix:set m 1 2 10 ;; 6を10に変更する print m => {{matrix: [ [ 1 2 3 ][ 4 5 10 ] ]}} let m2 matrix:make-identity 3 print m2 => {{matrix: [ [ 1 0 0 ][ 0 1 0 ][ 0 0 1 ] ]}} print matrix:times m m2 ;; 単位行列と掛け算をしても何も変わらない => {{matrix: [ [ 1 2 3 ][ 4 5 10 ] ]}} ;; 中央の1を-1に変更した新しい行列を作る let m3 (matrix:set-and-report m2 1 1 -1) print m3 => {{matrix: [ [ 1 0 0 ][ 0 -1 0 ][ 0 0 1 ] ]}} print matrix:times m m3 => {{matrix: [ [ 1 -2 3 ][ 4 -5 10 ] ]}} print matrix:to-row-list (matrix:plus m2 m3) => [[2 0 0] [0 0 0] [0 0 2]]
Code Example: Matrix Example
コード例: Matrix Example
The Matrix extension for NetLogo was originally written by Forrest Stonedahl, with significant contributions from Charles Staelin (in particular, the forecast & regression primitives). The matrix extension provides a wrapper around Jama, which is a free/open-source matrix library for Java.
NetLogoの行列拡張機能はForrest Stonedahlが最初に作成し、Charles Staelinが、特に予測と回帰のプリミティブにおいて重要な貢献を果たしました。行列拡張機能はJavaのオープンソースの行列ライブラリであるJamaの周辺のラッパーを提供するものです。
The source code for the Matrix extension is hosted on GitHub You are more than welcome to modify/improve upon the existing functionality and features and we encourage you to contribute your changes back to the NetLogo community.
行列拡張機能のソースコードはGitHubにホストされています。あなたが既存の機能を修正・改善してくれるのであれば歓迎されます。あなたが加えた変更をNetLogoのコミュニティに還元してください。
matrix:make-constant matrix:make-identity matrix:from-row-list matrix:from-column-list matrix:to-row-list matrix:to-column-list matrix:copy matrix:pretty-print-text
matrix:get matrix:get-row matrix:get-column matrix:set matrix:set-row matrix:set-column matrix:swap-rows matrix:swap-columns matrix:set-and-report matrix:dimensions matrix:submatrix
matrix:times-scalar matrix:times matrix:times-element-wise matrix:plus-scalar matrix:plus matrix:inverse matrix:transpose matrix:real-eigenvalues matrix:imaginary-eigenvalues matrix:eigenvectors matrix:det matrix:rank matrix:cond matrix:trace
matrix:solve matrix:forecast-linear-growth matrix:forecast-compound-growth matrix:forecast-continuous-growth matrix:regress
Reports a new n-rows by n-cols matrix object, with all entries in the matrix containing the same value (number).
行列のすべての成分を同じ値(数値)とする(行数)行(列数)列の新しい行列オブジェクトを返します。
Reports a new square matrix object (with dimensions n-size x n-size), consisting of the identity matrix (1s along the main diagonal, 0s elsewhere).
単位行列(主対格成分がすべて1、その他の成分は0の行列)からなる新しい正方行列オブジェクト(次数×次数)を返します。
Reports a new matrix object, created from a NetLogo list, where each item in that list is another list (corresponding to each of the rows of the matrix.)
リストの各要素がリスト(行列の個別の行に対応)になっているNetLogoのリストから作成された新しい行列オブジェクトを返します。
print matrix:from-row-list [[1 2] [3 4]] => {{matrix: [ [ 1 2 ][ 3 4 ] ]}} ;; Corresponds to this matrix: ;; 1 2 ;; 3 4
print matrix:from-row-list [[1 2] [3 4]] => {{matrix: [ [ 1 2 ][ 3 4 ] ]}} ;; 下記の行列に対応: ;; 1 2 ;; 3 4
Reports a new matrix object, created from a NetLogo list containing each of the columns of the matrix.
行列の個別の列を含むNetLogoリストから作成された新しい行列オブジェクトを返します。
Reports a list of lists, containing each row of the matrix.
行列の個別の行を格納したリストのリストを返します。
Reports a list of lists, containing each column of the matrix.
行列の個別の列を格納したリストのリストを返します。
Reports a new matrix that is an exact copy of the given matrix. This primitive is important because the matrix type is mutable (changeable). Here's a code example:
与えられた行列を正確にコピーした新たな行列を返します。行列オブジェクトがミュータブル(変更可能)であるため、このプリミティブは重要です。以下はコード例です:
let m1 matrix:from-column-list [[1 4 7][2 5 8][3 6 9]] ; a 3x3 matrix print m1 => {{matrix: [ [ 1 2 3 ][ 4 5 6 ][ 7 8 9 ] ]}} let m2 m1 ;; m2 refers to the same matrix object as m1 let m3 matrix:copy m1 ;; m3 is a new copy containing m1's data matrix:set m1 0 0 100 ;; now m1 is changed print m1 => {{matrix: [ [ 100 2 3 ][ 4 5 6 ][ 7 8 9 ] ]}} print m2 => {{matrix: [ [ 100 2 3 ][ 4 5 6 ][ 7 8 9 ] ]}} ;;Notice that m2 was also changed, when m1 was changed! print m3 => {{matrix: [ [ 1 2 3 ][ 4 5 6 ][ 7 8 9 ] ]}}
let m1 matrix:from-column-list [[1 4 7][2 5 8][3 6 9]] ; a 3x3 matrix print m1 => {{matrix: [ [ 1 2 3 ][ 4 5 6 ][ 7 8 9 ] ]}} let m2 m1 ;; m2はm1と同じ行列オブジェクトを参照している let m3 matrix:copy m1 ;; m3はm1のデータを格納した新しいコピーである matrix:set m1 0 0 100 ;; m1に変更を加えた print m1 => {{matrix: [ [ 100 2 3 ][ 4 5 6 ][ 7 8 9 ] ]}} print m2 => {{matrix: [ [ 100 2 3 ][ 4 5 6 ][ 7 8 9 ] ]}} ;;m1が変更されたためm2も変更されていることに注意! print m3 => {{matrix: [ [ 1 2 3 ][ 4 5 6 ][ 7 8 9 ] ]}}
Reports a string that is a textual representation of the matrix, in a format that is reasonably human-readable when displayed.
行列を表現する文字列を、画面上で人間が読みやすい書式で返します。
Reports the (numeric) value at location row-i,col-j in the given matrix.
与えられた行列のi行j列の値(数値)を返します。
Reports a simple (not nested) NetLogo list containing the elements of row-i of the given matrix.
与えられた行列のi行の要素からなる(2段階になっていない)NetLogoリストを返します。
Reports a simple (not nested) NetLogo list containing the elements of col-j of the given matrix.
与えられた行列のj列の要素からなる(2段階になっていない)NetLogoリストを返します。
Changes the given matrix, by setting the locationrow-i,col-j to new-value
与えられた行列のi行j列を新しい値に変更します。
Changes the given matrix matrix by replacing the row at row-i with the contents of the simple (not nested) NetLogo list simple-list. The simple-list must have a length equal to the number of columns in the matrix, i.e., the matrix row length.
与えられた行列のi行を(2段階になっていない)NetLogoリストの内容と置き換えます。そのリストは行列の列数、すなわち行の長さと同じ長さである必要があります。
Changes the given matrix matrix by replacing the column at col-j with the contents of the simple (not nested) NetLogo list simple-list. The simple-list must have a length equal to the number of rows in the matrix, i.e., the matrix column length.
与えられた行列のj列を(2段階になっていない)NetLogoリストの内容と置き換えます。そのリストは行列の行数、すなわち列の長さと同じ長さである必要があります。
Changes the given matrix matrix by swapping the rows at row1 and row2 with each other.
与えられた行列の行1と行2を入れ替えます。
Changes the given matrix matrix by swapping the columns at col1 and col2 with each other.
与えられた行列の列1と列2を入れ替えます。
Reports a new matrix, which is a copy of the given matrix except that the value at row-i,col-j has been changed to new-value. A NetLogo statement such as "set mat matrix:set-and-report mat 2 3 10" will result in mat pointing to this new matrix, a copy of the old version of mat with the element at row 2, column 3 being set to 10. The old version of mat will be "lost".
与えられた行列のi行j列を新しい値に入れ替えた行列のコピーを返します。"set mat matrix:set-and-report mat 2 3 10" のようなNetLogo文を用いると、行列matは、matの古いバージョンの第2行第3列要素を10に入れ替えた新しい行列を指します。matの古いバージョンは失われます。
Reports a 2-element list ([num-rows,num-cols]), containing the number of rows and number of columns in the given matrix
与えられた行列の行数と列数を含む2要素のリスト([行数,列数])を返します。
Reports a new matrix object, consisting of a rectangular subsection of the given matrix. The rectangular region is from row r1 up to (but not including) row r2, and from column c1 up to (but not including) column c2. Here is an example:
与えられた行列の一部分の長方形の領域からなる新しい行列オブジェクトを返します。長方形の領域は、r1行からr2行まで(r2行自身は含まない)、c1列からc2列まで(c2列自身は含まない)からなります。以下は例です:
let m matrix:from-row-list [[1 2 3][4 5 6][7 8 9]] print matrix:submatrix m 0 1 2 3 ; matrix, row-start, col-start, row-end, col-end ; rows from 0 (inclusive) to 2 (exclusive), ; columns from 1 (inclusive) to 3 (exclusive) => {{matrix: [ [ 2 3 ][ 5 6 ] ]}}
let m matrix:from-row-list [[1 2 3][4 5 6][7 8 9]] print matrix:submatrix m 0 1 2 3 ; 行列, 最初の行, 最初の列, 最後の行, 最後の列 ; 行は第0行(自身を含む)から第2行まで(自身を含まない)、 ; 列は第1行(自身を含む)から第3行まで(自身を含まない) => {{matrix: [ [ 2 3 ][ 5 6 ] ]}}
Reports a new matrix, which is the result of multiplying every entry in the original matrix by the given scaling factor.
元の行列のすべての要素に係数を掛けた新しい行列を返します。
Reports a matrix, which is the result of multiplying matrix m1 by matrix m2 (using standard matrix multiplication -- make sure your matrix dimensions match up.)
行列1に行列2を掛けた積を返します(通常の行列の積を用います。行列の次元数が増えることに注意してください)。
Reports a matrix, which is the result of multiplying each element of matrix m1 by the corresponding element in m2. (Note: m1 and m2 must have the same dimensions).
行列1の各要素に行列2の対応する各要素を掛けた、行列の要素ごとの積を返します。(行列1と行列2とは同じ次元でなくてはなりません)
Reports a matrix, which is the result of adding the constant number to each element of the given matrix.
与えられた行列の各要素に定数値を加算した行列を返します。
Reports a matrix, which is the result of adding each element of matrix m1 to the corresponding element in m2. (Note: m1 and m2 must have the same dimensions).
行列1の各要素に行列2の対応する各要素を加算した、行列の要素ごとの和を返します。(行列1と行列2とは同じ次元でなくてはなりません)
Reports the inverse of the given matrix, or results in an error if the matrix is not invertible.
与えられた行列の逆行列を返します。逆行列が存在しないときはエラーを返します。
Reports the transpose of the given matrix.
与えられた行列の転置行列を返します。
Reports a list containing the real eigenvalues of the given matrix.
与えられた行列の実数固有値のリストを返します。
Reports a list containing the imaginary eigenvalues of the given matrix.
与えられた行列の虚数固有値のリストを返します。
Reports a matrix that contains the eigenvectors of the given matrix. (Each eigenvector as a column of the resulting matrix.)
与えられた行列の固有ベクトルからなる行列を返します(各固有ベクトルは行列の列となります)。
Reports the determinant of the matrix.
与えられた行列の行列式(det)を返します。
Reports the effective numerical rank of the matrix, obtained from SVD (Singular Value Decomposition).
与えられた行列の階数(rank)を返します。特異値分解(SVD)を用いて計算します。
Reports the matrix condition (2 norm), which is the ratio of largest to smallest singular value (obtained from SVD).
行列の条件数(2ノルム)を返します。これはSVDを用いて計算した最大の特異値と最小の特異値の比率です。
Reports the trace of the matrix, which is simply the sum of the main diagonal elements.
行列の跡(トレース)を返します。これは行列の主対角成分の総和です。
Reports the solution to a linear system of equations, specified by the A and C matrices. In general, solving a set of linear equations is akin to matrix division. That is, the goal is to find a matrix B such that A * B = C. (For simple linear systems, C and B can both be 1-dimensional matrices -- i.e. vectors). If A is not a square matrix, then a "least squares" solution is returned.
行列Aと行列Cによって規定された一次方程式の解を返します。一般的には、連立一次方程式の解を求めることは行列の除算に近似します。言い換えれば、A * B = C を満たす行列Bを求めることが目的となります(単純な一次系では、行列Cと行列Bはともに1次元行列、すなわちベクトルとなります)。Aが正方行列でない場合は最小二乗解を返します。
;; To solve the set of equations x + 3y = 10 and 7x - 4y = 20 ;; We make our A matrix [[1 3][7 -4]], and our C matrix [[10][20]] let A matrix:from-row-list [[1 3][7 -4]] let C matrix:from-row-list [[10][20]] print matrix:solve A C => {{matrix: [ [ 4 ][ 2.0000000000000004 ] ]}} ;; NOTE: as you can see, the the results may be only approximate ;; (In this case, the true solution should be x=4 and y=2.)
;; x + 3y = 10 と 7x - 4y = 20 の連立方程式を解くために、 ;; 行列Aとして [[1 3][7 -4]] 、 行列Cとして [[10][20]] を作成する。 let A matrix:from-row-list [[1 3][7 -4]] let C matrix:from-row-list [[10][20]] print matrix:solve A C => {{matrix: [ [ 4 ][ 2.0000000000000004 ] ]}} ;; 注意:上記のように計算結果は近似値である。 ;; (このケースでは真の解は x=4 と y=2 である。)
Reports a four-element list of the form: [ forecast constant slope R2 ]. The forecast is the predicted next value that would follow in the sequence given by the data-list input, based on a linear trend-line. Normally data-list will contain observations on some variable, Y, from time t = 0 to time t = (n-1) where n is the number of observations. The forecast is the predicted value of Y at t = n. The constant and slope are the parameters of the trend-line
[ 予測値 定数 傾き R2 ] からなる4要素のリストを返します。予測値は、データリストを入力値としたときの線形のトレンドライン上に次に来ると予測される値となります。一般的には、データリストは n を観測数としたときの t = 0 から t = (n-1) までの変数 Y の観測値を含みます。予測値は t = n のときに予測される Y の値です。定数と傾きはトレンドラインのパラメータです。
Y = constant + slope * t.
Y = 定数 + 傾き * t.
The R2 value measures the goodness of fit of the trend-line to the data, with an R2 = 1 being a perfect fit and an R2 of 0 indicating no discernible trend. Linear growth assumes that the variable Y grows by a constant absolute amount each period.
R2 値はトレンドラインのデータへのあてはまりの良さを示す値で、 R2 = 1 は完全に当てはまること、 R2 = 0 は明確なトレンドがまったくないことを意味します。線形の成長は各時点において変数 Y が一定値で増加することを仮定しています。
;; a linear extrapolation of the next item in the list. print matrix:forecast-linear-growth [20 25 28 32 35 39] => [42.733333333333334 20.619047619047638 3.6857142857142824 0.9953743395474031] ;; These results tell us: ;; * the next predicted value is roughly 42.7333 ;; * the linear trend line is given by Y = 20.6190 + 3.6857 * t ;; * Y grows by approximately 3.6857 units each period ;; * the R^2 value is roughly 0.9954 (a good fit)
;; 次のリストの要素の線形外挿。 print matrix:forecast-linear-growth [20 25 28 32 35 39] => [42.733333333333334 20.619047619047638 3.6857142857142824 0.9953743395474031] ;; この結果からわかることは、 ;; * 次の予測値はおおむね 42.7333 ;; * 線形のトレンドラインは Y = 20.6190 + 3.6857 * t で与えられる ;; * Y は各時点において約 3.6857 増加する ;; * R^2 値は約 0.9954 (当てはまりは良い)
Reports a four-element list of the form: [ forecast constant growth-proportion R2 ]. Whereas matrix:forecast-linear-growth assumes growth by a constant absolute amount each period, matrix:forecast-compound-growth assumes that Y grows by a constant proportion each period. The constant and growth-proportion are the parameters of the trend-line
[ 予測値 定数 成長比 R2 ] からなる4要素のリストを返します。matrix:forecast-linear-growthでは各時点における一定値の成長を仮定している一方で、matrix:forecast-compound-growthでは Y は各時点において一定の成長比で成長すると仮定します。定数と成長比は下記のトレンドラインのパラメータです。
Y = constant * growth-proportiont.
Y = 定数 * 成長比t.
Note that the growth proportion is typically interpreted as growth-proportion = (1.0 + growth-rate). Therefore, if matrix:forecast-compound-growth returns a growth-proportion of 1.10, that implies that Y grows by (1.10 - 1.0) = 10% each period. Note that if growth is negative, matrix:forecast-compound-growth will return a growth-proportion of less than one. E.g., a growth-proportion of 0.90 implies a growth rate of -10%.
成長比は 成長比 = (1.0 + 成長率) と解釈できることに注意してください。したがってmatrix:forecast-compound-growthが成長比として 1.10 を返すならば、 Y は各時点において (1.10 - 1.0) = 10% の成長率で成長します。成長率がマイナスならばmatrix:forecast-compound-growthは成長比として1.0以下の値を返します。成長比が 0.90 ならば成長率は -10% です。
NOTE: The compound growth forecast is achieved by taking the ln of Y. (See matrix:regress, below.) Because it is impossible to take the natural log of zero or a negative number, matrix:forecast-compound-growth will result in an error if it finds a zero or negative number in data-list.
注意:平均成長比の予測は Y の自然対数を取ることで行われます。(下記のmatrix:regressを参照してください) ゼロや負数の自然対数を取ることはできませんので、データリスト中にゼロか負数があればmatrix:forecast-compound-growthはエラーとなります。
;; a compound growth extrapolation of the next item in the list. print matrix:forecast-compound-growth [20 25 28 32 35 39] => [45.60964465307147 21.15254147944863 1.136621034423892 0.9760867518334806] ;; These results tell us: ;; * the next predicted value is approximately 45.610 ;; * the compound growth trend line is given by Y = 21.1525 * 1.1366 ^ t ;; * Y grows by approximately 13.66% each period ;; * the R^2 value is roughly 0.9761 (a good fit)
;; リストの次の要素による平均成長率の外挿 print matrix:forecast-compound-growth [20 25 28 32 35 39] => [45.60964465307147 21.15254147944863 1.136621034423892 0.9760867518334806] ;; この結果からわかることは、 ;; * 次の予測値はおおむね 45.610 ;; * 平均成長のトレンドラインは Y = 21.1525 * 1.1366 ^ t で与えられる ;; * Y は各時点において約 13.66% 増加する ;; * R^2 値は約 0.9761 (当てはまりは良い)
Reports a four-element list of the form: [ forecast constant growth-rate R2 ]. Whereas matrix:forecast-compound-growth assumes discrete time with Y growing by a given proportion each finite period of time (e.g., a month or a year), matrix:forecast-continuous-growth assumes that Y is compounded continuously (e.g., each second or fraction of a second). The constant and growth-rate are the parameters of the trend-line
[ 予測値 定数 成長率 R2 ] からなる4要素のリストを返します。matrix:forecast-compound-growthでは離散時間、すなわち有限の各時間間隔(例えば月や年)において与えられた成長比で成長する Y を仮定しているのに対して、matrix:forecast-continuous-growthでは連続時間 (例えば秒単位以下の微小時間) における平均を仮定しています。定数と成長率は下記のトレンドラインのパラメータです。
Y = constant * e(growth-rate * t).
Y = 定数 * e(成長率 * t).
matrix:forecast-continuous-growth is the "calculus" analog of matrix:forecast-compound-growth. The two will normally yield similar (but not identical) results, as shown in the example below. growth-rate may, of course, be negative.
matrix:forecast-continuous-growthはmatrix:forecast-compound-growthの計算上の類似形です。これら2つはほぼ同じ(しかし同一ではない)結果を計算します。成長率は負数になり得ます。
NOTE: The continuous growth forecast is achieved by taking the ln of Y. (See matrix:regress, below.) Because it is impossible to take the natural log of zero or a negative number, matrix:forecast-continuous-growth will result in an error if it finds a zero or negative number in data-list.
注意:連続成長率の予測は Y の自然対数を取ることで行われます。(下記のmatrix:regressを参照してください) ゼロや負数の自然対数を取ることはできませんので、データリスト中にゼロか負数があればmatrix:forecast-continuous-growthはエラーとなります。
;; a continuous growth extrapolation of the next item in the list. print matrix:forecast-continuous-growth [20 25 28 32 35 39] => [45.60964465307146 21.15254147944863 0.12805985615332668 0.9760867518334806] ;; These results tell us: ;; * the next predicted value is approximately 45.610 ;; * the compound growth trend line is given by Y = 21.1525 * e ^ (0.1281 * t) ;; * Y grows by approximately 12.81% each period if compounding takes place continuously ;; * the R^2 value is roughly 0.9761 (a good fit)
;; リストの次の要素による連続成長率の外挿 print matrix:forecast-continuous-growth [20 25 28 32 35 39] => [45.60964465307146 21.15254147944863 0.12805985615332668 0.9760867518334806] ;; この結果からわかることは、 ;; * 次の予測値はおおむね 45.610 ;; * 平均成長のトレンドラインは Y = 21.1525 * e ^ (0.1281 * t) で与えられる ;; * 平均成長率を連続値におきかえれば Y は各時点において約 12.81% 増加する ;; * R^2 値は約 0.9761 (当てはまりは良い)
All three of the forecast primitives above are just special cases of performing an OLS (ordinary-least-squares) linear regression -- the matrix:regress primitive provides a flexible/general-purpose approach. The input is a matrix data-matrix, with the first column being the observations on the dependent variable and each subsequent column being the observations on the (1 or more) independent variables. Thus each row consists of an observation of the dependent variable followed by the corresponding observations for each independent variable.
上記の3つの予測プリミティブはOLS(最小二乗法)の活用の特殊ケースですが、matrix:regressは一般的な使用法を提供します。第1列を従属変数の観測値、第2列以降を(1個以上の)独立変数の観測値とするデータ行列をインプットとします。したがってそれぞれの列は従属変数の観測値とそれに対応する各独立変数の観測値からなります。
The output is a Logo nested list composed of two elements. The first element is a list containing the regression constant followed by the coefficients on each of the independent variables. The second element is a 3-element list containing the R2 statistic, the total sum of squares, and the residual sum of squares. The following code example shows how the matrix:regress primitive can be used to perform the same function as the code examples shown in the matrix:forecast-*-growth primitives above. (However, keep in mind that the matrix:regress primitive is more powerful than this, and can have many more independent variables in the regression, as indicated in the fourth example below.)
アウトプットは2つの要素からなるネストされた(2段階の)NetLogoリストとなります。第1の要素は回帰定数と各独立変数に対応する回帰係数からなるリストです。第2の要素はR2、平方和、残差平方和からなる3要素のリストです。下記のコード例は、どのようにmatrix:regressプリミティブが上記のmatrix:forecast-*-growthプリミティブのコード例と同じ機能として利用できるかを示すものです。(しかしながら、matrix:regressプリミティブはこれよりも強力であり、下記の4番目の例に示すようにより多くの独立変数を取ることができます。
;; this is equivalent to what the matrix:forecast-linear-growth does let data-list [20 25 28 32 35 39] let indep-var (n-values length data-list [?]) ; 0,1,2...,5 let lin-output matrix:regress matrix:from-column-list (list data-list indep-var) let lincnst item 0 (item 0 lin-output) let linslpe item 1 (item 0 lin-output) let linR2 item 0 (item 1 lin-output) ;;Note the "6" here is because we want to forecast the value at time t=6. print (list (lincnst + linslpe * 6) (lincnst) (linslpe) (linR2)) ;; this is equivalent to what the matrix:forecast-compound-growth does let com-log-data-list (map [ln ?] [20 25 28 32 35 39]) let com-indep-var2 (n-values length com-log-data-list [?]) ; 0,1,2...,5 let com-output matrix:regress matrix:from-column-list (list com-log-data-list com-indep-var2) let comcnst exp item 0 (item 0 com-output) let comprop exp item 1 (item 0 com-output) let comR2 item 0 (item 1 com-output) ;;Note the "6" here is because we want to forecast the value at time t=6. print (list (comcnst * comprop ^ 6) (comcnst) (comprop) (comR2)) ;; this is equivalent to what the matrix:forecast-continuous-growth does let con-log-data-list (map [ln ?] [20 25 28 32 35 39]) let con-indep-var2 (n-values length con-log-data-list [?]) ; 0,1,2...,5 let con-output matrix:regress matrix:from-column-list (list con-log-data-list con-indep-var2) let concnst exp item 0 (item 0 con-output) let conrate item 1 (item 0 con-output) let conR2 item 0 (item 1 con-output) print (list (concnst * exp (conrate * 6)) (concnst) (conrate) (conR2)) ;; example of a regression with two independent variables: ;; Pretend we have a dataset, and we want to know how well happiness ;; is correlated to snack-food consumption and accomplishing goals. let happiness [2 4 5 8 10] let snack-food-consumed [3 4 3 7 8] let goals-accomplished [2 3 5 8 9] print matrix:regress matrix:from-column-list (list happiness snack-food-consumed goals-accomplished) => [[-0.14606741573033788 0.3033707865168543 0.8202247191011234] [0.9801718440185063 40.8 0.8089887640449439]] ;; linear regression: happiness = -0.146 + 0.303*snack-food-consumed + 0.820*goals-accomplished ;; (Since the 0.820 coefficient is higher than the 0.303 coefficient, it appears that each goal ;; accomplished yields more happiness than does each snack consumed, although both are positively ;; correlated with happiness.) ;; Also, we see that R^2 = 0.98, so the two factors together provide a good fit.
;; 下記は matrix:forecast-linear-growth で行うのと同じ処理内容になる let data-list [20 25 28 32 35 39] let indep-var (n-values length data-list [?]) ; 0,1,2...,5 let lin-output matrix:regress matrix:from-column-list (list data-list indep-var) let lincnst item 0 (item 0 lin-output) let linslpe item 1 (item 0 lin-output) let linR2 item 0 (item 1 lin-output) ;;下記の "6" は予測したい値が t=6 のときのものであるため print (list (lincnst + linslpe * 6) (lincnst) (linslpe) (linR2)) ;; 下記は matrix:forecast-compound-growth で行うのと同じ処理内容になる let com-log-data-list (map [ln ?] [20 25 28 32 35 39]) let com-indep-var2 (n-values length com-log-data-list [?]) ; 0,1,2...,5 let com-output matrix:regress matrix:from-column-list (list com-log-data-list com-indep-var2) let comcnst exp item 0 (item 0 com-output) let comprop exp item 1 (item 0 com-output) let comR2 item 0 (item 1 com-output) ;;下記の "6" は予測したい値が t=6 のときのものであるため print (list (comcnst * comprop ^ 6) (comcnst) (comprop) (comR2)) ;; 下記は matrix:forecast-continuous-growth で行うのと同じ処理内容になる let con-log-data-list (map [ln ?] [20 25 28 32 35 39]) let con-indep-var2 (n-values length con-log-data-list [?]) ; 0,1,2...,5 let con-output matrix:regress matrix:from-column-list (list con-log-data-list con-indep-var2) let concnst exp item 0 (item 0 con-output) let conrate item 1 (item 0 con-output) let conR2 item 0 (item 1 con-output) ;;下記の "6" は予測したい値が t=6 のときのものであるため print (list (concnst * exp (conrate * 6)) (concnst) (conrate) (conR2)) ;; 2独立変数による回帰の例: ;; 下記データのhappinessが、snack-food-consumedとgoals-accomplishedに ;; どのくらい比例しているかを知りたいものとする let happiness [2 4 5 8 10] let snack-food-consumed [3 4 3 7 8] let goals-accomplished [2 3 5 8 9] print matrix:regress matrix:from-column-list (list happiness snack-food-consumed goals-accomplished) => [[-0.14606741573033788 0.3033707865168543 0.8202247191011234] [0.9801718440185063 40.8 0.8089887640449439]] ;; 線形回帰: happiness = -0.146 + 0.303*snack-food-consumed + 0.820*goals-accomplished ;; (0.820という係数は0.303という係数よりも大きいので、snack-food-consumedと ;; goals-accomplishedとは、両方ともhappinessと正比例するが、一単位当たりでは ;; goals-accomplishedの方がより多くのhappinessをもたらすことわかる。 ;; また、 R^2 = 0.98 であるので、2つの要素を合わせると良く当てはまることがわかる。