# Condition for particular index in array to be maximum

**URL:** <https://discourse.julialang.org/t/condition-for-particular-index-in-array-to-be-maximum/49846>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [November 9, 2020, 2:19pm UTC](https://discourse.julialang.org/t/condition-for-particular-index-in-array-to-be-maximum/49846 "2020-11-09T14:19:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shashankchahar](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@shashankchahar](https://discourse.julialang.org/u/shashankchahar)\
**Post date:** [November 9, 2020, 2:19pm UTC](https://discourse.julialang.org/t/condition-for-particular-index-in-array-to-be-maximum/49846/1 "2020-11-09T14:19:05Z")

</div>

Hi,

I am new to Julia and trying to solve a fitting problem. I have a transition matrix that I am trying to fit. I have got some results but they are non-intutive as the probability of being in state i,i should be the greatest. How can I put such a constraint in JuMP?

The working code (not tested) is as follows:

```julia
function calibrate_trans_rate(filepath_pd)
	
	# Get PD values
	pd_table = zeros(Float64, 30, 7)
	open(filepath_pd, "r") do fp
		for row in 1:30
			line = readline(fp)
			pd_table[row, :] .= parse.(Float64, split(line, ",")) 
		end
	end

	# Set up solver
	model = Model(Ipopt.Optimizer) # need to decide what solver to use
	
	# Set variable to be transition values
	@variable(model, 0 <= x[1:8, 1:8] <= 1)
	@variable(model, y[1:8, 1:8, 1:31])	# This is needed as JuMP only supports matrix operators up to quadratic terms. It doesn’t support cubic.
	
	# Constraints on transition rates
	@constraints(model, begin
			default_col[i = 1:7], x[i, 8] == pd_table[1, i]
			default_row[i = 1:7], x[8, i] == 0
			default_state, x[8, 8] == 1
			sum_of_rows[i = 1:8], sum(x[i, :]) == 1
		end)
	
	# Constraints on y {this is just a hack to get cubic terms for JuMP}
	@constraints(model, begin
			first_y_cons, y[:, :, 1] .== x # Or maybe set equal to x
			[t = 1:30], x * y[:, :, t] .== y[:, :, t + 1]
		end)

	# Define statistic to minimize
	@NLexpression(model, tot_sum_of_sq_diff, sum((y[k,8,time] - pd_table[1, k])^2 for k=1:7, time=1:30))
	
	@NLobjective(model, Min, tot_sum_of_sq_diff)

	JuMP.optimize!(model)

    obj_value = JuMP.objective_value(model)
	trans_matrix = zeros(8, 8)
    for row=1:8, col=1:8
		trans_matrix[row, col] = JuMP.value(x[row,col])
	end
	
	return trans_matrix
end

```

I tried adding a line in @constraints as follows:

```julia
curr_state[i = 1:8], x[i,i] == maximum(x[i, :])

```

This gives me the following error:

```julia
MethodError: no method matching isless(::JuMP.VariableRef, ::JuMP.VariableRef)

Closest candidates are:

isless(!Matched::Missing, ::Any) at missing.jl:87

isless(::Any, !Matched::Missing) at missing.jl:88

max(::JuMP.VariableRef, ::JuMP.VariableRef)@operators.jl:417
_mapreduce@reduce.jl:408[inlined]
_mapreduce_dim@reducedim.jl:318[inlined]
#mapreduce#620@reducedim.jl:310[inlined]
mapreduce@reducedim.jl:310[inlined]
_maximum@reducedim.jl:727[inlined]
_maximum@reducedim.jl:726[inlined]
#maximum#631@reducedim.jl:722[inlined]
maximum@reducedim.jl:722[inlined]
macro expansion@rewrite.jl:227[inlined]
macro expansion@macros.jl:440[inlined]
(::Main.workspace192.var"#7#15"{JuMP.Model,Array{JuMP.VariableRef,2}})(::Int64)@macro.jl:183
#26@container.jl:70[inlined]
iterate@generator.jl:47[inlined]
collect(::Base.Generator{JuMP.Containers.VectorizedProductIterator{Tuple{Base.OneTo{Int64}}},JuMP.Containers.var"#26#27"{Main.workspace192.var"#7#15"{JuMP.Model,Array{JuMP.VariableRef,2}}}})@array.jl:686
map@abstractarray.jl:2188[inlined]
container@container.jl:70[inlined]
container@container.jl:65[inlined]
macro expansion@macros.jl:91[inlined]
calibrate_trans_rate(::String)@Other: 25
top-level scope@Local: 1

```

Any ideas?

The problem that I am trying to solve is here:

> [@Excel to Julia](https://discourse.julialang.org/t/excel-to-julia/49678):
>
> A bit of background - I am an actuary who is trying to learn Julia. In doing so, I am trying to move some of the models that I would usually make in Excel to Julia. I am stuck on one particular problem which Excel makes quite easy for me. I am looking for ways to do the same in Julia. The problem is as follows: I know the probabilities of default for various ratings (AAA to CCC) at various maturities (year 1 to 30 - discrete). I don’t know the underlying transition matrix that produced this ta…

---

<div class="post-metadata">

**Author:** ![shashankchahar](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@shashankchahar](https://discourse.julialang.org/u/shashankchahar)\
**Post date:** [November 9, 2020, 6:27pm UTC](https://discourse.julialang.org/t/condition-for-particular-index-in-array-to-be-maximum/49846/2 "2020-11-09T18:27:44Z")

</div>

> [@shashankchahar](#):
>
> ```julia
> @NLexpression(model, tot_sum_of_sq_diff, sum((y[k,8,time] - pd_table[1, k])^2 for k=1:7, time=1:30))
> 	
> 
> ```

Ignore the question - the error was in this line and pd\_table should be:

```julia
@NLexpression(model, tot_sum_of_sq_diff, sum((y[k,8,time] - pd_table[time, k])^2 for k=1:7, time=1:30))

```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [November 9, 2020, 10:28pm UTC](https://discourse.julialang.org/t/condition-for-particular-index-in-array-to-be-maximum/49846/3 "2020-11-09T22:28:19Z")

</div>

Related to [Array variable - matrix multplication in JuMP - #3 by shashankchahar](https://discourse.julialang.org/t/array-variable-matrix-multplication-in-jump/49801/3)

You probably want

```nohighlight
@constraint(model, [i = 1:8, j = 1:8], x[i, i] >= x[i, j])

```
