# Dynare 4.2.0 Error using Rand

**URL:** <https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868>\
**Category:** Dynare help (legacy posts)\
**Created:** [27 February 2011 16:11 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868 "2011-02-27T16:11:08Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![xuduan](https://forum.dynare.org/letter_avatar_proxy/v4/letter/x/977dab/32.png) [@xuduan](https://forum.dynare.org/u/xuduan)\
**Post date:** [27 February 2011 16:11 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/1 "2011-02-27T16:11:08Z")

</div>

Hi guys. I was trying to replicate an example with external\_funtion. However, with Dynare 4.2.0, I kept receving the following message:

?? Error using ==\> rand  
Unknown command option.

Error in ==\> set\_dynare\_seed at 80  
rand(options\_.DynareRandomStreams.algo,options\_.DynareRandomStreams.seed);

Error in ==\> global\_initialization at 319  
set\_dynare\_seed(‘default’);

Error in ==\> example1\_no\_deriv\_functions\_provided at 15  
global\_initialization;

Error in ==\> dynare at 132  
evalin(‘base’,fname) ;

Anybody knows what’s going on here? Is it my matlab version is too low( version 7.0)?

Attached please find the mod file and external function.  
Thanks a lot for your help!

Duan  
[extFunNoDerivs.m](https://forum.dynare.org/uploads/default/original/1X/a66abbfda58e675e53036bd87a129ac4577c2c28.m) (52 Bytes)  
[example1\_no\_deriv\_functions\_provided.mod](https://forum.dynare.org/uploads/default/original/1X/809f2c9057060b20f67223d532d341230a56db7c.mod) (801 Bytes)

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [27 February 2011 16:31 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/2 "2011-02-27T16:31:25Z")

</div>

Hi,  
Your mod-file runs fine on my computer. Which Matlab version are you using? This seems to be a problem with the rand-function. Probably the syntax in your Matlab version is different. Or you have a name conflict so that not the original Matlab-file is used when calling rand.

---

<div class="post-metadata">

**Author:** ![xuduan](https://forum.dynare.org/letter_avatar_proxy/v4/letter/x/977dab/32.png) [@xuduan](https://forum.dynare.org/u/xuduan)\
**Post date:** [27 February 2011 16:44 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/3 "2011-02-27T16:44:25Z")

</div>

Thanks a lot jpfeifer! Well , I have matlab 7.0  
Do you happen to know what I can do except getting new version of matlab?

Thanks again

Duan

---

<div class="post-metadata">

**Author:** ![stepan-a](https://forum.dynare.org/user_avatar/forum.dynare.org/stepan-a/32/6652_2.png) [@stepan-a](https://forum.dynare.org/u/stepan-a)\
**Post date:** [2 March 2011 08:37 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/4 "2011-03-02T08:37:33Z")

</div>

Hi, The bug (related to the random number generators in old versions of matlab) has been fixed in branches 4.2 and master of our Git repository. This fix will published in the next release of Dynare. Meanwhile, as a workaround, you can replace the content of the file **set\_dynare\_seed.m** (you will find this file in the matlab subfolder of Dynare) by:

```auto
function set_dynare_seed(a,b)
	% Set seeds depending on matlab (octave) version. This routine is called in dynare_config and can be by the
	% user in the mod file.
	%   
	% Copyright (C) 2010-2011 Dynare Team
	%
	% This file is part of Dynare.
	%
	% Dynare is free software: you can redistribute it and/or modify
	% it under the terms of the GNU General Public License as published by
	% the Free Software Foundation, either version 3 of the License, or
	% (at your option) any later version.
	%
	% Dynare is distributed in the hope that it will be useful,
	% but WITHOUT ANY WARRANTY; without even the implied warranty of
	% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	% GNU General Public License for more details.
	%
	% You should have received a copy of the GNU General Public License
	% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
	global options_
	
	if ~nargin
	    error('set_dynare_seed:: I need at least one input argument!')
	end
	
	matlab_random_streams = ~(exist('OCTAVE_VERSION') || matlab_ver_less_than('7.7'));
	
	if matlab_random_streams% Use new matlab interface.
	    if nargin==1
	        if ischar(a) && strcmpi(a,'default')
	            options_.DynareRandomStreams.algo = 'mt19937ar';
	            options_.DynareRandomStreams.seed = 0;
	            s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
	            reset(RandStream.setDefaultStream(s));
	            return
	        end
	        if ischar(a) && strcmpi(a,'reset')
	            s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
	            reset(RandStream.setDefaultStream(s));
	            return
	        end
	        if ischar(a)
	            error('set_dynare_seed:: something is wrong in the calling sequence!')
	        end
	        if ~ischar(a)
	            options_.DynareRandomStreams.algo = 'mt19937ar';
	            options_.DynareRandomStreams.seed = a;
	            s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
	            reset(RandStream.setDefaultStream(s));
	            return
	        end
	    elseif nargin==2
	        if ~ischar(a) || ~( strcmpi(a,'mcg16807') || ...
	                            strcmpi(a,'mlfg6331_64') || ...
	                            strcmpi(a,'mrg32k3a') || ...
	                            strcmpi(a,'mt19937ar') || ...
	                            strcmpi(a,'shr3cong') || ...
	                            strcmpi(a,'swb2712') )
	            disp('set_dynare_seed:: First argument must be string designing the uniform random number algorithm!')
	            RandStream.list
	            disp(' ')
	            disp('set_dynare_seed:: Change the first input accordingly...')
	            disp(' ')
	            error(' ')
	        end
	        if ~isint(b)
	            error('set_dynare_seed:: The second input argument must be an integer!')
	        end
	        options_.DynareRandomStreams.algo = a;
	        options_.DynareRandomStreams.seed = b;
	        s = RandStream(options_.DynareRandomStreams.algo,'Seed',options_.DynareRandomStreams.seed);
	        reset(RandStream.setDefaultStream(s));
	    end
	else% Use old matlab interface.
	    if nargin==1
	        if ischar(a) && strcmpi(a,'default')
	            if exist('OCTAVE_VERSION') || matlab_ver_less_than('7.4')
	                options_.DynareRandomStreams.algo = 'state';
	            else
	                % Twister was introduced in MATLAB 7.4
	                options_.DynareRandomStreams.algo = 'twister';
	            end
	            options_.DynareRandomStreams.seed = 0;
	            rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
	            randn('state',options_.DynareRandomStreams.seed);
	            return
	        end
	        if ischar(a) && strcmpi(a,'reset')
	            rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
	            randn('state',options_.DynareRandomStreams.seed);
	            return
	        end
	        if ~ischar(a) && isint(a)
	            options_.DynareRandomStreams.seed = a;
	            rand(options_.DynareRandomStreams.algo,options_.DynareRandomStreams.seed);
	            randn('state',options_.DynareRandomStreams.seed);
	        else
	            error('set_dynare_seed:: Something is wrong in the calling sequence!')
	        end
	    else
	        error('set_dynare_seed:: Cannot use more than one input argument with your version of Matlab/Octave!')
	    end
	end
```

Best,  
Stéphane.

---

<div class="post-metadata">

**Author:** ![xuduan](https://forum.dynare.org/letter_avatar_proxy/v4/letter/x/977dab/32.png) [@xuduan](https://forum.dynare.org/u/xuduan)\
**Post date:** [3 March 2011 10:42 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/5 "2011-03-03T10:42:08Z")

</div>

Thanks Stephane! It works fine now!

---

<div class="post-metadata">

**Author:** ![cyepez](https://forum.dynare.org/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@cyepez](https://forum.dynare.org/u/cyepez)\
**Post date:** [24 February 2014 02:44 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/6 "2014-02-24T02:44:58Z")

</div>

I’m using Dynare (4.2?) on Ubuntu with the latest Matlab Release (2014a) and got the same issue of the random generator.  
I replaced problematic method ‘setDefaultStream’ with the Matlab’s ‘setGlobalStream’ on the set\_dynare\_seed.m script and it did the trick.

---

<div class="post-metadata">

**Author:** ![hlt215](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/3bc359/32.png) [@hlt215](https://forum.dynare.org/u/hlt215)\
**Post date:** [29 October 2014 11:26 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/7 "2014-10-29T11:26:24Z")

</div>

Hi,  
I define a function

function f = epsilon(~,~)  
av=0;  
sd=1;  
f = normrnd(av,sd);  
end

declaring it as an external function as:  
external\_function(name=epsilon, nargs=2);

and then I define it as a new variable for convenience, like:

epsi = epsilon(av,sd);

After running my dynare code a get a non-zero static residual on this last equation, do you have an idea of why?

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [29 October 2014 12:01 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/8 "2014-10-29T12:01:44Z")

</div>

Please provide the full files needed to replicate the problem. Attach them as a zip or rar archive.

---

<div class="post-metadata">

**Author:** ![hlt215](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/3bc359/32.png) [@hlt215](https://forum.dynare.org/u/hlt215)\
**Post date:** [29 October 2014 12:49 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/9 "2014-10-29T12:49:27Z")

</div>

I attach only the component related to the generation of the normal shock, in order to isolate other potential problems coming from the rest of the code which is still preliminary. Maybe I’m wrong, but I would expect the residual of this equation to be zero independently of the rest of the model. I also tried to assume that in the steady-state this random number should be to the unconditional mean, so that I have set epsi\_ss = 0 as initial value, but I still don’t get zero residuals.  
[normshock.zip](https://forum.dynare.org/uploads/default/original/2X/7/7b2c21633331bc2bc816b3dfd8779be229ffebfa.zip) (1.8 KB)

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [29 October 2014 15:45 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/10 "2014-10-29T15:45:03Z")

</div>

I don’t understand what you are doing. epsilon will always return a different random number. Conceptually, what is the steady state? You are defining epsi as an endogenous variable, but actually it is purely exogenous. Moreover, what is the purpose of this function? It just does the same as any var\_exo defined in Dynare.

---

<div class="post-metadata">

**Author:** ![hlt215](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/3bc359/32.png) [@hlt215](https://forum.dynare.org/u/hlt215)\
**Post date:** [29 October 2014 16:20 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/11 "2014-10-29T16:20:17Z")

</div>

Yes, it is an exogenous shock. It’s an additive term to some model’s equations.  
For example:  
w = a - epsilon + beta\*x;

where epsilon is the exogenous shock drowned from a normal pdf(0,1). I thought to implement it via an external function that gives me this random number satisfying my statistical distributional properties. Conceptually in the steady state shocks are zero, so I would put zero in the initial value.  
If this is not the right procedure, are you saying that I could simply do the following?

varexo epsilon;  
shocks;  
var epsilon = 1;

But how do I make sure that it is drowned from a normal pdf?

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [29 October 2014 16:24 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/12 "2014-10-29T16:24:29Z")

</div>

Dynare always uses perturbation if you use stoch\_simul. It assumes that exogenous variables are mean 0 and have a specified variance. That is, the distribution is specified by the first two moments. Higher moments do not play a role. This is isomorphic to shocks being normal.

---

<div class="post-metadata">

**Author:** ![hlt215](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/3bc359/32.png) [@hlt215](https://forum.dynare.org/u/hlt215)\
**Post date:** [29 October 2014 16:43 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/13 "2014-10-29T16:43:53Z")

</div>

Okay, thanks a lot. I will try trough the standard varexo block of Dynare then.

---

<div class="post-metadata">

**Author:** ![hlt215](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/3bc359/32.png) [@hlt215](https://forum.dynare.org/u/hlt215)\
**Post date:** [3 November 2014 12:09 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/14 "2014-11-03T12:09:15Z")

</div>

May I ask you one additional thing:

If I want to compute the conditional expectations of that epsilon-shock, I would write a matlab function that looks like:

f = epsilon.\*normpdf(epsilon, mu,std) and then integrate this function over some interval.

Now, I am in the position of wanting to compute the expectation of another variable, say “w”, but conditional to the same epsilon-distribution. I would write something like:

f = w.\*normpdf(epsilon,mu,std). Clearly it doesn’t work, but it was to show you what ideally I would like to do.

Any suggestion?

Thanks.

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [5 November 2014 15:52 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/15 "2014-11-05T15:52:28Z")

</div>

I don’t get it. The conditional expectation of a shock is always 0. Every expression dated (+1) or higher in Dynare’s stochastic simulations obtains a conditional expectations. For example, if x is an AR1-process with stochastic shock epsilon, the conditional expectation will simply be y in

`
x=0.9*x(-1)+epsilon;
y=x(+1);`

---

<div class="post-metadata">

**Author:** ![hlt215](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/3bc359/32.png) [@hlt215](https://forum.dynare.org/u/hlt215)\
**Post date:** [5 November 2014 18:02 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/16 "2014-11-05T18:02:21Z")

</div>

I’ll try being more precise. My understanding is that the conditional expectation of a shock is always zero iff I’m integrating over the full distribution, that is from -inf to +inf. But if one integral’s boundary is an endogenous variable, the expected value of the epsilon-shock shouldn’t be zero. I think I can better explain myself if you have a look at the attached document from where it should be clearer what I would need to do.

Thank you for your time.  
[dynare.pdf](https://forum.dynare.org/uploads/default/original/2X/e/e849fdee427090500fca45a551f941449a30cca5.pdf) (69.7 KB)

---

<div class="post-metadata">

**Author:** ![hlt215](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/3bc359/32.png) [@hlt215](https://forum.dynare.org/u/hlt215)\
**Post date:** [12 November 2014 15:06 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/17 "2014-11-12T15:06:21Z")

</div>

Hi, anybody?

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [12 November 2014 20:24 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/18 "2014-11-12T20:24:44Z")

</div>

The answer is not so easy. If you don’t have a recursive representation to get rid the integral by defining the whole thing as a variable (e.g. [[How to include an integral in dynare code?](https://forum.dynare.org/t/how-to-include-an-integral-in-dynare-code/1923/1))), you can only try a external function [[Integration function - dynare v4?](https://forum.dynare.org/t/integration-function-dynare-v4/1169/1)). Unfortunately, I don’t have experience with models like this. You may want to look at how Christiano/Motto/Rostagno (2014): RIsk shocks, AER dealt with this. They have replication files online.

---

<div class="post-metadata">

**Author:** ![hlt215](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/3bc359/32.png) [@hlt215](https://forum.dynare.org/u/hlt215)\
**Post date:** [12 November 2014 23:44 UTC](https://forum.dynare.org/t/dynare-4-2-0-error-using-rand/1868/19 "2014-11-12T23:44:54Z")

</div>

Using external functions is actually what I did. It looked to be quite a successful method, in the sense that I think I can succeed to implement the first integral in this way:

1. Declaring the external function before the model block:  
external\_function(name=integral\_afa2,nargs=1,first\_deriv\_provided);

2. Writing a function afa2.m that looks like  
function f = afa2(epsilon)  
f = epsilon.\*normpdf(epsilon,0,1);  
end

3. integral\_afa\_2 is then a function that integrates afa\_2

My problem, however, is about the second integral. In particular, I am not able to write a function like the one in 2) but for the variable ‘w’ and associated to the epsilon-pdf.  
Something like:  
f = w.\*normpdf(epsilon,0,1); (But this doesn’t work)

Thank you for your references, I will try to have a look.
