Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:39:19+00:00 2026-06-17T18:39:19+00:00

I would like to dynamically parameterize an array for a state-space model depending on

  • 0

I would like to dynamically parameterize an array for a state-space model depending on how many states I choose.

I am doing this with a loop –

Q <- function(params,states) {
qmat <- matrix(0,statespace,statespace)
for (i in 1:statespace)
  qmat[i,i] <- statshockvar(params[(i-1)*5+1], params[(i-1)*5+2], 
                            params[(i-1)*5+3],states[i])          
qmat            

}

This function is called many times, as the point of the program is to optimize a paramter set. However, this function setup is slowing down the optimization phase very substantially because this function and a bunch of others like it keep getting called, and they keep redefining the arrays.

How can I define the arrays I need once, dynamically, with the relevant parameters as above, and then be able to call the matrix function with a new set of parameters for optimization?

Thanks!

Edit –

statespace is just an integer describing the number of states to use in the model, say 3/

statshockvar <- function(meanrev,longrun,sigma,sstate) {

longrun*sigma^2/(2*meanrev)*(1-exp(-longrun))^2+sigma^2/longrun*(exp(-longrun) - 
  exp(-2*longrun))*sstate

}

statshockvar – in this particular example is the discretized variance of a CIR model for the term structure

Edit 2 –

params looks like this – please note these are just arbitrary number

params = c(
0.3275,
0.07,
0.197,
0,
0.05,

0.01,
0.2,
0.3,
0,
0.05,

0.01,
0.1,
0.3,
0,
0.05)

states would be something like this –

states = c(0.07,0.07,0.07)

again these states are arbitrary.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:39:20+00:00Added an answer on June 17, 2026 at 6:39 pm

    Here’s a solution:

    Q <- function(params, states) {
      diag(mapply(function(y, z) statshockvar(y[1], y[2], y[3], z), 
                  lapply(seq(statespace), function(x) params[(x-1)*5 + 1:3]), 
                  states))
    }
    

    Test with the example parameters:

    Q(params, states)
    
                [,1]       [,2]        [,3]
    [1,] 0.002465305 0.00000000 0.000000000
    [2,] 0.000000000 0.03424762 0.000000000
    [3,] 0.000000000 0.00000000 0.009499883
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to dynamically change path to sqlite database depending on currently logged
I would like dynamically (via JavaScript) hide the address bar. I realize this can
I have this following where I would like to dynamically add the list elements
I would like to dynamically hide a button in one of my views depending
I would like to dynamically change the home icon in the ActionBar. This is
Currently I have this <body> ... </body> But I would like to dynamically add
I would like to dynamically allocate memory from the machine_init function in my arm
For debugging / performance tests I would like to dynamically add logging code to
I created Extjs.Panel and now I would like to dynamically load a content to
I am working on a children's book app and would like to dynamically populate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.