# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Fit multiple datasets with multiple expressions that share parameters

Contributed by:
Sjoerd Smit

ResourceFunction["MultiNonlinearModelFit"][{ is a generalized form of NonlinearModelFit where each dataset | |

ResourceFunction["MultiNonlinearModelFit"][{ fits the model subject to parameter constraints |

ResourceFunction["MultiNonlinearModelFit"] works by recasting the data and fitting forms into a form suitable for NonlinearModelFit.

The datasets *dat*_{i} must all be numeric matrices.

The number of datasets *dat*_{i} must match the number of expressions *form*_{i}. It is also possible to specify a function that evaluates to a list when provided with numerical arguments.

The parameters *β*_{i} can be specified in the same way as in NonlinearModelFit. ResourceFunction["MultiNonlinearModelFit"] also inherits all options from NonlinearModelFit.

Internally, the datasets *dat*_{i} are joined together into a single matrix, so if a Weights vector is specified as an option, it should have the same length as Join[*dat*_{1},*dat*_{2},…]. It is also possible to specify a Weights vector that has a length matching the number of datasets. There is a special option Weights → "InverseLengthWeights" to specify that each dataset should weigh equally in the fit, regardless of the number of points in each. Finally, it is also possible to specify the Weights as a list of vectors matching the input data.

The returned model uses the symbol (\[FormalN]) to switch between the different expressions *form*_{i}. This symbol should therefore be avoided by the user when using ResourceFunction["MultiNonlinearModelFit"]. This symbol can be changed with the "DatasetIndexSymbol" option.

Fit some random data to simple linear models with a shared slope parameter:

In[1]:= |

Out[4]= |

In[5]:= |

Out[5]= |

Fit the same model with parameter constraints to allow for a small difference between the slopes of the lines (this works best by using a quadratic form for the difference between the slope parameters, since (*a*_{1}-*a*_{2})^{2}is differentiable whereas Abs[*a*_{1}–*a*_{2}] is not):

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

It is possible to define a fitting function that evaluates to a list of values equal to the number of datasets when provided with numerical parameters:

The fit function does not evaluate symbolically, only numerically:

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

Use this function to fit with:

In[10]:= |

Out[10]= |

In[11]:= |

Out[11]= |

Fit two Gaussian peaks with a shared location parameter:

In[12]:= |

Out[13]= |

Fit with the models that were used to generate the data:

In[14]:= |

Out[14]= |

In[15]:= |

Out[15]= |

Extract the fits as a list of expressions:

In[16]:= |

Out[16]= |

Compare the fits to the data:

In[17]:= |

Out[17]= |

The Weights option can be specified in a number of ways. First generate datasets with an unequal number of points and offset them slightly:

In[18]:= |

Out[19]= |

Fit the data normally. In this case, each individual data point has equal weights in the fit, so the first dataset gets more weight overall since it has more points:

In[20]:= |

Out[21]= |

In[22]:= |

Out[22]= |

Assign more weight to the second dataset:

In[23]:= |

Out[24]= |

In[25]:= |

Out[25]= |

Assign weights inversely proportional to the number of points in the dataset. This asserts that each dataset is equally important:

In[26]:= |

Out[27]= |

In[28]:= |

Out[28]= |

To assign weights for each individual data point, you can pass a list of vectors matching the input data:

In[29]:= |

Out[30]= |

In[31]:= |

Out[31]= |

Use a different symbol to index the datasets:

In[32]:= |

Out[33]= |

In[34]:= |

Out[34]= |

- 7.1.0 – 14 October 2022
- 7.0.0 – 06 October 2020
- 6.0.0 – 24 July 2020
- 5.0.0 – 05 December 2019
- 4.0.0 – 22 May 2019
- 3.0.0 – 19 April 2019
- 2.0.0 – 01 April 2019
- 1.0.0 – 14 March 2019

This work is licensed under a Creative Commons Attribution 4.0 International License