I need to find the roots for a generalized state space. That is, I have a discrete grid of sizes grid=AxBx(...)xX, of which I do not know ex ante how many sizes it has (the solution should be applicable to anyone grid.size).
I want to find the roots ( f(z) = 0) for each state zinside gridusing the bisection method . Say it remaindercontains f(z), and I know f'(z) < 0. Then i need
- Increase
zif remainder> 0 - decrease
zif remainder<0
Wlog, let's say that the historyform matrix (grid.shape, T)contains a history of earlier values zfor each grid point, and I need to increase z(since remainder> 0). Then I will need to select zAlternativeinside history[z, :], which is "the smallest of those that are greater than z." In pseudo code, that is:
zAlternative = hist[z,:][hist[z,:] > z].min()
I asked about this before . The solution I received was
b = sort(history[..., :-1], axis=-1)
mask = b > history[..., -1:]
index = argmax(mask, axis=-1)
indices = tuple([arange(j) for j in b.shape[:-1]])
indices = meshgrid(*indices, indexing='ij', sparse=True)
indices.append(index)
indices = tuple(indices)
lowerZ = history[indices]
b = sort(history[..., :-1], axis=-1)
mask = b <= history[..., -1:]
index = argmax(mask, axis=-1)
indices = tuple([arange(j) for j in b.shape[:-1]])
indices = meshgrid(*indices, indexing='ij', sparse=True)
indices.append(index)
indices = tuple(indices)
higherZ = history[indices]
newZ = history[..., -1]
criterion = 0.05
increase = remainder > 0 + criterion
decrease = remainder < 0 - criterion
newZ[increase] = 0.5*(newZ[increase] + higherZ[increase])
newZ[decrease] = 0.5*(newZ[decrease] + lowerZ[decrease])
However, this code stops working for me. I take this very badly, but I never understood the magic that happens with indexes, so unfortunately I need help.
What the code actually does is to give me the lowest, respectively the highest. That is, if I fix two specific values z:
history[z1] = array([0.3, 0.2, 0.1])
history[z2] = array([0.1, 0.2, 0.3])
higherZ[z1]= 0.3 lowerZ[z2] = 0.1, . 0.2. ?
, , -
history = tile(array([0.1, 0.3, 0.2, 0.15, 0.13])[newaxis,newaxis,:], (10, 20, 1))
remainder = -1*ones((10, 20))
.
history , , . :
lowerZ = 0.1 * ones((10,20))
higherZ = 0.15 * ones((10,20))
z [z,:], (higherZ) (lowerZ). z ([0.1, 0.3, 0.2, 0.15, 0.13]), lowerZ higherZ. , z , , , .