# Fit two intersecting straight lines in Mathematica

I am trying to do a Kurie analysis of the double beta decay of Strontium-90.

Here we have two straight sections, and I need to obtain a value for the intersection along with an error for it. I cannot fit the linear sections separately and use gaussian error propagation, since the fit parameters are correlated, which leads to a wrong result.

My idea was to fit the entire data with one function which is defined piecewise, since the intersection parameter would then not be correlated to the others.

However, after several attempts and a fine looking fit, Mathematica reports an error of 0.

NonlinearModelFit[data, (a*x + b)*UnitStep[x + k] + (c*x + d)*UnitStep[k - x],
{{a, -6.5}, {b, 3}, {c, -1}, {d, 1.8}, {k, 0.55}}, x, Method -> "NMinimize"]


Are there any other ways of doing this?

data = {
{2.160471686147928, 0.14147102932224526},{2.0826390189609363,0.19988842616357125},
{2.018773794495269, 0.2541246545339056}, {1.9476326649797793, 0.3042648279519891},
{1.8745330031773189, 0.3792877696740006}, {1.8048469776435923, 0.4333264807298175},
{1.731446650327849, 0.4855243148263505}, {1.6651014082972075, 0.5645773920773384},
{1.5932847071615477, 0.5972837751080117}, {1.52139595538298, 0.6653922271670848},
{1.4422671545128036, 0.7171604890149067}, {1.3757137824715229, 0.79262676502781},
{1.3073786844775914, 0.8404843652309524}, {1.2337051064574602, 0.8954499357694174},
{1.1906240113139641, 0.9399196660012136}, {1.1601352767466366, 0.9890482524748053},
{1.0867224338785175, 1.0227388598388758}, {1.0206577309807057, 1.0542107134579233},
{0.9477230432778146, 1.1301493061594161}, {0.8733917792374216, 1.1877273944557323},
{0.8065623408414323, 1.2154971541338453}, {0.736758267792002, 1.3121152330650923},
{0.6676435767247085, 1.3823323706871722}, {0.635101745155093, 1.3868057675840053},
{0.599382518347645, 1.4179592728730186}, {0.5622741608125342, 1.4536070008301036},
{0.5321771084417978, 1.4636936970264636}, {0.490818693322041, 1.6204857554395113},
{0.4613963222976981, 1.6851063997905946}, {0.42109009911055906,1.8551603288963705},
{0.39567303637157025, 1.945847339995944}, {0.36747227092332413, 2.145514603636669},
{0.34125816875918824, 2.333372509561687}, {0.3124933023702082, 2.347039270807089}};

-

## migrated from stats.stackexchange.comApr 5 '14 at 11:30

This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.

The problem is, that NLMFit does not calculate an error for the parameter k. I need to quantify exactly this error. – ephimetheus Apr 5 '14 at 10:43
Data would be nice. – Vitaliy Kaurov Apr 5 '14 at 11:35
I would estimate an error from the distance between closest to k data points . – swish Apr 5 '14 at 12:04
– ephimetheus Apr 5 '14 at 12:16

Something strange happens when you allow your lines in your model to have a gap at point k. You also specified UnitStep functions wrongly - see my version below. You do realize your model function allows for gap?

It is better to have less parameters in the model. Obviously your data assume that lines meet without gap. Why not to explicitly specify this excluding one parameter?

Solve[a*x + b == c x + d /. x -> k, d]


{{d -> b + a k - c k}}

g[x_, a_, b_, c_, k_] :=
(a x + b) UnitStep[k - x] + (c x + (b + a k - c k)) UnitStep[x - k]


But I would better define it as:

g[x_, a_, b_, c_, k_] :=
Piecewise[{{a x + b, x < k}, {b + (a - c) k + c x, x >= k}}]


nlm = NonlinearModelFit[data,
g[x, a, b, c, k], {{a, -3.5}, {b, 3.5}, {c, -1}, {k, 0.5}}, x];

Plot[nlm[x], {x, 0, 3}, Epilog -> {Red, PointSize[.01], Point@data},
Frame -> True, Axes -> False]


nlm["ParameterTable"]


-
The problem is that the residuals don't change continuously with respect to changes in k. – Michael E2 Apr 5 '14 at 13:02
@MichaelE2 you mean the problem with his model function? – Vitaliy Kaurov Apr 5 '14 at 13:05
@VitaliyKaurov Yes, that's what I mean. A sufficiently small change in k makes no change in the fitting errors. – Michael E2 Apr 5 '14 at 13:36
@MichaelE2 What you are saying then is that it is not possible to fit a discontinuous function? – Vitaliy Kaurov Apr 5 '14 at 13:39
I would add that as a problem in probability, estimating the error in k is beyond my understanding of probability and statistics. One might take as a point estimate the midpoint of the interval of values for k that minimizes the error. But I have no idea how such a statistic is distributed. – Michael E2 Apr 5 '14 at 13:59