| 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 | $x = sequence(20)-10; | ||||||||||||||||||||||||||||
| 13 | $coeff_orig = cdouble(30,-2,3,-2); # order used in this module | ||||||||||||||||||||||||||||
| 14 | $y = 30-2*$x+3*$x**2-2*$x**3; # or: $y = polyval($coeff_orig->slice("-1:0"), $x->r2C); | ||||||||||||||||||||||||||||
| 15 | $y += ($x->grandom - 0.5) * 100; | ||||||||||||||||||||||||||||
| 16 | ($yfit, $coeff) = fitpoly1d($x,$y,4); | ||||||||||||||||||||||||||||
| 17 | use PDL::Graphics::Simple; | ||||||||||||||||||||||||||||
| 18 | $w = pgswin(); | ||||||||||||||||||||||||||||
| 19 | $xi = zeroes(100)->xlinvals(-10,9); | ||||||||||||||||||||||||||||
| 20 | $yi = polyval($coeff->r2C->slice("-1:0"), $xi->r2C); | ||||||||||||||||||||||||||||
| 21 | $w->plot(with=>'points',$x,$y, | ||||||||||||||||||||||||||||
| 22 | with=>'points',$x,$yfit, with=>'line',$xi,$yi); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | $yfit = fitpoly1d $data,2; # Least-squares line fit | ||||||||||||||||||||||||||||
| 25 | ($yfit, $coeffs) = fitpoly1d $x, $y, 4; # Fit a cubic | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head2 fitpoly1d | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =for ref | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Fit 1D polynomials to data using min chi^2 (least squares) | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =for usage | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | Usage: ($yfit, [$coeffs]) = fitpoly1d [$xdata], $data, $order, [Options...] | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =for sig | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | Signature: (x(n); y(n); [o]yfit(n); [o]coeffs(order)) | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Uses a standard matrix inversion method to do a least | ||||||||||||||||||||||||||||
| 44 | squares/min chi^2 polynomial fit to data. Order=2 is a linear | ||||||||||||||||||||||||||||
| 45 | fit (two parameters). | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Returns the fitted data and optionally the coefficients (in ascending | ||||||||||||||||||||||||||||
| 48 | order of degree, unlike L<PDL::Math/polyval>). | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | One can broadcast over extra dimensions to do multiple fits (except | ||||||||||||||||||||||||||||
| 51 | the order can not be broadcasted over - i.e. it must be one fixed | ||||||||||||||||||||||||||||
| 52 | scalar number like "4"). | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | The data is normalised internally to avoid overflows (using the | ||||||||||||||||||||||||||||
| 55 | mean of the abs value) which are common in large polynomial | ||||||||||||||||||||||||||||
| 56 | series but the returned fit, coeffs are in | ||||||||||||||||||||||||||||
| 57 | unnormalised units. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =for example | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | $yfit = fitpoly1d $data,2; # Least-squares line fit | ||||||||||||||||||||||||||||
| 62 | ($yfit, $coeffs) = fitpoly1d $x, $y, 4; # Fit a cubic | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | $fitimage = fitpoly1d $image,3 # Fit a quadratic to each row of an image | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | $myfit = fitpoly1d $line, 2, {Weights => $w}; # Weighted fit | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =for options | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Options: | ||||||||||||||||||||||||||||
| 71 | Weights Weights to use in fit, e.g. 1/$sigma**2 (default=1) | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | package PDL::Fit::Polynomial; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | use PDL::Core; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | use PDL::Basic; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 1 | ▶ | use PDL::Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | use PDL::Options ':Func'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 1 | ▶ | use PDL::MatrixOps; # for inv(), using this instead of call to Slatec routine | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | our @EXPORT_OK = qw( fitpoly1d ); | |||||||||||||||||||||||||||
| 86 | 1 | our %EXPORT_TAGS = (Func=>\@EXPORT_OK); | |||||||||||||||||||||||||||
| 87 | 1 | our @ISA = qw( PDL::Exporter ); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | sub PDL::fitpoly1d { | ||||||||||||||||||||||||||
|
Subroutine
fitpoly1d: called CC=8 SCAR=22.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 1 | ▶ | my $opthash = ref($_[-1]) eq "HASH" ? pop(@_) : {} ; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | my %opt = parse( { Weights=>ones(1) }, $opthash ) ; | |||||||||||||||||||||||||||
| 92 | 1 | ▶ | barf "Usage: fitpoly1d [\$x,] \$y, \$order\n" if @_<2 or @_ > 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ < 2 or @_ > 3
MC/DC0%0/2
@_ < 2 or @_ > 3
@_ < 2
@_ > 3
Truth table33%1/3
A or B
A= @_ < 2B= @_ > 3
| |||||||||||||||||||||||||||||
| 93 | 1 | my ($x, $y, $order) = @_; | |||||||||||||||||||||||||||
| 94 | 1 | ▶ | if ($#_ == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ($y, $order) = @_; | |||||||||||||||||||||||||||
| 96 | 0 | $x = $y->xvals; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | my $wt = $opt{Weights}; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Internally normalise data | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | # means for each 1D data set | ||||||||||||||||||||||||||||
| 104 | 1 | my $xmean = (abs($x)->average)->dummy(0); # dummy for correct broadcasting | |||||||||||||||||||||||||||
| 105 | 1 | my $ymean = (abs($y)->average)->dummy(0); | |||||||||||||||||||||||||||
| 106 | 1 | ▶ | (my $tmp = $ymean->where($ymean == 0)) .= 1 if any $ymean == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | ($tmp = $xmean->where($xmean == 0)) .= 1 if any $xmean == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1 | my $y2 = $y / $ymean; | |||||||||||||||||||||||||||
| 109 | 1 | my $x2 = $x / $xmean; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Do the fit | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 1 | my $pow = sequence($order); | |||||||||||||||||||||||||||
| 114 | 1 | my $M = $x2->dummy(0) ** $pow; | |||||||||||||||||||||||||||
| 115 | 1 | my $C = $M->transpose x ($M * $wt->dummy(0)) ; | |||||||||||||||||||||||||||
| 116 | 1 | my $Y = $M->transpose x ($y2->dummy(0) * $wt->dummy(0)); | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # Fitted coefficients vector | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | ## $a1 = matinv($C) x $Y; | ||||||||||||||||||||||||||||
| 121 | ## print "matinv: \$C = $C, \$Y = $Y, \$a1 = $a1\n"; | ||||||||||||||||||||||||||||
| 122 | 1 | my $a1 = inv($C) x $Y; # use inv() instead of matinv() to avoid Slatec dependency | |||||||||||||||||||||||||||
| 123 | ## print "inv: \$C = $C, \$Y = $Y, \$a1 = $a1\n"; | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # Fitted data | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 1 | my $yfit = ($M x $a1)->clump(2) * $ymean; # Remove first dim=1, un-normalise | |||||||||||||||||||||||||||
| 128 | 1 | ▶ | return wantarray ? ($yfit, $a1->clump(2) * $ymean / ($xmean ** $pow)) : $yfit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | =cut | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 1 | 1; | |||||||||||||||||||||||||||