Brownian Motion: Difference between revisions

From bradwiki
Jump to navigation Jump to search
No edit summary
 
(110 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[File:Brownian-Diffusion.gif|right]]
Brownian motion describes the stochastic [[diffusion]] of particles as they travel through ''n''-dimensional spaces filled with other particles and physical barriers. Here the term ''particle'' is a generic term that can be generalized to describe the motion of molecule (e.g. H<sub>2</sub>O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see [https://en.wikipedia.org/wiki/Random_walk random walk]) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the [[normal distribution]]. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements.
{{TOC right}}


==Background==


Brownian motion describes the stochastic movement of particles as they travel through space. This type of random movement is often referred to as a ''random walk'', which is typical of particles that diffuse about a 1D 2D or 3D space filled with other particles or barriers. To understand Brownian motion, lets start by characterizing this phenomenon in 2D space. [[File:Brownian5.gif|right]] The mathematical description of this process often includes these terms:
==Properties of Brownian Motion==


Physical experiments characterize the following properties of Brownian motion...
* continuous increments
* increments of a particle over disjoint time intervals are independent events
* each increment is assumed to result from collisions with many molecules
* each increment is assumed to have a normal probability distribution
* the mean increment is zero as there is no preferred direction
* the position of a particle spreads out with time
* the variance of the increment is proportional to the length of time that Brownian motion has been observed


==== <big>D</big> &nbsp;(<big><code>Δ</code></big>): diffusion rate coefficient ====
:* <big><code>D = L<sup>2</sup> / 2d⋅t</code></big>
:: <big><code>L</code></big> : step length
:: <big><code>d</code></big> : dimensions
:: <big><code>t</code></big> : time
: <big><code>D (in units: '''µm²/s''')</code></big> is the mean diffusion rate per unit time, often in µm²/s for biological motion on a molecular level. This refers to how fast, on average, the particle moves along its trajectories. This value is often of ultimate interest, particularly for simulating Brownian motion; however under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle's movement, and would have to be done on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this: {{Fig|[[File:Stochastic Diffusion.gif]]}}. Instead, '''D''' is often calculated from the ''mean squared diffusion'' (MSD) path of the particle, defined below.


<HTML5video>Stochastic_Diffusion</HTML5video>
The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:


::<math>
f(x, \mu, \sigma) = \frac{1}{\sigma \sqrt{2\pi} } e^{ - \frac{1}{2} (\frac{x-\mu}{\sigma})^2 }
</math>


==== <big>MSD</big> &nbsp;(<big><code>Ω</code></big>): mean squared displacement ====
Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:
:* <big><code>MSD {{=}} 2d⋅D</code></big>
: <big><code>MSD (in units: '''µm²''')</code></big> is the mean squared displacement of a particle over some time period.


::<math>
\frac{1}{\sqrt{t2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{t}})^2 }
</math>


==== <big>K</big> &nbsp;(<big><code>κ</code></big>): standard deviation of the normal distribution for '''D''' ====
where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:
:* <big><code>k {{=}} √(d⋅D)</code></big>
: <big><code>k (in units: '''µm''')</code></big> is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate '''D'''. This value is useful for simulating Brownian motion for a particular diffusion rate.


::<math>
P[B(t+u) - B(t) \leq a] = \int_{x=-\infty}^{a} \frac{1}{\sqrt{u2\pi} } e^{ - \frac{1}{2} (\frac{x}{\sqrt{u}})^2 } dx
</math>


==== <big>L</big> &nbsp;(<big><code>λ</code></big>): mean step length ====
:* <big><code>L {{=}} √(2d⋅D)</code></big>
: <big><code>L (in units: '''µm''')</code></big> is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration. This equation sometimes shows up in the equivalent form <big><code>L = (2d * D)<sup>.5</sup></code></big>


==Common Diffusion Terms & Parameters==
<br>
I've found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include:


==== <big>Lx</big> &nbsp;(<big><code>λˣ</code></big>): mean 1D step length component of L ====
:* <big><code>λˣ {{=}} L / √2</code></big>
: <big><code>Lx or λˣ (in units: '''µm''')</code></big> is the average 1-dimensional step length for each component (X,Y,Z) dimension of '''''L'''''. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λˣ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2(λˣ)² = λ² where 2(λˣ)² represents a² + b² and λ² represents c².


=== <big>D</big> &nbsp;: diffusion rate coefficient ===


==== <big>Ls</big> &nbsp;(<big><code>λˢ</code></big>): step length scalar coefficient ====
::<math>
:* <big><code>Ls {{=}} 1 / √(D/Ds)</code></big>
D = {L^{2} \over 2d \cdot t}
:: <big><code>Ds</code></big> : new desired diffusion rate
</math>
: <big><code>Ls or λˢ (in units: units)</code></big> is a coefficient value that, when multiplied by each Lx component step length, will scale those lengths to achieve a new diffusion rate '''Ds'''. After scaling, the new diffusion rate = D/(D/Ds).


:: <big><code>L</code></big> : step length
:: <big><code>d</code></big> : dimensions
:: <big><code>t</code></big> : time




<big><code>D (in units: '''µm²/s''')</code></big> is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once '''D''' is known, this Diffusion Coefficient can then be easily implemented in [[computer]] simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle's movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this:


{{Clear}}
Instead, '''D''' is often calculated from the ''mean squared diffusion'' (MSD) path of the particle, defined below.


==Brownian Motion Matlab Code==


<syntaxhighlight lang="matlab" line start="1" highlight="1" enclose="div">
=== Mean Squared Displacement (MSD)===
function [boom] = BradsBrownianMotionScript()
<br>
format compact;
format short;
close all;


% Note: This matlab script requires 'msdanalyzer' toolbox
::<math>
%-------------############################------------------%
MSD = { 2d \cdot D}
%                STARTING PARAMETERS
</math>
%-------------############################------------------%
D = .3;                    % Diffusion Rate
Ds = .1;                    % Diffusion Scalar (Ds = Dn)
Dn = D/(D/Ds);              % new D after scaling L
d = 2;                      % dimensions
dT = 1;                    % time step
k = sqrt(d*D);             % stdev of D's step size distribution
MSD = 2*d*D;                % mean squared displacement
L = sqrt(2*d*D);            % average diagonal (2D) step size
Lx = L/sqrt(2);            % average linear (1D) step size
Ls = 1/sqrt(D/Ds);          % scales Lx values for Dn


:: <big><code>d</code></big> : dimensions
:: <big><code>D</code></big> : diffusion coefficient


MSDtest = [1 0 0]; % test: D, Dn, or L
Scale = 1/10; % scale of model
Ndots = 100;
Nsteps = Ndots;


<big><code>MSD (in units: '''µm²''')</code></big> is the mean square displacement of a particle over some time period.


xyl = ones(2,Ndots);
=== Standard deviation (k) of the normal distribution of step lengths ===
xyds = ones(2,Ndots);
<br>
lims = ((D+1)^2)*10;


::<math>
k = \sqrt{d \cdot D}
</math>


%===========================================================%
:: <big><code>d</code></big> : dimensions
%              LIVE PARTICLE DIFFUSION
:: <big><code>D</code></big> : diffusion coefficient
%-----------------------------------------------------------%
for t = 1:Nsteps


    xyds = STEPxyds(Ndots, k);
[xyl] = AMPARSTEP(Ndots, xyds, xyl);
MAINPLOT(xyl, lims);


end
<big><code>k (in units: '''µm''')</code></big> is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate '''D'''. This value is useful for simulating Brownian motion for a particular diffusion rate.
%===========================================================%




%===========================================================%
%              MSD RANDOM STEPS ANALYSIS
%-----------------------------------------------------------%
tracks = cell(Ndots, 1);


stepN = 1;
===Mean Step Length (L)===
for t = 1:Nsteps
<br>


xyds = STEPxyds(Ndots, k);
::<math>
[xyl] = AMPARSTEP(Ndots, xyds, xyl);
L = \sqrt{2d \cdot D}
    [tracks] = MSDfun(stepN, Nsteps, tracks, xyds);
</math>


stepN = stepN+1;
:: <big><code>d</code></big> : dimensions
end
:: <big><code>D</code></big> : diffusion coefficient
MSDfunction(tracks,Ndots,Nsteps,D,Dn,L,dT,k,Scale,MSDtest);
 
 
 
 
 
%===========================================================%
%              MSD UNIFORM STEPS ANALYSIS
%-----------------------------------------------------------%
stepN = 1;
for t = 1:Nsteps
 
xyds = stepsize(Ndots, Lx);
[xyl xyds] = MSDAMPARSTEP(Ndots, xyds, xyl, Ls);
    [tracks] = MSDfun(stepN, Nsteps, tracks, xyds);
 
stepN = stepN+1;
end
MSDfunction(tracks,Ndots,Nsteps,D,Dn,L,dT,k,Scale,MSDtest);
 
 
boom = 'done';
%-------------############################------------------%
end %        ##    END MAIN FUNCTION  ##
%-------------############################------------------%
%%
 
 
 
 
 
%%
%-------------############################------------------%
%            ##      SUBFUNCTIONS      ##
%-------------############################------------------%
 
 
%-------------------------------------------%
% STEP SIZE GENERATOR
%-------------------------------------------%
function xyds = STEPxyds(Ndots, k)
 
    xyds = (k * randn(2,Ndots));
 
end
 
 
%-------------------------------------------%
% MOVE PARTICLES MAIN FUNCTION
%-------------------------------------------%
function [xyl] = AMPARSTEP(Ndots, xyds, xyl)
for j = 1:Ndots
        xyl(:,j) = xyl(:,j)+xyds(:,j);
end
end
 
 
%-------------------------------------------%
% LIVE DIFFUSION PLOT
%-------------------------------------------%
function [] = MAINPLOT(xyl, lims)
%-------------------------------------------%
xlim = [-lims lims];
ylim = [-lims lims];
zlim = [-5 5];
 
%=================================%
%      MAIN 2D PLOT
%---------------------------------%
figure(1)
subplot(2,1,1),
AMPARPlot = gscatter(xyl(1,:),xyl(2,:));
axis([xlim, ylim]);
set(AMPARPlot,'marker','.','markersize',[6],'color',[1 0 0])
 
 
%=================================%
%          3D PLOT
%---------------------------------%
figure(1);
subplot(2,1,2),
gscatter(xyl(1,:),xyl(2,:)); view(20, 30);
axis normal;
grid off
axis([xlim, ylim, zlim]);
set(gca, 'Box', 'on');
 
end
 
 
%-------------------------------------------%
% MANUAL STEP SIZE FUNCTION
%-------------------------------------------%
function xyds = stepsize(Ndots, Lx)
%-------------------------------------------%
 
  Lx(1:2,1:Ndots) = Lx;
  xyd = randi([0 1],Ndots,2)';
  xyd(xyd == 0) = -1;
  xyds = (Lx.*xyd);
 
end
 
 
%-------------------------------------------%
% MSD SCALED STEPS FUNCTION
%-------------------------------------------%
function [xyl xyds] = MSDAMPARSTEP(Ndots, xyds, xyl, Ls)
for j = 1:Ndots
        xyds(:,j) = xyds(:,j)*Ls;
        xyl(:,j) = xyl(:,j)+xyds(:,j);
end
end
 
 
%-------------------------------------------%
% MSD TRACKS GENERATOR
%-------------------------------------------%
function [tracks] = MSDfun(stepN, Nsteps, tracks, xyds)
    time = (0:Nsteps-1)';
    xymsd = xyds';
    xymsd = cumsum(xymsd,1);
    tracks{stepN} = [time xymsd];
 
end
 
 
%-------------------------------------------%
% MSD TRACKS ANALYSIS
%-------------------------------------------%
function [] = MSDfunction(tracks,Ndots,Nsteps,D,Dn,L,dT,k,Scale,MSDtest)
 
SPACE_UNITS = 'µm';
TIME_UNITS = 's';
N_PARTICLES = Ndots;
N_TIME_STEPS = Nsteps;
N_DIM = 2;
 
oD = D; % raw µm^2/s
D  = D*Scale;      % to-scale µm^2/s
 
oDn = Dn; % raw µm^2/s
Dn = Dn*Scale; % to-scale µm^2/s
 
oL = L; % raw µm
L = L*Scale; % to-scale µm
 
dTbase = dT; % raw time-step
dT = dT*Scale; % to-scale time-step
k = k; % stdv of step distribution
 
ma = msdanalyzer(2, SPACE_UNITS, TIME_UNITS);
ma = ma.addAll(tracks);
disp(ma)
 
figure
ma.plotTracks;
ma.labelPlotTracks;
 
ma = ma.computeMSD;
ma.msd;
 
t = (0 : N_TIME_STEPS)' * dT;
[T1, T2] = meshgrid(t, t);
all_delays = unique( abs(T1 - T2) );
 
figure
ma.plotMSD;
 
 
cla
ma.plotMeanMSD(gca, true)
 
mmsd = ma.getMeanMSD;
t = mmsd(:,1);
x = mmsd(:,2);
dx = mmsd(:,3) ./ sqrt(mmsd(:,4));
errorbar(t, x, dx, 'k')
 
[fo, gof] = ma.fitMeanMSD;
plot(fo)
ma.labelPlotMSD;
legend off
 
 
ma = ma.fitMSD;
 
good_enough_fit = ma.lfit.r2fit > 0.8;
Dmean = mean( ma.lfit.a(good_enough_fit) ) / 2 / ma.n_dim;
Dstd  =  std( ma.lfit.a(good_enough_fit) ) / 2 / ma.n_dim;
 
Dheader1 = ['Raw Unscaled Values'];
Dhead1 = ['    D        Dn        L'];
Ddat1 = [oD oDn oL];
disp(' ')
disp(Dheader1)
disp(Dhead1)
disp(Ddat1)
 
 
yourtesthead = ['YOU ARE TESTING DIFFUSION FOR:'];
if MSDtest(1)
yourtest = ['  D:  original diffusion rate'];
elseif MSDtest(2)
yourtest = ['  Dn:  new diffusion rate'];
elseif MSDtest(3)
yourtest = ['  L: step length'];
else
yourtest = ['  generic diffusion rate'];
end
disp(yourtesthead)
disp(yourtest)


disp(' ')
fprintf('Estimation of raw D coefficient from MSD:\n')
fprintf('D = %.3g ± %.3g (mean ± std, N = %d)\n', ...
    Dmean, Dstd, sum(good_enough_fit));


<big><code>L or Λ (in units: '''µm''')</code></big> is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: <big><code>L = (2d * D)<sup>.5</sup></code></big>).






% Retrieve instantaneous velocities, per track
=== Mean 1D step length component (λ) ===
trackV = ma.getVelocities;
<br>


% Pool track data together
::<math>
TV = vertcat( trackV{:} );
\lambda = {L \over{\sqrt{2}}}
</math>


% Velocities are returned in a N x (nDim+1) array: [ T Vx Vy ...]. So the
% velocity vector in 2D is:
V = TV(:, 2:3);


% Compute diffusion coefficient
<big><code>λ (in units: '''µm''')</code></big> is the average 1-dimensional step length for each component (X,Y,Z) dimension of '''''L'''''. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ {{=}} L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².
varV = var(V);
mVarV = mean(varV); % Take the mean of the two estimates
Dest = mVarV / 2 * dT;






Dheader2 = ['Scaling to model...'];
=== Step length scalar coefficient (ε) ===
Dhead2 = ['    D        Dn        L'];
<br>
Ddat2 = [D Dn L];


disp(' ')
::<math>
disp(Dheader2)
\epsilon = {\sqrt{\delta \over{D}}}
disp(Dhead2)
</math>
disp(Ddat2)
fprintf('Estimation from velocities histogram:\n')
fprintf('Tested D = %.3g %s, compare to scaled Des value of %.3g %s\n', ...
    Dest, [SPACE_UNITS '²/' TIME_UNITS], D, [SPACE_UNITS '²/' TIME_UNITS]);


% printf('D.psd target value was %.3g %s\n', ...
%    Dest, msdDpsd, [SPACE_UNITS '²/' TIME_UNITS]);


end
:: <big><code>δ</code></big> : new desired diffusion rate


<big><code>ε (in units: au)</code></big> is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate '''δ'''.


</syntaxhighlight>


<br /><br />
{{Clear}}
{{Clear}}
==More About Brownian Motion==
Anything below this line might not be up-to-date and/or consistent with the spotless masterpiece above.
----
==MSD Explained==
* <code><big>m = 2d*D</big></code>
* <code><big>k = sqrt(d * D)</big></code>
* <code><big><s>λ = k * √2 / √π</s></big></code>
* <code><big>λ = (2d * D)<sup>0.5</sup></big></code>
* <code><big>λᵃᵇ = √(2λ²)/2</big></code>
* m is mean squared displacement
* d is dimensions (2)
* D is diffusion rate
* k is σ of sampling distribution that yields D (µ=0)
* <s>λ is mean of half normal distribution with σ = k</s>
* λ is the mean length of the XY step
* λᵃᵇ is the step size for both X and Y is λᵃᵇ
In statistical mechanics, the mean squared displacement (MSD or average squared displacement) is the most common measure of the spatial extent of random motion; one can think of MSD as the amount of the system "explored" by a random walker.
{{ExpandBox|More Background|
The [[Normal Distribution|Probability Density Function]] (PDF) for a particle in one dimension is found by solving the one-dimensional Diffusion equation. (This equation states that the position probability density diffuses out over time - this is the method used by Einstein to describe a Brownian particle.
As a particle travels, the molecule is jostled by collisions with other molecules which prevent it from following a straight line. If the path is examined in close detail, it will be seen to be a good approximation to a random walk. Mathematically, a random walk is a series of steps, one after another, where each step is taken in a completely random direction from the one before. This kind of path was famously analyzed by Albert Einstein in a study of Brownian motion and he showed that the mean square of the distance traveled by particle following a random walk is proportional to the time elapsed. In two dimensions this relationship can be written as:
<code><big>r²{{=}}4•D•t</big></code>
In 1D, since both forward and backward steps are equally probable, we come to the surprising conclusion that the probable distance travelled sums up to zero! This is clearly a useless property to calculate. If however, instead of adding the distance of each step we added the square of the distance, we realise that we will always be adding positive quantities to the total. In this case the sum will be some positive number, which grows larger with every step. This obviously gives a better idea about the distance (squared in this case) that a particle moves. If we assume each step happens at regular time intervals, we can easily see how the square distance grows with time, and Einstein showed that it grows linearly with time.
In a molecular system a molecule moves in three dimensions, but the same principle applies. Also, since we have many molecules to consider we can calculate a square displacement for all of them. The average square distance, taken over all molecules, gives us the mean square displacement. This is what makes the mean square displacement (or MSD for short) significant in science: through its relation to diffusion it is a measurable quantity, one which relates directly to the underlying motion of the molecules.
In molecular dynamics the MSD is easily calculated by adding the squares of the distance. The linear (i.e. straight line) dependence of the MSD plot is apparent. If the slope of this plot is taken, the diffusion coefficient D may be readily obtained.
At very short times however, the plot is not linear. This is because the the path a molecule takes will be an approximate straight line until it collides with its neighbour. Only when it starts the collision process will its path start to resemble a random walk. Until it makes that first collision, we may say it moves with approximately constant velocity, which means the distance it travels is proportional to time, and its MSD is therefore proportional to the time squared. Thus at very short time, the MSD resembles a parabola. This is of course a simplification - the collision between molecules is not like the collision between two pebbles, it is not instantaneous in space or time, but is `spread out' a little in both. This means that the behaviour of the MSD at short time is sometimes more complicated than this MSD plot shows.
[http://bradleymonk.com/matlab/msd/MSDTuto.html]
}}
[[File:Brownian.gif|thumb|500px]]
Albert Einstein in a study of Brownian motion and he showed that the mean square of the distance traveled by particle following a random walk is proportional to the time elapsed. This relationship can be written as:
<code><big>r²{{=}}2d•D•t</big></code>
*r² MSD
*d dimensions
*D diffusion coefficient (diffusion rate)
*t time step
The next important equation is the standard deviation of the diffusion rate distribution. It would be more convenient to call this the '''step size distribution''' but the two values are not exactly proportional. That equation is given as:
<big><code>k {{=}} sqrt(d * D * t)</code></big>
*k σ or standard deviation of the diffusion rate distribution
*d dimensions
*D diffusion coefficent
*t time step
Knowing k we can then generate random steps from a [[normal distribution]] with a mean µ {{=}} 0 and sd σ {{=}} k pulling a value from this distribution for both the X and Y dimensions for each step. In Matlab this would look like:
<code><big>xyds = k * randn(2,Ndots);</big></code>
This will generate a random walk with a diffusion rate = D and MSD = 2d•D. We might also be interested in the average step size that is being generated from this distribution to create such a diffusion rate. This can be done by taking the mean absolute value of the generated steps:
<code><big>mStep ≈ mean(mean(abs(xyds),2))</big></code>
The analytical solution to this problem however is the mean µ of the half normal distribution, with a standard deviation σ = k :
<big><code>λ = k * √2 / √π</code></big>
* <code>mS = k*sqrt(2)/sqrt(pi)</code>
However, this value cannot be used as the X and Y step size, if you're expecting to get back to D. What you need is the Pythagorean length of the XY combined step. Einstein solved this too:
λ = (4 * D)^.5;
mSt = sqrt((2*ld^2))/2;
I'm simulating Brownian motion. Each particle (dot) in the simulation take a random size step in a random direction. This is in 2D. Each iteration, a dot gets passed 2 values, a random step size for the X direction and Y direction. Voila! Brownian motion.
Now to characterize this motion. This wasn't an easy task, until this really smart dude came along and provided a few theorems. Albert Einstein showed that the mean square of the distance traveled by particle following a random walk is proportional to the time elapsed. This relationship can be written as (and hopefully the unicode looks like r^2 and not r2.. I won't put integers directly after variables):
* r² = 2d*D*t
* r²  = mean squared displacement MSD (µm²)
* d = number of dimensions
* D = diffusion coefficient (µm²/s)
* t = time elapsed between each measurement (1 s)
Why is the mean squared displacement good to know? Well, creating Brownian motion is easy (like I mentioned above), but simulating Brownian motion based on real-life observations is not so easy. This is because we can't directly observe the step size (the distance traveled between particle collisions), but we can observe the MSD. Ultimately I'd like to estimate the average step size, but having a good estimate of the diffusion rate (D) would suffice for making a simulation based on real-life observations.
Say we're observing a particle through a microscope diffusing along a flat surface (d=2), and each second (t = 1) we write down its displacement (for both X and Y), squaring each value (**see footnote). When we're done, we can calculate the mean of these values and find that MSD=8 µm². We can now find D using the equation above.
* 8 µm² = (2*2) * D µm²/s * 1 s
* 8 µm²/s = 4 * D µm²/s
* 2 µm²/s = D µm²/s
Sweet, we have the diffusion rate. We can now use this value to make a simulation of our empirical observations.... But how? The simulation needs random step sizes, not a diffusion rate. Einstein figured this out too. He determined that the standard deviation (σ=k) of the distribution of random steps is:
k = sqrt(d * D * t)
From our observations:
k = sqrt(2*2*1) = 2
Now all we have to do is pull random values from a normal distribution with µ=0 σ=2 for each X and Y step size. If I do that and run a simulation - lets do 100 steps for 100 particles. boom:
[[File:MSDmatlab1.png]]
Sweet, it worked! D = 2.0
We could stop there. But we're curious... What was the average step size it was creating to get that D? So we make a subroutine to track that information. We find that the average of the absolute values of the step sizes for each iteration are coming out one by one:
1.48, 1.61, 1.56, 1.51, 1.68
The average of all those values is around 1.59
Why? We can take a more analytical approach. Since we are using the absolute value of each step to calculate the mean step size, we are essentially sampling from a half normal distribution. The mean of a half normal distribution is:
µ = σ * sqrt(2) / sqrt(pi)
Our standard deviation was 2.
2*sqrt(2) / sqrt(pi) = 1.5958
Nice, that's pretty much what we got (1.59).
So, to recap, we started with a diffusion rate D=2, which, using Einstein's equation, helped us determine we needed to generate a bunch of steps from a distribution µ=0 σ=2 which resulted in a bunch of steps that were  +/- 1.59 on average. Then as a check, we took the mean square of all 100 steps for each particle and calculated D which turned out to be 2.02 so everything worked as planned.
We could stop there, but will we? Of course not. Let's do one last check, to see if we completely understand what's going on. Say we don't know D or MSD. In fact, we don't have any real-life empirical information to go on. Perhaps all we want to do is generate some random walks, and if we're interested, we can use those walks to calculate the diffusion coefficient (D). Let's make 100 particles take 100 steps with a step size of 1.5958 (randomly negative or positive), aka the average step size from our experiment above.
Before we do this, any guess what D is gunna turn out to be?
[[File:MSDmatlab2.png]]
D = 1.27
Wrong!!
Duh! This value cannot be used as the X and Y step size, if you're expecting to get back to D. What you need is the Pythagorean length of the XY combined step. Einstein solved this too. But first, a recap:
* <code><big>m = 2d*D</big></code>
* <code><big>k = sqrt(d * D)</big></code>
* <code><big><s>λ = k * √2 / √π</s></big></code>
* <code><big>λ = (2d * D)<sup>0.5</sup></big></code>
* m is mean squared displacement
* d is dimensions (2)
* D is diffusion rate
* k is σ of sampling distribution that yields D (µ=0)
* <s>λ is mean of half normal distribution with σ = k</s>
* λ is the mean length of the XY step
See our new equation there: λ = (2d * D). Now, all we have to do to get the X and Y step length values, is to calculate what two values equals λ when using the Pythagorean theorem:
* a²+b²=λ²
since 'a' and 'b' can be the same value, lets just use:
* 2a² = λ²
and solve for 'a'
* <code><big>λᵃᵇ = √(2λ²)/2</big></code>
so the step size for both X and Y is λᵃᵇ
{{ExpandBox|Image Gallery|
[[File:Brownian1.gif]]
[[File:Brownian2.gif]]
[[File:Brownian3.png]]
[[File:Brownian5.gif]]
[[File:Brownian4.ogv]]
}}
==Notes==
Finding the MSD of the simulation is ([http://en.wikipedia.org/wiki/Mean_squared_displacement a trivial task]) using a Matlab toolbox (see video below). The target extrasynaptic MSD was '''0.1 <sup>µm&sup2;</sup>&frasl;<sub>s</sub>''' (from Choquet {{Fig|[[File:ChoquetDiffusionRate1.png]]}}). Scaling the model PSD size and distance between synapses to real-world values based on the target MSD is not a trivial task, and may actually be related only indirectly. Just because a particle moves quickly, does not mean it moves from A to B quickly - it depends on how much stuff there is to collide with. Linear estimates have shown that glycine receptor movements along dendrites at a speed of 1–2 µm/min (.016-.008 µm/s) {{Fig|[[File:Choquet MSD2.png]]}}
First, the randomly generated step-size was scaled to produce an MSD of '''0.1 <sup>units&sup2;</sup>&frasl;<sub>step</sub>''' using the Matlab Brownian motion toolbox. Second, the dimensions of the model need to be scaled. It was found that an XY random step-size of µ{{=}}0.4 (&sigma;{{=}}.2) units produced an MSE &asymp; '''0.1 <sup>units&sup2;</sup>&frasl;<sub>step</sub>'''. Next, the arbitrary 0.4 units need to be given meaning...
Given movements along dendrites at a speed of 1–2 µm/min (.016-.008 µm/s) observation, we can use a derivation of the MSD equation to help scale the model. The root-mean-square distance after N unit steps, with a step length of (L) is:
* d {{=}} L*sqrt(N)
In order to travel a distance d, N steps are required by this equation
* N {{=}} (d/L)²
* N {{=}} (1000 nm /400 nm)² {{=}} 6.25
* N {{=}} (2000 nm /400 nm)² {{=}} 25
* N {{=}} (4000 nm /400 nm)² {{=}} 100
* Lets say a particle is traveling linearly at a rate of 1 µm / 100 s
* And takes between 6-25 steps to cover 1-2 µm. 
[[File:ScaleModel.png]]
here are several good resources for explaining and calculating MSD:
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html wolfram alpha]
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus]
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories Matlab toolbox]


{{ExpandBox|VIDEO|
=Brownian Motion MATLAB Code=
<HTML><embed src="http://bradleymonk.com/media/MSD.mp4" height="500" width="640" autoplay="false"></HTML>
<br>
}}<!-- END VIDEO -->


<big>Visit this page to view or download a full [[Brownian Motion MATLAB Code|Brownian Motion simulation and analysis in MATLAB]].</big>


{{ExpandBox|MEAN SQUARED DISPLACEMENT|


;Brownian Motion Mean Squared Displacement
* The goal of this calculation is to relate the simulated particle diffusion to real world values, namely velocity.
*Particle velocity will be a function of MSD {{Button|''x'' units&thinsp;&sup2;&frasl;s}} which scales on space (units) and time (s) parameters.
* Space and time in the model are defined arbitrarily as '''Step_Size''' and '''Step''' where each Step a particle moves a distance randomly chosen from a normal distribution (µ{{=}}1,&sigma;{{=}}.2)
*a step size of {{Button|1 unit/step}} will produce a brownian motion MSD of {{Button|~0.52 ±0.2 units&thinsp;&sup2;/s}}
*empirical observations show that reasonable values for MSD are:
** PSD 0.01 µm&thinsp;&sup2;/s
** synaptic 0.05 µm&thinsp;&sup2;/s
** extrasynaptic 0.1 µm&thinsp;&sup2;/s


*given an MSD of {{Button|0.52 ±0.2 units&thinsp;&sup2;/s}} at the current parameters: 1 step {{=}} 1 unit (at µ{{=}}1,&sigma;{{=}}.2), the model will need to be scaled such that particles move at an extrasynaptic rate of 0.1 µm&thinsp;&sup2;/s.
*spines are on average 1 to 10 µm apart, if the model is comparing two spines 1 µm apart, they should be separated by 5 units of model space. This is because the current particle diffusion rate of the model is .5 µm&thinsp;&sup2;/s and the empirical MSD is .1 µm&thinsp;&sup2;/s


=Simulating Particle Trajectories=


----
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB. You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks. Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads. These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.
to make '''0.1 <sup>units&sup2;</sup>&frasl;<sub>step</sub>''' &asymp; '''0.1 <sup>µm&sup2;</sup>&frasl;<sub>s</sub>'''. It was found that an XY random step-size of µ{{=}}0.4 (&sigma;{{=}}.2) units produced an MSE &asymp; '''0.1 <sup>units&sup2;</sup>&frasl;<sub>step</sub>'''. Then, the arbitrary 0.5 units were given meaning (converted to 0.5 µm) by scaling the model according to real-world values (see below) by making 1 unit {{=}} 1 µm; as a convention, a ''subunit'' will be 1/10th of a unit, thus 1 subunit {{=}} 0.1 µm). The PSD areas were set to 3-subunits (.3 µm) square, 20 subunits (2 µm) apart, within a rectangular field 20 subunits (2 µm) wide and 60 subunits (6 µm) long. Given these scaled dimensions where 10 subunits &asymp; 1 µm, a particle with an XY step-size of 0.5 units moving in a straight line, could theoretically go from PSD1 to PSD2 in 4 steps (obviously given the simulated particles are moving with Brownian motion, this lower-bound would be extremely rare).  


}}<!-- END BOX -->
==Lab==
'''All pages in this lab'''
I. [[Brownian Motion in Cells]]
II. Simulating Brownian Motion
III. [[Experimental Procedures]]
IV. [[BMC Software]]
----
=Introduction=
This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using Matlab.  You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks.  Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads.  These simulations will generate the predictions you can test in your experiment. 
In each section, Matlab code shown in the box to the left is used to generate the plot or analysis shown on the right.  '''To use the code, copy it from the box on the left, launch the Matlab application,  and paste the code into the Matlab Command Window.''' If you are new to Matlab, check out the [[Intro to Matlab]] page to help you get started.
=Simulating Single Particle Trajectories=
This exercise shows how to simulate the motion of a single particle in one and two dimensions .


==One Dimensional Brownian Motion==
==One Dimensional Brownian Motion==
Line 666: Line 147:
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.
The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.


<pre>
 
<syntaxhighlight lang="matlab" line start="1">
% One Dimensional Brownian Motion
 
N = 1000;
N = 1000;
displacement = randn(1,N);
displacement = randn(1,N);
plot(displacement);
plot(displacement);
</pre>
</syntaxhighlight>
 
 
 


{{Clear}}
==Distribution of Displacements==     
==Distribution of Displacements==     


Line 682: Line 170:
</pre>
</pre>


{{Clear}}
==Convert displacements to position==
==Convert displacements to position==


Line 688: Line 178:
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]
[[Image:SingleParticleSimulations_03.png|right|thumb|300px]]


<pre>
<syntaxhighlight lang="matlab" line start="1">
x = cumsum(displacement);
x = cumsum(displacement);
plot(x);
plot(x);
Line 694: Line 184:
xlabel('time step');
xlabel('time step');
title('Position of 1D Particle versus Time');
title('Position of 1D Particle versus Time');
</pre>
</syntaxhighlight>
 
 


{{Clear}}
==Two dimensional particle simulation==
==Two dimensional particle simulation==


[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]
[[Image:SingleParticleSimulations_04.png|right|thumb|300px]]


<p>Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a [http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/struct.html&http://www.mathworks.com/cgi-bin/texis/webinator/search_spt?db=MSS&prox=page&rorder=750&rprox=750&rdfreq=500&rwfreq=500&rlead=250&sufs=0&order=r&is_summary_on=1&pr=SPT&cq=1&collection=1&ResultCount=10&query=struct Matlab structure] called <tt>particle</tt>. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.</p>
Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called <tt>particle</tt>. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.
 
 
<syntaxhighlight lang="matlab" line start="1">
% Two Dimensional Brownian Motion


<pre>
particle = struct();
particle = struct();
particle.x = cumsum( randn(N, 1) );
particle.x = cumsum( randn(N, 1) );
particle.y = cumsum( randn(N, 1) );
particle.y = cumsum( randn(N, 1) );
plot(particle.x, particle.y);
plot(particle.x, particle.y);
ylabel('Y Position');
ylabel('Y Position');
xlabel('X Position');
xlabel('X Position');
title('position versus time in 2D');
title('position versus time in 2D');
</pre>
</syntaxhighlight>
 
 
 
 


{{Clear}}
==Compute the Displacement Squared==
==Compute the Displacement Squared==


[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]
[[Image:SingleParticleSimulations_05.png|right|thumb|300px]]


<p>The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).</p>
The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).
 
The dot caret (.^) operator raises each element of a matrix to a power.


<p>The dot caret (.^) operator raises each element of a matrix to a power.</p>
<syntaxhighlight lang="matlab" line start="1">
% Displacement Squared


<pre>
dsquared = particle.x .^ 2 + particle.y .^ 2;
dsquared = particle.x .^ 2 + particle.y .^ 2;
plot(dsquared);
plot(dsquared);
</pre>
</syntaxhighlight>
 
 


==Theoretical Value of <i>D</i>==


<p>The theoretical value of the diffusion coefficient, <i>D</i>, is given by [[Image:SingleParticleSimulations_eq6021.png]] where <i>T</i> = temperature (Kelvin), <i>k</i><sub>B</sub> = Boltzmann's constant, eta = viscosity, and <i>d</i> = radius.</p>
{{Clear}}
==Theoretical Value of D==


<p>Note that the units of <i>D</i> are length squared divided by time. See the lab writeup for more information. Let's compute <i>D</i> for a 1 micron particle in water at 293 degrees Kelvin.</p>
The theoretical value of the diffusion coefficient, <i>D</i>, is given by <code>D    = k<sub>B</sub> * T / (3πrη)</code> where <i>T</i> = temperature (Kelvin), <i>k</i><sub>B</sub> = Boltzmann's constant, eta = viscosity, and <i>r</i> = diffusion_radius. Note that the units of <i>D</i> are length squared divided by time. See the lab writeup for more information. Let's compute <i>D</i> for a 1 micron particle in water at 293 degrees Kelvin.


<pre>
<syntaxhighlight lang="matlab" line start="1">
d   = 1.0e-6;              % radius in meters
r   = 1.0e-6;              % radius in meters
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K
kB  = 1.38e-23;            % Boltzmann constant
kB  = 1.38e-23;            % Boltzmann constant
T    = 293;                % Temperature in degrees Kelvin
T    = 293;                % Temperature in degrees Kelvin


D   = kB * T / (3 * pi * eta * d)
D = kB * T / (3 * pi * r * eta);
</syntaxhighlight>
 
Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then


ans =
<pre>
      4.2902e-013
v = 2D / dT
</pre>
</pre>


Line 749: Line 258:
<p>So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.</p>
<p>So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.</p>


<p>According to theory, the mean squared displacement of the particle is proportional to the time interval, [[Image:SingleParticleSimulations_eq171153.png]], where <i>r</i>(<i>t</i>) = position, <i>d</i> = number of dimensions, <i>D</i> = diffusion coefficient, and tau = time interval.</p>  
<p>According to theory, the mean squared displacement of the particle is proportional to the time interval, where <i>r</i>(<i>t</i>) = position, <i>d</i> = number of dimensions, <i>D</i> = diffusion coefficient, and tau = time interval.</p>  


<p>To generate the correct distribution, the output from <tt>randn</tt> (which has a standard normal distribution) must be scaled by the factor <tt>k</tt>.</p>
<p>To generate the correct distribution, the output from <tt>randn</tt> (which has a standard normal distribution) must be scaled by the factor <tt>k</tt>.</p>


<pre>
 
 
<syntaxhighlight lang="matlab" line start="1">
N = 1000;
 
r    = 1.0e-6;              % radius in meters
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K
kB  = 1.38e-23;            % Boltzmann constant
T    = 293;                % Temperature in degrees Kelvin
 
D = kB * T / (3 * pi * r * eta);
 
 
dimensions = 2;        % two dimensional simulation
dimensions = 2;        % two dimensional simulation
tau = .1;              % time interval in seconds
tau = .1;              % time interval in seconds
Line 766: Line 287:


dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);
dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);
  squaredDisplacement = ( x .^ 2) + ( y .^ 2);
squaredDisplacement = ( x .^ 2) + ( y .^ 2);


plot(x,y);
plot(x,y);
title('Particle Track of a Single Simulated Particle');
title('Particle Track of a Single Simulated Particle');
</pre>
</syntaxhighlight>


==Displacement Squared Plot==
==Displacement Squared Plot==
Line 778: Line 299:
<p>Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.</p>
<p>Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.</p>


<pre>
<syntaxhighlight lang="matlab" line start="1">
% continuing from above...
 
clf;
clf;
plot(time, (0:1:(N-1)) * 2*k^2 , 'k', 'LineWidth', 3);      % plot theoretical line
hold on;
hold on;
plot(time, (0:1:(N-1)) * 2*k^2 , 'k', 'LineWidth', 3);      % plot theoretical line
plot(time, squaredDisplacement);
plot(time, squaredDisplacement);
hold off;
hold off;
xlabel('Time');
xlabel('Time');
ylabel('Displacement Squared');
ylabel('Displacement Squared');
title('Displacement Squared versus Time for 1 Particle in 2 Dimensions');
title('Displacement Squared versus Time for 1 Particle in 2 Dimensions');
</pre>
</syntaxhighlight>
 
 
 
 
 
 


==Estimating <i>D</i> from the Simulated Data==
==Estimating <i>D</i> from the Simulated Data==


<p>Solving for <i>D</i> gives [[Image:SingleParticleSimulations_eq250744.png]]. The best estimate of the value of <i>D</i> from the simulated data is:</p>
The best estimate of the value of <i>D</i> from the simulated data is:</p>


<pre>
<pre>
Line 798: Line 327:


ans =  
ans =  
     4.2192e-013
     4.22 E-13
</pre>
</pre>


Line 815: Line 344:
               7.1019e-015
               7.1019e-015
</pre>
</pre>


==Systematic Error -- Bulk Flow in the Solvent==
==Systematic Error -- Bulk Flow in the Solvent==
Line 822: Line 357:
<p>Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.</p>
<p>Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.</p>


<pre>
<syntaxhighlight lang="matlab" line start="1">
dx = dx + 0.2 * k;
dx = dx + 0.2 * k;
dy = dy + 0.05 * k;
dy = dy + 0.05 * k;
Line 845: Line 380:
actualError  =  
actualError  =  
               -2.3859e-016
               -2.3859e-016
</pre>
</syntaxhighlight>
 
 
 
 
 
 
 


==Displacement Squared in the Presence of Bulk Flow==
==Displacement Squared in the Presence of Bulk Flow==
Line 865: Line 407:
</pre>
</pre>


=Simulating Multiple Particles=
 
 
 
 
 
 
==Simulating Multiple Particles==


<p>When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let's simulate this.</p>
<p>When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let's simulate this.</p>


==Using a For Loop to Generate Multiple Data Sets==


<p>A <tt>for</tt> loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called <tt>particle</tt>. For example, <tt>particle{3}</tt> refers to a structure containing the results of simulation number 3. <tt>particle{3}.D</tt> is the estimated value of <i>D</i> for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look.</p>


<pre>
 
===Using a For Loop to Generate Multiple Data Sets===
[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]
 
A <tt>for</tt> loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called <tt>particle</tt>. For example, <tt>particle{3}</tt> refers to a structure containing the results of simulation number 3. <tt>particle{3}.D</tt> is the estimated value of <i>D</i> for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.
 
<syntaxhighlight lang="matlab" line start="1">
particleCount = 10;
particleCount = 10;
N = 50;
N = 50;
Line 891: Line 443:
     particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );
     particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );
end
end
</pre>


==SimulateParticle Function==


<p>That's a lot of typing. Fortunately, all of the commands to generate multiple particle tracks have been combined into in a single function called <tt>SimulateParticles</tt>. If you are interested in how the function is implemented, type <tt>edit SimulateParticles.m</tt> to have a look at the m-file for this function.</p>
clf;
 
<pre>
help SimulateParticles
 
particle = SimulateParticles(N, particleCount, tau, k);
usage: out =
SimulateParticles( N, particleCount, tau, k )
 
N is the number of samples
particleCount is the number of particles
tau is the sample period
k is the standard deviation of dx and dy
returns a cellular array of length particleCount
</pre>
 
==Look at the Results==
 
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]
 
<p>The following code plots all of the generated particle tracks on a single set of axes, each in a random color.</p>
 
<pre>clf;
hold on;
hold on;
for i = 1:particleCount
for i = 1:particleCount
Line 929: Line 455:
title('Combined Particle Tracks');
title('Combined Particle Tracks');
hold off;
hold off;
</pre>
</syntaxhighlight>
 
 
 
 


==Displacement Squared==
===Displacement Squared===


[[Image:MultipleParticleSimulation_02.png|right|thumb|300px]]
[[Image:MultipleParticleSimulation_01.png|right|thumb|300px]]


<p>The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.</p>
<p>The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.</p>


<pre>
<syntaxhighlight lang="matlab" line start="1">
% compute the ensemble average
% compute the ensemble average
rsquaredSum = zeros(1,N);
rsquaredSum = zeros(1,N);
Line 963: Line 493:
title('Displacement Squared vs Time');
title('Displacement Squared vs Time');
hold off;
hold off;
</pre>
</syntaxhighlight>
 
 
 
 
 


==Estimated Value of <i>D</i>==
===Estimated Value of <i>D</i>===


[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]
[[Image:MultipleParticleSimulation_03.png|right|thumb|300px]]
Line 971: Line 506:
<p>This plot shows the computed value of <i>D</i> for each simulation with error bars. A thick blue line indicates the best overall estimate of <i>D</i> (the average of the <i>D</i> value from each simulation) along with error bars in green <b>*</b> WHICH HAPPEN TO BE WRONG -- NEED TO FIX <b>*</b>.</p>
<p>This plot shows the computed value of <i>D</i> for each simulation with error bars. A thick blue line indicates the best overall estimate of <i>D</i> (the average of the <i>D</i> value from each simulation) along with error bars in green <b>*</b> WHICH HAPPEN TO BE WRONG -- NEED TO FIX <b>*</b>.</p>


<pre>
 
<syntaxhighlight lang="matlab" line start="1">
clear D e dx;
clear D e dx;


Line 1,007: Line 543:
uncertainty =  
uncertainty =  
             2.3294e-014
             2.3294e-014
</pre>
</syntaxhighlight>
 
 
 
 
 


=Fancy Statistics and Plots=
=Motion Statistics and Plots=


<p>This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:</p>
<p>This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:</p>
Line 1,036: Line 577:
title('Histogram of Values for 1000000 Samples of randn');
title('Histogram of Values for 1000000 Samples of randn');
</pre>
</pre>


==Sampling Uncertainty==
==Sampling Uncertainty==
Line 1,047: Line 592:
<p>Matlab note: check out the nested <tt>for</tt> loops used to create the plot.</p>
<p>Matlab note: check out the nested <tt>for</tt> loops used to create the plot.</p>


<pre>clf;
 
hold on;
<syntaxhighlight lang="matlab" line start="1">
<span class="keyword">for i= 1:100
clf; hold on;
     <span class="keyword">for j = 1:50
for i= 1:100
     for j = 1:50
         y = randn(1,i);
         y = randn(1,i);
         m = mean(y);
         m = mean(y);
         plot(i,m,'x');
         plot(i,m,'x');
     <span class="keyword">end
     end
end


<span class="keyword">end
plot(1:100, 1./sqrt(1:100), 'k', 'LineWidth', 2);  % plot upper error bar in dark black
plot(1:100, 1./sqrt(1:100), 'k', 'LineWidth', 2);  % plot upper error bar in dark black
plot(1:100, -1./sqrt(1:100), 'k', 'LineWidth', 2);  % plot lower error bar in dark black
plot(1:100, -1./sqrt(1:100), 'k', 'LineWidth', 2);  % plot lower error bar in dark black
Line 1,063: Line 609:
ylabel('Population Mean');
ylabel('Population Mean');
title('Population Mean of Normally Distributed Random Variable versus Population Size');
title('Population Mean of Normally Distributed Random Variable versus Population Size');
</pre>
</syntaxhighlight>
 
 
 
 
 


==What Happens when you Square a Random Variable?==
==What Happens when you Square a Random Variable?==


[[Image:FancyStatistics_04.png|right|thumb|300px]]
[[Image:FancyStatistics_04.png|right|thumb|300px]]


<p>We did not estimate <i>D</i> in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:</p>
<p>We did not estimate <i>D</i> in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a [http://en.wikipedia.org/wiki/Chi_squared chi-squared distribution] (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:</p>


<pre>
<pre>
Line 1,090: Line 643:
</pre>
</pre>


==100 Years of BIO Lab Data in 1 Second==


[[Image:FancyStatistics_05.png|right|thumb|300px]]


<p>So does the uncertainty of the squared and summed behave as expected? The following plot is a simulation of 5000 data sets - 50 at each value of N just as above. (This is what a plot of all the data from students doing the BIO lab for the next hundred years might look like. Some lazy groups decided to use very low values of N.)</p>


<pre>
clf;
hold on;


for i= 1:100
    for j = 1:50
        dx = randn(1,i);
        dy = randn(1,i);
        m = mean( dx .^ 2 + dy .^ 2 );
        plot(i,m,'x');
    end
end


plot(1:100, 2 + 2./sqrt(1:100), 'k', 'LineWidth', 2);  % plot upper error bar in dark black
plot(1:100, 2 - 2./sqrt(1:100), 'k', 'LineWidth', 2);  % plot lower error bar in dark black
hold off;
xlabel('Population Size (N)');
ylabel('Population Mean');
title('Population Mean of Chi Squared (2 DOF) Distributed Random Variable versus Population Size');
</pre>
==Planning Your Time in the Lab==
<p>Sampling error sets a lower bound on the uncertainty in your estimate of the diffusion coefficient from experimental data. To plan your time in the lab, it will be important to understand how much data you should take for each experimental condition. Because the particles go out of focus and drift away from of the observation area, it can be difficult to make movies longer than about 5 seconds. At a frame rate of 10 frames per second, how many movies would you have to make to achieve a final uncertainty of 10%? 1%? .1%? What level of uncertainty will you try for in the lab?</p>


==Auto Correlation - A Closer Look at the Data==
==Auto Correlation - A Closer Look at the Data==
Line 1,126: Line 653:
[[Image:FancyStatistics_06.png|right|thumb|300px]]
[[Image:FancyStatistics_06.png|right|thumb|300px]]


<p>The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn't mean it's true.</p>
The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn't mean it's true.


<p>There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called <tt>xcorr</tt>.</p>
There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called <tt>xcorr</tt>.


<p>By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with <tt>xcorr</tt>. If x is a vector, <tt>xcorr(x)</tt> returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option 'coeff' causes the xcorr function to normalize this value to exactly 1 --perfect correlation.</p>
By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with <tt>xcorr</tt>. If x is a vector, <tt>xcorr(x)</tt> returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option 'coeff' causes the xcorr function to normalize this value to exactly 1 --perfect correlation.


<p>Also notice that the autocorrelation sequence is symmetric about the origin.</p>
Also notice that the autocorrelation sequence is symmetric about the origin.


<pre>
<pre>
Line 1,144: Line 671:
title('Particle 1 x-axis Displacement Autocorrelation');
title('Particle 1 x-axis Displacement Autocorrelation');
</pre>
</pre>


==Cross Correlation==
==Cross Correlation==
Line 1,161: Line 691:
</pre>
</pre>


==Matlab Tricks==
 
 
 
 
==MATLAB Tricks==


[[Image:FancyStatistics_08.png|right|thumb|300px]]
[[Image:FancyStatistics_08.png|right|thumb|300px]]


<p>Matlab can generate (and plot) an amazing amount of data. For example, the <tt>xcorr</tt> function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, <tt>xcorr(A)</tt> returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.</p>
<p>Matlab can generate (and plot) an amazing amount of data. For example, the <tt>xcorr</tt> function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, <tt>xcorr(A)</tt> returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of  the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.</p>


<pre>
<pre>
Line 1,190: Line 725:
title('All Possible Auto and Cross Correlations in the x Dimension');
title('All Possible Auto and Cross Correlations in the x Dimension');
</pre>
</pre>


==A Correlated Trajectory==
==A Correlated Trajectory==
Line 1,216: Line 755:




==Articles==
{{Article|Michalet|2010|Phys Rev E Stat Nonlin Soft Matter Phys - [http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ PDF]|PMC3055791|Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium}}
{{ExpandBox|Expand to view experiment summary|
We examine the capability of mean square displacement analysis to extract reliable values of the diffusion coefficient D of single particle undergoing Brownian motion in an isotropic medium in the presence of localization uncertainty. The theoretical results, supported by simulations, show that a simple unweighted least square fit of the MSD curve can provide the best estimate of D provided an optimal number of MSD points is used for the fit. We discuss the practical implications of these results for data analysis in single-particle tracking experiments.
}}<!-- END ARTICLE -->
{{Article|Czondor Choquet|2012|PNAS - [http://www.ncbi.nlm.nih.gov/pubmed/22331885 PDF]|22331885|Unified quantitative model of AMPA receptor trafficking at synapses}}
{{ExpandBox|Expand to view experiment summary|
{{Fig|[[File:UnifiedFig1.png|500px]]|(A) Schematic diagramof themodel. Kinetic parameters include Dout (extrasynaptic diffusion), Din (synaptic diffusion), DPSD (diffusion coefficient of the PSD), kon (AMPAR/ scaffold binding rate), koff (AMPAR/scaffold dissociation rate), and kendo (endocytosis rate). (B) Simulated trajectory (50 s). The synapse is in green, the PSD in red, and dendrite borders in gray. Geometric parameters are: a (synapse spacing), b (synapse width), c (PSD width), w (dendrite width). (C) High-magnification 30-s trajectory of an AMPAR-bound Qdot on the surface of a DIV 9 neuron. and simulated (plain curves) AMPAR median diffusion coefficients obtained at different neuronal ages (DIV 4–15) or varying synaptic spacing (0.75–30 μm), respectively, were plotted against synapse density. The simulated curves were computed for different kon values, keeping koff: 0.1 s−1. (F) Interquartile distributions of AMPAR diffusion coefficients from experiments (black) and simulations (green). Neuroligin-1 expression, which doubled the number of Homer1c-positive puncta, was mimicked by a decrease in synapse spacing (a: 1 μm). Overexpressing PSD-95 was modeled by enhancing kon (2.5 s−1) to mimic an increase in PSD binding sites, plus an increase in PSD size (c: 0.4 μm).}}
;Dendrite
* length: 10 - 60 µm
* width: 2 µm
* D.out: 0.05 - 0.3 µm²/s
;PSD:
* length: 0.3 µm
* width: 0.3 µm
* distance: 1 - 30 µm apart


;PSA (perisynaptic area PSD pad):
* length: 0.3 µm
* width: 0.3 µm
* D.in: 0.03 µm²/s
Total synaptic area: .6 x .6 µm
In some cases, we varied the size of the synapse and the PSD, keeping a constant ratio of 2 between them (b: 0.1–1.6 μm; c: 0.05–0.8 μm).


AMPARs bound to the PSD were allowed to diffuse at a very low diffusion coefficient DPSD: 0.0002 μm2/s, reflecting the intrinsic movement or morphing of the PSD. We neglected the turnover of individual scaffold molecules, typically much longer than the AMPAR diffusion time scale
=Additional Resources=
<br>
*[http://mathworld.wolfram.com/RandomWalk2-Dimensional.html Wolfram Alpha - Random Walk]
*[http://www.compsoc.man.ac.uk/~lucky/Democritus/Theory/msd.html Democritus - MSD Theory]
*[http://www.mathworks.com/matlabcentral/fileexchange/40692-mean-square-displacement-analysis-of-particles-trajectories MSD MATLAB Toolbox]
*[http://www.ncbi.nlm.nih.gov/pmc/articles/PMC3055791/ Mean Square Displacement Analysis of Single-Particle Trajectories with Localization Error: Brownian Motion in Isotropic Medium]
*[http://www.nature.com/ncb/journal/v17/n4/full/ncb3131.html Active Diffusion Positions in the Nucleus]
*[[Stochastic Diffusion]]


}}<!-- END ARTICLE -->


[[Category:ReDiClus]][[Category:Neurobiology]]
*[[Category:Diffusion]]
*[[Category:Neurobiology]]
*[[Category:Math]]
*[[Category:Statistics]]
*[[Category:Synaptic Plasticity]]
*[[Category:Programming]]

Latest revision as of 02:12, 23 June 2024

Brownian motion describes the stochastic diffusion of particles as they travel through n-dimensional spaces filled with other particles and physical barriers. Here the term particle is a generic term that can be generalized to describe the motion of molecule (e.g. H2O) or proteins (e.g. NMDA receptors); note however that stochastic diffusion can also apply to things like the price index of a stock (see random walk) or the propagation heat energy across a surface. Brownian motion is among the simplest continuous-time stochastic processes, and a limit of various probabilistic processes (see random walk). As such, Brownian motion is highly generalizable to many applications, and is directly related to the universality of the normal distribution. In some sense, stochastic diffusion is a pure actuation of the basic statistical properties of probability distributions - it is distribution sampling translated into movements.


Properties of Brownian Motion

Physical experiments characterize the following properties of Brownian motion...

  • continuous increments
  • increments of a particle over disjoint time intervals are independent events
  • each increment is assumed to result from collisions with many molecules
  • each increment is assumed to have a normal probability distribution
  • the mean increment is zero as there is no preferred direction
  • the position of a particle spreads out with time
  • the variance of the increment is proportional to the length of time that Brownian motion has been observed


The probability density of a normally distributed random variable with mean μ and standard deviation σ is given by:

Mathematically, the random process called Brownian motion is denoted here as B(t) and defined for times t ≥ 0; the probability density of Brownian particles at the end of time period [0, t] is obtained by substituting μ = 0 and σ = √t, giving:

where x denotes the value of random variable B(t). The probability distribution of the increment B(t + u) − B(t) is:


Common Diffusion Terms & Parameters


I've found it most helpful to learn about the terms and parameters used to characterize stochastic diffusion as they apply to diffusion phenomena along a flat 2D surface. These parameters include:


D  : diffusion rate coefficient

L : step length
d : dimensions
t : time


D (in units: µm²/s) is the mean diffusion rate per unit time (velocity), often in µm²/s for biological motion on a molecular level (n.b. µm²/s are the units for particle diffusion on a surface/membrane; the surface can actually be curved or ruffled such that the surface fills a 3D space, however the diffusion equations for those instances are slightly more complex. Units for 3D space are naturally: µm³/s). This refers to how fast, on average, the particle moves along its surface trajectories. This value is often of ultimate interest, that is, the goal of single-particle tracking studies is often to define the average diffusion rate of a molecule and report it as a Diffusion Coefficient; once D is known, this Diffusion Coefficient can then be easily implemented in computer simulations of Brownian motion. However under most circumstances, the diffusion rate cannot be observed directly in empirical experiments - this would require the ability to visualize all the microscopic particles and collisions that dictate the particle's movement on a nanosecond timescale. In the animation on the right, the diffusion rate can actually be quantified directly; but what is often seen when observing particle diffusion through a microscope would more closely resemble this:

Instead, D is often calculated from the mean squared diffusion (MSD) path of the particle, defined below.


Mean Squared Displacement (MSD)


d : dimensions
D : diffusion coefficient


MSD (in units: µm²) is the mean square displacement of a particle over some time period.

Standard deviation (k) of the normal distribution of step lengths


d : dimensions
D : diffusion coefficient


k (in units: µm) is the standard deviation (σ) of the normal distribution of step lengths that, when randomly sampled, will give rise to a diffusion rate D. This value is useful for simulating Brownian motion for a particular diffusion rate.


Mean Step Length (L)


d : dimensions
D : diffusion coefficient


L or Λ (in units: µm) is the average step length per interval of observation. In diffusion simulations, this is the step size per iteration (equivalent form: L = (2d * D).5).


Mean 1D step length component (λ)



λ (in units: µm) is the average 1-dimensional step length for each component (X,Y,Z) dimension of L. For example, simulating 2D particle diffusion will require the generation of individual step lengths for both the X and Y dimension. The total step distance from the origin will be the length of the hypotenuse created by the individual X and Y component step lengths. In fact, the equation: λ = L / √2 is derived from the Pythagorean theorem for right triangles, such that 2λ² = λ² where 2λ² represents a² + b² and λ² represents c².


Step length scalar coefficient (ε)



δ : new desired diffusion rate

ε (in units: au) is a coefficient value that, when multiplied by each λ component step length, will scale those lengths to achieve a new diffusion rate δ.


Brownian Motion MATLAB Code


Visit this page to view or download a full Brownian Motion simulation and analysis in MATLAB.



Simulating Particle Trajectories

This exercise shows how to simulate the motion of single and multiple particles in one and two dimensions using MATLAB. You will discover some useful ways to visualize and analyze particle motion data, as well as learn the Matlab code to accomplish these tasks. Once you understand the simulations, you can tweak the code to simulate the actual experimental conditions you choose for your study of Brownian motion of synthetic beads. These simulations will generate the predictions you can test in your experiment. In each section, MATLAB code shown in the box to the left is used to generate the plot or analysis shown on the right.


One Dimensional Brownian Motion

Brownian motion in one dimension is composed of a sequence of normally distributed random displacements. The randn function returns a matrix of a normally distributed random numbers with standard deviation 1. The two arguments specify the size of the matrix, which will be 1xN in the example below.

The first step in simulating this process is to generate a vector of random displacements. The commands to do this are shown below. N is the number of samples to generate.


% One Dimensional Brownian Motion

N = 1000;
displacement = randn(1,N);
plot(displacement);



Distribution of Displacements

Have a look at the distribution of the randomly generated displacements. The hist command plots a histogram of the values. The second argument - 25 - specifies that Matlab should divide the values into 25 bins.

hist(displacement, 25);


Convert displacements to position

Now we have some appropriate random displacements. Their sum represents a particle trajectory in 1 dimension. The Matlab function cumsum returns the cumulative sum of a vector. The following commands take the cumulative sum of displacement and save the result in a vector called x.

x = cumsum(displacement);
plot(x);
ylabel('position');
xlabel('time step');
title('Position of 1D Particle versus Time');


Two dimensional particle simulation

Extending this to two dimensions is simple. Since all directions are (assumed to be) equivalent, all we need to do is generate two vectors of random displacements. The vector of displacements saved in a Matlab struct called particle. x and y position vectors are stored in members of the structure. This data could also have been saved as a 2xN matrix. Using a structure has the important advantage that a meaningful name can be assigned to each member. This good practice makes your code much more readable.


% Two Dimensional Brownian Motion

particle = struct();
particle.x = cumsum( randn(N, 1) );
particle.y = cumsum( randn(N, 1) );

plot(particle.x, particle.y);
ylabel('Y Position');
xlabel('X Position');
title('position versus time in 2D');



Compute the Displacement Squared

The displacement squared is equal to the x coordinate squared plus the y coordinate squared. Since the simulated particle always start at (0,0), it is unnecessary to subtract off the initial position (as will be necessary with the data you gather in the lab).

The dot caret (.^) operator raises each element of a matrix to a power.

% Displacement Squared

dsquared = particle.x .^ 2 + particle.y .^ 2;
plot(dsquared);



Theoretical Value of D

The theoretical value of the diffusion coefficient, D, is given by D = kB * T / (3πrη) where T = temperature (Kelvin), kB = Boltzmann's constant, eta = viscosity, and r = diffusion_radius. Note that the units of D are length squared divided by time. See the lab writeup for more information. Let's compute D for a 1 micron particle in water at 293 degrees Kelvin.

r    = 1.0e-6;              % radius in meters
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K
kB   = 1.38e-23;            % Boltzmann constant
T    = 293;                 % Temperature in degrees Kelvin

D = kB * T / (3 * pi * r * eta);

Qian et al. derived a way to estimate the diffusion coefficient from the distribution of instantaneous velocities. If v is the instantaneous velocity vector, then

v = 2D / dT

A more realistic particle -- Getting the Units Right

So far, we have been looking at simulated particles with a mean squared displacement of 1 unit per time interval. To accurately model a real particle, it is necessary to adjust the distribution of random displacements to match the experimental conditions.

According to theory, the mean squared displacement of the particle is proportional to the time interval, where r(t) = position, d = number of dimensions, D = diffusion coefficient, and tau = time interval.

To generate the correct distribution, the output from randn (which has a standard normal distribution) must be scaled by the factor k.


N = 1000;

r    = 1.0e-6;              % radius in meters
eta  = 1.0e-3;              % viscosity of water in SI units (Pascal-seconds) at 293 K
kB   = 1.38e-23;            % Boltzmann constant
T    = 293;                 % Temperature in degrees Kelvin

D = kB * T / (3 * pi * r * eta);


dimensions = 2;         % two dimensional simulation
tau = .1;               % time interval in seconds
time = tau * 1:N;       % create a time vector for plotting

k = sqrt(D * dimensions * tau);
dx = k * randn(N,1);
dy = k * randn(N,1);

x = cumsum(dx);
y = cumsum(dy);

dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);
squaredDisplacement  = ( x .^ 2) + ( y .^ 2);

plot(x,y);
title('Particle Track of a Single Simulated Particle');

Displacement Squared Plot

Theory predicts that the displacement should increase in proportion to the square root of time. The theoretical value of displacement squared is plotted with a thick black line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot. With only a single particle and a small number of samples, deviation from the line can be quite large.

% continuing from above...

clf;
plot(time, (0:1:(N-1)) * 2*k^2 , 'k', 'LineWidth', 3);      % plot theoretical line
hold on;
plot(time, squaredDisplacement);
hold off;

xlabel('Time');
ylabel('Displacement Squared');
title('Displacement Squared versus Time for 1 Particle in 2 Dimensions');




Estimating D from the Simulated Data

The best estimate of the value of D from the simulated data is:

simulatedD = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )

ans = 
     4.22 E-13

Uncertainty in the Estimate

The likely error of this measurement decreases as the square root of the number of samples. This will be discussed in more detail later.

standardError = std( dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )
actualError = D - simulatedD

standardError = 
               1.3162e-014

actualError   = 
               7.1019e-015




Systematic Error -- Bulk Flow in the Solvent

Sometimes, evaporation or uneven heating of the solvent will cause a flow to occur on the slide you are observing. We can model this easily. The following code models a flow with a magnitude 0.5 k in the x direction and 0.1 k in the y direction.

dx = dx + 0.2 * k;
dy = dy + 0.05 * k;

x = cumsum(dx);
y = cumsum(dy);

dSquaredDisplacement = (dx .^ 2) + (dy .^ 2);
 squaredDisplacement = ( x .^ 2) + ( y .^ 2);

simulatedD    = mean( dSquaredDisplacement ) / ( 2 * dimensions * tau )
standardError = std(  dSquaredDisplacement ) / ( 2 * dimensions * tau * sqrt(N) )
actualError = D - simulatedD

plot(x,y);
title('Particle Track of a Single Simulated Particle with Bulk Flow');

simulatedD    =  
               4.2926e-013
standardError =  
               1.3694e-014
actualError   = 
              -2.3859e-016





Displacement Squared in the Presence of Bulk Flow

Notice how the plot of displacement squared diverges from the theoretical value. It has a distinct quadratic component. The magnitude of this error increases dramatically with time. This suggests that the error caused by bulk flow can be minimized by using the shortest possible sampling period. But there's a catch. As you increase the sampling rate, the amount of noise from the motion tracking algorithm goes up. A tenth of a second works pretty well for the particles you will observe in the lab. If you have time, take a few movies at a different rates to see the effect.

clf;
hold on;
plot(time, (0:1:(N-1)) * 2*k^2 , 'k', 'LineWidth', 3);      % plot theoretical line
plot(time, squaredDisplacement);
hold off;

xlabel('Time');
ylabel('Displacement Squared');
title('Displacement Squared versus Time with Bulk Flow');




Simulating Multiple Particles

When you take your data in the lab, you will make movies of many particles. You will use a Matlab program to extract particle tracks from these movies. Because particles drift out of view and go in and out of focus, most movies will be about 5 seconds long at a sample rate of 10 Hz or so. Let's simulate this.



Using a For Loop to Generate Multiple Data Sets

A for loop is the key to generating multiple particle simulations. The results of the simulations are stored in a cellular array of structures called particle. For example, particle{3} refers to a structure containing the results of simulation number 3. particle{3}.D is the estimated value of D for simulation number 3. It is not necessary to understand this code in depth. But do yourself a favor and have a look. The following code also plots all of the generated particle tracks on a single set of axes, each in a random color.

particleCount = 10;
N = 50;
tau = .1;
time = 0:tau:(N-1) * tau;
particle = { };             % create an empty cell array to hold the results

for i = 1:particleCount
    particle{i} = struct();
    particle{i}.dx = k * randn(1,N);
    particle{i}.x = cumsum(particle{i}.dx);
    particle{i}.dy = k * randn(1,N);
    particle{i}.y = cumsum(particle{i}.dy);
    particle{i}.drsquared = particle{i}.dx .^2 + particle{i}.dy .^ 2;
    particle{i}.rsquared = particle{i}.x .^ 2 + particle{i}.y .^ 2;
    particle{i}.D = mean( particle{i}.drsquared ) / ( 2 * dimensions * tau );
    particle{i}.standardError = std( particle{i}.drsquared ) / ( 2 * dimensions * tau * sqrt(N) );
end


clf;
hold on;
for i = 1:particleCount
    plot(particle{i}.x, particle{i}.y, 'color', rand(1,3));
end

xlabel('X position (m)');
ylabel('Y position (m)');
title('Combined Particle Tracks');
hold off;



Displacement Squared

The following plot shows displacement squared versus time for all of the particles. The ensemble average of all the displacements is shown with a thick black line. The theoretical value of displacement squared is plotted with a thick blue line. Since displacement is expected to increase with the square root of time, displacement squared is a straight line in the plot.

% compute the ensemble average
rsquaredSum = zeros(1,N);

for i = 1:particleCount
    rsquaredSum = rsquaredSum + particle{i}.rsquared;
end

ensembleAverage = rsquaredSum / particleCount;

% create the plot
clf;
hold on;
plot(time, (0:1:(N-1)) * 2*k^2 , 'b', 'LineWidth', 3);      % plot theoretical line

plot(time, ensembleAverage , 'k', 'LineWidth', 3);          % plot ensemble average
legend('Theoretical','Average','location','NorthWest');

for i = 1:particleCount
    plot(time, particle{i}.rsquared, 'color', rand(1,3));   % plot each particle track
end

xlabel('Time (seconds)');
ylabel('Displacement Squared (m^2)');
title('Displacement Squared vs Time');
hold off;




Estimated Value of D

This plot shows the computed value of D for each simulation with error bars. A thick blue line indicates the best overall estimate of D (the average of the D value from each simulation) along with error bars in green * WHICH HAPPEN TO BE WRONG -- NEED TO FIX *.


clear D e dx;

% extract the D value from each simulation and place them all into a single
% matrix called 'D'
for i = 1:particleCount
    D(i) = particle{i}.D;
    dx(i,:) = particle{i}.dx;
    e(i) = particle{i}.standardError;
end

% compute the estimate of D and the uncertainty
averageD = mean(D)
uncertainty = std(D)/sqrt(particleCount)

% plot everything
clf;
hold on;

plot(averageD * ones(1,particleCount), 'b', 'linewidth', 3);                    % plot estimated D
plot((averageD + uncertainty) * ones(1,particleCount), 'g-', 'linewidth', 1);   % plot upper error bar
plot((averageD - uncertainty) * ones(1,particleCount), 'g-', 'linewidth', 1);   % plot lower error bar
errorbar(D,e,'ro');                                                             % plot D values with error bars

xlabel('Simulation Number');
ylabel('Estimated Diffusion Coefficient');
title('Estimated Diffusion Coefficient with Error Bars')
legend('Average Value of D', 'location', 'NorthWest');

hold off;

averageD    = 
             4.2886e-013

uncertainty = 
             2.3294e-014




Motion Statistics and Plots

This section looks at the statistical properties of the simulated data in more detail. In particular, it discusses:

  • Uncertainty in the estimate of the mean value of a random variable from a population of samples
  • The effect of squaring a normally distributed random variable
  • The assumption of statistical independence of samples

A More Perfect Distribution

Error creating thumbnail: File missing
Error creating thumbnail: File missing

Did you notice that the distribution of random displacement values in the second section looked a little lopsided? Even with 1000 samples, there can be some noticeable deviations from the ideal distribution. A million samples looks quite a bit closer to the ideal.

hist(randn(1,1e3),25)
xlabel('Value');
ylabel('Frequency');
title('Histogram of Values for 1000 Samples of randn');
hist(randn(1,1e6),100)
xlabel('Value');
ylabel('Frequency');
title('Histogram of Values for 1000000 Samples of randn');



Sampling Uncertainty

Error creating thumbnail: File missing

This lab requires you to make an estimate of the average value of a random variable (actually, the square of a random variable) from a population of samples of that variable. The uncertainty in your estimate decreases with the square root of the number of samples, N. (That is, the standard error = 1/sqrt(N).)

This concept is illustrated in the following plot. To create the plot, fifty populations of N random samples are created for each value of N from 1 to 100. The mean value of each sample population is plotted with an 'x' versus N. The uncertainty appears to decrease as 1/sqrt(N) as expected. About two thirds of the values fall between the error bars (plotted in dark black).

Matlab note: check out the nested for loops used to create the plot.


clf; hold on;
for i= 1:100
    for j = 1:50
        y = randn(1,i);
        m = mean(y);
        plot(i,m,'x');
    end
end

plot(1:100, 1./sqrt(1:100), 'k', 'LineWidth', 2);   % plot upper error bar in dark black
plot(1:100, -1./sqrt(1:100), 'k', 'LineWidth', 2);  % plot lower error bar in dark black
hold off;
xlabel('Population Size (N)');
ylabel('Population Mean');
title('Population Mean of Normally Distributed Random Variable versus Population Size');




What Happens when you Square a Random Variable?

Error creating thumbnail: File missing


We did not estimate D in the simulations from the x and y displacements directly. Instead, we computed the mean squared value of dx and dy. What is the distribution of the resulting value? Adding the squares of two normally distributed random variables results in a chi-squared distribution (with two degrees of freedom) whose mean value equal to the sum of the variances of each variable. Here is what the chi-squared distribution looks like:


dx = randn(1,1e6);
dy = randn(1,1e6);
drSquared = dx .^ 2 + dy .^ 2;

mean(drSquared)
var(dx) + var(dy)

clf;
hist(drSquared,100);
title('Chi Squared Distribution (2 DOF), 1000000 Samples');

ans = 
    1.9982

ans = 
    1.9982




Auto Correlation - A Closer Look at the Data

Error creating thumbnail: File missing

The simulations so far have assumed that we are in the overdamped limit, or the inertia-less regime. This assumption makes generating simulated trajectories very easy - all you have to do is add up a bunch of properly distributed random numbers. Of course, just because something is convenient doesn't mean it's true.

There are many statistical tests for independence and normality. (My personal favorite is the Kolmogorov-Smirnov test – but never before 5:00 PM.) Auto and cross correlation are a good place to start testing for independence. Autocorrelation looks for a relationship between a variable and its past or future values. Cross correlation looks for a relationship between two variables. Matlab has a built in function to compute auto and cross correlations called xcorr.

By construction, the simulated displacements are independent. The change in position at one time should exhibit no relationship at all to the change at any other time. This can be verified with xcorr. If x is a vector, xcorr(x) returns the correlation of x with itself for all possible offsets. The length of the resulting autocorrelation sequence is 2N+1. The middle value always stands out because x correlates perfectly with itself when there is no offset. Specifying the option 'coeff' causes the xcorr function to normalize this value to exactly 1 --perfect correlation.

Also notice that the autocorrelation sequence is symmetric about the origin.

clf;
c = xcorr(particle{1}.dx, 'coeff');
xaxis = (1-length(c))/2:1:(length(c)-1)/2;

plot(xaxis, c);
xlabel('Lag');
ylabel('Correlation');
title('Particle 1 x-axis Displacement Autocorrelation');



Cross Correlation

Error creating thumbnail: File missing

With two vector arguments, xcorr(x,y) returns a cross correlation matrix. The cross correlation can be used to test the relationship (or lack thereof) between one particle's trajectory and another's. If two particles move independently, the cross correlations should be very small. The code below computes the cross correlation between the x displacements of particle simulations number 1 and 2.

clf;
c = xcorr(particle{1}.dx, particle{2}.dx, 'coeff');
xaxis = (1-length(c))/2:1:(length(c)-1)/2;
plot(xaxis, c);
xlabel('Lag');
ylabel('Correlation');
Title('Particle 1, 2 x-axis Displacement Cross Correlation');



MATLAB Tricks

Error creating thumbnail: File missing

Matlab can generate (and plot) an amazing amount of data. For example, the xcorr function can compute the auto and cross correlations of a large set of data values. If A is an MxN matrix, xcorr(A) returns a size 2*M-1 x N^2 matrix whose columns contain the cross-correlation sequences for all combinations of the columns of A. The football shape of the data is a result of the way that the data is windowed when computing the correlation.


% create an array whose columns contain the dx values for each particle

for i = 1:particleCount
    allDx(:,i) = particle{i}.dx';
end

% compute all possible auto and cross correlations

c = xcorr(allDx, 'coeff');

% plot the results
clf;
hold on;
for i=1:size(c,1)
    plot(xaxis, c(:,i),'color',rand(1,3));
end
hold off;

xlabel('Lag');
ylabel('Correlation Coefficient');
title('All Possible Auto and Cross Correlations in the x Dimension');



A Correlated Trajectory

Error creating thumbnail: File missing

So what does the autocorrelation sequence look like for a trajectory that is not generated from independent samples? In the following example, the displacement at each time interval depends in part on the displacement in the last interval plus a random number. The autocorrelation is not nearly as sharp as the one generated by independent random samples.

x     = zeros(1,N);
c     = 0.80;           % degree of correlation; 0 to 1
step  = randn(1,N);
x(2) = randn();

for t=2:N
     x(t) = (c * x(t-1)) + ((1-c)*step(t));
end;

clf;
plot(xaxis, xcorr(x, 'coeff'));

xlabel('Lag');
ylabel('Correlation Coefficient');
title('Autocorrelation');



Additional Resources