123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
- // Copyright 2008-2016 National ICT Australia (NICTA)
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- // ------------------------------------------------------------------------
- //! \addtogroup op_wishrnd
- //! @{
- // implementation based on:
- // Yu-Cheng Ku and Peter Bloomfield.
- // Generating Random Wishart Matrices with Fractional Degrees of Freedom in OX.
- // Oxmetrics User Conference, 2010.
-
- template<typename T1>
- inline
- void
- op_wishrnd::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_wishrnd>& expr)
- {
- arma_extra_debug_sigprint();
-
- typedef typename T1::elem_type eT;
-
- const eT df = expr.aux;
- const uword mode = expr.aux_uword_a;
-
- const bool status = op_wishrnd::apply_direct(out, expr.m, df, mode);
-
- if(status == false)
- {
- arma_stop_runtime_error("wishrnd(): given matrix is not symmetric positive definite");
- }
- }
- template<typename T1>
- inline
- bool
- op_wishrnd::apply_direct(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type,T1>& X, const typename T1::elem_type df, const uword mode)
- {
- arma_extra_debug_sigprint();
-
- typedef typename T1::elem_type eT;
-
- const quasi_unwrap<T1> U(X.get_ref());
-
- bool status = false;
-
- if(U.is_alias(out))
- {
- Mat<eT> tmp;
-
- if(mode == 1) { status = op_wishrnd::apply_noalias_mode1(tmp, U.M, df); }
- if(mode == 2) { status = op_wishrnd::apply_noalias_mode2(tmp, U.M, df); }
-
- out.steal_mem(tmp);
- }
- else
- {
- if(mode == 1) { status = op_wishrnd::apply_noalias_mode1(out, U.M, df); }
- if(mode == 2) { status = op_wishrnd::apply_noalias_mode2(out, U.M, df); }
- }
-
- if(status == false) { out.soft_reset(); }
-
- return status;
- }
- template<typename eT>
- inline
- bool
- op_wishrnd::apply_noalias_mode1(Mat<eT>& out, const Mat<eT>& S, const eT df)
- {
- arma_extra_debug_sigprint();
-
- arma_debug_check( (S.is_square() == false), "wishrnd(): given matrix must be square sized" );
-
- if(S.is_empty()) { out.reset(); return true; }
-
- if(auxlib::rudimentary_sym_check(S) == false) { return false; }
-
- Mat<eT> D;
-
- const bool status = op_chol::apply_direct(D, S, 0);
-
- if(status == false) { return false; }
-
- return op_wishrnd::apply_noalias_mode2(out, D, df);
- }
- template<typename eT>
- inline
- bool
- op_wishrnd::apply_noalias_mode2(Mat<eT>& out, const Mat<eT>& D, const eT df)
- {
- arma_extra_debug_sigprint();
-
- #if defined(ARMA_USE_CXX11)
- {
- arma_debug_check( (df <= eT(0)), "df must be greater than zero" );
- arma_debug_check( (D.is_square() == false), "wishrnd(): given matrix must be square sized" );
-
- if(D.is_empty()) { out.reset(); return true; }
-
- const uword N = D.n_rows;
-
- if(df < eT(N))
- {
- arma_extra_debug_print("simple generator");
-
- const uword df_floor = uword(std::floor(df));
-
- const Mat<eT> tmp = (randn< Mat<eT> >(df_floor, N)) * D;
-
- out = tmp.t() * tmp;
- }
- else
- {
- arma_extra_debug_print("standard generator");
-
- op_chi2rnd_varying_df<eT> chi2rnd_generator;
-
- Mat<eT> A(N, N, fill::zeros);
-
- for(uword i=0; i<N; ++i)
- {
- A.at(i,i) = std::sqrt( chi2rnd_generator(df - eT(i)) );
- }
-
- for(uword i=1; i < N; ++i)
- {
- arma_rng::randn<eT>::fill( A.colptr(i), i );
- }
-
- const Mat<eT> tmp = A * D;
-
- A.reset();
-
- out = tmp.t() * tmp;
- }
-
- return true;
- }
- #else
- {
- arma_ignore(out);
- arma_ignore(D);
- arma_ignore(df);
- arma_stop_logic_error("wishrnd(): C++11 compiler required");
-
- return false;
- }
- #endif
- }
- //
- template<typename T1>
- inline
- void
- op_iwishrnd::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_iwishrnd>& expr)
- {
- arma_extra_debug_sigprint();
-
- typedef typename T1::elem_type eT;
-
- const eT df = expr.aux;
- const uword mode = expr.aux_uword_a;
-
- const bool status = op_iwishrnd::apply_direct(out, expr.m, df, mode);
-
- if(status == false)
- {
- arma_stop_runtime_error("iwishrnd(): given matrix is not symmetric positive definite and/or df is too low");
- }
- }
- template<typename T1>
- inline
- bool
- op_iwishrnd::apply_direct(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type,T1>& X, const typename T1::elem_type df, const uword mode)
- {
- arma_extra_debug_sigprint();
-
- typedef typename T1::elem_type eT;
-
- const quasi_unwrap<T1> U(X.get_ref());
-
- bool status = false;
-
- if(U.is_alias(out))
- {
- Mat<eT> tmp;
-
- if(mode == 1) { status = op_iwishrnd::apply_noalias_mode1(tmp, U.M, df); }
- if(mode == 2) { status = op_iwishrnd::apply_noalias_mode2(tmp, U.M, df); }
-
- out.steal_mem(tmp);
- }
- else
- {
- if(mode == 1) { status = op_iwishrnd::apply_noalias_mode1(out, U.M, df); }
- if(mode == 2) { status = op_iwishrnd::apply_noalias_mode2(out, U.M, df); }
- }
-
- if(status == false) { out.soft_reset(); }
-
- return status;
- }
- template<typename eT>
- inline
- bool
- op_iwishrnd::apply_noalias_mode1(Mat<eT>& out, const Mat<eT>& T, const eT df)
- {
- arma_extra_debug_sigprint();
-
- arma_debug_check( (T.is_square() == false), "iwishrnd(): given matrix must be square sized" );
-
- if(T.is_empty()) { out.reset(); return true; }
-
- if(auxlib::rudimentary_sym_check(T) == false) { return false; }
-
- Mat<eT> Tinv;
- Mat<eT> Dinv;
-
- const bool inv_status = auxlib::inv_sympd(Tinv, T);
-
- if(inv_status == false) { return false; }
-
- const bool chol_status = op_chol::apply_direct(Dinv, Tinv, 0);
-
- if(chol_status == false) { return false; }
-
- return op_iwishrnd::apply_noalias_mode2(out, Dinv, df);
- }
- template<typename eT>
- inline
- bool
- op_iwishrnd::apply_noalias_mode2(Mat<eT>& out, const Mat<eT>& Dinv, const eT df)
- {
- arma_extra_debug_sigprint();
-
- #if defined(ARMA_USE_CXX11)
- {
- arma_debug_check( (df <= eT(0)), "df must be greater than zero" );
- arma_debug_check( (Dinv.is_square() == false), "iwishrnd(): given matrix must be square sized" );
-
- if(Dinv.is_empty()) { out.reset(); return true; }
-
- Mat<eT> tmp;
-
- const bool wishrnd_status = op_wishrnd::apply_noalias_mode2(tmp, Dinv, df);
-
- if(wishrnd_status == false) { return false; }
-
- const bool inv_status1 = auxlib::inv_sympd(out, tmp);
-
- const bool inv_status2 = (inv_status1) ? bool(true) : bool(auxlib::inv(out, tmp));
-
- if(inv_status2 == false) { return false; }
-
- return true;
- }
- #else
- {
- arma_ignore(out);
- arma_ignore(Dinv);
- arma_ignore(df);
- arma_stop_logic_error("iwishrnd(): C++11 compiler required");
-
- return false;
- }
- #endif
- }
- //! @}
|