필터 지우기
필터 지우기

Adding Bias To Random Walk

조회 수: 4 (최근 30일)
Kelly McGuire
Kelly McGuire 2019년 1월 14일
댓글: TADA 2019년 1월 14일
How would add a bias or probability of 34%, 22%, 22%, and 22% for each of the four directions in the attached random walk code?
  댓글 수: 3
Kelly McGuire
Kelly McGuire 2019년 1월 14일
Hey TADA, thanks for the link. I didn't find anything about adding a bias in each direction. Could you point that out for me?
TADA
TADA 2019년 1월 14일
Sorry, I was sure there was bias implementation there...

댓글을 달려면 로그인하십시오.

채택된 답변

TADA
TADA 2019년 1월 14일
편집: TADA 2019년 1월 14일
you can make a step & bias vector:
stepCoordinates = [1,0;0,1;-1,0;0,-1];
% this creates the bias to the north
bias = [ones(1, 34), repmat([2 3 4], 1, (100-34)/3)];
then when you generate the actual steps in your loop use this instead of what you had:
stepBiasIndex = randi(100, 1, numberOfSteps);
stepCoordIndex = bias(stepBiasIndex);
delta = stepCoordinates(stepCoordIndex,:);
if continuousSteps
signOfStep = sign(delta);
plusminusRandSign = [-1 1];
indicesWithSignZero = signOfStep == 0;
signOfStep(indicesWithSignZero) = plusminusRandSign(randi(2, size(signOfStep(indicesWithSignZero))));
delta = delta - signOfStep.*rand(numberOfSteps, 2);
end
deltax = delta(:,2)';
deltay = delta(:,1)';
You can vectorize the rest of it but you said it does what you want so i didn't touch anything else
In this specific solution the bias is only in percentage because the bias vector is a 1x100 vector
so if you want to be more specific about it you can make it a 1x1000 for promils or 1x10000 for higher precision...
I attached the edited file
  댓글 수: 5
Kelly McGuire
Kelly McGuire 2019년 1월 14일
Excellent! That is great, thank you.
TADA
TADA 2019년 1월 14일
Cheers! Good Luck

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2019년 1월 14일
There was a recent question in which someone asked for going immediately back to be prohibitted and for the probability of going forward to be doubled. My suggestion then was:
dd = randi(4)
for ss = 1:500
rr = rand;
if rr < 1/4
dd = 1+mod(dd,4) %next higher direction
elseif rr < 1/2
dd = 1+mod(dd-2,4) %next lower direction
end % 50 percent stays same direction
if dd==1
yy=yy+1; %north
elseif dd==2
xx=xx+1; %east
elseif dd==3
yy=yy-1; %south
else
xx=xx-1; %west
end
plot here probably
end
The initial dd is about picking some initial direction. The directions are numbered 1 (north), 2 (east), 3 (south), 4 (west), and the new direction is computed as a change of 0, -1 or +1 to the current direction (-2 was ruled out by not being permitted to go backwards.) You can modify the rr tests for whatever probabilities you want.
It is not clear to me in your question whether the 34% should be for a particular fixed direction (e.g., prefer to head east), or for a relative direction (e.g., prefer to go straight) ?
  댓글 수: 2
Kelly McGuire
Kelly McGuire 2019년 1월 14일
편집: Kelly McGuire 2019년 1월 14일
Thanks for the response! Any one of the directions can be the 34%, and the other three can have the 22%. For now, it doesn't matter, I just need one of the directions to be more probable than the other three. I'd like to add the bias to the code that I attached, because that code does everything exactly the way I want, just need to add some bias.
Walter Roberson
Walter Roberson 2019년 1월 14일
randsample() the direction numbers with a weights matrix.
Or construct
dv = [1*ones(1,34), 2*ones(1,22), 3*ones(1,22), 4*ones(1,22)]; %direction 1 is overrepresented
dx = [-1 0 1 0]; dy = [0 1 0 -1];
rand_direction = randi(length(dv), 1, num_steps_needed);
rand_dx = dx(rand_direction);
rand_dy = dy(rand_direction);
x_positions = [initial_x, cumsum(rand_dx)];
y_positions = [initial_y, cumsum(rand_dy)];

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by