def _find_previous_nonzero(backtrack,
                           k_index):
    """
    Finds the previous index in an array where the value is non-zero, starting from a specified index.
    If no previous value in the array is non-zero then an index to the first element (i.e. 0) is returned.
    """
    
    index = 0
    for c in range(k_index - 1, 0, -1):  # here we loop over the backtrack array to look for the most previous month step where the value is not zero
        if backtrack[c] != 0:
            # Backtracking continues in a backstepping procedure up through the most previous month
            # where the corresponding backtracking array element is not equal to zero.
            index = c + 1
            break

    return index