source: sasmodels/doc/guide/pd/polydispersity.rst @ 36873d1

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

improve docs for user defined distributions and fix cyclic gaussian example

  • Property mode set to 100644
File size: 13.8 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
[36873d1]321*Distribution* object with a *_weights* method.  The *_weights* method takes
322*center*, *sigma*, *lb* and *ub* as arguments, and can access *self.npts*
323and *self.nsigmas* from the distribution.  They are interpreted as follows:
[aa25fc7]324
[36873d1]325* *center* the value of the shape parameter (for size dispersity) or zero
326  if it is an angular dispersity.  This parameter may be fitted.
327
328* *sigma* the width of the distribution, with is the polydispersity parameter
329  times the center for size dispersity, or the polydispersity parameter alone
330  for angular dispersity.  This parameter may be fitted.
331
332* *lb*, *ub* are the parameter limits given in the model definition file.  For
333  example, a radius parameter has *lb* equal to zero.  A volume fraction
334  parameter would have *lb* equal to zero and *ub* equal to one.
335
336* *self.nsigmas* the distance to go into the tails when evaluating the
337  distribution.  For a two parameter distribution, this value could be
338  co-opted to use for the second parameter, though it will not be available
339  for fitting.
340
341* *self.npts* the number of points to use when evaluating the distribution.
342  The user will adjust this to trade calculation time for accuracy, but the
343  distribution code is free to return more or fewer, or use it for the third
344  parameter in a three parameter distribution.
345
346The code following wraps the Laplace distribution from scipy stats::
[aa25fc7]347
348    import numpy as np
349    from scipy.stats import laplace
350
351    from sasmodels import weights
352
353    class Dispersion(weights.Dispersion):
354        r"""
355        Laplace distribution
356
357        .. math::
358
359            w(x) = e^{-\sigma |x - \mu|}
360        """
361        type = "laplace"
362        default = dict(npts=35, width=0, nsigmas=3)  # default values
363        def _weights(self, center, sigma, lb, ub):
364            x = self._linspace(center, sigma, lb, ub)
365            wx = laplace.pdf(x, center, sigma)
366            return x, wx
367
[36873d1]368You can plot the weights for a given value and width using the following::
[aa25fc7]369
370    from numpy import inf
371    from matplotlib import pyplot as plt
372    from sasmodels import weights
373
374    # reload the user-defined weights
375    weights.load_weights()
376    x, wx = weights.get_weights('laplace', n=35, width=0.1, nsigmas=3, value=50,
377                                limits=[0, inf], relative=True)
378
379    # plot the weights
380    plt.interactive(True)
381    plt.plot(x, wx, 'x')
382
[36873d1]383The *self.nsigmas* and *self.npts* parameters are normally used to control
384the accuracy of the distribution integral. The *self._linspace* function
385uses them to define the *x* values (along with the *center*, *sigma*,
386*lb*, and *ub* which are passed as parameters).  If you repurpose npts or
387nsigmas you will need to generate your own *x*.  Be sure to honour the
388limits *lb* and *ub*, for example to disallow a negative radius or constrain
389the volume fraction to lie between zero and one.
[aa25fc7]390
391.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
392
[990d8df]393Note about DLS polydispersity
394^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
395
396Many commercial Dynamic Light Scattering (DLS) instruments produce a size
[1f058ea]397polydispersity parameter, sometimes even given the symbol $p$\ ! This
[990d8df]398parameter is defined as the relative standard deviation coefficient of
399variation of the size distribution and is NOT the same as the polydispersity
400parameters in the Lognormal and Schulz distributions above (though they all
401related) except when the DLS polydispersity parameter is <0.13.
402
[5026e05]403.. math::
404
405    p_{DLS} = \sqrt(\nu / \bar x^2)
406
[ed5b109]407where $\nu$ is the variance of the distribution and $\bar x$ is the mean
[f4ae8c4]408value of $x$.
[5026e05]409
[990d8df]410For more information see:
411S King, C Washington & R Heenan, *Phys Chem Chem Phys*, (2005), 7, 143
412
413.. ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
414
415*Document History*
416
417| 2015-05-01 Steve King
418| 2017-05-08 Paul Kienzle
[5026e05]419| 2018-03-20 Steve King
[29afc50]420| 2018-04-04 Steve King
Note: See TracBrowser for help on using the repository browser.