## How to do a summation

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.

### How to do a summation

Hello, I want to do a summation but I don't know how to do it

Following is my initial code.
I don't know how to write a loop to do the summation.

Could anyone help me?

Code: Select all
`dec real L k o sigxdec series intergral(i)nonlin L k o sigx varxcompute M=100frml integral = (1/M)*exp(2*o*sin(2*%pi*(i*L/M)))*exp(-2*k*(t-(i*t/M)))do i=0,Mcompute integral = integral + integralend do`
huanpipt

Posts: 23
Joined: Sun Mar 27, 2011 7:52 am

### Re: How to do a summation

Use SSTATS, though you need to be careful about your integer divides. As you've written it, 1/M is zero---you want 1.0/M, and you'll have similar problems with i*t/M. For SSTATS, the loop index has to be T, so it looks like you'll need to switch the T and I subscripts in your formula. Something like:

SSTATS 0 M (1/M)*exp(2*o*sin(2*%pi*(t*L/M)))*exp(-2*k*(i*(1.0-t)/M)))>>integral
TomDoan

Posts: 2725
Joined: Wed Nov 01, 2006 5:36 pm

### Re: How to do a summation

Thanks for response.

In fact, I'd like to estimate the four parameters in the equation, as following

L k o sigx are four parameters

equation :
ec = exp(-r*TT)*(S*%cdf(d1)-S*%cdf(d1-(varf^(0.5))))
d1 = (log(S/S)+0.5*varf)/(varf^(0.5))
varf = varx*exp(-2*k*(TT))*integral
integral = summation from t=0 to t=M of (1.0/M)*exp(2*o*sin(2*%pi*(t*L/M)))*exp(-2*k*(ii*(1.0-t)/M))

I run the following code, however there is an error message
## SR10. Missing Values And/Or SMPL Options Leave No Usable Data Points
Could anyone help me with this?

Thanks very much.

Code: Select all
`open data TD3monthc2.xlsCALENDAR(irregular)data(format=xls,org=columns) / ii TT r S A********************************************************************dec real L k o sigx varxdec series intergralnonlin L k o sigxcompute M = 100SSTATS 0 M (1.0/M)*exp(2*o*sin(2*%pi*(t*L/M)))*exp(-2*k*(ii*(1.0-t)/M))>>integralcompute varx = sigx**2frml varf = varx*exp(-2*k*(TT))*integralfrml d1 = (log(S/S)+0.5*varf)/(varf^(0.5))frml ec = exp(-r*TT)*(S*%cdf(d1)-S*%cdf(d1-(varf^(0.5))))frml RR = A - ecnlls(frml=RR,piters=10,iters=100) * 1 99 UU`
huanpipt

Posts: 23
Joined: Sun Mar 27, 2011 7:52 am

### Re: How to do a summation

You will need to create a FUNCTION which evaluates the integral at a given value of T. See, for instance, the BONDS.RPF example, which does NLLS using a functional form which, like your integral, isn't closed form. As you have it written, INTEGRAL is computed only once, not once per entry in each function evaluation.
TomDoan

Posts: 2725
Joined: Wed Nov 01, 2006 5:36 pm

### Re: How to do a summation

Dear Tom,

I follow the BOND.rpf to revise my code while there an error message that I cant solve.

1 part to set an loop of summation
Code: Select all
`dec real L k o sigx varx Mnonlin L k o sigxcompute M = 100function opvalue ptype real opvaluetype integer p*integral=opvaluecompute pp = 0while pp<100 {compute opvalue = (1/M)*exp(2*o*sin(2*%pi*(pp*L/M)))*exp(-2*k*(ii*(1-pp)/M))compute opvalue = opvalue + opvaluecompute pp=pp+1}end while`

2nd part to sum all things to opvalue and then to estimate parameters
the error message appeared in this part

## SX22. Expected Type REAL, Got MATRIX(REAL) Instead
>>>>f(d1)-S*%cdf(d2)))<<<<

Code: Select all
`compute varx = sigx**2compute varf = varx*exp(-2*k*(TT(p)))*opvaluecompute d1 = (log(S(p)/S(p))+0.5*varf)/(varf^(0.5))compute d2 = d1-(varf^(0.5))compute opvalue = exp(-r(p)*TT(p))*(S*(%cdf(d1)-S*%cdf(d2)))*frml optionprice A = opvalue(t)nlls(frml=optionprice,piters=10,iters=100) A 1 99 UU`

I'm confused that I didn't set any matrix, why error message inform that.
huanpipt

Posts: 23
Joined: Sun Mar 27, 2011 7:52 am

### Re: How to do a summation

huanpipt wrote:nonlin L k o sigx
compute M = 100
function opvalue p
type real opvalue
type integer p

*integral=opvalue

compute pp = 0
while pp<100 {
compute opvalue = (1/M)*exp(2*o*sin(2*%pi*(pp*L/M)))*exp(-2*k*(ii*(1-pp)/M))
compute opvalue = opvalue + opvalue
compute pp=pp+1
}end while

This has all kinds of problems. First, you need to read the message earlier about integer divides. 1/M and 1.0/M aren't the same thing and neither are (1-pp)/M and (1.0-pp)/M. Also, what do you think opvalue = opvalue + opvalue is going to do? What it will do is give you double the pp=99 value of the (1/M)... calculation. You need one variable for the pp-specific calculation, and a different one for the summation across pp.

Is there anything in this which depends upon data, or is this just a function of the non-linear parameters? And what is the argument "P" used for?

huanpipt wrote:2nd part to sum all things to opvalue and then to estimate parameters
the error message appeared in this part

## SX22. Expected Type REAL, Got MATRIX(REAL) Instead
>>>>f(d1)-S*%cdf(d2)))<<<<

Code: Select all
`compute varx = sigx**2compute varf = varx*exp(-2*k*(TT(p)))*opvaluecompute d1 = (log(S(p)/S(p))+0.5*varf)/(varf^(0.5))compute d2 = d1-(varf^(0.5))compute opvalue = exp(-r(p)*TT(p))*(S*(%cdf(d1)-S*%cdf(d2)))*frml optionprice A = opvalue(t)nlls(frml=optionprice,piters=10,iters=100) A 1 99 UU`

I'm confused that I didn't set any matrix, why error message inform that.

What's S. Isn't it a series? If you use it in a COMPUTE, it will be treated as a VECTOR. You're also using OPVALUE for two different purposes, and that entire second calculation is set up for executing just once, not once per function evaluation. That second one needs a different name, and it needs to be a function of the entry number or a FRML since it depends upon the data S.
TomDoan

Posts: 2725
Joined: Wed Nov 01, 2006 5:36 pm

### Re: How to do a summation

TomDoan wrote:
huanpipt wrote:nonlin L k o sigx
compute M = 100
function opvalue p
type real opvalue
type integer p

*integral=opvalue

compute pp = 0
while pp<100 {
compute opvalue = (1/M)*exp(2*o*sin(2*%pi*(pp*L/M)))*exp(-2*k*(ii*(1-pp)/M))
compute opvalue = opvalue + opvalue
compute pp=pp+1
}end while

This has all kinds of problems. First, you need to read the message earlier about integer divides. 1/M and 1.0/M aren't the same thing and neither are (1-pp)/M and (1.0-pp)/M. Also, what do you think opvalue = opvalue + opvalue is going to do? What it will do is give you double the pp=99 value of the (1/M)... calculation. You need one variable for the pp-specific calculation, and a different one for the summation across pp.

Is there anything in this which depends upon data, or is this just a function of the non-linear parameters? And what is the argument "P" used for?

I thought I could use opvalue= opvalue(pp)+opvalue(pp+1) because there is another opvalue above.
I'll revised it again.

TomDoan wrote:
huanpipt wrote:2nd part to sum all things to opvalue and then to estimate parameters
the error message appeared in this part

## SX22. Expected Type REAL, Got MATRIX(REAL) Instead
>>>>f(d1)-S*%cdf(d2)))<<<<

Code: Select all
`compute varx = sigx**2compute varf = varx*exp(-2*k*(TT(p)))*opvaluecompute d1 = (log(S(p)/S(p))+0.5*varf)/(varf^(0.5))compute d2 = d1-(varf^(0.5))compute opvalue = exp(-r(p)*TT(p))*(S*(%cdf(d1)-S*%cdf(d2)))*frml optionprice A = opvalue(t)nlls(frml=optionprice,piters=10,iters=100) A 1 99 UU`

I'm confused that I didn't set any matrix, why error message inform that.

What's S. Isn't it a series? If you use it in a COMPUTE, it will be treated as a VECTOR. You're also using OPVALUE for two different purposes, and that entire second calculation is set up for executing just once, not once per function evaluation. That second one needs a different name, and it needs to be a function of the entry number or a FRML since it depends upon the data S.

S is a series. Here I want to simulate the process in BOND.RPF - Add separate values into opvalue- so I add the summation part in the 1st part then add other part in 2nd part.
I thought I might misunderstand the FUNCTION, I'll try to revise it again.

huanpipt

Posts: 23
Joined: Sun Mar 27, 2011 7:52 am

### Re: How to do a summation

Dear Tom,

I revise the code

I use the function to do the summation integral
p I want to set it as different time, just as the code "compute mdate=maturity(bond)" in BONDS.rpf
Because there is only one variable ii in my equation so I only wrote "compute iii=ii(p)"
Code: Select all
`dec real L k o sigx Mnonlin L k o sigxcompute M = 100function integral ptype real integraltype integer plocal real ppcompute integral = 0compute iii = ii(p)compute pp = 0while pp<M {compute partintegral = (1.0/M)*exp(2*o*sin(2*%pi*(pp*L/M)))*exp(-2*k*(iii*(1.0-pp)/M))compute integral = integral + partintegralcompute pp = pp + 1}end whileend`

Then, I use SET to set each part of calculation

Code: Select all
`frml varf = sigx*exp(-2*k*(TT))*integral(t)set d1 = (log(S/S)+0.5*varf)/(varf^(0.5))set d2 = d1-(varf^(0.5))set opvalue = exp(-r*TT)*(S*(%cdf(d1)-S*%cdf(d2)))frml optionprice A = opvalue(t)nlls(frml=optionprice,piters=10,iters=100) A 1 99 UU`

However, there is a error message.
## SR10. Missing Values And/Or SMPL Options Leave No Usable Data Points
If I shouldn't end the FUNCTION in the 1st part ?
huanpipt

Posts: 23
Joined: Sun Mar 27, 2011 7:52 am

### Re: How to do a summation

Dear Tom,

I revised the code today. The 2nd part is put into FUNCTION in this code, but the error message still exists.

Thank you very much.

Code: Select all
`dec real L k o sigxnonlin L k o sigxfunction opvalue ptype real opvaluetype integer plocal real pp iii M SS rp TTT integral varf d1 d2compute integral = 0compute iii = ii(p)compute M = 100compute pp = 0while pp<M {compute integral = integral + \$(1.0/M)*exp(2*o*sin(2*%pi*(pp*L/M)))*exp(-2*k*(iii*(1.0-pp)/M))compute pp = pp + 1}end whilecompute TTT=TT(p)compute varf = sigx*exp(-2*k*(TTT))*integralcompute SS=S(p)compute d1 = (log(SS/SS)+0.5*varf)/(sqrt(varf))compute d2 = d1-(sqrt(varf))compute rp=r(p)compute opvalue = exp(-rp*TTT)*(SS*(%cdf(d1)-SS*%cdf(d2)))end*frml optionprice A = opvalue(t)nlls(frml=optionprice,piters=10,iters=100) A 1 99 UU`
huanpipt

Posts: 23
Joined: Sun Mar 27, 2011 7:52 am

### Re: How to do a summation

If you tried to run something similar to what you've posted, you should have also gotten warning messages that your non-linear parameters aren't initialized, and that it's trying 0. Zero is a bad number for at least the sigx if not for the others. Start with proper guess values and it should work. If it doesn't, then you probably need to try smaller (closer to zero) guesses for any of the parameters that are in exp(...) functions. With guess values of the wrong size, those could over- or underflow on the initial function evaluation.
TomDoan

Posts: 2725
Joined: Wed Nov 01, 2006 5:36 pm

### Re: How to do a summation

Dear Tom,

It works.
I'm very Thankful for your help.
huanpipt

Posts: 23
Joined: Sun Mar 27, 2011 7:52 am

### Re: How to do a summation

Dear Tom,

I'm really thankful for your previous help.

Now, I develop the model into a more complicated one and encounter a problem.
I attached the model in the word and following is my code.
Code: Select all
`open data TD3monthc2.xlsCALENDAR(irregular)data(format=xls,org=columns) / TT r S A X********************************************************************dec real L k o sigx miu Nnonlin L K o sigx miufunction opvalue ptype real opvaluetype integer plocal real ii M SS rp TTT integral F ppp TTTT pppp Fcompute TTT=TT(p)compute ppp = TTT*360compute XX=X(p)*****************************************************using if function to separate pre- or in-period****pppp is the calculation for summation F(t,Ti)  *****************************************************if TTT <= 1.0 / 12 compute pppp = pppelse compute pppp = 30*******************set F initial*******************compute F =0********************set loop for F********************while pppp > 0 {compute TTTT = ppp / 360***************************set loop for integral***************************   compute M = 100   compute ii = 0   compute integral = 0   while ii<M {   compute integral = integral + \$   (1.0/M)*exp(2.0*o*sin(2.0*%pi*(ii*TTTT/M+L)))*exp(-2.0*k*(TTTT*(1.0-ii/M)))   compute ii = ii + 1   }***************************end loop for integral***************************compute F = F + \$exp(XX*exp(-k*TTTT)+miu*(1-exp(-k*TTTT))+(0.5)*sigx*sigx*integral)*exp(-k*TTTT)compute ppp = ppp - 1compute pppp = pppp - 1}********************end loop for F*******************************************************************obtain sigmaF, d1, d2,and finally opvalue***********************************************compute SS=S(p)compute sigf = (sigx*exp(o*sin(2*%pi*(L)))/N/SS)*Fcompute d1 = (log(SS/SS)+0.5*sigf^(2))/(sigf)compute d2 = d1-(sigf)compute rp=r(p)compute opvalue = exp(-rp*TTT)*(SS*(%cdf(d1)-SS*%cdf(d2)))end**************set frml**************frml optionprice A = opvalue(t)**********************************************set the initial value for each parameter**********************************************compute N=30,L=-0.1,k=0.1,o=0.1,sigx=0.1,miu=0.1************************************using least square to estimate************************************nlls(frml=optionprice,piters=10,iters=500,trace) A`

Indeed, there is no error message but it only shows the result like the following.
---------------------------------------------------------------------------------------------------
Non-Linear Optimization, Iteration 1. Function Calls 281.
Cosine of Angle between Direction and Gradient 0.0000000. Alpha used was 0.000000
Diagnostic measure (0=perfect) 0.3000
Subiterations 0. Distance scale 0.000000000
Old Function = 633700.642178 New Function = 633700.642178
New Coefficients:
-0.090000 0.100000 0.100000 0.100000 0.100000

Nonlinear Least Squares - Estimation by Gauss-Newton
Convergence in 1 Iterations. Final criterion was 0.0000000 <= 0.0000100
Dependent Variable A
Usable Observations 197 Degrees of Freedom 192
Total Observations 198 Skipped/Missing 1
Centered R**2 -704.178541 R Bar **2 -718.869761
Uncentered R**2 -149.076410 T x R**2 -29368.053
Mean of Dependent Variable 4.107614213
Std Error of Dependent Variable 2.141235790
Standard Error of Estimate 57.450188668
Sum of Squared Residuals 633700.64218
Log Likelihood -1075.02952
Durbin-Watson Statistic 0.012088

Variable Coeff Std Error T-Stat Signif
*******************************************************************************
1. L -0.090000000 0.000000000 0.00000 0.00000000
2. K 0.100000000 0.000000000 0.00000 0.00000000
3. O 0.100000000 0.000000000 0.00000 0.00000000
4. SIGX 0.100000000 0.000000000 0.00000 0.00000000
5. MIU 0.100000000 0.000000000 0.00000 0.00000000
---------------------------------------------------------------------------------------------------

I think it's unusual, because I've set the pre-iteration and one data is missed from the sample.
Could you help me with this ?
I hope this may not be too annoying for you.

Thanks.
Attachments
model.docx
huanpipt

Posts: 23
Joined: Sun Mar 27, 2011 7:52 am

### Re: How to do a summation

This doesn't look right:

compute d1 = (log(SS/SS)+0.5*sigf^(2))/(sigf)

Why would you have SS/SS?

Your function, at least at the guess values, is obviously way off. You need to first walk through you formulas and make sure that you have them translated into calculations correctly. do

set test = opvalue(t)
print / test a

to see whether this is a general problem or if there are a few data points that are responsible for most of the very high sum of squared errors. If it's the latter, you can focus in on those calculations. If it's more general, than you will have to throw some DISPLAY instructions inside the function to determine where the calculation is going off track.
TomDoan

Posts: 2725
Joined: Wed Nov 01, 2006 5:36 pm

### Re: How to do a summation

Dear Tom,

I set SS/SS because it's ATM options. In that case, S equals K.

I found one data point has the TT=0, as a result, F=0, sigf=0, opvalue= NA.
However, after I exclude this data, I found it still runs "strangely" ( only one iteration and no pre-iteration).
I'll use a simple data to check if the code for calculation is wrong.