Python中的MatLab回归
我有一些使用蒙特卡罗模拟和OLS回归来估计一些系数的Matlab代码。 如何在Python中实现这一点Python中的MatLab回归,python,matlab,regression,montecarlo,Python,Matlab,Regression,Montecarlo,我有一些使用蒙特卡罗模拟和OLS回归来估计一些系数的Matlab代码。 如何在Python中实现这一点 有一些方法可以在python中使用MATLAB代码!!。。检查下面的链接 [检查此项]嘿,你的问题不清楚你在问什么,你在python中缺少哪些功能,你在matlab中有哪些功能?你想做最小二乘回归吗?如果是这样,试试看,sklearn有很多预测工具!我想知道如何在Python中进行类似于Matlab命令x1(1:n)的回归。您能帮我一下吗,并告诉我\n在Matlab中做了什么?我已经有一段时
有一些方法可以在python中使用MATLAB代码!!。。检查下面的链接
[检查此项]嘿,你的问题不清楚你在问什么,你在python中缺少哪些功能,你在matlab中有哪些功能?你想做最小二乘回归吗?如果是这样,试试看,sklearn有很多预测工具!我想知道如何在Python中进行类似于Matlab命令x1(1:n)的回归。您能帮我一下吗,并告诉我\n在Matlab中做了什么?我已经有一段时间没用了。它和使用的是同一个吗?它意味着回归!所以它用x1作为预测器回归y。欢迎来到堆栈溢出!请拿起,阅读,和,并提供一个。“为我实现此功能”是本网站的主题。你必须做一个诚实的尝试,然后问一个关于你的算法或技术的特定问题。答案不应该仅仅是关于堆栈溢出的其他帖子的链接。如果您认为该问题适用,则应将其标记为重复问题。请阅读并采取行动。欢迎来到SO!
Define the true DGP parameters and distributions
% Set the parameters in the model
b1 = 5;
b2 = -2;
% The variance of the error term
sigma2 = 2;
% The sample length. We will play with three different sample sizes and see how this affects the results
N = [100 500 1000];
% The number of simulations
S = 1000;
Generate x data
% Generate the x values as draws from the multivariate normal distributions
% This is the correlation structure between the x's
Sigma = [0.7 0.4;
0.4 0.3];
% Simple way of drawing random numbers from the multivariate normal distribution
x = chol(Sigma)'*randn(2,max(N));
% Make the x1 and x2 variables
x1 = x(1,:)';
x2 = x(2,:)';
Monte Carlo simulation 1
y = b1*x1 + e is the true model
We will now simulate data from this model and then use OLS to estimate two versions of the model:
y = b1mc*x1 + e and
y = b1mc_2*x1 + b2*x2 + e
% Always good practive to allocate empty output before loops
b1mc = nan(S, numel(N));
b1mc_2 = nan(S, numel(N));
% Simple counter to use when allocating results into b1mc below
cnt = 1;
for n = N % Loop over the different sample sizes N
for s = 1 : S
% generate random errors
u = randn(n,1)*sqrt(sigma2);
% simulate the process
y = b1*x1(1:n) + u;
% Estimate coefficients by OLS (easy in Matlab) and save
b1mc(s,cnt) = x1(1:n)\y;
tmp = [x1(1:n) x2(1:n)]\y;
b1mc_2(s,cnt) = tmp(1); % Only save the first parameter
end
cnt = cnt + 1;
end