| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | PDL::Fit::Polynomial - routines for fitting with polynomials | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | This module contains routines for doing simple | ||||||||||||||||||||||||||||
| 8 | polynomial fits to data | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | $yfit = fitpoly1d $data; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head2 fitpoly1d | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =for ref | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Fit 1D polynomials to data using min chi^2 (least squares) | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =for usage | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | Usage: ($yfit, [$coeffs]) = fitpoly1d [$xdata], $data, $order, [Options...] | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =for sig | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Signature: (x(n); y(n); [o]yfit(n); [o]coeffs(order)) | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | Uses a standard matrix inversion method to do a least | ||||||||||||||||||||||||||||
| 32 | squares/min chi^2 polynomial fit to data. Order=2 is a linear | ||||||||||||||||||||||||||||
| 33 | fit (two parameters). | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | Returns the fitted data and optionally the coefficients. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | One can thread over extra dimensions to do multiple fits (except | ||||||||||||||||||||||||||||
| 38 | the order can not be threaded over - i.e. it must be one fixed | ||||||||||||||||||||||||||||
| 39 | scalar number like "4"). | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | The data is normalised internally to avoid overflows (using the | ||||||||||||||||||||||||||||
| 42 | mean of the abs value) which are common in large polynomial | ||||||||||||||||||||||||||||
| 43 | series but the returned fit, coeffs are in | ||||||||||||||||||||||||||||
| 44 | unnormalised units. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =for example | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | $yfit = fitpoly1d $data,2; # Least-squares line fit | ||||||||||||||||||||||||||||
| 50 | ($yfit, $coeffs) = fitpoly1d $x, $y, 4; # Fit a cubic | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | $fitimage = fitpoly1d $image,3 # Fit a quadratic to each row of an image | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | $myfit = fitpoly1d $line, 2, {Weights => $w}; # Weighted fit | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =for options | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Options: | ||||||||||||||||||||||||||||
| 59 | Weights Weights to use in fit, e.g. 1/$sigma**2 (default=1) | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | package PDL::Fit::Polynomial; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 1 | @EXPORT_OK = qw( fitpoly1d ); | |||||||||||||||||||||||||||
| 67 | 1 | %EXPORT_TAGS = (Func=>[@EXPORT_OK]); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | ▶ | use PDL::Core; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | use PDL::Basic; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 1 | ▶ | use PDL::Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 1 | @ISA = qw( PDL::Exporter ); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | ▶ | use PDL::Options ':Func'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | # use PDL::Slatec; # For matinv() | ||||||||||||||||||||||||||||
| 76 | 1 | ▶ | use PDL::MatrixOps; # for inv(), using this instead of call to Slatec routine | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub PDL::fitpoly1d { | ||||||||||||||||||||||||||||
| 80 | 1 | ▶ | my $opthash = ref($_[-1]) eq "HASH" ? pop(@_) : {} ; | ||||||||||||||||||||||||||
|
Subroutine
fitpoly1d: called CC=8 SCAR=23.1
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | my %opt = parse( { Weights=>ones(1) }, $opthash ) ; | |||||||||||||||||||||||||||
| 82 | 1 | ▶ | barf "Usage: fitpoly1d incorrect args\n" if $#_<1 or $#_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$#_ < 1 or $#_ > 2
MC/DC0%0/2
$#_ < 1 or $#_ > 2
$#_ < 1
$#_ > 2
Truth table33%1/3
A or B
A= $#_ < 1B= $#_ > 2
| |||||||||||||||||||||||||||||
| 83 | 1 | my ($x, $y, $order) = @_; | |||||||||||||||||||||||||||
| 84 | 1 | ▶ | if ($#_ == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | ($y, $order) = @_; | |||||||||||||||||||||||||||
| 86 | 0 | $x = $y->xvals; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | my $wt = $opt{Weights}; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Internally normalise data | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # means for each 1D data set | ||||||||||||||||||||||||||||
| 94 | 1 | my $xmean = (abs($x)->average)->dummy(0); # dummy for correct threading | |||||||||||||||||||||||||||
| 95 | 1 | my $ymean = (abs($y)->average)->dummy(0); | |||||||||||||||||||||||||||
| 96 | 1 | ▶ | (my $tmp = $ymean->where($ymean == 0)) .= 1 if any $ymean == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | ($tmp = $xmean->where($xmean == 0)) .= 1 if any $xmean == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 1 | my $y2 = $y / $ymean; | |||||||||||||||||||||||||||
| 99 | 1 | my $x2 = $x / $xmean; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Do the fit | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | my $pow = sequence($order); | |||||||||||||||||||||||||||
| 104 | 1 | my $M = $x2->dummy(0) ** $pow; | |||||||||||||||||||||||||||
| 105 | 1 | my $C = $M->xchg(0,1) x ($M * $wt->dummy(0)) ; | |||||||||||||||||||||||||||
| 106 | 1 | my $Y = $M->xchg(0,1) x ($y2->dummy(0) * $wt->dummy(0)); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Fitted coefficients vector | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | ## $a = matinv($C) x $Y; | ||||||||||||||||||||||||||||
| 111 | ## print "matinv: \$C = $C, \$Y = $Y, \$a = $a\n"; | ||||||||||||||||||||||||||||
| 112 | 1 | $a = inv($C) x $Y; # use inv() instead of matinv() to avoid Slatec dependency | |||||||||||||||||||||||||||
| 113 | ## print "inv: \$C = $C, \$Y = $Y, \$a = $a\n"; | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Fitted data | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | $yfit = ($M x $a)->clump(2); # Remove first dim=1 | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | $yfit *= $ymean; # Un-normalise | |||||||||||||||||||||||||||
| 120 | 1 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | my $coeff = $a->clump(2); | |||||||||||||||||||||||||||
| 122 | 0 | $coeff *= $ymean / ($xmean ** $pow); # Un-normalise | |||||||||||||||||||||||||||
| 123 | 0 | return ($yfit, $coeff); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | else{ | ||||||||||||||||||||||||||||
| 126 | 1 | return $yfit; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 1 | *fitpoly1d = \&PDL::fitpoly1d; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 BUGS | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | May not work too well for data with large dynamic range. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | L<PDL::Slatec/"polyfit"> | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | This file copyright (C) 1999, Karl Glazebrook (kgb@aaoepp.aao.gov.au). | ||||||||||||||||||||||||||||
| 143 | All rights reserved. There | ||||||||||||||||||||||||||||
| 144 | is no warranty. You are allowed to redistribute this software | ||||||||||||||||||||||||||||
| 145 | documentation under certain conditions. For details, see the file | ||||||||||||||||||||||||||||
| 146 | COPYING in the PDL distribution. If this file is separated from the | ||||||||||||||||||||||||||||
| 147 | PDL distribution, the copyright notice should be included in the file. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1 | 1; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||