This is in some ways a follow-up to a post I had a month ago, but it's different enough that I think it justifies its own post.
I have created a super simple example of what I am trying to do. I have a time series that has a single step in it. Basically I want to find the start and end of the step, and the magnitude. In a real-world case there would be noise added, plus a bunch of other things that are far more complicated (see earlier post for the complications if you interested) but for this test problem it is really easy. I have attached a file for one-stop shopping: source the file in R (and wait a while) and it should just work.
As written, the data vector y has a series of 1 for a while; then they become 1.6 for a while; then down to 1 again.
This is seemingly a discrete problem: there is some integer index istart which is the first place y[istart] becomes 1.6, and another index iend which is the last place where y[iend] = 1.6 before dropping down to 1.
What happens if I define continuous variables start and end that define the range? In my earlier problem this did not work. The issue is that if there's no difference in the model predictions when start = 10.1 vs start = 10.9 (for example), the likelihood function doesn't vary continuously and the sampler gets stuck. In either case, the prediction takes one value up to i=10 and another prediction from i=11 onward.
I fixed this by adding something to the prediction _before_ the start time. If the start time is 10.9 I add very little to the point at i=10; if the start time is 10.1 I add a lot to the point at i=10. Now, if the real start time is i=11 and the sampler is at 10.1, the likelihood will improve monotonically as the sampler moves towards 11. Genius! (I do the same thing at the other end. By the way, I implement this as a start time and a duration, not a start time and an end time).
The good news is: it works.
The bad news is: it works very very slowly. 100 or even 1000 iterations won't do it -- indeed, I initially thought it wasn't working at all, until I looked at the chains and saw very slow progress towards the right answer. At 100000 iterations (of which the first half are warm-up) it is clearly making good progress, but still hasn't gotten down to a narrow interval around the correct answer.
After posting this, I will let it do 200000 and see where that gets me. I will post a follow-up here.
If convergence takes hundreds of thousands of iterations even with this simple problem, there's no way I can use this approach for my real problem, which will have dozens of steps at unknown times, as well as other complications. I'm hoping there's an easy improvement.
I notice that the sampler takes really small steps. It will try a start of 20.0133, and then the next iteration it will be at 20.0131 or something. Obviously it will take a long time to reach the right answer of 5, even if it moves the right direction the whole time.
I'm aware that there are some stan parameters like adapt_delta and adapt_kappa and adapt_window and so on, that might matter here. But I haven't been able to find the documentation that explains these, and even if setting these parameters is the solution to my problem I could use some wisdom in how to figure out which ones to monkey with and in which directions, and, equally important, how to figure it out for a more complicated problem like the one I am working towards.
As always, advice is greatly appreciated.
Phil Price