SebastianRiedel / bingham

Bingham Statistics Library (BSL)
BSD 3-Clause "New" or "Revised" License
42 stars 21 forks source link

Weirdness with sampler #7

Closed wfbradley closed 9 years ago

wfbradley commented 9 years ago

Dear Sebastian,

I seem to be having a difficulty with the Bingham sampler-- it always produces the same sample (which is a degenerate sample). I am going to include some example code and then the output.

I compiled with "-lbingham -lm" and nothing else interesting.

First, the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "bingham.h"
#include "bingham/util.h"
#include "bingham/bingham_constants.h"
#include "bingham/hypersphere.h"

int main(){

    // create n 4-D unit vectors
    int n = 10,i,j;
    bingham_t B_true;

    /* Make a non-uniform Bingham distribution */
    double Z[3] = {1, 2, 5};
    double V[3][4] = {{1,0,0,0}, {0,0,1,0}, {0,0,0,1}};
    double *Vp[3] = {&V[0][0], &V[1][0], &V[2][0]};
    bingham_new(&B_true, 4, Vp, Z);

    /* Print out some details */
    printf("B_true:\n Z:");
    for (i=0;i<3;i++){
        printf("  %.3f",B_true.Z[i]);
    }
    printf("\n");

    /* Draw some samples from that distribution */
    double **X = new_matrix2(n,4);
    bingham_sample(X, &B_true, n);

    /* Print the first few samples */
    for (i=0;i<3;i++){
        printf(" Sample %d: ",i);
        for (j=0;j<4;j++){
            printf("  %.3f",X[i][j]);
        }
        printf("\n");
    }

    /* cleanup */
    free_matrix2(X);
    bingham_free(&B_true);
    return(0);
}

Next, the output:

B_true:
 Z:  1.000  2.000  5.000
********* seed = 1439290056
d_off = nan, d_diag = nan
 Sample 0:   0.000  1.000  0.000  0.000
 Sample 1:   0.000  1.000  0.000  0.000
 Sample 2:   0.000  1.000  0.000  0.000
SebastianRiedel commented 9 years ago

The concentration parameters Z should be in the range [-900,0]. 0 represents a uniform distribution, towards -900 the distribution gets more concentrated and peaked. Can you try correct Z parameters and report back?

On August 11, 2015 1:02:15 PM CEST, wfbradley notifications@github.com wrote:

Dear Sebastian,

I seem to be having a difficulty with the Bingham sampler-- it always produces the same sample (which is a degenerate sample). I am going to include some sample code and then the output.

I compiled with "-lbingham -lm" and nothing else interesting.

First, the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "bingham.h"
#include "bingham/util.h"
#include "bingham/bingham_constants.h"
#include "bingham/hypersphere.h"

int main(){

  // create n 4-D unit vectors
  int n = 10,i,j;
  bingham_t B_true;

  /* Make a non-uniform Bingham distribution */
  double Z[3] = {1, 2, 5};
  double V[3][4] = {{1,0,0,0}, {0,0,1,0}, {0,0,0,1}};
  double *Vp[3] = {&V[0][0], &V[1][0], &V[2][0]};
  bingham_new(&B_true, 4, Vp, Z);

  /* Print out some details */
  printf("B_true:\n Z:");
  for (i=0;i<3;i++){
      printf("  %.3f",B_true.Z[i]);
  }
  printf("\n");

  /* Draw some samples from that distribution */
  double **X = new_matrix2(n,4);
  bingham_sample(X, &B_true, n);

  /* Print the first few samples */
  for (i=0;i<3;i++){
      printf(" Sample %d: ",i);
      for (j=0;j<4;j++){
          printf("  %.3f",X[i][j]);
      }
      printf("\n");
  }

  /* cleanup */
  free_matrix2(X);
  bingham_free(&B_true);
  return(0);
}

Next, the output:

B_true:
Z:  1.000  2.000  5.000
********* seed = 1439290056
d_off = nan, d_diag = nan
Sample 0:   0.000  1.000  0.000  0.000
Sample 1:   0.000  1.000  0.000  0.000
Sample 2:   0.000  1.000  0.000  0.000

Reply to this email directly or view it on GitHub: https://github.com/SebastianRiedel/bingham/issues/7

wfbradley commented 9 years ago

Dear Sebastian,

Thank you, that fixed it!

For future reference, how does the code define a Bingham distribution (i.e. "V" and "Z")? Based on the parameterizations I am more familiar with, I was assuming that Z[i]>=0 and V[i][] were unit length, both of which appear to be false. Is there some document I should be reading to figure this stuff out?

SebastianRiedel commented 9 years ago

The code is originally Jared Glover's. He describes the convention and code in his Bingham related publications. You can easily find them in Google scholar.

Fyi, the V vectors are unit vectors.

On August 11, 2015 4:43:49 PM CEST, wfbradley notifications@github.com wrote:

Dear Sebastian,

Thank you, that fixed it!

For future reference, how does the code define a Bingham distribution (i.e. "V" and "Z")? Based on the parameterizations I am more familiar with, I was assuming that Z[i]>=0 and V[i][] were unit length, both of which appear to be false. Is there some document I should be reading to figure this stuff out?


Reply to this email directly or view it on GitHub: https://github.com/SebastianRiedel/bingham/issues/7#issuecomment-129912448

wfbradley commented 9 years ago

Ah, of course. For other readers, a definition can be found on page two of Tracking the Spin on a Ping Pong Ball with the Quaternion Bingham Filter. I'd copy the definition, but I don't know how to write the math with Git's markdown format.

And on more careful examination, the V vectors are unit length, as you mentioned; I'd made a mistake interpreting the precision.