Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit tensor expression #163

Closed
Goysa2 opened this issue Apr 13, 2018 · 7 comments
Closed

Explicit tensor expression #163

Goysa2 opened this issue Apr 13, 2018 · 7 comments

Comments

@Goysa2
Copy link

Goysa2 commented Apr 13, 2018

Hi, I do not fully understand how this package works.
I was wondering if it's possible to only extract the hessian or tensor at a specific iteration?

I used the Rosenbrock function as an example: rosenbrock(x) = (1 - x[1])^2 + 100(x[2] - x[1]^2)^2
I understand how to get the value of (1/2)*(x-x0)^t * hessian of f at x * (x-x0) using the Taylor expand command.
(I do taylor_expand(rosenbrock,[0.0, 0.0])[2]([1.0, 1.0]) which gives me the expected value).
What I would want is only the Hessian matrix at a specific point. Or even better the tensor at a specific point.

Is it possible to do that?

Thank you very much for your help!

@lbenet
Copy link
Member

lbenet commented Apr 13, 2018

Hi!

Thanks for the interest in the package and the questions.

Regarding how it works, take a look in the documentation, specifically in the background. Though not all details are given, the essence of the philosophy is outlined. If that is not clear enough, please open issues for that.

About the hessian, yes, there is a function that does that, hessian(f, v), where f is a TaylorN expansion, and v is the value where it is evaluated (see the help, ? hessian). I am not sure I understand the specific question, so here is a way to obtain it.

julia> using TaylorSeries

julia> rosenbrock(x) = (1 - x[1])^2 + 100(x[2] - x[1]^2)^2
rosenbrock (generic function with 1 method)

julia> x, y = set_variables("x y")  # define the independent variables
2-element Array{TaylorSeries.TaylorN{Float64},1}:
  1.0 x + 𝒪(‖x‖⁷)
  1.0 y + 𝒪(‖x‖⁷)

julia> rr = rosenbrock([x, y])  # expansion of the Rosenbrock function around [0,0]
 1.0 - 2.0 x + 1.0+ 100.0- 200.0 x² y + 100.0 x⁴ + 𝒪(‖x‖⁷)

julia> hessian(rr)  # evaluate the Hessian at [0,0]
2×2 Array{Float64,2}:
 2.0    0.0
 0.0  200.0

julia> hessian(rr, [1.0,1.0]) # evaluate the Hessian at [1.0,1.0]
2×2 Array{Float64,2}:
  802.0  -400.0
 -400.0   200.0

Be aware that rr above is an series expansion around a given point, and therefore usually it is an approximation (x and y are usually "small"). In the case of Rosenbrock function above, the expansion point is the [0,0] and the expansion is actually exact (since we are using by default order up to 6 in each of the TaylorN variables, and Rosenbrock function is polynomial of order 4). If you want to expand around another point, say [1,2], you then need to define
rr = rosenbrock([1.0+x, 2.0+y]). Both x and y are supposed to be small differences with respect to the expansion point.

Let me know if this helps...

EDIT: Forgot to include the line defining the independent variables x and y.

@Goysa2
Copy link
Author

Goysa2 commented Apr 13, 2018

Thanks for your answer! Thanks a lot for the example it helped put a lot of the documentation in context for me! (I am not a very good programmer, so I can get lost in the documentation very easily).

That's exactly what I am looking for! Do you think it's possible to extract the 3rd-degree derivative (i.e. the tensor which would be a 2x2x2 Array)?

Thanks again for your help!

@lbenet
Copy link
Member

lbenet commented Apr 13, 2018

(Note that I corrected the code of my previous answer.)

The answer is yes, but you have to deal with the way you actually define the tensor.

Essentially, what you are asking is all possible third order partial derivatives (third order terms), which you can see are already in rr. To get them directly, you simply ask for rr[3] (the homogeneous polynomial of order 3 of rr):

julia> rr
 1.0 - 2.0 x + 1.0+ 100.0- 200.0 x² y + 100.0 x⁴ + 𝒪(‖x‖⁷)

julia> rr[3]
 - 200.0 x² y

julia> typeof(ans)
TaylorSeries.HomogeneousPolynomial{Float64}

I think from here, you just have to set-up the way you define the 2x2x2 tensor.

@Goysa2
Copy link
Author

Goysa2 commented Apr 13, 2018

Thank you very much! Really appreciate your help!

@lbenet
Copy link
Member

lbenet commented Apr 13, 2018

I just implemented a more general partial derivation; see #164. Perhaps it is useful. Comments are welcome!

@lbenet
Copy link
Member

lbenet commented Apr 17, 2018

Partial derivation has been added in #164, and merged to master.

@lbenet
Copy link
Member

lbenet commented Apr 17, 2018

Shall I close this, @Goysa2?

@lbenet lbenet closed this as completed Apr 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants