| 1 | use PDL; |
| 2 | use PDL::Math; |
| 3 | use PDL::Fit::LM; |
| 4 | use strict; |
| 5 | |
| 6 | |
| 7 | ### fit using pdl's lmfit (Marquardt-Levenberg non-linear least squares fitting) |
| 8 | ### |
| 9 | ### `lmfit' Syntax: |
| 10 | ### |
| 11 | ### ($ym,$finalp,$covar,$iters) |
| 12 | ### = lmfit $x, $y, $sigma, \&fn, $initp, {Maxiter => 300, Eps => 1e-3}; |
| 13 | ### |
| 14 | ### Explanation of variables |
| 15 | ### |
| 16 | ### OUTPUT |
| 17 | ### $ym = pdl of fitted values |
| 18 | ### $finalp = pdl of parameters |
| 19 | ### $covar = covariance matrix |
| 20 | ### $iters = number of iterations actually used |
| 21 | ### |
| 22 | ### INPUT |
| 23 | ### $x = x data |
| 24 | ### $y = y data |
| 25 | ### $sigma = ndarray of y-uncertainties for each value of $y (can be set to scalar 1 for equal weighting) |
| 26 | ### \&fn = reference to function provided by user (more on this below) |
| 27 | ### $initp = initial values for floating parameters |
| 28 | ### (needs to be explicitly set prior to use of lmfit) |
| 29 | ### Maxiter = maximum iterations |
| 30 | ### Eps = convergence criterion (maximum normalized change in Chi Sq.) |
| 31 | |
| 32 | ### Example: |
| 33 | # make up experimental data: |
| 34 | my $xdata = pdl sequence 5; |
| 35 | my $ydata = pdl [1.1,1.9,3.05,4,4.9]; |
| 36 | |
| 37 | # set initial prameters in a pdl (order in accord with fit function below) |
| 38 | my $initp = pdl [0,1]; |
| 39 | |
| 40 | # Weight all y data equally (else specify different uncertainties in a pdl) |
| 41 | my $sigma = 1; |
| 42 | |
| 43 | # Use lmfit. Fourth input argument is reference to user-defined |
| 44 | # subroutine ( here \&linefit ) detailed below. |
| 45 | my ($yf,$pf,$cf,$if) = lmfit $xdata, $ydata, $sigma, \&linefit, $initp; |
| 46 | |
| 47 | # Note output |
| 48 | print "\nXDATA\n$xdata\nY DATA\n$ydata\n\nY DATA FIT\n$yf\n\n"; |
| 49 | print "Slope and Intercept\n$pf\n\nCOVARIANCE MATRIX\n$cf\n\n"; |
| 50 | print "NUMBER ITERATIONS\n$if\n\n"; |
| 51 | |
| 52 | |
| 53 | # simple example of user defined fit function. Guidelines included on |
| 54 | # how to write your own function subroutine. |
| 55 | sub linefit { |
| 56 | |
| 57 | # leave this line as is |
| 58 | my ($x,$par,$ym,$dyda) = @_; |
| 59 | |
| 60 | # $m and $c are fit parameters, internal to this function |
| 61 | # call them whatever make sense to you, but replace (0..1) |
| 62 | # with (0..x) where x is equal to your number of fit parameters |
| 63 | # minus 1 |
| 64 | my ($m,$c) = map { $par->slice("($_)") } (0..1); |
| 65 | |
| 66 | # Write function with dependent variable $ym, |
| 67 | # independent variable $x, and fit parameters as specified above. |
| 68 | # Use the .= (dot equals) assignment operator to express the equality |
| 69 | # (not just a plain equals) |
| 70 | $ym .= $m * $x + $c; |
| 71 | |
| 72 | # Edit only the (0..1) part to (0..x) as above |
| 73 | my (@dy) = map {$dyda -> slice(",($_)") } (0..1); |
| 74 | |
| 75 | # Partial derivative of the function with respect to first |
| 76 | # fit parameter ($m in this case). Again, note .= assignment |
| 77 | # operator (not just "equals") |
| 78 | $dy[0] .= $x; |
| 79 | |
| 80 | # Partial derivative of the function with respect to next |
| 81 | # fit parameter ($c in this case) |
| 82 | $dy[1] .= 1; |
| 83 | |
| 84 | # Add $dy[ ] .= () lines as necessary to supply |
| 85 | # partial derivatives for all floating parameters. |
| 86 | } |
| 87 |