2021年1月18日星期一

Way to run a function with changing variable length through and optimization and record solutions in Matlab

General setup

Say the functions is

function [sum] = sumfunc(data,x1,x2,x3,x4,x5)  "Some magic happens here :)"   end  

Here data is a dataset with one variable.

then I needed parameter estimates that would maximize the sum. I know how to get that when the window size we select from "data" is fixed. Here's how I got the optimized values

Creating an empty problem

prob1 = optimproblem  

Defining Optimization variables with bounds

x1 = optimvar("x1", "LowerBound",0);  x2        = optimvar("x2","LowerBound",0,"UpperBound",1);  x3     = optimvar("x3","LowerBound",0);  x4      = optimvar("x4","LowerBound",0,"UpperBound",1);  x5     = optimvar("x5","LowerBound",0,"UpperBound",1);  

Creating an objective function

edit sumfunc  

Creating optimizing expression

obj = fcn2optimexpr(@sumfunc,data,x1,x2,x3,x4,x5)  

Assigning Objective function

prob1.Objective = obj  

Setting constraints

constr1 = x1 - (x4*x3) - x2 <=0;  prob1.Constraints.optim1 = constr1;  constr2 = x2 - ((2-x3)/3) <=0;  prob1.Constraints.optim2 = constr2;  constr3 = (x4*(x3))*(x2-((1-x3)/2))-(x1*(x2-x1+(x4*x3)))<=0;  prob1.Constraints.optim3 = constr3;  

Setting the Initial conditions

x0.x1 = 0.01;  x0.x2 = 0.15;  x0.x3 = 0.20;  x0.x4 = 0.20;  x0.x5 = 0.5;  

Solving the problem

[sol,fval,exitflag] = solve(prob1,x0)  

Now I need to change the size (change the amount of data from the data file / length of data ) of data in a rolling window. I'll try my best to illustrate the problem

say

rolling window size = m  length(data)        = T  increment size      = 1  

so windows would be (1:m),(2:m+1),....,(T-m:T)

obj = fcn2optimexpr(@sumfunc,**data(window)**,x1,x2,x3,x4,x5)  [sol,fval,exitflag] = solve(prob1,x0)  

then I need to save the parameter estimates I get for different rolling windows for the values x1,x2,x3,x4,x5. Any help and direction are appreciated.

https://stackoverflow.com/questions/65784955/way-to-run-a-function-with-changing-variable-length-through-and-optimization-and January 19, 2021 at 11:03AM

没有评论:

发表评论