Adaptation of NLsolve for function with vector output

I’m facing an issue with NLSolve. I have two interacting functions that are working well, and I need to adapt them. Let me briefly summarize how the functions are currently set up and what I’m aiming to achieve.

Firstly, I have a function that calculates the government deficit:

function deficit(t)
    tax_h, tax_l = t[1], t[2]

    # Lots of calculations
    
    dif_tax = (ep_h * wp_h * tax_h - (eg_h * wg_h)) + (ep_l * wp_l * tax_l - (eg_l * wg_l))
    F = [dif_tax]
    return F
end

Then, I’m using another function that incorporates the deficit function within it, like this:

function solvemodeltax(chute_param, chute_th, chute_tl)
    chute_t = [chute_th, chute_tl]
    taxopt = nlsolve(x -> deficit(x), chute_t).zero
    tax_h = taxopt[1]
    tax_l = taxopt[2]

    # Lots of calculations
return output
end

And this is working fine. However, I’m aiming to perform a counterfactual exercise where the government needs to balance the budget for each specific sector (noting that H represents high skill and L represents low skill). Consequently, I’ve modified the code as follows

function deficit_separatesector(t)
    tax_h, tax_l = t[1], t[2]

    # Lots of calculations

    dif_taxh = ep_h * wp_h * tax_h - (eg_h * wg_h)
    dif_taxl = ep_l * wp_l * tax_l - (eg_l * wg_l)
    F = [dif_taxh, dif_taxl]
    return F
end 

In this case, the return is no longer a one-element array, but a two-element collection representing the deficit for each type of worker. Consequently, I adapted the solving function as follows (with a focus on the relevant parts):

function solvemodeltax_v2(chute_param, chute_th, chute_tl)
    chute_t = [chute_th, chute_tl]
    taxopt = nlsolve(x -> deficit_separatesector(x, kappa_h=kappa_h, kappa_l=kappa_l, b_h=b_h, b_l=b_l, y_h=y_h), chute_t).zero
    tax_h = taxopt[1]
    tax_l = taxopt[2]

    # Lots of calculations

  return output
end

In this last part, this final NLSolve is where the code encounters issues. The dimensions seem correct, and everything else appears fine, but I’m uncertain why it’s unable to find a solution. Is this an error within the code or a theoretical problem? Is there anything I can do to improve it?

From my perspective, it should work since the deficits are interlinked, suggesting that it’s possible (due to function continuity) to find a rate that optimally combines both sectors.

Note: I haven’t provided the entire function details, only the main aspects, as they are quite extensive. I believe this should give you an understanding of my point and the challenges.

The problem is that nlsolve doesn’t converge? If so, then it is very hard to suggest anything if you don’t share what happens inside those functions.

1 Like

It may not be a programming problem: are you sure that the two equations you are trying to find a zero for actually can be zero at the same time? In other words: does the problem actually have a solution?

1 Like

The problem is:

DomainError with NaN:
integrand produced NaN in the interval (NaN, 38.328194321555955)

Thank you for the response. Initially, I thought it might be a programming problem, especially concerning nlsolve . However, I now believe it’s not a problem but rather a limitation of the model. It’s not possible to achieve what I was attempting to do: setting the tax level for high-skilled workers too high, which implies the absence of a private sector for such workers.

Yeah, I believe the code does not converge. However, my question is to know if the code I provided would work in NLsolve. I mean would nlsolve optim problem understand that I need taxes such that F (as below) is zero for each cell of the vector F, namely: dif_taxh and dif_taxl

F = [dif_taxh, dif_taxl]

[/quote]