-2

I have some Matlab code on using Monte Carlo Simulation and then OLS regression to estimate some coefficients. How is it possible to do this in Python?

see screenshot of code, or below

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
  • 1
    Hey, it's not clear from your question what you're asking, which functionality are you missing from python, that you do have in matlab? Do you want to do least squares regression? If so, try [this](https://python.plainenglish.io/ols-linear-regression-basics-with-pythons-scikit-learn-4ecfe88145b), sklearn has a lot of predictor tools – Leander Mar 09 '21 at 12:14
  • Hi! I'm wondering how to do similar regression in Python as the Matlab command x1(1:n)\y – koder124 Mar 09 '21 at 12:21
  • Could you help me out, and tell me what \ does in matlab? Been a while since I used it. Is it the same one as used [here](https://stackoverflow.com/questions/7160162/left-matrix-division-and-numpy-solve) ? – Leander Mar 09 '21 at 12:22
  • it means regression! So it regresses y with x1 as a predictor. – koder124 Mar 09 '21 at 12:25
  • I think [this](https://stackoverflow.com/questions/7160162/left-matrix-division-and-numpy-solve) covers it then, np.linalg.lstsq should do the trick :) – Leander Mar 09 '21 at 12:29
  • Hi again. It did not work... The dimensions are wrong. Do you know any other tricks? – koder124 Mar 09 '21 at 12:37
  • Could have to do with the fact that matlab array indexing is different to that of python. Matlab starts at 1, while python starts at 0. So if you copied you x1(1:n) to x1[1:n], you'll most likely run into that issue – Leander Mar 09 '21 at 13:36

1 Answers1

-1

There are some ways you can use MATLAB code in python!!..Check below link

[Check this] A tool to convert MATLAB code to Python

  • Answers should not be just links to other posts on Stack Overflow. You should instead flag the question as a duplicate if you feel that is applicable. Please read [answer] and take the [tour]. Welcome to SO! – Pranav Hosangadi Mar 10 '21 at 04:54