多元线性回归模型代码
多元线性回归模型代码
using System;
//using System.Math;
public class MatrixMultiply
{
public static void Main()
{
int a, b, p = 0;
//Console.WriteLine("该程序将求出两个矩阵的积:");
Console.WriteLine("请问所用模型为几元模型?:");
b = int.Parse(Console.ReadLine());
b = b + 1;
Console.WriteLine("请指定数据时间段数:");
a = int.Parse(Console.ReadLine());
double[,] MatrixX = new double[a, b];
for (int i = 0; i < a; i++)
{
for (int j = 1; j < b; j++)
{
//Console.WriteLine("请输入矩阵X第{0}行第{1}列的值:", i + 1, j + 1);
MatrixX[i, j] = double.Parse(Console.ReadLine());
MatrixX[i, 0] = 1;
}
}
Console.WriteLine("矩阵X输入完毕.");
//Console.WriteLine("请指定矩阵Y的行数:");
//c = int.Parse(Console.ReadLine());
//Console.WriteLine("请指定矩阵Y的列数:");
//d = int.Parse(Console.ReadLine());
double[,] MatrixY = new double[a, 1];
for (int i = 0; i < a; i++)
{
for (int j = 0; j < 1; j++)
{
Console.WriteLine("请输入矩阵Y第{0}行第{1}列的值:", i + 1, j + 1);
MatrixY[i, j] = double.Parse(Console.ReadLine());
}
}
Console.WriteLine("矩阵Y输入完毕.");
Console.WriteLine("矩阵X为:");
outputMatrix(MatrixX, a, b);
Console.WriteLine("矩阵Y为:");
outputMatrix(MatrixY, a, 1);
double[,]MatrixA = new double [b,a];
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
MatrixA[j, i] = MatrixX[i, j];
}
Console.WriteLine("矩阵X的转置矩阵A为:");
outputMatrix(MatrixA, b, a);
double[,] MatrixB = new double[b, b];
for (int i = 0; i < b; i++)
{
for (int k = 0; k < b; k++)
{
for (int j = 0; j < a; j++)
MatrixB[i, k] += MatrixA[i, j] * MatrixX[j, k];
}
}
Con