calculating mean and standard deviation

440 views
Skip to first unread message

Gazi Md Daud Iqbal

unread,
Apr 11, 2016, 9:53:23 PM4/11/16
to AMPL Modeling Language

 Hi,

I have defined my input data set as


read {i in 1..npts, j in 1..nparams}P[i, j]<mydata.txt;

 

For example, my text file look like

 

8

9

10

12

6

8

7

10

7

8

5

7

11

5

4

3

 

I defined the following parameters in my model.

param npts := 4;

param jthpt;

param nparams := 4;


Now I need to use normalized value for these input dataset. I have one constraints like :


subject to constraint11n : v[1] = (P[jthpt,2] - mean of 2nd column of above table)/standard deviation of 2nd column of above table ;

 

Is there any way to calculate mean and standard deviation in AMPL from a given data set? If so how to express it in AMPL?


Thanks in advance. 

 

Gazi Md Daud Iqbal

unread,
Apr 11, 2016, 10:02:04 PM4/11/16
to AMPL Modeling Language
Also I would like to calculate the maximum and minimum value of 2nd column of above table which I will use later for another purpose. How to get this maximum and minimum value in AMPL?

Robert Fourer

unread,
Apr 13, 2016, 6:30:44 PM4/13/16
to am...@googlegroups.com
You'll need to define the mean and standard deviation using formulas. For example,

param mean2nd = sum {i in 1..npts} P[i,2] / npts;
param stdev2nd = sqrt(sum {i in 1..npts} (P[i,2]-mean2nd)^2 / npts);

subject to constraint11n: v[1] = (P[jthpt,2] - mean2nd) / stdev2nd;

Bob Fourer
am...@googlegroups.com

=======

Robert Fourer

unread,
Apr 13, 2016, 6:32:04 PM4/13/16
to am...@googlegroups.com
There are min and max functions in AMPL for this purpose. Then can be used with the same syntax as sum, to minimize over and indexed collection of values. For example the minimum of the 2nd column is min {i in 1..npts} P[i,2].

Bob Fourer
am...@googlegroups.com

=======

From: am...@googlegroups.com [mailto:am...@googlegroups.com] On Behalf Of Gazi Md Daud Iqbal
Sent: Monday, April 11, 2016 9:02 PM
To: AMPL Modeling Language

Gazi Md Daud Iqbal

unread,
Apr 21, 2016, 10:15:34 PM4/21/16
to AMPL Modeling Language, 4...@ampl.com
Hi Bob,

Thanks for your help.  

I have added another parameter which is basically normalized value of second column.

param Q {i in 1..npts} = (P[i,2] – mean2nd) / std2nd;

 Q is a 4*1 matrix.

 

Now, instead of using data from table P, I would like to use Q data in a constraint.  

 

For example, previously it was

Subject to constraint 12n : v[2]= P[jthpt, 2];

Is it possible to define this constraint where my input data will be Q matrix?

 param npts := 4;

param jthpt;

param nparams := 4;

param mean2nd = sum {i in 1..npts} P[i,2] / npts; 


param stdev2nd = sqrt(sum {i in 1..npts} (P[i,2]-mean2nd)^2 / npts); 



Thanks in advance.

Robert Fourer

unread,
Apr 24, 2016, 9:19:47 AM4/24/16
to am...@googlegroups.com
After you define

param Q {i in 1..npts} = (P[i,2] – mean2nd) / std2nd;

you can use refer to Q in any constraint, for example by writing Q[i] in a constraint expression.
Reply all
Reply to author
Forward
0 new messages