epostma

1579 Reputation

19 Badges

17 years, 49 days
Maplesoft

Social Networks and Content at Maplesoft.com

Maple Application Center
I am the manager of the Mathematical Software Group, working mostly on the Maple library. I have been working at Maplesoft since 2007, mostly on the Statistics and Units packages and on contract work with industry users. My background is in abstract algebra, in which I completed a PhD at Eindhoven University of Technology. During my studies I was always searching for interesting questions at the crossroads of math and computer science. When I came to Canada in 2007, I had nothing but a work permit, some contacts at Maplesoft (whom I had met at a computer algebra conference a year earlier), and a plan to travel around beautiful Canada for a few months. Since Maplesoft is a company that solves more interesting math and computer science related questions than just about anywhere else, I was quite eager to join them, and after about three months, I could start.

MaplePrimes Activity


These are replies submitted by epostma

@sund0002 : if I open your worksheet and hit the 'three exclamation marks' button in the tool bar, it all works except for the definitions of answer1 and plot1 (which is because you used X, Y there, instead of V, C). It looks like you didn't evaluate the line that says with(Statistics), because the Fit(...) command returns as Fit(...), which means it doesn't call Statistics[Fit].

As to your Excel question: I never work with Excel, but I know we have an ?ExcelTools package which can presumably do that, and also a data import assistant ("Tools" menu -> "Assistants" -> "Import Data ...") which can read Excel sheets.

Erik.

@sund0002 : Again two reasons :)

  1. I think you meant b*x where you wrote bx in the first example.
  2. Now that you've made n a parameter, it's not linear in the parameters anymore. So now Maple chooses to use NonlinearFit as the fitting routine inside Fit. Nonlinear fitting may give results that are not as easily reproducible: Maple finds only a locally optimal set of parameter values, not a globally optimal one.
    For this reason it's highly recommended that you provide Maple with initial values for the parameters if you use nonlinear fitting. If you set infolevel[Statistics] := 1 or higher, then Fit prints whether it uses linear or nonlinear fitting.
    If you don't know what a good initial value could be, then I would suggest you guess a value for n (such as 1.25 from the linear fitting you showed above) and have Maple first perform the fit with n fixed (so that it can do linear fitting). Then use the results from that linear fitting run to provide inital values for the nonlinear fitting run.

Regarding your plotting question: it's easiest if you also ask for the leastsquaresfunction output, then plot that. So you would run

ans2 := Fit(a*x^1.25+b*x+c, V, C, x, output = [parametervalues]);
ans3 := Fit(a*x^n+b*x+c, V, C, x, output = [parametervalues, residualsumofsquares, leastsquaresfunction], initialvalues = [op(ans2[1]), n = 1.25]);
p2 := plot(ans3[3], x=0..200);

Hope this helps,

Erik.

@sund0002 : Again two reasons :)

  1. I think you meant b*x where you wrote bx in the first example.
  2. Now that you've made n a parameter, it's not linear in the parameters anymore. So now Maple chooses to use NonlinearFit as the fitting routine inside Fit. Nonlinear fitting may give results that are not as easily reproducible: Maple finds only a locally optimal set of parameter values, not a globally optimal one.
    For this reason it's highly recommended that you provide Maple with initial values for the parameters if you use nonlinear fitting. If you set infolevel[Statistics] := 1 or higher, then Fit prints whether it uses linear or nonlinear fitting.
    If you don't know what a good initial value could be, then I would suggest you guess a value for n (such as 1.25 from the linear fitting you showed above) and have Maple first perform the fit with n fixed (so that it can do linear fitting). Then use the results from that linear fitting run to provide inital values for the nonlinear fitting run.

Regarding your plotting question: it's easiest if you also ask for the leastsquaresfunction output, then plot that. So you would run

ans2 := Fit(a*x^1.25+b*x+c, V, C, x, output = [parametervalues]);
ans3 := Fit(a*x^n+b*x+c, V, C, x, output = [parametervalues, residualsumofsquares, leastsquaresfunction], initialvalues = [op(ans2[1]), n = 1.25]);
p2 := plot(ans3[3], x=0..200);

Hope this helps,

Erik.

@sund0002 : Two reasons.

  1. Fit and LinearFit (and NonlinearFit) are commands in the Statistics package, so you either need to type with(Statistics); before you run them, or you need to give their "full" name: Statistics[Fit], Statistics[LinearFit], etc. (or equivalently, Statistics:-Fit etc.)
  2. You typed a*V^2+b*v instead of a*v^2+b*v. The difference between V (a vector) and v (a variable) is important.

Hope this helps,

Erik.

@sund0002 : Two reasons.

  1. Fit and LinearFit (and NonlinearFit) are commands in the Statistics package, so you either need to type with(Statistics); before you run them, or you need to give their "full" name: Statistics[Fit], Statistics[LinearFit], etc. (or equivalently, Statistics:-Fit etc.)
  2. You typed a*V^2+b*v instead of a*v^2+b*v. The difference between V (a vector) and v (a variable) is important.

Hope this helps,

Erik.

Actually, I'd suggest using Fit. The expression you're fitting is linear in the parameters, so LinearFit can handle it; and LinearFit finds the globally optimal parameter values, as opposed to NonlinearFit which finds only a locally optimal point. The Fit command selects LinearFit if appropriate and NonlinearFit if necessary, and I'd recommend using that when you're not absolutely certain that you need NonlinearFit. That would involve just replacing the command NonlinearFit with Fit.

In this case it actually doesn't make a difference for the values for a and b that you get out, by the way, but it's just a more reliable process this way.

Oh, and if you need to extract the columns of an Array:

V := Data[.., 1];
C := Data[.., 2];

Hope this helps,

Erik Postma
Maplesoft.

Actually, I'd suggest using Fit. The expression you're fitting is linear in the parameters, so LinearFit can handle it; and LinearFit finds the globally optimal parameter values, as opposed to NonlinearFit which finds only a locally optimal point. The Fit command selects LinearFit if appropriate and NonlinearFit if necessary, and I'd recommend using that when you're not absolutely certain that you need NonlinearFit. That would involve just replacing the command NonlinearFit with Fit.

In this case it actually doesn't make a difference for the values for a and b that you get out, by the way, but it's just a more reliable process this way.

Oh, and if you need to extract the columns of an Array:

V := Data[.., 1];
C := Data[.., 2];

Hope this helps,

Erik Postma
Maplesoft.

@pagan : This is amazing. Somehow, Firefox (assisted by some scripting trickery, for sure) must have thought it would be a great idea to use that pasted expression, write it to my temporary directory on the local filesystem as /tmp/moz-screenshot.png, and then insert the img tag referring to the local uri file:///tmp/moz-screenshot.png. Wow. I just checked and indeed I have that file on my file system here.

Of course I had no idea that that's what's going on. I just saw an image appear in my post... So anyway, it looks like this is not very useful.

Erik.

@pagan : This is amazing. Somehow, Firefox (assisted by some scripting trickery, for sure) must have thought it would be a great idea to use that pasted expression, write it to my temporary directory on the local filesystem as /tmp/moz-screenshot.png, and then insert the img tag referring to the local uri file:///tmp/moz-screenshot.png. Wow. I just checked and indeed I have that file on my file system here.

Of course I had no idea that that's what's going on. I just saw an image appear in my post... So anyway, it looks like this is not very useful.

Erik.

I tried a few random (positive) values for the other parameters, and for a couple of them, there was a unique real solution for i, but not with these values:

y = 2
a = 9
b = 1/10
c = 1/10
d = 5
g = 3/4
h = 1/8

Here, there are still a number of solutions (Maple finds 51 of them), but they're all complex, and I guess that would not be valid in the context of neural networks. Here's a plot of the right hand side of the equation minus y:

plot(-2+3/4/(1+exp(-9-i/10))+1/8/(1+exp(-1/10-5*i)), i=-infinity..infinity)

And indeed, we can see that if y > g + h, then substituting anything positive for the exponentials is not going to lead to a solution. So this seems to indicate that the function is not invertible -- unless this violates some constraint on the values that the parameters should have?

Erik.

I tried a few random (positive) values for the other parameters, and for a couple of them, there was a unique real solution for i, but not with these values:

y = 2
a = 9
b = 1/10
c = 1/10
d = 5
g = 3/4
h = 1/8

Here, there are still a number of solutions (Maple finds 51 of them), but they're all complex, and I guess that would not be valid in the context of neural networks. Here's a plot of the right hand side of the equation minus y:

plot(-2+3/4/(1+exp(-9-i/10))+1/8/(1+exp(-1/10-5*i)), i=-infinity..infinity)

And indeed, we can see that if y > g + h, then substituting anything positive for the exponentials is not going to lead to a solution. So this seems to indicate that the function is not invertible -- unless this violates some constraint on the values that the parameters should have?

Erik.

@Alec Mihailovs: yeah, that's my doing. I had to fiddle around a bit to get the command to show up right (for certain values of right - the 0 and 3 are in an italic font, not roman, but I'd consider that minor). I guess there are some kinks to be worked out of the code dealing with that. For the record, here's the intended command in monospaced font:

plot(sin(t^2), t = 0 .. 3, labels = [sqrt(time), amplitude]);

Thanks, Erik.

@Alec Mihailovs: yeah, that's my doing. I had to fiddle around a bit to get the command to show up right (for certain values of right - the 0 and 3 are in an italic font, not roman, but I'd consider that minor). I guess there are some kinks to be worked out of the code dealing with that. For the record, here's the intended command in monospaced font:

plot(sin(t^2), t = 0 .. 3, labels = [sqrt(time), amplitude]);

Thanks, Erik.

@Alec Mihailovs : that's right, and that's a good point to make. I just wanted to make the slightly different point that it might not be a great idea to do RREF at all.

My 1/3 example was bad; what I meant is that such fractions can come up during the computation, not just if they're present in the input (or even in the correct output): if you encounter a pivot during RREF that you cannot divide exactly by in the arithmetic that's being used (e.g. 3 as a pivot for either binary or decimal arithmetic), then other entries in that row which are not a mulitple of that pivot will be divided by that pivot and will not be exact anymore. An example would be this matrix:

Actually Maple finds the right RREF here, but if you do it naively, then scaling the first row leaves you with <1., 1.3333333332, -1.6666666665> (in the binary arithmetic) and you get the wrong answer in the end, even though both input and (correct) output contain only things that are correctly representable.

Erik Postma
Maplesoft.

@Alec Mihailovs : that's right, and that's a good point to make. I just wanted to make the slightly different point that it might not be a great idea to do RREF at all.

My 1/3 example was bad; what I meant is that such fractions can come up during the computation, not just if they're present in the input (or even in the correct output): if you encounter a pivot during RREF that you cannot divide exactly by in the arithmetic that's being used (e.g. 3 as a pivot for either binary or decimal arithmetic), then other entries in that row which are not a mulitple of that pivot will be divided by that pivot and will not be exact anymore. An example would be this matrix:

Actually Maple finds the right RREF here, but if you do it naively, then scaling the first row leaves you with <1., 1.3333333332, -1.6666666665> (in the binary arithmetic) and you get the wrong answer in the end, even though both input and (correct) output contain only things that are correctly representable.

Erik Postma
Maplesoft.

First 11 12 13 14 15 16 17 Last Page 13 of 22