Class: CrossEntropy::MatrixProblem
- Inherits:
-
AbstractProblem
- Object
- AbstractProblem
- CrossEntropy::MatrixProblem
- Defined in:
- lib/cross_entropy/matrix_problem.rb
Overview
Assuming that the data are probabilities in an NArray (say dim 1 or dim 2 for now). Rows (NArray dimension 1) must sum to one. Columns (NArray dimension 0) represent the quantities to be optimized.
Caller should set seed with NArray.srand before calling.
Instance Attribute Summary
Attributes inherited from AbstractProblem
#elite_score, #max_iters, #min_score, #num_elite, #num_iters, #num_samples, #overall_min_score, #overall_min_score_sample, #params, #track_overall_min
Instance Method Summary collapse
-
#estimate_ml(elite) ⇒ NArray
Maximum likelihood estimate using only the given ‘elite’ solutions.
-
#generate_samples_directly ⇒ Object
Generate samples directly from the probabilities matrix AbstractProblem#params.
-
#initialize(params = nil) {|_self| ... } ⇒ MatrixProblem
constructor
A new instance of MatrixProblem.
-
#most_likely_solution(pr = params) ⇒ Narray
Find most likely solution so far based on given probabilities.
- #num_values ⇒ Object
- #num_variables ⇒ Object
Methods inherited from AbstractProblem
#for_stop_decision, #solve, #to_estimate, #to_generate_samples, #to_score_sample, #to_update
Constructor Details
#initialize(params = nil) {|_self| ... } ⇒ MatrixProblem
Returns a new instance of MatrixProblem.
14 15 16 17 18 19 20 21 |
# File 'lib/cross_entropy/matrix_problem.rb', line 14 def initialize(params = nil) super(params) to_generate_samples { generate_samples_directly } to_estimate { |elite| estimate_ml(elite) } yield(self) if block_given? end |
Instance Method Details
#estimate_ml(elite) ⇒ NArray
Maximum likelihood estimate using only the given ‘elite’ solutions.
This is often (but not always) the optimal estimate for the probabilities from the elite samples for problems of this form.
on the AbstractProblem#num_elite parameter, but is typically less than AbstractProblem#num_samples; elements are integer in [0, #num_values)
non-negative floats in [0,1] and sum to 1
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cross_entropy/matrix_problem.rb', line 55 def estimate_ml(elite) pr_est = NArray.float(num_values, num_variables) (0...num_variables).each do |i| elite_i = elite[true, i] (0...num_values).each do |j| pr_est[j, i] = elite_i.eq(j).count_true end end pr_est /= elite.shape[0] pr_est end |
#generate_samples_directly ⇒ Object
Generate samples directly from the probabilities matrix AbstractProblem#params.
If your problem is tightly constrained, you may want to provide a custom sample generation routine that avoids infeasible solutions; see AbstractProblem#to_generate_samples.
38 39 40 |
# File 'lib/cross_entropy/matrix_problem.rb', line 38 def generate_samples_directly params.tile(1, 1, num_samples).sample_pmf_dim.transpose(1, 0) end |
#most_likely_solution(pr = params) ⇒ Narray
Find most likely solution so far based on given probabilities.
#num_values columns; if not specified, the current AbstractProblem#params matrix is used
[0, #num_values)
77 78 79 80 81 82 83 84 |
# File 'lib/cross_entropy/matrix_problem.rb', line 77 def most_likely_solution(pr = params) pr_eq = pr.eq(pr.max(0).tile(1, pr.shape[0]).transpose(1, 0)) pr_ml = NArray.int(pr_eq.shape[1]) (0...pr_eq.shape[1]).each do |i| pr_ml[i] = pr_eq[true, i].where[0] end pr_ml end |
#num_values ⇒ Object
27 28 29 |
# File 'lib/cross_entropy/matrix_problem.rb', line 27 def num_values @params.shape[0] end |
#num_variables ⇒ Object
23 24 25 |
# File 'lib/cross_entropy/matrix_problem.rb', line 23 def num_variables @params.shape[1] end |