0
$\begingroup$

I want to create an iterative sequence for two Vectorfunctions, where both are dependent on the same variables and i represents the vector.

For simplicity let's assume this Scenario:

P=Piecewise[{{G[i],i=1,...,n},{F,{i=n+1}}]
n=10

1) Suppose the first step is to evaluate G[1].

2) Dependend on that, the next step has to be the Evaluation of F.

3) Recursivley, G[2] has to be evaluated dependent on the outcome of F.

And so on until n=10 is reached. My question is, how to implement such an iterative process with which commands to us?

Edit

I couldn't find a way to create a simple in-/output example to clearify the problem, but maybe words and a simplified description of the model, which I am working on, helps to understand the issue better.

Assumptions:

n = The number of banks inside a system = 10

P=Piecewise[{{G[i],i=1,...,n},{F,{i=n+1}}]

Where G[i] is a realized payment vector for a banks obligation and F is a function which assesses the quantity of assets in a market. Both depend on a price p for this asset and all Banks[i]hold this asset.

G[i] looks like this:

G[i]=Piecewise[{{o[i], if solvent}, {a*o[i], if not solvent}}]

Where o[i] is the real obligation, which can be paid if Bank[i] is solvent and a*o[i] is for simplicity a fraction parameter a€[0,1] which the bank[i] has to pay if insolvent.

1) Let's assume Bank[1] is insolvent and has to sell assets in the market to pay off debts, then the price p for the asset will fall according marked to market assumptions.

2) For Bank[2] it is now necessary to evaluate how many assets are in the market with the function F and how it is standing about its networth, since a devaluation of the asset price in this environment has an negative impact on its networth.

3) Now Bank[2]has to calculate its payment vector G[2]

4) If Bank[2] is still solvent, there will be no more devaluation of the assetprice p, but if it is insolvent, the same procedure repeats itself from step one to step 3 for Bank[3].

So the task of these functions is to find an equilibrium price after iteratively calculating the situation for each Bank starting from Bank[1] to [n].

$\endgroup$
2
  • $\begingroup$ Could you provide a small but concrete example of input and output? $\endgroup$
    – Mr.Wizard
    May 18, 2015 at 9:31
  • $\begingroup$ I tried to provide a better understanding of the issue. I don't know though if the problem now is clearer. Please let me know $\endgroup$ May 18, 2015 at 16:01

1 Answer 1

1
$\begingroup$

Presumably you want Nest or NestList with or without Composition

n = 3; (* increase n to 10 for your example *)

To display each step

clist1 = NestList[{g[#[[2]]], f[g[#[[2]]]]} &, {0, 1}, n] // Flatten[#, 1] & //
   Drop[#, 2] &

{g[1], f[g[1]], g[f[g[1]]], f[g[f[g[1]]]], g[f[g[f[g[1]]]]], f[g[f[g[f[g[1]]]]]]}

For just end result use Nest rather than NestList

c1 = Nest[{g[#[[2]]], f[g[#[[2]]]]} &, {0, 1}, n][[-1]]

f[g[f[g[f[g[1]]]]]]

Alternatively, using Composition to take two steps at a time

clist2 = NestList[f@*g@# &, 1, n] // Rest

{f[g[1]], f[g[f[g[1]]]], f[g[f[g[f[g[1]]]]]]}

This list is every other term of clist1

clist2 == Last /@ Partition[{clist1, ""} // Flatten, 2]

True

c2 = Nest[f@*g@# &, 1, n]

f[g[f[g[f[g[1]]]]]]

c1 == clist1[[-1]] == c2 == clist2[[-1]]

True

$\endgroup$
2
  • $\begingroup$ Thank you for the help, i think these functional programming commands lead into the right direction and might be the solution for the problem. I try to play a little with them and see if it works. $\endgroup$ May 18, 2015 at 16:04
  • $\begingroup$ And I also made a mistake by describing F as a Function for Vector [i]. However F is not a function for a specific Vector [i], but general function, which will give out a clear value, and not a Vector. I corrected that mistake in the Edit Part. However this just makes it simpler and the Nest argument still might be the solution $\endgroup$ May 18, 2015 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.