Constructing an optimal portfolio of loans