# Monte Carlo - Random Walk Simulation - polyfit the log log data points?

This is part of the code in matlab for a random-walk simulation.

• To test the code, I'm using steps=; there will be more values, but I want to run it for 1 trial to decrease code processing.
• log_steps = log(1:steps); <--- corresponds to the log (steps vector) for the x axis of the plot
• log_AVG = log(d_AVG); <---- corresponds to the log (average steps sizes) for the y axis of the plot

The intended approach to prove that $$p$$ which represents the probability of any step (forward || backward) is 0.5.

PROBLEM: the program's p value estimation is 10x larger than it should be. It gives a value between 4 to 5 for p, when p should be about 0.5.

where is the logic wrong? Relevant code below.

    figure(i+10);
hold on;
loglog(log_steps, log_AVG,'-s');
%loglog(1:steps(i), d_AVG, '-s');

N=log_steps;
c= log_AVG;

p = polyfit(N, c,0);
f = (c.* (N.^p));
hold on;
loglog(N, f);
hold off;
end;  • What language is this? Can you reformat your question so the code displays nicely and the images show? – Richard May 1 '19 at 23:51

If I understood the first image correctly, you should do a linear fit with $$\ln(N)$$ being the independent variable and, presumably, $$\ln(d)$$ the dependent variable.
Furthermore, it seems to me that in your code you use $$c$$ as $$\log(d_{AVG})$$, which is different from the first image, where $$c$$ appears to be a constant.