<commit_before>'''
Created on Mar 12, 2011

@author: johnsalvatier
'''
from scipy.optimize import fmin_bfgs, fmin_ncg
import numpy as np 

def find_MAP( model, chain_state, disp = False, retall = False):
    """
    moves the chain to the local maximum a posteriori point given a model
    """
    def logp(x):
        return nan_to_high(-(model.evaluate_as_vector(model.project(chain_state, x)))[0])
        
    def grad_logp(x):
        return np.nan_to_num(-(model.evaluate_as_vector(model.project(chain_state, x)))[1])

    x = fmin_bfgs(logp, model.subspace(chain_state), grad_logp, disp = disp, retall = retall, full_output = True)
    
    chain_state = model.project(chain_state, x[0])
    
    if retall:
        return chain_state, x[-1]
    else:
        return chain_state

    
    
def nan_to_high(x):
    x = x.copy() 
    if (x.ndim >= 1):
        x[np.logical_not(np.isfinite(x))] = 1.79769313e+308
        return x
    else: 
        if np.logical_not(np.isfinite(x)): 
            return 1.79769313e+308
        else:<commit_msg>added minimization function to parameters<commit_after>'''
Created on Mar 12, 2011

@author: johnsalvatier
'''
from scipy.optimize import fmin_bfgs, fmin_ncg
import numpy as np 

def find_MAP( model, chain_state, minalg = fmin_bfgs, disp = False, retall = False):
    """
    moves the chain to the local maximum a posteriori point given a model
    """
    def logp(x):
        return nan_to_high(-(model.evaluate_as_vector(model.project(chain_state, x)))[0])
        
    def grad_logp(x):
        return np.nan_to_num(-(model.evaluate_as_vector(model.project(chain_state, x)))[1])

    x = minalg(logp, model.subspace(chain_state), grad_logp, disp = disp, retall = retall, full_output = True)
    
    chain_state = model.project(chain_state, x[0])
    
    if retall:
        return chain_state, x[-1]
    else:
        return chain_state

    
    
def nan_to_high(x):
    x = x.copy() 
    if (x.ndim >= 1):
        x[np.logical_not(np.isfinite(x))] = 1.79769313e+308
        return x
    else: 
        if np.logical_not(np.isfinite(x)): 
            return 1.79769313e+200
        else:
{'fraction_non_alphanumeric': 0.11759216052263183, 'fraction_numerical': 0.032664489034064395, 'mean_word_length': 2.801418439716312, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4897408', 'n_tokens_mistral': 824, 'n_tokens_neox': 762, 'n_words': 177}