Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
dynare
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Marco Ratto
dynare
Commits
418382b3
Commit
418382b3
authored
13 years ago
by
Stéphane Adjemian
Browse files
Options
Downloads
Patches
Plain Diff
Fixed bug (trac#225). Added texinfo header and unitary test.
parent
9c3551bc
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
matlab/distributions/ig1fun.m
+2
-0
2 additions, 0 deletions
matlab/distributions/ig1fun.m
matlab/distributions/inverse_gamma_specification.m
+103
-41
103 additions, 41 deletions
matlab/distributions/inverse_gamma_specification.m
matlab/set_prior.m
+2
-2
2 additions, 2 deletions
matlab/set_prior.m
with
107 additions
and
43 deletions
matlab/distributions/ig1fun.m
0 → 100644
+
2
−
0
View file @
418382b3
function err = ig1fun(nu,mu2,sigma2)
err = 2*mu2*gamma(nu/2).^2-(sigma2+mu2)*(nu-2).*gamma((nu-1)/2).^2;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
matlab/distributions/inverse_gamma_specification.m
+
103
−
41
View file @
418382b3
function
[
s
,
nu
]
=
inverse_gamma_specification
(
mu
,
sigma
,
type
)
function
[
s
,
nu
]
=
inverse_gamma_specification
(
mu
,
sigma
,
type
,
use_fzero_flag
)
%
Computes
the
inverse
Gamma
hyperparameters
from
the
prior
mean
and
standard
deviation
.
% function [s,nu] = inverse_gamma_specification(mu,sigma,type)
% Specification of the inverse Gamma function parameters
% X ~ IG(s,nu)
%
% INPUTS
% mu: expectation
% sigma: standard deviation
% type=1: inverse Gamma 1
% type=2: inverse Gamma 2
% OUTPUTS
% s: shape parameter
% nu: scale parameter
%
% SPECIAL REQUIREMENTS
% none
%
@info
:
%!
@deftypefn
{
Function
File
}
{[
@var
{
s
},
@var
{
nu
}
]
=
}
colon
(
@var
{
mu
},
@var
{
sigma
},
@var
{
type
},
@var
{
use_fzero_flag
})
%!
@anchor
{
distributions
/
inverse_gamma_specification
}
%!
@sp
1
%!
Computes
the
inverse
Gamma
(
type
1
or
2
)
hyperparameters
from
the
prior
mean
(
@var
{
mu
})
and
standard
deviation
(
@var
{
sigma
}).
%!
@sp
2
%!
@strong
{
Inputs
}
%!
@sp
1
%!
@table
@
@var
%!
@item
mu
%!
Double
scalar
,
prior
mean
.
%!
@item
sigma
%!
Positive
double
scalar
,
prior
standard
deviation
.
%!
@item
type
%!
Integer
scalar
equal
to
one
or
two
,
type
of
the
Inverse
-
Gamma
distribution
.
%!
@item
use_fzero_flag
%!
Integer
scalar
equal
to
0
(
default
)
or
1
.
Use
(
matlab
/
octave
'
s
implementation
of
)
fzero
to
solve
for
@var
{
nu
}
if
equal
to
1
,
use
%!
dynare
'
s
implementation
of
the
secant
method
otherwise
.
%!
@end
table
%!
@sp
1
%!
@strong
{
Outputs
}
%!
@sp
1
%!
@table
@
@var
%!
@item
s
%!
Positive
double
scalar
(
greater
than
two
),
first
hypermarameter
of
the
Inverse
-
Gamma
prior
.
%!
@item
nu
%!
Positive
double
scala
,
second
hypermarameter
of
the
Inverse
-
Gamma
prior
.
%!
@end
table
%!
@sp
2
%!
@strong
{
This
function
is
called
by
:
}
%!
@sp
1
%!
@ref
{
set_prior
}
%!
@sp
2
%!
@strong
{
This
function
calls
:
}
%!
%!
@end
deftypefn
%
@eod
:
% Copyright (C) 2003-201
0
Dynare Team
%
Copyright
(
C
)
2003
-
201
1
Dynare
Team
%
%
This
file
is
part
of
Dynare
.
%
...
...
@@ -34,43 +56,83 @@ function [s,nu] = inverse_gamma_specification(mu,sigma,type)
%
You
should
have
received
a
copy
of
the
GNU
General
Public
License
%
along
with
Dynare
.
If
not
,
see
<
http
:
//www.gnu.org/licenses/>.
check_solution_flag
=
1
;
if
nargin
==
3
use_fzero_flag
=
0
;
end
sigma2
=
sigma
^
2
;
mu2
=
mu
^
2
;
if
type
==
2
;
% Inverse Gamma 2
if
type
==
2
;
%
Inverse
Gamma
2
nu
=
2
*
(
2
+
mu2
/
sigma2
);
s
=
2
*
mu
*
(
1
+
mu2
/
sigma2
);
elseif
type
==
1
;
% Inverse Gamma 1
if
sigma2
<
Inf
;
elseif
type
==
1
;
%
Inverse
Gamma
1
if
sigma2
<
Inf
nu
=
sqrt
(
2
*
(
2
+
mu2
/
sigma2
));
nu2
=
2
*
nu
;
nu1
=
2
;
err
=
2
*
mu2
*
gamma
(
nu
/
2
)
^
2
-
(
sigma2
+
mu2
)
*
(
nu
-
2
)
*
gamma
((
nu
-
1
)/
2
)
^
2
;
while
abs
(
nu2
-
nu1
)
>
1e-12
if
err
>
0
nu1
=
nu
;
if
nu
<
nu2
nu
=
nu2
;
if
use_fzero_flag
nu
=
fzero
(
@
(
nu
)
ig1fun
(
nu
,
mu2
,
sigma2
),
nu
);
else
nu2
=
2
*
nu
;
nu1
=
2
;
err
=
ig1fun
(
nu
,
mu2
,
sigma2
);
err1
=
ig1fun
(
nu1
,
mu2
,
sigma2
);
err2
=
ig1fun
(
nu2
,
mu2
,
sigma2
);
if
sign
(
err2
)
%
Too
short
interval
.
while
nu2
/
nu
<
1000
%
Shift
the
interval
contaioning
the
root
.
nu1
=
nu2
;
nu2
=
nu2
*
1
.
01
;
err2
=
ig1fun
(
nu2
,
mu2
,
sigma2
);
if
err2
<
0
break
end
end
end
%
Sove
for
nu
using
the
secant
method
.
while
abs
(
nu2
-
nu1
)
>
1e-14
if
err
>
0
nu1
=
nu
;
if
nu
<
nu2
nu
=
nu2
;
else
nu
=
2
*
nu
;
nu2
=
nu
;
end
else
nu
=
2
*
nu
;
nu2
=
nu
;
end
else
nu2
=
nu
;
nu
=
(
nu1
+
nu2
)
/
2
;
err
=
ig1fun
(
nu
,
mu2
,
sigma2
)
;
end
nu
=
(
nu1
+
nu2
)/
2
;
err
=
2
*
mu2
*
gamma
(
nu
/
2
)
^
2
-
(
sigma2
+
mu2
)
*
(
nu
-
2
)
*
gamma
((
nu
-
1
)/
2
)
^
2
;
end
s
=
(
sigma2
+
mu2
)
*
(
nu
-
2
);
else
;
if
check_solution_flag
if
abs
(
mu
-
sqrt
(
s
/
2
)
*
gamma
((
nu
-
1
)
/
2
)
/
gamma
(
nu
/
2
))
>
1e-9
error
(
'
inverse_gamma_specification
::
Failed
in
solving
for
the
hyperparameters
!
'
);
end
if
abs
(
sigma
-
sqrt
(
s
/
(
nu
-
2
)
-
mu
^
2
))
>
1e-9
error
(
'
inverse_gamma_specification
::
Failed
in
solving
for
the
hyperparameters
!
'
);
end
end
else
nu
=
2
;
s
=
2
*
mu2
/
pi
;
end
;
else
;
end
else
s
=
-
1
;
nu
=
-
1
;
end
;
end
% 01/18/2004 MJ replaced fsolve with secant
% suppressed chck
% changed order of output parameters
\ No newline at end of file
%
@test
:
1
%
$
%
addpath
..
/
matlab
/
distributions
%
$
%
$
%
Define
two
dates
%
$
[
s0
,
nu0
]
=
inverse_gamma_specification
(.
75
,.
1
,
1
,
0
);
%
$
[
s1
,
nu1
]
=
inverse_gamma_specification
(.
75
,.
1
,
1
,
1
);
%
$
%
$
%
Check
the
results
.
%
$
t
(
1
)
=
dyn_assert
(
s0
,
s1
,
1e-6
);
%
$
t
(
2
)
=
dyn_assert
(
nu0
,
nu1
,
1e-6
);
%
$
T
=
all
(
t
);
%
@eof
:
1
\ No newline at end of file
This diff is collapsed.
Click to expand it.
matlab/set_prior.m
+
2
−
2
View file @
418382b3
...
...
@@ -222,7 +222,7 @@ bayestopt_.p3(k(k1)) = zeros(length(k1),1);
bayestopt_
.
p4
(
k
(
k2
))
=
Inf
(
length
(
k2
),
1
);
for
i
=
1
:
length
(
k
)
[
bayestopt_
.
p6
(
k
(
i
)),
bayestopt_
.
p7
(
k
(
i
))]
=
...
inverse_gamma_specification
(
bayestopt_
.
p1
(
k
(
i
))
-
bayestopt_
.
p3
(
k
(
i
)),
bayestopt_
.
p2
(
k
(
i
)),
1
)
;
inverse_gamma_specification
(
bayestopt_
.
p1
(
k
(
i
))
-
bayestopt_
.
p3
(
k
(
i
)),
bayestopt_
.
p2
(
k
(
i
)),
1
,
0
)
;
bayestopt_
.
p5
(
k
(
i
))
=
compute_prior_mode
([
bayestopt_
.
p6
(
k
(
i
))
,
bayestopt_
.
p7
(
k
(
i
))
,
bayestopt_
.
p3
(
k
(
i
))
],
4
)
;
end
...
...
@@ -244,7 +244,7 @@ bayestopt_.p3(k(k1)) = zeros(length(k1),1);
bayestopt_
.
p4
(
k
(
k2
))
=
Inf
(
length
(
k2
),
1
);
for
i
=
1
:
length
(
k
)
[
bayestopt_
.
p6
(
k
(
i
)),
bayestopt_
.
p7
(
k
(
i
))]
=
...
inverse_gamma_specification
(
bayestopt_
.
p1
(
k
(
i
))
-
bayestopt_
.
p3
(
k
(
i
)),
bayestopt_
.
p2
(
k
(
i
)),
2
);
inverse_gamma_specification
(
bayestopt_
.
p1
(
k
(
i
))
-
bayestopt_
.
p3
(
k
(
i
)),
bayestopt_
.
p2
(
k
(
i
)),
2
,
0
);
bayestopt_
.
p5
(
k
(
i
))
=
compute_prior_mode
([
bayestopt_
.
p6
(
k
(
i
))
,
bayestopt_
.
p7
(
k
(
i
))
,
bayestopt_
.
p3
(
k
(
i
))
],
6
)
;
end
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment