text:
#include <sml_ann.h>
#include <string.h>
#include "sml_ann_variable_list.h"
#include <macros.h>
#include <math.h>
#include <errno.h>
#include "sml_cache.h"
#include <sml_engine.h>
#include <sol-vector.h>
#include <sml_log.h>
#include <float.h>
#include <stdlib.h>
#include <stdio.h>
#include "sml_ann_bridge.h"
#include "sml_util.h"
#include <sys/stat.h>
#include <config.h>

#define DEFAULT_EPOCHS (300)
#define DEFAULT_CANDIDATE_GROUPS (6)
#define DEFAULT_DESIRED_ERROR (0.01)
#define MIN_TRESHOLD (0.5)
#define INITIAL_REQUIRED_OBSERVATIONS (2500)
#define DEFAULT_CACHE_SIZE (30)
#define ANN_FILE_PREFIX "ann_"
#define ANN_FILE_EXTENSION "net"
#define CI_FILE_PREFIX "ann_ci_"
#define CI_FILE_EXTENSION "ci"
#define ANN_PSEUDOREHEARSAL_PREFIX "pseudo_rehearsal_ann"
#define CFG_FILE_PREFIX "ann_cfg_"
#define CFG_FILE_EXTENSION "cfg"
#define ANN_MAGIC (0xa22)
#define EXPAND_FACTOR (3)

struct sml_ann_engine {
    struct sml_engine engine;

    struct sml_variables_list *inputs;
    struct sml_variables_list *outputs;
    bool first_run;
    bool use_pseudorehearsal;

    unsigned int required_observations;
    unsigned int train_epochs;
    float train_error;
    unsigned int max_neurons;
    unsigned int candidate_groups;

    enum sml_ann_training_algorithm train_algorithm;

    struct sol_ptr_vector pending_add;
    struct sol_ptr_vector pending_remove;
    struct sol_vector activation_functions;

    struct sml_cache *anns_cache;
};

//FIXME: Is this a good approuch?
static struct sml_variables_list *
_sml_ann_output_has_significant_changes(struct sml_variables_list *outputs)
{
    uint16_t i, size;
    struct sml_variable *var;
    float neural_value, read_value;
    struct sml_variables_list *changed = sml_ann_variable_list_new();

    if (!changed) {
        sml_critical("Could not alloc the output changed list !");
        return NULL;
    }

    size = sml_ann_variables_list_get_length(outputs);
    for (i = 0; i < size; i++) {
        var = sml_ann_variables_list_index(outputs, i);

        neural_value = sml_ann_variable_get_value(var);
        read_value = sml_ann_variable_get_previous_value(var);
        /* Neural value will never be NAN at this point! */
        if (isnan(read_value) ||
            fabs(read_value - neural_value) >= MIN_TRESHOLD) {
            if (sml_ann_variable_list_add_variable(changed, var)) {
                sml_ann_variable_list_free(changed, false);
                sml_critical("Could not add a variable to the changed list!");
                return NULL;
            }
        } else /* If no significant changes happened, set the old value. */
            sml_ann_variable_set_value(var, read_value);
    }
    sml_debug("Changed list size:%d",
        sml_ann_variables_list_get_length(changed));
    return changed;
}

//FIXME: Is this a good approuch?
static bool
_sml_ann_variable_list_has_significant_changes(
    struct sml_variables_list *inputs)
{

    uint16_t i, size;
    struct sml_variable *var;
    float last, stable;

    size = sml_ann_variables_list_get_length(inputs);
    for (i = 0; i < size; i++) {
        var = sml_ann_variables_list_index(inputs, i);
        last = sml_ann_variable_get_value(var);
        stable = sml_ann_variable_get_last_stable_value(var);

        if (!isnan(stable) && !isnan(last)) {
            if (fabs(last - stable) >= MIN_TRESHOLD)
                return true;
        } else if (!(isnan(stable) && isnan(last)))
            return true;
    }
    return false;
}

static unsigned int
_sml_ann_get_observations_length(struct sml_ann_engine *ann_engine)
{
    struct sml_variable *var;

    var = sml_ann_variables_list_index(ann_engine->inputs, 0);

    if (!var)
        return 0;
    return sml_ann_variable_get_observations_length(var);
}

static bool
_sml_ann_can_alloc_memory_for_observations(unsigned int total_variables,
    unsigned int observations_size,
    unsigned int max_memory_size)
{
    if (!max_memory_size)
        return true;

    unsigned int final_size = observations_size *
        sizeof(float)  * total_variables;
    return final_size <= max_memory_size;
}

static int
_sml_ann_train(struct sml_ann_engine *ann_engine, struct sml_ann_bridge *iann,
    unsigned int observations_size)
{
    int error;
    unsigned int required_observations_suggestion;
    bool retrain, can_realloc;
    uint16_t in_size, out_size, pending_size, i;
    struct sml_variable *var;

    error = sml_ann_bridge_train(iann, ann_engine->inputs, ann_engine->outputs,
        ann_engine->train_error,
        observations_size,
        ann_engine->max_neurons,
        &required_observations_suggestion,
        ann_engine->use_pseudorehearsal);
    if (error)
        return error;

    can_realloc = true;
    retrain = false;
    if (required_observations_suggestion > ann_engine->required_observations) {
        in_size = sml_ann_variables_list_get_length(ann_engine->inputs);
        out_size = sml_ann_variables_list_get_length(ann_engine->outputs);
        pending_size = sol_ptr_vector_get_len(&ann_engine->pending_add);
        if (!_sml_ann_can_alloc_memory_for_observations(
            in_size + out_size + pending_size,
            required_observations_suggestion,
            ann_engine->engine.obs_max_size)) {
            sml_warning("Can not alloc more memory for observations,"   \
                "obs_max_size has been reached."       \
                "Considering the network trained");
            can_realloc = false;
            error = sml_ann_bridge_consider_trained(iann, ann_engine->inputs,
                ann_engine->required_observations,
                ann_engine->use_pseudorehearsal);
        }
    } else if (required_observations_suggestion <
        ann_engine->required_observations)
        retrain = true;
    else
        can_realloc = false;

    if (!sml_ann_bridge_is_trained(iann)) {
        if (can_realloc) {
            ann_engine->required_observations =
                required_observations_suggestion;
            if ((error = sml_ann_variables_list_realloc_observations_array(
                    ann_engine->inputs, ann_engine->required_observations)))
                return error;
            if ((error = sml_ann_variables_list_realloc_observations_array(
                    ann_engine->outputs, ann_engine->required_observations)))
                return error;
            SOL_PTR_VECTOR_FOREACH_IDX (&ann_engine->pending_add, var, i) {
                if ((error = sml_ann_variable_realloc_observation_array(
                        var, ann_engine->required_observations)))
                    return error;
            }
        }
        if (retrain) {
            error = sml_ann_bridge_train(iann, ann_engine->inputs,
                ann_engine->outputs,
                ann_engine->train_error,
                ann_engine->required_observations,
                ann_engine->max_neurons,
                NULL,
                ann_engine->use_pseudorehearsal);
        }
    }
    return error;
}

static bool
_sml_ann_remove_variable_from_sml(struct sml_ann_engine *ann_engine,
    struct sml_variable *var_to_remove, bool input)
{
    struct sml_variable *var;
    struct sml_variables_list *list;
    uint16_t i, size;

    if (input)
        list = ann_engine->inputs;
    else
        list = ann_engine->outputs;

    size = sml_ann_variables_list_get_length(list);
    for (i = 0; i < size; i++) {
        var = sml_ann_variables_list_index(list, i);
        if (var == var_to_remove) {
            sml_ann_variable_list_remove(list, i);
            return true;
        }
    }
    return false;
}

static struct sml_variable *
_sml_ann_find_variable_by_name(struct sml_ann_engine *ann_engine,
    const char *name, bool input)
{
    struct sml_variables_list *list;
    struct sml_variable *var;
    char var_name[SML_VARIABLE_NAME_MAX_LEN + 1];
    uint16_t i, size;

    if (input)
        list = ann_engine->inputs;
    else
        list = ann_engine->outputs;

    size = sml_ann_variables_list_get_length(list);
    for (i = 0; i < size; i++) {
        var = sml_ann_variables_list_index(list, i);
        if (sml_ann_variable_get_name(var, var_name, sizeof(var_name))) {
            sml_warning("Failed to get name of var %p", var);
            continue;
        }
        if (!strcmp(name, var_name))
            return var;
    }
    return NULL;
}

static bool
_sml_ann_remove_variable_from_lists(struct sml_ann_engine *ann_engine,
    struct sml_variable *var)
{
    if (!_sml_ann_remove_variable_from_sml(ann_engine, var, true))
        return _sml_ann_remove_variable_from_sml(ann_engine, var, false);
    return true;
}

static struct sml_variable *
_sml_ann_add_variable(struct sml_ann_engine *ann_engine, const char *name,
    bool input)
{
    struct sml_variable *var;
    struct sml_variables_list *list;
    unsigned int total;

    if (input)
        list = ann_engine->inputs;
    else
        list = ann_engine->outputs;

    var = sml_ann_variable_new(name, input);

    if (!var)
        return NULL;

    total = sml_ann_variables_list_get_length(ann_engine->inputs) +
        sml_ann_variables_list_get_length(ann_engine->outputs) +
        sol_ptr_vector_get_len(&ann_engine->pending_add) + 1;
    if (!_sml_ann_can_alloc_memory_for_observations(total,
        ann_engine->required_observations,
        ann_engine->engine.obs_max_size) ||
        sml_ann_variable_realloc_observation_array(var,
        ann_engine->required_observations)) {
        sml_critical("Could not alloc the observation array!");
        goto err_exit;
    }
    sml_ann_variable_set_observations_array_index(var,
        _sml_ann_get_observations_length(ann_engine));

    if (!sml_cache_get_size(ann_engine->anns_cache) &&
        sml_ann_variable_list_add_variable(list, var)) {
        sml_critical("Could not add the variable to the list");
        goto err_exit;
    } else if (sml_cache_get_size(ann_engine->anns_cache) &&
        sol_ptr_vector_append(&ann_engine->pending_add, var)) {
        sml_critical("Could not add the variable to the add pending list");
        goto err_exit;
    }

    return var;
err_exit:
    sml_ann_variable_free(var);
    return NULL;
}

static struct sml_ann_bridge *
_sml_ann_new(struct sml_ann_engine *ann_engine, int *error_code)
{

    if (ann_engine->use_pseudorehearsal &&
        sml_cache_get_size(ann_engine->anns_cache) == 1) {
        sml_debug("Returning previous created ANN - pseudorehearsal");
        return sml_cache_get_element(ann_engine->anns_cache, 0);
    }

    sml_debug("Creating a new ANN!");
    struct sml_ann_bridge *iann = sml_ann_bridge_new(
        sml_ann_variables_list_get_length(ann_engine->inputs),
        sml_ann_variables_list_get_length(ann_engine->outputs),
        ann_engine->candidate_groups,
        ann_engine->train_epochs,
        ann_engine->train_algorithm,
        &ann_engine->activation_functions, error_code);

    if (!iann)
        return NULL;

    if (!sml_cache_put(ann_engine->anns_cache, iann)) {
        sml_ann_bridge_free(iann);
        if (error_code)
            *error_code = -ENOMEM;
        return NULL;
    }
    return iann;
}

static int
_sml_ann_change_ann_layout_if_needed(struct sml_ann_engine *ann_engine)
{
    struct sml_ann_bridge *iann;
    struct sml_variable *var;
    struct sml_variables_list *list;
    unsigned int inputs, outputs;
    uint16_t i;
    bool changed = false;
    int error;

    if (!sol_ptr_vector_get_len(&ann_engine->pending_add) &&
        !sol_ptr_vector_get_len(&ann_engine->pending_remove))
        return 0;

    inputs = sml_ann_variables_list_get_length(ann_engine->inputs);
    outputs = sml_ann_variables_list_get_length(ann_engine->outputs);

    /* Check the new number of inputs/outputs */
    SOL_PTR_VECTOR_FOREACH_IDX (&ann_engine->pending_add, var, i) {
        if (sml_ann_variable_is_input(var)) {
            inputs++;
            list = ann_engine->inputs;
            sml_debug("Adding input variable");
        } else {
            outputs++;
            list = ann_engine->outputs;
            sml_debug("Adding output variable");
        }
        changed = true;
        sml_ann_variable_list_add_variable(list, var);
    }

    SOL_PTR_VECTOR_FOREACH_IDX (&ann_engine->pending_remove, var, i) {
        if (sml_ann_variable_is_input(var)) {
            if (inputs > 0)
                inputs--;
            sml_debug("Removing input variable");
        } else {
            if (outputs > 0)
                outputs--;
            sml_debug("Removing output variable");
        }
        changed = true;
        _sml_ann_remove_variable_from_lists(ann_engine, var);
    }

    sol_ptr_vector_clear(&ann_engine->pending_remove);
    sol_ptr_vector_clear(&ann_engine->pending_add);

    if (changed) {
        sml_cache_clear(ann_engine->anns_cache);
        iann = _sml_ann_new(ann_engine, &error);
        if (!iann) {
            sml_critical("Could not create a new ANN");
            return error;
        }
        ann_engine->max_neurons = 0;
        if ((error = _sml_ann_train(ann_engine, iann,
                ann_engine->required_observations)))
            return error;
        sml_ann_variables_list_reset_observations(ann_engine->inputs, false);
        sml_ann_variables_list_reset_observations(ann_engine->outputs, false);
    }
    return 0;
}

static int
_sml_ann_alloc_arrays_if_needed(struct sml_ann_engine *ann_engine)
{
    unsigned int inputs, outputs;
    int error;

    /* Already allocated */
    if (!ann_engine->first_run)
        return 0;

    inputs = sml_ann_variables_list_get_length(ann_engine->inputs);
    outputs = sml_ann_variables_list_get_length(ann_engine->outputs);

    while (!_sml_ann_can_alloc_memory_for_observations(inputs + outputs,
        ann_engine->required_observations,
        ann_engine->engine.obs_max_size))
        ann_engine->required_observations /= 2;

    if (!ann_engine->required_observations) {
        sml_critical("Can not alloc %d bytes for observations",
            ann_engine->engine.obs_max_size);
        return -ENOMEM;
    }

    error = sml_ann_variables_list_realloc_observations_array(
        ann_engine->inputs, ann_engine->required_observations);
    if (error) {
        sml_critical("Could not alloc the input observation arrays");
        return error;
    }

    error = sml_ann_variables_list_realloc_observations_array(
        ann_engine->outputs, ann_engine->required_observations);
    if (error) {
        sml_critical("Could not alloc the output observation arrays");
        return error;
    }

    return 0;
}

static struct sml_ann_bridge *
_sml_ann_get_best_ann_for_latest_observations(struct sml_ann_engine *ann_engine)
{
    struct sol_ptr_vector *anns;
    struct sml_ann_bridge *iann, *best_ann;
    unsigned int i;
    float sum_iann, sum_best, distance, min;

    anns = sml_cache_get_elements(ann_engine->anns_cache);
    best_ann = NULL;
    min = FLT_MAX;
    sum_best = FLT_MAX;

    sml_debug("Selecting best ANN. Neural networks size: %d",
        sol_ptr_vector_get_len(anns));

    SOL_PTR_VECTOR_FOREACH_IDX (anns, iann, i) {
        sml_debug("Neural network:%d", i);
        if (!sml_ann_bridge_is_trained(iann)) {
            sml_debug("ANN is not trained, skip");
            continue;
        }
        distance = sml_ann_bridge_confidence_intervals_distance_sum(iann,
            ann_engine->inputs);
        sml_debug("ANN:%d distance:%f", i, distance);
        if (distance < min) {
            min = distance;
            best_ann = iann;
            sum_best = sml_ann_bridge_get_confidence_interval_sum(iann);
        } else if (distance == min) {
            sum_iann = sml_ann_bridge_get_confidence_interval_sum(iann);
            if (sum_iann < sum_best) {
                best_ann = iann;
                sum_best = sum_iann;
            }
        }
    }

    if (best_ann)
        sml_cache_hit(ann_engine->anns_cache, best_ann);
    return best_ann;
}

static void
_sml_ann_engine_free(struct sml_engine *engine)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;
    uint16_t i;
    struct sml_variable *var;

    sml_ann_variable_list_free(ann_engine->inputs, true);
    sml_ann_variable_list_free(ann_engine->outputs, true);

    sml_cache_free(ann_engine->anns_cache);
    sol_ptr_vector_clear(&ann_engine->pending_remove);

    SOL_PTR_VECTOR_FOREACH_IDX (&ann_engine->pending_add, var, i)
        sml_ann_variable_free(var);

    sol_ptr_vector_clear(&ann_engine->pending_add);
    sol_vector_clear(&ann_engine->activation_functions);
    free(ann_engine);
}

API_EXPORT bool
sml_ann_set_training_algorithm(struct sml_object *sml,
    enum sml_ann_training_algorithm algorithm)
{
    if (!sml_is_ann(sml))
        return false;
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)sml;;

    ann_engine->train_algorithm = algorithm;
    return true;
}

API_EXPORT bool
sml_ann_set_training_epochs(struct sml_object *sml,
    unsigned int training_epochs)
{
    if (!sml_is_ann(sml))
        return false;
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)sml;;

    ann_engine->train_epochs = training_epochs;
    return true;
}

API_EXPORT bool
sml_ann_set_desired_error(struct sml_object *sml, float desired_error)
{
    if (!sml_is_ann(sml))
        return false;
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)sml;;

    ann_engine->train_error = desired_error;
    return true;
}

static int
_sml_ann_pseudorehearsal_train(struct sml_ann_engine *ann_engine,
    struct sml_ann_bridge *iann)
{

    int r;
    unsigned int total_size, diff, i, j, len, old_size;
    struct sml_variable *var;

    len = sml_ann_variables_list_get_length(ann_engine->inputs);
    old_size = ann_engine->required_observations;
    total_size = ann_engine->required_observations * EXPAND_FACTOR;
    diff = total_size - ann_engine->required_observations;

    if (!sml_ann_bridge_is_trained(iann)) {
        sml_debug("ANN is not trained yet, training with the usual way");
        return _sml_ann_train(ann_engine, iann,
            ann_engine->required_observations);
    }

    if (sml_ann_bridge_get_error(iann, ann_engine->inputs, ann_engine->outputs,
        ann_engine->required_observations) <= ann_engine->train_error) {
        sml_debug("Not retraining the ANN. Error is good enought");
        return 0;
    }

    if ((r = sml_ann_variables_list_realloc_observations_array(
            ann_engine->inputs, total_size))) {
        sml_debug("Could not expand the input array");
        return r;
    }

    if ((r = sml_ann_variables_list_realloc_observations_array(
            ann_engine->outputs, total_size))) {
        sml_debug("Could not expand the output array");
        return r;
    }

    //Generate inputs
    for (i = 0; i < len; i++) {
        var = sml_ann_variables_list_index(ann_engine->inputs, i);
        sml_ann_variable_fill_with_random_values(var, diff);
    }

    //Predict the outputs
    for (i = 0, j = ann_engine->required_observations; i < diff; i++, j++) {
        sml_ann_bridge_predict_output_by_index(iann, ann_engine->inputs,
            ann_engine->outputs, j);
    }

    //Now train!
    if ((r = _sml_ann_train(ann_engine, iann, total_size))) {
        sml_debug("Could not retrain the ANN!");
        return r;
    }

    len = sml_ann_variables_list_get_length(ann_engine->inputs);
    for (i = 0; i < len; i++) {
        var = sml_ann_variables_list_index(ann_engine->inputs, i);
        sml_ann_variable_set_observations_array_index(var, old_size);
    }

    //reset values
    sml_ann_variables_list_realloc_observations_array(ann_engine->inputs,
        old_size);
    sml_ann_variables_list_realloc_observations_array(ann_engine->outputs,
        old_size);
    return 0;
}

static int
_sml_ann_store_observations(struct sml_ann_engine *ann_engine)
{
    struct sol_ptr_vector *anns;
    struct sml_ann_bridge *iann, *to_train;
    unsigned int hits, i, input_len;
    int r;
    bool use_common_pool;

    if (ann_engine->engine.learn_disabled) {
        sml_debug("Learn is disabled, not storing values");
        return 0;
    }

    use_common_pool = true;
    to_train = NULL;
    if (!ann_engine->use_pseudorehearsal) {
        anns = sml_cache_get_elements(ann_engine->anns_cache);
        input_len = sml_ann_variables_list_get_length(ann_engine->inputs);
        sml_debug("Total ANNS:%d", sol_ptr_vector_get_len(anns));
        SOL_PTR_VECTOR_FOREACH_IDX (anns, iann, i) {
            if (!sml_ann_bridge_is_trained(iann)) {
                sml_debug("ANN is not trained, skip");
                to_train = iann;
                continue;
            }
            hits = sml_ann_bridge_inputs_in_confidence_interval_hits(iann,
                ann_engine->inputs);
            if (hits == input_len) {
                use_common_pool = false;
                sml_ann_bridge_add_observation(iann, ann_engine->inputs,
                    ann_engine->outputs);
                sml_debug("Adding current observation to ANN:%d", i);
            }
        }
    }

    if (use_common_pool) {
        sml_debug("Storing observation in the common pool %d",
            _sml_ann_get_observations_length(ann_engine));
        sml_ann_variables_list_add_last_value_to_observation(
            ann_engine->inputs);
        sml_ann_variables_list_add_last_value_to_observation(
            ann_engine->outputs);

        if (_sml_ann_get_observations_length(ann_engine) ==
            ann_engine->required_observations) {
            if (!to_train) {
                iann = _sml_ann_new(ann_engine, &r);
                if (!iann) {
                    sml_critical("Could not create a new neural network");
                    return r;
                }
            } else {
                iann = to_train;
                sml_debug("Trying to train a previous created ANN.");
            }

            if (!ann_engine->use_pseudorehearsal) {
                if ((r = _sml_ann_train(ann_engine, iann,
                        ann_engine->required_observations))) {
                    sml_critical("Could not train the neural network");
                    sml_cache_remove(ann_engine->anns_cache, iann);
                    return r;
                }
            } else {
                if ((r = _sml_ann_pseudorehearsal_train(ann_engine, iann))) {
                    sml_critical("Could not train the neural network");
                    return r;
                }
            }

            if (sml_ann_bridge_is_trained(iann)) {
                sml_debug("ANN is trained, reseting variable observations.");
                sml_ann_variables_list_reset_observations(ann_engine->inputs, false);
                sml_ann_variables_list_reset_observations(ann_engine->outputs, false);
            }
        }
    }
    return 0;
}

static int
_sml_ann_process(struct sml_engine *engine)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;
    bool r, significant_changes, should_act;
    int error = 0;
    struct sml_ann_bridge *iann;
    struct sml_variables_list *changed;

    if ((error = _sml_ann_alloc_arrays_if_needed(ann_engine))) {
        sml_critical("Could not alloc observation arrays123! %d", error);
        return error;
    }

    /* Changes the ANN layout if variables were added/removed */
    if ((error = _sml_ann_change_ann_layout_if_needed(ann_engine))) {
        sml_critical("Could not change the ANN layout");
        return error;
    }

    if ((error = sml_call_read_state_cb(&ann_engine->engine)))
        return error;

    should_act = false;
    if (ann_engine->first_run) {
        sml_ann_variables_list_set_current_value_as_stable(
            ann_engine->inputs);
        sml_ann_variables_list_set_current_value_as_stable(
            ann_engine->outputs);
        ann_engine->first_run = false;
    } else {
        significant_changes =
            _sml_ann_variable_list_has_significant_changes(ann_engine->inputs);
        if (significant_changes)
            ann_engine->engine.output_state_changed_called = false;
        significant_changes |=
            _sml_ann_variable_list_has_significant_changes(ann_engine->outputs);
        if (significant_changes) {
            ann_engine->engine.hits = 0;
            sml_ann_variables_list_set_current_value_as_stable(
                ann_engine->inputs);
            sml_ann_variables_list_set_current_value_as_stable(
                ann_engine->outputs);
        }
    }

    if (ann_engine->engine.hits == ann_engine->engine.stabilization_hits) {
        ann_engine->engine.hits = 0;
        should_act = true;
        _sml_ann_store_observations(ann_engine);
        sml_debug("Reads are stabilized!");
    } else
        ann_engine->engine.hits++;

    if (ann_engine->engine.output_state_changed_cb &&
        should_act && !ann_engine->engine.output_state_changed_called) {

        if (!ann_engine->use_pseudorehearsal)
            iann = _sml_ann_get_best_ann_for_latest_observations(ann_engine);
        else
            iann = sml_cache_get_element(ann_engine->anns_cache, 0);

        if (iann && sml_ann_bridge_is_trained(iann)) {
            sml_debug("Trying to predict output");
            r = sml_ann_bridge_predict_output(iann, ann_engine->inputs,
                ann_engine->outputs);
            if (r) {
                changed = _sml_ann_output_has_significant_changes(
                    ann_engine->outputs);
                if (changed && sml_ann_variables_list_get_length(changed)) {
                    sml_call_output_state_changed_cb(&ann_engine->engine,
                        changed);
                    if (should_act)
                        ann_engine->engine.output_state_changed_called = true;
                } else
                    sml_debug("Not calling changed cb.");
                if (changed)
                    sml_ann_variable_list_free(changed, false);
            } else
                sml_critical("Could not predict the output");
        } else
            sml_critical("Could not select the best ann");
    }

    return error;
}

static bool
_sml_ann_predict(struct sml_engine *engine)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;
    struct sml_ann_bridge *iann;

    if (!ann_engine->use_pseudorehearsal)
        iann = _sml_ann_get_best_ann_for_latest_observations(ann_engine);
    else
        iann = sml_cache_get_element(ann_engine->anns_cache, 0);

    if (!iann || !sml_ann_bridge_is_trained(iann)) {
        sml_critical("Could not select the best ann");
        return false;
    }

    if (!sml_ann_bridge_predict_output(iann, ann_engine->inputs,
        ann_engine->outputs)) {
        sml_critical("Could not predict the output");
        return false;
    }

    return true;
}

static bool
_sml_ann_save(struct sml_engine *engine, const char *path)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;
    struct sml_ann_bridge *iann;
    char ann_path[SML_PATH_MAX], cfg_path[SML_PATH_MAX];
    struct sol_ptr_vector *anns;
    uint16_t i, ann_idx;
    bool exists;

    if (!sml_cache_get_size(ann_engine->anns_cache)) {
        sml_critical("Could not save the neural network." \
            "The neural network is NULL");
        return false;
    }

    exists = file_exists(path);
    if (exists && !is_dir(path)) {
        sml_critical("Failed to save sml: %s is not a directory\n", path);
        return false;
    } else if (!exists && !create_dir(path)) {
        sml_critical("Could not create the directory:%s", path);
        return false;
    }

    if (!clean_dir(path, ANN_FILE_PREFIX) ||
        !clean_dir(path, CFG_FILE_PREFIX)) {
        sml_critical("Failed to clear %s to save sml\n", path);
        return false;
    }

    if (ann_engine->use_pseudorehearsal) {
        snprintf(ann_path, sizeof(ann_path), "%s/%s.%s", path,
            ANN_PSEUDOREHEARSAL_PREFIX, ANN_FILE_EXTENSION);
        iann = sml_cache_get_element(ann_engine->anns_cache, 0);
        if (iann && sml_ann_bridge_is_trained(iann)) {
            if (!sml_ann_bridge_save_with_no_cfg(iann, ann_path)) {
                sml_critical("Could not save the ANN at:%s", ann_path);
                return false;
            }
        } else
            sml_debug("Not saving ANN. Not trained or does not exist yet.");
    } else {
        anns = sml_cache_get_elements(ann_engine->anns_cache);
        ann_idx = 0;
        SOL_PTR_VECTOR_FOREACH_IDX (anns, iann, i) {
            if (!sml_ann_bridge_is_trained(iann)) {
                sml_debug("Not saving untrained ANN.");
                continue;
            }
            snprintf(ann_path, sizeof(ann_path), "%s/%s%d.%s",
                path, ANN_FILE_PREFIX, ann_idx, ANN_FILE_EXTENSION);
            snprintf(cfg_path, sizeof(cfg_path), "%s/%s%d.%s",
                path, CFG_FILE_PREFIX, ann_idx, CFG_FILE_EXTENSION);
            ann_idx++;
            if (!sml_ann_bridge_save(iann, ann_path, cfg_path)) {
                sml_critical("Could not save the neural network at:%s", path);
                continue;
            }
        }
    }
    sml_debug("Neural network saved at:%s", path);
    return true;
}

API_EXPORT bool
sml_ann_set_activation_function_candidates(struct sml_object *sml,
    enum sml_ann_activation_function *functions,
    unsigned int size)
{
    if (!sml_is_ann(sml))
        return false;
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)sml;;
    unsigned int i;
    enum sml_ann_activation_function *func;

    for (i = 0; i < size; i++) {
        func = sol_vector_append(&ann_engine->activation_functions);
        if (!func) {
            sml_critical("Could append an activation function");
            continue;
        }
        *func = functions[i];
    }
    return true;
}

API_EXPORT bool
sml_ann_set_max_neurons(struct sml_object *sml, unsigned int max_neurons)
{
    if (!sml_is_ann(sml))
        return false;
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)sml;;

    ann_engine->max_neurons = max_neurons;
    return true;
}

API_EXPORT bool
sml_ann_set_candidate_groups(struct sml_object *sml,
    unsigned int candidate_groups)
{
    if (!sml_is_ann(sml))
        return false;
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)sml;;

    ann_engine->candidate_groups = candidate_groups;
    return true;
}

static bool
_sml_ann_load(struct sml_engine *engine, const char *path)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;
    struct sml_ann_bridge *iann;
    char ann_path[SML_PATH_MAX], cfg_path[SML_PATH_MAX];
    unsigned int i = 0;

    if (sml_cache_get_size(ann_engine->anns_cache)) {
        sml_cache_clear(ann_engine->anns_cache);
        sml_warning("Destroying a previously created neural network");
    }

    if (!is_dir(path)) {
        sml_critical("Failed to load sml in directory %s\n", path);
        return false;
    }

    if (ann_engine->use_pseudorehearsal) {
        snprintf(ann_path, sizeof(ann_path), "%s/%s.%s", path,
            ANN_PSEUDOREHEARSAL_PREFIX, ANN_FILE_EXTENSION);
        iann = sml_ann_bridge_load_from_file_with_no_cfg(ann_path);
        if (!iann) {
            sml_critical("Could not load the ann at:%s", ann_path);
            return false;
        }
        if (!sml_cache_put(ann_engine->anns_cache, iann)) {
            sml_critical(
                "Could not add the struct sml_ann_bridge to the cache");
            sml_ann_bridge_free(iann);
            return false;
        }
    } else {
        while (1) {
            snprintf(ann_path, sizeof(ann_path), "%s/%s%d.%s", path,
                ANN_FILE_PREFIX, i, ANN_FILE_EXTENSION);
            snprintf(cfg_path, sizeof(cfg_path), "%s/%s%d.%s",
                path, CFG_FILE_PREFIX, i, CFG_FILE_EXTENSION);
            if (!is_file(ann_path)) {
                sml_warning("The path:%s is not an ANN file", ann_path);
                break;
            } else if (!is_file(cfg_path)) {
                sml_warning("The path:%s is not a cfg file",
                    cfg_path);
                break;
            }

            iann = sml_ann_bridge_load_from_file(ann_path, cfg_path);
            if (!iann)
                break;
            if (!sml_cache_put(ann_engine->anns_cache, iann)) {
                sml_ann_bridge_free(iann);
                sml_cache_clear(ann_engine->anns_cache);
                return false;
            }
            i++;
        }
    }
    sml_debug("Neural network loaded");
    return true;
}

static struct sml_variable *
_sml_ann_new_input(struct sml_engine *engine, const char *name)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;

    return _sml_ann_add_variable(ann_engine, name, true);
}

static struct sml_variable *
_sml_ann_new_output(struct sml_engine *engine, const char *name)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;

    return _sml_ann_add_variable(ann_engine, name, false);
}

static bool
_sml_ann_remove_variable(struct sml_engine *engine, struct sml_variable *var)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;

    if (sml_cache_get_size(ann_engine->anns_cache)) {
        if (sol_ptr_vector_append(&ann_engine->pending_remove, var)) {
            sml_critical("Could not add the variable to the pending remove" \
                " list");
            return false;
        }
        return true;
    }
    return _sml_ann_remove_variable_from_lists(ann_engine, var);
}

static struct sml_variable *
_sml_ann_get_input(struct sml_engine *engine, const char *name)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;

    return _sml_ann_find_variable_by_name(ann_engine, name, true);
}

static struct sml_variable *
_sml_ann_get_output(struct sml_engine *engine, const char *name)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;

    return _sml_ann_find_variable_by_name(ann_engine, name, false);
}

static struct sml_variables_list *
_sml_ann_get_input_list(struct sml_engine *engine)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;

    return ann_engine->inputs;
}

static struct sml_variables_list *
_sml_ann_get_output_list(struct sml_engine *engine)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;

    return ann_engine->outputs;
}

static int
_sml_ann_variable_set_enabled(struct sml_engine *engine,
    struct sml_variable *var, bool enabled)
{
    return sml_ann_variable_set_enabled(var, enabled);
}

static void
_sml_ann_print_variables_list(struct sml_variables_list *list)
{
    struct sml_variable *var;
    char var_name[SML_VARIABLE_NAME_MAX_LEN + 1];
    uint16_t i, len;

    len = sml_ann_variables_list_get_length(list);
    for (i = 0; i < len; i++) {
        var = sml_ann_variables_list_index(list, i);
        if (sml_ann_variable_get_name(var, var_name, sizeof(var_name))) {
            sml_warning("Failed to get name of var %p", var);
            continue;
        }
        sml_debug("\t%s: %g", var_name, sml_ann_variable_get_value(var));
    }
}

static void
_sml_ann_print_debug(struct sml_engine *engine, bool full)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;
    struct sol_ptr_vector *elems;
    struct sml_ann_bridge *ann;
    uint16_t i;

    sml_debug("Inputs (%d) {",
        sml_ann_variables_list_get_length(ann_engine->inputs));
    _sml_ann_print_variables_list(ann_engine->inputs);
    sml_debug("}");
    sml_debug("Outputs (%d) {",
        sml_ann_variables_list_get_length(ann_engine->outputs));
    _sml_ann_print_variables_list(ann_engine->outputs);
    sml_debug("}");

    if (full) {
        sml_debug("Required observations: %d",
            ann_engine->required_observations);
        sml_debug("Train epochs: %d", ann_engine->train_epochs);
        sml_debug("Train error: %g", ann_engine->train_error);
        sml_debug("Max neurons: %d", ann_engine->max_neurons);
        sml_debug("Candidate groups: %d", ann_engine->candidate_groups);
        sml_debug("Observations max size: %d",
            ann_engine->engine.obs_max_size);

        sml_debug("ANNs (%d) {",
            sml_cache_get_size(ann_engine->anns_cache));
        elems = sml_cache_get_elements(ann_engine->anns_cache);
        SOL_PTR_VECTOR_FOREACH_IDX (elems, ann, i) {
            sml_debug("{");
            sml_ann_bridge_print_debug(ann);
            sml_debug("}");
        }
        sml_debug("}");
#ifdef Debug
        sml_debug("Total ANNs created: (%d)",
            sml_cache_get_total_elements_inserted(ann_engine->anns_cache));
#endif
    }
}

static void
_sml_ann_cache_element_free(void *element, void *data)
{
    sml_ann_bridge_free(element);
}

static bool
_sml_ann_erase_knowledge(struct sml_engine *engine)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)engine;

    sml_cache_clear(ann_engine->anns_cache);
    sml_ann_variables_list_reset_observations(ann_engine->inputs, true);
    sml_ann_variables_list_reset_observations(ann_engine->outputs, true);
    ann_engine->engine.hits = 0;
    ann_engine->first_run = true;
    return true;
}

API_EXPORT struct sml_object *
sml_ann_new(void)
{
    struct sml_ann_engine *ann_engine = calloc(1,
        sizeof(struct sml_ann_engine));

    if (!ann_engine) {
        sml_critical("Could not create a sml");
        return NULL;
    }
    ann_engine->inputs = sml_ann_variable_list_new();

    if (!ann_engine->inputs) {
        sml_critical("Could not create the input variable list");
        goto err_inputs;
    }
    ann_engine->outputs = sml_ann_variable_list_new();
    if (!ann_engine->outputs) {
        sml_critical("Could not create the output variable list");
        goto err_outputs;
    }

    ann_engine->anns_cache = sml_cache_new(DEFAULT_CACHE_SIZE,
        _sml_ann_cache_element_free, NULL);

    if (!ann_engine->anns_cache) {
        sml_critical("Could not create the ANN cache");
        goto err_cache;
    }

    ann_engine->train_algorithm = SML_ANN_TRAINING_ALGORITHM_QUICKPROP;
    ann_engine->train_epochs = DEFAULT_EPOCHS;
    ann_engine->train_error = DEFAULT_DESIRED_ERROR;
    ann_engine->candidate_groups = DEFAULT_CANDIDATE_GROUPS;
    ann_engine->required_observations = INITIAL_REQUIRED_OBSERVATIONS;
    ann_engine->first_run = true;
    ann_engine->use_pseudorehearsal = true;
    sol_vector_init(&ann_engine->activation_functions,
        sizeof(enum sml_ann_activation_function));
    sol_ptr_vector_init(&ann_engine->pending_remove);
    sol_ptr_vector_init(&ann_engine->pending_add);

    ann_engine->engine.free = _sml_ann_engine_free;
    ann_engine->engine.process = _sml_ann_process;
    ann_engine->engine.predict = _sml_ann_predict;
    ann_engine->engine.save = _sml_ann_save;
    ann_engine->engine.load = _sml_ann_load;
    ann_engine->engine.get_input_list = _sml_ann_get_input_list;
    ann_engine->engine.get_output_list = _sml_ann_get_output_list;
    ann_engine->engine.new_input = _sml_ann_new_input;
    ann_engine->engine.new_output = _sml_ann_new_output;
    ann_engine->engine.get_input = _sml_ann_get_input;
    ann_engine->engine.get_output = _sml_ann_get_output;
    ann_engine->engine.get_name = sml_ann_variable_get_name;
    ann_engine->engine.set_value = sml_ann_variable_set_value;
    ann_engine->engine.get_value = sml_ann_variable_get_value;
    ann_engine->engine.variable_set_enabled = _sml_ann_variable_set_enabled;
    ann_engine->engine.variable_is_enabled = sml_ann_variable_is_enabled;
    ann_engine->engine.remove_variable = _sml_ann_remove_variable;
    ann_engine->engine.variables_list_get_length =
        sml_ann_variables_list_get_length;
    ann_engine->engine.variables_list_index = sml_ann_variables_list_index;
    ann_engine->engine.variable_set_range = sml_ann_variable_set_range;
    ann_engine->engine.variable_get_range = sml_ann_variable_get_range;
    ann_engine->engine.print_debug = _sml_ann_print_debug;
    ann_engine->engine.erase_knowledge = _sml_ann_erase_knowledge;
    ann_engine->engine.magic_number = ANN_MAGIC;

    return (struct sml_object *)&ann_engine->engine;
err_cache:
    sml_ann_variable_list_free(ann_engine->outputs, true);
err_outputs:
    sml_ann_variable_list_free(ann_engine->inputs, true);
err_inputs:
    free(ann_engine);
    return NULL;
}

API_EXPORT bool
sml_is_ann(struct sml_object *sml)
{
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)sml;

    ON_NULL_RETURN_VAL(sml, false);
    return ann_engine->engine.magic_number == ANN_MAGIC;
}

API_EXPORT bool
sml_ann_supported(void)
{
    return true;
}

API_EXPORT bool
sml_ann_set_cache_max_size(struct sml_object *sml, unsigned int max_size)
{
    if (!sml_is_ann(sml))
        return false;
    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)sml;
    sml_debug("Setting ann cache to max size:%d", max_size);
    return sml_cache_set_max_size(ann_engine->anns_cache, max_size);
}

API_EXPORT bool
sml_ann_set_initial_required_observations(struct sml_object *sml,
    unsigned int required_observations)
{
    if (!sml_is_ann(sml))
        return false;

    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)sml;
    if (!ann_engine->first_run) {
        sml_warning("Initial required observation must be set before first call"
            " of sml_process");
        return false;
    }
    ann_engine->required_observations = required_observations;
    return true;
}

API_EXPORT bool
sml_ann_use_pseudorehearsal_strategy(struct sml_object *sml,
    bool use_pseudorehearsal)
{
    if (!sml_is_ann(sml))
        return false;

    struct sml_ann_engine *ann_engine = (struct sml_ann_engine *)sml;
    if (!ann_engine->first_run) {
        sml_warning("Pseudorehearsal strategy can only be set before the first"
            " call of sml_process");
        return false;
    }
    ann_engine->use_pseudorehearsal = use_pseudorehearsal;
    return true;
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.netease.cloud</groupId>
	<artifactId>robot-demo</artifactId>
	<version>1.0.0</version>
	<packaging>war</packaging>
	<name> robot demo Maven Webapp    </name>
	<profiles>
		<profile>
			<id>dev</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<build>
				<resources>
					<resource>
						<directory>src/main/resources/dev</directory>
					</resource>
					<resource>
						<directory>src/main/resources/</directory>
					</resource>
				</resources>
			</build>
		</profile>
		<profile>
			<id>prod</id>
			<build>
				<resources>
					<resource>
						<directory>src/main/resources/prod</directory>
					</resource>
					<resource>
						<directory>src/main/resources/</directory>
					</resource>
				</resources>
			</build>
		</profile>
	</profiles>
	<properties>
		<spring.version>3.2.2.RELEASE</spring.version>
		<spring.amqp.version>1.2.0.RELEASE</spring.amqp.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<repositories>
		<repository>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
			<id>central</id>
			<name>repo</name>
			<url>http://mvn.hz.netease.com/artifactory/repo</url>
		</repository>
		<repository>
			<id>maven</id>
			<name>Slowly office site</name>
			<url>http://repo1.maven.org/maven2</url>
		</repository>
	</repositories>
	<dependencies>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.2</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.2</version>
		</dependency>

		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>

		<!--JEE and Web related -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
		<!--spring framework -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>com.thoughtworks.xstream</groupId>
			<artifactId>xstream</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.4.3</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.2.0</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>2.2.0</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.2.0</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>robot-dt2</finalName>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<includes>
						<include>**/*Tests.java</include>
					</includes>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<path>/</path>
					<port>7070</port>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

redpajama_set_name: RedPajamaGithub


text:
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview ONC network configuration support class. Wraps a dictionary
 * object containing ONC managed or unmanaged dictionaries. Also provides
 * special accessors for ONC properties. Used by consumers of the
 * chrome.networkingPrivate API. See components/onc/docs/onc_spec.html.
 */
Polymer({
  is: 'cr-onc-data',

  properties: {
    /**
     * ONC configuration property dictionary, e.g. the result of a
     * chrome.networkingPrivate.getProperties() call.
     *
     * @type {?chrome.networkingPrivate.NetworkStateProperties}
     */
    data: {
      type: Object,
      value: function() { return {}; },
    },
  },

  /** @return {boolean} True if the network is connected. */
  connected: function() {
    return this.data.ConnectionState == CrOnc.ConnectionState.CONNECTED;
  },

  /** @return {boolean} True if the network is connecting. */
  connecting: function() {
    return this.data.ConnectionState == CrOnc.ConnectionState.CONNECTING;
  },

  /** @return {boolean} True if the network is disconnected. */
  disconnected: function() {
    return this.data.ConnectionState == CrOnc.ConnectionState.NOT_CONNECTED;
  },

  /** @return {number} The signal strength of the network. */
  getStrength: function() {
    var type = this.data.Type;
    var strength = 0;
    if (type == 'WiFi')
      strength = this.data.WiFi ? this.data.WiFi.SignalStrength : 0;
    else if (type == 'Cellular')
      strength = this.data.Cellular ? this.data.Cellular.SignalStrength : 0;
    else if (type == 'WiMAX')
      strength = this.data.WiMAX ? this.data.WiMAX.SignalStrength : 0;
    return strength;
  },

  /** @return {CrOnc.Security} The ONC security type. */
  getWiFiSecurity: function() {
    return (this.data.WiFi && this.data.WiFi.Security) ?
        this.data.WiFi.Security : CrOnc.Security.NONE;
  },

  /** @return {CrOnc.RoamingState} The ONC roaming state. */
  getCellularRoamingState: function() {
    return (this.data.Cellular && this.data.Cellular.RoamingState) ?
        this.data.Cellular.RoamingState : CrOnc.RoamingState.UNKNOWN;
  },

  /** @return {CrOnc.NetworkTechnology} The ONC network technology. */
  getCellularTechnology: function() {
    return (this.data.Cellular && this.data.Cellular.NetworkTechnology) ?
        this.data.Cellular.NetworkTechnology : CrOnc.NetworkTechnology.UNKNOWN;
  }
});

// Temporary constructor method. TODO(stevenjb): Replace with proper
// construction after switching to Polymer 0.8.

var CrOncDataElement = {};

/**
 * Helper method to create and return a typed cr-onc-data Polymer element.
 * Sets the data property of the element to |state|.
 *
 * @param {!chrome.networkingPrivate.NetworkStateProperties} state The network
 *     state properties.
 * @return {!CrOncDataElement} A cr-onc-data element.
 */
CrOncDataElement.create = function(state) {
  var oncData = /** @type {!CrOncDataElement} */ (
      document.createElement('cr-onc-data'));
  oncData.data = state;
  return oncData;
};


text:
using System;
using Xamarin.Forms;
using CouchbaseConnect2014.ViewModels;
using CouchbaseConnect2014.Controls;
using CouchbaseConnect2014.ValueConverters;

namespace CouchbaseConnect2014.Views
{
	public class FullScheduleView : ContentPage
	{
		public FullScheduleView ()
		{
            BaseViewModel.CreateAndBind<FullScheduleViewModel> (this);
            SetBinding (FullScheduleView.NavigationProperty, new Binding ("Navigation"));

            Title = "Full Schedule";
			BackgroundColor = Color.Default;
			NavigationPage.SetBackButtonTitle (this, "");

            this.ToolbarItems.Add (CreateFiltersItem ());
            SetBinding (ToolbarIconProperty, new Binding ("FilterIcon"));
            Content = CreateRootStack ();
		}

        public static BindableProperty ToolbarIconProperty =
            BindableProperty.Create<FullScheduleView, string> (
                b => b.ToolbarIcon,
                "filter_0.png",
                propertyChanged: (bindable, oldValue, newValue) => {
                    ((FullScheduleView)bindable).SetToolbarIcon (newValue);
                });

        public string ToolbarIcon {
            get {
                return (string)GetValue (ToolbarIconProperty);
            }
            set {
                SetValue (ToolbarIconProperty, value);
            }
        }

        void SetToolbarIcon (string newValue)
        {
            var toolbarItem = ToolbarItems [0];
            toolbarItem.Icon = newValue;
            ToolbarItems [0] = toolbarItem;
        }

        static ToolbarItem CreateFiltersItem ()
        {
            var filters = new ToolbarItem {
                Icon = "filter.png",
                Name = "Filter"
            };

            filters.SetBinding (ToolbarItem.CommandProperty, "ShowFilters");
            return filters;
        }

        View CreateRootStack ()
        {
            return new StackLayout {
				Spacing = 0,
                Children = {
                    CreateDayRow (),
                    CreatePips (),
					CreateSeparator(),
                    CreateSessonListView ()
                }
            };
        }

        View CreateDayRow ()
        {
            var carousel = new CarouselLayout {
                HeightRequest = 40,
                ItemTemplate = new DataTemplate (typeof(DayHeader))
            };
            carousel.SetBinding (CarouselLayout.ItemsSourceProperty, "FullScheduleDays");
            carousel.SetBinding (CarouselLayout.SelectedItemProperty, "SelectedDay");
            return carousel;
        }

        View CreatePips ()
        {
			var pips = new PipSet {
				TranslationY = -10
			};
            pips.SetBinding (PipSet.ItemsSourceProperty, "FullScheduleDays");
            pips.SetBinding (PipSet.SelectedItemProperty, "SelectedDay");
            return pips;
        }

		View CreateSeparator ()
		{
			var line = new BoxView
			{
				WidthRequest = 320,
				HeightRequest = 1.0,
				Color = Color.FromHex ("c8c7cc"),
				HorizontalOptions = LayoutOptions.CenterAndExpand
			};

			return line;
		}

        View CreateSessonListView ()
        {
            var listView = new ListView {
                IsGroupingEnabled = true,
                RowHeight = 100,
				HasUnevenRows = true,

                GroupDisplayBinding = new Binding ("StartTime",
                    converter: new DateTimeValueConverter ("h:mm tt")),
				GroupHeaderTemplate = new DataTemplate(typeof(FullScheduleHeaderCell)),

                ItemTemplate = new DataTemplate (typeof(FullScheduleCell))
            };

            listView.SetBinding (ListView.ItemsSourceProperty, "Slots");
            listView.SetBinding (ListView.SelectedItemProperty, "SelectedSession");

            return listView;
        }
	}
}


text:
<?php
namespace Concrete\Core\Application\UserInterface\Menu\Item;

interface ItemInterface
{

    /**
     * @param ControllerInterface $controller
     * @return void
     */
    public function setController(ControllerInterface $controller);

    /**
     * @return ControllerInterface
     */
    public function getController();

    /**
     * @return bool
     */
    public function getPosition();

}


text:
import Vue from 'vue'
import CModal from 'components/c-modal'
import tap from 'directives/tap'

Vue.directive('tap', tap)

describe('modal.vue', () => {
  let el
  let vm

  beforeEach(() => {
    el = document.createElement('div')
    document.body.appendChild(el)
  })

  afterEach(() => {
    // document.body.removeChild(el)
    vm.$destroy()
  })

  it('should render correct contents', done => {
    vm = new Vue({
      el,
      template: `<c-modal
        :show="show"
        :actions="actions">hello?</c-modal>`,
      data: {
        show: true,
        actions: {
          cancel: {
            label: 'Cancel',
            'class': 'primary'
          },
          submit: {
            label: 'Submit',
            'class': 'primary'
          }
        }
      },
      components: {
        CModal
      }
    })

    // only .content
    expect(vm.$children[0].$el.children.length).to.equal(1)
    expect(vm.$el.querySelector('.c-modal-body').textContent).to.equal('hello?')
    expect(vm.$el.querySelector('.c-modal-actions')).to.not.equal(null)

    // update actions
    vm.actions = null
    vm.$nextTick(() => {
      expect(vm.$el.querySelector('.c-modal-actions')).to.equal(null)
      done()
    })
  })

  it('should respond actions', done => {
    vm = new Vue({
      el,
      template: `<c-modal
        :show="show"
        @cancel="callback('cancel')"
        @submit="callback('submit')">hello?</c-modal>`,
      data: {
        show: true
      },
      methods: {
        callback (key) {
          if (key === 'cancel') {
            triggerMouseEvents(vm.$el.querySelectorAll('a.c-modal-link')[1], 'tap')
          } else {
            expect(vm.show).to.be.true
            vm.show = false
            vm.$nextTick(() => {
              expect(vm.show).to.be.false
              done()
            })
          }
        }
      },
      components: {
        CModal
      }
    })

    // button
    triggerMouseEvents(vm.$el.querySelectorAll('a.c-modal-link')[0], 'tap')
  })

  it('should show/hide modal', done => {
    vm = new Vue({
      el,
      template: '<c-modal :show="show">hello?</c-modal>',
      data: {
        show: false
      },
      components: {
        CModal
      }
    })

    const { style } = vm.$children[0].$el

    expect(style.display).to.equal('none')

    vm.show = true
    vm.$nextTick(() => {
      expect(style.display).to.equal('')
      done()
    })
  })

  it('should NOT prevent close', done => {
    vm = new Vue({
      el,
      template: `<c-modal
        :show="show"
        @cancel="callback">hello?</c-modal>`,
      data: {
        show: true
      },
      methods: {
        callback () {
          expect(vm.show).to.be.true
          vm.show = false
          vm.$nextTick(() => {
            expect(vm.show).to.be.false
            done()
          })
          return false
        }
      },
      components: {
        CModal
      }
    })

    const modal = vm.$children[0]

    // button
    triggerMouseEvents(modal.$el.querySelector('a:first-child'), 'tap')
  })
})


text:
module ApplicationHelper
  def escape_language(lang)
    url_encode(lang.downcase) if lang
  end

  def parameterize_language(lang)
    if lang
      lang.gsub(/\+/, 'p')
          .gsub(/\#/, 'sharp')
          .parameterize
    end
  end

  def language_link(language, label=nil)
    language = if language.respond_to? :map
      language.map &method(:escape_language)
    else
      escape_language language
    end
    label = label || [language].flatten.join(', ')
    link_to label, '#', data: {language: language}
  end

  def github_button(nickname)
    %().html_safe
  end

  def gratipay_button(nickname)
    %(Support via Gratipay).html_safe
  end

  def contributors_in year
    PullRequest.year(year).load.map(&:user_id).uniq.length
  end

  def pull_requests_in year
    PullRequest.year(year).count
  end

  def projects_in year
    PullRequest.year(year).select(:repo_name).map(&:repo_name).uniq.count
  end

  def current_path locale=nil
    path = request.env["REQUEST_PATH"]
    path += "?locale=#{locale}" if locale.present?
    path
  end

  def available_locales
    [ 'en', 'es', 'el', 'pt_br', 'fi', 'fr', 'de', 'ru', 'uk', 'th', 'it', 'nb', 'ta', 'tr', 'zh_Hans', 'zh_Hant' ]
  end

  def current_translations
    @translations ||= I18n.backend.send(:translations)
    @translations[I18n.locale].with_indifferent_access
  end

  def contributing_url type='html'
    url = 'http://24pullrequests.com/contributing'
    return url if type == 'text'
    link_to url, url
  end

  def twitter_url type='html'
    url = 'http://twitter.com/24pullrequests'
    return url if type == 'text'
    link_to url, url
  end

  def preferences_url type='html'
    url = 'http://24pullrequests.com/preferences'
    return url if type == 'text'
    link_to url, url
  end

  def favicon
    if (Time.parse("1st December")..Time.parse("25th December")
    ).cover?(Time.zone.now)
      return { filename: "/assets/favicon/#{Time.zone.now.strftime("%d")}.png",
      type: "image/png" }
    else
      return { filename: "favicon.ico", type: "image/vnd.microsoft.icon" }
    end
  end
end


text:
// Copyright (C) 2022 Fievus
//
// This software may be modified and distributed under the terms
// of the MIT license.  See the LICENSE file for details.
namespace Carna.Runner;

/// <summary>
/// Represents errors that occur when the type of the sample data source is invalid.
/// </summary>
public class InvalidSampleDataSourceTypeException : Exception
{
    /// <summary>
    /// Gets the invalid type of the sample data source.
    /// </summary>
    public Type SampleDataSourceType { get; }

    /// <summary>
    /// Initializes a new instance of the <see cref="InvalidSampleDataSourceTypeException"/> class
    /// with the specified invalid type of the sample data source.
    /// </summary>
    /// <param name="sampleDataSourceType">The invalid type of the sample data source.</param>
    public InvalidSampleDataSourceTypeException(Type sampleDataSourceType) : this(sampleDataSourceType, $"{sampleDataSourceType} is invalid type of the sample data source")
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="InvalidSampleDataSourceTypeException"/> class
    /// with the specified invalid type of the sample data source and error message.
    /// </summary>
    /// <param name="sampleDataSourceType">The invalid type of the sample data source.</param>
    /// <param name="message">The message that describes the error.</param>
    public InvalidSampleDataSourceTypeException(Type sampleDataSourceType, string message) : this(sampleDataSourceType, message, null)
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="InvalidSampleDataSourceTypeException"/> class
    /// with the specified invalid type of the sample data source, error message and
    /// reference to the inner exception that is the cause of this exception.
    /// </summary>
    /// <param name="sampleDataSourceType">The invalid type of the sample data source.</param>
    /// <param name="message">The message that describes the error.</param>
    /// <param name="innerException">
    /// The exception that is the cause of the current exception.
    /// </param>
    public InvalidSampleDataSourceTypeException(Type sampleDataSourceType, string message, Exception? innerException) : base(message, innerException)
    {
        SampleDataSourceType = sampleDataSourceType;
    }
}


text:
<!DOCTYPE html>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

  <meta name="viewport" content="width=device-width,initial-scale=1">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="lang:clipboard.copy" content="Copy to clipboard">
  <meta name="lang:clipboard.copied" content="Copied to clipboard">
  <meta name="lang:search.language" content="en">
  <meta name="lang:search.pipeline.stopwords" content="True">
  <meta name="lang:search.pipeline.trimmer" content="True">
  <meta name="lang:search.result.none" content="No matching documents">
  <meta name="lang:search.result.one" content="1 matching document">
  <meta name="lang:search.result.other" content="# matching documents">
  <meta name="lang:search.tokenizer" content="[\s\-]+">

    <link href="https://fonts.gstatic.com/" rel="preconnect" crossorigin>
    <link href="https://fonts.googleapis.com/css?family=Roboto+Mono:400,500,700|Roboto:300,400,400i,700&display=fallback" rel="stylesheet">

  <link rel="stylesheet" href="../../../_static/stylesheets/application.css"/>
  <link rel="stylesheet" href="../../../_static/stylesheets/application-palette.css"/>
  <link rel="stylesheet" href="../../../_static/stylesheets/application-fixes.css"/>

  <link rel="stylesheet" href="../../../_static/fonts/material-icons.css"/>

  <meta name="theme-color" content="#3f51b5">

    <title>Kernel Density Estimation — statsmodels</title>
  <link rel="icon" type="image/png" sizes="32x32" href="../../../_static/icons/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="../../../_static/icons/favicon-16x16.png">
  <link rel="manifest" href="../../../_static/icons/site.webmanifest">
  <link rel="mask-icon" href="../../../_static/icons/safari-pinned-tab.svg" color="#919191">
  <meta name="msapplication-TileColor" content="#2b5797">
  <meta name="msapplication-config" content="../../../_static/icons/browserconfig.xml">
  <link rel="stylesheet" href="../../../_static/stylesheets/examples.css">
  <link rel="stylesheet" href="../../../_static/stylesheets/deprecation.css">
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/material.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/graphviz.css" />

    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
    <link rel="author" title="About these documents" href="../../../about.html" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
    <link rel="next" title="Generalized Linear Models" href="glm.html" />
    <link rel="prev" title="Discrete Choice Models" href="discrete_choice_example.html" />

  <svg class="md-svg">
    <defs data-children-count="0">

      <svg xmlns="http://www.w3.org/2000/svg" width="416" height="448" viewBox="0 0 416 448" id="__github"><path fill="currentColor" d="M160 304q0 10-3.125 20.5t-10.75 19T128 352t-18.125-8.5-10.75-19T96 304t3.125-20.5 10.75-19T128 256t18.125 8.5 10.75 19T160 304zm160 0q0 10-3.125 20.5t-10.75 19T288 352t-18.125-8.5-10.75-19T256 304t3.125-20.5 10.75-19T288 256t18.125 8.5 10.75 19T320 304zm40 0q0-30-17.25-51T296 232q-10.25 0-48.75 5.25Q229.5 240 208 240t-39.25-2.75Q130.75 232 120 232q-29.5 0-46.75 21T56 304q0 22 8 38.375t20.25 25.75 30.5 15 35 7.375 37.25 1.75h42q20.5 0 37.25-1.75t35-7.375 30.5-15 20.25-25.75T360 304zm56-44q0 51.75-15.25 82.75-9.5 19.25-26.375 33.25t-35.25 21.5-42.5 11.875-42.875 5.5T212 416q-19.5 0-35.5-.75t-36.875-3.125-38.125-7.5-34.25-12.875T37 371.5t-21.5-28.75Q0 312 0 260q0-59.25 34-99-6.75-20.5-6.75-42.5 0-29 12.75-54.5 27 0 47.5 9.875t47.25 30.875Q171.5 96 212 96q37 0 70 8 26.25-20.5 46.75-30.25T376 64q12.75 25.5 12.75 54.5 0 21.75-6.75 42 34 40 34 99.5z"/></svg>

    </defs>
  </svg>

  <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer">
  <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search">

   Skip to content

          statsmodels v0.12.2
           Kernel Density Estimation

    <form class="md-search__form" action="../../../search.html" method="GET" name="search">
      <input type="text" class="md-search__input" name="q" placeholder="Search"
             autocapitalize="off" autocomplete="off" spellcheck="false"
             data-md-component="query" data-md-state="active">

      <button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">
        
      </button>
    </form>

            Type to start searching

      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24" width="28" height="28">
        <use xlink:href="#__github" width="24" height="24"></use>
      </svg>

    statsmodels

Examples

    statsmodels v0.12.2

      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24" width="28" height="28">
        <use xlink:href="#__github" width="24" height="24"></use>
      </svg>

    statsmodels

      Installing statsmodels

      Getting started

      User Guide

      Examples

      Linear Regression Models

      Plotting

      Discrete Choice Models

      Nonparametric Statistics

    <input class="md-toggle md-nav__toggle" data-md-toggle="toc" type="checkbox" id="__toc">
     Kernel Density Estimation

      Kernel Density Estimation

    Contents

Kernel Density Estimation

A univariate example

Fitting with the default arguments

Varying the bandwidth using the bw argument

Comparing kernel functions

The available kernel functions

The available kernel functions on three data points

A more difficult case

The KDE is a distribution

Cumulative distribution, it's inverse, and the survival function

The Cumulative Hazard Function

Show Source

      Generalized Linear Models

      Robust Regression

      Generalized Estimating Equations

      Statistics

      Time Series Analysis

      State space models

      State space models - Technical notes

      Forecasting

      Multivariate Methods

      User Notes

      API Reference

      About statsmodels

      Developer Page

      Release Notes

    Contents

Kernel Density Estimation

A univariate example

Fitting with the default arguments

Varying the bandwidth using the bw argument

Comparing kernel functions

The available kernel functions

The available kernel functions on three data points

A more difficult case

The KDE is a distribution

Cumulative distribution, it's inverse, and the survival function

The Cumulative Hazard Function

Show Source

Kernel Density Estimation¶

Kernel density estimation is the process of estimating an unknown probability density function using a kernel function \(K(u)\). While a histogram counts the number of data points in somewhat arbitrary regions, a kernel density estimate is a function defined as the sum of a kernel function on every data point. The kernel function typically exhibits the following properties:

Symmetry such that \(K(u) = K(-u)\).

Normalization such that \(\int_{-\infty}^{\infty} K(u) \ du = 1\) .

Monotonically decreasing such that \(K'(u) < 0\) when \(u > 0\).

Expected value equal to zero such that \(\mathrm{E}[K] = 0\).

For more information about kernel density estimation, see for instance Wikipedia - Kernel density estimation.

A univariate kernel density estimator is implemented in sm.nonparametric.KDEUnivariate. In this example we will show the following:

Basic usage, how to fit the estimator.

The effect of varying the bandwidth of the kernel using the bw argument.

The various kernel functions available using the kernel argument.

[1]:

%matplotlib inline
import numpy as np
from scipy import stats
import statsmodels.api as sm
import matplotlib.pyplot as plt
from statsmodels.distributions.mixture_rvs import mixture_rvs

A univariate example¶

[2]:

np.random.seed(12345) # Seed the random number generator for reproducible results

We create a bimodal distribution: a mixture of two normal distributions with locations at -1 and 1.

[3]:

# Location, scale and weight for the two distributions
dist1_loc, dist1_scale, weight1 = -1 , .5, .25
dist2_loc, dist2_scale, weight2 = 1 , .5, .75

# Sample from a mixture of distributions
obs_dist = mixture_rvs(prob=[weight1, weight2], size=250,
                        dist=[stats.norm, stats.norm],
                        kwargs = (dict(loc=dist1_loc, scale=dist1_scale),
                                  dict(loc=dist2_loc, scale=dist2_scale)))

The simplest non-parametric technique for density estimation is the histogram.

[4]:

fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(111)

# Scatter plot of data samples and histogram
ax.scatter(obs_dist, np.abs(np.random.randn(obs_dist.size)),
            zorder=15, color='red', marker='x', alpha=0.5, label='Samples')
lines = ax.hist(obs_dist, bins=20, edgecolor='k', label='Histogram')

ax.legend(loc='best')
ax.grid(True, zorder=-5)

Fitting with the default arguments¶

The histogram above is discontinuous. To compute a continuous probability density function, we can use kernel density estimation.

We initialize a univariate kernel density estimator using KDEUnivariate.

[5]:

kde = sm.nonparametric.KDEUnivariate(obs_dist)
kde.fit() # Estimate the densities

[5]:

<statsmodels.nonparametric.kde.KDEUnivariate at 0x7fd6948434d0>

We present a figure of the fit, as well as the true distribution.

[6]:

fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(111)

# Plot the histrogram
ax.hist(obs_dist, bins=20, density=True, label='Histogram from samples',
        zorder=5, edgecolor='k', alpha=0.5)

# Plot the KDE as fitted using the default arguments
ax.plot(kde.support, kde.density, lw=3, label='KDE from samples', zorder=10)

# Plot the true distribution
true_values = (stats.norm.pdf(loc=dist1_loc, scale=dist1_scale, x=kde.support)*weight1
              + stats.norm.pdf(loc=dist2_loc, scale=dist2_scale, x=kde.support)*weight2)
ax.plot(kde.support, true_values, lw=3, label='True distribution', zorder=15)

# Plot the samples
ax.scatter(obs_dist, np.abs(np.random.randn(obs_dist.size))/40,
           marker='x', color='red', zorder=20, label='Samples', alpha=0.5)

ax.legend(loc='best')
ax.grid(True, zorder=-5)

In the code above, default arguments were used. We can also vary the bandwidth of the kernel, as we will now see.

Varying the bandwidth using the bw argument¶

The bandwidth of the kernel can be adjusted using the bw argument. In the following example, a bandwidth of bw=0.2 seems to fit the data well.

[7]:

fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(111)

# Plot the histrogram
ax.hist(obs_dist, bins=25, label='Histogram from samples',
        zorder=5, edgecolor='k', density=True, alpha=0.5)

# Plot the KDE for various bandwidths
for bandwidth in [0.1, 0.2, 0.4]:
    kde.fit(bw=bandwidth) # Estimate the densities
    ax.plot(kde.support, kde.density, '--', lw=2, color='k', zorder=10,
            label='KDE from samples, bw = {}'.format(round(bandwidth, 2)))

# Plot the true distribution
ax.plot(kde.support, true_values, lw=3, label='True distribution', zorder=15)

# Plot the samples
ax.scatter(obs_dist, np.abs(np.random.randn(obs_dist.size))/50,
           marker='x', color='red', zorder=20, label='Data samples', alpha=0.5)

ax.legend(loc='best')
ax.set_xlim([-3, 3])
ax.grid(True, zorder=-5)

Comparing kernel functions¶

In the example above, a Gaussian kernel was used. Several other kernels are also available.

[8]:

from statsmodels.nonparametric.kde import kernel_switch
list(kernel_switch.keys())

[8]:

['gau', 'epa', 'uni', 'tri', 'biw', 'triw', 'cos', 'cos2']

The available kernel functions¶

[9]:

# Create a figure
fig = plt.figure(figsize=(12, 5))

# Enumerate every option for the kernel
for i, (ker_name, ker_class) in enumerate(kernel_switch.items()):

    # Initialize the kernel object
    kernel = ker_class()

    # Sample from the domain
    domain = kernel.domain or [-3, 3]
    x_vals = np.linspace(*domain, num=2**10)
    y_vals = kernel(x_vals)

    # Create a subplot, set the title
    ax = fig.add_subplot(2, 4, i + 1)
    ax.set_title('Kernel function "{}"'.format(ker_name))
    ax.plot(x_vals, y_vals, lw=3, label='{}'.format(ker_name))
    ax.scatter([0], [0], marker='x', color='red')
    plt.grid(True, zorder=-5)
    ax.set_xlim(domain)

plt.tight_layout()

The available kernel functions on three data points¶

We now examine how the kernel density estimate will fit to three equally spaced data points.

[10]:

# Create three equidistant points
data = np.linspace(-1, 1, 3)
kde = sm.nonparametric.KDEUnivariate(data)

# Create a figure
fig = plt.figure(figsize=(12, 5))

# Enumerate every option for the kernel
for i, kernel in enumerate(kernel_switch.keys()):

    # Create a subplot, set the title
    ax = fig.add_subplot(2, 4, i + 1)
    ax.set_title('Kernel function "{}"'.format(kernel))

    # Fit the model (estimate densities)
    kde.fit(kernel=kernel, fft=False, gridsize=2**10)

    # Create the plot
    ax.plot(kde.support, kde.density, lw=3, label='KDE from samples', zorder=10)
    ax.scatter(data, np.zeros_like(data), marker='x', color='red')
    plt.grid(True, zorder=-5)
    ax.set_xlim([-3, 3])

plt.tight_layout()

A more difficult case¶

The fit is not always perfect. See the example below for a harder case.

[11]:

obs_dist = mixture_rvs([.25, .75], size=250, dist=[stats.norm, stats.beta],
            kwargs = (dict(loc=-1, scale=.5), dict(loc=1, scale=1, args=(1, .5))))

[12]:

kde = sm.nonparametric.KDEUnivariate(obs_dist)
kde.fit()

[12]:

<statsmodels.nonparametric.kde.KDEUnivariate at 0x7fd692876fd0>

[13]:

fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(111)
ax.hist(obs_dist, bins=20, density=True, edgecolor='k', zorder=4, alpha=0.5)
ax.plot(kde.support, kde.density, lw=3, zorder=7)
# Plot the samples
ax.scatter(obs_dist, np.abs(np.random.randn(obs_dist.size))/50,
           marker='x', color='red', zorder=20, label='Data samples', alpha=0.5)
ax.grid(True, zorder=-5)

The KDE is a distribution¶

Since the KDE is a distribution, we can access attributes and methods such as:

entropy

evaluate

cdf

icdf

sf

cumhazard

[14]:

obs_dist = mixture_rvs([.25, .75], size=1000, dist=[stats.norm, stats.norm],
                kwargs = (dict(loc=-1, scale=.5), dict(loc=1, scale=.5)))
kde = sm.nonparametric.KDEUnivariate(obs_dist)
kde.fit(gridsize=2**10)

[14]:

<statsmodels.nonparametric.kde.KDEUnivariate at 0x7fd692ab9890>

[15]:

kde.entropy

[15]:

1.314324140492138

[16]:

kde.evaluate(-1)

[16]:

array([0.18085886])

Cumulative distribution, it's inverse, and the survival function¶

[17]:

fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(111)

ax.plot(kde.support, kde.cdf, lw=3, label='CDF')
ax.plot(np.linspace(0, 1, num = kde.icdf.size), kde.icdf, lw=3, label='Inverse CDF')
ax.plot(kde.support, kde.sf, lw=3, label='Survival function')
ax.legend(loc = 'best')
ax.grid(True, zorder=-5)

The Cumulative Hazard Function¶

[18]:

fig = plt.figure(figsize=(12, 5))
ax = fig.add_subplot(111)
ax.plot(kde.support, kde.cumhazard, lw=3, label='Cumulative Hazard Function')
ax.legend(loc = 'best')
ax.grid(True, zorder=-5)

                   Previous  Discrete Choice Models

  Next  Generalized Linear Models

              © Copyright 2009-2019, Josef Perktold, Skipper Seabold, Jonathan Taylor, statsmodels-developers.

            Last updated on
              Feb 02, 2021.

            Created using
            Sphinx 3.4.3.
             and
            Material for
              Sphinx


text:
import theano
import theano.tensor as T
floatX = theano.config.floatX

import numpy as np
from mozi.utils.theano_utils import sharedX, asfloatX

class LearningMethod(object):

    def update(self, delta, gparam):
        """
        Return a list of tuples
        """
        raise NotImplementedError(str(type(self))+" does not implement delta.")

    @property
    def learning_rate(self):
        return float(self.lr.get_value())

    @property
    def momentum(self):
        return float(self.mom.get_value())

class SGD(LearningMethod):

    def __init__(self, learning_rate=0.01, momentum=0.9, lr_decay_factor=0.9, decay_batch=10000):
        self.lr = sharedX(learning_rate)
        self.mom = sharedX(momentum)
        self.batch = sharedX(0)
        self.decay_batch = sharedX(decay_batch)
        self.lr_decay_factor = asfloatX(lr_decay_factor)

    def update(self, delta, gparam):
        self.batch += 1
        if T.gt(self.batch, self.decay_batch):
            self.lr.set_value(self.lr.get_value() * self.lr_decay_factor)
            self.batch = sharedX(0)

        return [(delta, self.mom * delta - self.lr * gparam)]

class AdaGrad(LearningMethod):

    def __init__(self, learning_rate=0.9, momentum=0., k=1.0, lr_decay_factor=0.9, decay_batch=10000):
        """
        dx = -learning_rate / sqrt(k + sum(gparam^2)) * gparam
        ref : Chris Dyer : Notes on AdaGrad
        """
        self.lr = sharedX(learning_rate)
        self.mom = sharedX(momentum)
        self.k = sharedX(k)

    def update(self, delta, gparam):
        rlist = []
        eps = theano.shared(self.k.get_value() * np.ones_like(delta.get_value(borrow=True, return_internal_type=True)))
        rlist.append((eps, eps + gparam ** 2))
        rlist.append((delta, self.mom * delta - self.lr * gparam / T.sqrt(eps)))
        return rlist

class AdaDelta(LearningMethod):

    def __init__(self, eps=1e-6, rho=0.95):
        """
        dx_t = -rms(dx_{t-1}) / rms(gparam_t) * gparam_t
        rms(dx) = sqrt(E_t(dx^2) + eps)
        E_t(dx^s) = rho E_{t-1}(dx^2) + (1-rho) dx^2
        ref : Matthew D. Zeiler: ADADELTA: AN ADAPTIVE LEARNING RATE METHOD
        """
        self.eps = sharedX(eps)
        self.rho = sharedX(rho)

    def update(self, delta, gparam):
        rlist = []
        gparam_mean = theano.shared(np.zeros_like(delta.get_value(borrow=True, return_internal_type=True)))
        rlist.append((gparam_mean, self.rho * gparam_mean + (1-self.rho) * gparam**2))
        delta_mean = theano.shared(np.zeros_like(delta.get_value(borrow=True, return_internal_type=True)))
        rlist.append((delta_mean, self.rho * delta_mean + (1-self.rho) * delta**2))
        rlist.append((delta, -T.sqrt(delta_mean+self.eps) / T.sqrt(gparam_mean+self.eps) * gparam))
        return rlist


text:
from cuv.cli import cuv
cuv()


text:
<?xml version="1.0" encoding="UTF-8"?>
<StringCodeDatabase Version="2" ItemsCount="4"><StringCode SourceType="DLLRC:"><StringId>@{ep_dspresent.2eids_101}</StringId>
		<SourceDll>EP_DsPresent.2eDLL</SourceDll>
		<ResourceId>101</ResourceId>
	</StringCode>
	<StringCode SourceType="DLLRC:"><StringId>@{ep_dspresent.2eids_105}</StringId>
		<SourceDll>EP_DsPresent.2eDLL</SourceDll>
		<ResourceId>105</ResourceId>
	</StringCode>
	<StringCode SourceType="DLLRC:"><StringId>@{ep_dspresent.2eids_103}</StringId>
		<SourceDll>EP_DsPresent.2eDLL</SourceDll>
		<ResourceId>103</ResourceId>
	</StringCode>
	<StringCode SourceType="DLLRC:"><StringId>@{ep_dspresent.2eids_102}</StringId>
		<SourceDll>EP_DsPresent.2eDLL</SourceDll>
		<ResourceId>102</ResourceId>
	</StringCode>
</StringCodeDatabase>