Method 1: Linear model mixing
This method is derived from Coleman's thesis, and
uses a mixing function with hyperparameters to be estimated using data, to construct the mixed model. There are several possible mixing functions given in the code below for a user to play with and build off of to write their own mixing function. There are also priors to choose from (in the priors.py file) for the hyperparameters of each mixing function.
Once the mixing function has been chosen, and data supplied or simulated, the user can construct the mixed model by sampling the parameter space using the sampler wrapper below, and then building the posterior predictive distribution (PPD). This is given as
where \(\alpha(g; \theta_{j})\) is the chosen mixing function with hyperparameters \(\theta_{j}\).
LMM(loworder, highorder, error_model='informative')
of specific orders of the true model to mix. Dependent on the Models class to run the expansion functions.
Example
LMM(loworder=np.array([2]), highorder=np.array([2]), error_model='informative')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loworder
|
(ndarray, int)
|
The truncation order to which we calculate the small-g expansion. |
required |
highorder
|
(ndarray, int)
|
The truncation order to which we calculate the large-g expansion. |
required |
error_model
|
str
|
The error model chosen for this calculation. Can be either 'uninformative' or 'informative'. Default is 'informative'. |
'informative'
|
Returns:
| Type | Description |
|---|---|
|
None. |
Source code in samba/mixing.py
MAP_values(thin, g, g_data, data, sigma, plot=True)
A function to calculate the MAP values of sampled distributions of parameters. Will calculate for as many parameters as are present and return results in an array.
Example
LMM.MAP_values(thin=np.array([]), g_data=np.linspace(), g=np.linspace(), data=np.array([]), sigma=np.array([]))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
thin
|
ndarray
|
The array of thinned samples from the stats_chain() function. |
required |
g
|
linspace
|
The input space over which the mixing is calculated. |
required |
g_data
|
linspace
|
The array of input points in g for the data set. |
required |
data
|
ndarray
|
The data set being used for the mixing calculation. |
required |
sigma
|
ndarray
|
The data error set being used for the mixing calculation. |
required |
plot
|
bool
|
The option to plot the weights over the input space in g. Default is True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
map_values |
ndarray
|
The MAP values of each parameter. |
Source code in samba/mixing.py
add_data(g_true, g_data, data=None, sigma=None, error=None, plot=True)
A data generation function that generates data based on the g_data linspace provided (with the number of points chosen by the user) and the error desired on each point (also input by the user), or accepts the user's input of an array of data and standard deviations of the data points.
Example
LMM.add_data(g_true=np.linspace(0.0, 0.5, 100), g_data=np.linspace(0.0, 0.5, 20), error=0.01, plot=False)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g_true
|
linspace
|
The linspace desired for the true model to be calculated. |
required |
g_data
|
linspace
|
The linspace input for the data to be generated within. |
required |
data
|
ndarray
|
The data array entered by the user; if user wishes to generate data, this remains set to None. |
None
|
sigma
|
ndarray
|
The standard deviation array entered by the user; if user wishes to generate data, this will remain set to None. |
None
|
error
|
float
|
The error to put on the data set if the data set is not being given by the user. Enter in decimal form (0.01 = 1%). Default is None. |
None
|
plot
|
bool
|
The option to plot the data. Default is True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
data |
ndarray
|
The array of data (generated or entered by the user). |
sigma |
ndarray
|
The standard deviation at each data point (generated or entered by the user). |
Source code in samba/mixing.py
burnin_trace(sampler_object, nsteps)
A small function to take the burn-in samples off of the sampler chain from the LMM.mixed_model function, and to send back the trace of the sampler chain to LMM.mixed_model.
Example
LMM.burnin_trace(sampler_object=sampler_mixed, nsteps=3000)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sampler_object
|
object
|
The chain sent back by the emcee sampler after it finishes running through the samples and walkers. |
required |
nsteps
|
int
|
The number of steps per walker. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
emcee_trace_mixed |
ndarray
|
The trace of the sampler chain with the user's desired number of burn-in samples removed. |
Source code in samba/mixing.py
cdf(params, g)
staticmethod
The cumulative distribution function of a standard normal distribution, with two free parameters determined by sampling.
Example
cdf(params=np.array(), g=0.5)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ndarray
|
The array of parameters the sampler will determine (here labelled beta0 and beta1, where beta0 controls the location of the function and beta1 controls the slope). |
required |
g
|
float The value of g the cdf is calculated at. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
function |
float
|
The result of the cdf function at the value of g. |
Source code in samba/mixing.py
hpd_interval(trace, fraction)
staticmethod
A function to calculate the Bayesian credible intervals of a posterior distribution. This function uses the HPD (highest posterior density) method.
Example
LMM.hpd_interval(trace=emcee_trace, fraction=0.95)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
ndarray
|
The trace generated by a sampler when sampling a variable to obtain its posterior distribution. |
required |
fraction
|
float
|
The percent (in decimal form) requested by the user to set the credibility interval. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
interval |
ndarray
|
The credibility interval bounds in a numpy array (format: [min, max]). |
Source code in samba/mixing.py
likelihood_high(g_data, data, sigma, sighigh)
The likelihood function for the data using the large-g expansion as the model in the chi-squared.
Example
LMM.likelihood_high(g_data=np.linspace(0.0, 0.5, 20), data=np.array(), sigma=np.array(), highorder=23)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g_data
|
linspace
|
A linspace used to generate data points. |
required |
data
|
ndarray
|
An array of data points generated or supplied by the user. |
required |
sigma
|
ndarray
|
An array of standard deviations at each point in 'data'. |
required |
Returns:
| Type | Description |
|---|---|
|
An array of the likelihood calculated at each data point. |
Source code in samba/mixing.py
likelihood_low(g_data, data, sigma, siglow)
The likelihood function for the data using the small-g expansion as the model in the chi-squared.
Example
LMM.likelihood_low(g_data=np.linspace(0.0, 0.5, 20), data=np.array(), sigma=np.array(), loworder=5)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g_data
|
linspace
|
A linspace used to generate data points. |
required |
data
|
ndarray
|
An array of data points generated or supplied by the user. |
required |
sigma
|
ndarray
|
An array of standard deviations at each point in 'data'. |
required |
Returns:
| Type | Description |
|---|---|
|
An array of the likelihood calculated at each data point. |
Source code in samba/mixing.py
logistic(params, g)
staticmethod
A basic logistic function often used in machine learning, implemented here with two free parameters to be determined via sampling.
Example
logistic(params=np.array(), g=0.5)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ndarray
|
The array of parameters the sampler will determine (here labelled beta0 and beta1, where beta0 controls the location of the function and beta1 controls the slope). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mixing |
float
|
The result of the logistic function given the value g. |
Source code in samba/mixing.py
mixed_model(g_data, data, sigma, mixing_function='cosine', nsteps=1000)
A function that will run the emcee ensemble sampler for a given mixed model to determine at least one unknown parameter in the mixing function selected. The function asks the user to decide which mixing function to use, and runs the subsequent code to use the correct one. Functions sent to the sampler are static methods defined at the end of this class.
Example
LMM.mixed_model(g_data=np.linspace(0.0, 0.5, 20), data=np.array(), sigma=np.array(), mixing_function='cosine', nsteps=3000)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g_data
|
linspace
|
The linspace over which the data was generated. |
required |
data
|
ndarray
|
An array of data points, either generated or supplied by the user. |
required |
sigma
|
ndarray
|
An array of standard deviations at each data point. |
required |
mixing_function
|
str
|
The name of the mixing function to use for the LMM method. Default is the piecewise cosine. |
'cosine'
|
nsteps
|
int
|
The number of steps per walker for the sampler to use. |
1000
|
Returns:
| Name | Type | Description |
|---|---|---|
sampler_mixed |
object
|
The sampler results, contained in a sampler object, from the determination of the unknown parameter. |
emcee_trace_mixed |
ndarray
|
The trace of each parameter, with burnin samples extracted. |
Source code in samba/mixing.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
plot_MAP(g, map_values)
A simple rough plotter to plot the weight/mixing function for the LMM method using the mixing function calculated at the points in g and the MAP values of its parameters.
Example
LMM.plot_MAP(g=np.linspace(), map_values=numpy.ndarray([]))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g
|
linspace
|
The input space over which the mixing is calculated. |
required |
map_values
|
ndarray
|
The results of the MAP_values() function (MAP values of each parameter in the mixing function selected). |
required |
Returns:
| Type | Description |
|---|---|
|
None. |
Source code in samba/mixing.py
plot_data(g_true, g_data, data)
The plotting function to display the generated data and true model.
Example
LMM.plot_data(g_true=np.linspace(0.0, 0.5, 100), g_data=np.linspace(0.0, 0.5, 20), data=np.array([]))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g_true
|
linspace
|
The linspace desired for the true model to be calculated. |
required |
g_data
|
linspace
|
The linspace over which the data was generated. |
required |
data
|
ndarray
|
The array of data generated using the LMM.add_data function. |
required |
Returns:
| Type | Description |
|---|---|
|
None. |
Source code in samba/mixing.py
plot_ppd(results, g_data, g_ppd, data, ppd_results, ppd_intervals, percent)
A plotting function that can be used to plot the posterior predictive distribution (PPD) results (mean and credible interval) obtained from calling the functions above in the main code, as well as data generated, the true model, and the small- and large-g expansions chosen for the mixed model calculation.
Example
LMM.plot_ppd(g_data=np.linspace(0.0, 0.5, 20), g_true=np.linspace(0.0, 0.5, 100), g_ppd=np.linspace(0.0, 0.5, 200), data=np.array(), ppd_results=np.array(), ppd_intervals=np.array(), percent=68)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
ndarray
|
The mean or the median of the estimated parameters from the posterior draws. |
required |
g_data
|
linspace
|
The linspace used to generate the data. |
required |
g_ppd
|
linspace
|
The linspace chosen to calculate the PPD over. |
required |
data
|
ndarray
|
An array of data either generated or supplied by the user. |
required |
ppd_results
|
ndarray
|
An array of the mean of the PPD at each point in the g_ppd linspace. |
required |
ppd_intervals
|
ndarray
|
A 2D array of the credibility interval calculated for the PPD (containing both bounds). |
required |
percent
|
int
|
The percent credibility interval calculated for the variable ppd_intervals (used in the plot legend). |
required |
Returns:
| Type | Description |
|---|---|
|
None. |
Source code in samba/mixing.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | |
ppd(trace, param_values, g_data, g, data, ci, plot=True)
A function to calculate the posterior predictive distribution (PPD) for any chosen mixing function defined in this class.
Example
LMM.ppd(trace, param_values=np.array([]),g_data=np.linspace(1e-6,1.0,10), g_ppd=np.linspace(0.0, 0.5, 100), ci=68)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
ndarray
|
The trace of each of the parameters from the sampler. |
required |
param_values
|
ndarray
|
The mean, median, or MAP values of the parameters. |
required |
g_data
|
linspace
|
The linspace in g from which the data set was calculated. |
required |
g
|
linspace
|
The linspace over which the PPD result will be calculated. |
required |
data
|
ndarray
|
The data set used to calculate the mixed model. |
required |
ci
|
int
|
The desired credibility interval. Can be either 68 or 95. |
required |
plot
|
bool
|
The option to plot the PPD result with the series expansions and true model. Default is True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
switch_med_results |
ndarray
|
The array of median values from the PPD at each point in g. |
switch_g_intervals |
ndarray
|
The array of credibility interval values for the median results of the PPD. |
Source code in samba/mixing.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
sampler_mix(params, g_data, data, sigma, siglow, sighigh)
The model mixing function sent to the sampler to find the values of the parameters in the selected mixing function.
Example
emcee.EnsembleSampler(nwalkers, self.sampler_mix, args=[g_data, data, sigma])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ndarray
|
The parameters that are being determined by the sampler, in an array (not input). |
required |
g_data
|
linspace
|
The linspace used to generate the data. |
required |
data
|
ndarray
|
An array of data either generated or supplied by the user. |
required |
sigma
|
ndarray
|
An array of standard deviations for each data point. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mixed_results |
ndarray
|
The results of the mixing function for the entire linspace in g, in an array format. |
Source code in samba/mixing.py
stats_chain(chain, plot=True)
Calculates the autocorrelation time and thins the samples accordingly for a better estimate of the mean, median, and MAP values.
Example
LMM.stats_chain(chain=emcee.object, plot=False)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chain
|
object
|
The object resulting from sampling the parameters using emcee. The chain of samples must be extracted from it. |
required |
plot
|
bool
|
The option to plot the traces of the sample chains and the corner plot of the parameter distributions. Default is True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
thin |
ndarray
|
The array of thinned samples per parameter. Used externally to calculate the MAP values. |
median_results |
ndarray
|
Each of the median parameter values found from the sampling. |
mean_results |
ndarray
|
Each of the mean parameter values found from the sampling. |
Source code in samba/mixing.py
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 | |
stats_trace(trace)
A function to calculate the mean and credible intervals corresponding to each parameter. The trace plots for each parameter are plotted.
Example
LMM.stats_trace(trace=np.array([]))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
ndarray
|
The trace from the sampler object that was generated when estimating the parameters of the mixing function. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mean |
ndarray
|
The array of mean values for each parameter. |
ci |
ndarray
|
The array of sets of credible interval bounds for each parameter. |
Source code in samba/mixing.py
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 | |
step(params, g)
staticmethod
A step mixing function to switch between two models. Only useful for two models.
Example
step(params, g=0.2)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ndarray
|
One single parameter to determine where the step function will break from one model to the other. |
required |
g
|
float
|
One value of the input space. |
required |
Returns:
| Type | Description |
|---|---|
|
The value of the step function at a specific |
|
|
point in g. |
Source code in samba/mixing.py
switchcos(params, g)
staticmethod
A piecewise function using two constants at either end, and two cosine functions in the centre, to be used as a mixing function. One free parameter, g3, is found by sampling.
Example
switchcos(params=np.array(), g=0.5)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
ndarray
|
The array of parameters to be determined by the sampler (here labelled g1, g2, and g3, where g1 is the separation point between the first constant function and the first cosine function, g2 is the separation point between the second cosine function and the second constant function, and g3 is the point between the two cosine functions). |
required |
g
|
float
|
The value of g that this cosine function is calculated at. |
required |
Returns:
| Type | Description |
|---|---|
|
The value of the function at a specific point in g. |