{ From user Lonnie, Model Untitled at Wed, Sep 05, 2007 3:41 PM} Softwareversion 4.0.0 Linklibrary Optimization_functio Title: Optimization Functions Description: This library contains various functions for optimization ~~ and equation solving. Author: Lonnie Chrisman, Ph.D.~ Lumina Decision Systems Date: Fri, Apr 25, 2003 10:23 PM Saveauthor: Lonnie Savedate: Wed, Sep 05, 2007 3:41 PM Defaultsize: 48,24 Nodesize: 56,24 Nodeinfo: 1,1,1,1,1,1,0,0,0,0 Diagstate: 1,40,184,424,245,17 Fileinfo: 0,Linklibrary Optimization_functio,2,2,0,0,C:\Src\AnalyticaD~~ evelopment\Analytica\ExecDebug\Libraries\Optimization Functions.ANA Function Goalseek(Y,X:varType;goal:atomic) Title: GoalSeek( Y, X, g) Description: Finds the value of X that makes Y=goal.~ Based on a Newton-Rapshon method starting at the current value of X. Definition: var z[]:=whatif(X,X,x); /* trick to get the value of X */~~ ~ var maxIter := Max_goal_seek_iterat;~ var curY := Y;~ var ans := z;~ while any (abs(curY-goal)>1e-6 and maxIter>0) ( ~ z := whatif( ~ curY := Y;~ ans := z;~ var m:=Dydx(Y,X);~ if (abs(m)<1e-20) then Z else Z - (Y-goal)/m~ , X, z) ;~ maxIter := maxIter - 1~ );~ ans Nodelocation: 72,40,1 Nodesize: 48,24 Windstate: 1,179,87 Paramnames: Y,X,goal Module Goal_seek_examples Title: Goal Seek Example -- Target interest rate Description: This module demonstrates the use of the goal-seek functio~~ n.~ The problem considered in this example is this: What interest rate w~~ ould I need to invest my principal at in order to attain my target va~~ lue at the end of my investment period.~ ~ This example can obviously be solved in closed form with simple algeb~~ ra, but it serves as a understandable example of the use fo goal seek~~ , which generalizes to cases that aren't solvable in closed form. Author: Lonnie Chrisman, Ph.D.~ Lumina Decision Systems, Inc. Date: Fri, Apr 25, 2003 10:23 PM Defaultsize: 48,24 Nodelocation: 72,128,1 Nodesize: 48,56 Diagstate: 1,104,213,315,453,17 Windstate: 1,36,452 Variable Gs_p Title: Principal Units: $ Definition: 10K Nodelocation: 80,56,1 Nodesize: 48,24 Nodeinfo: 1,1,1,1,1,1,0,0,0,0 Valuestate: 1,40,50,416,303,0,MIDM Numberformat: 1,D,4,2,0,1 Variable Gs_i Title: Interest Units: %/yr Definition: 4% Nodelocation: 200,136,1 Nodesize: 48,24 Nodeinfo: 1,1,1,1,1,1,0,0,0,0 Valuestate: 1,40,50,416,303,0,MIDM Numberformat: 1,%,4,2,0,0 Variable Gs_n Title: Years to maturity Units: yrs Definition: 20 Nodelocation: 200,56,1 Nodesize: 48,24 Nodeinfo: 1,1,1,1,1,1,0,0,0,0 Valuestate: 1,40,50,416,303,0,MIDM Variable Gs_v Title: Value at Maturity Units: $ Definition: Gs_p*(1+Gs_i)^Gs_n Nodelocation: 80,136,1 Nodesize: 48,24 Valuestate: 1,56,66,416,303,0,MIDM Numberformat: 1,F,4,2,0,1 Objective Goal_seek_test Title: Target Interest Units: %/yr Description: This example demonstrates the use of goal seek to compute~~ the interest rate that one would need on a one-time investment to at~~ tain a given target principle goal. It is set up to compute this acr~~ oss a range of goals, so it is actually carrying out a whole series o~~ f optimizations when computing this node. Definition: GoalSeek(Gs_v,Gs_i,Target_value_at_matu) Nodelocation: 80,224,1 Nodesize: 48,24 Windstate: 1,128,273 Valuestate: 1,52,48,533,509,0,MIDM Graphsetup: Graphtool:0~ Distresol:10~ Diststeps:1~ Cdfresol:5~ Cdfsteps:1~ Symbolsize:6~ Baroverlap:0~ Linestyle:10~ Frame:1~ Grid:3~ Ticks:1~ Mesh:1~ Scales:1~ Rotation:45~ Tilt:0~ Depth:70~ Frameauto:1~ Showkey:1~ Xminimum:10K~ Xmaximum:20K~ Yminimum:0~ Ymaximum:0.04~ Zminimum:1~ Zmaximum:1~ Xintervals:0~ Yintervals:0~ Includexzero:0~ Includeyzero:0~ Includezzero:0~ Statsselect:[1, 1, 1, 1, 1, 0, 0, 0]~ Probindex:[5%, 25%, 50%, 75%, 95%]~ Fontstyle: Arial, 8 Numberformat: 1,%,4,2,0,1 Index Target_value_at_matu Title: Target Value at Maturity Definition: Sequence( 10K, 40K, 1000 ) Nodelocation: 200,224,1 Nodesize: 48,24 Variable Verification_of_corr Title: Verified Mature Value Definition: Gs_p*(1+Goal_seek_test)^Gs_n Nodelocation: 80,296,1 Nodesize: 48,24 Valuestate: 1,204,76,439,458,0,MIDM Close Goal_seek_examples Constant Max_goal_seek_iterat Title: Max Goal Seek Iterations Definition: 200 Nodelocation: 192,40,1 Nodesize: 48,29 Function Gradient(Y:varType;X:varType; I:IndexType) Title: Gradient(Y,X,I) Description: Computes the gradient of Y with respect to vector X, wher~~ e X is indexed by I. Essentially a generalization of dydx. The grad~~ ient is evaluated at the value that X currently has. Y would depend ~~ on X. Definition: var epsilon := 1e-6;~ for j:=I do ~ (whatif( Y, X, if j=I then X+epsilon else X ) - Y) / epsilon Nodelocation: 200,112,1 Nodesize: 48,24 Windstate: 1,27,196 Paramnames: Y,X,I Function Solve(Y,X:varType;I:IndexType) Title: Solve(Y,X,I) Description: This finds a vector, X, indexed by I, which makes Y=0.~ Uses a newton-raphson style search. Y should depend on X, and X shou~~ ld be indexed by I. Definition: var z:=whatif(X,X,x); /* trick to get the value of X */~ var maxIter := Max_goal_seek_iterat;~ var curY := Y;~ var newY := Y;~ var nextZ := z;~ var grad := Gradient(Y,X,I);~ var c := 1; /* brazenness factor */~ while (abs(curY)>1e-6 and maxIter>0) ( ~ var mag := sum(grad^2,I); ~ var newZ := (if abs(mag)<1e-20 then Z else Z-c*y*grad/mag);~ newY := WhatIf( Y,X,newZ);~ if abs(newY)<=abs(curY) then (~ /* Improvement. We'll take it */~ curY := newY;~ Z := newZ;~ c := c * 1.5; /* Gain momentum */~ grad := WhatIf(Gradient(Y,X,I),X,Z);~ ~ ) else (~ c := c * 0.7 /* try a smaller step */~ );~ maxIter := maxIter - 1~ );~ z Nodelocation: 320,40,1 Nodesize: 48,24 Windstate: 2,188,21,579,599 Paramnames: Y,X,I Close Optimization_functio