# Optimization using STAN using Simplex

**URL:** <https://discourse.mc-stan.org/t/optimization-using-stan-using-simplex/8223>\
**Category:** Modeling\
**Tags:** fitting-issues, specification\
**Created:** [March 25, 2019, 7:00pm UTC](https://discourse.mc-stan.org/t/optimization-using-stan-using-simplex/8223 "2019-03-25T19:00:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BG\_2019](https://avatars.discourse-cdn.com/v4/letter/b/858c86/32.png) [@BG\_2019](https://discourse.mc-stan.org/u/BG_2019)\
**Post date:** [March 25, 2019, 7:00pm UTC](https://discourse.mc-stan.org/t/optimization-using-stan-using-simplex/8223/1 "2019-03-25T19:00:59Z")

</div>

Hi,  
I am trying to fit a model to optimize the response variable Y where  
Y = \beta\_0 + X\_1\beta\_1 + X\_2\beta\_2 such that X\_1\beta \_1 + X\_2\beta\_2 \leq 0.7 .

I was wondering, how to modify the below stan code or use simplex to solve the above problem.

* * *

```stan
data{
  int N;  
  int Np;
  int J;
  vector[Np] x;                             
}

parameters {
  vector<lower=0> [N] beta0;
  vector<lower=0> [N] beta1;
  vector<lower=0> [N] beta2;
  vector<lower=0, upper=0.7>[N] k_constant;
  simplex[Np] alpha[N];

}

transformed parameters {
  
  vector[Np] y_beta[N];
  for(i in 1:N) {
    y_beta[i,1] = beta1[i]*x[1];
    y_beta[i,2] = beta2[i]*x[2];
  
    y_beta[i] = k_constant[i] * alpha[i];   
} 
  
model {
  beta0 ~ normal(0,1);
  beta1 ~ normal(0,1);                     
  beta2 ~ normal(0,1); 

  y ~ normal(beta0 + beta1*x[1] + beta2*x[2], 1);
  
}
****
```

---

<div class="post-metadata">

**Author:** ![andre.pfeuffer](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@andre.pfeuffer](https://discourse.mc-stan.org/u/andre.pfeuffer)\
**Post date:** [April 4, 2019, 5:49am UTC](https://discourse.mc-stan.org/t/optimization-using-stan-using-simplex/8223/2 "2019-04-04T05:49:42Z")

</div>

> [@BG\_2019](#):
>
> vector\<lower=0, upper=0.7\>[N] k\_constant;

This defines a lower bound too, your definition not. If you want both, just use inv\_logit and scale by 0.7.
