# Symbolic Optimisation

I'm trying to solve symbolically the following optimisation:

$$\min_{q_uu}\ \Biggl[ q_u q_{uu}\psi B_{uu} + q_u q_{ud}\psi B_{ud} + q_d q_{du}\psi B_{ud} + q_d q_{dd}\psi B_{dd}$$ $$+ \eta_s \left ( q_u \log \frac{q_u}{p_u} + q_d \log \frac{q_d}{p_d} \right )$$ $$\eta_p q_u \left ( q_{uu} \log \frac{q_{uu}}{p_{uu}} + q_{ud} \log \frac{q_{ud}}{p_{ud}} \right ) + \eta_p q_d \left ( q_{du} \log \frac{q_{du}}{p_{du}} + q_{dd} \log \frac{q_{dd}}{p_{dd}} \right ) \Biggr]$$ subject to $$q_u + q_d = 1\ ,\ p_u + p_d = 1\ ,\ q_{uu} + q_{dd} = 1\ ,\ p_{uu} + p_{dd} = 1$$

is there an elegant method for solving such a problem? My approach has been to substitute in the constraints , define the resulting objective as a function , and then differentiate. Unfortunately I get a strange output with 'pattern' coming in for some reason. Apologies if this is trivial - I'm new to mathematica...

f[qu_, quu_, qdu_, \[Psi]_] := (qu*quu*\[Psi]*Buu + qu*(1 - quu)*\[Psi]*Bud
+ (1 - qu)*qdu*\[Psi]*Bud + (1 - qu)*(1 - qdu)*\[Psi]*Bdd) +
\[Eta]s*(qu*log[qu/pu] + (1 - qu)*log[(1 - qu)/pd]) +
\[Eta]d*qu*(quu*log[quu/puu] + (1 - quu)*log[(1 - quu)/pud]) + \[Eta]d*(1 - qu)*
(qdu*log[qdu/pdu] + (1 - qdu)*log[(1 - qdu)/pdd]);


Then ,

FOC = D[f[qu_, quu_, qud_, \[Psi]_], quu]


but I get a strange answer ?

-Bud qu_ \[Psi]_ (Pattern^(1,0))[quu,_]+Buu qu_ \[Psi]_ (Pattern^(1,0))[quu,_]+qu_
Subscript[\[Eta], d] (-log[(1-quu_)/pud] (Pattern^(1,0))[quu,_]+log[quu_/puu]
(Pattern^(1,0))[quu,_]-((1-quu_) (log^\[Prime])[(1-quu_)/pud] (Pattern^(1,0))
[quu,_])/pud+(quu_ (log^\[Prime])[quu_/puu] (Pattern^(1,0))[quu,_])/puu)

• Remove the underscores from the rhs of FOC. Commented Feb 18, 2012 at 18:43
• For future reference, please try providing a minimal (non-) working example and indent your code pastes appropriately. The code above is very hard to read, and you're lucky the error was obvious in this case, otherwise chances could have been that nobody would even understand the problem. Commented Feb 18, 2012 at 18:49

The error is in your differentiation line:

FOC = D[f[qu_, quu_, qud_, \[Psi]_], quu]


Change this to

FOC = D[f[qu, quu, qud, \[Psi]], quu]


and you'll get a meaningful answer.

The reason for this behavior is that x_ is a pattern that matches anything, and this anything can then be referred to by using x. You can read your function definition, f[qu_, ...] := ... as "Whenever Mathematica encounters an expression of the form f[something, ...], it is to be replaced by whatever you specified on the right side of the :=. When you call the function, you do of course not want to differentiate the pattern f[qu_, ...], you want Mathematica to substitute it by some term, and then differentiate whatever is the result of the substitution (i.e. your actual formula). What the program does right now is use your definition of f, insert a pattern as function values, and then differentiate that. You do not want that of course, you want to insert the actual variables.