source: sasmodels/doc/guide/pd/polydispersity.rst @ aa25fc7

Last change on this file since aa25fc7 was aa25fc7, checked in by Paul Kienzle <pkienzle@…>, 6 years ago

load user-defined weight functions from ~/.sasview/weights/*.py

  • Property mode set to 100644
File size: 13.1 KB
RevLine 
[990d8df]1.. pd_help.rst
2
3.. This is a port of the original SasView html help file to ReSTructured text
4.. by S King, ISIS, during SasView CodeCamp-III in Feb 2015.
5
6.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
7
[eda8b30]8.. _polydispersityhelp:
9
[990d8df]10Polydispersity Distributions
11----------------------------
12
[eda8b30]13With some models in sasmodels we can calculate the average intensity for a
[990d8df]14population of particles that exhibit size and/or orientational
[eda8b30]15polydispersity. The resultant intensity is normalized by the average
[990d8df]16particle volume such that
17
18.. math::
19
20  P(q) = \text{scale} \langle F^* F \rangle / V + \text{background}
21
22where $F$ is the scattering amplitude and $\langle\cdot\rangle$ denotes an
23average over the size distribution.
24
[ed5b109]25Each distribution is characterized by a center value $\bar x$ or
26$x_\text{med}$, a width parameter $\sigma$ (note this is *not necessarily*
27the standard deviation, so read the description carefully), the number of
28sigmas $N_\sigma$ to include from the tails of the distribution, and the
29number of points used to compute the average. The center of the distribution
[aa25fc7]30is set by the value of the model parameter. The meaning of a polydispersity
31parameter *PD* (not to be confused with a molecular weight distributions
32in polymer science) in a model depends on the type of parameter it is being
[29afc50]33applied too.
[ed5b109]34
[aa25fc7]35The distribution width applied to *volume* (ie, shape-describing) parameters
36is relative to the center value such that $\sigma = \mathrm{PD} \cdot \bar x$.
37However, the distribution width applied to *orientation* (ie, angle-describing)
[29afc50]38parameters is just $\sigma = \mathrm{PD}$.
[ed5b109]39
40$N_\sigma$ determines how far into the tails to evaluate the distribution,
41with larger values of $N_\sigma$ required for heavier tailed distributions.
[990d8df]42The scattering in general falls rapidly with $qr$ so the usual assumption
43that $G(r - 3\sigma_r)$ is tiny and therefore $f(r - 3\sigma_r)G(r - 3\sigma_r)$
44will not contribute much to the average may not hold when particles are large.
45This, too, will require increasing $N_\sigma$.
46
47Users should note that the averaging computation is very intensive. Applying
48polydispersion to multiple parameters at the same time or increasing the
49number of points in the distribution will require patience! However, the
50calculations are generally more robust with more data points or more angles.
51
[22279a4]52The following distribution functions are provided:
[990d8df]53
[75e4319]54*  *Uniform Distribution*
[5026e05]55*  *Rectangular Distribution*
[990d8df]56*  *Gaussian Distribution*
[5026e05]57*  *Boltzmann Distribution*
[990d8df]58*  *Lognormal Distribution*
59*  *Schulz Distribution*
60*  *Array Distribution*
61
62These are all implemented as *number-average* distributions.
63
[5026e05]64Additional distributions are under consideration.
[990d8df]65
[5026e05]66Suggested Applications
67^^^^^^^^^^^^^^^^^^^^^^
[990d8df]68
[ed5b109]69If applying polydispersion to parameters describing particle sizes, use
[5026e05]70the Lognormal or Schulz distributions.
[990d8df]71
[ed5b109]72If applying polydispersion to parameters describing interfacial thicknesses
[5026e05]73or angular orientations, use the Gaussian or Boltzmann distributions.
[990d8df]74
[aa25fc7]75If applying polydispersion to parameters describing angles, use the Uniform
76distribution. Beware of using distributions that are always positive (eg, the
[29afc50]77Lognormal) because angles can be negative!
78
[5026e05]79The array distribution allows a user-defined distribution to be applied.
[990d8df]80
[5026e05]81.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
[990d8df]82
[5026e05]83Uniform Distribution
84^^^^^^^^^^^^^^^^^^^^
[990d8df]85
[5026e05]86The Uniform Distribution is defined as
[990d8df]87
[f4ae8c4]88.. math::
[990d8df]89
[f4ae8c4]90    f(x) = \frac{1}{\text{Norm}}
91    \begin{cases}
92        1 & \text{for } |x - \bar x| \leq \sigma \\
93        0 & \text{for } |x - \bar x| > \sigma
94    \end{cases}
[990d8df]95
[f4ae8c4]96where $\bar x$ ($x_\text{mean}$ in the figure) is the mean of the
97distribution, $\sigma$ is the half-width, and *Norm* is a normalization
98factor which is determined during the numerical calculation.
[990d8df]99
[f4ae8c4]100The polydispersity in sasmodels is given by
[990d8df]101
[f4ae8c4]102.. math:: \text{PD} = \sigma / \bar x
[92d330fd]103
[f4ae8c4]104.. figure:: pd_uniform.jpg
[3d58247]105
[f4ae8c4]106    Uniform distribution.
[990d8df]107
[5026e05]108The value $N_\sigma$ is ignored for this distribution.
109
110.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
111
112Rectangular Distribution
[75e4319]113^^^^^^^^^^^^^^^^^^^^^^^^
114
[5026e05]115The Rectangular Distribution is defined as
[75e4319]116
[f4ae8c4]117.. math::
[75e4319]118
[f4ae8c4]119    f(x) = \frac{1}{\text{Norm}}
120    \begin{cases}
121        1 & \text{for } |x - \bar x| \leq w \\
122        0 & \text{for } |x - \bar x| > w
123    \end{cases}
[75e4319]124
[f4ae8c4]125where $\bar x$ ($x_\text{mean}$ in the figure) is the mean of the
126distribution, $w$ is the half-width, and *Norm* is a normalization
127factor which is determined during the numerical calculation.
[75e4319]128
[f4ae8c4]129Note that the standard deviation and the half width $w$ are different!
[75e4319]130
[f4ae8c4]131The standard deviation is
[75e4319]132
[f4ae8c4]133.. math:: \sigma = w / \sqrt{3}
[75e4319]134
[f4ae8c4]135whilst the polydispersity in sasmodels is given by
[92d330fd]136
[f4ae8c4]137.. math:: \text{PD} = \sigma / \bar x
[5026e05]138
[f4ae8c4]139.. figure:: pd_rectangular.jpg
[5026e05]140
[f4ae8c4]141    Rectangular distribution.
[ed5b109]142
[f4ae8c4]143.. note:: The Rectangular Distribution is deprecated in favour of the
144            Uniform Distribution above and is described here for backwards
145            compatibility with earlier versions of SasView only.
[75e4319]146
[990d8df]147.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
148
149Gaussian Distribution
150^^^^^^^^^^^^^^^^^^^^^
151
152The Gaussian Distribution is defined as
153
[f4ae8c4]154.. math::
[5026e05]155
[f4ae8c4]156    f(x) = \frac{1}{\text{Norm}}
157            \exp\left(-\frac{(x - \bar x)^2}{2\sigma^2}\right)
[990d8df]158
[f4ae8c4]159where $\bar x$ ($x_\text{mean}$ in the figure) is the mean of the
160distribution and *Norm* is a normalization factor which is determined
161during the numerical calculation.
[990d8df]162
[f4ae8c4]163The polydispersity in sasmodels is given by
[990d8df]164
[f4ae8c4]165.. math:: \text{PD} = \sigma / \bar x
[5026e05]166
[f4ae8c4]167.. figure:: pd_gaussian.jpg
[5026e05]168
[f4ae8c4]169    Normal distribution.
[5026e05]170
171.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
172
173Boltzmann Distribution
174^^^^^^^^^^^^^^^^^^^^^^
175
176The Boltzmann Distribution is defined as
[990d8df]177
[f4ae8c4]178.. math::
[990d8df]179
[f4ae8c4]180    f(x) = \frac{1}{\text{Norm}}
181            \exp\left(-\frac{ | x - \bar x | }{\sigma}\right)
[990d8df]182
[f4ae8c4]183where $\bar x$ ($x_\text{mean}$ in the figure) is the mean of the
184distribution and *Norm* is a normalization factor which is determined
185during the numerical calculation.
[5026e05]186
[f4ae8c4]187The width is defined as
[5026e05]188
[f4ae8c4]189.. math:: \sigma=\frac{k T}{E}
[5026e05]190
[f4ae8c4]191which is the inverse Boltzmann factor, where $k$ is the Boltzmann constant,
192$T$ the temperature in Kelvin and $E$ a characteristic energy per particle.
[5026e05]193
[f4ae8c4]194.. figure:: pd_boltzmann.jpg
[5026e05]195
[f4ae8c4]196    Boltzmann distribution.
[990d8df]197
198.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
199
200Lognormal Distribution
201^^^^^^^^^^^^^^^^^^^^^^
202
[ed5b109]203The Lognormal Distribution describes a function of $x$ where $\ln (x)$ has
204a normal distribution. The result is a distribution that is skewed towards
205larger values of $x$.
[5026e05]206
[990d8df]207The Lognormal Distribution is defined as
208
[f4ae8c4]209.. math::
[990d8df]210
[f4ae8c4]211    f(x) = \frac{1}{\text{Norm}}\frac{1}{x\sigma}
212            \exp\left(-\frac{1}{2}
213                        \bigg(\frac{\ln(x) - \mu}{\sigma}\bigg)^2\right)
[990d8df]214
[f4ae8c4]215where *Norm* is a normalization factor which will be determined during
216the numerical calculation, $\mu=\ln(x_\text{med})$ and $x_\text{med}$
217is the *median* value of the *lognormal* distribution, but $\sigma$ is
218a parameter describing the width of the underlying *normal* distribution.
[ed5b109]219
[f4ae8c4]220$x_\text{med}$ will be the value given for the respective size parameter
221in sasmodels, for example, *radius=60*.
[990d8df]222
[f4ae8c4]223The polydispersity in sasmodels is given by
[990d8df]224
[29afc50]225.. math:: \text{PD} = \sigma = p / x_\text{med}
[990d8df]226
[29afc50]227The mean value of the distribution is given by $\bar x = \exp(\mu+ \sigma^2/2)$
228and the peak value by $\max x = \exp(\mu - \sigma^2)$.
[990d8df]229
[f4ae8c4]230The variance (the square of the standard deviation) of the *lognormal*
231distribution is given by
[990d8df]232
[f4ae8c4]233.. math::
[990d8df]234
[f4ae8c4]235    \nu = [\exp({\sigma}^2) - 1] \exp({2\mu + \sigma^2})
[990d8df]236
[f4ae8c4]237Note that larger values of PD might need a larger number of points
238and $N_\sigma$.
[ed5b109]239
[f4ae8c4]240.. figure:: pd_lognormal.jpg
[990d8df]241
[29afc50]242    Lognormal distribution for PD=0.1.
[990d8df]243
[5026e05]244For further information on the Lognormal distribution see:
[ed5b109]245http://en.wikipedia.org/wiki/Log-normal_distribution and
[5026e05]246http://mathworld.wolfram.com/LogNormalDistribution.html
[990d8df]247
248.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
249
250Schulz Distribution
251^^^^^^^^^^^^^^^^^^^
252
[ed5b109]253The Schulz (sometimes written Schultz) distribution is similar to the
254Lognormal distribution, in that it is also skewed towards larger values of
255$x$, but which has computational advantages over the Lognormal distribution.
[5026e05]256
[990d8df]257The Schulz distribution is defined as
258
[f4ae8c4]259.. math::
[990d8df]260
[f4ae8c4]261    f(x) = \frac{1}{\text{Norm}} (z+1)^{z+1}(x/\bar x)^z
262            \frac{\exp[-(z+1)x/\bar x]}{\bar x\Gamma(z+1)}
[990d8df]263
[f4ae8c4]264where $\bar x$ ($x_\text{mean}$ in the figure) is the mean of the
265distribution, *Norm* is a normalization factor which is determined
266during the numerical calculation, and $z$ is a measure of the width
267of the distribution such that
[990d8df]268
[f4ae8c4]269.. math:: z = (1-p^2) / p^2
[990d8df]270
[f4ae8c4]271where $p$ is the polydispersity in sasmodels given by
[990d8df]272
[f4ae8c4]273.. math:: PD = p = \sigma / \bar x
[990d8df]274
[f4ae8c4]275and $\sigma$ is the RMS deviation from $\bar x$.
[ed5b109]276
[f4ae8c4]277Note that larger values of PD might need a larger number of points
278and $N_\sigma$. For example, for PD=0.7 with radius=60 |Ang|, at least
279Npts>=160 and Nsigmas>=15 are required.
[990d8df]280
[f4ae8c4]281.. figure:: pd_schulz.jpg
[990d8df]282
[f4ae8c4]283    Schulz distribution.
[990d8df]284
285For further information on the Schulz distribution see:
[5026e05]286M Kotlarchyk & S-H Chen, *J Chem Phys*, (1983), 79, 2461 and
[ed5b109]287M Kotlarchyk, RB Stephens, and JS Huang, *J Phys Chem*, (1988), 92, 1533
[990d8df]288
289.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
290
291Array Distribution
292^^^^^^^^^^^^^^^^^^
293
[a5a12ca]294This user-definable distribution should be given as a simple ASCII text
[990d8df]295file where the array is defined by two columns of numbers: $x$ and $f(x)$.
296The $f(x)$ will be normalized to 1 during the computation.
297
298Example of what an array distribution file should look like:
299
300====  =====
301 30    0.1
302 32    0.3
303 35    0.4
304 36    0.5
305 37    0.6
306 39    0.7
307 41    0.9
308====  =====
309
310Only these array values are used computation, therefore the parameter value
311given for the model will have no affect, and will be ignored when computing
312the average.  This means that any parameter with an array distribution will
[a5a12ca]313not be fitable.
314
315.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
316
[aa25fc7]317User-defined Distributions
318^^^^^^^^^^^^^^^^^^^^^^^^^^
319
320You can define your own distribution by creating a python file defining a
321*Distribution* object.  The distribution is parameterized by *center*
322(which is always zero for orientation dispersity, or parameter value for
323size dispersity), *sigma* (which is the distribution width in degrees for
324orientation parameters, or center times width for size dispersity), and
325bounds *lb* and *ub* (which are the bounds on the possible values of the
326parameter given in the model definition).
327
328For example, the following wraps the Laplace distribution from scipy stats::
329
330    import numpy as np
331    from scipy.stats import laplace
332
333    from sasmodels import weights
334
335    class Dispersion(weights.Dispersion):
336        r"""
337        Laplace distribution
338
339        .. math::
340
341            w(x) = e^{-\sigma |x - \mu|}
342        """
343        type = "laplace"
344        default = dict(npts=35, width=0, nsigmas=3)  # default values
345        def _weights(self, center, sigma, lb, ub):
346            x = self._linspace(center, sigma, lb, ub)
347            wx = laplace.pdf(x, center, sigma)
348            return x, wx
349
350To see that the distribution is correct use the following::
351
352    from numpy import inf
353    from matplotlib import pyplot as plt
354    from sasmodels import weights
355
356    # reload the user-defined weights
357    weights.load_weights()
358    x, wx = weights.get_weights('laplace', n=35, width=0.1, nsigmas=3, value=50,
359                                limits=[0, inf], relative=True)
360
361    # plot the weights
362    plt.interactive(True)
363    plt.plot(x, wx, 'x')
364
365Any python code can be used to define the distribution.  The distribution
366parameters are available as *self.npts*, *self.width* and *self.nsigmas*.
367Try to follow the convention of gaussian width, npts and number of sigmas
368in the tail, but if your distribution requires more parameters you are free
369to interpret them as something else.  In particular, npts allows you to
370trade accuracy against running time when evaluating your models.  The
371*self._linspace* function uses *self.npts* and *self.nsigmas* to define
372the set of *x* values to use for the distribution (along with the *center*,
373*sigma*, *lb*, and *ub* passed as parameters).  You can use an arbitrary
374set of *x* points.
375
376.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
377
[990d8df]378Note about DLS polydispersity
379^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
380
381Many commercial Dynamic Light Scattering (DLS) instruments produce a size
[1f058ea]382polydispersity parameter, sometimes even given the symbol $p$\ ! This
[990d8df]383parameter is defined as the relative standard deviation coefficient of
384variation of the size distribution and is NOT the same as the polydispersity
385parameters in the Lognormal and Schulz distributions above (though they all
386related) except when the DLS polydispersity parameter is <0.13.
387
[5026e05]388.. math::
389
390    p_{DLS} = \sqrt(\nu / \bar x^2)
391
[ed5b109]392where $\nu$ is the variance of the distribution and $\bar x$ is the mean
[f4ae8c4]393value of $x$.
[5026e05]394
[990d8df]395For more information see:
396S King, C Washington & R Heenan, *Phys Chem Chem Phys*, (2005), 7, 143
397
398.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
399
400*Document History*
401
402| 2015-05-01 Steve King
403| 2017-05-08 Paul Kienzle
[5026e05]404| 2018-03-20 Steve King
[29afc50]405| 2018-04-04 Steve King
Note: See TracBrowser for help on using the repository browser.