| 1 |
%\iffalse |
| 2 |
% file: linearregression.dtx |
| 3 |
% author: Battista Benciolini |
| 4 |
% contact: benciolinibattista at gmail dot com |
| 5 |
% |
| 6 |
% process this file with pdflatex to obtain: |
| 7 |
% |
| 8 |
% - linearregression.pfd (full documentation, three pass needed) |
| 9 |
% - mainlinearregression.tex (interactive main-program document) |
| 10 |
% - linearregression.sty (package) |
| 11 |
% - sampledata.txt (as the name says) |
| 12 |
% |
| 13 |
% The author would strongly appreciate to receive |
| 14 |
% any comment, criticism and just usage report |
| 15 |
% |
| 16 |
%\fi |
| 17 |
%\iffalse |
| 18 |
%<*ins> |
| 19 |
\begingroup |
| 20 |
\input docstrip.tex |
| 21 |
\keepsilent |
| 22 |
\preamble |
| 23 |
------------------------------------------------------------------------ |
| 24 |
[2025-11-08] |
| 25 |
This file is part of the (expanded) distribution of linearregression |
| 26 |
The author of linearregression is Battista Benciolini |
| 27 |
<benciolinibattista at gmail dot com > |
| 28 |
------------------------------------------------------------------------ |
| 29 |
The author would strongly appreciate to receive |
| 30 |
any comment, criticism and just usage report |
| 31 |
------------------------------------------------------------------------ |
| 32 |
This program may be used, distributed and modified under |
| 33 |
the conditions of the LaTeX Project Public License. |
| 34 |
(see: http://www.latex-project.org/lppl.txt) |
| 35 |
------------------------------------------------------------------------ |
| 36 |
\endpreamble |
| 37 |
\askforoverwritefalse |
| 38 |
\generate{\file{linearregression.sty}{\from{linearregression.dtx}{package}}} |
| 39 |
\generate{\file{mainlinearregression.tex}{\from{linearregression.dtx}{main}}} |
| 40 |
\nopreamble\nopostamble |
| 41 |
\generate{\file{sampledata.txt}{\from{linearregression.dtx}{data}}} |
| 42 |
\endgroup |
| 43 |
%</ins> |
| 44 |
%\fi |
| 45 |
%\iffalse |
| 46 |
%<*driver> |
| 47 |
\documentclass[a4paper,10pt]{ltxdoc} |
| 48 |
\usepackage[T1]{fontenc} |
| 49 |
\usepackage{lmodern} |
| 50 |
\usepackage[lite,nobysame,non-compressed-cites]{amsrefs} |
| 51 |
\usepackage{amsmath,amssymb,amsfonts} |
| 52 |
\usepackage{multicol} |
| 53 |
\usepackage{linearregression} |
| 54 |
\usepackage{graphics} |
| 55 |
\DeclareRobustCommand*{\Ars}{\textsf{% |
| 56 |
\lower -.48ex\hbox{\rotatebox{-20}{A}}\kern -.3em{rs}}% |
| 57 |
\discretionary{-}{}{\kern -.05em}\TeX\discretionary{-}{}{% |
| 58 |
\kern -.17em}\lower -.357ex\hbox{nica}}% excerpt from some GUIT sty file |
| 59 |
%%\NewDocumentCommand\vect{m}{\underline{#1}} % vector |
| 60 |
%%\NewDocumentCommand\matr{m}{{#1}} % matrix |
| 61 |
\NewDocumentCommand\vect{m}{\mathbf{#1}} % vector |
| 62 |
\NewDocumentCommand\matr{m}{\mathbf{#1}} % matrix |
| 63 |
\NewDocumentCommand\barycenter{m}{\overline{#1}} % barycenter |
| 64 |
\NewDocumentCommand\point{}{\vect{y}} % point |
| 65 |
\NewDocumentCommand\coeff{}{\vect{x}} % direction |
| 66 |
\NewDocumentCommand\dx{}{\vect\delta} % direction variation |
| 67 |
\NewDocumentCommand\vv{}{\vect{v}} % barycentric coordinates |
| 68 |
\NewDocumentCommand\trasp{}{^{\mathsf{T}}} % traspose |
| 69 |
\NewDocumentCommand\Renne{}{\mathbb{R}^n} % vector space |
| 70 |
\NewDocumentCommand\dor{}{f} % distance from origin |
| 71 |
\NewDocumentCommand\ipoint{}{i} % index for points |
| 72 |
\NewDocumentCommand\pointsum{}{\sum_{\ipoint=1}^m} % sum over points |
| 73 |
\NewDocumentCommand\reff{m}{(\ref{#1})} % ref in ( ) |
| 74 |
\NewDocumentCommand\mC{}{\matr{C}} % matrix C |
| 75 |
\NewDocumentCommand\mc{}{k} % elements of matrix C |
| 76 |
\NewDocumentCommand\mL{}{\matr{\Lambda}} % matrix lambda |
| 77 |
\NewDocumentCommand\mX{}{\matr{X}} % matrix X |
| 78 |
\NewDocumentCommand\spm{}{\phantom{-}} % space for the sign |
| 79 |
\NewDocumentCommand\ctext{}{caption} % caption (a variable !) |
| 80 |
\NewDocumentCommand\matrixtwotwo{mmmm}{ % | 2 x 2 |
| 81 |
\begin{pmatrix} #1 & #2 \\ #3 & #4 \end{pmatrix}} % | matrix |
| 82 |
\DeclareMathOperator\tr{tr} % trace |
| 83 |
\DeclareMathOperator\sgn{sgn} % signum |
| 84 |
\title{Linear regression with \LaTeX} |
| 85 |
\author{Battista Benciolini} |
| 86 |
\NewDocumentCommand\titleauthorfootnote{}{\begingroup% Not an elegant solution |
| 87 |
\let\thefootnote\relax % but it is ok at the moment |
| 88 |
\footnote{Linear regression with LaTeX - available in CTAN}% |
| 89 |
\footnote{Battista Benciolini - contact: benciolinibattista at gmail dot com}% |
| 90 |
\endgroup\setcounter{footnote}{0}}% |
| 91 |
\parindent=0pt |
| 92 |
\begin{document} |
| 93 |
\hypersetup{hidelinks} |
| 94 |
\maketitle |
| 95 |
\titleauthorfootnote |
| 96 |
\tableofcontents |
| 97 |
\vfill |
| 98 |
\DocInput{linearregression.dtx} |
| 99 |
\end{document} |
| 100 |
%</driver> |
| 101 |
%\fi |
| 102 |
% |
| 103 |
% \section{Introduction: first description of the problem\label{intro}} |
| 104 |
% I start with a quote from \Ars\ (April 2021, number 31, page 73): |
| 105 |
% \begin{quotation} |
| 106 |
% The physicist Mario Rossi is investigating a phenomenon, |
| 107 |
% presumably linear, and he performs measurements in his laboratory |
| 108 |
% to verify his hypothesis; he measures the quantity $x$ which generates |
| 109 |
% the phenomenon and he measures also one of the characteristics |
| 110 |
% $y$ showed by the phenomenon under the effect of the stimulation $x$. |
| 111 |
% \\ ... \par |
| 112 |
% Subsequently Mario graphs the data of the table to judge if the points |
| 113 |
% reasonably follow a linear trend or not; in this regard he computes the |
| 114 |
% parameters of the regression line and he draws this line on the graph |
| 115 |
% in order to judge the quality of the obtained results. |
| 116 |
% \\ ... \par |
| 117 |
% Being a \LaTeX\ user, he thinks to kill two birds with one stone: |
| 118 |
% using \LaTeX\ to draw the graph with the experimental data consisting |
| 119 |
% in the $x$, $y$ points and, at the same time, to compute the |
| 120 |
% parameter $a$ e $b$ of the regression line $y = ax+b$, |
| 121 |
% and finally to draw also this line on the same graph. |
| 122 |
% \end{quotation} |
| 123 |
% A summary description of the problem is therefore the following. |
| 124 |
% A set of data pairs is available and each pair is represented as a point |
| 125 |
% in the plane. A straight line is searched that optimally approximates |
| 126 |
% the points. The first step is therefore the choice of an optimality criterion. |
| 127 |
% This choice is the topic of the next section. \par |
| 128 |
% From the text we also know that the possible deviation of $y$ |
| 129 |
% with respect to the model is quite larger than the deviation of $x$. |
| 130 |
% \par |
| 131 |
% After reading the description of the problem |
| 132 |
% of Mario Rossi I tried to produce a solution. |
| 133 |
% In this work I will use $y_1$ and $y_2$ |
| 134 |
% instead of $x$ and $y$ for the two measured quantities that |
| 135 |
% will become the first and second coordinate, or abscissa and ordinate, |
| 136 |
% in the Cartesian plane. |
| 137 |
% \par |
| 138 |
% The problem can be treated as a mere problem of approximation or |
| 139 |
% alternatively as an estimation problem in the frame of a |
| 140 |
% probabilistic description of the uncertainty. The two treatments are |
| 141 |
% conceptually different. The probabilistic treatment produces some more |
| 142 |
% results, but the estimation of the parameters is the same. |
| 143 |
% On the other hand the treatment as an approximation problem is in some sense |
| 144 |
% more immediate and requires a less extended theoretical background. |
| 145 |
% For this reason it will be preferred here. |
| 146 |
% I consider the original problem and also a variation |
| 147 |
% of it based on the assumption that the two variables are known with |
| 148 |
% the same uncertainty. The two considered situations will prove |
| 149 |
% to be quite different. |
| 150 |
% |
| 151 |
% \section{Geometric definition of three optimality criteria} |
| 152 |
% \begin{figure} |
| 153 |
% \setlength\unitlength{4cm} |
| 154 |
% \begin{picture}(1, 0.7)(0.,0.) |
| 155 |
% \multiput(0.,0.)(1.1,0){3}{\line(1,0){1}} |
| 156 |
% \multiput(0.,0.)(1.1,0){3}{\line(0,1){1}} |
| 157 |
% \multiput(1,1)(1.1,0){3}{\line(-1,0){1}} |
| 158 |
% \multiput(1,1)(1.1,0){3}{\line(0,-1){1}} |
| 159 |
% \thicklines |
| 160 |
% \multiput(0.08,0.06)(1.1,0){3}{\line(4,3){0.88}} |
| 161 |
% \multiput(0.26,0.09)(1.1,0){3}{\circle{0.03}} |
| 162 |
% \multiput(0.45,0.65)(1.1,0){3}{\circle{0.03}} |
| 163 |
% \multiput(0.92,0.44)(1.1,0){3}{\circle{0.03}} |
| 164 |
% \put(0.26,0.09){\line(0,1){0.1050}} |
| 165 |
% \put(0.45,0.65){\line(0,-1){0.3125}} |
| 166 |
% \put(0.92,0.44){\line(0,1){0.2500}} |
| 167 |
% \put(1.36,0.09){\line(-1,0){0.14}} |
| 168 |
% \put(1.55,0.65){\line( 1,0){0.4167}} |
| 169 |
% \put(2.02,0.44){\line(-1,0){0.3333}} |
| 170 |
% \put(2.46,0.09){\line(-3,4){0.05}} |
| 171 |
% \put(2.65,0.65){\line(3,-4){0.15}} |
| 172 |
% \put(3.12,0.44){\line(-3,4){0.12}} |
| 173 |
% \end{picture} |
| 174 |
% \caption{The three kinds of segments |
| 175 |
% used in the definition of the objective function} |
| 176 |
% \label{fig:criteria} |
| 177 |
% \end{figure} |
| 178 |
% For each point given in the plane we can consider the corresponding point |
| 179 |
% with the same abscissa and belonging to the line. |
| 180 |
% Remember that the line is exactly what has to be determined. |
| 181 |
% The distance between the given point and the just defined point on the line |
| 182 |
% is a reasonable measure of the discrepancy between the empirical data and the |
| 183 |
% corresponding theoretical model. |
| 184 |
% The distances we are speaking about are the lengths of the segments shown |
| 185 |
% in the leftmost scheme of figure (\ref{fig:criteria}). |
| 186 |
% To obtain a global discrepancy measure that considers all the points |
| 187 |
% at once we perform the sum of the squares of the lengths |
| 188 |
% of the mentioned segments. It is now clear that the two coordinates |
| 189 |
% of the points are treated quite differently and play a different role in |
| 190 |
% the definition of the optimality criterion. This choice is reasonable when |
| 191 |
% the measuring errors only (or mainly) affect the second coordinate. |
| 192 |
% The optimal line is the line that minimize the just defined |
| 193 |
% global discrepancy. The procedure for the determination of the optimal line |
| 194 |
% is named linear regression. |
| 195 |
% In this work it is named \textit{classical linear regression}. |
| 196 |
% We can easily exchange the role of the two quantities, i.e.\ we can |
| 197 |
% imagine that the first quantity is affected by errors. |
| 198 |
% The problem is not conceptually different. The segments plotted in the |
| 199 |
% central picture |
| 200 |
% of figure (\ref{fig:criteria}) represent the discrepancy between |
| 201 |
% the empirical data and the model. |
| 202 |
% This other procedure is named \textit{classical linear regression |
| 203 |
% with inverted role of the coordinates}.\par |
| 204 |
% The situation is really different if the two coordinates have to be treated |
| 205 |
% symmetrically. |
| 206 |
% In this case the discrepancy between |
| 207 |
% the empirical data and the model must be defined in a purely geometrical way. |
| 208 |
% Just the line and the points enter in the definition without any special role |
| 209 |
% for any predefined direction. With these requirements it is quite natural |
| 210 |
% to use the distance of each point from the line. |
| 211 |
% Remember that the distance of a point |
| 212 |
% from a line is intended along the shortest path, i.e.\ measured in the |
| 213 |
% direction orthogonal to the line itself. The rightmost scheme of |
| 214 |
% figure (\ref{fig:criteria}) shows the segments that are considered. |
| 215 |
% The global measure of discrepancy is again obtained as the sum |
| 216 |
% of the squares of the lengts of the mentioned orthogonal segments. |
| 217 |
% The procedure that obtain the optimal line that |
| 218 |
% minimize the just defined global discrepancy is named |
| 219 |
% \textit{symmetrical linear regression}.\par |
| 220 |
% Some arguments of the present section will be repeated in section |
| 221 |
% \ref{package} from the algebraic and computational point of view. |
| 222 |
% |
| 223 |
% \section{Information on the realised solution, including limitations} |
| 224 |
% The code that implements the solution is recorded in two files, that are |
| 225 |
% a package (sty) file and a main interactive document. |
| 226 |
% The file |linearregression.sty| provides several commands |
| 227 |
% that can be used in any document. The file |mainlinearregression.tex| |
| 228 |
% provides a simple interactive user interface. |
| 229 |
% The package described in the sections \ref{manual} and |
| 230 |
% \ref{package} (user manual and implementation) provides the |
| 231 |
% functions that execute the various needed operations, i.e.\ |
| 232 |
% data input, computations, printing the numerical results and |
| 233 |
% generating a graphic representation of data and results. |
| 234 |
% Some auxiliary functions complete the package. |
| 235 |
% The design of the output (tables and plots) includes some arbitrary choices. |
| 236 |
% The style of the graphic output is quite minimalist |
| 237 |
% (e.g.:\ no colors, no variations of line styles).\par |
| 238 |
% |
| 239 |
% \section{Some comments about the programming of the package} |
| 240 |
% Large part of the code is written using the |expl3| language. |
| 241 |
% I have tried to be compliant with the various recommendations and |
| 242 |
% prescriptions for a correct use of the language, |
| 243 |
% but I probably only partly succeeded.\par |
| 244 |
% Different more elegant and more coherent solutions probably exist |
| 245 |
% both for the general structure of the package and for some specific part |
| 246 |
% of the code, but this is what I have been able to do. |
| 247 |
% Some perhaps problematic aspects are mentioned here after.\par |
| 248 |
% Several used variables are global and they are accessed by various functions. |
| 249 |
% This makes the various parts of the package |
| 250 |
% quite connected to each other and creates strong dependencies. \par |
| 251 |
% The layered programming style is only partially applied. |
| 252 |
% The partition between document command and lower level functions is present, |
| 253 |
% but part of the low level code is directly in the document commands. |
| 254 |
% Variants are not used. |
| 255 |
% |
| 256 |
% \section{A ready to use simple user interface\label{main}} |
| 257 |
% The main file asks the user for the name of a |
| 258 |
% file containing the data and generates a one page output. |
| 259 |
%\iffalse |
| 260 |
%<*main> |
| 261 |
%\fi |
| 262 |
% \begin{macrocode} |
| 263 |
\documentclass[a4paper]{article} |
| 264 |
\usepackage{lmodern} |
| 265 |
\usepackage{linearregression} |
| 266 |
\begin{document} |
| 267 |
\pagestyle{empty} |
| 268 |
\lraskfilename |
| 269 |
\lrcomputation |
| 270 |
\lrplotparameters{0.16}{11.0} |
| 271 |
\lrplot{11.0}{+}{+}{-}{-} |
| 272 |
\lrprint |
| 273 |
\end{document} |
| 274 |
% \end{macrocode} |
| 275 |
%\iffalse |
| 276 |
%</main> |
| 277 |
%\fi |
| 278 |
% The plot just includes the points and the line obtained with the classical |
| 279 |
% linear regression. Lines 8 and 9 of the code must be modified if a different |
| 280 |
% result is desired. See the next section for details. |
| 281 |
% |
| 282 |
% \section{A user manual for the package\label{manual}} |
| 283 |
% The various analysis of a data set and the representation of the data |
| 284 |
% and of the results is obtained with a sequence of several commands. |
| 285 |
% The main operations are: |
| 286 |
% (i) selection of the data file, (ii) data imput and computation, |
| 287 |
% (iii) printing of a table, |
| 288 |
% (iv) printing of a picture (that can be repeated with different parameters). |
| 289 |
% It is generally convenient to put the table and the picture(s) |
| 290 |
% in a proper floating environment. |
| 291 |
% The commands for the four mentioned operations are described here after. |
| 292 |
% The first needed operation is to set the name of the data file. |
| 293 |
% This is done with the command \DescribeMacro{\lrfilename} |
| 294 |
% \cs{lrfilename}\marg{file} that has a mandatory argument. |
| 295 |
% The argument is the name of the data file. As an alternative the |
| 296 |
% command \DescribeMacro{\lraskfilename} \cs{lraskfilename} can be used. |
| 297 |
% It asks the user to type the name of the data file in the terminal. |
| 298 |
% \par |
| 299 |
% The macro \DescribeMacro{\lrcomputation} |
| 300 |
% \cs{lrcomputation} reads the data |
| 301 |
% and performs all the computations. |
| 302 |
% The results of the computations remain available in internal |
| 303 |
% variables and are then used by the macro that print them |
| 304 |
% or generates a plot. |
| 305 |
%\par |
| 306 |
% The macro \DescribeMacro{\lrprint} |
| 307 |
% \cs{lrprint} generates a table with all the estimated |
| 308 |
% parameters and some information about the data. |
| 309 |
% The computed numbers are printed with four decimal digit maximum by default, |
| 310 |
% but this number can be set to any desired value with the command |
| 311 |
% \DescribeMacro{\lrnumdigit} \cs{lrnumdigit}\marg{number}. |
| 312 |
% The mandatory argument is the desired maximum number of digits. |
| 313 |
% \par |
| 314 |
% The macro \DescribeMacro{\lrplot} |
| 315 |
% \cs{lrplot}\marg{imagewidth}\marg{key1}\marg{key2}\marg{key3}\marg{key4} |
| 316 |
% really generates the plot. The first argument is the |
| 317 |
% width of the plot in centimetrs, while the height is computed according |
| 318 |
% to the distribution of the points or it is recorded with the command |
| 319 |
% \cs{lrplotparameters} (see below). |
| 320 |
% The other four arguments are referred |
| 321 |
% to the data points, to the lines determined with classical regression, |
| 322 |
% with classical regression with inverted role of the coordinates and |
| 323 |
% with symmetric regression. |
| 324 |
% The four items, i.e.\ the set of points and the three lines, are drawn |
| 325 |
% or not according to the corresponding string found in |key|$i$. |
| 326 |
% Each item is not plotted if the string is the character |-|, it is plotted |
| 327 |
% in any other case. Furthermore the lines are accompanied by a label made |
| 328 |
% by the corresponding |key|, unless it is just a |+| or a |-|. |
| 329 |
% \par |
| 330 |
% The command \DescribeMacro{\lrplotparameters} |
| 331 |
% \cs{lrplotparameters}\marg{diameter}\marg{imageheight} |
| 332 |
% is used to record some more parameters that are related to the generation |
| 333 |
% of the plot. |
| 334 |
% The first argument \marg{diameter} is the diameter in centimeters of the discs |
| 335 |
% that represent the points. It must be a real positive number. |
| 336 |
% The default value is 0.2. |
| 337 |
% The second argument \marg{imageheight} is the height of the plot |
| 338 |
% in centimeters. |
| 339 |
% It must be a real number with a special meaning of any non positive value. |
| 340 |
% If the number is positive it is really used as the height and the variables are |
| 341 |
% independently scaled to fill the width and the height of the plot. |
| 342 |
% If the value is not positive or if the command is not used the |
| 343 |
% two variables are scaled with the same factor and the height is computed |
| 344 |
% accordingly. This can generate reasonable results only if the ranges |
| 345 |
% of the two variables are not too much different. |
| 346 |
% All the arguments are mandatory. |
| 347 |
% \par |
| 348 |
% The use of \cs{lrnumdigit} and \cs{lrplotparameters} is a convenient solution |
| 349 |
% to introduce some flexibility in the output and to completely preserve |
| 350 |
% compatibility with the previous version of the package. |
| 351 |
% Some more elegant solution could be realized with the use of optional |
| 352 |
% arguments and perhaps of key-value pairs, but the implemented solution |
| 353 |
% is preferred because it is extremely simple. |
| 354 |
% \par |
| 355 |
% Few words are necessary about the format of the data file. |
| 356 |
% Each record of the file hold the two values related to a point. |
| 357 |
% The two values must be separated by any number (one is needed as a minimum) of |
| 358 |
% space and comma characters. No character different from space |
| 359 |
% can be accepted before the first value and after the second value. |
| 360 |
% |
| 361 |
% \section{An example\label{example}} |
| 362 |
% The data reported here after will be available in |sampledata.txt| |
| 363 |
% and will be used in the examples presented in this section . |
| 364 |
%\iffalse |
| 365 |
%<*data> |
| 366 |
%\fi |
| 367 |
% \begin{multicols}{4} |
| 368 |
% \begin{macrocode} |
| 369 |
-0.546 0.107 |
| 370 |
1.093 -0.510 |
| 371 |
1.440 1.995 |
| 372 |
1.414 0.991 |
| 373 |
0.735 1.585 |
| 374 |
-1.848 -0.235 |
| 375 |
-0.203 -0.292 |
| 376 |
1.517 0.779 |
| 377 |
0.559 -1.341 |
| 378 |
-0.462 -0.437 |
| 379 |
-0.785 -0.661 |
| 380 |
-0.558 0.397 |
| 381 |
0.181 -2.616 |
| 382 |
0.619 1.859 |
| 383 |
-0.223 -1.915 |
| 384 |
0.629 -0.534 |
| 385 |
-1.989 -2.300 |
| 386 |
-0.241 1.098 |
| 387 |
-0.931 -1.613 |
| 388 |
-1.070 0.592 |
| 389 |
2.341 0.413 |
| 390 |
1.993 -0.111 |
| 391 |
-2.357 -0.312 |
| 392 |
-1.975 0.140 |
| 393 |
% \end{macrocode} |
| 394 |
% \end{multicols} |
| 395 |
%\iffalse |
| 396 |
%</data> |
| 397 |
%\fi |
| 398 |
% |
| 399 |
% The analysis of the sample data and the generation of a numeric table |
| 400 |
% is operated by a code similar to the following |
| 401 |
% (see table \ref{tab:sampledata}). \\ |
| 402 |
% |\lrfilename{sampledata.txt}| \\ |\lrcomputation| \\ |
| 403 |
% |\lrnumdigit{5}| \\ |\begin{table}| \\ |
| 404 |
% | \lrprint| \\ |
| 405 |
% | \caption{Analysis of ... }| \\ |\label{tab:sampledata}\end{table}| |
| 406 |
% \par |
| 407 |
% The generation of some different graphical representation of the data and of |
| 408 |
% the results is operated by a code similar to the following |
| 409 |
% (see figures \ref{fig:sampledataB} ).\\ |
| 410 |
% \RenewDocumentCommand\ctext{}{LEFT The three lines are obtained with the three |
| 411 |
% optimality criteria. (AA) classical linear regression; (BB) classical linear |
| 412 |
% regression with inverted role of the coordinates; (S) symmetric linear |
| 413 |
% regression. RIGHT Data points and line estimated with |
| 414 |
% symmetric linear regression.} |
| 415 |
% |\begin{figure}|\\|\lrplot{10.}{-}{AA}{BB}{S}| \\ |
| 416 |
% |\lrplot{10.}{+}{-}{-}{+}| |
| 417 |
% \\ |\caption{|\ctext|}|\\ | \label{fig:sampledataB} \end{figure}| |
| 418 |
% |
| 419 |
% \lrfilename{sampledata.txt} \lrcomputation \lrnumdigit{5} |
| 420 |
% \begin{table} \lrprint \caption{Analysis of the sample data} |
| 421 |
% \label{tab:sampledata} \end{table} |
| 422 |
% \begin{figure} \lrplot{6.}{-}{AA}{BB}{S} \hfill \lrplot{6.}{+}{-}{-}{+} |
| 423 |
% \caption{\ctext} \label{fig:sampledataB} \end{figure} |
| 424 |
% The same data are used to show the effect of the command |
| 425 |
% \cs{lrplotparameters}. |
| 426 |
% \begin{figure} |
| 427 |
% \lrplotparameters{0.12}{5.} \lrplot{4.}{+}{-}{-}{+} \hfill |
| 428 |
% \lrplotparameters{0.10}{3.} \lrplot{4.}{+}{-}{-}{+} \hfill |
| 429 |
% \lrplotparameters{0.11}{-1.} \lrplot{4.}{+}{-}{-}{+} |
| 430 |
% \caption{Three different version of a plot to demonstrate the use of |
| 431 |
% the command \cs{lrplotparameters}} \label{fig:sampledataC} \end{figure} |
| 432 |
% The commands:\\ |...| \\ |
| 433 |
% |\lrplotparameters{0.12}{5.} \lrplot{4.}{+}{-}{-}{+} \hfill | \\ |
| 434 |
% |\lrplotparameters{0.10}{3.} \lrplot{4.}{+}{-}{-}{+} \hfill | \\ |
| 435 |
% |\lrplotparameters{0.11}{-1.} \lrplot{4.}{+}{-}{-}{+} | \\ |...| \\ |
| 436 |
% generate the figure (\ref{fig:sampledataC}). |
| 437 |
% |
| 438 |
% \section{A package for linear regression |
| 439 |
% and the theory behind it\label{package}} |
| 440 |
%\iffalse |
| 441 |
%<*package> |
| 442 |
%\fi |
| 443 |
% |
| 444 |
% \subsection{Math preliminaries and notation \label{prelim}} |
| 445 |
% The coordinates of a set of $m$ points on the plane are available. |
| 446 |
% A straight line is searched that optimally approximates the points.\par |
| 447 |
% The coordinates of a generic point are $y_1$ and $y_2$ |
| 448 |
% and they are collected in the vector $\point$. |
| 449 |
% Any given point is identified with the index $\ipoint$. |
| 450 |
% Explicit indices $(\dots)_1$ or $(\dots)_2$ always refer to the first |
| 451 |
% or second coordinate of a point or to the first or second component |
| 452 |
% of a vector in the plane. |
| 453 |
% Symbolic index $(\dots)\ipoint$ always refers to the different points. Few |
| 454 |
% formulas require both indices $(\dots)_{1\ipoint}$, $(\dots)_{2\ipoint}$.\par |
| 455 |
% With more then two points a criterion of best approximation |
| 456 |
% is needed to select the optimal line that describes the data. \par |
| 457 |
% Lower case symbols are used for scalars. Lower case bold |
| 458 |
% symbols are used for vectors in the plane. Upper case bold symbols |
| 459 |
% are used for matrices. |
| 460 |
% \par |
| 461 |
% It is possible that certain data generate an ambiguity or a singularity |
| 462 |
% in the computation. |
| 463 |
% The following mathematical treatment of the problem |
| 464 |
% do not mention these situations and the code does not deal with them. |
| 465 |
% |
| 466 |
% \subsection{Package declaration, required package and definition of variables} |
| 467 |
% The various macro will be provided in a package file |
| 468 |
% that is introduced as usual. Most of the macros require |
| 469 |
% the \LaTeX3 syntax. |
| 470 |
% \begin{macrocode} |
| 471 |
\ProvidesPackage{linearregression}[2025-11-08] |
| 472 |
\RequirePackage{pict2e} |
| 473 |
\ExplSyntaxOn |
| 474 |
% \end{macrocode} |
| 475 |
% The variables used in the package are defined hereafter. |
| 476 |
% \begin{macrocode} |
| 477 |
\ior_new:N \g_BBLR_file_ior |
| 478 |
\tl_new:N \g_BBLR_file_name_tl |
| 479 |
\int_new:N \g_BBLR_number_of_points_int |
| 480 |
\fp_new:N \g_BBLR_abscissa_fp |
| 481 |
\fp_new:N \g_BBLR_ordinate_fp |
| 482 |
\fp_new:N \g_BBLR_mean_abscissa_fp |
| 483 |
\fp_new:N \g_BBLR_mean_ordinate_fp |
| 484 |
\fp_new:N \g_BBLR_abscissa_SecOrdMoment_fp |
| 485 |
\fp_new:N \g_BBLR_ordinate_SecOrdMoment_fp |
| 486 |
\fp_new:N \g_BBLR_mixed_SecOrdMoment_fp |
| 487 |
\fp_new:N \g_BBLR_slope_A_fp |
| 488 |
\fp_new:N \g_BBLR_slope_B_fp |
| 489 |
\fp_new:N \g_BBLR_slope_S_fp |
| 490 |
\fp_new:N \g_BBLR_intercept_A_fp |
| 491 |
\fp_new:N \g_BBLR_intercept_B_fp |
| 492 |
\fp_new:N \g_BBLR_intercept_S_fp |
| 493 |
\fp_new:N \g_BBLR_cos_fp |
| 494 |
\fp_new:N \g_BBLR_sin_fp |
| 495 |
\fp_new:N \g_BBLR_sig_sin_fp |
| 496 |
\fp_new:N \g_BBLR_eig_diff_fp |
| 497 |
\fp_new:N \g_BBLR_diag_diff_fp |
| 498 |
\tl_new:N \g_BBLR_file_line_tl |
| 499 |
\fp_new:N \g_BBLR_min_abscissa_fp |
| 500 |
\fp_new:N \g_BBLR_min_ordinate_fp |
| 501 |
\fp_new:N \g_BBLR_max_abscissa_fp |
| 502 |
\fp_new:N \g_BBLR_max_ordinate_fp |
| 503 |
\fp_new:N \g_BBLR_min_draw_abscissa_fp |
| 504 |
\fp_new:N \g_BBLR_max_draw_abscissa_fp |
| 505 |
\bool_new:N \g_BBLR_data_eof_bool |
| 506 |
\int_new:N \g_BBLR_record_length_int |
| 507 |
\int_new:N \g_BBLR_rec_count_int |
| 508 |
\int_new:N \g_BBLR_first_separator_int |
| 509 |
\int_new:N \g_BBLR_last_separator_int |
| 510 |
\str_const:Nn \c_BBLR_space_str {~} |
| 511 |
\str_const:Nn \c_BBLR_comma_str {,} |
| 512 |
\str_const:Nn \c_BBLR_plus_str {+} |
| 513 |
\str_const:Nn \c_BBLR_minus_str {-} |
| 514 |
\bool_new:N \g_BBLR_plot_points_bool |
| 515 |
\bool_new:N \g_BBLR_plot_lineA_bool |
| 516 |
\bool_new:N \g_BBLR_plot_lineB_bool |
| 517 |
\bool_new:N \g_BBLR_plot_lineS_bool |
| 518 |
\bool_new:N \g_BBLR_two_scale_bool |
| 519 |
\bool_gset_false:N \g_BBLR_two_scale_bool |
| 520 |
\fp_new:N \g_BBLR_base_fp |
| 521 |
\fp_new:N \g_BBLR_height_fp |
| 522 |
\fp_new:N \g_BBLR_Xbase_fp |
| 523 |
\fp_new:N \g_BBLR_Xheight_fp |
| 524 |
\fp_new:N \g_BBLR_XXheight_fp |
| 525 |
\fp_new:N \g_BBLR_Dabscissa_fp |
| 526 |
\fp_new:N \g_BBLR_Dordinate_fp |
| 527 |
\fp_new:N \g_BBLR_diameter_fp |
| 528 |
\fp_gset:Nn \g_BBLR_diameter_fp{0.2} |
| 529 |
\fp_new:N \g_BBLR_line_base_length_fp |
| 530 |
\fp_new:N \g_BBLR_scale_factor_fp |
| 531 |
\fp_new:N \g_BBLR_scale_factor_H_fp |
| 532 |
\str_new:N \g_BBLR_point_code_str |
| 533 |
\str_new:N \g_BBLR_labelA_str |
| 534 |
\str_new:N \g_BBLR_labelB_str |
| 535 |
\str_new:N \g_BBLR_labelS_str |
| 536 |
\int_new:N \g_BBLR_num_dec_dig_int |
| 537 |
\int_gset:Nn \g_BBLR_num_dec_dig_int{4} |
| 538 |
% \end{macrocode} |
| 539 |
% |
| 540 |
% \subsection{Preparing data input} |
| 541 |
% \begin{macro}{\lrfilename} |
| 542 |
% The command \cs{lrfilename} records the file name passed as argument. |
| 543 |
% \begin{macrocode} |
| 544 |
\NewDocumentCommand{\lrfilename}{m}{ |
| 545 |
\tl_gset:Nn \g_BBLR_file_name_tl {#1} |
| 546 |
} |
| 547 |
% \end{macrocode} |
| 548 |
% \end{macro} |
| 549 |
% \begin{macro}{\lraskfilename} |
| 550 |
% The command \cs{lraskfilename} asks for the data file name from the terminal. |
| 551 |
% \begin{macrocode} |
| 552 |
\NewDocumentCommand{\lraskfilename}{}{ |
| 553 |
\ior_get_term:nN {filename ? } \g_BBLR_file_name_tl |
| 554 |
\tl_trim_spaces:N \g_BBLR_file_name_tl |
| 555 |
} |
| 556 |
% \end{macrocode} |
| 557 |
% \end{macro} |
| 558 |
% |
| 559 |
% \subsection{Computation} |
| 560 |
% \begin{macro}{\lrcomputation} |
| 561 |
% The command \cs{lrcomputation} reads the data file and |
| 562 |
% performs all the relevant computations to solve the |
| 563 |
% proposed problem. |
| 564 |
% \begin{macrocode} |
| 565 |
\NewDocumentCommand{\lrcomputation}{}{% |
| 566 |
% \end{macrocode} |
| 567 |
% |
| 568 |
% \subsubsection{Computation of first and second order moments} |
| 569 |
% In the sequel it will results that the first and second order moments |
| 570 |
% of the data provide everything needed to solve the problem. |
| 571 |
% The barycenter of the data is defined as |
| 572 |
% \begin{equation} |
| 573 |
% \barycenter{\point}=\frac{1}{m}\pointsum \point_\ipoint. |
| 574 |
% \label{barycenter} \end{equation} |
| 575 |
% It is convenient to scan the data to accumulate the sum |
| 576 |
% that appears in \reff{barycenter}. |
| 577 |
% The coordinates of each point are read from the file |
| 578 |
% and they are immediately used. The data are not globally recorded. |
| 579 |
% \begin{macrocode} |
| 580 |
\bool_gset_false:N \g_BBLR_data_eof_bool |
| 581 |
\int_zero:N \g_BBLR_number_of_points_int |
| 582 |
\fp_zero:N \g_BBLR_mean_abscissa_fp |
| 583 |
\fp_zero:N \g_BBLR_mean_ordinate_fp |
| 584 |
\ior_open:Nn \g_BBLR_file_ior \g_BBLR_file_name_tl |
| 585 |
\bool_until_do:Nn \g_BBLR_data_eof_bool { |
| 586 |
\ior_str_get:NN \g_BBLR_file_ior \g_BBLR_file_line_tl |
| 587 |
\if_eof:w \g_BBLR_file_ior |
| 588 |
\bool_gset_true:N \g_BBLR_data_eof_bool |
| 589 |
\else: |
| 590 |
\int_incr:N \g_BBLR_number_of_points_int |
| 591 |
\BBLR_decode_data: |
| 592 |
\fp_gset:Nn \g_BBLR_mean_abscissa_fp |
| 593 |
{\g_BBLR_mean_abscissa_fp + \g_BBLR_abscissa_fp} |
| 594 |
\fp_gset:Nn \g_BBLR_mean_ordinate_fp |
| 595 |
{\g_BBLR_mean_ordinate_fp + \g_BBLR_ordinate_fp} |
| 596 |
\fi: |
| 597 |
} |
| 598 |
% \end{macrocode} |
| 599 |
% Loop ended. Now close the file, |
| 600 |
% globally set the value of the number of points |
| 601 |
% and divide the accumulated sums by the number of points. |
| 602 |
% \begin{macrocode} |
| 603 |
\int_gset:Nn \g_BBLR_number_of_points_int {\g_BBLR_number_of_points_int} |
| 604 |
\ior_close:N \g_BBLR_file_ior |
| 605 |
\fp_gset:Nn \g_BBLR_mean_abscissa_fp |
| 606 |
{\g_BBLR_mean_abscissa_fp / \g_BBLR_number_of_points_int} |
| 607 |
\fp_gset:Nn \g_BBLR_mean_ordinate_fp |
| 608 |
{\g_BBLR_mean_ordinate_fp / \g_BBLR_number_of_points_int} |
| 609 |
% \end{macrocode} |
| 610 |
% |
| 611 |
% The barycentric coordinates are defined for each point |
| 612 |
% \begin{equation} \vv_\ipoint= \point_\ipoint - \barycenter{\point} |
| 613 |
% \label{residual} \end{equation} |
| 614 |
% and the empirical dispersion matrix is defined as: |
| 615 |
% \begin{equation} \mC=\frac{1}{m}\pointsum \vv_\ipoint\vv_\ipoint\trasp . |
| 616 |
% \label{matrixC} \end{equation} |
| 617 |
% Superscript as in $()\trasp$ means transpose. The elements of $\mC$ are the |
| 618 |
% second order central moments and they are denoted as: |
| 619 |
% \begin{equation} \mC=\matrixtwotwo{\mc_{11}}{\mc_{12}}{\mc_{12}}{\mc_{22}}. |
| 620 |
% \label{matrixCc} \end{equation} |
| 621 |
% A second scan of the data is performed to compute the |
| 622 |
% sums that appears in \reff{matrixC} and to determine the |
| 623 |
% extremal values of the coordinates. Record scan can be regulated |
| 624 |
% by a record counter, because the number of points is now known. |
| 625 |
% \begin{macrocode} |
| 626 |
\fp_zero:N \g_BBLR_abscissa_SecOrdMoment_fp |
| 627 |
\fp_zero:N \g_BBLR_ordinate_SecOrdMoment_fp |
| 628 |
\fp_zero:N \g_BBLR_mixed_SecOrdMoment_fp |
| 629 |
\fp_gset_eq:NN \g_BBLR_min_abscissa_fp \g_BBLR_mean_abscissa_fp |
| 630 |
\fp_gset_eq:NN \g_BBLR_min_ordinate_fp \g_BBLR_mean_ordinate_fp |
| 631 |
\fp_gset_eq:NN \g_BBLR_max_abscissa_fp \g_BBLR_mean_abscissa_fp |
| 632 |
\fp_gset_eq:NN \g_BBLR_max_ordinate_fp \g_BBLR_mean_ordinate_fp |
| 633 |
\ior_open:Nn \g_BBLR_file_ior \g_BBLR_file_name_tl |
| 634 |
\int_zero:N \g_BBLR_rec_count_int |
| 635 |
\int_do_until:nn |
| 636 |
{\g_BBLR_rec_count_int = \g_BBLR_number_of_points_int} |
| 637 |
{ |
| 638 |
\ior_str_get:NN \g_BBLR_file_ior \g_BBLR_file_line_tl |
| 639 |
\int_incr:N \g_BBLR_rec_count_int |
| 640 |
\BBLR_decode_data: |
| 641 |
\fp_gset:Nn \g_tmpa_fp |
| 642 |
{\g_BBLR_abscissa_fp - \g_BBLR_mean_abscissa_fp} |
| 643 |
\fp_gset:Nn \g_tmpb_fp |
| 644 |
{\g_BBLR_ordinate_fp - \g_BBLR_mean_ordinate_fp} |
| 645 |
\fp_gset:Nn \g_BBLR_abscissa_SecOrdMoment_fp |
| 646 |
{\g_BBLR_abscissa_SecOrdMoment_fp + \g_tmpa_fp * \g_tmpa_fp} |
| 647 |
\fp_gset:Nn \g_BBLR_mixed_SecOrdMoment_fp |
| 648 |
{\g_BBLR_mixed_SecOrdMoment_fp + \g_tmpa_fp * \g_tmpb_fp} |
| 649 |
\fp_gset:Nn \g_BBLR_ordinate_SecOrdMoment_fp |
| 650 |
{\g_BBLR_ordinate_SecOrdMoment_fp + \g_tmpb_fp * \g_tmpb_fp} |
| 651 |
\fp_gset:Nn \g_BBLR_min_abscissa_fp |
| 652 |
{min(\g_BBLR_min_abscissa_fp, \g_BBLR_abscissa_fp)} |
| 653 |
\fp_gset:Nn \g_BBLR_min_ordinate_fp |
| 654 |
{min(\g_BBLR_min_ordinate_fp, \g_BBLR_ordinate_fp)} |
| 655 |
\fp_gset:Nn \g_BBLR_max_abscissa_fp |
| 656 |
{max(\g_BBLR_max_abscissa_fp, \g_BBLR_abscissa_fp)} |
| 657 |
\fp_gset:Nn \g_BBLR_max_ordinate_fp |
| 658 |
{max(\g_BBLR_max_ordinate_fp, \g_BBLR_ordinate_fp)} |
| 659 |
} |
| 660 |
\ior_close:N \g_BBLR_file_ior |
| 661 |
\fp_gset:Nn \g_BBLR_abscissa_SecOrdMoment_fp |
| 662 |
{\g_BBLR_abscissa_SecOrdMoment_fp / \g_BBLR_number_of_points_int} |
| 663 |
\fp_gset:Nn \g_BBLR_mixed_SecOrdMoment_fp |
| 664 |
{\g_BBLR_mixed_SecOrdMoment_fp / \g_BBLR_number_of_points_int} |
| 665 |
\fp_gset:Nn \g_BBLR_ordinate_SecOrdMoment_fp |
| 666 |
{\g_BBLR_ordinate_SecOrdMoment_fp / \g_BBLR_number_of_points_int} |
| 667 |
\fp_gset:Nn \g_BBLR_Dabscissa_fp |
| 668 |
{\g_BBLR_max_abscissa_fp - \g_BBLR_min_abscissa_fp } |
| 669 |
\fp_gset:Nn \g_BBLR_Dordinate_fp |
| 670 |
{\g_BBLR_max_ordinate_fp - \g_BBLR_min_ordinate_fp } |
| 671 |
% \end{macrocode} |
| 672 |
% A single pass algorithm exists, but it is numerically less stable. |
| 673 |
% |
| 674 |
% \subsubsection{Classical linear regression \label{classical}} |
| 675 |
% A line in the plane is described by the equation |
| 676 |
% \begin{equation} y_2=ay_1+b \label{eqab} \end{equation} |
| 677 |
% that contains the parameters $a$ and $b$. |
| 678 |
% For each point it is possible to define a distance or a discrepancy |
| 679 |
% of the experimental data with respect to the model. |
| 680 |
% In the given problem the second coordinate is much more affected by |
| 681 |
% errors than the first coordinate. It is therefore reasonable |
| 682 |
% to define the approximation error of each point as |
| 683 |
% \begin{equation} e_\ipoint=y_{2\ipoint}-ay_{1\ipoint}-b |
| 684 |
% \label{e}\end{equation} |
| 685 |
% i.e.\ the difference between the empirical value $y_{2\ipoint}$ |
| 686 |
% and its model counterpart $ay_{1\ipoint}+b$. |
| 687 |
% This leads to the already mentioned classical linear regression. In fact the |
| 688 |
% absolute value of $e_\ipoint$ expressed in (\ref{e}) is the lenght |
| 689 |
% of the segments shown in the leftmost scheeme of figure (\ref{fig:criteria}). |
| 690 |
% The global discrepancy between the data and the model is measured by the |
| 691 |
% least square objective function defined by: |
| 692 |
% \begin{equation} \psi=\pointsum e_\ipoint^2 \label{psiab} \end{equation} |
| 693 |
% and the parameters $a$ and $b$ will be determined |
| 694 |
% just by the minimization of the function $\psi$ defined in \reff{psiab}. |
| 695 |
% \par |
| 696 |
% In the present treatment of the regression problem as a pure |
| 697 |
% approximation problem the definition of $\psi$ in \reff{psiab} |
| 698 |
% seams quite arbitrary. It is anyway a convenient choice. |
| 699 |
% \par |
| 700 |
% Expression \reff{e} can be rewritten in the different form |
| 701 |
% \begin{equation} |
| 702 |
% e_\ipoint=v_{2\ipoint}-av_{1\ipoint}+\barycenter{y}_2-a\barycenter{y}_1-b |
| 703 |
% \label{e2}\end{equation} |
| 704 |
% so that the function to be minimized can be expressed |
| 705 |
% as the sum of two quadratic functions: |
| 706 |
% \begin{equation} |
| 707 |
% \psi= |
| 708 |
% \pointsum (v_{2\ipoint}-av_{1\ipoint})^2+ |
| 709 |
% m(\barycenter{y}_2-a\barycenter{y}_1-b)^2 |
| 710 |
% \label{psiab2} \end{equation} |
| 711 |
% and the minimum can be attained considering |
| 712 |
% the two terms one at a time. |
| 713 |
% The second term in the right-hand side of \reff{psiab2} |
| 714 |
% vanishes if the choice of $b$ is: |
| 715 |
% \begin{equation} b=\barycenter{y}_2-a\barycenter{y}_1. |
| 716 |
% \label{estb} \end{equation} |
| 717 |
% The first term in the right-hand side of \reff{psiab2} becomes: |
| 718 |
% \begin{equation} \psi_{(a)}=m\left(\mc_{22}-2a\mc_{12}+a^2\mc_{11}\right). |
| 719 |
% \label{parabola} \end{equation} |
| 720 |
% Searching the minimum of $\psi$ w.r.t.\ $a$ is therefore the search |
| 721 |
% of the abscissa of the vertex of a parabola |
| 722 |
% with axis parallel to the second coordinated axis. |
| 723 |
% The result is: |
| 724 |
% \begin{equation} a=\mc_{12}/\mc_{11} |
| 725 |
% \label{esta} \end{equation} |
| 726 |
% Now the slope $a$ and the intercept $b$ can be actually computed. |
| 727 |
% \begin{macrocode} |
| 728 |
\fp_gset:Nn \g_BBLR_slope_A_fp |
| 729 |
{\g_BBLR_mixed_SecOrdMoment_fp / \g_BBLR_abscissa_SecOrdMoment_fp } |
| 730 |
\fp_gset:Nn \g_BBLR_intercept_A_fp |
| 731 |
{\g_BBLR_mean_ordinate_fp - \g_BBLR_slope_A_fp * \g_BBLR_mean_abscissa_fp} |
| 732 |
% \end{macrocode} |
| 733 |
% \par |
| 734 |
% The empirical data and the estimated values of $a$ and $b$ |
| 735 |
% can be used to compute |
| 736 |
% the value actually attained by the residuals $e_\ipoint$ and |
| 737 |
% by the function $\psi$. Then the index |
| 738 |
% \begin{equation} \hat\sigma_0^2=\psi/(m-2)\end{equation} |
| 739 |
% can be used to evaluate the general quality of the data and of the model. |
| 740 |
% This claim is clearly quite generic. A complete understanding |
| 741 |
% of this evaluation would require to treat the linear regression |
| 742 |
% problem in the framework of the probabilistic estimation theory. |
| 743 |
% The used notation is derived from that theory.\par |
| 744 |
% If the role of the two coordinates is exchanged the result |
| 745 |
% for $a$ becomes (still with reference to \reff{eqab}) |
| 746 |
% \begin{equation} a=\mc_{22}/\mc_{12}.\end{equation} |
| 747 |
% The slope and the intercept can be computed accordingly. |
| 748 |
% \begin{macrocode} |
| 749 |
\fp_gset:Nn \g_BBLR_slope_B_fp |
| 750 |
{\g_BBLR_ordinate_SecOrdMoment_fp / \g_BBLR_mixed_SecOrdMoment_fp} |
| 751 |
\fp_gset:Nn \g_BBLR_intercept_B_fp |
| 752 |
{\g_BBLR_mean_ordinate_fp - \g_BBLR_slope_B_fp * \g_BBLR_mean_abscissa_fp} |
| 753 |
% \end{macrocode} |
| 754 |
% |
| 755 |
% \subsubsection{Symmetric linear regression \label{symmetric}} |
| 756 |
% If both the coordinates of the experimental points are affected |
| 757 |
% by the same uncertainty it is advisable to use a more symmetric |
| 758 |
% optimality criterion and it is convenient to use a different model equation. |
| 759 |
% \par |
| 760 |
% The same line can be described by a different equation, i.e.\ |
| 761 |
% \begin{equation} x_1y_1+x_2y_2=\dor \end{equation} |
| 762 |
% or in vector form: |
| 763 |
% \begin{equation} \coeff\trasp\point=\dor. \label{eqvx} \end{equation} |
| 764 |
% The parameters in \reff{eqvx} |
| 765 |
% are the scalar $\dor$ and the elements |
| 766 |
% of the vector $\coeff$, i.e.\ $x_1$ and $x_2$. |
| 767 |
% The line described by \reff{eqvx} is obviously |
| 768 |
% invariant when the three parameters are simultaneously |
| 769 |
% scaled by a constant. The normalization condition |
| 770 |
% \begin{equation} \coeff\trasp\coeff=1, \label{norm} \end{equation} |
| 771 |
% supplemented by $\dor\ge 0$, |
| 772 |
% is quite convenient because the parameters will assume |
| 773 |
% a significant geometrical meaning: |
| 774 |
% $\coeff$ is a unit vector orthogonal to the line and $\dor$ |
| 775 |
% is the distance of the line from the origin. |
| 776 |
% The expression |
| 777 |
% \begin{equation} d=\dor-\coeff\trasp\point \label{distance} \end{equation} |
| 778 |
% is the distance of the generic point $\point$ from the line |
| 779 |
% with a sign that is positive for points on the same side of the origin. |
| 780 |
% \par |
| 781 |
% The distance of each given point from the desired optimal line |
| 782 |
% is denoted by $d_\ipoint$. |
| 783 |
% It has a clear intrinsic geometrical meaning and it does not |
| 784 |
% privileges one coordinate w.r.t.\ the other. |
| 785 |
% The function to be minimized by the optimal line is |
| 786 |
% \begin{equation} |
| 787 |
% \phi=\frac{1}{m}\pointsum d_\ipoint^2. \label{phi1} \end{equation} |
| 788 |
% The parameters of \reff{eqvx} are determined by the minimization |
| 789 |
% of the function $\phi$ that can be expressed as: |
| 790 |
% \begin{equation} |
| 791 |
% \phi=\frac{1}{m}\pointsum (\coeff\trasp\point_{\ipoint}-\dor)^{2} |
| 792 |
% \label{phi2} \end{equation} |
| 793 |
% and then, after some algebraic manipulations: |
| 794 |
% \begin{equation} |
| 795 |
% \phi=\coeff\trasp\mC\coeff+(\dor-\coeff\trasp\barycenter{\point})^2 |
| 796 |
% \label{phi3}. \end{equation} |
| 797 |
% The function $\phi$ is composed (as it was the function $\psi$) by the sum |
| 798 |
% of two parts. The second term in the right-hand side of \reff{phi3} |
| 799 |
% vanishes if the choice of $\dor$ is: |
| 800 |
% \begin{equation} \dor=\coeff\trasp\barycenter{\point}. |
| 801 |
% \label{estd} \end{equation} |
| 802 |
% Both (\ref{estb}) and (\ref{estd}) means that the optimal line includes |
| 803 |
% the mean point. |
| 804 |
% Then it is necessary to minimize the function |
| 805 |
% \begin{equation} \phi_{(\coeff)} = \coeff\trasp\mC\coeff |
| 806 |
% \label{quadraticfun} \end{equation} |
| 807 |
% with the constrain $\coeff\trasp\coeff=1$. |
| 808 |
% It can be proved that the function $\phi_{(\coeff)}$ |
| 809 |
% is stationary if $\coeff$ is an eigenvector of $\mC$. \par |
| 810 |
% The function $\phi_{(\coeff)}$ and the constrain must be combined |
| 811 |
% using a Lagrange multiplier: |
| 812 |
% \begin{equation} |
| 813 |
% \Phi= \coeff\trasp\mC\coeff+\lambda(1-\coeff\trasp\coeff). |
| 814 |
% \label{Phi} \end{equation} |
| 815 |
% Then the stationarity points of $\Phi$ must be determined. |
| 816 |
% Equating to zero the derivatives of $\Phi$ gives |
| 817 |
% \begin{equation} |
| 818 |
% \mC\coeff=\lambda\coeff |
| 819 |
% \label{auto} \end{equation} |
| 820 |
% i.e.\ $\coeff$ is an eigenvector of $\mC$. \par |
| 821 |
% The same result is obtained with the following argument. |
| 822 |
% The function $\phi_{(\coeff)}$ is stationary if its first variation |
| 823 |
% is zero. The variation of $\coeff$ is named $\dx$ . |
| 824 |
% It must respect the constrain, that in linearized form becomes $\dx\trasp\coeff=0$. |
| 825 |
% The first variation of $\phi_{(\coeff)}$ is $2\dx\trasp\mC\coeff$, |
| 826 |
% and it is zero if and only if the following implication is valid: |
| 827 |
% $\dx\trasp\coeff=0 \implies \dx\trasp\mC\coeff=0$, |
| 828 |
% and the implication is valid if and only if the vector |
| 829 |
% $\mC\coeff$ has the same direction of $\coeff$, i.e.\ if |
| 830 |
% $\coeff$ is an eigenvector of $\mC$. |
| 831 |
% \par |
| 832 |
% The result on the optimal line |
| 833 |
% can be described geometrically in the following way: |
| 834 |
% (i) the optimal line includes the barycenter of the data; |
| 835 |
% (ii) the optimal line is orthogonal to the eigenvector of |
| 836 |
% $\mC$ corresponding to the minimum eigenvalue .\par |
| 837 |
% The obtained result can be generalized in $\Renne$. |
| 838 |
% A set of points in $\Renne$ must be approximated by an $(n-1)$-dimensional |
| 839 |
% affine subspace. (Other more general situations can be considered.) |
| 840 |
% \par |
| 841 |
% The trace of the matrix $\mC$, denoted as $\tr(\mC)$, is a measure of the |
| 842 |
% global dispersion of the set of points. |
| 843 |
% The minimum eigenvalue $\lambda_{\textrm{min}}$ of $\mC$ is a measure |
| 844 |
% of the dispersion of the set of points with |
| 845 |
% respect to the optimal affine subspace. Therefore the index |
| 846 |
% \begin{equation} \frac{n\lambda_{\textrm{min}}}{\tr(\mC)} |
| 847 |
% \end{equation} |
| 848 |
% can be used as an indicator of the relative residual |
| 849 |
% dispersion of the data around the optimal affine subspace. |
| 850 |
% The defined index is dimensionless and it is |
| 851 |
% always between $0$ and $1$. |
| 852 |
% \par |
| 853 |
% For the actual computation of $\coeff$ it is convenient to consider |
| 854 |
% the spectral factorization of the matrix $\mC$, i.e.\ |
| 855 |
% $\mC=\mX\mL\mX\trasp$ where $\mL$ is a diagonal matrix |
| 856 |
% whose diagonal elements are the eigenvalues of $\mC$ |
| 857 |
% and $\mX$ is an orthonormal matrix whose columns are |
| 858 |
% the eigenvectors of $\mC$. The spectral factorization exists |
| 859 |
% for any symmetric matrix, but it is specially simple for |
| 860 |
% a $2\times 2$ matrix. |
| 861 |
% \begin{equation} |
| 862 |
% \matrixtwotwo{\mc_{11}}{\mc_{12}}{\mc_{12}}{\mc_{22}}= |
| 863 |
% \matrixtwotwo{c}{-s}{s}{\spm c} |
| 864 |
% \matrixtwotwo{\lambda_1}{0}{0}{\lambda_2} |
| 865 |
% \matrixtwotwo{\spm c}{s}{-s}{c} |
| 866 |
% \label{spectral}\end{equation} |
| 867 |
% The eigenvalues can be easily obtained because |
| 868 |
% their sum is the trace of $\mC$ |
| 869 |
% \begin{equation} |
| 870 |
% \lambda_1 + \lambda_2 = \mc_{11}+\mc_{22} |
| 871 |
% \label{Sum}\end{equation} |
| 872 |
% and their product |
| 873 |
% is the determinant of the same matrix. |
| 874 |
% Therefore after some manipulations it results: |
| 875 |
% \begin{equation} |
| 876 |
% \lambda_1 - \lambda_2 = \sqrt{(\mc_{11}-\mc_{22})^2+4\mc_{12}^2} |
| 877 |
% \label{Difference}\end{equation} |
| 878 |
% and the two eigenvalues are then immediately obtained. \par |
| 879 |
% It is convenient to compute the difference of the two diagonal elements |
| 880 |
% of the dispersion matrix and the difference of its eigenvalues. |
| 881 |
% \begin{macrocode} |
| 882 |
\fp_gset:Nn \g_BBLR_diag_diff_fp |
| 883 |
{\g_BBLR_abscissa_SecOrdMoment_fp - \g_BBLR_ordinate_SecOrdMoment_fp} |
| 884 |
\fp_gset:Nn \g_BBLR_eig_diff_fp |
| 885 |
{sqrt(\g_BBLR_diag_diff_fp * \g_BBLR_diag_diff_fp + |
| 886 |
4 * \g_BBLR_mixed_SecOrdMoment_fp * \g_BBLR_mixed_SecOrdMoment_fp)} |
| 887 |
% \end{macrocode} |
| 888 |
% The computation of $c$ and $s$ is obtained from \reff{spectral} |
| 889 |
% taking into account that $c^2+s^2=1$. |
| 890 |
% From \reff{spectral} it results: |
| 891 |
% \begin{equation} \mc_{11}-\mc_{22}=(\lambda_1-\lambda_2)(c^2-s^2) |
| 892 |
% \label{Cos2A}\end{equation} |
| 893 |
% and also |
| 894 |
% \begin{equation} \mc_{12}=(\lambda_1-\lambda_2)cs |
| 895 |
% \label{Sin2A}\end{equation} |
| 896 |
% that is only used to determine the sign of $cs$. |
| 897 |
% The expression for the parameters $c$ and $s$ are: |
| 898 |
% \begin{equation} |
| 899 |
% c=\sqrt{\frac{1}{2}+\frac{\mc_{11}-\mc_{22}}{2(\lambda_1-\lambda_2)}} |
| 900 |
% \label{cos}\end{equation} |
| 901 |
% \begin{equation} |
| 902 |
% s=\sgn(\mc_{12}) |
| 903 |
% \sqrt{\frac{1}{2}-\frac{\mc_{11}-\mc_{22}}{2(\lambda_1-\lambda_2)}} |
| 904 |
% \label{sin}\end{equation} |
| 905 |
% The parameters $s$ and $c$ are the sine and cosine |
| 906 |
% of the angle between the axis of $y_1$ and the eigenvector |
| 907 |
% corresponding to the maximum eigenvalue. \par |
| 908 |
% They are computed using the already defined elements. |
| 909 |
% \begin{macrocode} |
| 910 |
\fp_gset:Nn \g_BBLR_cos_fp% |
| 911 |
{sqrt((1 + \g_BBLR_diag_diff_fp / \g_BBLR_eig_diff_fp) / 2)} |
| 912 |
\fp_gset:Nn \g_BBLR_sig_sin_fp {\fp_sign:n {\g_BBLR_mixed_SecOrdMoment_fp}} |
| 913 |
\fp_gset:Nn \g_BBLR_sin_fp |
| 914 |
{\g_BBLR_sig_sin_fp*sqrt((1-\g_BBLR_diag_diff_fp / \g_BBLR_eig_diff_fp) / 2)} |
| 915 |
% \end{macrocode} |
| 916 |
% The vector $\coeff$ is : |
| 917 |
% \begin{equation} |
| 918 |
% \coeff=\sgn(-s\barycenter{y}_1+c\barycenter{y}_2) |
| 919 |
% \begin{pmatrix} -s \\ c\end{pmatrix}. |
| 920 |
% \label{xhat}\end{equation} |
| 921 |
% |
| 922 |
%\par |
| 923 |
% The parameter $a$ of model \reff{eqab} can be obtained as: |
| 924 |
% \begin{equation} |
| 925 |
% a=s/c |
| 926 |
% \end{equation} |
| 927 |
% Now the slope and the intercept of the optimal line corresponding to the |
| 928 |
% symmetric criterion can be computed. |
| 929 |
% |
| 930 |
% \begin{macrocode} |
| 931 |
\fp_gset:Nn \g_BBLR_slope_S_fp |
| 932 |
{\g_BBLR_sin_fp / \g_BBLR_cos_fp } |
| 933 |
\fp_gset:Nn \g_BBLR_intercept_S_fp |
| 934 |
{\g_BBLR_mean_ordinate_fp - |
| 935 |
\g_BBLR_slope_S_fp * \g_BBLR_mean_abscissa_fp} |
| 936 |
} |
| 937 |
% \end{macrocode} |
| 938 |
% |
| 939 |
% The theoretical treatment of the proposed problem and the |
| 940 |
% implementation of its numerical solution end here. |
| 941 |
% \end{macro} |
| 942 |
% |
| 943 |
% \subsection{Print of table of results} |
| 944 |
% \begin{macro}{\lrprint} |
| 945 |
% The command \cs{lrprint} prints some info on the data |
| 946 |
% and the results of the computations in tabular form. |
| 947 |
% \begin{macrocode} |
| 948 |
\NewDocumentCommand{\lrprint}{}{ |
| 949 |
\begin{center} |
| 950 |
\begin{tabular}{| l | r |} \hline |
| 951 |
Data~File & \g_BBLR_file_name_tl \\ |
| 952 |
\hline |
| 953 |
Number~of~points & \int_use:N\g_BBLR_number_of_points_int \\ |
| 954 |
\hline |
| 955 |
Mean~values~of~the~coordinates &% |
| 956 |
$\BBLR_print_fp:N \g_BBLR_mean_abscissa_fp$ \\ & |
| 957 |
$\BBLR_print_fp:N \g_BBLR_mean_ordinate_fp$ \\ \hline |
| 958 |
Minimum~values~of~the~coordinates &% |
| 959 |
$\BBLR_print_fp:N \g_BBLR_min_abscissa_fp$ \\ & |
| 960 |
$\BBLR_print_fp:N \g_BBLR_min_ordinate_fp$ \\ \hline |
| 961 |
Maximum~values~of~the~coordinates &% |
| 962 |
$\BBLR_print_fp:N \g_BBLR_max_abscissa_fp$ \\ & |
| 963 |
$\BBLR_print_fp:N \g_BBLR_max_ordinate_fp$ \\ \hline |
| 964 |
{Second~order~moments}\phantom{xxxxxxxxx}{abscissa} &% |
| 965 |
$\BBLR_print_fp:N \g_BBLR_abscissa_SecOrdMoment_fp$ \\ |
| 966 |
\multicolumn{1}{|r|}{mixed} & % |
| 967 |
$\BBLR_print_fp:N \g_BBLR_mixed_SecOrdMoment_fp$ ~ \\ |
| 968 |
\multicolumn{1}{|r|}{ordinate} & % |
| 969 |
$\BBLR_print_fp:N \g_BBLR_ordinate_SecOrdMoment_fp$ \\ \hline |
| 970 |
Slope~and~intercept~of~optimal~line & $\BBLR_print_fp:N |
| 971 |
\g_BBLR_slope_A_fp$ \\ |
| 972 |
(estimated~with~errors~in~ordinate)&$\BBLR_print_fp:N |
| 973 |
\g_BBLR_intercept_A_fp$\\ \hline |
| 974 |
Slope~and~intercept~of~optimal~line & $\BBLR_print_fp:N |
| 975 |
\g_BBLR_slope_B_fp$ \\ |
| 976 |
(estimated~with~errors~in~abscissa)&$\BBLR_print_fp:N |
| 977 |
\g_BBLR_intercept_B_fp$\\ \hline |
| 978 |
Unit~vector~along~the~line & $\BBLR_print_fp:N |
| 979 |
\g_BBLR_cos_fp$ \\ |
| 980 |
& $\BBLR_print_fp:N \g_BBLR_sin_fp$ \\ |
| 981 |
Slope~and~intercept~of~optimal~line &$\BBLR_print_fp:N |
| 982 |
\g_BBLR_slope_S_fp$ \\ |
| 983 |
(estimated~with~symmetric~regression) & |
| 984 |
$\BBLR_print_fp:N \g_BBLR_intercept_S_fp$\\ \hline |
| 985 |
\end{tabular} |
| 986 |
\end{center} |
| 987 |
} |
| 988 |
% \end{macrocode} |
| 989 |
% \end{macro} |
| 990 |
% |
| 991 |
% \begin{macro}{\lrnumdigit} |
| 992 |
% The command \cs{lrnumdigit} records the maximum number of digits |
| 993 |
% used in the table for computed numbers. The default value is $4$. |
| 994 |
% \begin{macrocode} |
| 995 |
\NewDocumentCommand{\lrnumdigit}{m}{ |
| 996 |
\int_gset:Nn \g_BBLR_num_dec_dig_int{#1} |
| 997 |
} |
| 998 |
% \end{macrocode} |
| 999 |
% \end{macro} |
| 1000 |
% |
| 1001 |
% \subsection{Plot of points and lines} |
| 1002 |
% \begin{macro}{\lrplot} |
| 1003 |
% The command \cs{lrplot} produce a framed plot of the data |
| 1004 |
% and of the regression line(s). The size of the plot and its actual |
| 1005 |
% content are determined by the arguments. |
| 1006 |
% \begin{macrocode} |
| 1007 |
\NewDocumentCommand{\lrplot}{mmmmm}{% |
| 1008 |
% \end{macrocode} |
| 1009 |
% The plotting area is divided into a main plotting area for |
| 1010 |
% the representation of points and line(s) and a small surrounding free space. |
| 1011 |
% The height is computed taking into account the distribution of the points, |
| 1012 |
% if it is not explicitelly given. |
| 1013 |
% \begin{macrocode} |
| 1014 |
\fp_gset:Nn \g_BBLR_base_fp {#1} |
| 1015 |
\fp_gset:Nn \g_BBLR_Xbase_fp {\g_BBLR_base_fp - 0.6} |
| 1016 |
\fp_gset:Nn \g_BBLR_scale_factor_fp{\g_BBLR_Xbase_fp / |
| 1017 |
\g_BBLR_Dabscissa_fp} |
| 1018 |
\bool_if:NTF \g_BBLR_two_scale_bool |
| 1019 |
{\fp_gset:Nn \g_BBLR_height_fp {\g_BBLR_XXheight_fp} |
| 1020 |
\fp_gset:Nn \g_BBLR_Xheight_fp {\g_BBLR_height_fp - 0.6} |
| 1021 |
\fp_gset:Nn \g_BBLR_scale_factor_H_fp{\g_BBLR_Xheight_fp / |
| 1022 |
\g_BBLR_Dordinate_fp} |
| 1023 |
} |
| 1024 |
{\fp_gset:Nn \g_BBLR_Xheight_fp {\g_BBLR_Dordinate_fp * |
| 1025 |
\g_BBLR_scale_factor_fp} |
| 1026 |
\fp_gset:Nn \g_BBLR_height_fp {\g_BBLR_Xheight_fp + 0.6} |
| 1027 |
\fp_gset:Nn \g_BBLR_scale_factor_H_fp {\g_BBLR_scale_factor_fp } |
| 1028 |
} |
| 1029 |
% \end{macrocode} |
| 1030 |
% The information about the items to be plotted is in the remaining arguments. |
| 1031 |
% \begin{macrocode} |
| 1032 |
\str_gset:Nn \g_BBLR_point_code_str {#2} |
| 1033 |
\str_gset:Nn \g_BBLR_labelA_str {#3} |
| 1034 |
\str_gset:Nn \g_BBLR_labelB_str {#4} |
| 1035 |
\str_gset:Nn \g_BBLR_labelS_str {#5} |
| 1036 |
\bool_gset:Nn \g_BBLR_plot_points_bool |
| 1037 |
{!(\str_if_eq_p:NN \g_BBLR_point_code_str \c_BBLR_minus_str)} |
| 1038 |
\bool_gset:Nn \g_BBLR_plot_lineA_bool |
| 1039 |
{!(\str_if_eq_p:NN \g_BBLR_labelA_str \c_BBLR_minus_str)} |
| 1040 |
\bool_gset:Nn \g_BBLR_plot_lineB_bool |
| 1041 |
{!(\str_if_eq_p:NN \g_BBLR_labelB_str \c_BBLR_minus_str)} |
| 1042 |
\bool_gset:Nn \g_BBLR_plot_lineS_bool |
| 1043 |
{!(\str_if_eq_p:NN \g_BBLR_labelS_str \c_BBLR_minus_str)} |
| 1044 |
% \end{macrocode} |
| 1045 |
% The unit of length is $1$ centimeter. The plotting area is framed. |
| 1046 |
% \begin{macrocode} |
| 1047 |
\setlength{\unitlength}{1.0cm} |
| 1048 |
\fp_gset:Nn \g_tmpa_fp {\g_BBLR_Xbase_fp +0.2} |
| 1049 |
\fp_gset:Nn \g_tmpb_fp {\g_BBLR_Xheight_fp +0.1} |
| 1050 |
\begin{picture}(\fp_use:N\g_BBLR_base_fp,\fp_use:N\g_BBLR_height_fp)(-0.3,-0.3) |
| 1051 |
\put(-0.1,-0.1){\line(1,0){\fp_use:N\g_tmpa_fp}} |
| 1052 |
\put(-0.1,\fp_use:N\g_tmpb_fp){\line(1,0){\fp_use:N\g_tmpa_fp}} |
| 1053 |
\fp_gset:Nn \g_tmpa_fp {\g_tmpa_fp -0.1} |
| 1054 |
\fp_gset:Nn \g_tmpb_fp {\g_tmpb_fp +0.1} |
| 1055 |
\put(-0.1,-0.1){\line(0,1){\fp_use:N\g_tmpb_fp}} |
| 1056 |
\put(\fp_use:N\g_tmpa_fp,-0.1){\line(0,1){\fp_use:N\g_tmpb_fp}} |
| 1057 |
% \end{macrocode} |
| 1058 |
% The plot of points and line(s) is obtained using auxiliary functions. |
| 1059 |
% \begin{macrocode} |
| 1060 |
\thicklines |
| 1061 |
\bool_if:nT {\g_BBLR_plot_points_bool}{\BBLR_plot_points:} |
| 1062 |
\bool_if:nT {\g_BBLR_plot_lineA_bool}{ |
| 1063 |
\BBLR_draw_line:NNN \g_BBLR_slope_A_fp\g_BBLR_intercept_A_fp |
| 1064 |
\g_BBLR_labelA_str} |
| 1065 |
\bool_if:nT {\g_BBLR_plot_lineB_bool}{ |
| 1066 |
\BBLR_draw_line:NNN \g_BBLR_slope_B_fp\g_BBLR_intercept_B_fp |
| 1067 |
\g_BBLR_labelB_str} |
| 1068 |
\bool_if:nT {\g_BBLR_plot_lineS_bool}{ |
| 1069 |
\BBLR_draw_line:NNN \g_BBLR_slope_S_fp\g_BBLR_intercept_S_fp |
| 1070 |
\g_BBLR_labelS_str} |
| 1071 |
\end{picture} |
| 1072 |
}% |
| 1073 |
% \end{macrocode} |
| 1074 |
% \end{macro} |
| 1075 |
% \begin{macro}{\lrplotparameters} |
| 1076 |
% The command \cs{lrplotparameters} records two parameters related to |
| 1077 |
% the generation of the plot and sets a boolean variable. |
| 1078 |
% \begin{macrocode} |
| 1079 |
\NewDocumentCommand{\lrplotparameters}{mm}{ |
| 1080 |
\fp_gset:Nn \g_BBLR_diameter_fp{#1} |
| 1081 |
\fp_gset:Nn \g_BBLR_XXheight_fp{#2} |
| 1082 |
\bool_gset:Nn \g_BBLR_two_scale_bool |
| 1083 |
{\fp_compare_p:nNn {\g_BBLR_XXheight_fp}>{0.}} |
| 1084 |
} |
| 1085 |
% \end{macrocode} |
| 1086 |
% \end{macro} |
| 1087 |
% |
| 1088 |
% \subsection{Functions for internal use} |
| 1089 |
% The functions listed here after are for internal |
| 1090 |
% use and are just minimally documented. \par |
| 1091 |
% The function |\BBLR_decode_data:| |
| 1092 |
% \marginpar{\raggedleft\texttt{ |
| 1093 |
% \textbackslash{}BBLR\textunderscore{}decode\textunderscore{}data:}} |
| 1094 |
% extract two numeric values from the string read from the file. |
| 1095 |
% Some clever actions are necessary because |
| 1096 |
% a so called csv file sometime do not contains the separating commas. |
| 1097 |
% \begin{macrocode} |
| 1098 |
\cs_new_protected:Nn \BBLR_decode_data: { |
| 1099 |
\tl_trim_spaces:N \g_BBLR_file_line_tl |
| 1100 |
\int_gzero:N \g_tmpa_int |
| 1101 |
\int_gzero:N \g_BBLR_first_separator_int |
| 1102 |
\int_gzero:N \g_BBLR_last_separator_int |
| 1103 |
\int_gset:Nn \g_BBLR_record_length_int { |
| 1104 |
\str_count:N \g_BBLR_file_line_tl} |
| 1105 |
\str_map_variable:NNn \g_BBLR_file_line_tl \g_tmpa_str { |
| 1106 |
\int_gincr:N \g_tmpa_int |
| 1107 |
\bool_lazy_or:nnTF |
| 1108 |
{\str_if_eq_p:NN \g_tmpa_str \c_BBLR_comma_str} |
| 1109 |
{\str_if_eq_p:NN \g_tmpa_str \c_BBLR_space_str} |
| 1110 |
{\int_gset_eq:NN \g_BBLR_last_separator_int \g_tmpa_int |
| 1111 |
\int_if_zero:nTF {\g_BBLR_first_separator_int} |
| 1112 |
{\int_gset_eq:NN \g_BBLR_first_separator_int \g_tmpa_int |
| 1113 |
}{\prg_do_nothing:} |
| 1114 |
}{\prg_do_nothing:} |
| 1115 |
} |
| 1116 |
\int_gincr:N \g_BBLR_last_separator_int |
| 1117 |
\int_gdecr:N \g_BBLR_first_separator_int |
| 1118 |
\fp_gset:Nn \g_BBLR_abscissa_fp{ |
| 1119 |
\str_range:Nnn \g_BBLR_file_line_tl{1}{\g_BBLR_first_separator_int}} |
| 1120 |
\fp_gset:Nn \g_BBLR_ordinate_fp{ |
| 1121 |
\str_range:Nnn \g_BBLR_file_line_tl |
| 1122 |
{\g_BBLR_last_separator_int}{\g_BBLR_record_length_int}} |
| 1123 |
} |
| 1124 |
% \end{macrocode} |
| 1125 |
% The function |\BBLR_plot_points:| \marginpar{\raggedleft\texttt{ |
| 1126 |
% \textbackslash{}BBLR\textunderscore{}plot\textunderscore{}points:}} |
| 1127 |
% scans the data file to read the coordinates and |
| 1128 |
% it draws a circle for each point. |
| 1129 |
% |
| 1130 |
% \begin{macrocode} |
| 1131 |
\cs_new_protected:Nn \BBLR_plot_points: { |
| 1132 |
\ior_open:Nn \g_BBLR_file_ior \g_BBLR_file_name_tl |
| 1133 |
\int_zero:N \g_BBLR_rec_count_int |
| 1134 |
\int_do_until:nn |
| 1135 |
{\g_BBLR_rec_count_int = \g_BBLR_number_of_points_int} |
| 1136 |
{ |
| 1137 |
\ior_str_get:NN \g_BBLR_file_ior \g_BBLR_file_line_tl |
| 1138 |
\int_incr:N \g_BBLR_rec_count_int |
| 1139 |
\BBLR_decode_data: |
| 1140 |
\fp_gset:Nn \g_tmpa_fp{(\g_BBLR_abscissa_fp-\g_BBLR_min_abscissa_fp)* |
| 1141 |
\g_BBLR_scale_factor_fp} |
| 1142 |
\fp_gset:Nn \g_tmpb_fp{(\g_BBLR_ordinate_fp-\g_BBLR_min_ordinate_fp)* |
| 1143 |
\g_BBLR_scale_factor_H_fp} |
| 1144 |
\put(\fp_use:N\g_tmpa_fp, \fp_use:N\g_tmpb_fp){ |
| 1145 |
{\circle*{\fp_use:N\g_BBLR_diameter_fp}}} |
| 1146 |
} |
| 1147 |
\ior_close:N \g_BBLR_file_ior |
| 1148 |
} |
| 1149 |
% \end{macrocode} |
| 1150 |
% The function |\BBLR_draw_line:NNN| \marginpar{\raggedleft\texttt{ |
| 1151 |
% \textbackslash{}BBLR\textunderscore{}draw\textunderscore{}line:NNN}} |
| 1152 |
% draws the line. The first two parameters given as arguments |
| 1153 |
% are the slope and the intercept. The third parameter is a label. |
| 1154 |
% \par The next code finds the intersection of the line with the plotting area. |
| 1155 |
% \begin{macrocode} |
| 1156 |
\cs_new_protected:Nn \BBLR_draw_line:NNN { |
| 1157 |
\fp_gset:Nn \fp_tmpa_fp {#1 * \g_BBLR_min_abscissa_fp + #2 } |
| 1158 |
\fp_compare:nTF{\fp_tmpa_fp > \g_BBLR_max_ordinate_fp}{ |
| 1159 |
\fp_gset:Nn \g_BBLR_min_draw_abscissa_fp {(\g_BBLR_max_ordinate_fp -#2) / #1} |
| 1160 |
}{ |
| 1161 |
\fp_compare:nTF{\fp_tmpa_fp < \g_BBLR_min_ordinate_fp}{ |
| 1162 |
\fp_gset:Nn \g_BBLR_min_draw_abscissa_fp {(\g_BBLR_min_ordinate_fp - #2) / #1} |
| 1163 |
}{ |
| 1164 |
\fp_gset:Nn \g_BBLR_min_draw_abscissa_fp { \g_BBLR_min_abscissa_fp } |
| 1165 |
}} |
| 1166 |
\fp_gset:Nn \fp_tmpa_fp {#1 * \g_BBLR_max_abscissa_fp + #2 } |
| 1167 |
\fp_compare:nTF{\fp_tmpa_fp > \g_BBLR_max_ordinate_fp}{ |
| 1168 |
\fp_gset:Nn \g_BBLR_max_draw_abscissa_fp {(\g_BBLR_max_ordinate_fp -#2) / #1} |
| 1169 |
}{ |
| 1170 |
\fp_compare:nTF{\fp_tmpa_fp < \g_BBLR_min_ordinate_fp}{ |
| 1171 |
\fp_gset:Nn \g_BBLR_max_draw_abscissa_fp { (\g_BBLR_min_ordinate_fp - #2) / #1} |
| 1172 |
}{ |
| 1173 |
\fp_gset:Nn \g_BBLR_max_draw_abscissa_fp { \g_BBLR_max_abscissa_fp } |
| 1174 |
}} |
| 1175 |
% \end{macrocode} |
| 1176 |
% Some parameters (coordinates of starting point, base-length, scaled slope) |
| 1177 |
% are computed and the line is drawn. |
| 1178 |
% \begin{macrocode} |
| 1179 |
\fp_gset:Nn \fp_tmpa_fp {(\g_BBLR_min_draw_abscissa_fp - |
| 1180 |
\g_BBLR_min_abscissa_fp)* \g_BBLR_scale_factor_fp} |
| 1181 |
\fp_gset:Nn \fp_tmpb_fp {(#1 * \g_BBLR_min_draw_abscissa_fp + #2 - |
| 1182 |
\g_BBLR_min_ordinate_fp)* \g_BBLR_scale_factor_H_fp} |
| 1183 |
\fp_gset:Nn \fp_BBLR_line_base_length_fp{(\g_BBLR_max_draw_abscissa_fp - |
| 1184 |
\g_BBLR_min_draw_abscissa_fp) * \g_BBLR_scale_factor_fp} |
| 1185 |
\fp_gset:Nn \fp_scaled_slope_fp |
| 1186 |
{(#1 * \g_BBLR_scale_factor_H_fp / \g_BBLR_scale_factor_fp)} |
| 1187 |
\put(\fp_use:N\fp_tmpa_fp, \fp_use:N\fp_tmpb_fp){ |
| 1188 |
\line(1.,\fp_use:N\fp_scaled_slope_fp){\fp_use:N\fp_BBLR_line_base_length_fp}} |
| 1189 |
% \end{macrocode} |
| 1190 |
%The third parameter is used as a label, if it is not a |+|. |
| 1191 |
% \begin{macrocode} |
| 1192 |
\bool_if:nF {\str_if_eq_p:NN #3 \c_BBLR_plus_str}{ |
| 1193 |
\fp_gset:Nn \fp_tmpa_fp |
| 1194 |
{0.08 * \g_BBLR_min_draw_abscissa_fp + 0.92 * \g_BBLR_max_draw_abscissa_fp} |
| 1195 |
\fp_gset:Nn \fp_tmpb_fp {#1 * \fp_tmpa_fp + #2 } |
| 1196 |
\fp_gset:Nn \fp_tmpa_fp |
| 1197 |
{(\fp_tmpa_fp-\g_BBLR_min_abscissa_fp)*\g_BBLR_scale_factor_fp |
| 1198 |
+ 0.3 * #1 /sqrt(1.+#1*#1)} |
| 1199 |
\fp_gset:Nn \fp_tmpb_fp |
| 1200 |
{(\fp_tmpb_fp-\g_BBLR_min_ordinate_fp)* \g_BBLR_scale_factor_fp |
| 1201 |
- 0.3 /sqrt(1.+#1*#1)} |
| 1202 |
\put(\fp_use:N\fp_tmpa_fp, \fp_use:N\fp_tmpb_fp){#3} |
| 1203 |
} |
| 1204 |
} |
| 1205 |
% \end{macrocode} |
| 1206 |
% The function |\BBLR_print_fp:N | \marginpar{\raggedleft\texttt{ |
| 1207 |
% \textbackslash{}BBLR\textunderscore{}print\textunderscore{}fp:N }} |
| 1208 |
% is used to print floating point numbers with a limited number |
| 1209 |
% of decimal digit. |
| 1210 |
% |
| 1211 |
% \begin{macrocode} |
| 1212 |
\cs_new_protected:Nn \BBLR_print_fp:N {% |
| 1213 |
{\fp_eval:n{trunc(#1 ,\g_BBLR_num_dec_dig_int)}} |
| 1214 |
} |
| 1215 |
% \end{macrocode} |
| 1216 |
% |
| 1217 |
% \begin{macrocode} |
| 1218 |
\ExplSyntaxOff |
| 1219 |
% \end{macrocode} |
| 1220 |
% |
| 1221 |
% |
| 1222 |
%\iffalse |
| 1223 |
%</package> |
| 1224 |
%\fi |
| 1225 |
% |
| 1226 |
% \section{Versions} |
| 1227 |
% 2024-06-10: first post on CTAN.\par |
| 1228 |
% 2024-11-23: small changes in the text, |
| 1229 |
% different format of numbers in the table.\par |
| 1230 |
% 2024-12-14: more flexibe output, some changes in the text.\par |
| 1231 |
% 2025-11-08: different notation for vectors and matrices, |
| 1232 |
% some changes and additions in the text, updated year of two references, |
| 1233 |
% a significant bug corrected |
| 1234 |
% (variable holding the number of points needs to be globally setted). |
| 1235 |
% |
| 1236 |
% \section{Acknowledgments} |
| 1237 |
% The colleagues Paolo Zatelli, Alfonso Vitti and Giulia Graldi |
| 1238 |
% read some preliminary version |
| 1239 |
% of this text and suggested several improvements. |
| 1240 |
% Claudio Beccari usefully commented about the first version |
| 1241 |
% when I was preparing the second version. \par |
| 1242 |
% |
| 1243 |
% \section{Citations and references} |
| 1244 |
% \subsection*{Mathematics} |
| 1245 |
% The books by Lang \cite{Lang} and by Strang \cite{Strang} give |
| 1246 |
% all the background on linear algebra.\par |
| 1247 |
% The texts by Sansò \cites{Sanso1, Sanso2} (in italian) treat the |
| 1248 |
% teory of probability and its application to metrology. |
| 1249 |
% The paper by Pearson \cite{Pearson} is the oldest text that |
| 1250 |
% I have found on the symmetric regression, or total regression. |
| 1251 |
% \subsection*{Programming} |
| 1252 |
% The two documents \cites{L3A, L3B} are the fountamental and official guide |
| 1253 |
% for \LaTeX3 programming. The books by Donald Knuth \cites{Knuth} |
| 1254 |
% and Leslie Lamport \cites{Lamport} are still essential references. |
| 1255 |
% The papers by Enrico Gregorio \cites{egreg1, egreg2, egreg3, egreg4, egreg5} |
| 1256 |
% explain some general and some special aspect of \LaTeX3 programming. |
| 1257 |
% \subsection*{References} |
| 1258 |
% \begin{biblist}[\normalsize] |
| 1259 |
% \bib{egreg1}{article}{ |
| 1260 |
% author={Gregorio, Enrico}, |
| 1261 |
% journal={ArsTeXnica}, |
| 1262 |
% number={14},pages={41\ndash 47}, date={2012}, |
| 1263 |
% title={\LaTeX3: un nuovo gioco per i maghi e per diventarlo}, |
| 1264 |
% } |
| 1265 |
% \bib{egreg2}{article}{ |
| 1266 |
% author={Gregorio, Enrico}, |
| 1267 |
% journal={ArsTeXnica}, |
| 1268 |
% number={22},pages={69\ndash 77}, date={2016}, |
| 1269 |
% title={Liste, cicli, \LaTeX3}, |
| 1270 |
% } |
| 1271 |
% \bib{egreg3}{article}{ |
| 1272 |
% author={Gregorio, Enrico}, |
| 1273 |
% journal={ArsTeXnica}, |
| 1274 |
% number={24},pages={37\ndash 44}, date={2017}, |
| 1275 |
% title={Condizionali in \LaTeX}, |
| 1276 |
% } |
| 1277 |
% \bib{egreg4}{article}{ |
| 1278 |
% author={Gregorio, Enrico}, |
| 1279 |
% journal={ArsTeXnica}, |
| 1280 |
% number={30},pages={36\ndash 45}, date={2020}, |
| 1281 |
% title={Funzioni e |expl3|}, |
| 1282 |
% } |
| 1283 |
% \bib{egreg5}{article}{ |
| 1284 |
% author={Gregorio, Enrico}, |
| 1285 |
% journal={TUGboat}, |
| 1286 |
% volume={41},number={3},pages={299\ndash 307}, date={2020}, |
| 1287 |
% title={Functions and |expl3|}, |
| 1288 |
% } |
| 1289 |
% \bib{Knuth}{book}{ |
| 1290 |
% author={Knuth, Donald}, |
| 1291 |
% title={The TeXbook}, |
| 1292 |
% date={1986}, |
| 1293 |
% publisher={American Mathematical Society and Addison-Wesley}, |
| 1294 |
% } |
| 1295 |
% \bib{Lang}{book}{ |
| 1296 |
% author={Lang, Serge}, |
| 1297 |
% title={Linear Algebra}, |
| 1298 |
% date={1987}, |
| 1299 |
% publisher={Springer-Verlag}, |
| 1300 |
% place={Berlin Heidelberg}, |
| 1301 |
% } |
| 1302 |
% \bib{Lamport}{book}{ |
| 1303 |
% author={Lamport, Leslie}, |
| 1304 |
% title={LaTeX - A document preparation system (2nd ed.\ )}, |
| 1305 |
% date={1994}, |
| 1306 |
% publisher={Addison-Wesley}, |
| 1307 |
% note={something interesting in the fist edition, too}, |
| 1308 |
% } |
| 1309 |
% \bib{L3A}{article}{ |
| 1310 |
% title={The |expl3| package and LaTeX3 programming}, |
| 1311 |
% author={The LaTeX project team}, date={2025}, |
| 1312 |
% note={file: |expl3.pdf| available in CTAN in l3kernel}, |
| 1313 |
% } |
| 1314 |
% \bib{L3B}{article}{ |
| 1315 |
% title={The \LaTeX3 interface}, |
| 1316 |
% author={The LaTeX project team}, date={2025}, |
| 1317 |
% note={file: |interface3.pdf| available in CTAN in l3kernel} |
| 1318 |
% } |
| 1319 |
% \bib{Pearson}{article}{ |
| 1320 |
% title={On lines and planes of closest fit to systems of points in space}, |
| 1321 |
% author={Pearson, Karl}, date={1901}, |
| 1322 |
% journal={Philosophical Magazine}, |
| 1323 |
% volume={2},number={11},pages={559\ndash 572}, |
| 1324 |
% } |
| 1325 |
% \bib{Sanso1}{book}{ |
| 1326 |
% author={Sansò, Fernando}, |
| 1327 |
% title={Elementi di teoria della probabilità}, |
| 1328 |
% date={1996}, |
| 1329 |
% publisher={Città-Studi}, |
| 1330 |
% place={Milano}, |
| 1331 |
% note={see: http://www.geolab.polimi.it/text-books/}, |
| 1332 |
% } |
| 1333 |
% \bib{Sanso2}{book}{ |
| 1334 |
% author={Sansò, Fernando}, |
| 1335 |
% title={La teoria della stima}, |
| 1336 |
% date={1996}, |
| 1337 |
% publisher={Città-Studi}, |
| 1338 |
% place={Milano}, |
| 1339 |
% note={see: http://www.geolab.polimi.it/text-books/}, |
| 1340 |
% } |
| 1341 |
% \bib{Strang}{book}{ |
| 1342 |
% author={Strang, Gilbert}, |
| 1343 |
% title={Introduction to linear algebra}, |
| 1344 |
% date={2009}, |
| 1345 |
% publisher={Wellesley-Cambridge press,}, |
| 1346 |
% } |
| 1347 |
% \end{biblist} |
| 1348 |
% |
| 1349 |
% \par\vfill\centerline{\small ***}\vfill |
| 1350 |
% \end{document} |
| 1351 |
% |
| 1352 |
%\iffalse |
| 1353 |
% END OF FILE linearregression.dtx |
| 1354 |
%\fi |