or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

builtin-models.mdconfidence.mdindex.mdminimization.mdmodels.mdparameters.mdreporting.md

builtin-models.mddocs/

0

# Built-in Models

1

2

LMFIT provides 34 built-in model classes and 30 mathematical lineshape functions covering common peak shapes, distributions, step functions, oscillators, and specialized physics functions. All built-in models include automatic parameter guessing and parameter hints for easy initialization.

3

4

## Capabilities

5

6

### Peak and Distribution Models

7

8

Models for common peak shapes and statistical distributions used in data analysis.

9

10

```python { .api }

11

# Gaussian and related models

12

class GaussianModel(Model):

13

"""Gaussian peak model: amplitude * exp(-(x-center)²/(2*sigma²))"""

14

def guess(self, data, x=None, **kws): ...

15

16

class Gaussian2dModel(Model):

17

"""2D Gaussian peak model for image/surface fitting"""

18

def guess(self, data, x=None, y=None, **kws): ...

19

20

class LorentzianModel(Model):

21

"""Lorentzian peak model: amplitude / (1 + ((x-center)/sigma)²)"""

22

def guess(self, data, x=None, **kws): ...

23

24

class SplitLorentzianModel(Model):

25

"""Asymmetric Lorentzian with different widths on each side"""

26

def guess(self, data, x=None, **kws): ...

27

28

class VoigtModel(Model):

29

"""Voigt profile (Gaussian ⊗ Lorentzian convolution)"""

30

def guess(self, data, x=None, **kws): ...

31

32

class PseudoVoigtModel(Model):

33

"""Pseudo-Voigt profile (weighted sum of Gaussian and Lorentzian)"""

34

def guess(self, data, x=None, **kws): ...

35

36

class MoffatModel(Model):

37

"""Moffat profile for astronomical point spread functions"""

38

def guess(self, data, x=None, **kws): ...

39

40

class Pearson4Model(Model):

41

"""Pearson Type IV distribution"""

42

def guess(self, data, x=None, **kws): ...

43

44

class Pearson7Model(Model):

45

"""Pearson Type VII distribution"""

46

def guess(self, data, x=None, **kws): ...

47

48

class StudentsTModel(Model):

49

"""Student's t-distribution"""

50

def guess(self, data, x=None, **kws): ...

51

52

class BreitWignerModel(Model):

53

"""Breit-Wigner (Fano) profile for resonances"""

54

def guess(self, data, x=None, **kws): ...

55

56

class LognormalModel(Model):

57

"""Log-normal distribution"""

58

def guess(self, data, x=None, **kws): ...

59

60

class SkewedGaussianModel(Model):

61

"""Skewed Gaussian distribution"""

62

def guess(self, data, x=None, **kws): ...

63

64

class SkewedVoigtModel(Model):

65

"""Skewed Voigt profile"""

66

def guess(self, data, x=None, **kws): ...

67

68

class DoniachModel(Model):

69

"""Doniach Sunjic lineshape for X-ray photoelectron spectroscopy"""

70

def guess(self, data, x=None, **kws): ...

71

72

class ExponentialGaussianModel(Model):

73

"""Exponentially modified Gaussian (EMG) for chromatography"""

74

def guess(self, data, x=None, **kws): ...

75

```

76

77

### Basic Mathematical Models

78

79

Simple mathematical functions for baseline fitting and basic functional forms.

80

81

```python { .api }

82

class ConstantModel(Model):

83

"""Constant value: c"""

84

def guess(self, data, x=None, **kws): ...

85

86

class ComplexConstantModel(Model):

87

"""Complex constant value"""

88

def guess(self, data, x=None, **kws): ...

89

90

class LinearModel(Model):

91

"""Linear function: slope * x + intercept"""

92

def guess(self, data, x=None, **kws): ...

93

94

class QuadraticModel(Model):

95

"""Quadratic function: a * x² + b * x + c"""

96

def guess(self, data, x=None, **kws): ...

97

98

class ParabolicModel(Model):

99

"""Alias for QuadraticModel"""

100

def guess(self, data, x=None, **kws): ...

101

102

class PolynomialModel(Model):

103

"""Arbitrary degree polynomial"""

104

def __init__(self, degree, **kws): ...

105

def guess(self, data, x=None, **kws): ...

106

107

class ExpressionModel(Model):

108

"""Model created from mathematical expression string"""

109

def __init__(self, expr, independent_vars=['x'], **kws): ...

110

```

111

112

### Exponential and Power Models

113

114

Models for exponential decay, growth, and power law relationships.

115

116

```python { .api }

117

class ExponentialModel(Model):

118

"""Exponential: amplitude * exp(decay * x)"""

119

def guess(self, data, x=None, **kws): ...

120

121

class PowerLawModel(Model):

122

"""Power law: amplitude * x^exponent"""

123

def guess(self, data, x=None, **kws): ...

124

```

125

126

### Step and Rectangle Functions

127

128

Models for step changes and rectangular pulses.

129

130

```python { .api }

131

class StepModel(Model):

132

"""Step function with various functional forms"""

133

def __init__(self, form='linear', **kws): ...

134

def guess(self, data, x=None, **kws): ...

135

136

class RectangleModel(Model):

137

"""Rectangle function between two points"""

138

def __init__(self, form='linear', **kws): ...

139

def guess(self, data, x=None, **kws): ...

140

```

141

142

### Oscillator Models

143

144

Models for oscillatory and wave-like data.

145

146

```python { .api }

147

class DampedOscillatorModel(Model):

148

"""Simple damped oscillator"""

149

def guess(self, data, x=None, **kws): ...

150

151

class DampedHarmonicOscillatorModel(Model):

152

"""Damped harmonic oscillator with resonance"""

153

def guess(self, data, x=None, **kws): ...

154

155

class SineModel(Model):

156

"""Sine wave: amplitude * sin(frequency * x + shift)"""

157

def guess(self, data, x=None, **kws): ...

158

```

159

160

### Physics Models

161

162

Specialized models for physics applications including thermal distributions.

163

164

```python { .api }

165

class ThermalDistributionModel(Model):

166

"""Generic thermal distribution"""

167

def __init__(self, form='bose', **kws): ...

168

def guess(self, data, x=None, **kws): ...

169

170

class BoseModel(Model):

171

"""Bose-Einstein distribution"""

172

def guess(self, data, x=None, **kws): ...

173

174

class FermiModel(Model):

175

"""Fermi-Dirac distribution"""

176

def guess(self, data, x=None, **kws): ...

177

```

178

179

### Advanced Models

180

181

Specialized models for complex fitting scenarios.

182

183

```python { .api }

184

class SplineModel(Model):

185

"""Spline interpolation model"""

186

def __init__(self, xknots, **kws): ...

187

def guess(self, data, x=None, **kws): ...

188

```

189

190

### Lineshape Functions

191

192

Direct mathematical functions (not model classes) for use in custom models.

193

194

```python { .api }

195

# Peak functions

196

def gaussian(x, amplitude=1.0, center=0.0, sigma=1.0): ...

197

def gaussian2d(x, y=0.0, amplitude=1.0, centerx=0.0, centery=0.0,

198

sigmax=1.0, sigmay=1.0): ...

199

def lorentzian(x, amplitude=1.0, center=0.0, sigma=1.0): ...

200

def split_lorentzian(x, amplitude=1.0, center=0.0, sigma=1.0, sigma_r=1.0): ...

201

def voigt(x, amplitude=1.0, center=0.0, sigma=1.0, gamma=None): ...

202

def pvoigt(x, amplitude=1.0, center=0.0, sigma=1.0, fraction=0.5): ...

203

def moffat(x, amplitude=1.0, center=0.0, sigma=1.0, beta=1.0): ...

204

def pearson4(x, amplitude=1.0, center=0.0, sigma=1.0, expon=1.0, skew=0.0): ...

205

def pearson7(x, amplitude=1.0, center=0.0, sigma=1.0, expon=1.0): ...

206

def breit_wigner(x, amplitude=1.0, center=0.0, sigma=1.0, q=1.0): ...

207

def damped_oscillator(x, amplitude=1.0, center=1.0, sigma=0.1): ...

208

def dho(x, amplitude=1.0, center=1.0, sigma=1.0, gamma=1.0): ...

209

210

# Distribution functions

211

def lognormal(x, amplitude=1.0, center=0.0, sigma=1.0): ...

212

def students_t(x, amplitude=1.0, center=0.0, sigma=1.0): ...

213

def expgaussian(x, amplitude=1.0, center=0.0, sigma=1.0, gamma=1.0): ...

214

def skewed_gaussian(x, amplitude=1.0, center=0.0, sigma=1.0, gamma=0.0): ...

215

def skewed_voigt(x, amplitude=1.0, center=0.0, sigma=1.0, gamma=None, skew=0.0): ...

216

def doniach(x, amplitude=1.0, center=0.0, sigma=1.0, gamma=0.0): ...

217

def logistic(x, amplitude=1.0, center=0.0, sigma=1.0): ...

218

219

# Physics functions

220

def bose(x, amplitude=1.0, center=0.0, kt=1.0): ...

221

def fermi(x, amplitude=1.0, center=0.0, kt=1.0): ...

222

def thermal_distribution(x, amplitude=1.0, center=0.0, kt=1.0, form='bose'): ...

223

224

# Step and basic functions

225

def step(x, amplitude=1.0, center=0.0, sigma=1.0, form='linear'): ...

226

def rectangle(x, amplitude=1.0, center1=0.0, sigma1=1.0,

227

center2=1.0, sigma2=1.0, form='linear'): ...

228

def exponential(x, amplitude=1.0, decay=1.0): ...

229

def powerlaw(x, amplitude=1.0, exponent=1.0): ...

230

def linear(x, slope=1.0, intercept=0.0): ...

231

def parabolic(x, a=0.0, b=0.0, c=0.0): ...

232

def sine(x, amplitude=1.0, frequency=1.0, shift=0.0): ...

233

def expsine(x, amplitude=1.0, frequency=1.0, shift=0.0, decay=0.0): ...

234

235

# Utility functions

236

def not_zero(value): ... # Ensure non-zero value to prevent division errors

237

```

238

239

## Usage Examples

240

241

### Using Built-in Models

242

243

```python

244

import numpy as np

245

from lmfit.models import GaussianModel, LinearModel, ExponentialModel

246

247

# Generate sample data with Gaussian peak on linear background

248

x = np.linspace(0, 20, 201)

249

y = (5 * np.exp(-((x-10)/2)**2) + # Gaussian peak

250

0.2 * x + 1 + # Linear background

251

np.random.normal(size=201, scale=0.1)) # Noise

252

253

# Create models

254

gaussian = GaussianModel(prefix='peak_')

255

linear = LinearModel(prefix='bg_')

256

257

# Combine models

258

model = gaussian + linear

259

260

# Let models guess initial parameters

261

params = gaussian.guess(y, x=x)

262

params.update(linear.guess(y, x=x))

263

264

# Fit the composite model

265

result = model.fit(y, params, x=x)

266

print(result.fit_report())

267

```

268

269

### Custom Model from Expression

270

271

```python

272

from lmfit.models import ExpressionModel

273

274

# Create model from mathematical expression

275

expr = 'amplitude * exp(-((x - center) / width)**2) * (1 + skew * (x - center))'

276

model = ExpressionModel(expr)

277

278

# Set parameter hints

279

model.set_param_hint('amplitude', value=1, min=0)

280

model.set_param_hint('center', value=0)

281

model.set_param_hint('width', value=1, min=0.01)

282

model.set_param_hint('skew', value=0)

283

284

# Create parameters and fit

285

params = model.make_params()

286

result = model.fit(data, params, x=x)

287

```

288

289

### Multiple Peak Fitting

290

291

```python

292

from lmfit.models import GaussianModel, ConstantModel

293

294

# Create multiple Gaussian peaks with shared background

295

peak1 = GaussianModel(prefix='p1_')

296

peak2 = GaussianModel(prefix='p2_')

297

peak3 = GaussianModel(prefix='p3_')

298

background = ConstantModel(prefix='bg_')

299

300

# Combine all models

301

model = peak1 + peak2 + peak3 + background

302

303

# Set up parameters for each peak

304

params = model.make_params()

305

306

# Peak 1 parameters

307

params['p1_amplitude'].set(value=10, min=0)

308

params['p1_center'].set(value=5)

309

params['p1_sigma'].set(value=1, min=0.1)

310

311

# Peak 2 parameters

312

params['p2_amplitude'].set(value=15, min=0)

313

params['p2_center'].set(value=10)

314

params['p2_sigma'].set(value=1.5, min=0.1)

315

316

# Peak 3 parameters

317

params['p3_amplitude'].set(value=8, min=0)

318

params['p3_center'].set(value=15)

319

params['p3_sigma'].set(value=0.8, min=0.1)

320

321

# Background

322

params['bg_c'].set(value=1)

323

324

# Fit the model

325

result = model.fit(data, params, x=x)

326

327

# Evaluate individual components

328

components = result.eval_components(x=x)

329

peak1_fit = components['p1_']

330

peak2_fit = components['p2_']

331

peak3_fit = components['p3_']

332

background_fit = components['bg_']

333

```

334

335

### Using Lineshape Functions Directly

336

337

```python

338

from lmfit import minimize, Parameters

339

import lmfit.lineshapes as ls

340

341

def multi_voigt_residual(params, x, data):

342

"""Residual function using lineshape functions directly"""

343

model = (ls.voigt(x, params['v1_amp'], params['v1_cen'],

344

params['v1_sig'], params['v1_gam']) +

345

ls.voigt(x, params['v2_amp'], params['v2_cen'],

346

params['v2_sig'], params['v2_gam']))

347

return model - data

348

349

# Set up parameters

350

params = Parameters()

351

params.add('v1_amp', value=10, min=0)

352

params.add('v1_cen', value=5)

353

params.add('v1_sig', value=1, min=0.1)

354

params.add('v1_gam', value=1, min=0.1)

355

params.add('v2_amp', value=8, min=0)

356

params.add('v2_cen', value=10)

357

params.add('v2_sig', value=1.2, min=0.1)

358

params.add('v2_gam', value=0.8, min=0.1)

359

360

# Fit using minimize

361

result = minimize(multi_voigt_residual, params, args=(x, data))

362

```

363

364

### Polynomial Models

365

366

```python

367

from lmfit.models import PolynomialModel

368

369

# Create polynomial model of specified degree

370

poly_model = PolynomialModel(degree=3)

371

372

# Parameters are automatically named c0, c1, c2, c3 for coefficients

373

params = poly_model.guess(data, x=x)

374

375

# Or set manually

376

params = poly_model.make_params()

377

params['c0'].set(value=1) # constant term

378

params['c1'].set(value=0) # linear term

379

params['c2'].set(value=0) # quadratic term

380

params['c3'].set(value=0) # cubic term

381

382

result = poly_model.fit(data, params, x=x)

383

```