Thread overview
Mir simple linear
Mar 30, 2020
Vino
Mar 30, 2020
jmh530
Mar 30, 2020
jmh530
Mar 31, 2020
9il
March 30, 2020
Hi All,

  Can anyone  guide me what is the problem  with below code as it throws  error.

import std.stdio, mir.math.common: approxEqual;
static immutable x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
static immutable y = [1, 3, 2, 5, 7, 8, 8, 9, 10, 12];
auto params = x.simpleLinearRegression(y);
writeln(params);

March 30, 2020
On Monday, 30 March 2020 at 17:21:57 UTC, Vino wrote:
> Hi All,
>
>   Can anyone  guide me what is the problem  with below code as it throws  error.
>
> import std.stdio, mir.math.common: approxEqual;
> static immutable x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
> static immutable y = [1, 3, 2, 5, 7, 8, 8, 9, 10, 12];
> auto params = x.simpleLinearRegression(y);
> writeln(params);

This should be in the Learn forum.

As far as I can tell, the problem is that x and y are both type immutable(int[]), which it cannot handle. I tried just turning them into slices, but that wasn't sufficient, I had to ensure they were double as well (float would also work). Note that the example in the document uses a y variable that is a floating point type.

/+dub.sdl:
dependency "mir-algorithm" version="~>3.7.25"
+/
import std.stdio : writeln;
import mir.math.stat : simpleLinearRegression;
import mir.ndslice : sliced;

void main() {
    auto x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].sliced!double;
    auto y = [1, 3, 2, 5, 7, 8, 8, 9, 10, 12].sliced!double;
    auto params = x.simpleLinearRegression(y);
    writeln(params);
}
March 30, 2020
On Monday, 30 March 2020 at 17:47:19 UTC, jmh530 wrote:
> 
> [snip]

The relevant function signature is

@safe sumType!YRange[2] simpleLinearRegression(XRange, YRange)(XRange x, YRange y)
if (isInputRange!XRange && isInputRange!YRange && !(isArray!XRange && isArray!YRange) && isFloatingPoint!(sumType!YRange));

and when you pass an int[] for y, then it determines that int is not a floating point and you get an error about not matching the signature, nothing about why you're not matching it.

A good example of where Atila's concepts library would be helpful...
March 31, 2020
On Monday, 30 March 2020 at 17:21:57 UTC, Vino wrote:
> Hi All,
>
>   Can anyone  guide me what is the problem  with below code as it throws  error.
>
> import std.stdio, mir.math.common: approxEqual;
> static immutable x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
> static immutable y = [1, 3, 2, 5, 7, 8, 8, 9, 10, 12];
> auto params = x.simpleLinearRegression(y);
> writeln(params);

static immutable y = [1.0, 3, 2, 5, 7, 8, 8, 9, 10, 12];

`.0` will change the sum type of y to double.