LeastTrimmedSquares - Maple Help

Statistics

 LeastTrimmedSquares
 compute least trimmed squares regression

 Calling Sequence LeastTrimmedSquares(X, Y, v) LeastTrimmedSquares(XY, v) LeastTrimmedSquares(X, Y, v, opts) LeastTrimmedSquares(XY, v, opts)

Parameters

 X - values of independent variable(s) Y - values of dependent variable XY - values of independent and dependent variables v - algebraic expression in which to express the result, or list or Vector of values of the independent variable(s) at which to evaluate the computed estimator opts - (optional) one or more equations of the form include = h, intercept = tf, or subsets = s

Description

 • The LeastTrimmedSquares function computes a robust linear estimator from a collection of input data. It is a quantity first described by Rousseeuw [1], and is computed using the FAST-LTS algorithm by Rousseeuw and Van Driessen [2]. The article [1] that introduced least trimmed squares regression also introduced least median of squares regression, but least trimmed squares regression was later considered the better choice by many, including the original author - see e.g. [2].
 • Least trimmed squares regression yields a robust estimator. This means that it will continue to perform well if some points are replaced by outliers. Least-squares regression, the type of regression most commonly used and implemented by LinearFit and NonlinearFit, is very susceptible to outliers.
 • Conceptually, one performs least trimmed squares regression by selecting an arbitrary subset of h of the input data points, where h is an input parameter (which can be set using the include option). One then performs regular least-squares regression on these h data points, and records the sum of squared residuals. This sum of squared residuals is now minimized over all subsets of h data points.
 The FAST-LTS algorithm implemented in this command is an approximation algorithm: it will in all reasonable cases return the optimal regression model, but this is not guaranteed. An exact algorithm that does guarantee the optimal result can be selected with the subsets = all option, but this algorithm will take a number of steps that grows very quickly with the size of the problem.
 • Least trimmed squares regression with parameter h on n input points has a breakdown point of h / n. This means that when fewer than n - h of the data points in a sample are changed, the result of least trimmed squares regression can only change a limited amount. See the discussion of the include option below for limits on the values h can assume.
 • In the first and third calling sequences, the parameter X is a Matrix containing the values of the independent variables; each row corresponds to one data point and each column corresponds to one independent variable. Alternatively, you can specify X as a list of lists. If there is only a single independent variable, you can also specify X as a column Vector or a list.
 In those same calling sequences, Y is a Vector containing the values of the dependent variable, in the same order as the data points occur in X. Alternatively, you can specify Y as a list, or a Matrix with a single column.
 • In the second and fourth calling sequences, XY is a Matrix containing both the dependent and independent data, with the dependent data being in the right-most column.
 • The returned value is an expression representing the estimator evaluated at the value v. By supplying a variable name here, you obtain the general expression for the estimator in terms of variables v[1], v[2], etc., corresponding to the independent variables. In particular, if there is a single independent variable, the result will be in terms of v[1]. If instead you supply a list (or Vector) of values, corresponding to the values of the independent variables, you obtain the value of the estimator at those values.

Options

 • include = h
 This option determines how many data points are included (and consequently, how many are ignored). The value h should either be an integer greater than 1, in which case it is directly used as the number of data points to include, or a number that is less than or equal to one, in which case it is multiplied by n (the number of data points) and rounded to obtain the number of data points to include.
 In all cases, the resulting number of data points to include (which will be denoted by h in the rest of this discussion) needs to be at least equal to (n + p + 1)/2, where n is the number of data points and p is the number of independent variables, including (if present) the intercept. The default value for h is this minimum number (rounded up to an integer).

If you know that the number of outliers in your data is less than some number m, and n - m > (n + p + 1)/2, then you can supply the option include = n - m to get better statistical efficiency.

 • intercept = tf
 This option determines if a column for the intercept (a constant term in the resulting regression model) should be added to X. The default value is true; you can supply the option intercept = false to turn this off. The intercept is internally handled by adding a column filled with the value 1 to the right of X.
 Using this option is necessary if there is already an independent variable assuming only one constant value, or if the all-one vector is otherwise already in the column space of X: the matrix used internally needs to have full column rank, so it will not work if the all-one vector is joined to a Matrix that already contains that vector in its column space.
 • subsets = s
 This option can be used by experts to control the details of how the approximation process used for the FAST-LTS algorithm works. The discussion of this option assumes knowledge of the Rousseeuw and Van Driessen paper [2] that describes the algorithm.
 If p = 1 (after including the intercept), this command uses a fast exact algorithm and ignores the subsets option.
 By submitting the subsets = all option, the user can select a variant that tries all p-subsets of the data points. It runs two C-steps on all regression lines generated this way, selects the 10 best results, and runs C-steps on these results until convergence. This should in all cases return the optimal result, but the number of regression lines to process grows very quickly as a function of n and p. This algorithm is selected by default if binomial(n, p) < 1500.
 Otherwise, the algorithm runs in a number of rounds in which candidate regression lines are generated and subjected to C-steps; the details of these depend by default on n but can be fine-tuned by submitting an option of the form subsets = s, where s is a list of lists. Each sublist corresponds to one round of the algorithm, and contains a number of options specified as equations, listed below.
 – subsets = c and size = s
 These options specify that the data points should be subdivided into c disjoint subsets of size s each, all treated independently. Any points not included are ignored for the time being. The other options then apply to each subset; for example, the generate option specifies how many candidates are generated for each subset. The default values are c = 1 and s equal to the total number of data points. These options can only be specified for the first round.
 – generate = g
 This specifies how to obtain the candidates at the start of the round:
 • If g is a positive integer, Maple will randomly create that many candidates. This can only be specified for the first round.
 • If g is the value merge, then the data points selected in all subsets in previous rounds are merged, and the candidates found are merged as well.
 • If g is the value all, then all original data points are used, including those potentially ignored due to subsets and size options in the first round. The candidates found are merged as well.
 – steps = t
 This specifies the number of C-steps done on each candidate in this round. The default is 2.
 – select = k
 This specifies that the top k candidates go on to the next round. This is the only required option: there is no default, because this option needs to be included for every round (if the subsets option is used at all).

Notes

 • The underlying computation is done in floating-point; therefore, all data points must have type realcons and all returned solutions are floating-point, even if the problem is specified with exact values.  For more information about numeric computation in the Statistics package, see the Statistics/Computation help page.
 • Another method for robust regression is implemented in the RepeatedMedianEstimator procedure.

Examples

 > $\mathrm{with}\left(\mathrm{Statistics}\right):$

In this example, we have 1000 data points. There is a single independent variable, $x$, with values uniformly distributed between 0 and 10. The dependent variable is a linear function of the independent variable plus some additive noise, $y=5x+10+\mathrm{noise}$, where the noise is from a probability distribution known to have severe outliers - the Cauchy distribution, with location parameter 0 and scale parameter 5.

 > $x≔\mathrm{Sample}\left(\mathrm{Uniform}\left(0,10\right),1000\right)$
 ${x}{≔}\left[{8.14723686393179}{,}{9.05791937075619}{,}{1.26986816293506}{,}{9.13375856139019}{,}{6.32359246225410}{,}{0.975404049994095}{,}{2.78498218867048}{,}{5.46881519204984}{,}{9.57506835434298}{,}{9.64888535199277}{,}{1.57613081677548}{,}{9.70592781760616}{,}{9.57166948242946}{,}{4.85375648722841}{,}{8.00280468888800}{,}{1.41886338627215}{,}{4.21761282626275}{,}{9.15735525189067}{,}{7.92207329559554}{,}{9.59492426392903}{,}{6.55740699156587}{,}{0.357116785741896}{,}{8.49129305868777}{,}{9.33993247757551}{,}{6.78735154857773}{,}{7.57740130578333}{,}{7.43132468124916}{,}{3.92227019534168}{,}{6.55477890177557}{,}{1.71186687811562}{,}{7.06046088019609}{,}{0.318328463774207}{,}{2.76922984960890}{,}{0.461713906311539}{,}{0.971317812358475}{,}{8.23457828327293}{,}{6.94828622975817}{,}{3.17099480060861}{,}{9.50222048838355}{,}{0.344460805029088}{,}{4.38744359656398}{,}{3.81558457093008}{,}{7.65516788149002}{,}{7.95199901137063}{,}{1.86872604554379}{,}{4.89764395788231}{,}{4.45586200710899}{,}{6.46313010111265}{,}{7.09364830858073}{,}{7.54686681982361}{,}{2.76025076998578}{,}{6.79702676853675}{,}{6.55098003973841}{,}{1.62611735194631}{,}{1.18997681558377}{,}{4.98364051982143}{,}{9.59743958516081}{,}{3.40385726666133}{,}{5.85267750979777}{,}{2.23811939491137}{,}{7.51267059305653}{,}{2.55095115459269}{,}{5.05957051665142}{,}{6.99076722656686}{,}{8.90903252535798}{,}{9.59291425205444}{,}{5.47215529963803}{,}{1.38624442828679}{,}{1.49294005559057}{,}{2.57508254123736}{,}{8.40717255983663}{,}{2.54282178971531}{,}{8.14284826068816}{,}{2.43524968724989}{,}{9.29263623187228}{,}{3.49983765984809}{,}{1.96595250431208}{,}{2.51083857976031}{,}{6.16044676146639}{,}{4.73288848902729}{,}{3.51659507062997}{,}{8.30828627896291}{,}{5.85264091152724}{,}{5.49723608291140}{,}{9.17193663829810}{,}{2.85839018820374}{,}{7.57200229110721}{,}{7.53729094278495}{,}{3.80445846975357}{,}{5.67821640725221}{,}{0.758542895630636}{,}{0.539501186666071}{,}{5.30797553008973}{,}{7.79167230102011}{,}{9.34010684229183}{,}{1.29906208473730}{,}{5.68823660872193}{,}{4.69390641058206}{,}{0.119020695012414}{,}{3.37122644398882}{,}{\dots }{,}{\text{⋯ 900 row vector entries not shown}}\right]$ (1)
 > $\mathrm{noise}≔\mathrm{Sample}\left(\mathrm{Cauchy}\left(0,1\right),1000\right)$
 ${\mathrm{noise}}{≔}\left[{0.438876285244151}{,}{-0.491463594489803}{,}{170.590828271958}{,}{-1.18100919831304}{,}{0.521204985903946}{,}{0.343727943517054}{,}{-0.371306654331434}{,}{-2.09877907095432}{,}{-13.2174786304885}{,}{-0.254093174017617}{,}{-1.53770237182407}{,}{0.861162139439657}{,}{-0.433064991536248}{,}{1.84873765772896}{,}{0.908514196726544}{,}{0.227869742546098}{,}{-1.61720210426961}{,}{7.61322805250931}{,}{-0.911088181650396}{,}{4.19371146303173}{,}{-1.18403244659440}{,}{-0.421150489957570}{,}{-3.58257194142929}{,}{0.472811744413310}{,}{-1.57520950144133}{,}{-7.18550256098111}{,}{0.847025561996858}{,}{-0.521650204471473}{,}{0.554315899056769}{,}{-0.383454705154216}{,}{0.424528348348180}{,}{-15.4731007333996}{,}{3.50013739746017}{,}{1.38646163115761}{,}{0.977455473507018}{,}{1.50859631922343}{,}{-0.385486938414303}{,}{0.387606185755355}{,}{0.242738212421262}{,}{0.0951032873778647}{,}{-0.856549120155696}{,}{-1.01197786199731}{,}{-0.153769684929132}{,}{-1.15470182170982}{,}{1.42283743078110}{,}{24.9039012382310}{,}{-10.9813058418964}{,}{0.113001265118378}{,}{-3.60113095315593}{,}{1.40064202950975}{,}{32.7043229719714}{,}{-4.75351886310035}{,}{5.27559153781149}{,}{-18.6362694585819}{,}{0.653970714472146}{,}{1.24234420255103}{,}{0.108126432065482}{,}{2.67444678639560}{,}{3.07124255664684}{,}{0.419303895175839}{,}{-2.17448554104687}{,}{-1.23012138289219}{,}{-1.55863982139490}{,}{-7.76410779049744}{,}{-2.88619336710811}{,}{0.384582327110011}{,}{5.29958673634365}{,}{-0.493879589209775}{,}{-0.289575857622400}{,}{21.8884344275046}{,}{5.90230215985247}{,}{0.622150169416936}{,}{30.0931240235425}{,}{1.11547081269953}{,}{-0.565401901449362}{,}{0.561594721678296}{,}{-1.04077467299554}{,}{-0.750891485698618}{,}{0.637666443490380}{,}{0.0880888329445191}{,}{-0.286284790578470}{,}{0.335463367306689}{,}{1.00658082361453}{,}{0.269739459043548}{,}{0.164866517497936}{,}{0.269866061686888}{,}{0.0373128359640214}{,}{-3.80940236054882}{,}{0.827766640083894}{,}{117.311122932214}{,}{-0.493572119818394}{,}{11.4821055014999}{,}{-0.525613674332747}{,}{2.70520525500836}{,}{-0.143916822281931}{,}{-0.280042576812745}{,}{-1.23067073856702}{,}{-2.41529246962294}{,}{-0.686986549268073}{,}{0.862964766729828}{,}{\dots }{,}{\text{⋯ 900 row vector entries not shown}}\right]$ (2)
 > $y≔\mathrm{~}\left[\mathrm{+}\right]\left(5x+\mathrm{noise},\mathrm{ },10\right)$
 ${y}{≔}\left[{51.1750606049031}{,}{54.7981332592912}{,}{186.940169086633}{,}{54.4877836086379}{,}{42.1391672971744}{,}{15.2207481934875}{,}{23.5536042890210}{,}{35.2452968892949}{,}{44.6578631412264}{,}{57.9903335859462}{,}{16.3429517120533}{,}{59.3908012274704}{,}{57.4252824206110}{,}{36.1175200938710}{,}{50.9225376411666}{,}{17.3221866739069}{,}{29.4708620270441}{,}{63.4000043119627}{,}{48.6992782963273}{,}{62.1683327826769}{,}{41.6030025112349}{,}{11.3644334387519}{,}{48.8738933520096}{,}{57.1724741322908}{,}{42.3615482414473}{,}{40.7015039679356}{,}{48.0036489682427}{,}{29.0897007722369}{,}{43.3282104079346}{,}{18.1758796854239}{,}{45.7268327493286}{,}{-3.88145841452852}{,}{27.3462866455047}{,}{13.6950311627153}{,}{15.8340445352994}{,}{52.6814877355881}{,}{44.3559442103765}{,}{26.2425801887984}{,}{57.7538406543390}{,}{11.8174073125233}{,}{31.0806688626642}{,}{28.0659449926531}{,}{48.1220697225210}{,}{48.6052932351433}{,}{20.7664676585000}{,}{59.3921210276425}{,}{21.2980041936485}{,}{42.4286517706816}{,}{41.8671105897477}{,}{49.1349761286278}{,}{56.5055768219004}{,}{39.2316149795834}{,}{48.0304917365035}{,}{-0.505682698850393}{,}{16.6038547923910}{,}{36.1605468016582}{,}{58.0953243578695}{,}{29.6937331197023}{,}{42.3346301056357}{,}{21.6099008697327}{,}{45.3888674242358}{,}{21.5246343900713}{,}{33.7392127618622}{,}{37.1897283423369}{,}{51.6589692596818}{,}{58.3491535873822}{,}{42.6603632345338}{,}{16.4373425522242}{,}{17.1751244203305}{,}{44.7638471336914}{,}{57.9381649590356}{,}{23.3362591179935}{,}{80.8073653269833}{,}{23.2917192489490}{,}{55.8977792579120}{,}{28.0607830209187}{,}{18.7889878485649}{,}{21.8033014131029}{,}{41.4399002508223}{,}{33.7525312780810}{,}{27.2966905625714}{,}{51.8768947621212}{,}{40.2697853812507}{,}{37.7559198736005}{,}{56.0245497089884}{,}{24.5618170027056}{,}{47.8973242915001}{,}{43.8770523533759}{,}{29.8500589888517}{,}{155.702204968475}{,}{13.2991423583348}{,}{24.1796114348302}{,}{36.0142639761159}{,}{51.6635667601089}{,}{56.5566173891772}{,}{16.2152678468738}{,}{37.2105123050426}{,}{31.0542395832873}{,}{9.90811692579400}{,}{27.7190969866739}{,}{\dots }{,}{\text{⋯ 900 row vector entries not shown}}\right]$ (3)

Here we see all data points:

 > $\mathrm{plots}\left[\mathrm{setoptions}\right]\left(\mathrm{size}=\left[0.7,"golden"\right]\right):$
 > $\mathrm{pp}≔\mathrm{PointPlot}\left(y,\mathrm{xcoords}=x\right):$$\mathrm{pp}$

Linear least squares regression will be severely affected by the outliers.

 > $\mathrm{ls_regression_result}≔\mathrm{Fit}\left(aX+b,x,y,X\right)$
 ${\mathrm{ls_regression_result}}{≔}{3.44970682383807}{}{X}{+}{10.6816568681413}$ (4)
 > $\mathrm{ls_deviation_from_model}≔{\left(\mathrm{coeff}\left(\mathrm{ls_regression_result},X,1\right)-5\right)}^{2}+{\left(\mathrm{coeff}\left(\mathrm{ls_regression_result},X,0\right)-10\right)}^{2}$
 ${\mathrm{ls_deviation_from_model}}{≔}{2.86806501793850}$ (5)

Least trimmed squares regression gets much closer to the true line without noise.

 > $\mathrm{lts_regression_result}≔\mathrm{LeastTrimmedSquares}\left(x,y,\left[X\right]\right)$
 ${\mathrm{lts_regression_result}}{≔}{5.03537530551574}{}{X}{+}{9.82475419561271}$ (6)
 > $\mathrm{lts_deviation_from_model}≔{\left(\mathrm{coeff}\left(\mathrm{lts_regression_result},X,1\right)-5\right)}^{2}+{\left(\mathrm{coeff}\left(\mathrm{lts_regression_result},X,0\right)-10\right)}^{2}$
 ${\mathrm{lts_deviation_from_model}}{≔}{0.0319625041956798}$ (7)

The result is even better if we include 900 out of the 1000 points, instead of the default of a little over 500.

 > $\mathrm{lts_900_regression_result}≔\mathrm{LeastTrimmedSquares}\left(x,y,\left[X\right],\mathrm{include}=900\right)$
 ${\mathrm{lts_900_regression_result}}{≔}{5.00862730339998}{}{X}{+}{10.0156318668695}$ (8)
 > $\mathrm{lts_900_deviation_from_model}≔{\left(\mathrm{coeff}\left(\mathrm{lts_900_regression_result},X,1\right)-5\right)}^{2}+{\left(\mathrm{coeff}\left(\mathrm{lts_900_regression_result},X,0\right)-10\right)}^{2}$
 ${\mathrm{lts_900_deviation_from_model}}{≔}{0.000318785625781020}$ (9)

The other robust regression method, implemented in the RepeatedMedianEstimator procedure, also gets a good result.

 > $\mathrm{rme_regression_result}≔\mathrm{RepeatedMedianEstimator}\left(x,y,X\right)$
 ${\mathrm{rme_regression_result}}{≔}{10.0306661686300}{+}{5.00564125476873}{}{X}$ (10)
 > $\mathrm{rme_deviation_from_model}≔{\left(\mathrm{coeff}\left(\mathrm{rme_regression_result},X,1\right)-5\right)}^{2}+{\left(\mathrm{coeff}\left(\mathrm{rme_regression_result},X,0\right)-10\right)}^{2}$
 ${\mathrm{rme_deviation_from_model}}{≔}{0.000972237653807886}$ (11)

In order to visualize these results, we show the same point plot as before, including the four regression lines. The three regression lines from robust methods cannot be distinguished, but the least squares method is clearly off. We zoom in on the vertical range that includes most points.

 > $\mathrm{plots}\left[\mathrm{display}\right]\left(\mathrm{pp},\mathrm{plot}\left(\left[\mathrm{ls_regression_result},\mathrm{lts_regression_result},\mathrm{lts_900_regression_result},\mathrm{rme_regression_result}\right],X=0..10,\mathrm{legend}=\left["Least squares","Least trimmed squares","Least trimmed squares \left(900 points\right)","Repeated median estimator"\right]\right),\mathrm{view}=\left[0..10,-10..110\right]\right)$

In the next example, we have 1000 input points with two independent variables, most of which follow the model $z=5x-3y+2+\mathrm{noise}$, where the noise is relatively small. However, 10 of the points are far away in both the dependent and independent directions (and they have much greater noise). Such cases are particularly hard for non-robust methods to deal with.

We first see what this point cloud looks like, and then view the resulting fit.

 > $\mathrm{xy}≔\mathrm{Sample}\left(\mathrm{Normal}\left(0,1\right),\left[1000,2\right]\right)$
 ${\mathrm{xy}}{≔}\begin{array}{c}\left[\begin{array}{cc}{0.105339940461754}& {0.287051052605269}\\ {0.709853029122273}& {-1.07763466172273}\\ {-1.73034223774769}& {1.76952142183998}\\ {-1.37171224038413}& {0.542059860037101}\\ {1.36822294408878}& {-1.28530860698237}\\ {0.507864605572146}& {1.14482502096484}\\ {1.16607000857466}& {0.706310506199858}\\ {-0.202316960804702}& {1.47571931579124}\\ {0.178764522251086}& {0.499068843584967}\\ {-0.610301852690734}& {1.02172528408076}\\ {⋮}& {⋮}\end{array}\right]\\ \hfill {\text{1000 × 2 Matrix}}\end{array}$ (12)
 > $\mathrm{noise}≔\mathrm{Sample}\left(\mathrm{Normal}\left(0,1\right),1000\right)$
 ${\mathrm{noise}}{≔}\left[{-0.117144491808193}{,}{-0.897134447728629}{,}{0.862007630638958}{,}{0.458805209240829}{,}{0.303401553372019}{,}{0.513826498940742}{,}{-0.328675348371930}{,}{-0.714943212489833}{,}{-1.63432033965298}{,}{1.08289892162733}{,}{-1.55172286295297}{,}{-0.191421664639609}{,}{-1.11413981900620}{,}{0.324855456182234}{,}{-0.820146818863459}{,}{-0.274601425651991}{,}{0.355989688437521}{,}{0.334511168354300}{,}{-0.655208944223618}{,}{1.24423010542319}{,}{0.338346094352076}{,}{-1.27941173281570}{,}{-0.496112866791132}{,}{-0.359214816638707}{,}{-0.00560937349563868}{,}{-0.267076050074094}{,}{0.390023124976060}{,}{0.449845746445759}{,}{-2.33141964314768}{,}{-0.100654404140551}{,}{-0.338849650539197}{,}{-0.342740872172819}{,}{0.196924844466666}{,}{0.129769581246196}{,}{0.662633628243585}{,}{1.47418282185042}{,}{-0.252724360182957}{,}{0.655696164115015}{,}{0.715293423060039}{,}{0.872978666620833}{,}{0.861169607930365}{,}{-0.615055205790728}{,}{-0.867416448441391}{,}{-1.00637910527489}{,}{0.449834229577305}{,}{0.323465154704534}{,}{-0.103014759428642}{,}{-0.524846108862569}{,}{0.741348857915754}{,}{-1.33539313290199}{,}{-0.126370417422256}{,}{1.29766622197353}{,}{0.646959349131106}{,}{0.298142013279896}{,}{0.959277349650644}{,}{-0.935216345912542}{,}{-1.50281288603742}{,}{0.820110243003301}{,}{0.0698415023268370}{,}{0.286667780203410}{,}{1.90145633569239}{,}{1.74786031722540}{,}{0.0624931915404228}{,}{-0.887799102456989}{,}{0.273453686580042}{,}{0.896585892055245}{,}{1.29858220948418}{,}{-1.50187518245540}{,}{1.14585363789801}{,}{-0.445838112653020}{,}{0.700143871119436}{,}{-1.03372704435787}{,}{-0.121984555044142}{,}{0.200278711864945}{,}{-1.89480820664421}{,}{-1.59710975310777}{,}{0.615711301763054}{,}{0.470870907433032}{,}{-0.995276083368608}{,}{-0.644717144843705}{,}{-0.292492723188432}{,}{-0.724236413517762}{,}{-1.34068630439967}{,}{0.120879050970583}{,}{0.872358237520894}{,}{-1.38477949333585}{,}{-0.222560405951805}{,}{0.667072346783008}{,}{-0.987702581301952}{,}{1.26601580511326}{,}{-0.216973300130736}{,}{-2.63513626537342}{,}{-0.441725314650084}{,}{0.581679827973753}{,}{-1.27384793854347}{,}{1.13919469234058}{,}{0.0879042474906729}{,}{0.480717139793731}{,}{-0.543520589006367}{,}{0.505076889294675}{,}{\dots }{,}{\text{⋯ 900 row vector entries not shown}}\right]$ (13)
 > $\mathrm{xy}\left[1..10\right]≔\mathrm{Sample}\left(\mathrm{Normal}\left(0,10\right),\left[10,2\right]\right)$
 ${{\mathrm{xy}}}_{{1}{..}{10}}{≔}\left[\begin{array}{cc}{-7.25835457416460}& {4.46546251109489}\\ {-12.8116871270602}& {6.95701976222452}\\ {5.99013830391918}& {-0.0711220669337460}\\ {5.75154152981187}& {-24.7628908901018}\\ {9.09548316783929}& {3.96336774375604}\\ {8.73959176796014}& {10.8028281369976}\\ {10.7436650987406}& {-17.8756884460622}\\ {4.35976236509024}& {-11.7928910523180}\\ {-5.77171156203078}& {4.33504635234648}\\ {2.59252474066801}& {-10.1924397735107}\end{array}\right]$ (14)
 > $\mathrm{noise}\left[1..10\right]≔\mathrm{Sample}\left(\mathrm{Normal}\left(0,100\right),10\right)$
 ${{\mathrm{noise}}}_{{1}{..}{10}}{≔}\left[\begin{array}{cccccccccc}{100.800809666339}& {8.96726548728334}& {-46.8119150928935}& {-259.969027745269}& {19.0856519868389}& {-153.183461105396}& {-25.7014908594142}& {94.1751893325151}& {75.9056085428043}& {-120.552449126529}\end{array}\right]$ (15)
 > $z≔\mathrm{~}\left[\mathrm{+}\right]\left(\mathrm{xy}·⟨5,-3⟩,\mathrm{ },2\right)+{\mathrm{noise}}^{\mathrm{%T}}$
 ${z}{≔}\begin{array}{c}\left[\begin{array}{c}{53.1126492622311}\\ {-73.9622294346913}\\ {-14.6478573724963}\\ {-154.922647425904}\\ {54.6729645947673}\\ {-139.893986676588}\\ {83.6438999724755}\\ {153.352674314920}\\ {36.0419116756109}\\ {-75.0125061026564}\\ {⋮}\end{array}\right]\\ \hfill {\text{1000 element Vector[column]}}\end{array}$ (16)
 > $\mathrm{pp}≔\mathrm{plots}\left[\mathrm{pointplot3d}\right]\left(⟨\mathrm{xy}|z⟩,\mathrm{color}=\mathrm{black}\right):$$\mathrm{pp}$
 > $\mathrm{ls_regression_result}≔\mathrm{Fit}\left(aX+bY+c,\mathrm{xy},z,\left[X,Y\right]\right)$
 ${\mathrm{ls_regression_result}}{≔}{2.79396059575048}{}{X}{-}{0.846671122546459}{}{Y}{+}{1.81658520523718}$ (17)
 > $\mathrm{lts_regression_result}≔\mathrm{LeastTrimmedSquares}\left(\mathrm{xy},z,\left[X,Y\right]\right)$
 ${\mathrm{lts_regression_result}}{≔}{4.89975550872497}{}{X}{-}{3.01061932803024}{}{Y}{+}{2.16309086233403}$ (18)
 > $\mathrm{plots}\left[\mathrm{display}\right]\left(\mathrm{pp},\mathrm{plot3d}\left(\left[\mathrm{ls_regression_result},\mathrm{lts_regression_result}\right],X=-2..2,Y=-2..2,\mathrm{color}=\left[\mathrm{red},\mathrm{blue}\right]\right),\mathrm{view}=\left[-2..2,-2..2,-5..20\right],\mathrm{orientation}=\left[25,80,-60\right]\right)$

Data sets up to significant size can be used in reasonable time. In the third example, we have 100000 points with 10 independent variables.

 > $x≔\mathrm{Sample}\left(\mathrm{Uniform}\left(0,1\right),\left[100000,10\right]\right)$
 ${x}{≔}\begin{array}{c}\left[\begin{array}{cccccccccc}{0.367038091564797}& {0.185313564146110}& {0.596251899861115}& {0.896834251354233}& {0.136436034037138}& {0.135477949163865}& {0.423134419407821}& {0.416848456200155}& {0.784976986881811}& {0.266199730536143}\\ {0.0124092809348463}& {0.421932259971704}& {0.230653027703788}& {0.319598106468845}& {0.182287784328967}& {0.714157922091506}& {0.0528119262559404}& {0.483129117222565}& {0.420117359605775}& {0.772693510544879}\\ {0.115236728656779}& {0.923117144100893}& {0.702171320334795}& {0.596474835601867}& {0.942292771449364}& {0.943947300645059}& {0.422744048489313}& {0.144327035281378}& {0.984541889156903}& {0.723643214132309}\\ {0.542906971168772}& {0.112367867915511}& {0.444251184638994}& {0.570634650765737}& {0.108941654307873}& {0.0985601201671871}& {0.0391540828594090}& {0.586727979153879}& {0.627266403165410}& {0.791327067089883}\\ {0.722551781776623}& {0.497628800725940}& {0.849592921836574}& {0.315434592118291}& {0.988039071820604}& {0.433021620956288}& {0.214397050496906}& {0.488385758048042}& {0.454256582374789}& {0.597755258456938}\\ {0.629920852952375}& {0.597264574363181}& {0.367717430153175}& {0.785927384275676}& {0.905919040778304}& {0.900746002134469}& {0.519543834672696}& {0.482570823977509}& {0.798910007265380}& {0.216457947078571}\\ {0.210801432484484}& {0.262133392716753}& {0.0149383128654993}& {0.438804351238576}& {0.416401823641104}& {0.713567158346040}& {0.492561903722674}& {0.338479801346010}& {0.466273374319095}& {0.897198015117521}\\ {0.510189846216937}& {0.802958290599986}& {0.250964650391639}& {0.206387660971816}& {0.648965361119896}& {0.406007786829896}& {0.718237705387588}& {0.669098490577297}& {0.655529872093959}& {0.0906265499694102}\\ {0.950052691877403}& {0.557272955833118}& {0.655302389885816}& {0.135364649008629}& {0.478170683787540}& {0.00164000449294222}& {0.855401530591335}& {0.980175750999691}& {0.226176616439696}& {0.786076506372425}\\ {0.834402532853014}& {0.295229177507291}& {0.405354287657111}& {0.956084941891989}& {0.744826266329152}& {0.304792110488169}& {0.581757670456881}& {0.824522159469803}& {0.349440655491842}& {0.383416527301407}\\ {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}& {⋮}\end{array}\right]\\ \hfill {\text{100000 × 10 Matrix}}\end{array}$ (19)
 > $y≔x·⟨1,2,3,4,5,6,7,8,9,10⟩$
 ${y}{≔}\begin{array}{c}\left[\begin{array}{c}{23.6323245627799}\\ {23.7657195275317}\\ {37.0401657467990}\\ {24.0455745691566}\\ {28.5403788458573}\\ {32.8575260166424}\\ {28.2227248866244}\\ {26.5619107599292}\\ {30.6982291489493}\\ {29.6657596543789}\\ {⋮}\end{array}\right]\\ \hfill {\text{100000 element Vector[column]}}\end{array}$ (20)
 > $y\left[1..1000\right]≔\mathrm{~}\left[\mathrm{+}\right]\left(y\left[1..1000\right],\mathrm{ },10\right)$
 ${{y}}_{{1}{..}{1000}}{≔}\begin{array}{c}\left[\begin{array}{c}{33.6323245627799}\\ {33.7657195275317}\\ {47.0401657467990}\\ {34.0455745691566}\\ {38.5403788458573}\\ {42.8575260166424}\\ {38.2227248866244}\\ {36.5619107599292}\\ {40.6982291489493}\\ {39.6657596543789}\\ {⋮}\end{array}\right]\\ \hfill {\text{1000 element Vector[column]}}\end{array}$ (21)
 > $\mathrm{CodeTools}\left[\mathrm{Usage}\right]\left(\mathrm{LeastTrimmedSquares}\left(x,y,X\right)\right)$
 memory used=299.31MiB, alloc change=157.30MiB, cpu time=9.35s, real time=3.68s, gc time=5.03s
 ${0.999999999999940}{}{{X}}_{{1}}{+}{1.99999999999987}{}{{X}}_{{2}}{+}{3.00000000000004}{}{{X}}_{{3}}{+}{4.00000000000007}{}{{X}}_{{4}}{+}{5.00000000000000}{}{{X}}_{{5}}{+}{5.99999999999996}{}{{X}}_{{6}}{+}{7.00000000000007}{}{{X}}_{{7}}{+}{7.99999999999999}{}{{X}}_{{8}}{+}{9.00000000000001}{}{{X}}_{{9}}{+}{10.}{}{{X}}_{{10}}{+}{1.88793644367342}{×}{{10}}^{{-15}}$ (22)

References

 [1] Rousseeuw, Peter J. Least Median of Squares Regression. Journal of the American Statistical Association 79, 1984, pp.871-880.
 [2] Rousseeuw, Peter J., and Van Driessen, Katrien. Computing LTS Regression for Large Data Sets. Data Mining and Knowledge Discovery 12, 2006, pp.29-45.

Compatibility

 • The Statistics[LeastTrimmedSquares] command was introduced in Maple 2019.