├── +maxent ├── Nesterov.m ├── WLS_estimateDensity.m ├── createModel.m ├── djs.m ├── dkl.m ├── generateSamples.m ├── getEmpiricalMarginals.m ├── getEmpiricalModel.m ├── getEntropy.m ├── getExplicitDistribution.m ├── getFactors.m ├── getLogProbability.m ├── getMarginals.m ├── getNumFactors.m ├── getStepScaling.m ├── getWeightedMarginals.m ├── normalizeModel.m ├── setFactors.m ├── trainModel.m ├── trainModelExhaustive.m ├── trainModelIndependent.m ├── trainModelMCMC.m └── wangLandau.m ├── LICENSE.txt ├── README.md ├── docs ├── build.html ├── createModel.html ├── djs.html ├── dkl.html ├── faq.html ├── fonts │ ├── NotoSans-Bold.ttf │ ├── NotoSans-BoldItalic.ttf │ ├── NotoSans-Italic.ttf │ └── NotoSans-Regular.ttf ├── function_reference.html ├── generateSamples.html ├── getEmpiricalMarginals.html ├── getEmpiricalModel.html ├── getEntropy.html ├── getExplicitDistribution.html ├── getFactors.html ├── getLogProbability.html ├── getMarginals.html ├── getNumFactors.html ├── getWeightedMarginals.html ├── google110427594a524edf.html ├── helptoc.xml ├── index.html ├── javascripts │ └── scale.fix.js ├── maxent_example.html ├── maxent_example.png ├── maxent_example_01.png ├── maxent_example_02.png ├── maxent_example_03.png ├── maxent_example_04.png ├── maxent_example_05.png ├── maxent_example_06.png ├── normalizeModel.html ├── params.json ├── quickstart.html ├── setFactors.html ├── sitemap.xml ├── sitemap_tk.xml ├── style.css ├── stylesheets │ ├── github-light.css │ └── styles.css ├── trainModel.html └── wangLandau.html ├── example15.mat ├── example15_spatiotemporal.mat ├── example50.mat ├── info.xml ├── maxent_example.m ├── mexEmpiricalMarginals.mexa64 ├── mexEmpiricalMarginals.mexmaci64 ├── mexEmpiricalMarginals.mexw64 ├── mexExhaustiveMarginals.mexa64 ├── mexExhaustiveMarginals.mexmaci64 ├── mexExhaustiveMarginals.mexw64 ├── mexGibbsSampler.mexa64 ├── mexGibbsSampler.mexmaci64 ├── mexGibbsSampler.mexw64 ├── mexLogProbability.mexa64 ├── mexLogProbability.mexmaci64 ├── mexLogProbability.mexw64 ├── mexWangLandauSampler.mexa64 ├── mexWangLandauSampler.mexmaci64 ├── mexWangLandauSampler.mexw64 └── mex_code ├── CompositeEnergy.cpp ├── CompositeEnergy.h ├── EnergyFunction.h ├── EnergyFunctionFactory.cpp ├── EnergyFunctionFactory.h ├── HighOrderEnergy.cpp ├── HighOrderEnergy.h ├── IndependentEnergy.cpp ├── IndependentEnergy.h ├── IsingEnergy.cpp ├── IsingEnergy.h ├── KIsingEnergy.cpp ├── KIsingEnergy.h ├── KSyncEnergy.cpp ├── KSyncEnergy.h ├── MerpFastEnergy.cpp ├── MerpFastEnergy.h ├── MerpSparseEnergy.cpp ├── MerpSparseEnergy.h ├── autotester ├── autotester.m ├── createMaxentTests.m ├── maxent_tests.mat ├── runMaxentTests.m ├── testMarginals.m └── testsToSingle.m ├── build_mac.sh ├── build_unix.sh ├── buildmex.sh ├── common.h ├── matlab_utils.cpp ├── matlab_utils.h ├── maxentToolbox_windows ├── maxentToolbox_windows.sln ├── mexEmpiricalMarginals │ ├── Source.def │ ├── mexEmpiricalMarginals.vcxproj │ └── mexEmpiricalMarginals.vcxproj.filters ├── mexExhaustiveMarginals │ ├── Source.def │ ├── mexExhaustiveMarginals.vcxproj │ └── mexExhaustiveMarginals.vcxproj.filters ├── mexGibbsSampler │ ├── Source.def │ ├── mexGibbsSampler.vcxproj │ └── mexGibbsSampler.vcxproj.filters ├── mexLogProbability │ ├── Source.def │ ├── mexLogProbability.vcxproj │ └── mexLogProbability.vcxproj.filters └── mexWangLandauSampler │ ├── Source.def │ ├── mexWangLandauSampler.vcxproj │ └── mexWangLandauSampler.vcxproj.filters ├── maxent_functions.cpp ├── maxent_functions.h ├── mexEmpiricalMarginals.cpp ├── mexExhaustiveMarginals.cpp ├── mexGibbsSampler.cpp ├── mexLogProbability.cpp ├── mexWangLandauSampler.cpp ├── mex_C++_maci64.xml ├── mex_g++_glnxa64.xml ├── mtrand.cpp └── mtrand.h /+maxent/Nesterov.m: -------------------------------------------------------------------------------- 1 | 2 | % paramteres for nesterov accelerated gradient descent 3 | classdef Nesterov 4 | properties 5 | lambda 6 | gamma 7 | end 8 | methods 9 | function obj = Nesterov 10 | obj.lambda = [0 0]; 11 | obj.gamma = (1-obj.lambda(1))/obj.lambda(2); 12 | end 13 | 14 | function obj = nextGamma(obj) 15 | obj.lambda(1) = obj.lambda(2); 16 | obj.lambda(2) = (sqrt(4*(obj.lambda(1)^2)+1)+1)/2; 17 | 18 | obj.gamma = (1-obj.lambda(1))/obj.lambda(2); 19 | 20 | end 21 | 22 | end 23 | end -------------------------------------------------------------------------------- /+maxent/djs.m: -------------------------------------------------------------------------------- 1 | % Computes the Jensen-Shannon divergence between two distributions with a finite number of states. 2 | % https://en.wikipedia.org/wiki/Jensen-Shannon_divergence 3 | % The inputs are vectors of probabilities or log-probabilities (natural logarithm), the type of input is auto-detected. 4 | % The probability distributions must be normalized (sum to 1), this is not checked by the function. 5 | % 6 | % Usage: djs(p1,p2) 7 | % 8 | % Input: 9 | % p1 - Vector of probabilities for the first distribution (or log probabilities in natural logarithm). 10 | % p2 - Vector of probabilities for the second distribution (probabilities or natural log, same format as p1). 11 | % 12 | % Output: 13 | % Jensen-Shannon divergence between the distributions: DJS(p1||p2). 14 | function out = djs(p1,p2) 15 | 16 | if (min(p1) < 0) 17 | % we were given log probabilities, we must convert them to regular probabilities before we can sum them 18 | p1 = exp(p1); 19 | p2 = exp(p2); 20 | end 21 | 22 | % DJS is just DKL of the two distributions to a middle distribution 23 | m = (p1+p2)/2; 24 | out = (maxent.dkl(p1,m) + maxent.dkl(p2,m))/2; 25 | 26 | end -------------------------------------------------------------------------------- /+maxent/dkl.m: -------------------------------------------------------------------------------- 1 | % Computes the Kullback-Leibler divergence between two distributions with a finite number of states. 2 | % https://en.wikipedia.org/wiki/Kullback-Leibler_divergence 3 | % The inputs are vectors of probabilities or log-probabilities (natural logarithm), the type of input is auto-detected. 4 | % The probability distributions must be normalized (sum to 1), this is not checked by the function. 5 | % 6 | % Usage: dkl(p1,p2) 7 | % 8 | % Input: 9 | % p1 - Vector of probabilities for the first distribution (or log probabilities in natural logarithm). 10 | % p2 - Vector of probabilities for the second distribution (probabilities or natural log, same format as p1). 11 | % 12 | % Output: 13 | % Kullback-Leibler divergence between the distributions: DKL(p1||p2). 14 | 15 | function out = dkl(p1,p2) 16 | 17 | if (min(p1) < 0) 18 | % we were given log probabilities. 19 | 20 | % we assume that the distributions were given in the natural logarithm, so first convert them to the 21 | % base-2 logarithm so that the result will be in bits 22 | lp1 = p1(:) / log(2); 23 | lp2 = p2(:) / log(2); 24 | 25 | % now compute DKL 26 | ratios = 2.^(lp1) .* (lp1-lp2); 27 | out = nansum(ratios); 28 | else 29 | % we were given probabilities 30 | p1 = p1(:); 31 | p2 = p2(:); 32 | 33 | ratios = p1.*log2(p1) - p1 .* log2(p2); 34 | out = nansum(ratios); 35 | end 36 | 37 | 38 | end -------------------------------------------------------------------------------- /+maxent/generateSamples.m: -------------------------------------------------------------------------------- 1 | % Samples from a maximum entropy model 2 | % 3 | % Usage: 4 | % samples = generateSamples(model, nsamples) 5 | % samples = generateSamples(model, nsamples, Name,Value,...) 6 | % 7 | % The function accepts a model and the number of samples to be generated from it. 8 | % The samples are generated via a Metropolis-Hastings sampling scheme. In order to obtain samples which 9 | % represent the target disribution, default settings include dropping the first 10000 generated samples ("burn-in") and skipping 10 | % most of the samples generated along the way ("separation"). These parameters can be controlled by the user. 11 | % 12 | % Arguments (mandatory): 13 | % model - maximum entropy model as returned by trainModel() 14 | % nsamples - how many samples to generate 15 | % 16 | % Optional arguments (in the form of Name,Value pairs): 17 | % x0 - starting state for MCMC walk. Default: the all-zero (0000...) pattern. 18 | % burnin - how many samples to drop before returning results (default: 10000). 19 | % separation - how many bit flips to perform before taking each sample (default: one for every dimension of the input). 20 | % fix_indices - indices to elements that should be fixed during sampling (i.e. remain unchanged) 21 | % 22 | % Output: 23 | % samples - an (ncells x nsamples) matrix of samples from the model. 24 | % 25 | % 26 | % Last update: Ori Maoz 09/11/2017 27 | function samples = generateSamples(model, nsamples,varargin) 28 | 29 | DEFAULT_BURNIN = 100; 30 | 31 | if nargin<2 32 | error('Usage: generateSamples(model, nsamples, ...)'); 33 | end 34 | 35 | p = inputParser; 36 | addOptional(p,'x0',0); % starting state 37 | addOptional(p,'burnin',DEFAULT_BURNIN); % number of burn-in samples 38 | addOptional(p,'separation',model.ncells); % number of burn-in samples 39 | addOptional(p,'randseed',nan); % random seed 40 | addOptional(p,'fix_indices',[]); % indices to elements that should be fixed during sampling (i.e. remain unchanged) 41 | p.parse(varargin{:}); 42 | x0 = p.Results.x0; 43 | burnin = p.Results.burnin; 44 | separation = p.Results.separation; 45 | randseed = p.Results.randseed; 46 | fix_indices = p.Results.fix_indices; 47 | 48 | 49 | if (numel(x0)>1) 50 | if (numel(x0) ~= model.ncells) 51 | error('starting state did not have the same number of dimensions as model'); 52 | end 53 | end 54 | 55 | if (((max(fix_indices)) > model.ncells) | (min(fix_indices) < 1)) 56 | error('fixed indices should be in the range of 1...#cells'); 57 | end 58 | 59 | 60 | % change the "fixed" elements into a list of "to change" elements that the internal implementation expects. 61 | % the "-1" is because the C code expects indices to be zero-based 62 | params.variable_indices = uint32(setdiff(1:model.ncells,fix_indices) - 1); 63 | 64 | if (numel(params.variable_indices) == 0) 65 | error('at least some of the indices should remain unfixed'); 66 | end 67 | 68 | 69 | % delegate to the MEX implementation 70 | if ~isnan(randseed) 71 | params.randseed = randseed; 72 | end 73 | params.burnin = burnin; 74 | params.separation = separation; 75 | samples = mexGibbsSampler(model,nsamples,x0,params); 76 | 77 | 78 | end -------------------------------------------------------------------------------- /+maxent/getEmpiricalMarginals.m: -------------------------------------------------------------------------------- 1 | % Returns the empirical marginals of a ME model for a set of samples 2 | % 3 | % Usage: 4 | % marginals = getEmpiricalMarginals(samples,model) 5 | % 6 | % 7 | % The function accepts an array of samples and a model and returns the empirical average of the model's moment 8 | % functions over the set of samples. 9 | % 10 | % Arguments (mandatory): 11 | % samples - Set of samples to train the model on, in the format (ncells x nsamples). 12 | % model - Maximum entropy model as returned by trainModel(). This model is only used to choose the appropriate set 13 | % of marginals, the actual model parameters (lagrange multipliers in the model.factors) are irrelevant. 14 | % 15 | % Output: 16 | % marginals - an (1xnfactors) vector of marginals. 17 | % 18 | % 19 | % Last update: Ori Maoz 10/07/2016 20 | function marginals = getEmpiricalMarginals(samples, model) 21 | 22 | if (nargin<2) 23 | error('Usage: marginals = getEmpiricalMarginals(samples,model)'); 24 | end 25 | 26 | 27 | % delegate to the MEX implementation 28 | marginals = mexEmpiricalMarginals(samples,model); 29 | 30 | end -------------------------------------------------------------------------------- /+maxent/getEmpiricalModel.m: -------------------------------------------------------------------------------- 1 | % Returns an empirical distribution corresponding to a set of samples 2 | % 3 | % Usage: 4 | % model_out = getEmpiricalModel(samples) 5 | % model_out = getEmpiricalModel(samples,Name,Value,...) 6 | % 7 | % 8 | % Arguments (mandatory): 9 | % samples - Set of samples, in the format (ncells x nsamples). 10 | % 11 | % Optional arguments (in the form of Name,Value pairs): 12 | % min_count - ignore all samples that appears less than this number of times (default 1) 13 | % 14 | % Output: 15 | % model_out - structure with several fields describing the empirical distrbution: 16 | % model_out.words - unique codewords in the empirical distribution 17 | % model_out.counts - how many times each of these codewords repeated in the empirical dataset 18 | % model_out.logprobs - log probabilities of the codewords in the empirical distribution 19 | % model_out.logprobs - entropy of the empirical distribution, in bits 20 | % 21 | 22 | % Last update: Ori Maoz 09/11/2016 23 | function model_out = getEmpiricalModel(samples,varargin) 24 | 25 | DEFAULT_MIN_COUNT = 1; 26 | 27 | p = inputParser; 28 | addOptional(p,'min_count',DEFAULT_MIN_COUNT,@isnumeric); % force exhaustive solver 29 | p.parse(varargin{:}); 30 | min_count = p.Results.min_count; 31 | 32 | [ncells,npatterns] = size(samples); 33 | 34 | % find the different input words and their corresponding codewords 35 | [unique_words I J] = unique(samples','rows','sorted'); 36 | counts = hist(J,numel(I)); % appearance count of each word 37 | 38 | logprobs = log(counts / npatterns); 39 | 40 | % keep only patterns for which we have reliable measurements 41 | reliable_patterns = counts >= min_count; 42 | logprobs = logprobs(reliable_patterns); 43 | unique_words = unique_words(reliable_patterns,:); 44 | counts = counts(reliable_patterns); 45 | 46 | model_out.type = 'empirical'; 47 | model_out.ncells = ncells; 48 | model_out.words = unique_words'; 49 | model_out.counts = counts; 50 | model_out.logprobs = logprobs; 51 | model_out.nsamples = npatterns; 52 | model_out.entropy = -sum( (counts/npatterns) .* logprobs) / log(2); 53 | 54 | 55 | end -------------------------------------------------------------------------------- /+maxent/getEntropy.m: -------------------------------------------------------------------------------- 1 | % Returns the empirical entropy for a dataset or computes the entropy for a model. 2 | % This function only computes the entropy for small models (because it does so exhaustively), in order to obtain 3 | % an approximation of the entropy of large distributions use the wangLandau function. 4 | % 5 | % Usage: 6 | % entropy = getEntropy(model) 7 | % entropy = getEntropy(samples) 8 | % 9 | % Arguments (mandatory): 10 | % model - Maximum entropy model as returned by trainModel() 11 | % samples - an (ncells x nsamples) set of samples 12 | % 13 | % Output: 14 | % entropy - entropy in bits 15 | % 16 | % Last update: Ori Maoz 05/07/2016 17 | function entropy = getEntropy(input) 18 | 19 | MAX_EXHAUSTIVE_SIZE = 25; 20 | 21 | if (nargin<1) 22 | error('Usage: entropy = getEntropy(input)'); 23 | end 24 | 25 | 26 | % check if the input is a model or a dataset 27 | if isfield(input,'type') 28 | model = input; 29 | if (model.ncells > MAX_EXHAUSTIVE_SIZE) 30 | error('model is too big to exhaustively compute the entropy, use wangLandau.m instead'); 31 | end 32 | 33 | npatterns = 2^model.ncells; 34 | 35 | % get the probabilities of all the possible patterns 36 | unique_words = logical(de2bi(0:(npatterns-1)))'; 37 | 38 | % get probabilities of the input patterns 39 | logprobs = maxent.getLogProbability(model,unique_words); 40 | 41 | % compute entropy (it will be in nats because we used the natural logarithm) 42 | entropy = -sum(logprobs .* exp(logprobs)); 43 | 44 | % switch entropy from nats to bits 45 | entropy = entropy / log(2); 46 | 47 | 48 | else 49 | samples = input; 50 | 51 | % for a set of samples, we just delegate to the getEmpiricalModel which already does everything we need 52 | model = maxent.getEmpiricalModel(samples); 53 | entropy = model.entropy; 54 | end 55 | 56 | 57 | end -------------------------------------------------------------------------------- /+maxent/getExplicitDistribution.m: -------------------------------------------------------------------------------- 1 | % Returns an explicit representation of a probability distribution as a vector of probabilities. 2 | % This function will return an error for large (n >= 30) distributions since they typically cannot fit in memory. 3 | % 4 | % Usage: 5 | % probabilities = getExplicitDistribution(model) 6 | % 7 | % Arguments (mandatory): 8 | % model - Maximum entropy model as returned by trainModel() 9 | % 10 | % Output: 11 | % probabilities - vector of probabilities for all system states starting from 00000, 00001.... up to 11111 12 | % 13 | % Last update: Ori Maoz 28/01/2018 14 | function probabilities = getExplicitDistribution(model) 15 | 16 | if model.ncells < 30 17 | 18 | % get a vector of all possible states 19 | npatterns = (2^model.ncells); 20 | all_states = logical(de2bi(0:(npatterns-1)))'; 21 | 22 | % get the probability for each 23 | all_log_probabilities = maxent.getLogProbability(model,all_states); 24 | 25 | probabilities = exp(all_log_probabilities); 26 | 27 | % check if we need to normalize the distribution 28 | prob_sum = sum(probabilities); 29 | if (abs(prob_sum-1)>eps) 30 | probabilities = probabilities / prob_sum; 31 | end 32 | 33 | 34 | else 35 | error('Cannot work on n >= 30, will take up too much memory!'); 36 | end 37 | 38 | 39 | 40 | end 41 | -------------------------------------------------------------------------------- /+maxent/getFactors.m: -------------------------------------------------------------------------------- 1 | % returns the factors of a model as an array 2 | % 3 | % Usage: 4 | % model = getFactors(model) 5 | function factors = getFactors(model) 6 | 7 | if (strcmpi(model.type,'composite')) 8 | % if it's a composite model, recursively iterate over all the submodels 9 | factors = []; 10 | for i = 1:numel(model.innermodels) 11 | factors = [factors, maxent.getFactors(model.innermodels{i})]; 12 | end 13 | 14 | else 15 | % if it's a simple model, just return the factors 16 | factors = model.factors; 17 | end 18 | 19 | end -------------------------------------------------------------------------------- /+maxent/getLogProbability.m: -------------------------------------------------------------------------------- 1 | % Returns the log probability (in natural logarithm) of samples according to a trained maximum entropy model 2 | % 3 | % Usage: 4 | % logprobs = getLogProbability(model, samples) 5 | % logprobs = getLogProbability(model, samples,Name,Value,...) 6 | % 7 | % 8 | % The function accepts a model and an array of samples and returns the log probability for each sample 9 | % according to the model. If the model has not been normalize, the results will also be un-normalized. 10 | % 11 | % Arguments (mandatory): 12 | % model - Maximum entropy model as returned by trainModel() 13 | % samples - Set of samples to return the probabilities of, in the format (ncells x nsamples). 14 | % 15 | % Optional arguments (in the form of Name,Value pairs): 16 | % normalize - force with or without normalization. If this value is true but the model is un-normalized, the 17 | % function will issue a warning. 18 | % 19 | % Output: 20 | % logprobs - an (1xnsamples) vector of log probabilities in natural logarithm. 21 | % 22 | % 23 | % Last update: Ori Maoz 30/06/2016 24 | function logprobs = getLogProbability(model, samples,varargin) 25 | 26 | 27 | if (nargin<2) 28 | error('Usage: logprobs = getLogProbability(model, samples,Name,Value,...)'); 29 | end 30 | 31 | p = inputParser; 32 | addOptional(p,'normalize',nan); % silent mode - don't print anything 33 | 34 | p.parse(varargin{:}); 35 | normalize = p.Results.normalize; 36 | 37 | % if the user specified something as to normalization 38 | if ~isnan(normalize) 39 | 40 | % the user specifically asked for normalization 41 | if (normalize) 42 | if (~isfield(model,'z')) 43 | warning('Model is un-normalized!'); 44 | end 45 | else 46 | % the user specifically asked for no normalization 47 | if (isfield(model,'z')) 48 | model = rmfield(model,'z'); 49 | end 50 | end 51 | end 52 | 53 | % Now delegate to the MEX implementation 54 | logprobs = mexLogProbability(samples,model); 55 | 56 | end -------------------------------------------------------------------------------- /+maxent/getMarginals.m: -------------------------------------------------------------------------------- 1 | % Returns the marginals of a maximum entropy model, by either exhaustive computation (for small models) or MCMC 2 | % sampling (for big models). 3 | % 4 | % Usage: 5 | % marginals = getMarginals(model) 6 | % marginals = getMarginals(model,Name,Value,...) 7 | % 8 | % Arguments (mandatory): 9 | % model - Maximum entropy model as returned by trainModel() 10 | % 11 | % Optional arguments (in the form of Name,Value pairs): 12 | % force_exhaustive - force exhaustive computation. This could incur a very long runtime for big models. 13 | % force_mcmc - force MCMC (sampling-based) computation, which returns only an approximate result. 14 | % nsamples - number of empirical sampled used to compute the marginals for the MCMC-based method 15 | % 16 | % Output: 17 | % marginals - an (1xnfactors) vector of marginals. 18 | % 19 | % Last update: Ori Maoz 05/07/2016 20 | function marginals = getMarginals(model,varargin) 21 | 22 | DEFAULT_NSAMPLES = 100000; 23 | 24 | if (nargin<1) 25 | error('Usage: marginals = getMarginals(model,,...)'); 26 | end 27 | 28 | 29 | % check if the user wants to force any type of solver 30 | p = inputParser; 31 | addOptional(p,'force_exhaustive',false,@islogical); % force exhaustive solver 32 | addOptional(p,'force_mcmc',false,@islogical); % force MCMC solver 33 | addOptional(p,'nsamples',DEFAULT_NSAMPLES,@isnumeric); % samples to use for MCMC marginal estimation 34 | p.KeepUnmatched = true; 35 | p.parse(varargin{:}); 36 | 37 | 38 | % should we get the marginals exhaustively or using MCMC? 39 | solve_exhaustive = (model.ncells < 30); 40 | if (p.Results.force_mcmc) 41 | solve_exhaustive = false; 42 | end 43 | if (p.Results.force_exhaustive) 44 | if (model.ncells > 30) 45 | warning('Your model is big. This is a heavy computation. Are you sure you want to do this?'); 46 | end 47 | solve_exhaustive = true; 48 | end 49 | 50 | 51 | if (solve_exhaustive) 52 | % Exhaustive computation - delegate to the MEX implementation 53 | marginals = mexExhaustiveMarginals(model); 54 | else 55 | % sample from the model 56 | samples = maxent.generateSamples(model,p.Results.nsamples); 57 | 58 | % return empirical marginals on the sample set 59 | marginals = maxent.getEmpiricalMarginals(samples,model); 60 | end 61 | 62 | 63 | 64 | end -------------------------------------------------------------------------------- /+maxent/getNumFactors.m: -------------------------------------------------------------------------------- 1 | % returns the (total) number of factors in a model 2 | % 3 | % Usage: 4 | % model = getNumFactors(model) 5 | function nfactors = getNumFactors(model) 6 | 7 | if (strcmpi(model.type,'composite')) 8 | % if it's a composite model, recursively iterate over all the submodels 9 | nfactors = 0; 10 | for i = 1:numel(model.innermodels) 11 | nfactors = nfactors + maxent.getNumFactors(model.innermodels{i}); 12 | end 13 | 14 | else 15 | % if it's a simple model, just return the factors 16 | nfactors = numel(model.factors); 17 | end 18 | 19 | end -------------------------------------------------------------------------------- /+maxent/getStepScaling.m: -------------------------------------------------------------------------------- 1 | % returns the step scaling for a model as an array 2 | % 3 | % Usage: 4 | % model = getFactors(model) 5 | % 6 | % returns: step scaling if it exists, or nan if not 7 | function scaling = getStepScaling(model) 8 | 9 | if (strcmpi(model.type,'composite')) 10 | % if it's a composite model, recursively iterate over all the submodels 11 | scaling = []; 12 | for i = 1:numel(model.innermodels) 13 | scaling = [scaling, maxent.getStepScaling(model.innermodels{i})]; 14 | end 15 | 16 | else 17 | % if it's a simple model, just return the factors 18 | if isfield(model,'step_scaling') 19 | scaling = model.step_scaling; 20 | else 21 | scaling = nan; 22 | end 23 | end 24 | 25 | end -------------------------------------------------------------------------------- /+maxent/getWeightedMarginals.m: -------------------------------------------------------------------------------- 1 | % Returns the marginals of a ME model for a set of samples weighted by a set of probabilities 2 | % 3 | % Usage: 4 | % marginals = getWeightedMarginals(samples,model, probabilities) 5 | % 6 | % 7 | % The function accepts a model and an array of samples and returns the empirical average of the model's moment 8 | % functions over the set of samples. 9 | % 10 | % Arguments (mandatory): 11 | % samples - Set of samples to train the model on, in the format (ncells x nsamples). 12 | % model - Maximum entropy model as returned by trainModel() 13 | % probabilities - the probability used to reweight each sample 14 | % 15 | % Output: 16 | % marginals - an (1xnfactors) vector of marginals. 17 | % 18 | % 19 | % Last update: Ori Maoz 30/06/2016 20 | function marginals = getWeightedMarginals(samples,model,probabilities) 21 | 22 | if (nargin<3) 23 | error('Usage: marginals = getWeightedMarginals(samples,model,probabilities)'); 24 | end 25 | 26 | 27 | [ncells,nsamples] = size(samples); 28 | 29 | if (numel(probabilities) ~= nsamples) 30 | error('number of samples must be equal to the number of probabilities'); 31 | end 32 | 33 | % delegate to the MEX implementation 34 | marginals = mexEmpiricalMarginals(samples,model,probabilities); 35 | 36 | end -------------------------------------------------------------------------------- /+maxent/normalizeModel.m: -------------------------------------------------------------------------------- 1 | % Normalizes a probabilistic model. 2 | % If the model is of less than 30 dimensions, it is normalized exactly by summing over all its states. 3 | % Otherwise, it is normalized approximately by calling the wangLandau function. 4 | % 5 | % Usage: 6 | % entropy = normalizeModel(model) 7 | % 8 | % Arguments (mandatory): 9 | % model - Maximum entropy model as returned by trainModel() 10 | % 11 | % Output: 12 | % model_out - same model after setting its 'z' (normalization) value so that it is properly normalized 13 | % 14 | % Last update: Ori Maoz 08/01/2018 15 | function model_out = normalizeModel(model,varargin) 16 | 17 | 18 | if model.ncells < 30 19 | % exhaustive (exact) computation 20 | 21 | % we will just shortcut and use the built-in mex implementation that also computes the marginals 22 | [marginals,z] = mexExhaustiveMarginals(model); 23 | model_out = model; 24 | model_out.z = -log(z); 25 | 26 | else 27 | % approximation based on the Wang-Landau implementation, using the default arguments or whatever arguments were 28 | % passed into this function 29 | model_out = maxent.wangLandau(model,varargin{:}); 30 | end 31 | 32 | 33 | 34 | end 35 | -------------------------------------------------------------------------------- /+maxent/setFactors.m: -------------------------------------------------------------------------------- 1 | % sets the factors of a model from an array. 2 | % 3 | % Usage: 4 | % model = createModel(model) 5 | % 6 | % returns: 7 | % model structure with updated factors 8 | function model = setFactors(model,factors) 9 | 10 | if (strcmpi(model.type,'composite')) 11 | % if it's a composite model, recursively iterate over all the submodels 12 | factors_idx = 1; 13 | for i = 1:numel(model.innermodels) 14 | innermodel = model.innermodels{i}; 15 | nfactors_inner = maxent.getNumFactors(innermodel); 16 | 17 | model.innermodels{i} = maxent.setFactors(model.innermodels{i},factors(factors_idx:(factors_idx+nfactors_inner-1))); 18 | 19 | factors_idx = factors_idx + nfactors_inner; 20 | 21 | end 22 | 23 | else 24 | % if it's a simple model, just set the factors 25 | model.factors = factors; 26 | end 27 | 28 | end -------------------------------------------------------------------------------- /+maxent/trainModelIndependent.m: -------------------------------------------------------------------------------- 1 | % Trains a independent maximum-entropy model on a set of samples. This is a sub-function 2 | % invoked by trainModel() and typically should not be invoked directly. 3 | % 4 | % Usage: 5 | % model_out = trainModelExhaustive(model,samples) 6 | % 7 | % Input: 8 | % refer to trainModel.m 9 | % 10 | % Output: 11 | % model_out - trained ME model (normalized and with the entropy stored in model_out.H) 12 | % 13 | % Last update: Ori Maoz 20/11/2016 14 | function model_out = trainModelIndependent(model,raster) 15 | 16 | 17 | if (~strcmp(model.type,'indep')) 18 | error('This function can only train independent models'); 19 | end 20 | 21 | 22 | % check if our input was a raster (of samples from a distribution) or an actual Boltzmann distribution 23 | if (isstruct(raster)) 24 | % we got a model (that we can compute the marginals directly on) 25 | use_exact_marginals = true; 26 | model_base = raster; 27 | ncells = model_base.ncells; 28 | else 29 | use_exact_marginals = false; 30 | [ncells,nsamples] = size(raster); 31 | if (nsamples < ncells) 32 | warning('Input raster must be of the form (nsamples x ncells), are you sure you did not transpose it?'); 33 | end 34 | end 35 | 36 | 37 | 38 | if (ncells ~= model.ncells) 39 | error('Number of cells in model does not match number of cells in data'); 40 | 41 | end 42 | 43 | 44 | % compute the observables which are the mean firing rates. If we are preseted with experimental data, 45 | % just take the empirical count. Otherwise we need to compute it explicitly on the base model 46 | if (use_exact_marginals) 47 | npatterns = 2^ncells; 48 | unique_words = logical(de2bi(0:(npatterns-1)))'; 49 | 50 | % get probabilities of the input patterns 51 | lp = maxent.getLogProbability(model_base,unique_words); 52 | lp = lp - log(sum(exp(lp(:)))); % normalize 53 | empirical_probs = exp(lp); 54 | 55 | % get the observables - our goal is to fit the model marginals to these. 56 | firing_rates = maxent.getWeightedMarginals(unique_words,model,empirical_probs)'; 57 | 58 | else 59 | % mean firing rates 60 | firing_rates = mean(raster,2); 61 | end 62 | 63 | 64 | model_out = model; 65 | 66 | % we need special handling for cells that never fire or that always fire 67 | idx_always_zero = logical(firing_rates == 0); 68 | idx_always_one = logical(firing_rates == 1); 69 | 70 | % assume that our measurements are approximate: if we encountered a marginal of zero, then the actual marginal 71 | % is just below the quantization error i.e. 1/(2*nsamples). Similarly a marginal of one is actually just below one. 72 | firing_rates(idx_always_zero) = 1 / (2*nsamples); 73 | firing_rates(idx_always_one) = 1 - (1 / (2*nsamples)); 74 | 75 | % compute the partition function: z = (1-p1)(1-p2).....(1-pn) 76 | logz = sum(log(1-firing_rates)); 77 | 78 | 79 | % now compute the entropy 80 | model_out.entropy = sum(-firing_rates .* log2(firing_rates) - (1-firing_rates) .* log2(1-firing_rates)); 81 | 82 | 83 | % compute each of the factors 84 | for i = 1:ncells 85 | 86 | % first compute (1-p1)(1-p2)....(pi)(1-p_1+1)...(1-pn) 87 | vec = ones(ncells,1); 88 | vec(i) = 2*firing_rates(i); 89 | lambda = -(sum(log(vec - firing_rates)) - logz); 90 | model_out.factors(i) = lambda; 91 | end 92 | 93 | 94 | model_out.z = logz; 95 | 96 | 97 | end -------------------------------------------------------------------------------- /+maxent/wangLandau.m: -------------------------------------------------------------------------------- 1 | % Estimates the partition function and entropy of Boltzmann distributions on binary inputs using the Wang-Landau method. 2 | % 3 | % Usage: 4 | % model_out = wangLandau(model) 5 | % model_out = wangLandau(model, Name,Value,...) 6 | % 7 | % Arguments (mandatory): 8 | % model: Maximum entropy model as returned by trainModel(); 9 | % 10 | % Optional arguments (in the form of Name,Value pairs): 11 | % binsize: Bin size for the energy histogram (Default: 0.01). 12 | % depth: Accuracy parameter for the simulation (integer). Higher values mean a higher accuracy and longer runtime. 13 | % The final accuracy is in the order of exp(2^-(depth-1)) 14 | % separation: Number of samples to skip for every sample obtained in the MCMC random walk. A larger value decorrelates 15 | % the samples and provides more accurate results, but incurs a longer run-time. 16 | % savefile - will constantly save the state in this file, and try to resume from it if it already exists. 17 | % save_delay - delay between saves (in seconds). 18 | % 19 | % Output: 20 | % model_out - Model structure with two additional fields appended to it: 21 | % model_out.z - Log partition function of the model. 22 | % model_out.entropy -Entropy of the model (in bits). 23 | % 24 | % Last update: Ori Maoz, July 2017 25 | % 26 | function model_out = wangLandau(model,varargin) 27 | 28 | DEFAULT_BIN_SIZE = 0.01; 29 | DEFAULT_DEPTH = 20; 30 | DEFAULT_SEPARATION = 1; 31 | DEFAULT_ANALYTIC = false; 32 | 33 | % number of seconds after we save our current status to a file 34 | DEFAULT_TIME_BETWEEN_SAVES = 60; 35 | 36 | 37 | if nargin==0 38 | error('usage: wanglandau(model,[optional arguments])') 39 | 40 | end 41 | 42 | 43 | % parse optional arguments 44 | p = inputParser; 45 | addOptional(p,'binsize',DEFAULT_BIN_SIZE,@isnumeric); % energy bin size 46 | addOptional(p,'depth',DEFAULT_DEPTH,@isnumeric); % depth of MCMC interations 47 | addOptional(p,'separation',DEFAULT_SEPARATION,@isnumeric); % separation between samples 48 | addOptional(p,'analytic',DEFAULT_ANALYTIC,@islogical); % exhaustive computation 49 | addOptional(p,'savefile',''); % save file for re-entrant code 50 | addOptional(p,'save_delay',DEFAULT_TIME_BETWEEN_SAVES); % time between re-entry file saves 51 | 52 | p.parse(varargin{:}); 53 | binsize = p.Results.binsize; 54 | depth = p.Results.depth; 55 | separation = p.Results.separation; 56 | analytic = p.Results.analytic; 57 | savefile = p.Results.savefile; 58 | save_delay = p.Results.save_delay; 59 | 60 | % remove any current value of the partition function, we will estimate it by ourselves 61 | model.z = 0; 62 | 63 | if (analytic) 64 | % exhaustive computation of the energy histogram 65 | [e, g] = maxent.energyDensity(model,binsize); 66 | else 67 | % Use MCMC on the energies 68 | [e, g] = maxent.WLS_estimateDensity(model,binsize,depth,separation,savefile,save_delay); 69 | end 70 | 71 | % since g is not normalized we can scale it down to save numerical problems. 72 | % g it in log-scale so scaling it down is equivalent to subtraction. 73 | % we have to take care to do it only in the non-zero entries of the histogram. 74 | minval = min(g); 75 | g = g - minval; 76 | 77 | % g is an unnormalized log of energy state densities. Un-log it and normalize to 2^N: 78 | g = exp(g); 79 | g = g / sum(g); 80 | g = g * 2^model.ncells(1); 81 | 82 | % now we can estimate the partition function 83 | Z = sum(g .* exp(-e)); 84 | logZ = log(Z); 85 | 86 | % use the partition function to estimate the entropy 87 | H = sum(e/log(2) .* g .* exp(-e))/sum(g.*exp(-e)) + log2(sum(g.*exp(-e))); 88 | 89 | % return the partition function and the entropy in the model 90 | model_out = model; 91 | model_out.z = -logZ; 92 | model_out.entropy = H; 93 | 94 | 95 | end -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Ori Maoz 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /docs/build.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: build instructions 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
17 |
18 |

maxent_toolbox

19 |

Maximum Entropy toolbox for MATLAB

20 | 21 |

View the Project on GitHub orimaoz/maxent_toolbox

22 | 23 | 24 | 28 | 32 |
33 |
34 | 35 |

36 | Building the maxent toolbox

37 |

The maxent toolbox is pre-compiled for Windows, OSX and CentOS so you can often skip building it and directly download the binaries from here. 38 | 39 |

In order to provide faster run-time, the maxent toolbox was built with the Intel C++ compiler and makes use of the MKL library. These are available as part of the Intel Parallel Studio which is a commercial package but offers free licenses for students. It is possible, with minor modifications, to build the code without these packages but this will result in noticeably slower runtimes. 40 | 41 | 42 |

Building the toolbox for Windows

43 |

We provide a prebuilt project file for Microsoft Visual studio named maxentToolbox_windows.sln. This project file was created using Visual Studio Community 2015 which is available for free from Microsoft. Note that the project settings assume that you have Intel Parallel Studio installed.

44 |

After the build is complete, copy all the *.mexw64 files to the root of the maxent_toolbox package.

45 | 46 | 47 |

Building the toolbox for Mac

48 |

Edit the file build_mac.sh to set the directories of the MKL packages provided by Intel Composer XE. Then run:

49 |
source build_mac.sh
50 |

After the build is complete, copy all the *.mexmaci64 files to the root of the maxent_toolbox package.

51 | 52 |

Building the toolbox for Unix

53 |

Edit the file build_unix.sh to set the directories of the MKL packages provided by Intel Composer XE. Then run:

54 |
source build_unix.sh
55 |

to build binaries backwards-compatible for older CPUs.

56 |

After the build is complete, copy all the *.mexa64 files to the root of the maxent_toolbox package.

57 | 58 | 59 | 60 |
61 | 65 |
66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /docs/createModel.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: createModel() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
17 |
18 |

maxent_toolbox

19 |

Maximum Entropy toolbox for MATLAB

20 | 21 |

View the Project on GitHub orimaoz/maxent_toolbox

22 | 23 | 24 | 28 | 32 |
33 |
34 | 35 |

maxent.createModel

36 | 37 | 38 | 39 |

Description

40 |

Initializes a new maximum entropy model of a specified type. After creating a model, you will probably want to fit it to the experimental data with trainModel.

41 | 42 | 43 |

Usage

44 |
model = maxent.createModel(ncells,model_string)
45 |
model = maxent.createModel(ncells,'highorder',correlations)
46 |
model = maxent.createModel(ncells,'composite',inner_models)
47 |
model = maxent.createModel(ncells,model_string,Name,Value,...)
48 | 49 |

Arguments

50 |

Mandatory arguments

51 |
    52 |
  • ncells - number of cells (dimensions) in the distribution
  • 53 |
  • model_string - string denoting the type of models. Currently supported model types:
  • 54 |
      55 |
    • indep - independent model
    • 56 |
    • pairwise (or "ising") - pairwise maximum entropy model
    • 57 |
    • ksync - k-synchrony
    • 58 |
    • kpairwise (or "kising") - pairwise maximum entropy model with k-synchrony
    • 59 |
    • highorder - custom high-order interaction model, supplied as a third argument in the form of a cell array of arrays, for example: {[1 3],[3 5 7 8],[2 3]} denotes interactions x1x3, x3x5x7x8, x2x3
    • 60 |
    • rp - RP (Random Projection) model
    • 61 |
    • composite- composite model combining the energy function of several other maximum entropy models. these should be provided in the 3rd argument as pre-created models in a cell array.
    • 62 |
    63 |
64 | 65 |

Optional arguments (in the form of name,value pairs)

66 |
    67 |
  • nprojections - number of random projections (for RP model)
  • 68 |
  • distribution - distribution the random projection values are drawn from (for RP model)
  • 69 |
  • sparsity - sparsity between 0 (completely sparse) and 1 (not sparse at all) - (for RP model)
  • 70 |
  • threshold - relative threshold for random projection (for RP model)
  • 71 |
72 | 73 |

Output

74 |
    75 |
  • model_out - Untrained maximum entropy model.
  • 76 |
77 | 78 |

Example usage

79 | Create a pairwise maximum entropy model: 80 |
model = maxent.createModel(100,'pairwise')
81 | Create a maximum entropy models with moments x1x2, x3x5x7x8, x2x3: 82 |
model = maxent.createModel(30,'highorder',{[1 3],[3 5 7 8],[2 3]})
83 | Create an RP model with 500 projections and an average indegree of 5 84 |
model = maxent.createModel(30,'rp','nprojections',500,'indegree',5)
85 | Create a composite model consisting of an indepedent model, population synchrony and third-order correlations: 86 |
 87 | m1 = maxent.createModel(20,'indep');
 88 | m2 = maxent.createModel(20,'ksync');
 89 | m3 = maxent.createModel(20,'highorder',num2cell(nchoosek(1:30,3),2));
 90 | model = maxent.createModel(20,'composite',{m1,m2,m3});
 91 | 
92 | 93 | 94 | 95 | 96 |
97 | 101 |
102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /docs/djs.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Maximum entropy toolbox 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
17 |
18 |

maxent_toolbox

19 |

Maximum Entropy toolbox for MATLAB

20 | 21 |

View the Project on GitHub orimaoz/maxent_toolbox

22 | 23 | 24 | 28 | 32 |
33 |
34 | 35 |

maxent.djs

36 | 37 | 38 |
39 |

Description

40 |

Computes the Jensen-Shannon divergence 41 | between two distributions with a finite number of states. The inputs can be vectors of probabilities or log-probabilities (natural logarithm). 42 |

43 |
44 | 45 | 46 |
47 |

Usage

48 |
d = maxent.djs(p1,p2)
49 |
50 | 51 |
52 |

Arguments

53 |
54 |

Mandatory arguments

55 |
    56 |
  • p1- Vector of probabilities for the first distribution (or log probabilities in natural logarithm). 57 | The numbers should either be >=0 and sum to one, or their natural exponent should be >= and some to one.
  • 58 |
  • p2 - Vector of probabilities for the second distribution (ame format as p1)
  • 59 |
60 |
61 | 62 | 63 | 64 |
65 |

Output

66 |
  • d - Jensen-Shannon divergence between the distributions: DJS(p1||p2)
  • 67 |
    68 | 69 | 70 | 71 |
    72 | 76 |
    77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /docs/dkl.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Maximum entropy toolbox 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.dkl

    36 | 37 | 38 |
    39 |

    Description

    40 |

    Computes the Kullback-Leibler divergence 41 | between two distributions with a finite number of states. The inputs can be vectors of probabilities or log-probabilities (natural logarithm).

    42 |
    43 | 44 | 45 |
    46 |

    Usage

    47 |
    d = maxent.dkl(p1,p2)
    48 |
    49 | 50 |
    51 |

    Arguments

    52 |
    53 |

    Mandatory arguments

    54 |
      55 |
    • p1- Vector of probabilities for the first distribution (or log probabilities in natural logarithm). 56 | The numbers should either be >=0 and sum to one, or their natural exponent should be >= and some to one.
    • 57 |
    • p2 - Vector of probabilities for the second distribution (ame format as p1)
    • 58 |
    59 |
    60 | 61 | 62 | 63 |
    64 |

    Output

    65 |
  • d - Kullback-Leibler divergence between the distributions: DKL(p1||p2)
  • 66 |
    67 | 68 | 69 | 70 |
    71 | 75 |
    76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /docs/faq.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: frequently asked questions 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    36 | Frequently asked questions

    37 | 38 | 39 | 40 | 41 |

    Does the maxent toolbox support python?

    42 |

    There is an experimental port to Python but it is relatively immature and lacks some of the optimizations that the MATLAB version has. Contact us if you are interested in trying it out.

    43 | 44 |

    Can I use the maxent toolbox to learn distributions of non-binary inputs (for example spike counts)?

    45 |

    The current version only supports binary patterns. In theory the same approach will work for other inputs, 46 | but some of the optimizations we used to make the package run faster made use of the assumption 47 | that each input dimension is either 0 or 1. We might extend the toolbox to support larger ranges of inputs if there 48 | is sufficient demand so let us know.

    49 | 50 |

    Support for multi-core? MIC/GPU?

    51 |

    The maxent package uses a single CPU core, which was useful for us because we used a computing cluster 52 | where each node had access to a single core. Making the package multi-core (or adding GPU support) 53 | is non-trivial because of the inherently sequential nature of MCMC techniques, but as before 54 | let us know if you think this would have a big impact on you.

    55 | 56 | 57 | 58 | 59 |
    60 | 64 |
    65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /docs/fonts/NotoSans-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/docs/fonts/NotoSans-Bold.ttf -------------------------------------------------------------------------------- /docs/fonts/NotoSans-BoldItalic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/docs/fonts/NotoSans-BoldItalic.ttf -------------------------------------------------------------------------------- /docs/fonts/NotoSans-Italic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/docs/fonts/NotoSans-Italic.ttf -------------------------------------------------------------------------------- /docs/fonts/NotoSans-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/docs/fonts/NotoSans-Regular.ttf -------------------------------------------------------------------------------- /docs/function_reference.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: function reference 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    36 | List of functions in the Maximum Entropy toolbox

    37 |

    Note: all the functions reside in the maxent namespace, and can be imported into the general namespace by calling:

    38 |

     39 | import maxent.*;
     40 | 
    41 | 42 |
    43 |

    createModel

    44 |

    Initializes a new entropy model.

    45 |
    46 | 47 |
    48 |

    trainModel

    49 |

    Trains a maximum entropy model on an empirical dataset.

    50 |
    51 | 52 |
    53 |

    getLogProbability

    54 |

    Returns the log probabilities of samples according to a maximum entropy model.

    55 |
    56 | 57 |
    58 |

    getMarginals

    59 |

    Returns the marginals of a maximum entropy model.

    60 |
    61 | 62 | 66 | 67 |
    68 |

    getWeightedMarginals

    69 |

    Returns the empirical marginals of a dataset reweighted by a probability distribution.

    70 |
    71 | 72 |
    73 |

    generateSamples

    74 |

    Generates samples from a maximum entropy model.

    75 |
    76 | 77 |
    78 |

    wangLandau

    79 |

    Estimates the partition function and entropy of a model using the Wang-Landau algorithm.

    80 |
    81 | 82 |
    83 |

    getEmpiricalModel

    84 |

    Returns an empirical distribution corresponding to a set of samples.

    85 |
    86 | 87 |
    88 |

    getEntropy

    89 |

    Returns the entropy of a maximum entropy model.

    90 |
    91 | 92 |
    93 |

    dkl

    94 |

    Returns Kullback-Leibler divergence between two probability distributions.

    95 |
    96 | 97 |
    98 |

    djs

    99 |

    Returns Jensen-Shannon divergence between two probability distributions.

    100 |
    101 | 102 | 106 | 107 |
    108 |

    getExplicitDistribution

    109 |

    Returns an explicit representation of a probabilistic model as a vector of probabilities.

    110 |
    111 | 112 | 113 |

    getNumFactors

    114 |

    Returns the number of factors (parameters) in a maximum entropy model.

    115 | 116 | 117 |
    118 |

    getFactors

    119 |

    Returns the factors (parameters) in a maximum entropy model.

    120 |
    121 | 122 |
    123 |

    setFactors

    124 |

    Sets the factors (parameters) in a maximum entropy model.

    125 |
    126 | 127 | 128 |
    129 | 133 |
    134 | 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /docs/generateSamples.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: generateSamples() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.generateSamples

    36 | 37 | 38 |
    39 |

    Description

    40 |

    Samples from a maximum entropy model. 41 | The function accepts a model and the number of samples to be generated from it. 42 | The samples are generated via a Metropolis-Hastings sampling scheme. In order to obtain samples which 43 | represent the target disribution, default settings include dropping the first 10000 generated samples ("burn-in") and skipping 44 | most of the samples generated along the way ("separation"). These parameters can be controlled by the user.

    45 |
    46 | 47 | 48 |
    49 |

    Usage

    50 |
    logprobs = maxent.generateSamples(model, nsamples)
    51 |
    logprobs = maxent.generateSamples(model, nsamples,Name,Value,...)
    52 |
    53 | 54 |
    55 |

    Arguments

    56 |
    57 |

    Mandatory arguments

    58 |
      59 |
    • model - Maximum entropy model as returned by the trainModel function.
    • 60 |
    • nsamples - Number of samples to generate.
    • 61 |
    62 |
    63 | 64 |
    65 |

    Optional arguments (in the form of name,value pairs)

    66 |
      67 |
    • x0 - starting state for MCMC walk. Default: the all-zero (0000...) pattern.
    • 68 |
    • burnin - how many samples to drop before returning results (default: 10000).
    • 69 |
    • separation - how many bit flips to perform before taking each sample (default: one for every dimension of the input).
    • 70 |
    • fix_indices -indices to elements that should be fixed during sampling (i.e. remain unchanged).
    • 71 |
    72 |
    73 | 74 | 75 |
    76 |

    Example usage

    77 |
     78 | 
     79 | % generate 5000 samples from the distribution with default starting  
     80 | % point of 0 and default burn-in of 10000 samples
     81 | generated_samples = maxent.generateSamples(model, 5000);
     82 | 
     83 | 
     84 | % generate 500 samples from the distribution that are more decorrelated
     85 | % by increasing the distance betwen each sample to 300 bit-flips
     86 | generated_samples = maxent.generateSamples(model, 5000, 'separation',30);
     87 | 
     88 | 
     89 | % generate samples from the distribution, starting from [0,0,...]
     90 | % using multiple blocks of 1000 where each continues from where the last 
     91 | % one left off:
     92 | all_generated_samples = []; % here we collect the data
     93 | x0 = zeros(model.ncells,1); % starting point
     94 | for i = 1:num_repeats
     95 |   curr_samples = maxent.generateSamples(model, 1000, 'x0', x0);
     96 |   x0 = curr_samples(:,end);  % starting point for next call 
     97 |   all_generated_samples = [all_generated_samples,curr_samples];
     98 | end
     99 | 
    100 | 
    101 | % generate samples from a distribution while locking the first 5 bits 
    102 | % and sampling only from the marginal distribution:
    103 | x0 = [1,0,1,0,1,0,0,0,0,0]';
    104 | maxent.generateSamples(model, 1000, 'x0', x0,'fix_indices',1:5);
    105 | 
    106 | 
    107 | 
    108 |
    109 | 110 | 111 |
    112 |

    Output

    113 |
  • samples - an (ncells x nsamples) matrix of samples from the model.
  • 114 |
    115 | 116 | 117 | 118 |
    119 | 123 |
    124 | 125 | 126 | 127 | 128 | -------------------------------------------------------------------------------- /docs/getEmpiricalMarginals.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: getEmpiricalMarginals() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.getEmpiricalMarginals

    36 | 37 |
    38 |

    Description

    39 |

    Returns the empirical marginals of a maximum entropy model for a set of samples. The function accepts an array of 40 | samples over which the marginals are to be computed, and a model which defines the observables to be computed. 41 |

    42 |
    43 | 44 | 45 |
    46 |

    Usage

    47 |
    marginals = maxent.getEmpiricalMarginals(samples,model)
    48 |
    49 | 50 |
    51 |

    Arguments

    52 |
    53 |

    Mandatory arguments

    54 |
      55 |
    • samples - Set of samples to train the model on, in the format (ncells x nsamples).
    • 56 |
    • model - Maximum entropy model as returned by the createModel function. 57 | It does not matter whether or not this model has been trained because only the its moment-generating functions are 58 | used in order to compute the marginals
    • 59 |
    60 |
    61 | 62 | 63 |
    64 |

    Output

    65 |
      66 |
    • marginals - an (1 x <# of marginals>) vector of marginals.
    • 67 |
    68 |
    69 | 70 |
    71 |

    Example usage

    72 |
    73 | % initialize a maximum entropy model
    74 | model = maxent.createModel(30,'ising');   
    75 | 
    76 | % The observables of Ising model are firing rates and correlations, so getting the empirical marginals of this
    77 | % set of samples will actually return the firing rates and correlations of the population activity
    78 | marginals = maxent.getEmpiricalMarginals(samples,model);
    79 | 
    80 |
    81 | 82 | 83 | 84 | 85 |
    86 | 90 |
    91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /docs/getEmpiricalModel.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: getEmpiricalModel() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.getEmpiricalModel

    36 | 37 |
    38 |

    Description

    39 |

    Returns an empirical distribution corresponding to a set of samples. 40 |

    41 |
    42 | 43 | 44 |
    45 |

    Usage

    46 |
    model_out = maxent.getEmpiricalModel(samples)
    47 |
    model_out = maxent.getEmpiricalModel(samples,Name,Value,...)
    48 |
    49 | 50 |
    51 |

    Arguments

    52 |
    53 |

    Mandatory arguments

    54 |
      55 |
    • samples - Set of samples to train the model on, in the format (ncells x nsamples).
    • 56 |
    57 |
    58 | 59 |
    60 |

    Optional arguments (in the form of name,value pairs)

    61 |
      62 |
    • min_count - ignore all samples that appears less than this number of times (default 1, which does not ignore any samples).
    • 63 |
    64 |
    65 | 66 |
    67 |

    Output

    68 |
  • model_out - structure with several fields describing the empirical distrbution:
  • 69 |
      70 |
    • model_out.words - unique codewords in the empirical distribution.
    • 71 |
    • model_out.counts - how many times each of these codewords repeated in the empirical dataset.
    • 72 |
    • model_out.logprobs - log probabilities of the codewords in the empirical distribution.
    • 73 |
    • model_out.entropy - entropy of the empirical distribution, in bits.
    • 74 |
    75 |
    76 | 77 |
    78 | 79 | 80 |
    81 | 85 |
    86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /docs/getEntropy.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: getEntropy() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.getEntropy

    36 | 37 | 38 |
    39 |

    Description

    40 |

    Returns the empirical entropy for a dataset or computes the entropy for a model. 41 | This function only computes the entropy for small models (because it does so exhaustively), in order to obtain 42 | an approximation of the entropy of large distributions use the wangLandau function. 43 |

    44 |
    45 | 46 | 47 |
    48 |

    Usage

    49 |
    entropy = maxent.getEntropy(model)
    50 |
    entropy = maxent.getEntropy(samples)
    51 |
    52 | 53 |
    54 |

    Arguments

    55 |
    56 |

    Mandatory arguments

    57 |
      58 |
    • model - Maximum entropy model as returned by the trainModel function.
    • 59 |
    • samples - Set of samples, in the format (ncells x nsamples).
    • 60 |
    61 |
    62 | 63 | 64 | 65 |
    66 |

    Output

    67 |
  • entropy - entropy in bits.
  • 68 |
    69 | 70 | 71 | 72 |
    73 | 77 |
    78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /docs/getExplicitDistribution.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: getFactors() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.getExplicitDistribution

    36 | 37 | 38 |
    39 |

    Description

    40 |

    Returns an explicit representation of a probability distribution as a vector of probabilities. 41 | This function will return an error for large (default ≥ 30) distributions since they typically cannot fit in memory. 42 |

    43 |
    44 | 45 |
    46 |

    Usage

    47 |
    probabilities = maxent.getExplicitDistribution(model)
    48 |
    49 | 50 |
    51 |

    Arguments

    52 |
    53 |

    Mandatory arguments

    54 |
      55 |
    • model - Probabilistic model as returned by the trainModel function.
    • 56 |
    57 |
    58 | 59 | 60 | 61 |
    62 |

    Output

    63 |
  • probabilities - vector of probabilities for all system states starting from 00000, 00001.... up to 11111.
  • 64 |
    65 | 66 | 67 | 68 |
    69 | 73 |
    74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /docs/getFactors.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: getFactors() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.getFactors

    36 | 37 | 38 |
    39 |

    Description

    40 |

    Returns the factors (parameters) of a maximum entropy model as an array. 41 |

    42 |
    43 | 44 |
    45 |

    Usage

    46 |
    factors = maxent.getFactors(model)
    47 |
    48 | 49 |
    50 |

    Arguments

    51 |
    52 |

    Mandatory arguments

    53 |
      54 |
    • model - Maximum entropy model as returned by the trainModel function.
    • 55 |
    56 |
    57 | 58 | 59 | 60 |
    61 |

    Output

    62 |
  • factors - model parameters as an array of type double.
  • 63 |
    64 | 65 | 66 | 67 |
    68 | 72 |
    73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /docs/getLogProbability.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: getLogProbability() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.getLogProbability

    36 | 37 | 38 |
    39 |

    Description

    40 |

    Returns the log probability (in natural logarithm) of samples according to a trained maximum entropy model. 41 | The function accepts a model and an array of samples and returns the log probability for each sample 42 | according to the model. If the model has not been normalize, the results will also be un-normalized.

    43 |
    44 | 45 | 46 |
    47 |

    Usage

    48 |
    logprobs = maxent.getLogProbability(model, samples)
    49 |
    logprobs = maxent.getLogProbability(model, samples,Name,Value,...)
    50 |
    51 | 52 |
    53 |

    Arguments

    54 |
    55 |

    Mandatory arguments

    56 |
      57 |
    • model - Maximum entropy model as returned by the trainModel function.
    • 58 |
    • samples - Set of samples to get the probabilities of, in the format (ncells x nsamples).
    • 59 |
    60 |
    61 | 62 |
    63 |

    Optional arguments (in the form of name,value pairs)

    64 |
      65 |
    • normalize - force with or without normalization. If this value is true but the model is un-normalized, the function will issue a warning.
    • 66 |
    67 |
    68 |
    69 | 70 | 71 |
    72 |

    Output

    73 |
      74 |
    • logprobs - an (1xnsamples) vector of log probabilities in natural logarithm.
    • 75 |
    76 |
    77 | 78 | 79 | 80 |
    81 | 85 |
    86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /docs/getMarginals.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: getMarginals() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.getMarginals

    36 | 37 | 38 | 39 |
    40 |

    Description

    41 |

    Returns the marginals of a maximum entropy model, by either exhaustive computation (for small models) or MCMC sampling (for big models).

    42 |
    43 | 44 | 45 |
    46 |

    Usage

    47 |
    marginals = maxent.getMarginals(model)
    48 |
    marginals = maxent.getMarginals(model,Name,Value,...)
    49 |
    50 | 51 |
    52 |

    Arguments

    53 |
    54 |

    Mandatory arguments

    55 |
      56 |
    • model - Maximum entropy model as returned by the trainModel function.
    • 57 |
    58 |
    59 | 60 |
    61 |

    Optional arguments (in the form of name,value pairs)

    62 |
      63 |
    • force_exhaustive - force exhaustive computation. This could incur a very long runtime for big models.
    • 64 |
    • force_mcmc - force MCMC (sampling-based) computation, which returns only an approximate result.
    • 65 |
    • nsamples - number of empirical samples used to compute the marginals for the MCMC-based method.
    • 66 |
    67 |
    68 |
    69 | 70 | 71 |
    72 |

    Output

    73 |
  • marginals - an (1xnfactors) vector of marginals.
  • 74 |
    75 | 76 | 77 |
    78 | 82 |
    83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /docs/getNumFactors.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: getNumFactors() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.getNumFactors

    36 | 37 | 38 |
    39 |

    Description

    40 |

    Returns the number of factors (parameters) of a maximum entropy model. 41 |

    42 |
    43 | 44 |
    45 |

    Usage

    46 |
    nfactors = maxent.getNumFactors(model)
    47 |
    48 | 49 |
    50 |

    Arguments

    51 |
    52 |

    Mandatory arguments

    53 |
      54 |
    • model - Maximum entropy model as returned by the trainModel function.
    • 55 |
    56 |
    57 | 58 | 59 | 60 |
    61 |

    Output

    62 |
  • nfactors - number of factors (parameters) that the model has.
  • 63 |
    64 | 65 | 66 | 67 |
    68 | 72 |
    73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /docs/getWeightedMarginals.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: getWeightedMarginals() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.getWeightedMarginals

    36 | 37 | 38 | 39 |
    40 |

    Description

    41 |

    Returns the marginals of a maximum entropy model for a set of samples weighted by a set of probabilities. 42 | This is typically used in order to compute the expected value of the moment-generating functions according to a second distribution. 43 |

    44 |
    45 | 46 | 47 |
    48 |

    Usage

    49 |
    marginals = maxent.getWeightedMarginals(samples,model, probabilities)
    50 |
    51 | 52 |
    53 |

    Arguments

    54 |
    55 |

    Mandatory arguments

    56 |
      57 |
    • samples - Set of samples to train the model on, in the format (ncells x nsamples).
    • 58 |
    • model - Maximum entropy model as returned by the createModel function. 59 | It does not matter whether or not this model has been trained because only the its moment-generating functions are 60 | used in order to compute the marginals
    • 61 |
    • probabilities - The values used to reweight each sample, this should be a vector of size (1 xnsamples).
    • 62 |
    63 |
    64 | 65 | 66 |
    67 |

    Output

    68 |
  • marginals - an (1xnfactors) vector of marginals.
  • 69 |
    70 | 71 |
    72 |

    Example usage

    73 |
     74 | % create an ME model
     75 | model = maxent.createModel(10,'ising');   
     76 | 
     77 | % train it on a set of samples
     78 | model = maxent.trainModel(model,samples);
     79 | 
     80 | % create a set of samples corresponding to all possible states of the system
     81 | npatterns = 2^model.ncells;
     82 | unique_words = logical(de2bi(0:(npatterns-1)))';
     83 |     
     84 | % get the probabilities of all the possible patterns
     85 | logprobs = maxent.getLogProbability(model,unique_words);
     86 | probabilities = exp(logprobs);
     87 | 
     88 | % taking the values of all the unique words, reweighted by their probabilities,
     89 | % is equivalent to taking the observables of the model E_p[h(x)]:
     90 | marginals = maxent.getWeightedMarginals(unique_words,model,probabilities);
     91 | 
     92 | % the code above is functionally equivalent to calling: 
     93 | % marginals = maxent.getMarginals(model)
     94 | 
     95 | 
    96 | 97 |
    98 | 99 | 100 | 101 |
    102 | 106 |
    107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /docs/google110427594a524edf.html: -------------------------------------------------------------------------------- 1 | google-site-verification: google110427594a524edf.html -------------------------------------------------------------------------------- /docs/helptoc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Maximum Entropy toolbox 5 | Quick start guide 6 | Function Reference 7 | createModel 8 | trainModel 9 | getLogProbability 10 | getMarginals 11 | getEmpiricalMarginals 12 | getWeightedMarginals 13 | generateSamples 14 | wangLandau 15 | getEntropy 16 | getEmpiricalModel 17 | dkl 18 | djs 19 | normalizeModel 20 | getExplicitDistribution 21 | 22 | Code example 23 | 24 | 25 | -------------------------------------------------------------------------------- /docs/javascripts/scale.fix.js: -------------------------------------------------------------------------------- 1 | var metas = document.getElementsByTagName('meta'); 2 | var i; 3 | if (navigator.userAgent.match(/iPhone/i)) { 4 | for (i=0; i 2 | 3 | 4 | 5 | 6 | maxent_toolbox: getFactors() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.normalizeModel

    36 | 37 | 38 |
    39 |

    Description

    40 |

    Normalizes a probabilistic model. The function will automatically choose, based on the number of 41 | dimensions in the input distribution, which of two modes of operation to use:

    42 |

      43 |
    • For a small number (default < 30) of input dimensions the model will be explicitly normalized by summing over all possible states.
    • 44 |
    • For a large number (default ≥ 30) of input dimensions it will compute an approximate normalization using the Wang-Landau algorithm. The function does this by internally calling the function wangLandau, and accepts the same arguments.
    • 45 | 46 |

      47 |
    48 | 49 |
    50 |

    Usage

    51 |
    model = maxent.normalizeModel(model)
    52 |
    53 | 54 |
    55 |

    Arguments

    56 |
    57 |

    Mandatory arguments

    58 |
      59 |
    • model - Probabilistic model as returned by the trainModel function.
    • 60 |
    61 |
    62 | 63 | 64 | 65 |
    66 |

    Output

    67 |
  • model - Probabilistic model after normalization.
  • 68 |
    69 | 70 | 71 | 72 |
    73 | 77 |
    78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /docs/params.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "maxent_toolbox", 3 | "tagline": "Maximum entropy toolbox for MATLAB", 4 | "body": "### Maximum entropy toolbox for MATLAB\r\n", 5 | "note": "Don't delete this file! It's used internally to help with page regeneration." 6 | } -------------------------------------------------------------------------------- /docs/setFactors.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: setFactors() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.setFactors

    36 | 37 | 38 |
    39 |

    Description

    40 |

    Sets the factors (parameters) of a maximum entropy model.. 41 |

    42 |
    43 | 44 |
    45 |

    Usage

    46 |
    model = setFactors(model,factors)
    47 |
    48 | 49 |
    50 |

    Arguments

    51 |
    52 |

    Mandatory arguments

    53 |
      54 |
    • model - Maximum entropy model as returned by the trainModel function.
    • 55 |
    • factors - Maximum entropy model factors (parameters) as returned by the getFactors function. This should be an array of type double and the same length as returned by getNumFactors.
    • 56 |
    57 |
    58 | 59 | 60 | 61 |
    62 |

    Output

    63 |
  • model - model structure with updated factors.
  • 64 |
    65 | 66 | 67 | 68 |
    69 | 73 |
    74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /docs/sitemap.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | https://orimaoz.github.io/maxent_toolbox/ 5 | 0.8 6 | 7 | 8 | https://orimaoz.github.io/maxent_toolbox/build.html 9 | https://orimaoz.github.io/maxent_toolbox/createModel.html 10 | https://orimaoz.github.io/maxent_toolbox/djs.html 11 | https://orimaoz.github.io/maxent_toolbox/dkl.html 12 | https://orimaoz.github.io/maxent_toolbox/faq.html 13 | https://orimaoz.github.io/maxent_toolbox/function_reference.html 14 | https://orimaoz.github.io/maxent_toolbox/generateSamples.html 15 | https://orimaoz.github.io/maxent_toolbox/getEmpiricalMarginals.html 16 | https://orimaoz.github.io/maxent_toolbox/getEmpiricalModel.html 17 | https://orimaoz.github.io/maxent_toolbox/getEntropy.html 18 | https://orimaoz.github.io/maxent_toolbox/getLogProbability.html 19 | https://orimaoz.github.io/maxent_toolbox/getMarginals.html 20 | https://orimaoz.github.io/maxent_toolbox/getWeightedMarginals.html 21 | https://orimaoz.github.io/maxent_toolbox/index.html 22 | https://orimaoz.github.io/maxent_toolbox/maxent_example.html 23 | https://orimaoz.github.io/maxent_toolbox/quickstart.html 24 | https://orimaoz.github.io/maxent_toolbox/trainModel.html 25 | https://orimaoz.github.io/maxent_toolbox/wangLandau.html 26 | https://orimaoz.github.io/maxent_toolbox/getNumFactors.html 27 | https://orimaoz.github.io/maxent_toolbox/getFactors.html 28 | https://orimaoz.github.io/maxent_toolbox/setFactors.html 29 | 30 | 31 | -------------------------------------------------------------------------------- /docs/sitemap_tk.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | https://www.maxent.tk/ 5 | 0.8 6 | 7 | 8 | https://www.maxent.tk/build.html 9 | https://www.maxent.tk/createModel.html 10 | https://www.maxent.tk/djs.html 11 | https://www.maxent.tk/dkl.html 12 | https://www.maxent.tk/faq.html 13 | https://www.maxent.tk/function_reference.html 14 | https://www.maxent.tk/generateSamples.html 15 | https://www.maxent.tk/getEmpiricalMarginals.html 16 | https://www.maxent.tk/getEmpiricalModel.html 17 | https://www.maxent.tk/getEntropy.html 18 | https://www.maxent.tk/getLogProbability.html 19 | https://www.maxent.tk/getMarginals.html 20 | https://www.maxent.tk/getWeightedMarginals.html 21 | https://www.maxent.tk/index.html 22 | https://www.maxent.tk/maxent_example.html 23 | https://www.maxent.tk/quickstart.html 24 | https://www.maxent.tk/trainModel.html 25 | https://www.maxent.tk/wangLandau.html 26 | 27 | 28 | -------------------------------------------------------------------------------- /docs/style.css: -------------------------------------------------------------------------------- 1 | header, nav, section, article, footer 2 | {border:1px solid grey; margin:5px; padding:8px;} 3 | nav ul {margin:0; padding:0;} 4 | nav ul li {display:inline; margin:5px;} 5 | body {font-family: sans-serif; font-size: 16px} 6 | table {font-size: inherit;} 7 | 8 | -------------------------------------------------------------------------------- /docs/stylesheets/github-light.css: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2016 GitHub, Inc. 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | 24 | */ 25 | 26 | .pl-c /* comment */ { 27 | color: #969896; 28 | } 29 | 30 | .pl-c1 /* constant, variable.other.constant, support, meta.property-name, support.constant, support.variable, meta.module-reference, markup.raw, meta.diff.header */, 31 | .pl-s .pl-v /* string variable */ { 32 | color: #0086b3; 33 | } 34 | 35 | .pl-e /* entity */, 36 | .pl-en /* entity.name */ { 37 | color: #795da3; 38 | } 39 | 40 | .pl-smi /* variable.parameter.function, storage.modifier.package, storage.modifier.import, storage.type.java, variable.other */, 41 | .pl-s .pl-s1 /* string source */ { 42 | color: #333; 43 | } 44 | 45 | .pl-ent /* entity.name.tag */ { 46 | color: #63a35c; 47 | } 48 | 49 | .pl-k /* keyword, storage, storage.type */ { 50 | color: #a71d5d; 51 | } 52 | 53 | .pl-s /* string */, 54 | .pl-pds /* punctuation.definition.string, string.regexp.character-class */, 55 | .pl-s .pl-pse .pl-s1 /* string punctuation.section.embedded source */, 56 | .pl-sr /* string.regexp */, 57 | .pl-sr .pl-cce /* string.regexp constant.character.escape */, 58 | .pl-sr .pl-sre /* string.regexp source.ruby.embedded */, 59 | .pl-sr .pl-sra /* string.regexp string.regexp.arbitrary-repitition */ { 60 | color: #183691; 61 | } 62 | 63 | .pl-v /* variable */ { 64 | color: #ed6a43; 65 | } 66 | 67 | .pl-id /* invalid.deprecated */ { 68 | color: #b52a1d; 69 | } 70 | 71 | .pl-ii /* invalid.illegal */ { 72 | color: #f8f8f8; 73 | background-color: #b52a1d; 74 | } 75 | 76 | .pl-sr .pl-cce /* string.regexp constant.character.escape */ { 77 | font-weight: bold; 78 | color: #63a35c; 79 | } 80 | 81 | .pl-ml /* markup.list */ { 82 | color: #693a17; 83 | } 84 | 85 | .pl-mh /* markup.heading */, 86 | .pl-mh .pl-en /* markup.heading entity.name */, 87 | .pl-ms /* meta.separator */ { 88 | font-weight: bold; 89 | color: #1d3e81; 90 | } 91 | 92 | .pl-mq /* markup.quote */ { 93 | color: #008080; 94 | } 95 | 96 | .pl-mi /* markup.italic */ { 97 | font-style: italic; 98 | color: #333; 99 | } 100 | 101 | .pl-mb /* markup.bold */ { 102 | font-weight: bold; 103 | color: #333; 104 | } 105 | 106 | .pl-md /* markup.deleted, meta.diff.header.from-file */ { 107 | color: #bd2c00; 108 | background-color: #ffecec; 109 | } 110 | 111 | .pl-mi1 /* markup.inserted, meta.diff.header.to-file */ { 112 | color: #55a532; 113 | background-color: #eaffea; 114 | } 115 | 116 | .pl-mdr /* meta.diff.range */ { 117 | font-weight: bold; 118 | color: #795da3; 119 | } 120 | 121 | .pl-mo /* meta.output */ { 122 | color: #1d3e81; 123 | } 124 | 125 | -------------------------------------------------------------------------------- /docs/stylesheets/styles.css: -------------------------------------------------------------------------------- 1 | 2 | @import url('https://fonts.googleapis.com/css?family=Noto+Sans:400,400i,700,700i'); 3 | 4 | 5 | body { 6 | background-color: #fff; 7 | padding:50px; 8 | font: 14px/1.5 "Noto Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; 9 | color:#727272; 10 | font-weight:400; 11 | } 12 | 13 | h1, h2, h3, h4, h5, h6 { 14 | color:#222; 15 | margin:0 0 20px; 16 | } 17 | 18 | p, ul, ol, table, pre, dl { 19 | margin:0 0 20px; 20 | } 21 | 22 | h1, h2, h3 { 23 | line-height:1.1; 24 | } 25 | 26 | h1 { 27 | font-size:28px; 28 | } 29 | 30 | h2 { 31 | color:#393939; 32 | } 33 | 34 | h3, h4, h5, h6 { 35 | color:#494949; 36 | } 37 | 38 | a { 39 | color:#39c; 40 | text-decoration:none; 41 | } 42 | 43 | a:hover { 44 | color:#069; 45 | } 46 | 47 | a small { 48 | font-size:11px; 49 | color:#777; 50 | margin-top:-0.3em; 51 | display:block; 52 | } 53 | 54 | a:hover small { 55 | color:#777; 56 | } 57 | 58 | .wrapper { 59 | width:900px; 60 | margin:0 auto; 61 | } 62 | 63 | blockquote { 64 | border-left:1px solid #e5e5e5; 65 | margin:0; 66 | padding:0 0 0 20px; 67 | font-style:italic; 68 | } 69 | 70 | code, pre { 71 | font-family:Monaco, Bitstream Vera Sans Mono, Lucida Console, Terminal, Consolas, Liberation Mono, DejaVu Sans Mono, Courier New, monospace; 72 | color:#333; 73 | font-size:12px; 74 | } 75 | 76 | pre { 77 | padding:8px 15px; 78 | background: #f8f8f8; 79 | border-radius:5px; 80 | border:1px solid #e5e5e5; 81 | overflow-x: auto; 82 | } 83 | 84 | table { 85 | width:100%; 86 | border-collapse:collapse; 87 | } 88 | 89 | th, td { 90 | text-align:left; 91 | padding:5px 10px; 92 | border-bottom:1px solid #e5e5e5; 93 | } 94 | 95 | dt { 96 | color:#444; 97 | font-weight:700; 98 | } 99 | 100 | th { 101 | color:#444; 102 | } 103 | 104 | img { 105 | max-width:100%; 106 | } 107 | 108 | header { 109 | width:270px; 110 | float:left; 111 | position:fixed; 112 | -webkit-font-smoothing:subpixel-antialiased; 113 | } 114 | 115 | header ul { 116 | list-style:none; 117 | height:40px; 118 | padding:0; 119 | background: #f4f4f4; 120 | border-radius:5px; 121 | border:1px solid #e0e0e0; 122 | width:270px; 123 | } 124 | 125 | header li { 126 | width:134px; 127 | float:left; 128 | border-right:1px solid #e0e0e0; 129 | height:40px; 130 | } 131 | 132 | header li:first-child a { 133 | border-radius:5px 0 0 5px; 134 | } 135 | 136 | header li:last-child a { 137 | border-radius:0 5px 5px 0; 138 | } 139 | 140 | header ul a { 141 | line-height:1; 142 | font-size:11px; 143 | color:#999; 144 | display:block; 145 | text-align:center; 146 | padding-top:6px; 147 | height:34px; 148 | } 149 | 150 | header ul a:hover { 151 | color:#999; 152 | } 153 | 154 | header ul a:active { 155 | background-color:#f0f0f0; 156 | } 157 | 158 | strong { 159 | color:#222; 160 | font-weight:700; 161 | } 162 | 163 | header ul li + li + li { 164 | border-right:none; 165 | width:89px; 166 | } 167 | 168 | header ul a strong { 169 | font-size:14px; 170 | display:block; 171 | color:#222; 172 | } 173 | 174 | section { 175 | width:550px; 176 | float:right; 177 | padding-bottom:50px; 178 | } 179 | 180 | small { 181 | font-size:11px; 182 | } 183 | 184 | hr { 185 | border:0; 186 | background:#e5e5e5; 187 | height:1px; 188 | margin:0 0 20px; 189 | } 190 | 191 | footer { 192 | width:270px; 193 | float:left; 194 | position:fixed; 195 | bottom:50px; 196 | -webkit-font-smoothing:subpixel-antialiased; 197 | } 198 | 199 | @media print, screen and (max-width: 960px) { 200 | 201 | div.wrapper { 202 | width:auto; 203 | margin:0; 204 | } 205 | 206 | header, section, footer { 207 | float:none; 208 | position:static; 209 | width:auto; 210 | } 211 | 212 | header { 213 | padding-right:320px; 214 | } 215 | 216 | section { 217 | border:1px solid #e5e5e5; 218 | border-width:1px 0; 219 | padding:20px 0; 220 | margin:0 0 20px; 221 | } 222 | 223 | header a small { 224 | display:inline; 225 | } 226 | 227 | header ul { 228 | position:absolute; 229 | right:50px; 230 | top:52px; 231 | } 232 | } 233 | 234 | @media print, screen and (max-width: 720px) { 235 | body { 236 | word-wrap:break-word; 237 | } 238 | 239 | header { 240 | padding:0; 241 | } 242 | 243 | header ul, header p.view { 244 | position:static; 245 | } 246 | 247 | pre, code { 248 | word-wrap:normal; 249 | } 250 | } 251 | 252 | @media print, screen and (max-width: 480px) { 253 | body { 254 | padding:15px; 255 | } 256 | 257 | header ul { 258 | width:99%; 259 | } 260 | 261 | header li, header ul li + li + li { 262 | width:33%; 263 | } 264 | } 265 | 266 | @media print { 267 | body { 268 | padding:0.4in; 269 | font-size:12pt; 270 | color:#444; 271 | } 272 | } 273 | -------------------------------------------------------------------------------- /docs/wangLandau.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | maxent_toolbox: wangLandau() 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 |
    17 |
    18 |

    maxent_toolbox

    19 |

    Maximum Entropy toolbox for MATLAB

    20 | 21 |

    View the Project on GitHub orimaoz/maxent_toolbox

    22 | 23 | 24 | 28 | 32 |
    33 |
    34 | 35 |

    maxent.wangLandau

    36 | 37 | 38 |
    39 |

    Description

    40 |

    Estimates the partition function and entropy of Boltzmann distributions on binary inputs using the Wang-Landau method.

    41 |
    42 | 43 | 44 |
    45 |

    Usage

    46 |
    model_out = maxent.wangLandau(model)
    47 |
    model_out = maxent.wangLandau(model,Name,Value,...)
    48 |
    49 | 50 |
    51 |

    Arguments

    52 |
    53 |

    Mandatory arguments

    54 |
      55 |
    • model - Maximum entropy model as returned by the trainModel function.
    • 56 |
    57 |
    58 | 59 |
    60 |

    Optional arguments (in the form of name,value pairs)

    61 |
      62 |
    • binsize - Bin size for the energy histogram (Default: 0.01).
    • 63 |
    • depth - Accuracy parameter for the simulation (integer). Higher values mean a higher accuracy and longer runtime. The final accuracy is in the order of exp(2^-(depth-1)).
    • 64 |
    • separation - Number of samples to skip for every sample obtained in the MCMC random walk. A larger value decorrelates the samples and provides more accurate results, but incurs a longer run-time.
    • 65 |
    • savefile - will constantly save the state in this file, and try to resume from it if it already exists.
    • 66 |
    • save_delay - delay between saves (in seconds).
    • 67 |
    68 |
    69 |
    70 | 71 | 72 |
    73 |

    Output

    74 |

    model_out - Model structure with two additional fields appended to it:

    75 |

      76 |
    • model_out.z - Log partition function of the model.
    • 77 |
    • model_out.entropy - Entropy of the model (in bits).
    • 78 |
    79 |
    80 | 81 | 82 | 83 |
    84 | 88 |
    89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /example15.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/example15.mat -------------------------------------------------------------------------------- /example15_spatiotemporal.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/example15_spatiotemporal.mat -------------------------------------------------------------------------------- /example50.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/example50.mat -------------------------------------------------------------------------------- /info.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 2015b 6 | Maximum entropy 7 | toolbox 8 | 9 | docs 10 | 11 | -------------------------------------------------------------------------------- /mexEmpiricalMarginals.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexEmpiricalMarginals.mexa64 -------------------------------------------------------------------------------- /mexEmpiricalMarginals.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexEmpiricalMarginals.mexmaci64 -------------------------------------------------------------------------------- /mexEmpiricalMarginals.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexEmpiricalMarginals.mexw64 -------------------------------------------------------------------------------- /mexExhaustiveMarginals.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexExhaustiveMarginals.mexa64 -------------------------------------------------------------------------------- /mexExhaustiveMarginals.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexExhaustiveMarginals.mexmaci64 -------------------------------------------------------------------------------- /mexExhaustiveMarginals.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexExhaustiveMarginals.mexw64 -------------------------------------------------------------------------------- /mexGibbsSampler.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexGibbsSampler.mexa64 -------------------------------------------------------------------------------- /mexGibbsSampler.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexGibbsSampler.mexmaci64 -------------------------------------------------------------------------------- /mexGibbsSampler.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexGibbsSampler.mexw64 -------------------------------------------------------------------------------- /mexLogProbability.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexLogProbability.mexa64 -------------------------------------------------------------------------------- /mexLogProbability.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexLogProbability.mexmaci64 -------------------------------------------------------------------------------- /mexLogProbability.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexLogProbability.mexw64 -------------------------------------------------------------------------------- /mexWangLandauSampler.mexa64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexWangLandauSampler.mexa64 -------------------------------------------------------------------------------- /mexWangLandauSampler.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexWangLandauSampler.mexmaci64 -------------------------------------------------------------------------------- /mexWangLandauSampler.mexw64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mexWangLandauSampler.mexw64 -------------------------------------------------------------------------------- /mex_code/CompositeEnergy.h: -------------------------------------------------------------------------------- 1 | // Class that implements a generic composite energy function built out of several other energy functions 2 | #include "EnergyFunction.h" 3 | #include 4 | 5 | 6 | // A K-Ising model combines K-Synchrony constraints with K-Ising constraints. 7 | // As such, we can simply relegate both to their corresponding energy functions, 8 | // and this class would simply serve as a wrapper. 9 | class CompositeEnergy : public EnergyFunction 10 | { 11 | public: 12 | 13 | 14 | // Constructor - constructs an empty composite 15 | CompositeEnergy(uint32_t ncells); 16 | 17 | 18 | // destructor - removes all internally kept energy function (the destructor deletes them 19 | ~CompositeEnergy(); 20 | 21 | // makes it handle an additional energy function 22 | void addEnergyFunction(EnergyFunction * energy); 23 | 24 | 25 | 26 | // Accepts a vector x and returns its energy. A class implementing this interface is 27 | // also expected to store x as the current state of the random walk which is used 28 | // when proposing a new state. 29 | // 30 | // Input: 31 | // x - state as a vector of boolean entries (0/1) 32 | // 33 | // Returns: 34 | // The energy (un-normalized log probability) of the inputed state 35 | virtual double getEnergy(uint32_t * x); 36 | 37 | // Proposes a new state obtained by a single bit-flip from the current state, 38 | // and returns the new energy level. This implementation of this function may assume that getEnergy() has been called 39 | // at some point in the past. 40 | // 41 | // Input: 42 | // nbit - bit to flip 43 | // 44 | // Returns: 45 | // The energy (un-normalized log probability) of the new state after the bit flip 46 | virtual double propose(uint32_t nbit); 47 | 48 | // Accept/reject the proposed change and updates x. 49 | // This function may assume that propose() has been called prior to calling this function. 50 | // 51 | // Input: 52 | // bAccept - if true, then fixes the proposed state as the current state. Otherwise dumps the proposed state and 53 | // reverts to the pre-proposal state. 54 | // 55 | // Returns: 56 | // The energy (un-normalized log probability) of the new state. 57 | virtual double accept(uint32_t bAccept = 1); 58 | 59 | // Accepts the proposed changes and adds factors of current state to a running sum (marginal). 60 | // This function may assume that propose() has been called prior to calling this function. 61 | // 62 | // Input: 63 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the proposed state 64 | // into this vector after multipying them by the argument 'p'. 65 | // p - multiply factors by this number before summing them into factor_sum. 66 | // 67 | // Returns: 68 | // (none) 69 | virtual void accept(double * factor_sum, double p); 70 | 71 | // Returns the current state of the system 72 | virtual uint32_t * getX(); 73 | 74 | // Returns the dimensions of this energy functions' inputs 75 | virtual uint32_t getDim(); 76 | 77 | // Returns the number of factors (parameters) of the model 78 | virtual uint32_t getNumFactors(); 79 | 80 | // Adds the factors of the inputed sample to a vector of factor sums, multiplied by the inputed probabilities. This is mainly used to compute marginals. 81 | // 82 | // Input: 83 | // x - state to compute the factors of. 84 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the inputed state 85 | // into this vector after multipying them by the argument 'p'. 86 | // p - multiply factors by this number before summing them into factor_sum. 87 | // 88 | // Returns: 89 | // (none) 90 | virtual void sumSampleFactor(uint32_t * x, double * factor_sum, double p); 91 | 92 | // Returns the partition function (it it is known, otherwise just returns 0) 93 | virtual double getLogZ(); 94 | 95 | // Sets the partition function 96 | virtual void setLogZ(double logz); 97 | private: 98 | 99 | // internal energy functions that we keep and delegate to 100 | std::vector m_internalEnergies; 101 | 102 | uint32_t m_ndims; 103 | double m_logz; 104 | double m_proposed_energy; 105 | 106 | }; -------------------------------------------------------------------------------- /mex_code/EnergyFunction.h: -------------------------------------------------------------------------------- 1 | // Interface for a class that implements an energy function for a binary vector. 2 | // Classes with this interface will be used in MCMC-based samplers so they need to provide 3 | // a mechanism for proposing new samples which differ from the previous sample only by a single bit. 4 | // Ori Maoz 07/2014 5 | 6 | #pragma once 7 | 8 | #include 9 | //#include 10 | 11 | 12 | class EnergyFunction 13 | { 14 | public: 15 | 16 | // Accepts a vector x and returns its energy. A class implementing this interface is 17 | // also expected to store x as the current state of the random walk which is used 18 | // when proposing a new state. 19 | // 20 | // Input: 21 | // x - state as a vector of boolean entries (0/1) 22 | // 23 | // Returns: 24 | // The energy (un-normalized log probability) of the inputed state 25 | virtual double getEnergy(uint32_t * x) = 0; 26 | 27 | // Proposes a new state obtained by a single bit-flip from the current state, 28 | // and returns the new energy level. This implementation of this function may assume that getEnergy() has been called 29 | // at some point in the past. 30 | // 31 | // Input: 32 | // nbit - bit to flip 33 | // 34 | // Returns: 35 | // The energy (un-normalized log probability) of the new state after the bit flip 36 | virtual double propose(uint32_t nbit) = 0; 37 | 38 | // Accept/reject the proposed change and updates x. 39 | // This function may assume that propose() has been called prior to calling this function. 40 | // 41 | // Input: 42 | // bAccept - if true, then fixes the proposed state as the current state. Otherwise dumps the proposed state and 43 | // reverts to the pre-proposal state. 44 | // 45 | // Returns: 46 | // The energy (un-normalized log probability) of the new state. 47 | virtual double accept(uint32_t bAccept = 1) = 0; 48 | 49 | // Accepts the proposed changes and adds factors of current state to a running sum (marginal). 50 | // This function may assume that propose() has been called prior to calling this function. 51 | // 52 | // Input: 53 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the proposed state 54 | // into this vector after multipying them by the argument 'p'. 55 | // p - multiply factors by this number before summing them into factor_sum. 56 | // 57 | // Returns: 58 | // (none) 59 | virtual void accept(double * factor_sum, double p) = 0; 60 | 61 | // Returns the current state of the system 62 | virtual uint32_t * getX() = 0; 63 | 64 | // Returns the dimensions of this energy functions' inputs 65 | virtual uint32_t getDim() = 0; 66 | 67 | // Returns the number of factors (parameters) of the model 68 | virtual uint32_t getNumFactors() = 0; 69 | 70 | // Adds the factors of the inputed sample to a vector of factor sums, multiplied by the inputed probabilities. This is mainly used to compute marginals. 71 | // 72 | // Input: 73 | // x - state to compute the factors of. 74 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the inputed state 75 | // into this vector after multipying them by the argument 'p'. 76 | // p - multiply factors by this number before summing them into factor_sum. 77 | // 78 | // Returns: 79 | // (none) 80 | virtual void sumSampleFactor(uint32_t * x, double * factor_sum,double p) = 0; 81 | 82 | // Returns the partition function (it it is known, otherwise just returns 0) 83 | virtual double getLogZ() = 0; 84 | 85 | // Sets the partition function 86 | virtual void setLogZ(double logz) = 0; 87 | 88 | // Destructor 89 | virtual ~EnergyFunction() {} 90 | 91 | }; -------------------------------------------------------------------------------- /mex_code/EnergyFunctionFactory.h: -------------------------------------------------------------------------------- 1 | // Creates and initializes classes that implement energy functions of Boltzmann distributions. 2 | // Ori Maoz 07/2014 3 | 4 | #include "mex.h" 5 | #include "EnergyFunction.h" 6 | 7 | 8 | class EnergyFunctionFactory 9 | { 10 | 11 | public: 12 | // Reads the model parameters received by matlab and creates an appropriate energy function. 13 | // The returned energy function is dynamically allocate dan dshould be deleted when it is no 14 | // longer in use. 15 | EnergyFunction * createEnergyFunction(const mxArray * model_params); 16 | 17 | private: 18 | EnergyFunction * createMerpEnergy(const mxArray * model_params, bool is_sparse); 19 | EnergyFunction * createKSyncEnergy(const mxArray * model_params); 20 | EnergyFunction * createKIsingEnergy(const mxArray * model_params); 21 | EnergyFunction * createIsingEnergy(const mxArray * model_params); 22 | EnergyFunction * createIndependentEnergy(const mxArray * model_params); 23 | EnergyFunction * createHighOrderEnergy(const mxArray * model_params); 24 | 25 | // helper functions for extracting model params 26 | uint32_t getNcells(const mxArray * model_params); // return number of cells 27 | double getZ(const mxArray * model_params); // return partition function 28 | double * getFactors(const mxArray * model_params); // return factors 29 | uint32_t getNFactors(const mxArray * model_params); // return number of factors 30 | 31 | }; -------------------------------------------------------------------------------- /mex_code/HighOrderEnergy.h: -------------------------------------------------------------------------------- 1 | // Implementation of the EnergyFunction interface on the MERP model. 2 | // This version uses a sparse representation of the connection matrix W to facilitate faster operations 3 | // when the matrix is sparse 4 | // Ori Maoz 11/2014 5 | 6 | #include "EnergyFunction.h" 7 | #include 8 | #include "common.h" 9 | 10 | 11 | class HighOrderEnergy : public EnergyFunction 12 | { 13 | public: 14 | 15 | // Constructor - constructs it from a random projection and a single threshold 16 | HighOrderEnergy(float* in_W, double * in_lambda, uint32_t ncells, uint32_t nfactors, float * in_thresholds); 17 | 18 | // destructor 19 | virtual ~HighOrderEnergy(); 20 | 21 | // Accepts a vector x and returns its energy. A class implementing this interface is 22 | // also expected to store x as the current state of the random walk which is used 23 | // when proposing a new state. 24 | // 25 | // Input: 26 | // x - state as a vector of boolean entries (0/1) 27 | // 28 | // Returns: 29 | // The energy (un-normalized log probability) of the inputed state 30 | virtual double getEnergy(uint32_t * x); 31 | 32 | // Proposes a new state obtained by a single bit-flip from the current state, 33 | // and returns the new energy level. This implementation of this function may assume that getEnergy() has been called 34 | // at some point in the past. 35 | // 36 | // Input: 37 | // nbit - bit to flip 38 | // 39 | // Returns: 40 | // The energy (un-normalized log probability) of the new state after the bit flip 41 | virtual double propose(uint32_t nbit); 42 | 43 | // Accept/reject the proposed change and updates x. 44 | // This function may assume that propose() has been called prior to calling this function. 45 | // 46 | // Input: 47 | // bAccept - if true, then fixes the proposed state as the current state. Otherwise dumps the proposed state and 48 | // reverts to the pre-proposal state. 49 | // 50 | // Returns: 51 | // The energy (un-normalized log probability) of the new state. 52 | virtual double accept(uint32_t bAccept = 1); 53 | 54 | // Accepts the proposed changes and adds factors of current state to a running sum (marginal). 55 | // This function may assume that propose() has been called prior to calling this function. 56 | // 57 | // Input: 58 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the proposed state 59 | // into this vector after multipying them by the argument 'p'. 60 | // p - multiply factors by this number before summing them into factor_sum. 61 | // 62 | // Returns: 63 | // (none) 64 | virtual void accept(double * factor_sum, double p); 65 | 66 | // Returns the current state of the system 67 | uint32_t * getX(); 68 | 69 | // Returns the dimensions of this energy functions' inputs 70 | virtual uint32_t getDim(); 71 | 72 | // Returns the number of factors (parameters) of the model 73 | virtual uint32_t getNumFactors(); 74 | 75 | // Adds the factors of the inputed sample to a vector of factor sums, multiplied by the inputed probabilities. This is mainly used to compute marginals. 76 | // 77 | // Input: 78 | // x - state to compute the factors of. 79 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the inputed state 80 | // into this vector after multipying them by the argument 'p'. 81 | // p - multiply factors by this number before summing them into factor_sum. 82 | // 83 | // Returns: 84 | // (none) 85 | virtual void sumSampleFactor(uint32_t * x, double * factor_sum, double p); 86 | 87 | // Returns the partition function (it it is known, otherwise just returns 0) 88 | virtual double getLogZ(); 89 | 90 | // Sets the partition function 91 | virtual void setLogZ(double logz); 92 | 93 | 94 | 95 | private: 96 | 97 | 98 | double applyThreshold(float * y); 99 | 100 | double getSumDiffBitOn(uint32_t nbit); 101 | double getSumDiffBitOff(uint32_t nbit); 102 | 103 | 104 | 105 | DECLARE_ALIGNED float * m_onevals ALIGN_AFTER; // vector of ones (for addition/subtraction) 106 | DECLARE_ALIGNED MKL_INT ** m_W_idx ALIGN_AFTER; // sparse columns of W (indices) 107 | DECLARE_ALIGNED uint32_t * m_W_nz ALIGN_AFTER; // sparse columns of W (lengths) 108 | 109 | DECLARE_ALIGNED double * m_lambda ALIGN_AFTER; 110 | DECLARE_ALIGNED double ** m_sparse_lambda ALIGN_AFTER; // lambdas sectioned into sparsified parts of W 111 | 112 | DECLARE_ALIGNED float * m_threshold ALIGN_AFTER; 113 | DECLARE_ALIGNED float * m_y ALIGN_AFTER; 114 | DECLARE_ALIGNED float * m_tmp ALIGN_AFTER; 115 | 116 | std::vector m_x; 117 | uint32_t m_proposed_bit; 118 | 119 | DECLARE_ALIGNED double m_energy ALIGN_AFTER; 120 | DECLARE_ALIGNED double m_energy_dif ALIGN_AFTER; 121 | DECLARE_ALIGNED double m_logz ALIGN_AFTER; 122 | DECLARE_ALIGNED double m_proposed_energy ALIGN_AFTER; 123 | DECLARE_ALIGNED uint32_t m_ndims ALIGN_AFTER; 124 | DECLARE_ALIGNED uint32_t m_nfactors ALIGN_AFTER; 125 | DECLARE_ALIGNED bool m_bProposed ALIGN_AFTER; 126 | 127 | }; -------------------------------------------------------------------------------- /mex_code/IndependentEnergy.cpp: -------------------------------------------------------------------------------- 1 | #include "IndependentEnergy.h" 2 | #include 3 | 4 | 5 | // constructor 6 | IndependentEnergy::IndependentEnergy(uint32_t ndims, double * factors) : 7 | m_logz(0) 8 | { 9 | m_ndims = ndims; 10 | m_factors = factors; 11 | m_x = new uint32_t[ndims]; // allocate memory for current state 12 | } 13 | 14 | // destructor 15 | IndependentEnergy::~IndependentEnergy() 16 | { 17 | // remove memory allocated for current state 18 | delete m_x; 19 | } 20 | 21 | 22 | 23 | // Accepts a vector x and returns its energy. A class implementing this interface is 24 | // also expected to store x as the current state of the random walk which is used 25 | // when proposing a new state. 26 | // 27 | // Input: 28 | // x - state as a vector of boolean entries (0/1) 29 | // 30 | // Returns: 31 | // The energy (un-normalized log probability) of the inputed state 32 | double IndependentEnergy::getEnergy(uint32_t * x) 33 | { 34 | double energy = 0; 35 | unsigned long active_bits = 0; 36 | 37 | std::memcpy(m_x, x, m_ndims * sizeof(uint32_t)); 38 | 39 | for (unsigned long i = 0; i < m_ndims; i++) 40 | { 41 | // add active lagrange multipliers 42 | energy += (double)x[i] * m_factors[i]; 43 | } 44 | 45 | 46 | m_energy = energy; 47 | return m_energy; 48 | } 49 | 50 | // Proposes a new state obtained by a single bit-flip from the current state, 51 | // and returns the new energy level. This implementation of this function may assume that getEnergy() has been called 52 | // at some point in the past. 53 | // 54 | // Input: 55 | // nbit - bit to flip 56 | // 57 | // Returns: 58 | // The energy (un-normalized log probability) of the new state after the bit flip 59 | double IndependentEnergy::propose(uint32_t nbit) 60 | { 61 | m_proposed_bit = nbit; 62 | if (m_x[nbit]) 63 | { 64 | m_proposed_energy = m_energy - m_factors[nbit]; 65 | } 66 | else 67 | { 68 | m_proposed_energy = m_energy + m_factors[nbit]; 69 | } 70 | 71 | return m_proposed_energy; 72 | } 73 | 74 | 75 | // Accept/reject the proposed change and updates x. 76 | // This function may assume that propose() has been called prior to calling this function. 77 | // 78 | // Input: 79 | // bAccept - if true, then fixes the proposed state as the current state. Otherwise dumps the proposed state and 80 | // reverts to the pre-proposal state. 81 | // 82 | // Returns: 83 | // The energy (un-normalized log probability) of the new state. 84 | double IndependentEnergy::accept(uint32_t bAccept) 85 | { 86 | if (bAccept) { 87 | m_x[m_proposed_bit] = !m_x[m_proposed_bit]; 88 | m_energy = m_proposed_energy; 89 | } 90 | 91 | return m_energy; 92 | } 93 | 94 | 95 | // Accepts the proposed changes and adds factors of current state to a running sum (marginal). 96 | // This function may assume that propose() has been called prior to calling this function. 97 | // 98 | // Input: 99 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the proposed state 100 | // into this vector after multipying them by the argument 'p'. 101 | // p - multiply factors by this number before summing them into factor_sum. 102 | // 103 | // Returns: 104 | // (none) 105 | void IndependentEnergy::accept(double * factor_sum, double p) 106 | { 107 | // change the state to the proposed state 108 | accept(); 109 | 110 | sumSampleFactor(m_x, factor_sum, p); 111 | } 112 | 113 | 114 | // Returns the current state of the system 115 | uint32_t * IndependentEnergy::getX() 116 | { 117 | return m_x; 118 | } 119 | 120 | 121 | // Returns the dimensions of this energy functions' inputs 122 | uint32_t IndependentEnergy::getDim() 123 | { 124 | return m_ndims; 125 | } 126 | 127 | // Returns the number of factors (parameters) of the model 128 | uint32_t IndependentEnergy::getNumFactors() 129 | { 130 | // number off factors in this model is the same as the number of dimensions 131 | return m_ndims; 132 | } 133 | 134 | // Adds the factors of the inputed sample to a vector of factor sums, multiplied by the inputed probabilities. This is mainly used to compute marginals. 135 | // 136 | // Input: 137 | // x - state to compute the factors of. 138 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the inputed state 139 | // into this vector after multipying them by the argument 'p'. 140 | // p - multiply factors by this number before summing them into factor_sum. 141 | // 142 | // Returns: 143 | // (none) 144 | void IndependentEnergy::sumSampleFactor(uint32_t * x, double * factor_sum, double p) 145 | { 146 | double bitsum = 0; 147 | 148 | // activate the active bits multiplied by the population activity 149 | for (unsigned int i=0; i < m_ndims; i++) 150 | { 151 | factor_sum[i] += p * (double)x[i]; 152 | } 153 | 154 | } 155 | // Returns the partition function (it it is known, otherwise just returns 0) 156 | double IndependentEnergy::getLogZ() 157 | { 158 | return m_logz; 159 | } 160 | 161 | // Sets the partition function 162 | void IndependentEnergy::setLogZ(double logz) 163 | { 164 | m_logz = logz; 165 | } -------------------------------------------------------------------------------- /mex_code/IndependentEnergy.h: -------------------------------------------------------------------------------- 1 | // Energy function of an independent distribution 2 | // July 2015 3 | 4 | #pragma once 5 | #include "EnergyFunction.h" 6 | 7 | class IndependentEnergy : public EnergyFunction 8 | { 9 | public: 10 | 11 | // constructor 12 | IndependentEnergy(uint32_t ndims, double * factors); 13 | 14 | // destructor 15 | virtual ~IndependentEnergy(); 16 | 17 | // Accepts a vector x and returns its energy. A class implementing this interface is 18 | // also expected to store x as the current state of the random walk which is used 19 | // when proposing a new state. 20 | // 21 | // Input: 22 | // x - state as a vector of boolean entries (0/1) 23 | // 24 | // Returns: 25 | // The energy (un-normalized log probability) of the inputed state 26 | virtual double getEnergy(uint32_t * x); 27 | 28 | // Proposes a new state obtained by a single bit-flip from the current state, 29 | // and returns the new energy level. This implementation of this function may assume that getEnergy() has been called 30 | // at some point in the past. 31 | // 32 | // Input: 33 | // nbit - bit to flip 34 | // 35 | // Returns: 36 | // The energy (un-normalized log probability) of the new state after the bit flip 37 | virtual double propose(uint32_t nbit); 38 | 39 | // Accept/reject the proposed change and updates x. 40 | // This function may assume that propose() has been called prior to calling this function. 41 | // 42 | // Input: 43 | // bAccept - if true, then fixes the proposed state as the current state. Otherwise dumps the proposed state and 44 | // reverts to the pre-proposal state. 45 | // 46 | // Returns: 47 | // The energy (un-normalized log probability) of the new state. 48 | virtual double accept(uint32_t bAccept = 1); 49 | 50 | // Accepts the proposed changes and adds factors of current state to a running sum (marginal). 51 | // This function may assume that propose() has been called prior to calling this function. 52 | // 53 | // Input: 54 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the proposed state 55 | // into this vector after multipying them by the argument 'p'. 56 | // p - multiply factors by this number before summing them into factor_sum. 57 | // 58 | // Returns: 59 | // (none) 60 | virtual void accept(double * factor_sum, double p); 61 | 62 | // Returns the current state of the system 63 | virtual uint32_t * getX(); 64 | 65 | // Returns the dimensions of this energy functions' inputs 66 | virtual uint32_t getDim(); 67 | 68 | // Returns the number of factors (parameters) of the model 69 | virtual uint32_t getNumFactors(); 70 | 71 | // Adds the factors of the inputed sample to a vector of factor sums, multiplied by the inputed probabilities. This is mainly used to compute marginals. 72 | // 73 | // Input: 74 | // x - state to compute the factors of. 75 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the inputed state 76 | // into this vector after multipying them by the argument 'p'. 77 | // p - multiply factors by this number before summing them into factor_sum. 78 | // 79 | // Returns: 80 | // (none) 81 | virtual void sumSampleFactor(uint32_t * x, double * factor_sum, double p); 82 | 83 | // Returns the partition function (it it is known, otherwise just returns 0) 84 | virtual double getLogZ(); 85 | 86 | // Sets the partition function 87 | virtual void setLogZ(double logz); 88 | 89 | 90 | private: 91 | // current state 92 | //std::vector m_x; 93 | uint32_t * m_x; 94 | 95 | // last bit that has been proposed 96 | uint32_t m_proposed_bit; 97 | double m_proposed_energy; 98 | 99 | // current energy 100 | double m_energy; 101 | 102 | // number of dimensions 103 | uint32_t m_ndims; 104 | 105 | // partition function 106 | double m_logz; 107 | 108 | // lagrage multipliers 109 | double * m_factors; 110 | 111 | 112 | 113 | 114 | }; 115 | 116 | -------------------------------------------------------------------------------- /mex_code/IsingEnergy.h: -------------------------------------------------------------------------------- 1 | // Class that implements an Ising model energy function 2 | // This an experimental accelerated version which uses a matrix representation of the lagrange multipliers 3 | #include "EnergyFunction.h" 4 | #include 5 | #pragma once 6 | 7 | 8 | #include "common.h" 9 | 10 | class IsingEnergy : public EnergyFunction 11 | { 12 | 13 | 14 | public: 15 | 16 | 17 | 18 | // Constructor 19 | IsingEnergy(uint32_t ndims, double * lambdas); 20 | 21 | // destructor 22 | ~IsingEnergy(); 23 | 24 | public: 25 | 26 | // Accepts a vector x and returns its energy. A class implementing this interface is 27 | // also expected to store x as the current state of the random walk which is used 28 | // when proposing a new state. 29 | // 30 | // Input: 31 | // x - state as a vector of boolean entries (0/1) 32 | // 33 | // Returns: 34 | // The energy (un-normalized log probability) of the inputed state 35 | virtual double getEnergy(uint32_t * x); 36 | 37 | // Proposes a new state obtained by a single bit-flip from the current state, 38 | // and returns the new energy level. This implementation of this function may assume that getEnergy() has been called 39 | // at some point in the past. 40 | // 41 | // Input: 42 | // nbit - bit to flip 43 | // 44 | // Returns: 45 | // The energy (un-normalized log probability) of the new state after the bit flip 46 | virtual double propose(uint32_t nbit); 47 | 48 | // Accept/reject the proposed change and updates x. 49 | // This function may assume that propose() has been called prior to calling this function. 50 | // 51 | // Input: 52 | // bAccept - if true, then fixes the proposed state as the current state. Otherwise dumps the proposed state and 53 | // reverts to the pre-proposal state. 54 | // 55 | // Returns: 56 | // The energy (un-normalized log probability) of the new state. 57 | virtual double accept(uint32_t bAccept = 1); 58 | 59 | // Accepts the proposed changes and adds factors of current state to a running sum (marginal). 60 | // This function may assume that propose() has been called prior to calling this function. 61 | // 62 | // Input: 63 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the proposed state 64 | // into this vector after multipying them by the argument 'p'. 65 | // p - multiply factors by this number before summing them into factor_sum. 66 | // 67 | // Returns: 68 | // (none) 69 | virtual void accept(double * factor_sum, double p); 70 | 71 | // Returns the current state of the system 72 | virtual uint32_t * getX(); 73 | 74 | // Returns the dimensions of this energy functions' inputs 75 | virtual uint32_t getDim(); 76 | 77 | // Returns the number of factors (parameters) of the model 78 | virtual uint32_t getNumFactors(); 79 | 80 | // Adds the factors of the inputed sample to a vector of factor sums, multiplied by the inputed probabilities. This is mainly used to compute marginals. 81 | // 82 | // Input: 83 | // x - state to compute the factors of. 84 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the inputed state 85 | // into this vector after multipying them by the argument 'p'. 86 | // p - multiply factors by this number before summing them into factor_sum. 87 | // 88 | // Returns: 89 | // (none) 90 | virtual void sumSampleFactor(uint32_t * x, double * factor_sum, double p); 91 | 92 | // Returns the partition function (it it is known, otherwise just returns 0) 93 | virtual double getLogZ(); 94 | 95 | // Sets the partition function 96 | virtual void setLogZ(double logz); 97 | 98 | 99 | private: 100 | 101 | std::vector m_x; 102 | DECLARE_ALIGNED double * m_double_x ALIGN_AFTER; // double version of m_x for accelerated computation 103 | uint32_t m_proposed_bit; 104 | double m_energy; 105 | double m_logz; 106 | double m_proposed_energy[2]; 107 | DECLARE_ALIGNED uint32_t m_ndims ALIGN_AFTER; 108 | double * m_lambdaMatrix; // lambdas in (symmetric) matrix format 109 | double * m_lambdaVector; // lambdas in vector format 110 | }; -------------------------------------------------------------------------------- /mex_code/KIsingEnergy.cpp: -------------------------------------------------------------------------------- 1 | #include "KIsingEnergy.h" 2 | 3 | 4 | 5 | // Constructor - constructs it with a set of factors 6 | KIsingEnergy::KIsingEnergy(uint32_t ncells, double * factors) : 7 | m_KSyncEnergy(ncells + 1, factors), 8 | m_IsingEnergy(ncells, factors + (ncells + 1)), 9 | m_logz(0) 10 | { 11 | // The first (n+1) factors are k-sync factors, the rest are ising 12 | 13 | m_ndims = ncells; 14 | } 15 | 16 | 17 | // Accepts a vector x and returns its energy. A class implementing this interface is 18 | // also expected to store x as the current state of the random walk which is used 19 | // when proposing a new state. 20 | // 21 | // Input: 22 | // x - state as a vector of boolean entries (0/1) 23 | // 24 | // Returns: 25 | // The energy (un-normalized log probability) of the inputed state 26 | double KIsingEnergy::getEnergy(uint32_t * x) 27 | { 28 | return m_KSyncEnergy.getEnergy(x) + m_IsingEnergy.getEnergy(x); 29 | } 30 | 31 | // Proposes a new state obtained by a single bit-flip from the current state, 32 | // and returns the new energy level. This implementation of this function may assume that getEnergy() has been called 33 | // at some point in the past. 34 | // 35 | // Input: 36 | // nbit - bit to flip 37 | // 38 | // Returns: 39 | // The energy (un-normalized log probability) of the new state after the bit flip 40 | double KIsingEnergy::propose(uint32_t nbit) 41 | { 42 | m_proposed_energy = m_KSyncEnergy.propose(nbit) + m_IsingEnergy.propose(nbit); 43 | return m_proposed_energy; 44 | } 45 | 46 | // Accept/reject the proposed change and updates x. 47 | // This function may assume that propose() has been called prior to calling this function. 48 | // 49 | // Input: 50 | // bAccept - if true, then fixes the proposed state as the current state. Otherwise dumps the proposed state and 51 | // reverts to the pre-proposal state. 52 | // 53 | // Returns: 54 | // The energy (un-normalized log probability) of the new state. 55 | double KIsingEnergy::accept(uint32_t bAccept) 56 | { 57 | return m_KSyncEnergy.accept(bAccept) + m_IsingEnergy.accept(bAccept); 58 | } 59 | 60 | // Accepts the proposed changes and adds factors of current state to a running sum (marginal). 61 | // This function may assume that propose() has been called prior to calling this function. 62 | // 63 | // Input: 64 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the proposed state 65 | // into this vector after multipying them by the argument 'p'. 66 | // p - multiply factors by this number before summing them into factor_sum. 67 | // 68 | // Returns: 69 | // (none) 70 | void KIsingEnergy::accept(double * factor_sum, double prob) 71 | { 72 | m_KSyncEnergy.accept(factor_sum, prob); 73 | m_IsingEnergy.accept(factor_sum + (m_ndims + 1), prob); 74 | 75 | } 76 | 77 | 78 | // Returns the current state of the system 79 | uint32_t * KIsingEnergy::getX() 80 | { 81 | return m_IsingEnergy.getX(); 82 | } 83 | 84 | // Returns the dimensions of this energy functions' inputs 85 | uint32_t KIsingEnergy::getDim() 86 | { 87 | return m_ndims; 88 | } 89 | 90 | // Returns the number of factors (parameters) of the model 91 | uint32_t KIsingEnergy::getNumFactors() 92 | { 93 | return m_KSyncEnergy.getNumFactors() + m_IsingEnergy.getNumFactors(); 94 | } 95 | 96 | // Adds the factors of the inputed sample to a vector of factor sums, multiplied by the inputed probabilities. This is mainly used to compute marginals. 97 | // 98 | // Input: 99 | // x - state to compute the factors of. 100 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the inputed state 101 | // into this vector after multipying them by the argument 'p'. 102 | // p - multiply factors by this number before summing them into factor_sum. 103 | // 104 | // Returns: 105 | // (none) 106 | void KIsingEnergy::sumSampleFactor(uint32_t * x, double * factor_sum, double p) 107 | { 108 | // Each of the energy functions only sees part of the factors, so we transfer 109 | // them corresponding halves of the factor sum to each of the sub-functions 110 | m_KSyncEnergy.sumSampleFactor(x, factor_sum, p); 111 | m_IsingEnergy.sumSampleFactor(x, factor_sum + (m_ndims + 1), p); 112 | } 113 | 114 | // Returns the partition function (it it is known, otherwise just returns 0) 115 | double KIsingEnergy::getLogZ() 116 | { 117 | return m_logz; 118 | } 119 | 120 | // Sets the partition function 121 | void KIsingEnergy::setLogZ(double logz) 122 | { 123 | m_logz = logz; 124 | } 125 | 126 | -------------------------------------------------------------------------------- /mex_code/KIsingEnergy.h: -------------------------------------------------------------------------------- 1 | // Class that implements a K-Isingmodel energy function 2 | #include "EnergyFunction.h" 3 | #include "IsingEnergy.h" 4 | #include "KSyncEnergy.h" 5 | #include 6 | 7 | 8 | // A K-Ising model combines K-Synchrony constraints with K-Ising constraints. 9 | // As such, we can simply relegate both to their corresponding energy functions, 10 | // and this class would simply serve as a wrapper. 11 | class KIsingEnergy : public EnergyFunction 12 | { 13 | public: 14 | 15 | // Constructor - constructs it with a set of factors 16 | KIsingEnergy(uint32_t ncells, double * factors); 17 | 18 | 19 | // Accepts a vector x and returns its energy. A class implementing this interface is 20 | // also expected to store x as the current state of the random walk which is used 21 | // when proposing a new state. 22 | // 23 | // Input: 24 | // x - state as a vector of boolean entries (0/1) 25 | // 26 | // Returns: 27 | // The energy (un-normalized log probability) of the inputed state 28 | virtual double getEnergy(uint32_t * x); 29 | 30 | // Proposes a new state obtained by a single bit-flip from the current state, 31 | // and returns the new energy level. This implementation of this function may assume that getEnergy() has been called 32 | // at some point in the past. 33 | // 34 | // Input: 35 | // nbit - bit to flip 36 | // 37 | // Returns: 38 | // The energy (un-normalized log probability) of the new state after the bit flip 39 | virtual double propose(uint32_t nbit); 40 | 41 | // Accept/reject the proposed change and updates x. 42 | // This function may assume that propose() has been called prior to calling this function. 43 | // 44 | // Input: 45 | // bAccept - if true, then fixes the proposed state as the current state. Otherwise dumps the proposed state and 46 | // reverts to the pre-proposal state. 47 | // 48 | // Returns: 49 | // The energy (un-normalized log probability) of the new state. 50 | virtual double accept(uint32_t bAccept = 1); 51 | 52 | // Accepts the proposed changes and adds factors of current state to a running sum (marginal). 53 | // This function may assume that propose() has been called prior to calling this function. 54 | // 55 | // Input: 56 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the proposed state 57 | // into this vector after multipying them by the argument 'p'. 58 | // p - multiply factors by this number before summing them into factor_sum. 59 | // 60 | // Returns: 61 | // (none) 62 | virtual void accept(double * factor_sum, double p); 63 | 64 | // Returns the current state of the system 65 | virtual uint32_t * getX(); 66 | 67 | // Returns the dimensions of this energy functions' inputs 68 | virtual uint32_t getDim(); 69 | 70 | // Returns the number of factors (parameters) of the model 71 | virtual uint32_t getNumFactors(); 72 | 73 | // Adds the factors of the inputed sample to a vector of factor sums, multiplied by the inputed probabilities. This is mainly used to compute marginals. 74 | // 75 | // Input: 76 | // x - state to compute the factors of. 77 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the inputed state 78 | // into this vector after multipying them by the argument 'p'. 79 | // p - multiply factors by this number before summing them into factor_sum. 80 | // 81 | // Returns: 82 | // (none) 83 | virtual void sumSampleFactor(uint32_t * x, double * factor_sum, double p); 84 | 85 | // Returns the partition function (it it is known, otherwise just returns 0) 86 | virtual double getLogZ(); 87 | 88 | // Sets the partition function 89 | virtual void setLogZ(double logz); 90 | private: 91 | 92 | IsingEnergy m_IsingEnergy; 93 | KSyncEnergy m_KSyncEnergy; 94 | 95 | uint32_t m_ndims; 96 | double m_logz; 97 | double m_proposed_energy; 98 | 99 | }; -------------------------------------------------------------------------------- /mex_code/KSyncEnergy.cpp: -------------------------------------------------------------------------------- 1 | #include "KSyncEnergy.h" 2 | 3 | #define MAX_LENGTH 500 4 | 5 | 6 | // Constructor - constructs it with a set of factors 7 | KSyncEnergy::KSyncEnergy(uint32_t nfactors, double * factors) : 8 | m_factors(factors, factors + nfactors), m_logz(0) 9 | { 10 | // There is 1 factor more than there are dimensions (because they are for 11 | // all the cases from 0 cells firing up to n cells firing) 12 | m_ndims = nfactors - 1; 13 | } 14 | 15 | 16 | // Accepts a vector x and returns its energy. A class implementing this interface is 17 | // also expected to store x as the current state of the random walk which is used 18 | // when proposing a new state. 19 | // 20 | // Input: 21 | // x - state as a vector of boolean entries (0/1) 22 | // 23 | // Returns: 24 | // The energy (un-normalized log probability) of the inputed state 25 | double KSyncEnergy::getEnergy(uint32_t * x) 26 | { 27 | uint32_t bitsum = 0; 28 | 29 | // Just count how many ones we have 30 | for (unsigned int i = 0; i < m_ndims; i++) 31 | { 32 | if (x[i]) { 33 | bitsum++; 34 | } 35 | } 36 | 37 | // This the value of x for later so we can make incremental changes 38 | m_x.assign(x, x + m_ndims); 39 | 40 | m_nbitson = bitsum; 41 | m_energy = m_factors[bitsum]; 42 | 43 | return m_energy; 44 | } 45 | 46 | // Proposes a new state obtained by a single bit-flip from the current state, 47 | // and returns the new energy level. This implementation of this function may assume that getEnergy() has been called 48 | // at some point in the past. 49 | // 50 | // Input: 51 | // nbit - bit to flip 52 | // 53 | // Returns: 54 | // The energy (un-normalized log probability) of the new state after the bit flip 55 | double KSyncEnergy::propose(uint32_t nbit) 56 | { 57 | // Save this proposed bit for later 58 | m_proposed_bit = nbit; 59 | 60 | // Check the bit that changes 61 | if (m_x[nbit]) 62 | { 63 | // it is changing 1->0 64 | m_proposed_energy = m_factors[m_nbitson - 1]; 65 | } 66 | else 67 | { 68 | // it is changing 0->1 69 | m_proposed_energy = m_factors[m_nbitson + 1]; 70 | } 71 | 72 | 73 | return m_proposed_energy; 74 | } 75 | 76 | // Accept/reject the proposed change and updates x. 77 | // This function may assume that propose() has been called prior to calling this function. 78 | // 79 | // Input: 80 | // bAccept - if true, then fixes the proposed state as the current state. Otherwise dumps the proposed state and 81 | // reverts to the pre-proposal state. 82 | // 83 | // Returns: 84 | // The energy (un-normalized log probability) of the new state. 85 | double KSyncEnergy::accept(uint32_t bAccept) 86 | { 87 | if (bAccept) 88 | { 89 | m_x[m_proposed_bit] = !m_x[m_proposed_bit]; 90 | 91 | // keep track of how many bits are 1 92 | if (m_x[m_proposed_bit]) 93 | { 94 | m_nbitson++; 95 | } 96 | else 97 | { 98 | m_nbitson--; 99 | } 100 | 101 | m_energy = m_proposed_energy; 102 | } 103 | return m_energy; 104 | } 105 | 106 | // Accepts the proposed changes and adds factors of current state to a running sum (marginal). 107 | // This function may assume that propose() has been called prior to calling this function. 108 | // 109 | // Input: 110 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the proposed state 111 | // into this vector after multipying them by the argument 'p'. 112 | // p - multiply factors by this number before summing them into factor_sum. 113 | // 114 | // Returns: 115 | // (none) 116 | void KSyncEnergy::accept(double * factor_sum, double p) 117 | { 118 | 119 | // change the state to the proposed state 120 | accept(); 121 | 122 | sumSampleFactor(m_x.data(), factor_sum, p); 123 | } 124 | 125 | 126 | // Returns the current state of the system 127 | uint32_t * KSyncEnergy::getX() 128 | { 129 | return m_x.data(); 130 | } 131 | 132 | // Returns the dimensions of this energy functions' inputs 133 | uint32_t KSyncEnergy::getDim() 134 | { 135 | return m_ndims; 136 | } 137 | 138 | 139 | // Returns the number of factors (parameters) of the model 140 | uint32_t KSyncEnergy::getNumFactors() 141 | { 142 | return m_factors.size(); 143 | } 144 | 145 | // Adds the factors of the inputed sample to a vector of factor sums, multiplied by the inputed probabilities. This is mainly used to compute marginals. 146 | // 147 | // Input: 148 | // x - state to compute the factors of. 149 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the inputed state 150 | // into this vector after multipying them by the argument 'p'. 151 | // p - multiply factors by this number before summing them into factor_sum. 152 | // 153 | // Returns: 154 | // (none) 155 | void KSyncEnergy::sumSampleFactor(uint32_t * x, double * factor_sum, double p) 156 | { 157 | uint32_t bitsum = 0; 158 | 159 | // Just count how many ones we have 160 | for (unsigned int i = 0; i < m_ndims; i++) 161 | { 162 | if (x[i]) { 163 | bitsum++; 164 | } 165 | } 166 | 167 | factor_sum[bitsum] += p; 168 | 169 | } 170 | 171 | 172 | // Returns the partition function (it it is known, otherwise just returns 0) 173 | double KSyncEnergy::getLogZ() 174 | { 175 | return m_logz; 176 | } 177 | 178 | 179 | // Sets the partition function 180 | void KSyncEnergy::setLogZ(double logz) 181 | { 182 | m_logz = logz; 183 | } -------------------------------------------------------------------------------- /mex_code/KSyncEnergy.h: -------------------------------------------------------------------------------- 1 | // Class that implements a K-synchrony model energy function 2 | #include "EnergyFunction.h" 3 | #include 4 | #pragma once 5 | 6 | class KSyncEnergy : public EnergyFunction 7 | { 8 | public: 9 | 10 | // Constructor - constructs it with a set of factors 11 | KSyncEnergy(uint32_t nfactors, double * factors); 12 | 13 | 14 | 15 | // Accepts a vector x and returns its energy. A class implementing this interface is 16 | // also expected to store x as the current state of the random walk which is used 17 | // when proposing a new state. 18 | // 19 | // Input: 20 | // x - state as a vector of boolean entries (0/1) 21 | // 22 | // Returns: 23 | // The energy (un-normalized log probability) of the inputed state 24 | virtual double getEnergy(uint32_t * x); 25 | 26 | // Proposes a new state obtained by a single bit-flip from the current state, 27 | // and returns the new energy level. This implementation of this function may assume that getEnergy() has been called 28 | // at some point in the past. 29 | // 30 | // Input: 31 | // nbit - bit to flip 32 | // 33 | // Returns: 34 | // The energy (un-normalized log probability) of the new state after the bit flip 35 | virtual double propose(uint32_t nbit); 36 | 37 | // Accept/reject the proposed change and updates x. 38 | // This function may assume that propose() has been called prior to calling this function. 39 | // 40 | // Input: 41 | // bAccept - if true, then fixes the proposed state as the current state. Otherwise dumps the proposed state and 42 | // reverts to the pre-proposal state. 43 | // 44 | // Returns: 45 | // The energy (un-normalized log probability) of the new state. 46 | virtual double accept(uint32_t bAccept = 1); 47 | 48 | // Accepts the proposed changes and adds factors of current state to a running sum (marginal). 49 | // This function may assume that propose() has been called prior to calling this function. 50 | // 51 | // Input: 52 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the proposed state 53 | // into this vector after multipying them by the argument 'p'. 54 | // p - multiply factors by this number before summing them into factor_sum. 55 | // 56 | // Returns: 57 | // (none) 58 | virtual void accept(double * factor_sum, double p); 59 | 60 | // Returns the current state of the system 61 | virtual uint32_t * getX(); 62 | 63 | // Returns the dimensions of this energy functions' inputs 64 | virtual uint32_t getDim(); 65 | 66 | // Returns the number of factors (parameters) of the model 67 | virtual uint32_t getNumFactors(); 68 | 69 | // Adds the factors of the inputed sample to a vector of factor sums, multiplied by the inputed probabilities. This is mainly used to compute marginals. 70 | // 71 | // Input: 72 | // x - state to compute the factors of. 73 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the inputed state 74 | // into this vector after multipying them by the argument 'p'. 75 | // p - multiply factors by this number before summing them into factor_sum. 76 | // 77 | // Returns: 78 | // (none) 79 | virtual void sumSampleFactor(uint32_t * x, double * factor_sum, double p); 80 | 81 | // Returns the partition function (it it is known, otherwise just returns 0) 82 | virtual double getLogZ(); 83 | 84 | // Sets the partition function 85 | virtual void setLogZ(double logz); 86 | 87 | 88 | private: 89 | 90 | std::vector m_x; 91 | uint32_t m_proposed_bit; 92 | uint32_t m_nbitson; // number of bits in x that are currently on 93 | double m_energy; 94 | double m_logz; 95 | double m_proposed_energy; 96 | uint32_t m_ndims; 97 | std::vector m_factors; 98 | }; -------------------------------------------------------------------------------- /mex_code/MerpFastEnergy.h: -------------------------------------------------------------------------------- 1 | // Implementation of the EnergyFunction interface on the MERP model. 2 | // Ori Maoz 07/2014 3 | 4 | #include "EnergyFunction.h" 5 | #include 6 | #include "common.h" 7 | 8 | class MerpFastEnergy : public EnergyFunction 9 | { 10 | public: 11 | 12 | // Constructor - constructs it from a random projection and a single threshold 13 | MerpFastEnergy(float* in_W, double * in_lambda, uint32_t ncells, uint32_t nfactors, float cutoff, double bStochasticSlope = 0); 14 | 15 | // Constructor - construct it from a random projection and a set of thresholds 16 | MerpFastEnergy(float* in_W, double * in_lambda, uint32_t ncells, uint32_t nfactors, float * in_thresholds, double bStochasticSlope = 0); 17 | 18 | // destructor 19 | virtual ~MerpFastEnergy(); 20 | 21 | 22 | // Accepts a vector x and returns its energy. A class implementing this interface is 23 | // also expected to store x as the current state of the random walk which is used 24 | // when proposing a new state. 25 | // 26 | // Input: 27 | // x - state as a vector of boolean entries (0/1) 28 | // 29 | // Returns: 30 | // The energy (un-normalized log probability) of the inputed state 31 | virtual double getEnergy(uint32_t * x); 32 | 33 | // Proposes a new state obtained by a single bit-flip from the current state, 34 | // and returns the new energy level. This implementation of this function may assume that getEnergy() has been called 35 | // at some point in the past. 36 | // 37 | // Input: 38 | // nbit - bit to flip 39 | // 40 | // Returns: 41 | // The energy (un-normalized log probability) of the new state after the bit flip 42 | virtual double propose(uint32_t nbit); 43 | 44 | // Accept/reject the proposed change and updates x. 45 | // This function may assume that propose() has been called prior to calling this function. 46 | // 47 | // Input: 48 | // bAccept - if true, then fixes the proposed state as the current state. Otherwise dumps the proposed state and 49 | // reverts to the pre-proposal state. 50 | // 51 | // Returns: 52 | // The energy (un-normalized log probability) of the new state. 53 | virtual double accept(uint32_t bAccept = 1); 54 | 55 | // Accepts the proposed changes and adds factors of current state to a running sum (marginal). 56 | // This function may assume that propose() has been called prior to calling this function. 57 | // 58 | // Input: 59 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the proposed state 60 | // into this vector after multipying them by the argument 'p'. 61 | // p - multiply factors by this number before summing them into factor_sum. 62 | // 63 | // Returns: 64 | // (none) 65 | virtual void accept(double * factor_sum, double p); 66 | 67 | // Returns the current state of the system 68 | virtual uint32_t * getX(); 69 | 70 | // Returns the dimensions of this energy functions' inputs 71 | virtual uint32_t getDim(); 72 | 73 | // Returns the number of factors (parameters) of the model 74 | virtual uint32_t getNumFactors(); 75 | 76 | // Adds the factors of the inputed sample to a vector of factor sums, multiplied by the inputed probabilities. This is mainly used to compute marginals. 77 | // 78 | // Input: 79 | // x - state to compute the factors of. 80 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the inputed state 81 | // into this vector after multipying them by the argument 'p'. 82 | // p - multiply factors by this number before summing them into factor_sum. 83 | // 84 | // Returns: 85 | // (none) 86 | virtual void sumSampleFactor(uint32_t * x, double * factor_sum, double p); 87 | 88 | // Returns the partition function (it it is known, otherwise just returns 0) 89 | virtual double getLogZ(); 90 | 91 | // Sets the partition function 92 | virtual void setLogZ(double logz); 93 | 94 | 95 | private: 96 | 97 | // Common initialization called from the constructors 98 | void init() 99 | { 100 | 101 | } 102 | 103 | 104 | //double applyThreshold(DynamicVector & y); 105 | double applyThreshold(float * y); 106 | 107 | //DynamicMatrix m_W; 108 | DECLARE_ALIGNED float * m_W ALIGN_AFTER; 109 | DECLARE_ALIGNED double * m_lambda ALIGN_AFTER; 110 | DECLARE_ALIGNED float * m_threshold ALIGN_AFTER; 111 | DECLARE_ALIGNED float * m_y ALIGN_AFTER; 112 | DECLARE_ALIGNED float * m_proposed_y ALIGN_AFTER; 113 | DECLARE_ALIGNED float * m_tmp ALIGN_AFTER; 114 | 115 | std::vector m_x; 116 | uint32_t m_proposed_bit; 117 | 118 | DECLARE_ALIGNED double m_energy ALIGN_AFTER; 119 | DECLARE_ALIGNED double m_logz ALIGN_AFTER; 120 | DECLARE_ALIGNED double m_proposed_energy ALIGN_AFTER; 121 | DECLARE_ALIGNED uint32_t m_ndims ALIGN_AFTER; 122 | DECLARE_ALIGNED uint32_t m_nfactors ALIGN_AFTER; 123 | DECLARE_ALIGNED 124 | DECLARE_ALIGNED bool m_bProposed ALIGN_AFTER; 125 | DECLARE_ALIGNED 126 | DECLARE_ALIGNED bool m_bStochastic ALIGN_AFTER; // true if the model has a stochastic response 127 | DECLARE_ALIGNED double m_stochasticSlope ALIGN_AFTER; // slope for stochastic function 128 | }; -------------------------------------------------------------------------------- /mex_code/MerpSparseEnergy.h: -------------------------------------------------------------------------------- 1 | // Implementation of the EnergyFunction interface on the MERP model. 2 | // This version uses a sparse representation of the connection matrix W to facilitate faster operations 3 | // when the matrix is sparse 4 | // Ori Maoz 11/2014 5 | 6 | #include "EnergyFunction.h" 7 | #include 8 | #include "common.h" 9 | 10 | 11 | class MerpSparseEnergy : public EnergyFunction 12 | { 13 | public: 14 | 15 | // Constructor - constructs it from a random projection and a single threshold 16 | MerpSparseEnergy(float* in_W, double * in_lambda, uint32_t ncells, uint32_t nfactors, float * in_thresholds, double bStochasticSlope = 0); 17 | 18 | // destructor 19 | virtual ~MerpSparseEnergy(); 20 | 21 | // Accepts a vector x and returns its energy. A class implementing this interface is 22 | // also expected to store x as the current state of the random walk which is used 23 | // when proposing a new state. 24 | // 25 | // Input: 26 | // x - state as a vector of boolean entries (0/1) 27 | // 28 | // Returns: 29 | // The energy (un-normalized log probability) of the inputed state 30 | virtual double getEnergy(uint32_t * x); 31 | 32 | // Proposes a new state obtained by a single bit-flip from the current state, 33 | // and returns the new energy level. This implementation of this function may assume that getEnergy() has been called 34 | // at some point in the past. 35 | // 36 | // Input: 37 | // nbit - bit to flip 38 | // 39 | // Returns: 40 | // The energy (un-normalized log probability) of the new state after the bit flip 41 | virtual double propose(uint32_t nbit); 42 | 43 | // Accept/reject the proposed change and updates x. 44 | // This function may assume that propose() has been called prior to calling this function. 45 | // 46 | // Input: 47 | // bAccept - if true, then fixes the proposed state as the current state. Otherwise dumps the proposed state and 48 | // reverts to the pre-proposal state. 49 | // 50 | // Returns: 51 | // The energy (un-normalized log probability) of the new state. 52 | virtual double accept(uint32_t bAccept = 1); 53 | 54 | // Accepts the proposed changes and adds factors of current state to a running sum (marginal). 55 | // This function may assume that propose() has been called prior to calling this function. 56 | // 57 | // Input: 58 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the proposed state 59 | // into this vector after multipying them by the argument 'p'. 60 | // p - multiply factors by this number before summing them into factor_sum. 61 | // 62 | // Returns: 63 | // (none) 64 | virtual void accept(double * factor_sum, double p); 65 | 66 | // Returns the current state of the system 67 | uint32_t * getX(); 68 | 69 | // Returns the dimensions of this energy functions' inputs 70 | virtual uint32_t getDim(); 71 | 72 | // Returns the number of factors (parameters) of the model 73 | virtual uint32_t getNumFactors(); 74 | 75 | // Adds the factors of the inputed sample to a vector of factor sums, multiplied by the inputed probabilities. This is mainly used to compute marginals. 76 | // 77 | // Input: 78 | // x - state to compute the factors of. 79 | // factor_sum - vector of marginals (one for each factor). The function should sum the factors of the inputed state 80 | // into this vector after multipying them by the argument 'p'. 81 | // p - multiply factors by this number before summing them into factor_sum. 82 | // 83 | // Returns: 84 | // (none) 85 | virtual void sumSampleFactor(uint32_t * x, double * factor_sum, double p); 86 | 87 | // Returns the partition function (it it is known, otherwise just returns 0) 88 | virtual double getLogZ(); 89 | 90 | // Sets the partition function 91 | virtual void setLogZ(double logz); 92 | 93 | 94 | 95 | private: 96 | 97 | 98 | double applyThreshold(float * y); 99 | 100 | double getSumDiffBitOn(uint32_t nbit); 101 | double getSumDiffBitOff(uint32_t nbit); 102 | 103 | 104 | 105 | DECLARE_ALIGNED float ** m_W_val ALIGN_AFTER; // sparse columns of W (values) 106 | DECLARE_ALIGNED MKL_INT ** m_W_idx ALIGN_AFTER; // sparse columns of W (indices) 107 | DECLARE_ALIGNED uint32_t * m_W_nz ALIGN_AFTER; // sparse columns of W (lengths) 108 | 109 | DECLARE_ALIGNED double * m_lambda ALIGN_AFTER; 110 | DECLARE_ALIGNED double ** m_sparse_lambda ALIGN_AFTER; // lambdas sectioned into sparsified parts of W 111 | 112 | DECLARE_ALIGNED float * m_threshold ALIGN_AFTER; 113 | DECLARE_ALIGNED float * m_y ALIGN_AFTER; 114 | DECLARE_ALIGNED float * m_tmp ALIGN_AFTER; 115 | 116 | std::vector m_x; 117 | uint32_t m_proposed_bit; 118 | 119 | DECLARE_ALIGNED double m_energy ALIGN_AFTER; 120 | DECLARE_ALIGNED double m_energy_diff ALIGN_AFTER; 121 | DECLARE_ALIGNED double m_logz ALIGN_AFTER; 122 | DECLARE_ALIGNED double m_proposed_energy ALIGN_AFTER; 123 | DECLARE_ALIGNED uint32_t m_ndims ALIGN_AFTER; 124 | DECLARE_ALIGNED uint32_t m_nfactors ALIGN_AFTER; 125 | DECLARE_ALIGNED bool m_bProposed ALIGN_AFTER; 126 | 127 | DECLARE_ALIGNED bool m_bStochastic ALIGN_AFTER; // true if the model has a stochastic response 128 | DECLARE_ALIGNED double m_stochasticSlope ALIGN_AFTER; // slope for stochastic function 129 | 130 | }; -------------------------------------------------------------------------------- /mex_code/autotester/autotester.m: -------------------------------------------------------------------------------- 1 | % automatic testing for some basic functions of the maxent package 2 | load maxent_tests 3 | result = runMaxentTests(maxent_tests); -------------------------------------------------------------------------------- /mex_code/autotester/maxent_tests.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/orimaoz/maxent_toolbox/82b4528675fed43beb1d90a1c06b43189afe023e/mex_code/autotester/maxent_tests.mat -------------------------------------------------------------------------------- /mex_code/autotester/runMaxentTests.m: -------------------------------------------------------------------------------- 1 | function all_pass = runMaxentTests(all_testdata) 2 | 3 | % running tests 4 | 5 | epsilon = 1e-10; 6 | all_pass = true; 7 | for i = 1:numel(all_testdata) 8 | testdata = all_testdata{i}; 9 | fprintf('%s: ',testdata.description); 10 | eval(testdata.command); 11 | bresult = eval(testdata.test); 12 | if bresult 13 | fprintf('pass\n'); 14 | else 15 | fprintf('FAIL\n'); 16 | end 17 | 18 | all_pass = all_pass && bresult; 19 | end 20 | 21 | if all_pass 22 | fprintf('all tests: pass\n'); 23 | else 24 | fprintf('all tests: FAIL\n'); 25 | end 26 | 27 | end -------------------------------------------------------------------------------- /mex_code/autotester/testMarginals.m: -------------------------------------------------------------------------------- 1 | % checks whether the model is within (threshold) standard deviations of the empirical marginals 2 | function result = testMarginals(model, data, threshold) 3 | 4 | nsamples = size(data,2); 5 | 6 | % we assume that getEmpiricalMarginals and getMarginals have already been verified to work fine... 7 | empirical_marginals = maxent.getEmpiricalMarginals(data,model); 8 | model_marginals = maxent.getMarginals(model); 9 | 10 | % find the expected standard error of each marginal 11 | [phat, marginals_pci] = binofit(round(empirical_marginals * nsamples),nsamples,0.32); 12 | empirical_marginals_std = max(abs(repmat(phat',[1 2]) - marginals_pci),[],2)'; 13 | 14 | % normalize the errors to units of standard deviations (sample noise) 15 | marginal_error = abs(model_marginals - empirical_marginals) ./ empirical_marginals_std; 16 | 17 | result = max(marginal_error) < threshold; 18 | 19 | end -------------------------------------------------------------------------------- /mex_code/autotester/testsToSingle.m: -------------------------------------------------------------------------------- 1 | function tests = testsToSingle(tests) 2 | 3 | for i = 1:numel(tests) 4 | if (strcmpi(tests{i}.model.type,'merp') || strcmpi(tests{i}.model.type,'merpsparse')) 5 | tests{i}.model.threshold = single(tests{i}.model.threshold); 6 | tests{i}.model.connections = single(tests{i}.model.connections); 7 | fprintf('changing test %d\n',i); 8 | end 9 | 10 | end 11 | 12 | 13 | end -------------------------------------------------------------------------------- /mex_code/build_mac.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Builds the maximum entropy toolbox for MATLAB on MacOS hosts 3 | # Usage: 4 | # 5 | # "source build_mac.sh" - builds with the mex code 6 | 7 | 8 | source /opt/intel/compilers_and_libraries/mac/bin/compilervars.sh intel64 9 | 10 | 11 | # The following options file creates two execution branches, one with AVX2 and one with SSE4.2 for older processors 12 | export OPTIONSFILE="mex_C++_maci64.xml" 13 | 14 | 15 | sh ./buildmex.sh 16 | 17 | -------------------------------------------------------------------------------- /mex_code/build_unix.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Builds the maximum entropy toolbox for MATLAB on unix hosts 3 | # Usage: 4 | # 5 | # "source build_unix.sh" - builds the MEX functions 6 | 7 | source compilervars.sh intel64 8 | 9 | # The following options file uses two execution paths, one with AVX2 for newer CPUs and one with SSE4.2 for older CPUs 10 | export OPTIONSFILE="mex_g++_glnxa64.xml" 11 | 12 | sh ./buildmex.sh 13 | #sh ./testbuild.sh 14 | -------------------------------------------------------------------------------- /mex_code/buildmex.sh: -------------------------------------------------------------------------------- 1 | 2 | DIRECTIVES="CXX=icpc LD=icpc" 3 | SUPPORT_FILES=$(find *.cpp -not -name 'mex*.cpp') 4 | INCLUDES="-I$CPATH" 5 | LIBRARIES="" 6 | FLAGS="-v -f $OPTIONSFILE" 7 | 8 | echo Compiling gibbsSampler 9 | mex mexGibbsSampler.cpp $DIRECTIVES $SUPPORT_FILES $LIBRARIES $INCLUDES $FLAGS 10 | echo Compiling mexLogProbability 11 | mex mexLogProbability.cpp $DIRECTIVES $SUPPORT_FILES $INCLUDES $LIBRARIES $FLAGS 12 | echo Compiling mexGetMarginals 13 | mex mexEmpiricalMarginals.cpp $DIRECTIVES $SUPPORT_FILES $INCLUDES $LIBRARIES $FLAGS 14 | echo Compiling mexGetExhaustiveMarginals 15 | mex mexExhaustiveMarginals.cpp $DIRECTIVES $SUPPORT_FILES $INCLUDES $LIBRARIES $FLAGS 16 | echo Compiling wangLandauSampler 17 | mex mexWangLandauSampler.cpp $DIRECTIVES $SUPPORT_FILES $INCLUDES $LIBRARIES $FLAGS 18 | 19 | -------------------------------------------------------------------------------- /mex_code/common.h: -------------------------------------------------------------------------------- 1 | // Maximum entropy toolbox 2 | // This is definitions to help compile with or without the Intel compiler and MKL/IPP libraries 3 | // Ori Maoz, August 2016 4 | 5 | 6 | #pragma once 7 | #include 8 | 9 | 10 | 11 | 12 | 13 | 14 | #ifdef _MSC_VER 15 | #define DECLARE_ALIGNED _declspec(align(64)) 16 | #define ALIGN_AFTER 17 | #else 18 | #define DECLARE_ALIGNED 19 | #define ALIGN_AFTER __attribute__((aligned(64))) 20 | #endif 21 | 22 | 23 | // comment these out if you don't want MKL and IPP 24 | #include 25 | 26 | 27 | #if defined(_MKL_H_) 28 | 29 | #define malloc_aligned(X) mkl_malloc(X,64) 30 | #define free_aligned mkl_free 31 | 32 | 33 | #else 34 | // not using the intel compiler 35 | 36 | #define malloc_aligned malloc 37 | #define free_aligned free 38 | #define MKL_INT int 39 | 40 | 41 | #define DECLARE_ALIGNED 42 | 43 | 44 | inline void ippsAdd_64f_I(double * src, double * dst, int len) 45 | { 46 | for (int j = 0; j < len; j++) 47 | { 48 | dst[j] += src[j]; 49 | } 50 | } 51 | 52 | 53 | inline void ippsSub_64f_I(double * src, double * dst, int len) 54 | { 55 | for (int j = 0; j < len; j++) 56 | { 57 | dst[j] -= src[j]; 58 | } 59 | } 60 | 61 | inline void ippsDotProd_64f(double *a, double *b, int len, double * sum) 62 | { 63 | for (int i = 0; i < len; i++) 64 | { 65 | *sum += a[i] * b[i]; 66 | } 67 | } 68 | 69 | inline void vdAdd(int n, double *a, double *b, double *y) 70 | { 71 | for (uint32_t j = 0; j < n; j++) 72 | { 73 | y[j] = a[j] + b[j]; 74 | } 75 | 76 | } 77 | 78 | 79 | inline void vdSub(int n, double *a, double *b, double *y) 80 | { 81 | for (uint32_t j = 0; j < n; j++) 82 | { 83 | y[j] = a[j] - b[j]; 84 | } 85 | } 86 | 87 | 88 | inline void cblas_dgthr(MKL_INT nz, double * y, double * x, MKL_INT * idx) 89 | { 90 | for (uint32_t j = 0; j < nz; j++) 91 | { 92 | x[j] = y[idx[j]]; 93 | } 94 | 95 | } 96 | 97 | inline void cblas_daxpyi(MKL_INT nz, double a, double * x, MKL_INT *idx, double * y) 98 | { 99 | for (uint32_t j = 0; j < nz; j++) 100 | { 101 | y[j] += a*x[idx[j]]; 102 | } 103 | } 104 | 105 | #endif // NO_INTEL_COMPILER -------------------------------------------------------------------------------- /mex_code/matlab_utils.cpp: -------------------------------------------------------------------------------- 1 | // MATLAB-specific utility functions for the maxent toolbox 2 | // Ori Maoz, August 2016 3 | 4 | #include "matlab_utils.h" 5 | 6 | 7 | // allocates a new buffer and copies the data elementwise into it while converting the data 8 | uint32_t * reallocate_uint32(void * buffer_in, mxClassID sampleClassid, uint32_t nElements) 9 | { 10 | uint32_t * buffer_out; 11 | 12 | // If the input is a different type, we will need to convert it to 32-bit integers 13 | if ((sampleClassid == mxUINT32_CLASS) || (sampleClassid == mxINT32_CLASS)) 14 | { 15 | buffer_out = (uint32_t *)buffer_in; 16 | } 17 | else if ((sampleClassid == mxCHAR_CLASS) || (sampleClassid == mxLOGICAL_CLASS) || (sampleClassid == mxUINT8_CLASS) || (sampleClassid == mxINT8_CLASS)) 18 | { 19 | // buffer_out a new array 20 | buffer_out = new uint32_t[nElements]; 21 | 22 | // copy the data to the new array while converting to the new datatype 23 | char * p_old = (char*)buffer_in; 24 | for (uint32_t i = 0; i < nElements; i++) 25 | { 26 | buffer_out[i] = p_old[i] != 0; 27 | } 28 | } 29 | else if (sampleClassid == mxDOUBLE_CLASS) 30 | { 31 | // allocate a new array 32 | buffer_out = new uint32_t[nElements]; 33 | 34 | // copy the data to the new array while converting to the new datatype 35 | double * p_old = (double*)buffer_in; 36 | for (uint32_t i = 0; i < nElements; i++) 37 | { 38 | buffer_out[i] = p_old[i] != 0; 39 | } 40 | } 41 | else 42 | { 43 | mexErrMsgIdAndTxt("maxent_toolbox:init", 44 | "unsupported type"); 45 | } 46 | 47 | return buffer_out; 48 | } -------------------------------------------------------------------------------- /mex_code/matlab_utils.h: -------------------------------------------------------------------------------- 1 | // MATLAB-specific utility functions for the maxent toolbox 2 | // Ori Maoz, August 2016 3 | 4 | 5 | #include 6 | #include "mex.h" 7 | 8 | // allocates a new buffer and copies the data elementwise into it while converting the data 9 | uint32_t * reallocate_uint32(void * buffer_in, mxClassID sampleClassid, uint32_t nElements); -------------------------------------------------------------------------------- /mex_code/maxentToolbox_windows/maxentToolbox_windows.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mexGibbsSampler", "mexGibbsSampler\mexGibbsSampler.vcxproj", "{B4C3CB84-7468-4392-8166-332A46552DB7}" 7 | EndProject 8 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mexEmpiricalMarginals", "mexEmpiricalMarginals\mexEmpiricalMarginals.vcxproj", "{569535D6-BEA9-4933-AE3E-2874C8BC0D32}" 9 | EndProject 10 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mexLogProbability", "mexLogProbability\mexLogProbability.vcxproj", "{C632DE53-252C-42AB-90B3-65BE026EA24A}" 11 | EndProject 12 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mexExhaustiveMarginals", "mexExhaustiveMarginals\mexExhaustiveMarginals.vcxproj", "{5C801189-1D64-4145-931F-94E7923501E8}" 13 | EndProject 14 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mexWangLandauSampler", "mexWangLandauSampler\mexWangLandauSampler.vcxproj", "{21E22563-CC93-4827-BFD6-197500A9446E}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|x64 = Debug|x64 19 | Debug|x86 = Debug|x86 20 | Release|x64 = Release|x64 21 | Release|x86 = Release|x86 22 | EndGlobalSection 23 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 24 | {B4C3CB84-7468-4392-8166-332A46552DB7}.Debug|x64.ActiveCfg = Debug|x64 25 | {B4C3CB84-7468-4392-8166-332A46552DB7}.Debug|x64.Build.0 = Debug|x64 26 | {B4C3CB84-7468-4392-8166-332A46552DB7}.Debug|x86.ActiveCfg = Debug|Win32 27 | {B4C3CB84-7468-4392-8166-332A46552DB7}.Debug|x86.Build.0 = Debug|Win32 28 | {B4C3CB84-7468-4392-8166-332A46552DB7}.Release|x64.ActiveCfg = Release|x64 29 | {B4C3CB84-7468-4392-8166-332A46552DB7}.Release|x64.Build.0 = Release|x64 30 | {B4C3CB84-7468-4392-8166-332A46552DB7}.Release|x86.ActiveCfg = Release|Win32 31 | {B4C3CB84-7468-4392-8166-332A46552DB7}.Release|x86.Build.0 = Release|Win32 32 | {569535D6-BEA9-4933-AE3E-2874C8BC0D32}.Debug|x64.ActiveCfg = Debug|x64 33 | {569535D6-BEA9-4933-AE3E-2874C8BC0D32}.Debug|x64.Build.0 = Debug|x64 34 | {569535D6-BEA9-4933-AE3E-2874C8BC0D32}.Debug|x86.ActiveCfg = Debug|Win32 35 | {569535D6-BEA9-4933-AE3E-2874C8BC0D32}.Debug|x86.Build.0 = Debug|Win32 36 | {569535D6-BEA9-4933-AE3E-2874C8BC0D32}.Release|x64.ActiveCfg = Release|x64 37 | {569535D6-BEA9-4933-AE3E-2874C8BC0D32}.Release|x64.Build.0 = Release|x64 38 | {569535D6-BEA9-4933-AE3E-2874C8BC0D32}.Release|x86.ActiveCfg = Release|Win32 39 | {569535D6-BEA9-4933-AE3E-2874C8BC0D32}.Release|x86.Build.0 = Release|Win32 40 | {C632DE53-252C-42AB-90B3-65BE026EA24A}.Debug|x64.ActiveCfg = Debug|x64 41 | {C632DE53-252C-42AB-90B3-65BE026EA24A}.Debug|x64.Build.0 = Debug|x64 42 | {C632DE53-252C-42AB-90B3-65BE026EA24A}.Debug|x86.ActiveCfg = Debug|Win32 43 | {C632DE53-252C-42AB-90B3-65BE026EA24A}.Debug|x86.Build.0 = Debug|Win32 44 | {C632DE53-252C-42AB-90B3-65BE026EA24A}.Release|x64.ActiveCfg = Release|x64 45 | {C632DE53-252C-42AB-90B3-65BE026EA24A}.Release|x64.Build.0 = Release|x64 46 | {C632DE53-252C-42AB-90B3-65BE026EA24A}.Release|x86.ActiveCfg = Release|Win32 47 | {C632DE53-252C-42AB-90B3-65BE026EA24A}.Release|x86.Build.0 = Release|Win32 48 | {5C801189-1D64-4145-931F-94E7923501E8}.Debug|x64.ActiveCfg = Debug|x64 49 | {5C801189-1D64-4145-931F-94E7923501E8}.Debug|x64.Build.0 = Debug|x64 50 | {5C801189-1D64-4145-931F-94E7923501E8}.Debug|x86.ActiveCfg = Debug|Win32 51 | {5C801189-1D64-4145-931F-94E7923501E8}.Debug|x86.Build.0 = Debug|Win32 52 | {5C801189-1D64-4145-931F-94E7923501E8}.Release|x64.ActiveCfg = Release|x64 53 | {5C801189-1D64-4145-931F-94E7923501E8}.Release|x64.Build.0 = Release|x64 54 | {5C801189-1D64-4145-931F-94E7923501E8}.Release|x86.ActiveCfg = Release|Win32 55 | {5C801189-1D64-4145-931F-94E7923501E8}.Release|x86.Build.0 = Release|Win32 56 | {21E22563-CC93-4827-BFD6-197500A9446E}.Debug|x64.ActiveCfg = Debug|x64 57 | {21E22563-CC93-4827-BFD6-197500A9446E}.Debug|x64.Build.0 = Debug|x64 58 | {21E22563-CC93-4827-BFD6-197500A9446E}.Debug|x86.ActiveCfg = Debug|Win32 59 | {21E22563-CC93-4827-BFD6-197500A9446E}.Debug|x86.Build.0 = Debug|Win32 60 | {21E22563-CC93-4827-BFD6-197500A9446E}.Release|x64.ActiveCfg = Release|x64 61 | {21E22563-CC93-4827-BFD6-197500A9446E}.Release|x64.Build.0 = Release|x64 62 | {21E22563-CC93-4827-BFD6-197500A9446E}.Release|x86.ActiveCfg = Release|Win32 63 | {21E22563-CC93-4827-BFD6-197500A9446E}.Release|x86.Build.0 = Release|Win32 64 | EndGlobalSection 65 | GlobalSection(SolutionProperties) = preSolution 66 | HideSolutionNode = FALSE 67 | EndGlobalSection 68 | EndGlobal 69 | -------------------------------------------------------------------------------- /mex_code/maxentToolbox_windows/mexEmpiricalMarginals/Source.def: -------------------------------------------------------------------------------- 1 | LIBRARY mexEmpiricalMarginals 2 | EXPORTS mexFunction 3 | -------------------------------------------------------------------------------- /mex_code/maxentToolbox_windows/mexEmpiricalMarginals/mexEmpiricalMarginals.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | Source Files 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | Source Files 37 | 38 | 39 | Source Files 40 | 41 | 42 | Source Files 43 | 44 | 45 | Source Files 46 | 47 | 48 | Source Files 49 | 50 | 51 | Source Files 52 | 53 | 54 | Source Files 55 | 56 | 57 | Source Files 58 | 59 | 60 | Source Files 61 | 62 | 63 | -------------------------------------------------------------------------------- /mex_code/maxentToolbox_windows/mexExhaustiveMarginals/Source.def: -------------------------------------------------------------------------------- 1 | LIBRARY mexExhaustiveMarginals 2 | EXPORTS mexFunction 3 | -------------------------------------------------------------------------------- /mex_code/maxentToolbox_windows/mexExhaustiveMarginals/mexExhaustiveMarginals.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | Source Files 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | Source Files 37 | 38 | 39 | Source Files 40 | 41 | 42 | Source Files 43 | 44 | 45 | Source Files 46 | 47 | 48 | Source Files 49 | 50 | 51 | Source Files 52 | 53 | 54 | Source Files 55 | 56 | 57 | Source Files 58 | 59 | 60 | -------------------------------------------------------------------------------- /mex_code/maxentToolbox_windows/mexGibbsSampler/Source.def: -------------------------------------------------------------------------------- 1 | LIBRARY mexGibbsSampler 2 | EXPORTS mexFunction 3 | -------------------------------------------------------------------------------- /mex_code/maxentToolbox_windows/mexGibbsSampler/mexGibbsSampler.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | Source Files 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | Source Files 37 | 38 | 39 | Source Files 40 | 41 | 42 | Source Files 43 | 44 | 45 | Source Files 46 | 47 | 48 | Source Files 49 | 50 | 51 | Source Files 52 | 53 | 54 | Source Files 55 | 56 | 57 | Source Files 58 | 59 | 60 | Source Files 61 | 62 | 63 | -------------------------------------------------------------------------------- /mex_code/maxentToolbox_windows/mexLogProbability/Source.def: -------------------------------------------------------------------------------- 1 | LIBRARY mexLogProbability 2 | EXPORTS mexFunction 3 | -------------------------------------------------------------------------------- /mex_code/maxentToolbox_windows/mexLogProbability/mexLogProbability.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | Source Files 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | Source Files 37 | 38 | 39 | Source Files 40 | 41 | 42 | Source Files 43 | 44 | 45 | Source Files 46 | 47 | 48 | Source Files 49 | 50 | 51 | Source Files 52 | 53 | 54 | Source Files 55 | 56 | 57 | Source Files 58 | 59 | 60 | Source Files 61 | 62 | 63 | -------------------------------------------------------------------------------- /mex_code/maxentToolbox_windows/mexWangLandauSampler/Source.def: -------------------------------------------------------------------------------- 1 | LIBRARY mexWangLandauSampler 2 | EXPORTS mexFunction 3 | -------------------------------------------------------------------------------- /mex_code/maxentToolbox_windows/mexWangLandauSampler/mexWangLandauSampler.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | Source Files 28 | 29 | 30 | Source Files 31 | 32 | 33 | Source Files 34 | 35 | 36 | Source Files 37 | 38 | 39 | Source Files 40 | 41 | 42 | Source Files 43 | 44 | 45 | Source Files 46 | 47 | 48 | Source Files 49 | 50 | 51 | Source Files 52 | 53 | 54 | Source Files 55 | 56 | 57 | Source Files 58 | 59 | 60 | -------------------------------------------------------------------------------- /mex_code/maxent_functions.h: -------------------------------------------------------------------------------- 1 | // Main functions for maximum entropy toolbox. 2 | // The platform-specific parts are abstracted from these functions so that they can work in more than one environment. 3 | // Ori Maoz, August 2016 4 | 5 | #include "EnergyFunction.h" 6 | 7 | // Returns the log probabilities of samples according to the model 8 | // Input: 9 | // pModel (in) - model to generate the samples from 10 | // npatterns (in) - number of patterns 11 | // patterns_in (in) - samples in UINT32 form (32 bit integer) 12 | // logprobs_out (out) - preallocated buffer in which the log probabilities are returned 13 | void getLogProbability(EnergyFunction * pModel, uint32_t npatterns, uint32_t * patterns_in, double * logprobs_out); 14 | 15 | 16 | // Returns the empirical marginals for a set of samples 17 | // Input: 18 | // pModel (in) - model to generate the samples from 19 | // npatterns (in) - number of patterns 20 | // patterns_in (in) - samples in UINT32 form (32 bit integer) 21 | // weights (in) - probabilites used to reweight the patterns or NULL to treat them as uniform 22 | // pMarginals (out) - preallocated buffer in which the marginals are returned 23 | void getEmpiricalMarginals(EnergyFunction * pModel, uint32_t npatterns, uint32_t * patterns_in, double * weights, double * pMarginals); 24 | 25 | 26 | // Performs a Metropolis-Hasting type MCMC random walk on the state space and returns samples. 27 | // For an n-bit word, n bit flips are performed between each returned sample. This can be changed with the "nSeparation" argument. 28 | // The bits are flipped in a sequential order unless specified otherwise by the global bSequentialBits argument. 29 | // 30 | // Input: 31 | // pModel (in) - model to generate the samples from 32 | // nsteps (in) - number of samples to generate 33 | // x0 (in/out) - initial state for the random walk 34 | // x0_out (out) - final state of the random walk. 35 | // pOutputSamples (out)- pointer to preallocated array that will contain the output samples, or NULL if we don't want to return samples (for burn-in) 36 | // nSeparation (in) - how many samples between every returned sample. Is used to generate less-correlated data. 37 | // bSequentialBits (in)- true if we want bits to be flipped in a sequential order, false for random order 38 | // indices_to_change - array of indices to be changed by the random walk. Default, if NULL, is equivalent to an array containing 0..(ncells-1) 39 | // which denotes that all the bits are to be changed. 40 | // num_indices_to_change - number of elements in the array indices_to_change. 41 | // 42 | // Returns: 43 | // Generated samples in array pointed by pOutputSamples 44 | void runGibbsSampler(EnergyFunction * pModel, uint32_t nsteps, uint32_t * x0, uint32_t * x0_out, uint32_t * pOutputSamples, uint32_t nSeparation, bool bSequentialBits, uint32_t * indices_to_change = NULL, uint32_t num_indices_to_change=0); 45 | 46 | // Returns the marginals of a model (exhaustively computed) 47 | // Input: 48 | // pModel (in) - model to generate the samples from 49 | // pMarginals (out - preallocated buffer in which the marginals are returned 50 | // 51 | // Returns: 52 | // The partition function of the model. 53 | double getMarginals(EnergyFunction * pModel, double * pMarginals); 54 | 55 | 56 | // Runs the Wang-Landau steps of random walk on the energy function 57 | // nsteps - number of steps 58 | // x - starting point (in/out - returns ending state) 59 | // pModel - model that we use to compute energy 60 | // bin_limits - bin limits for the energy function discretization 61 | // g - energy density function (discretized) 62 | // h - energy function histogram 63 | // separation - how many samples to skip in each MCMC operation 64 | void runWangLandauStep(uint32_t nsteps, uint32_t * x, EnergyFunction * pModel, uint32_t nbins, double bin_limits[], double g[], double h[], double update_size, uint32_t nSeparation); 65 | 66 | 67 | // Seeds the random number generator using the system time 68 | void seedRNG(); 69 | 70 | // Seeds the random number generator with a user-specified seed 71 | // seed - seed for the RNG 72 | void seedRNG(uint32_t seed); -------------------------------------------------------------------------------- /mex_code/mexEmpiricalMarginals.cpp: -------------------------------------------------------------------------------- 1 | // Computes the marginals of a set of samples according to the inputed model. 2 | // 3 | 4 | #pragma warning(disable:4996) 5 | 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "mex.h" 13 | #include "matlab_utils.h" 14 | #include "maxent_functions.h" 15 | 16 | #include "EnergyFunctionFactory.h" 17 | #include 18 | 19 | #include 20 | #include 21 | #include 22 | 23 | #define DEFAULT_SEPARATION 1 24 | 25 | //#define DEBUG_PRINTS 26 | 27 | void printVector(std::strstream & str, char* name, double vec[], size_t len); 28 | 29 | void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 30 | { 31 | EnergyFunction* pModel; 32 | 33 | if(nrhs<2) { 34 | mexErrMsgIdAndTxt("mexEmpiricalMarginals:init", 35 | "Usage: mexEmpiricalMarginals(x,model,[probabilities])"); 36 | } 37 | 38 | mkl_disable_fast_mm(); // make MKL use simple and safe memory management 39 | 40 | 41 | // get the set of inputs 42 | void * px = (uint32_t*)mxGetData(prhs[0]); 43 | size_t nsamples = mxGetN(prhs[0]); 44 | size_t ndims = mxGetM(prhs[0]); 45 | mxClassID sampleClassid = mxGetClassID(prhs[0]); 46 | 47 | 48 | // Process structure containing information about the model 49 | const mxArray * model_struct = prhs[1]; 50 | 51 | EnergyFunctionFactory factory; 52 | pModel = factory.createEnergyFunction(model_struct); 53 | 54 | 55 | if (pModel->getDim() != ndims) 56 | { 57 | mexErrMsgIdAndTxt("mexEmpiricalMarginals:init", 58 | "model dimension does not match sample dimension"); 59 | } 60 | 61 | 62 | // Check if the probabilities of patterns were inputed. 63 | // If they were not, treat them as all ones. 64 | bool bInputedProbabilities = false; 65 | double * pInputProbabilities = NULL; 66 | double uniform_prob = (double)1/nsamples; 67 | if(nrhs>=3) { 68 | bInputedProbabilities = true; 69 | 70 | const mxArray * mxProbabilities = prhs[2]; 71 | 72 | // Check the datatype - probabilities must be zero 73 | mxClassID probsClassid = mxGetClassID(mxProbabilities); 74 | if (probsClassid != mxDOUBLE_CLASS) 75 | { 76 | mexErrMsgIdAndTxt("mexEmpiricalMarginals:init", 77 | "pattern probabilities must be of type double"); 78 | } 79 | 80 | // Check that it is the same length as the number of samples. We will allow it to be 81 | // either a row or column vector (because it is transparent to us anyway) 82 | if (!(((mxGetN(mxProbabilities) == nsamples) && (mxGetM(mxProbabilities) == 1)) || 83 | ((mxGetM(mxProbabilities) == nsamples) && (mxGetN(mxProbabilities) == 1)))) 84 | { 85 | mexErrMsgIdAndTxt("mexEmpiricalMarginals:init", 86 | "number of probabilities not equal to number of samples"); 87 | } 88 | 89 | 90 | pInputProbabilities = (double*)mxGetData(mxProbabilities); 91 | } 92 | 93 | 94 | 95 | // debug prints of the arguments 96 | #ifdef DEBUG_PRINTS 97 | std::strstream strOut; 98 | strOut << "Number of samples: " << (uint32_t)nsamples << "\n"; 99 | strOut << "Number of dimensions: " << (uint32_t)ndims << "\n"; 100 | strOut.clear(); 101 | 102 | // print lambdas 103 | //printVector(strOut,"x",px,ndims); 104 | 105 | mexPrintf(strOut.str()); 106 | mexEvalString("drawnow;"); // force it to update the screen so we can see the text 107 | #endif 108 | 109 | 110 | // Allocate a result array 111 | uint32_t nFactors = pModel->getNumFactors(); 112 | mxArray * mxMarginals= mxCreateNumericMatrix(1,nFactors, mxDOUBLE_CLASS, mxREAL); 113 | double * pMarginals = (double*)mxGetData(mxMarginals); 114 | plhs[0] = mxMarginals; 115 | 116 | // make sure that the datatype is correct 117 | uint32_t * pInputArray = reallocate_uint32(px, sampleClassid, nsamples*ndims); 118 | 119 | // build a temp output array that is aligned to 64-bit boundaries 120 | double * sum_factors = (double*)mkl_malloc(sizeof(double)*nFactors, 64); 121 | 122 | // run the main function and get the marginals 123 | getEmpiricalMarginals(pModel, nsamples, pInputArray, pInputProbabilities, sum_factors); 124 | 125 | 126 | // copy the result to the output array and release the temp array 127 | memcpy(pMarginals, sum_factors, sizeof(double) * nFactors); 128 | mkl_free(sum_factors); 129 | 130 | // Delete the model that we had previously allocated 131 | delete pModel; 132 | 133 | // if we needed to translate the input datatype, delete that temporary buffer too 134 | if (pInputArray != px) 135 | { 136 | delete[] pInputArray; 137 | } 138 | 139 | } 140 | 141 | 142 | 143 | 144 | // Prints the beginning and end of a vector to the display 145 | void printVector(std::strstream & str, char* name, double vec[], size_t len) 146 | { 147 | 148 | str << name << ": [ "; 149 | for (size_t i=0; i < std::min(len,4); i++) 150 | { 151 | str << vec[i] << " "; 152 | } 153 | 154 | if (len>1) 155 | { 156 | str << "... " << vec[len-1] << " ]\n"; 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /mex_code/mexExhaustiveMarginals.cpp: -------------------------------------------------------------------------------- 1 | // Computes the marginals of a set of samples according to the inputed model. 2 | // 3 | 4 | #pragma warning(disable:4996) 5 | 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "mex.h" 13 | #include "mtrand.h" 14 | #include 15 | 16 | #include "IsingEnergy.h" 17 | #include "EnergyFunctionFactory.h" 18 | #include "maxent_functions.h" 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | #define MAX_STRLEN_MODELTYPE 20 25 | #define DEFAULT_SEPARATION 1 26 | 27 | //#define DEBUG_PRINTS 28 | 29 | void printVector(std::strstream & str, char* name, double vec[], size_t len); 30 | 31 | void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 32 | { 33 | EnergyFunction* pModel; 34 | double z; 35 | 36 | mkl_disable_fast_mm(); // make MKL use simple and safe memory management 37 | 38 | if(nrhs<1) { 39 | mexErrMsgIdAndTxt("mexExhaustiveMarginals:init", 40 | "Usage: mexExhaustiveMarginals(model)"); 41 | } 42 | 43 | 44 | // Process structure containing information about the model 45 | const mxArray * model_struct = prhs[0]; 46 | 47 | EnergyFunctionFactory factory; 48 | pModel = factory.createEnergyFunction(model_struct); 49 | 50 | 51 | 52 | // debug prints of the arguments 53 | #ifdef DEBUG_PRINTS 54 | std::strstream strOut; 55 | strOut << "Number of samples: " << (uint32_t)nsamples << "\n"; 56 | strOut << "Number of dimensions: " << (uint32_t)ndims << "\n"; 57 | strOut.clear(); 58 | 59 | // print lambdas 60 | //printVector(strOut,"x",px,ndims); 61 | 62 | mexPrintf(strOut.str()); 63 | mexEvalString("drawnow;"); // force it to update the screen so we can see the text 64 | #endif 65 | 66 | 67 | // Allocate a result array 68 | uint32_t nFactors = pModel->getNumFactors(); 69 | mxArray * mxMarginals= mxCreateNumericMatrix(1,nFactors, mxDOUBLE_CLASS, mxREAL); 70 | double * pMarginals = (double*)mxGetData(mxMarginals); 71 | plhs[0] = mxMarginals; 72 | 73 | z = getMarginals(pModel, pMarginals); 74 | 75 | // return the partition function 76 | plhs[1] = mxCreateDoubleScalar(z); 77 | 78 | // Delete the model that we had previously allocated 79 | delete pModel; 80 | 81 | mkl_free_buffers(); 82 | 83 | } 84 | 85 | 86 | // Prints the beginning and end of a vector to the display 87 | void printVector(std::strstream & str, char* name, double vec[], size_t len) 88 | { 89 | 90 | str << name << ": [ "; 91 | for (size_t i=0; i < std::min(len,4); i++) 92 | { 93 | str << vec[i] << " "; 94 | } 95 | 96 | if (len>1) 97 | { 98 | str << "... " << vec[len-1] << " ]\n"; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /mex_code/mexLogProbability.cpp: -------------------------------------------------------------------------------- 1 | // Computes the log probabilities of a set of samples on the inputed model. 2 | // Setting up the model may take a while so this is mainly effective when a set of samples 3 | // is inputed and not just a single sample 4 | // 5 | // Generic Wang-Landau sampler for estimating the energy density of a Boltzmann distribution. 6 | // Performs a certain amount of random-walk steps on a weighted energy function and returns 7 | // ending state, accumulated histogram and updated density function. 8 | // 9 | // This can work with any generic Boltzmann distribution which implements the proper (TBD) interfaces. 10 | // 11 | 12 | #pragma warning(disable:4996) 13 | 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include "mex.h" 21 | 22 | #include "maxent_functions.h" 23 | #include "matlab_utils.h" 24 | #include "EnergyFunctionFactory.h" 25 | #include "common.h" 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #define MAX_STRLEN_MODELTYPE 20 32 | #define DEFAULT_SEPARATION 1 33 | 34 | //#define DEBUG_PRINTS 35 | 36 | void printVector(std::strstream & str, char* name, double vec[], size_t len); 37 | 38 | void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 39 | { 40 | EnergyFunction* pModel; 41 | 42 | mkl_disable_fast_mm(); // make MKL use simple and safe memory management 43 | 44 | if(nrhs<2) { 45 | mexErrMsgIdAndTxt("mexLogProbability:init", 46 | "Usage: mexLogProbability(x,model)"); 47 | } 48 | 49 | 50 | // get the set of inputs 51 | uint32_t * px = (uint32_t*)mxGetData(prhs[0]); 52 | size_t nsamples = mxGetN(prhs[0]); 53 | size_t ndims = mxGetM(prhs[0]); 54 | mxClassID sampleClassid = mxGetClassID(prhs[0]); 55 | 56 | // Process structure containing information about the model 57 | const mxArray * model_struct = prhs[1]; 58 | 59 | EnergyFunctionFactory factory; 60 | pModel = factory.createEnergyFunction(model_struct); 61 | 62 | 63 | if (pModel->getDim() != ndims) { 64 | mexErrMsgIdAndTxt("mexLogProbability:init", 65 | "data differs in dimension from model"); 66 | } 67 | 68 | // debug prints of the arguments 69 | #ifdef DEBUG_PRINTS 70 | std::strstream strOut; 71 | strOut << "Number of samples: " << (uint32_t)nsamples << "\n"; 72 | strOut << "Number of dimensions: " << (uint32_t)ndims << "\n"; 73 | strOut.clear(); 74 | 75 | // print lambdas 76 | //printVector(strOut,"x",px,ndims); 77 | 78 | mexPrintf(strOut.str()); 79 | mexEvalString("drawnow;"); // force it to update the screen so we can see the text 80 | #endif 81 | 82 | // Allocate a result array 83 | mxArray * mxLogprobs= mxCreateNumericMatrix(1,nsamples, mxDOUBLE_CLASS, mxREAL); 84 | double * pLogprobs = (double*)mxGetData(mxLogprobs); 85 | plhs[0] = mxLogprobs; 86 | 87 | 88 | uint32_t * pInputArray = reallocate_uint32(px, sampleClassid, nsamples*ndims); 89 | 90 | 91 | getLogProbability(pModel, nsamples, pInputArray, pLogprobs); 92 | 93 | // Delete the model that we had previously allocated 94 | delete pModel; 95 | 96 | mkl_free_buffers(); 97 | 98 | if (pInputArray != px) 99 | { 100 | delete [] pInputArray; 101 | } 102 | 103 | } 104 | 105 | 106 | // Prints the beginning and end of a vector to the display 107 | void printVector(std::strstream & str, char* name, double vec[], size_t len) 108 | { 109 | 110 | str << name << ": [ "; 111 | for (size_t i=0; i < std::min(len,4); i++) 112 | { 113 | str << vec[i] << " "; 114 | } 115 | 116 | if (len>1) 117 | { 118 | str << "... " << vec[len-1] << " ]\n"; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /mex_code/mex_g++_glnxa64.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 |
    25 | 46 | 47 | 48 | 54 | 55 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /mex_code/mtrand.cpp: -------------------------------------------------------------------------------- 1 | // mtrand.cpp, see include file mtrand.h for information 2 | 3 | #include "mtrand.h" 4 | // non-inline function definitions and static member definitions cannot 5 | // reside in header file because of the risk of multiple declarations 6 | 7 | // initialization of static private members 8 | unsigned long MTRand_int32::state[n] = {0x0UL}; 9 | int MTRand_int32::p = 0; 10 | bool MTRand_int32::init = false; 11 | 12 | void MTRand_int32::gen_state() { // generate new state vector 13 | for (int i = 0; i < (n - m); ++i) 14 | state[i] = state[i + m] ^ twiddle(state[i], state[i + 1]); 15 | for (int i = n - m; i < (n - 1); ++i) 16 | state[i] = state[i + m - n] ^ twiddle(state[i], state[i + 1]); 17 | state[n - 1] = state[m - 1] ^ twiddle(state[n - 1], state[0]); 18 | p = 0; // reset position 19 | } 20 | 21 | void MTRand_int32::seed(unsigned long s) { // init by 32 bit seed 22 | state[0] = s & 0xFFFFFFFFUL; // for > 32 bit machines 23 | for (int i = 1; i < n; ++i) { 24 | state[i] = 1812433253UL * (state[i - 1] ^ (state[i - 1] >> 30)) + i; 25 | // see Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier 26 | // in the previous versions, MSBs of the seed affect only MSBs of the array state 27 | // 2002/01/09 modified by Makoto Matsumoto 28 | state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines 29 | } 30 | p = n; // force gen_state() to be called for next random number 31 | } 32 | 33 | void MTRand_int32::seed(const unsigned long* array, int size) { // init by array 34 | seed(19650218UL); 35 | int i = 1, j = 0; 36 | for (int k = ((n > size) ? n : size); k; --k) { 37 | state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1664525UL)) 38 | + array[j] + j; // non linear 39 | state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines 40 | ++j; j %= size; 41 | if ((++i) == n) { state[0] = state[n - 1]; i = 1; } 42 | } 43 | for (int k = n - 1; k; --k) { 44 | state[i] = (state[i] ^ ((state[i - 1] ^ (state[i - 1] >> 30)) * 1566083941UL)) - i; 45 | state[i] &= 0xFFFFFFFFUL; // for > 32 bit machines 46 | if ((++i) == n) { state[0] = state[n - 1]; i = 1; } 47 | } 48 | state[0] = 0x80000000UL; // MSB is 1; assuring non-zero initial array 49 | p = n; // force gen_state() to be called for next random number 50 | } 51 | --------------------------------------------------------------------------------