# 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[1]= |

In[2]:= |

Out[2]= |

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 (unlike Abs[*a*_{1}–*a*_{2}]):

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

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:

In[5]:= |

The fit function does not evaluate symbolically, only numerically:

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

Use this function to fit with:

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

Fit two Gaussian peaks with a shared location parameter:

In[10]:= |

Out[10]= |

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

In[11]:= |

Out[11]= |

In[12]:= |

Out[12]= |

Extract the fits as a list of expressions:

In[13]:= |

Out[13]= |

Compare the fits to the data:

In[14]:= |

Out[14]= |

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[15]:= |

Out[15]= |

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[16]:= |

Out[16]= |

In[17]:= |

Out[17]= |

Assign more weight to the second dataset:

In[18]:= |

Out[18]= |

In[19]:= |

Out[19]= |

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

In[20]:= |

Out[20]= |

In[21]:= |

Out[21]= |

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

In[22]:= |

Out[22]= |

In[23]:= |

Out[23]= |

Use a different symbol to index the datasets:

In[24]:= |

Out[24]= |

In[25]:= |

Out[25]= |

- 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