JWS C Library
C language utility library
mv_pmf.c
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  */
00033 
00034 
00046 #include <jwsc/config.h>
00047 
00048 #include <stdlib.h>
00049 #include <stdio.h>
00050 #include <assert.h>
00051 #include <math.h>
00052 #include <inttypes.h>
00053 
00054 #include "jwsc/vector/vector.h"
00055 #include "jwsc/vector/vector_math.h"
00056 #include "jwsc/matrix/matrix.h"
00057 #include "jwsc/matrix/matrix_math.h"
00058 #include "jwsc/prob/pmf.h"
00059 #include "jwsc/prob/mv_pmf.h"
00060 
00061 
00074 void mv_bernoulli_pmf_f
00075 (
00076     float*            p_out,
00077     const Vector_f*   mu,
00078     const Vector_u32* x
00079 )
00080 {
00081     uint32_t d, D;
00082     float    p;
00083 
00084     assert(mu->num_elts == x->num_elts);
00085 
00086     D = mu->num_elts;
00087  
00088     p = 1.0;
00089     for (d = 0; d < D; d++)
00090     {
00091         p *= (x->elts[ d ]) ? mu->elts[ d ] : 1.0 - mu->elts[ d ];
00092     }
00093 
00094     *p_out = p;
00095 }
00096 
00102 void mv_bernoulli_pmf_d
00103 (
00104     double*           p_out,
00105     const Vector_d*   mu, 
00106     const Vector_u32* x
00107 )
00108 {
00109     uint32_t d, D;
00110     double   p;
00111 
00112     assert(mu->num_elts == x->num_elts);
00113 
00114     D = mu->num_elts;
00115  
00116     p = 1.0;
00117     for (d = 0; d < D; d++)
00118     {
00119         p *= (x->elts[ d ]) ? mu->elts[ d ] : 1.0 - mu->elts[ d ];
00120     }
00121 
00122     *p_out = p;
00123 }
00124 
00142 void sample_mv_bernoulli_pmf_f
00143 (
00144     Vector_u32**    x_out,
00145     const Vector_f* mu
00146 )
00147 {
00148     uint32_t    d, D;
00149     Vector_u32* x;
00150 
00151     D = mu->num_elts;
00152 
00153     create_vector_u32(x_out, D);
00154     x = *x_out;
00155 
00156     for (d = 0; d < D; d++)
00157     {
00158         x->elts[ d ] = sample_bernoulli_pmf_f(mu->elts[ d ]);
00159     }
00160 }
00161 
00169 void sample_mv_bernoulli_pmf_d
00170 (
00171     Vector_u32**    x_out,
00172     const Vector_d* mu
00173 )
00174 {
00175     uint32_t    d, D;
00176     Vector_u32* x;
00177 
00178     D = mu->num_elts;
00179 
00180     create_vector_u32(x_out, D);
00181     x = *x_out;
00182 
00183     for (d = 0; d < D; d++)
00184     {
00185         x->elts[ d ] = sample_bernoulli_pmf_d(mu->elts[ d ]);
00186     }
00187 }
00188 
00207 void set_sample_mv_bernoulli_pmf_f
00208 (
00209     Matrix_u32**    x_out,
00210     uint32_t        N,
00211     const Vector_f* mu
00212 )
00213 {
00214     uint32_t    D;
00215     uint32_t    n;
00216     Vector_u32* x_n = NULL;
00217 
00218     D = mu->num_elts;
00219 
00220     create_matrix_u32(x_out, N, D);
00221 
00222     for (n = 0; n < N; n++)
00223     {
00224         sample_mv_bernoulli_pmf_f(&x_n, mu);
00225         copy_vector_into_matrix_u32(x_out, *x_out, x_n, MATRIX_ROW_VECTOR, n);
00226     }
00227 
00228     free_vector_u32(x_n);
00229 }
00230 
00239 void set_sample_mv_bernoulli_pmf_d
00240 (
00241     Matrix_u32**    x_out,
00242     uint32_t        N,
00243     const Vector_d* mu
00244 )
00245 {
00246     uint32_t    D;
00247     uint32_t    n;
00248     Vector_u32* x_n = NULL;
00249 
00250     D = mu->num_elts;
00251 
00252     create_matrix_u32(x_out, N, D);
00253 
00254     for (n = 0; n < N; n++)
00255     {
00256         sample_mv_bernoulli_pmf_d(&x_n, mu);
00257         copy_vector_into_matrix_u32(x_out, *x_out, x_n, MATRIX_ROW_VECTOR, n);
00258     }
00259 
00260     free_vector_u32(x_n);
00261 }
00262