Create random path with known end and start point

42 views (last 30 days)
Hi,
I would like to create "random" paths starting from one known value and ending to another one. They should not be too extreme so I have limited that I don't accept paths who get over twice as big or half as small than starting and ending points.
I have partial solution but it doesn't work when StartPoint and EndPoint are equal as it forces all values to be equal (forcing to different values to be same results this be definition)...
Any ideas? Not need to be perfect, but I would like to generate some of these to compare case where we get from start point to end point via straight line.
PathLength=100;
NumberOfSimulations=10;
StartPoint=1000;
EndPoint=2000;
Saved=NaN*zeros(PathLength, NumberOfSimulations);
n=0;
while n~=NumberOfSimulations
r =randn(PathLength,1); %Random nromally distributed numbers
c=cumsum(r);
%Linear equation to scale values with start and end point
k=(EndPoint-StartPoint)/(c(end)-c(1));
b=StartPoint-k*c(1);
d=c*k+b;
if (max(d)>(2*max([StartPoint,EndPoint]))|| min(d)<min([StartPoint, EndPoint])/2);
%not good path
else
n=n+1;
all(:,n)=d;
end
end
figure; hold on; plot(all) ; plot(mean(all,2),'k','Linewidth',2.5);
//Edit I found one solution but it is only for same size steps. Still need help.
%%http://cnr.lwlss.net/ConstrainedRandomWalk/
figure;close all;
%Biased random walk
NumberOfSimulations=10;
n=100; % number of steps, nt increasing and n(t-1) decreasing
x0=10; %StartPoint
xtarg=20; %End point after n steps
Saved=NaN*zeros(n+1,NumberOfSimulations+2);
Saved(1,:)=x0;
Saved(n+2,:)=xtarg;
for q=1:NumberOfSimulations
unifs=rand(n+1,1);
x=x0;
for i=0:(n-1)
t =(1-(xtarg-x)/(n-i))/2;
if unifs(i+1,1)<=t
x=x-1;
else
x=x+1;
end
Saved(i+2,q)=x;
end
end
figure; hold on; plot(Saved) ; plot(mean(Saved,2),'k','Linewidth',2.5);

Accepted Answer

John D'Errico
John D'Errico on 12 Aug 2016
Edited: John D'Errico on 14 Aug 2016
The goal is a ONE dimensional random walk?
What distribution do you want for the steps? normal? Uniform? Yes, I know that you are using randn, but is that what you WANT?
I have no idea what you mean by half as big or small. Provide a clear explanation. Is this relative to the start point? The difference from start to end? Since either of those points might be zero, or the difference zero, BE CLEAR. I can help you, but I need to know some answers first.
Edit: Since no answer, I'll make some assumptions. So normally distributed random steps in the walk.
startpoint = 0;
endpoint = 10;
% number of steps
N = 100;
% Nominal stddev for each step
s = 1;
steps = randn(1,N-1)*s;
% ensure the sum is exactly zero
steps = steps - mean(steps);
% add in a bias to each step.
steps = steps + (endpoint - startpoint)/(N-1);
walk = cumsum([startpoint,steps]);
walk(1)
ans =
0
walk(end)
ans =
10
So a random walk, with normal steps.
You talked about a max or a min step size. But really, the problem was in how you were trying to renormalize the random sample. You were trying to adjust the standard deviation, when in fact, you wanted to adjust the mean.
  2 Comments
John D'Errico
John D'Errico on 14 Aug 2016
Edited: John D'Errico on 14 Aug 2016
No response. So I will point out that IF you want normally distributed steps, then it is trivial. The problem is simply that of generating a set that has a known sum.
So just generate a set from randn. Subtract off the mean of the sample from each element, so the sum is now exactly zero. Then add in a fixed bias to every element, that will sum to the target sum.
It is more difficult to get a good uniform sampling, that will actually follow a uniform distribution.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 14 Aug 2016
Edited: Image Analyst on 14 Aug 2016
Kaisa, what's wrong with just using linspace to set the starting and ending points, and number of steps in between, and then adding noise.
numSteps = 20;
x = 1 : numSteps;
y = linspace(1, 50, numSteps);
noise = rand(1, length(y));
yNoisy = y + 20 * noise - 10; % Adjust 20 as desired.
% Make sure first and last points are not altered.
y(2:end-1) = yNoisy(2:end-1); % Set all but first and last points.
plot(x, y, 'bo-');
grid on;

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!