# Switch point Metropolis tuning

**URL:** <https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082>\
**Category:** Questions\
**Created:** [April 13, 2018, 2:56am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082 "2018-04-13T02:56:01Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/adam/32/694_2.png) [@adam](https://discourse.pymc.io/u/adam)\
**Post date:** [April 13, 2018, 2:56am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/1 "2018-04-13T02:56:02Z")

</div>

I have data with potentially 3 switch points:  
 ![data](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/1X/f959ccf19a4d3a7206e372fed0d3cc3df3a3bce3.png)  
The model will automatically choose Metropolis for the sampling of the tau\_1 and tau\_2, and NUTS for the lambdas.

> N = np.arange(0, all\_points)  
> with pm.Model() as model:  
> alpha = 1.0/data.mean()  
> lambda\_1 = pm.Exponential(“lambda\_1”, alpha)  
> lambda\_2 = pm.Exponential(“lambda\_2”, alpha)  
> lambda\_3 = pm.Exponential(“lambda\_3”, alpha)
> 
> ```
> tau1 = pm.DiscreteUniform("tau1", lower=N.min(), upper=N.max()) 
> tau2 = pm.DiscreteUniform("tau2", lower=tau1, upper=N.max()) 
> 
> _mu = T.switch(tau1>=N,lambda_1,lambda_2)
> mu = T.switch(tau2>=N,_mu,lambda_3)
> 
> observation = pm.Poisson("obs", mu, observed=data)
> trace = pm.sample(10000, tune=1000)
> 
> ```

When tune=500 it doesn’t converge, but with tune=1000, both tau’s converge successfully.

 ![traced](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/1X/da42a5d98d671d406d749fbc23b285b80ac4071e.png)

**Can you please share with me and the Pymc3 community how tuning works or what should be considered when tuning a Metropolis. Do you know of articles/websites where I could read more about tuning of the Metropolis?**

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 13, 2018, 5:03am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/2 "2018-04-13T05:03:10Z")

</div>

I am surprised that discrete switch point works well here, still, I suggest you to use a continuous step function instead of switch, more details see: [https://stackoverflow.com/questions/49144144/convert-numpy-function-to-theano/49152694#49152694](https://stackoverflow.com/questions/49144144/convert-numpy-function-to-theano/49152694#49152694)

Back to your original question, in `pm.sample` each sampler has some target that it would try to reach during tuning. For example, in Metropolis, it adjusts the scale of the transition kernel so that the acceptance probability is around 50%

> <https://github.com/pymc-devs/pymc3/blob/a0ec8e2c1b51370490fb8bb0ef79b80fd15f9a64/pymc3/step_methods/metropolis.py#L141-L146>

In NUTS, tuning is much more complex as it optimizes the step size for leapfrog integrator, the mass matrix of the Hamiltonian transition kernel. The best reference for this is the Stan User Manuel [http://mc-stan.org/users/documentation/](http://mc-stan.org/users/documentation/) chapter 34.2, although we dont use the excat tuning algorithm as Stan.

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/adam/32/694_2.png) [@adam](https://discourse.pymc.io/u/adam)\
**Post date:** [April 13, 2018, 5:25am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/3 "2018-04-13T05:25:05Z")

</div>

When I try to run what you created:

```
def logistic(L, x0, k=500, t=np.linspace(0., 1., 1000)):
    return L/(1+tt.exp(-k*(t_-x0)))

with pm.Model() as m2:
    lambda0 = pm.Normal('lambda0', mu, sd=sd)
    lambdad = pm.Normal('lambdad', 0, sd=sd, shape=nbreak-1)
    trafo = Composed(pm.distributions.transforms.LogOdds(), Ordered())
    b = pm.Beta('b', 1., 1., shape=nbreak-1, transform=trafo,
                    testval=[0.3, 0.5])
    theta_ = pm.Deterministic('theta', tt.exp(lambda0 +
                                              logistic(lambdad[0], b[0]) +
                                              logistic(lambdad[1], b[1])))
    obs = pm.Poisson('obs', theta_, observed=y)
    
    trace = pm.sample(1000, tune=1000)

```

I get the following error:

> AttributeError: Can’t get attribute ‘Composed’ on \<module ‘ **main** ’ (built-in)\>

Is there any way around that?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 13, 2018, 5:37am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/4 "2018-04-13T05:37:25Z")

</div>

Yes, more details is in the notebook, link copied below:

> <https://github.com/junpenglao/Planet_Sakaar_Data_Science/blob/master/PyM3QnA/stackoverflow_49144144.ipynb>

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/adam/32/694_2.png) [@adam](https://discourse.pymc.io/u/adam)\
**Post date:** [April 13, 2018, 5:44am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/5 "2018-04-13T05:44:22Z")

</div>

I’m sorry for the confusion. I already ran the whole notebook the first time, but it gives the error as soon as you try to run the model.

> AttributeError: Can’t get attribute ‘Composed’ on \<module ‘main’ (built-in)\>

I pretty much copy-pasted everything but the error keeps repeating. I tried changing some parameters but I feel the problem is within the class Composed(Transform):

Not sure…

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 13, 2018, 5:58am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/6 "2018-04-13T05:58:17Z")

</div>

That’s pretty strange… what is your pymc3 version?  
Also, could you please paste the full error trace?

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/adam/32/694_2.png) [@adam](https://discourse.pymc.io/u/adam)\
**Post date:** [April 13, 2018, 10:51am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/7 "2018-04-13T10:51:49Z")

</div>

Pymc3 version 3.3  
I’m running it in jupyter notebook (version 5.3.1).  
When I run the model, it gets up to this part:

> Auto-assigning NUTS sampler…  
> Initializing NUTS using jitter+adapt\_diag…  
> c:\anaconda\lib\site-packages\pymc3\model.py:384: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.  
> if not np.issubdtype(var.dtype, float):  
> Multiprocess sampling (2 chains in 2 jobs)  
> NUTS: [b\_logodds\_ordered\_\_, lambdas]

Then it just gets stuck, but if I you look at the Jupyter command prompt you’d see:

> Process SpawnPoolWorker-2:  
> Traceback (most recent call last):  
> File “c:\anaconda\lib\multiprocessing\process.py”, line 249, in \_bootstrap  
> self.run()  
> File “c:\anaconda\lib\multiprocessing\process.py”, line 93, in run  
> self.\_target(\*self.\_args, \*\*self.\_kwargs)  
> File “c:\anaconda\lib\multiprocessing\pool.py”, line 108, in worker  
> task = get()  
> File “c:\anaconda\lib\site-packages\joblib\pool.py”, line 362, in get  
> return recv()  
> File “c:\anaconda\lib\multiprocessing\connection.py”, line 251, in recv  
> return \_ForkingPickler.loads(buf.getbuffer())  
> AttributeError: Can’t get attribute ‘Composed’ on \<module ‘ **main** ’ (built-in)\>

Do you recommend I update Jupyter or Pymc3 versions?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 13, 2018, 11:18am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/8 "2018-04-13T11:18:02Z")

</div>

Looks like you have problem of the multiprocessing - try upgrading joblib.  
For now, setting `njobs=1` in `pm.sample()` should temporally suppress the error.

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/adam/32/694_2.png) [@adam](https://discourse.pymc.io/u/adam)\
**Post date:** [April 13, 2018, 4:15pm UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/9 "2018-04-13T16:15:04Z")

</div>

Thank you, junpenglao, and thank you for your patience. Upgrading joblib didn’t do it, but it worked when changed the njobs=1.  
When I try to apply your model to other datasets that I am analyzing, is it correct that the testval would most likely change for ‘b’?

In the current model, the testval for ‘b’ is [0.3,0.5]:

```
with pm.Model() as m2:
    lambda0 = pm.Normal('lambda0', mu, sd=sd)
    lambdad = pm.Normal('lambdad', 0, sd=sd, shape=nbreak-1)
    trafo = Composed(pm.distributions.transforms.LogOdds(), Ordered())
    b = pm.Beta('b', 1., 1., shape=nbreak-1, transform=trafo,
                    testval=[0.3, 0.5])

```

Can you please share why you chose testval=[0.3, 0.5]. I know these are starting values for ‘b’, but would those change if my initial observed data was different?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 13, 2018, 4:25pm UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/10 "2018-04-13T16:25:38Z")

</div>

The testval here is to avoid a bug in the Ordered transformed: the default testval of the Beta(1, 1) is .5, and with shape=2 it would be [.5, .5], however it generate an error for Ordered as the two value is the same. So as long as you specify a value that is sorted but not the same then it would work.

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/adam/32/694_2.png) [@adam](https://discourse.pymc.io/u/adam)\
**Post date:** [April 13, 2018, 4:26pm UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/11 "2018-04-13T16:26:10Z")

</div>

thank you. 🙂

---

<div class="post-metadata">

**Author:** ![narendramukherjee](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/narendramukherjee/32/753_2.png) [@narendramukherjee](https://discourse.pymc.io/u/narendramukherjee)\
**Post date:** [April 28, 2018, 3:29pm UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/12 "2018-04-28T15:29:27Z")

</div>

Jutting in here as I have a very similar model that I am working with. I was previously using the DiscreteUniform distribution to model the switchpoints in my data, but now am trying to shift to the logistic as explained here.

I am trying to model my data with a single regression discontinuity - so two straight lines fit my data, and at some point in time, the model has to switch from one straight line to the other. Here’s an example:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/1X/b8c29ae5d760645e1ccc20000c9f503d138992d5.jpg)

I tried the same logistic idea as in @junpenglao 's gist, here’s the code:

```auto
    def logistic(L, x0, k=500, t=np.linspace(0., 1., 2500)):
         return L/(1+tt.exp(-k*(t-x0)))                                    

```

And the model:

```auto
    In [34]: with pm.Model() as model_before_700:                    
        ...: alpha = pm.Normal("alpha", mu = 0, sd = 3.0, shape = 2)           
        ...: beta = pm.Normal("beta", mu = 0, sd = 1.0, shape = 2)
        ...: switchpoints = pm.Beta("switchpoints", 1, 1)                       
        ...: sd = pm.HalfCauchy("sd", 0.5, shape = 2)
        ...: intercept = alpha[0] + logistic(alpha[1], switchpoints)
        ...: slope = beta[0] + logistic(beta[1], switchpoints)
        ...: dev = sd[0] + logistic(sd[1], switchpoints)
        ...: regression = intercept + slope*np.arange(2500)                    
        ...: observed = pm.Normal("observed", mu = regression, sd = dev, observed = data_before_700)                
    In [34]: with model_before_700:                                  
        ...: trace_before_700 = pm.sample(tune = 2000, draws = 1000, njobs = 3)
        ...: 

```

I consistently get:  
`ValueError: Mass matrix contains zeros on the diagonal. Some derivatives might always be zero.`

I have seen previous discussions saying that this might be due to overflow errors, so I think I might be missing something simple in my model. Any help is much appreciated 🙂

Here’s relevant versions etc:

```auto
    In [38]: pm. __version__
    Out[38]: '3.3'

    In [39]: theano. __version__
    Out[39]: '1.0.1'

    In [40]: joblib. __version__
    Out[40]: '0.11'

```

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 29, 2018, 6:51am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/13 "2018-04-29T06:51:59Z")

</div>

The error is likely cause by the exponential of large value, check the logistic function:

```python
x0=.5
k=500
t=np.linspace(0., 1., 2500)
np.exp(-k*(t-x0))
Out[38]: 
array([3.74645461e+108, 3.06709213e+108, 2.51092169e+108, ...,
       3.98260131e-109, 3.26041722e-109, 2.66919022e-109])

```

As you can see, the output is in a wildly large range. You should find a way to scale the parameter `k`.

---

<div class="post-metadata">

**Author:** ![narendramukherjee](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/narendramukherjee/32/753_2.png) [@narendramukherjee](https://discourse.pymc.io/u/narendramukherjee)\
**Post date:** [April 29, 2018, 2:29pm UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/14 "2018-04-29T14:29:04Z")

</div>

Thanks for the suggestion, I didn’t really need k to be as large as 500, for my purposes having k as 10 would be enough.

```auto
In [62]: x0=.5
    ...: k=10
    ...: t=np.linspace(0., 1., 2500)
    ...: np.exp(-k*(t-x0))
    ...: 
Out[62]: 
array([ 1.48413159e+02, 1.47820456e+02, 1.47230119e+02, ...,
         6.79208851e-03, 6.76496359e-03, 6.73794700e-03])

```

The range of the exponential is much smaller now, but the mass matrix issue persists ☹

The model again:

```auto
In [69]: def logistic(L, x0, k=10, t=np.linspace(0., 1., 2500)):
    ...: return L/(1+tt.exp(-k*(t-x0)))
    ...: 

In [70]: with pm.Model() as model_before_700:
    ...: alpha = pm.Normal("alpha", mu = 0, sd = 3.0, shape = 2)
    ...: beta = pm.Normal("beta", mu = 0, sd = 1.0, shape = 2)
    ...: switchpoints = pm.Beta("switchpoints", alpha =1, beta=1)
    ...: sd = pm.HalfCauchy("sd", 0.5, shape = 2)
    ...: intercept = alpha[0] + logistic(alpha[1], switchpoints)
    ...: slope = beta[0] + logistic(beta[1], switchpoints)
    ...: dev = sd[0] + logistic(sd[1], switchpoints)
    ...:     

In [71]: with model_before_700:
    ...: regression = intercept + slope*np.arange(2500)
    ...: observed = pm.Normal("observed", mu = regression, sd = dev, observed = data_before_700)
    ...:     
In [73]: with model_before_700:
    ...: trace_before_700 = pm.sample(tune = 2000, draws = 1000, njobs = 3)
    ...:     

```

I don’t know if this is helpful but: the sampler does sample quite a few iterations before halting with that mass matrix exception. The sampling is pretty slow (slower than using NUTS + Metropolis while using the DiscreteUniform switchpoint model) while it happens.

---

<div class="post-metadata">

**Author:** ![narendramukherjee](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/narendramukherjee/32/753_2.png) [@narendramukherjee](https://discourse.pymc.io/u/narendramukherjee)\
**Post date:** [April 29, 2018, 2:31pm UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/15 "2018-04-29T14:31:40Z")

</div>

Also, the exception is raised as a joblib value error (though that might just be due to each sampling process running into the exception on its own, and might not be an issue with joblib).

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/adam/32/694_2.png) [@adam](https://discourse.pymc.io/u/adam)\
**Post date:** [April 29, 2018, 2:33pm UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/16 "2018-04-29T14:33:21Z")

</div>

if it won’t take too long, I suggest changing njobs=3 to njobs=1

---

<div class="post-metadata">

**Author:** ![narendramukherjee](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/narendramukherjee/32/753_2.png) [@narendramukherjee](https://discourse.pymc.io/u/narendramukherjee)\
**Post date:** [April 29, 2018, 4:22pm UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/17 "2018-04-29T16:22:58Z")

</div>

Tried njobs=1, the error trace is smaller here so posting the whole thing:

```auto
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Sequential sampling (2 chains in 1 job)
NUTS: [sd_log __, switchpoints_logodds__ , beta, alpha]
 13%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u258e | 393/3000 [00:01<00:11, 233.55it/s]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-82-b868e0738426> in <module>()
      1 with model_before_700:
----> 2 trace_before_700 = pm.sample(tune = 2000, draws = 1000, njobs = 1)
      3 

/home/narendra/anaconda3/lib/python3.6/site-packages/pymc3/sampling.py in sample(draws, step, init, n_init, start, trace, chain_idx, chains, cores, tune, nuts_kwargs, step_kwargs, progressbar, model, random_seed, live_plot, discard_tuned_samples, live_plot_kwargs, compute_convergence_checks, use_mmap, **kwargs)
    460 _log.info('Sequential sampling ({} chains in 1 job)'.format(chains))
    461 _print_step_hierarchy(step)
--> 462 trace = _sample_many(**sample_args)
    463 
    464 discard = tune if discard_tuned_samples else 0

/home/narendra/anaconda3/lib/python3.6/site-packages/pymc3/sampling.py in _sample_many(draws, chain, chains, start, random_seed, step, **kwargs)
    508 for i in range(chains):
    509 trace = _sample(draws=draws, chain=chain + i, start=start[i],
--> 510 step=step, random_seed=random_seed[i], **kwargs)
    511 if trace is None:
    512 if len(traces) == 0:

/home/narendra/anaconda3/lib/python3.6/site-packages/pymc3/sampling.py in _sample(chain, progressbar, random_seed, start, draws, step, trace, tune, model, live_plot, live_plot_kwargs, **kwargs)
    552 try:
    553 strace = None
--> 554 for it, strace in enumerate(sampling):
    555 if live_plot:
    556 if live_plot_kwargs is None:

/home/narendra/anaconda3/lib/python3.6/site-packages/tqdm/_tqdm.py in __iter__ (self)
    951 """, fp_write=getattr(self.fp, 'write', sys.stderr.write))
    952 
--> 953 for obj in iterable:
    954 yield obj
    955 # Update and possibly print the progressbar.

/home/narendra/anaconda3/lib/python3.6/site-packages/pymc3/sampling.py in _iter_sample(draws, step, start, trace, chain, tune, model, random_seed)
    650 step = stop_tuning(step)
    651 if step.generates_stats:
--> 652 point, states = step.step(point)
    653 if strace.supports_sampler_stats:
    654 strace.record(point, states)

/home/narendra/anaconda3/lib/python3.6/site-packages/pymc3/step_methods/arraystep.py in step(self, point)
    220 
    221 if self.generates_stats:
--> 222 apoint, stats = self.astep(array)
    223 point = self._logp_dlogp_func.array_to_full_dict(apoint)
    224 return point, stats

/home/narendra/anaconda3/lib/python3.6/site-packages/pymc3/step_methods/hmc/base_hmc.py in astep(self, q0)
    113 
    114 if not np.isfinite(start.energy):
--> 115 self.potential.raise_ok()
    116 raise ValueError('Bad initial energy: %s. The model '
    117 'might be misspecified.' % start.energy)

/home/narendra/anaconda3/lib/python3.6/site-packages/pymc3/step_methods/hmc/quadpotential.py in raise_ok(self)
    188 def raise_ok(self):
    189 if np.any(self._stds == 0):
--> 190 raise ValueError('Mass matrix contains zeros on the diagonal. '
    191 'Some derivatives might always be zero.')
    192 if np.any(self._stds < 0):

ValueError: Mass matrix contains zeros on the diagonal. Some derivatives might always be zero.

```

Again, a few samples were drawn before the ValueError came up. The sampling was faster in this case while it happened, but since few samples were drawn before the ValueError, those might just be the initial tuning steps.

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/adam/32/694_2.png) [@adam](https://discourse.pymc.io/u/adam)\
**Post date:** [April 30, 2018, 5:04am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/18 "2018-04-30T05:04:52Z")

</div>

sorry. not sure what to tell you

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 30, 2018, 7:40am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/19 "2018-04-30T07:40:08Z")

</div>

Actually I am not sure whether changing the k is the right answer - it change the shape of the sigmoid which controls the sharpness of the switch point.

I will try to simulate some data and get back to you.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 30, 2018, 10:50am UTC](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082/20 "2018-04-30T10:50:59Z")

</div>

Right, so the problem is:

```python
regression = intercept + slope*np.arange(2500)

```

where `np.arange(2500)` scaled the parameter to a large range that is difficult to sample from.  
Standardizing the predictor seems to do the trick:

```python
regression = intercept + slope*np.linspace(0., 1., 2500)

```

[Next page](https://discourse.pymc.io/t/switch-point-metropolis-tuning/1082.md?page=2)
