Closed rklees closed 6 months ago
CC @jessegrabowski
I can take a look, but this is just a debug function. Why are you using it? There is a large test suite that checks that the statespace models match the statsmodels outputs.
I use it because it makes data generation for a given state-space model easy, much better than statsmodel (as far as I know).
In this specific case, statsmodels expects sigma squared, whereas the structural model expects sigma. If you remove the square root from the structural model (but not the statsmodel argument), you will get the same output, modulo random draws. I don't think setting the seed is enough to guarantee the same outputs between the two functions, because there might be differences in how/when each random number is generated (all at once vs one-by-one in loop, for example).
I'm not sure what your exact use-case in, but in general I suggest you use the usual PyMC API to generate draws from the statespace models, rather than the testing function.
I know that statsmodel expects variances and pymc-experimental standard deviations. That is not the problem. Given initial states and model parameters, pymc-experimental generates a dataset that is completely wrong. I can make a gist so that you can run my code yourself. R Klees
Sent from Outlook for Androidhttps://aka.ms/AAb9ysg
From: Jesse Grabowski @.> Sent: Thursday, January 4, 2024 2:59:26 PM To: pymc-devs/pymc-experimental @.> Cc: rklees @.>; Author @.> Subject: Re: [pymc-devs/pymc-experimental] Something wrong with simulate_from_numpy_model ?? (Issue #295)
In this specific case, statsmodels expects sigma squared, whereas the structural model expects sigma. If you remove the square root from the structural model (but not the statsmodel argument), you will get the same output, modulo random draws. I don't think setting the seed is enough to guarantee the same outputs between the two functions, because there might be differences in how/when each random number is generated (all at once vs one-by-one in loop, for example).
I'm not sure what your exact use-case in, but in general I suggest you use the usual PyMC API to generate draws from the statespace models, rather than the testing function.
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https://github.com/pymc-devs/pymc-experimental/issues/295*issuecomment-1877138340__;Iw!!PAKc-5URQlI!5GOedFTO3WVctLK_gjtGKdqJx2oOFRxUONeBWpmwq2zYcH0Mf-MiSz-yyUVgxNcqkqohLLEYOt1J1tFSGMiHo16_1g$, or unsubscribehttps://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AWKBIZONMGVFIF65XCSGJQTYM2YT5AVCNFSM6AAAAABBLSGJPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZXGEZTQMZUGA__;!!PAKc-5URQlI!5GOedFTO3WVctLK_gjtGKdqJx2oOFRxUONeBWpmwq2zYcH0Mf-MiSz-yyUVgxNcqkqohLLEYOt1J1tFSGMjz8ddL_A$. You are receiving this because you authored the thread.Message ID: @.***>
measurement_error = st.MeasurementError(name="obs")
IRW = st.LevelTrendComponent(order=2, innovations_order=[0, 1])
# Change sigma_trend to remove the square root
param_dict = {
"initial_trend": [0., 0.01], "sigma_trend": np.array([5e-7]),
"sigma_obs": np.array([0.]),
}
mod = IRW + measurement_error
nobs = 1024 # we simulate a time series of length Nobs
x, y = simulate_from_numpy_model(mod, np.random.default_rng(2163), param_dict, steps=nobs)
# sm part unchanged
fig, ax = plt.subplots()
ax.plot(y, label='Structural')
ax.plot(y_sim, label='Statsmodels')
ax.legend()
plt.show()
This now looks correct to me?
Your plot is fine. However, the plot I posted was generated with: measurement_error = st.MeasurementError(name="obs") IRW = st.LevelTrendComponent(order=2, innovations_order=[0, 1]) mod = IRW + measurement_error param_dict = { "initial_trend": np.array([0., 0.01]), "sigma_trend": np.array([np.sqrt(5e-7)]), "sigma_obs": np.array([0.]), } nobs = 1024 x, y = simulate_from_numpy_model(mod, np.random.default_rng(2163), param_dict, steps=nobs)
Because sigma_trend is the slope disturbance standard deviation as the name suggests, not the variance. Hence, I assumed that in param_dict I have to input the standard deviation of the trend. Your code suggests that "sigma_trend" is the variance of the trend disturbance. I noticed that for the IRW model, the matrix Q = [sigma_trend], but should be [sigma_trend*signa_trend], because it is the state covariance matrix.
So, please tell me whether in param_dict I have to input the state disturbance variances (then, the name should be sigma2_trend etc.) and not the state disturbance standard deviations (then, the name should be sigma_trend etc).
R Klees
From: Jesse Grabowski @.> Reply to: pymc-devs/pymc-experimental @.> Date: Thursday, 4 January 2024 at 18:29 To: pymc-devs/pymc-experimental @.> Cc: Roland Klees @.>, Author @.***> Subject: Re: [pymc-devs/pymc-experimental] Something wrong with simulate_from_numpy_model ?? (Issue #295)
measurement_error = st.MeasurementError(name="obs")
IRW = st.LevelTrendComponent(order=2, innovations_order=[0, 1])
param_dict = {
"initial_trend": [0., 0.01], "sigma_trend": np.array([5e-7]),
"sigma_obs": np.array([0.]),
}
mod = IRW + measurement_error
nobs = 1024 # we simulate a time series of length Nobs
x, y = simulate_from_numpy_model(mod, np.random.default_rng(2163), param_dict, steps=nobs)
plt.plot(y)
Untitled.png (view on web)https://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/assets/48652735/b5a99ff1-e888-4b2b-92b9-5af9f66e9571__;!!PAKc-5URQlI!97wysGBjWmVV157W3XU9OlmsBrPL8m2yJdsPpvwNGI7aFHTOJhKLqk_w2kvqOQTcpfkyAnBkvECDcNE_X-bWvbKyIQ$
This now looks correct to me?
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/issues/295*issuecomment-1877494791__;Iw!!PAKc-5URQlI!97wysGBjWmVV157W3XU9OlmsBrPL8m2yJdsPpvwNGI7aFHTOJhKLqk_w2kvqOQTcpfkyAnBkvECDcNE_X-YfkzoofA$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AWKBIZKP636UPJJNJMDO2MDYM3RJFAVCNFSM6AAAAABBLSGJPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZXGQ4TINZZGE__;!!PAKc-5URQlI!97wysGBjWmVV157W3XU9OlmsBrPL8m2yJdsPpvwNGI7aFHTOJhKLqk_w2kvqOQTcpfkyAnBkvECDcNE_X-ZF2FSUzw$. You are receiving this because you authored the thread.Message ID: @.***>
Yeah that's a fair point, the name is not consistent with what you get. Currently, whatever number you pass in is directly plugged into the Q matrix at whatever position on the main diagonal, so either it should be squared internally (this is what statsmodels does) or the name should be changed.
My preference would be to square things internally and keep the name the same, because no other PyMC distributions are parameterized by the variance directly. Does that seem reasonable?
I agree. In fact, it does not matter as long as the names are consistent. As is, they are not. Roland
From: Jesse Grabowski @.> Reply to: pymc-devs/pymc-experimental @.> Date: Thursday, 4 January 2024 at 18:44 To: pymc-devs/pymc-experimental @.> Cc: Roland Klees @.>, Author @.***> Subject: Re: [pymc-devs/pymc-experimental] Something wrong with simulate_from_numpy_model ?? (Issue #295)
Yeah that's a fair point, the name is not consistent with what you get. Currently, whatever number you pass in is directly plugged into the Q matrix at whatever position on the main diagonal, so either it should be squared internally (this is what statsmodels does) or the name should be changed.
My preference would be to square things internally and keep the name the same, because no other PyMC distributions are parameterized by the variance directly. Does that seem reasonable?
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/issues/295*issuecomment-1877513897__;Iw!!PAKc-5URQlI!8-uxLAQw9mlLFukZxy1UpJgBJjPa5nh__wiMeKC9mn59fq3b7ZH110jFOut3RoftC33G52ObvltBUdhtT2IT7JbXRQ$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AWKBIZMCDNXJZKCNSLCIIW3YM3TA7AVCNFSM6AAAAABBLSGJPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZXGUYTGOBZG4__;!!PAKc-5URQlI!8-uxLAQw9mlLFukZxy1UpJgBJjPa5nh__wiMeKC9mn59fq3b7ZH110jFOut3RoftC33G52ObvltBUdhtT2IQkDZ3GA$. You are receiving this because you authored the thread.Message ID: @.***>
By the way, same applies to “sigma_obs” Roland
From: Jesse Grabowski @.> Reply to: pymc-devs/pymc-experimental @.> Date: Thursday, 4 January 2024 at 18:44 To: pymc-devs/pymc-experimental @.> Cc: Roland Klees @.>, Author @.***> Subject: Re: [pymc-devs/pymc-experimental] Something wrong with simulate_from_numpy_model ?? (Issue #295)
Yeah that's a fair point, the name is not consistent with what you get. Currently, whatever number you pass in is directly plugged into the Q matrix at whatever position on the main diagonal, so either it should be squared internally (this is what statsmodels does) or the name should be changed.
My preference would be to square things internally and keep the name the same, because no other PyMC distributions are parameterized by the variance directly. Does that seem reasonable?
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/issues/295*issuecomment-1877513897__;Iw!!PAKc-5URQlI!8-uxLAQw9mlLFukZxy1UpJgBJjPa5nh__wiMeKC9mn59fq3b7ZH110jFOut3RoftC33G52ObvltBUdhtT2IT7JbXRQ$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AWKBIZMCDNXJZKCNSLCIIW3YM3TA7AVCNFSM6AAAAABBLSGJPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZXGUYTGOBZG4__;!!PAKc-5URQlI!8-uxLAQw9mlLFukZxy1UpJgBJjPa5nh__wiMeKC9mn59fq3b7ZH110jFOut3RoftC33G52ObvltBUdhtT2IQkDZ3GA$. You are receiving this because you authored the thread.Message ID: @.***>
It applies to all shocks in all structural models I believe. I will have to check them all.
Dear Jesse,
Do I only need to download the latest version of pymc_experimental to fix this bug?
Roland Klees
From: Jesse Grabowski @.> Reply to: pymc-devs/pymc-experimental @.> Date: Sunday, 7 January 2024 at 01:49 To: pymc-devs/pymc-experimental @.> Cc: Roland Klees @.>, Author @.***> Subject: Re: [pymc-devs/pymc-experimental] Standard deviation parameters are incorrectly treated as variances in statespace covariance matrices (Issue #295)
Closed #295https://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/issues/295__;!!PAKc-5URQlI!-G6k2-DygvWZRuIXSL4rymvpTCII2Evf2BPl8bqNDE77VnoU_Tn-aSMVkUsZbKyxnoDyfGKFcPk0rxR3B5qs4bvVvQ$ as completed via #296https://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/pull/296__;!!PAKc-5URQlI!-G6k2-DygvWZRuIXSL4rymvpTCII2Evf2BPl8bqNDE77VnoU_Tn-aSMVkUsZbKyxnoDyfGKFcPk0rxR3B5odPse4_A$.
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/issues/295*event-11409989054__;Iw!!PAKc-5URQlI!-G6k2-DygvWZRuIXSL4rymvpTCII2Evf2BPl8bqNDE77VnoU_Tn-aSMVkUsZbKyxnoDyfGKFcPk0rxR3B5otXqildg$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AWKBIZPLGLCIGIUYQP6EGYTYNHWHVAVCNFSM6AAAAABBLSGJPWVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJRGQYDSOJYHEYDKNA__;!!PAKc-5URQlI!-G6k2-DygvWZRuIXSL4rymvpTCII2Evf2BPl8bqNDE77VnoU_Tn-aSMVkUsZbKyxnoDyfGKFcPk0rxR3B5o7nkn-KA$. You are receiving this because you authored the thread.Message ID: @.***>
You'll have to pip install the main branch from github. We haven't done a new release yet, so just doing pip install pymc-experimental
won't have the patch
I am not very familiar with github. What is the syntax which does this? Roland
Sent from Outlook for Androidhttps://aka.ms/AAb9ysg
From: Jesse Grabowski @.> Sent: Sunday, January 7, 2024 6:00:04 PM To: pymc-devs/pymc-experimental @.> Cc: rklees @.>; Author @.> Subject: Re: [pymc-devs/pymc-experimental] Standard deviation parameters are incorrectly treated as variances in statespace covariance matrices (Issue #295)
You'll have to pip install the main branch from github. We haven't done a new release yet, so just doing pip install pymc-experimental won't have the patch
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https://github.com/pymc-devs/pymc-experimental/issues/295*issuecomment-1880112822__;Iw!!PAKc-5URQlI!6pAK6xf9b-Swpow7wZzY4dk_jHaLODECsALrJzQxf_avEzDTToIUnUy6quuFR2ZUvcrhVR6psaIgYcHCrl4TJCXuEQ$, or unsubscribehttps://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AWKBIZP3P5GBIBIS2ZV342TYNLIBJAVCNFSM6AAAAABBLSGJPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBQGEYTEOBSGI__;!!PAKc-5URQlI!6pAK6xf9b-Swpow7wZzY4dk_jHaLODECsALrJzQxf_avEzDTToIUnUy6quuFR2ZUvcrhVR6psaIgYcHCrl4oY4fuKw$. You are receiving this because you authored the thread.Message ID: @.***>
It's similar to what you did before when you were installing from my branch, but now just targeting the main branch. Should be something like:
pip install git+https://github.com/pymc-devs/pymc-experimental.git
Sorry, but this command does not implement the changes. Everything stays the same. Roland
From: Jesse Grabowski @.> Reply to: pymc-devs/pymc-experimental @.> Date: Sunday, 7 January 2024 at 19:41 To: pymc-devs/pymc-experimental @.> Cc: Roland Klees @.>, Author @.***> Subject: Re: [pymc-devs/pymc-experimental] Standard deviation parameters are incorrectly treated as variances in statespace covariance matrices (Issue #295)
It's similar to what you did before when you were installing from my branch, but now just targeting the main branch. Should be something like:
pip install git+https://github.com/pymc-devs/pymc-experimental.git
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/issues/295*issuecomment-1880138355__;Iw!!PAKc-5URQlI!8o_q8J94GJVdUHkvVisxBjoPTeA-FKYkfsjuH1faIU8rZtXkOJsGhr1Pu-trcbFIOG0My-yicuEhKr-8h8T2c9tIGQ$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AWKBIZLQIZ7H4QHL66IUZPDYNLT5FAVCNFSM6AAAAABBLSGJPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBQGEZTQMZVGU__;!!PAKc-5URQlI!8o_q8J94GJVdUHkvVisxBjoPTeA-FKYkfsjuH1faIU8rZtXkOJsGhr1Pu-trcbFIOG0My-yicuEhKr-8h8SqasIftQ$. You are receiving this because you authored the thread.Message ID: @.***>
Make sure you pip uninstall pymc-experimental first
Did it, and then used your syntax to install pymc_experimental. However, sigma_xxx is still the entry in Q and H. Roland
From: Jesse Grabowski @.> Reply to: pymc-devs/pymc-experimental @.> Date: Sunday, 7 January 2024 at 20:04 To: pymc-devs/pymc-experimental @.> Cc: Roland Klees @.>, Author @.***> Subject: Re: [pymc-devs/pymc-experimental] Standard deviation parameters are incorrectly treated as variances in statespace covariance matrices (Issue #295)
Make sure you pip uninstall pymc-experimental first
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/issues/295*issuecomment-1880143690__;Iw!!PAKc-5URQlI!9z4m65ISItXNPZ5Drb_AqW479LV8Nps3uN3HvKmrj4s-sRRpsoYhDTuDSo7wMcmcqaSKbof5G_QuMrUVmMwPmX04Bw$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AWKBIZJQGRWBC7UYK2LKAX3YNLWURAVCNFSM6AAAAABBLSGJPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBQGE2DGNRZGA__;!!PAKc-5URQlI!9z4m65ISItXNPZ5Drb_AqW479LV8Nps3uN3HvKmrj4s-sRRpsoYhDTuDSo7wMcmcqaSKbof5G_QuMrUVmMz57Cj2Tw$. You are receiving this because you authored the thread.Message ID: @.***>
Your command just reinstalls pymc-experimental-0.0.16, which according to you does not contain the bug fix. Roland
From: Jesse Grabowski @.> Reply to: pymc-devs/pymc-experimental @.> Date: Sunday, 7 January 2024 at 20:04 To: pymc-devs/pymc-experimental @.> Cc: Roland Klees @.>, Author @.***> Subject: Re: [pymc-devs/pymc-experimental] Standard deviation parameters are incorrectly treated as variances in statespace covariance matrices (Issue #295)
Make sure you pip uninstall pymc-experimental first
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/issues/295*issuecomment-1880143690__;Iw!!PAKc-5URQlI!9z4m65ISItXNPZ5Drb_AqW479LV8Nps3uN3HvKmrj4s-sRRpsoYhDTuDSo7wMcmcqaSKbof5G_QuMrUVmMwPmX04Bw$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AWKBIZJQGRWBC7UYK2LKAX3YNLWURAVCNFSM6AAAAABBLSGJPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBQGE2DGNRZGA__;!!PAKc-5URQlI!9z4m65ISItXNPZ5Drb_AqW479LV8Nps3uN3HvKmrj4s-sRRpsoYhDTuDSo7wMcmcqaSKbof5G_QuMrUVmMz57Cj2Tw$. You are receiving this because you authored the thread.Message ID: @.***>
I just tested the command on my computer and it seems like it works. There hasn't been a release, so the version won't be bumped by this command.
You can check the source code by e.g. st.MeasurementError??
(in a Jupyter notebook) and check that you see the sigma being squared in the make_symbolic_graph
method, like this:
def make_symbolic_graph(self) -> None:
error_sigma = self.make_and_register_variable(f"sigma_{self.name}", shape=(self.k_endog,))
diag_idx = np.diag_indices(self.k_endog)
idx = np.s_["obs_cov", diag_idx[0], diag_idx[1]]
self.ssm[idx] = error_sigma**2
You are right. I had to restart the kernel to activate the changes. Thanks a lot.
Roland
From: Jesse Grabowski @.> Reply to: pymc-devs/pymc-experimental @.> Date: Sunday, 7 January 2024 at 20:29 To: pymc-devs/pymc-experimental @.> Cc: Roland Klees @.>, Author @.***> Subject: Re: [pymc-devs/pymc-experimental] Standard deviation parameters are incorrectly treated as variances in statespace covariance matrices (Issue #295)
I just tested the command on my computer and it seems like it works. There hasn't been a release, so the version won't be bumped by this command.
You can check the source code by e.g. st.MeasurementError?? (in a Jupyter notebook) and check that you see the sigma being squared in the make_symbolic_graph method, like this:
def make_symbolic_graph(self) -> None:
error_sigma = self.make_and_register_variable(f"sigma_{self.name}", shape=(self.k_endog,))
diag_idx = np.diag_indices(self.k_endog)
idx = np.s_["obs_cov", diag_idx[0], diag_idx[1]]
self.ssm[idx] = error_sigma**2
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/issues/295*issuecomment-1880149106__;Iw!!PAKc-5URQlI!5yruN-KbkUnT1_2QldFZLtXHd6R55GBhIuuIhjDOK6krMPpuyqNzWOlFNsVp_GlTWXXe5evYXwICTu95JeuY8UwRtA$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AWKBIZPD7U4KTFCFIAYZAM3YNLZQJAVCNFSM6AAAAABBLSGJPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBQGE2DSMJQGY__;!!PAKc-5URQlI!5yruN-KbkUnT1_2QldFZLtXHd6R55GBhIuuIhjDOK6krMPpuyqNzWOlFNsVp_GlTWXXe5evYXwICTu95Jetc6LK8cw$. You are receiving this because you authored the thread.Message ID: @.***>
Is it correct that in pymc.model I still define priors for the disturbance (shock) standard deviations (and not for the disturbance (shock) variances)? Roland
From: Jesse Grabowski @.> Reply to: pymc-devs/pymc-experimental @.> Date: Sunday, 7 January 2024 at 20:29 To: pymc-devs/pymc-experimental @.> Cc: Roland Klees @.>, Author @.***> Subject: Re: [pymc-devs/pymc-experimental] Standard deviation parameters are incorrectly treated as variances in statespace covariance matrices (Issue #295)
I just tested the command on my computer and it seems like it works. There hasn't been a release, so the version won't be bumped by this command.
You can check the source code by e.g. st.MeasurementError?? (in a Jupyter notebook) and check that you see the sigma being squared in the make_symbolic_graph method, like this:
def make_symbolic_graph(self) -> None:
error_sigma = self.make_and_register_variable(f"sigma_{self.name}", shape=(self.k_endog,))
diag_idx = np.diag_indices(self.k_endog)
idx = np.s_["obs_cov", diag_idx[0], diag_idx[1]]
self.ssm[idx] = error_sigma**2
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/issues/295*issuecomment-1880149106__;Iw!!PAKc-5URQlI!5yruN-KbkUnT1_2QldFZLtXHd6R55GBhIuuIhjDOK6krMPpuyqNzWOlFNsVp_GlTWXXe5evYXwICTu95JeuY8UwRtA$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AWKBIZPD7U4KTFCFIAYZAM3YNLZQJAVCNFSM6AAAAABBLSGJPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBQGE2DSMJQGY__;!!PAKc-5URQlI!5yruN-KbkUnT1_2QldFZLtXHd6R55GBhIuuIhjDOK6krMPpuyqNzWOlFNsVp_GlTWXXe5evYXwICTu95Jetc6LK8cw$. You are receiving this because you authored the thread.Message ID: @.***>
Everything works as it appears now. You put a prior on sigma (the standard deviation), which is then squared and placed on the diagonal of the covariance matrix.
Great. Thanks. Roland
From: Jesse Grabowski @.> Reply to: pymc-devs/pymc-experimental @.> Date: Sunday, 7 January 2024 at 20:56 To: pymc-devs/pymc-experimental @.> Cc: Roland Klees @.>, Author @.***> Subject: Re: [pymc-devs/pymc-experimental] Standard deviation parameters are incorrectly treated as variances in statespace covariance matrices (Issue #295)
Everything works as it appears now. You put a prior on sigma (the standard deviation), which is then squared and placed on the diagonal of the covariance matrix.
— Reply to this email directly, view it on GitHubhttps://urldefense.com/v3/__https:/github.com/pymc-devs/pymc-experimental/issues/295*issuecomment-1880155466__;Iw!!PAKc-5URQlI!6ufSzAfVddtGZny7X0o3gmIveeahcqFKQkIH5c6ny42BjC2_d5OZOCH67op3hu_kxdHmRHxb53Q-nguC2yCAWMKjSA$, or unsubscribehttps://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AWKBIZPHO5I656ZYWEQFNEDYNL4VDAVCNFSM6AAAAABBLSGJPWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBQGE2TKNBWGY__;!!PAKc-5URQlI!6ufSzAfVddtGZny7X0o3gmIveeahcqFKQkIH5c6ny42BjC2_d5OZOCH67op3hu_kxdHmRHxb53Q-nguC2yBPvMN0QA$. You are receiving this because you authored the thread.Message ID: @.***>
I simulate data from a simple integrated random walk model defining the initial state, the measurement disturbance standard deviation (here, I set it equal to 0), and the trend disturbance standard deviation. Given these parameters, the simulated data should be close to a straight line. However, is looks completely different. Therefore, I used statsmodel to simulate the data using the same initial state and disturbance standard deviations. Also the random generator is initialized the same in both simulations. The simulate dataset from statsmodel is close to a straight line as expected. Therefore, I wonder whether there is something wrong in simulate_from_numpy_model. Here is the code:
measurement_error = st.MeasurementError(name="obs") IRW = st.LevelTrendComponent(order=2, innovations_order=[0, 1])
param_dict = { "initial_trend": [0., 0.01], "sigma_trend": np.array([np.sqrt(5e-7)]), "sigma_obs": np.array([0.]), }
mod = IRW + measurement_error nobs = 1024 # we simulate a time series of length Nobs
x, y = simulate_from_numpy_model(mod, np.random.default_rng(2163), param_dict, steps=nobs)
And here is the code from statsmodel: model = sm.tsa.UnobservedComponents(y, level=True, stochastic_level=False, trend=True, stochastic_trend=True, irregular=True) res = model.fit() y_sim = model.simulate(initial_state = [0., 0.01], params=[0., 5e-7], nsimulations=nobs, random_state=np.random.default_rng(2163))
y and y_sim have nothing common. Whereas y_sim represents a slight deviation from the straight line, y is completely different, with a strong negative slope, though the undisturbed slope is positive. Attached the simulated data in both cases.
![statsmodel](https://github.com/pymc-devs/pymc-experimental/assets/93590629/93d29622-424c-4fec-be86-86f4a37cbac0)