Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Simulating time series with if else condition

Subject   st: Simulating time series with if else condition
Date   Tue, 21 Feb 2012 22:53:36 +0100

Dear Statalist,

I would like to generate a time series of debt ratios using Monte Carlo Simulation for ~ 200 firms over 19 years. I start with the debt-level (in $) in 1971 and add debt-issues/-repayments (in $, determined via OLS regression with a funds flow deficit as independent variable) to generate next year's debt-level. This process is repeated until 1989, thus generating a series of debt-ratios for each firm from 1971 to 1989.

The firm is assumed to issue debt if the funds-flow deficit is positive and to repay debt if the deficit is negative. However, debt is only repayed until the debt-level has reached 0.

I have written a program for this, using the "postfile-"command". However, I am having troubles with adding the condition that the minimum debt level cannot be less than zero. Thus, the problematic part of the following program is the if-else condition.

I would be really thankful if anyone of you could help me solve my problem.
Thanks in advance for your help!


Here is the do-file:

set seed  75225379

scalar const=0
scalar coeff=0.7

gen u=.
gen x=.
gen debtissue_sim=.
by id: gen debtlevel_sim=.
gen debtratio_sim=.

postfile sim debtissue_sim debtlevel_sim debtratio_sim using sim_results, replace
quietly {
forvalues i=1/1000 {
tsset id fyear
replace u=rnormal()
replace x=def /*def: funds flow deficit*/ replace debtissue_sim=const + coeff * x + u /*estimating the level of debt-issue/-repayments*/
regress debtissue_sim  x
if fyear==1971{
by id: replace debtlevel_sim=actual_debtlevel /*start with actual debt-level in 1971*/
else if fyear>1971 & (L.debtlevel_sim+debtissue_sim)<=0 { /*in years following 1971: no negative debt-level*/
    by id: replace debtlevel_sim=0
else {
by id: replace debtlevel_sim=L.debtlevel_sim+debtissue_sim /*transform debt-level to debt-ratio*/
replace debtratio_sim=debtlevel_sim/net_assets
post sim (debtissue_sim) (debtissue_sim_at) (debtlevel_sim) (debtratio_sim)
postclose sim

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index