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

Last change on this file since 7609046 was 7609046, checked in by GitHub <noreply@…>, 6 years ago

Merge branch 'master' into ticket-608-user-defined-weights

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