Output format for find_fit and solve

174 прегледа
Пређи на прву непрочитану поруку

Oscar Gerardo Lazo Arjona

непрочитано,
29. 10. 2010. 18:59:1629.10.10.
– sage-...@googlegroups.com
I want to propose the following changes to the output format of find_fit
and solve:

for find_fit the current output format is a list of equations:

sage: data = [(i, 1.2 * sin(0.5*i-0.2) + 0.1 * normalvariate(0, 1)) for
i in xsrange(0, 4*pi, 0.2)]
sage: var('a, b, c, x')
sage: model(x) = a * sin(b * x - c)
sage: find_fit(data, model)

[a == 1.2204167610363676, b == 0.50171964598627838, c ==0.22401763827376933]

or a dictionary:

sage: find_fit(data,model,solution_dict=True)

{c: 0.22401763827376933, b: 0.50171964598627838, a: 1.2204167610363676}

I'd like to get an expression where the values found for the parameters
are put in the model given to find_fit:

sage: find_fit(data, model)
1.2204167610363676*sin(0.50171964598627838 *x -0.22401763827376933 )

For solve the current output format depends on the input:

sage: var('x y')
sage: eq1=x+4==0
sage: eq2=x^2+4*x+2==0
sage: sys1=x+y==1
sage: sys2=x-2*y==-2
sage: solve(eq1,x)
[x == -4]
A list containing solutions (even when there is only one solution).

sage: solve(eq2,x)
[x == -sqrt(2) - 2, x == sqrt(2) - 2]
A list containing solutions when there is more than one solution

solve([sys1,sys2],x,y)
[[x == 0, y == 1]]
A list containing a list containing solutions (this is the worst)

The format I would like to see is:

sage: solve(eq1,x)
x == -4
A single equation

sage: solve(eq2,x)
[x == -sqrt(2) - 2, x == sqrt(2) - 2]
This one is fine

solve([sys1,sys2],x,y)
[x == 0, y == 1]
Just a list of solutions. It is harder to work with the solutions if
they are inside another list.

I would like all of this changes to become standard outputs, although I
imagine that that would produce some backwards compatibility issues.

thanks!

Oscar

Jason Grout

непрочитано,
30. 10. 2010. 03:26:2030.10.10.
– sage-...@googlegroups.com
On 10/29/10 5:59 PM, Oscar Gerardo Lazo Arjona wrote:
> I want to propose the following changes to the output format of find_fit
> and solve:
>
> for find_fit the current output format is a list of equations:
>
> sage: data = [(i, 1.2 * sin(0.5*i-0.2) + 0.1 * normalvariate(0, 1)) for
> i in xsrange(0, 4*pi, 0.2)]
> sage: var('a, b, c, x')
> sage: model(x) = a * sin(b * x - c)
> sage: find_fit(data, model)
>
> [a == 1.2204167610363676, b == 0.50171964598627838, c
> ==0.22401763827376933]
>
> or a dictionary:
>
> sage: find_fit(data,model,solution_dict=True)
>
> {c: 0.22401763827376933, b: 0.50171964598627838, a: 1.2204167610363676}
>
> I'd like to get an expression where the values found for the parameters
> are put in the model given to find_fit:
>
> sage: find_fit(data, model)
> 1.2204167610363676*sin(0.50171964598627838 *x -0.22401763827376933 )


Why not make solution_dict=True the default, since then you could say:

fit=find_fit(data,model)
model.subs(fit)

to get the model with those parameters?

In fact, it would be great if this worked too:

fit=find_fit(data,model)
model(**fit)

but (even if solution_dict=True is the default) that complains that the
keys in the dictionary are not strings.

However, we could make this work:

fit=find_fit(data,model)
model(fit)

but it require changes to the __call__ method to allow a dictionary
input that would do the same as .subs() would do if handed a dictionary.

What if there are two different solutions, like (x=0, y=0) and (x=0,y=1)?

Personally, I think returning an equation is okay for situations where I
am just checking something, and don't plan on using the solution again.
Returning a list of equations is horrible for actually doing anything
with the results, which is my intent in the vast majority of situations.
Having to always append the not-very-obvious "solution_dict=True" to
use solve in the common case is not very user-friendly. If we're
changing the output of solve, I'd propose returning either a single
dictionary (a single solution) or a list of dictionaries (multiple
solutions). Solve seems like it would be much more useful that way.

Thanks,

Jason

Oscar Lazo

непрочитано,
30. 10. 2010. 11:20:1330.10.10.
– sage-devel
Making solution_dict the default seems apropiate for find_fit and
solve. I still would prefer a symbolic result for find_fit though (at
least an option to get that). Usually when one fits some data to a
model what one is trying to do is to interpolate data in between the
current data (plot the fitted function etc). Mathematica uses this two
outputs in two different functions:

For Fit they use a symbolic output: http://reference.wolfram.com/mathematica/ref/Fit.html

For FindFit they use something that looks like a dictionary:
http://reference.wolfram.com/mathematica/ref/FindFit.html

As for solve, I agree, we should return a single dictionary when
there's just one solution and a list of dictionaries when there's more
than one solution.

thanks!

Oscar

Jason Grout

непрочитано,
30. 10. 2010. 18:47:0630.10.10.
– sage-...@googlegroups.com
On 10/30/10 10:20 AM, Oscar Lazo wrote:
>

> Making solution_dict the default seems apropiate for find_fit and
> solve. I still would prefer a symbolic result for find_fit though (at
> least an option to get that). Usually when one fits some data to a
> model what one is trying to do is to interpolate data in between the
> current data (plot the fitted function etc). Mathematica uses this two
> outputs in two different functions:
>
> For Fit they use a symbolic output: http://reference.wolfram.com/mathematica/ref/Fit.html
>
> For FindFit they use something that looks like a dictionary:
> http://reference.wolfram.com/mathematica/ref/FindFit.html


I think that having two functions sounds like a great approach.

Jason

David Kirkby

непрочитано,
31. 10. 2010. 07:59:0931.10.10.
– sage-...@googlegroups.com

Wolfram Research do the same with integration.

Integrate[] symbolic
NIntegrate[] - numeric

N[ Integrate[] ] - Numerical approximation to a symbolic answer.
Integrate[] //N - another way of writing the above

The same for finding roots.

http://reference.wolfram.com/mathematica/ref/FindRoot.html (numerical)
http://reference.wolfram.com/mathematica/ref/Root.html (symbolic)

There are undoubtedly other examples too. Mathematica uses different
functions for numerical and symbolic work.

Dave

Oscar Lazo

непрочитано,
4. 11. 2010. 15:39:564.11.10.
– sage-devel
Одговори свима
Одговори аутору
Проследи
0 нових порука