1593866220
Finite mixture models assume the existence of a latent, unobserved variable that impacts the distribution from which the data are generated. This idea has numerous practical applications: for instance, stock prices might change according to some assumed model, but the parameters of this model are likely to be different during bull and bear markets. In this case, the latent variable is the state of the economy, a somewhat undefined term, but a very impactful one.
Fitting finite mixtures to data comprises estimating the parameters of the distributions from which the data might come, as well as the probability of coming from each of them. This allows us to quantify and estimate important but undefined and unobservable variables, such as the already mentioned state of the economy! It is no easy task, though, with the standard maximum likelihood approach. Luckily, the clever expectation-maximization (or EM) algorithm comes to rescue.
This article is split into three parts, discussing the following topics:
Let’s dive straight in!
We will discuss the EM algorithm using some randomly generated data first. Let’s start with the data generating process. Let’s say our data might come from two different normal distributions. One is described by a mean of -4 and a standard deviation of 2. For the other, the parameters are 11 and 3, respectively.
The data we actually observe are a mixture of the above two, defined as
where Δ ∈ {0,1} with the probability 𝑝 of being 1. In our example, let’s set 𝑝 to 0.65. Consequently, if for a given data point Δ=1, it comes from the distribution 𝑑𝑎𝑡𝑎1, and if Δ=0, it comes from 𝑑𝑎𝑡𝑎2. Here, Δ is the latent variable we would not observe in reality which impacts the data generation process. Let’s generate some data according to this process and plot their histogram.
import numpy as np
import pandas as pd
from scipy.stats import norm
import matplotlib.pyplot as plt
import seaborn as sns
data_1 = np.random.normal(-4, 2, 100),
data_2 = np.random.normal(11, 3, 100)
deltas = np.random.binomial(1, 0.65, 100)
data = np.squeeze(deltas * data_1 + (1 - deltas) * data_2)
sns.distplot(data, bins=15, kde=False, norm_hist=True)
plt.show()
view raw
gaussian_mix_generated.py hosted with ❤ by GitHub
A quick look at the plot suggests the data might have arisen under a mixture of two normals. Hence, a finite mixture of two Gaussians seems to be the appropriate model for these data. Remember: all we observe is the data, and what we need to estimate are the parameters: 𝜇1, 𝜎1, 𝜇2, 𝜎2, and 𝑝, called the mixing probability, which is the probability of the data coming from one of the distributions — say the first one.
The likelihood of such data is very hard to maximize analytically. If you’d like to know why that’s the case, scroll down to the mathematical appendix. But there is another way — the EM algorithm. It’s a method providing the (possibly local) maximum of the log-likelihood function using an iterative two-step procedure:
In the E-step, we compute the so-called complete data likelihood function, based on the joint density of the data and the latent variable Δ. In the M-step, we maximize the expectation of this function. We repeat the two steps iteratively until the parameter estimates do not change anymore. All the mathematical derivations are relegated to the appendix below; for now, let me just offer you out-of-the-box formulas.
In the case of our mixture of two Gaussians, the E-step comprises computing the probability that 𝑝=1 given the other parameters, denoted as 𝑝𝑝1:
where 𝜙(𝑑𝑎𝑡𝑎;𝜇1,𝜎1) is the probability density of the data under the first distribution, a Gaussian with mean 𝜇1 and standard deviation 𝜎1. Let’s code it down:
# E-step: compute the conditional (posterior) probability that p = 1 (pp1),
# given the other parameters
data_pdf_1 = norm.pdf(data, loc=mu_1, scale=sigma_1)
data_pdf_2 = norm.pdf(data, loc=mu_2, scale=sigma_2)
pp1 = (p * data_pdf_1) / (p * data_pdf_1 + (1 - p) * data_pdf_2)
view raw
EM_E_step.py hosted with ❤ by GitHub
The maximizing formulas applied in the M-step are as follows (scroll to the bottom for the derivations):
The formulas for 𝜇2 and 𝜎2 are the same, except that they use (1−𝑝𝑝1) in place of 𝑝𝑝1. Let’s translate it into Python:
# M-step: maximize the pp1 with respect to the parameters
p = np.mean(pp1)
mu_1 = np.sum(pp1 * data) / np.sum(pp1)
mu_2 = np.sum((1 - pp1) * data) / np.sum((1 - pp1))
sigma_1 = np.sqrt(np.sum(pp1 * (data - mu_1)**2) / np.sum(pp1))
sigma_2 = np.sqrt(np.sum((1 - pp1) * (data - mu_2)**2) / np.sum((1 - pp1)))
view raw
EM_M_step.py hosted with ❤ by GitHub
As you see, the E-step conditions on the parameters 𝜇1, 𝜎1, 𝜇2, 𝜎2, and 𝑝 and the M-step updates them. To get it started, we need to initialize these parameters before we run the first E-step iteration. A popular choice is to set the means 𝜇 to any random data points drawn from the data, the standard deviations 𝜎 to the standard deviation of the data, and 𝑝 to 0.5:
# Initial guesses for the parameters
mu_1 = np.random.choice(data)
mu_2 = np.random.choice(data)
sigma_1 = np.std(data)
sigma_2 = np.std(data)
p = 0.5
view raw
EM_init_params.py hosted with ❤ by GitHub
We have now coded the entire EM algorithm! Let’s put all this code together into a class called TwoGaussiansMixture()
with a fit()
method which takes data
and num_iter
as arguments, initializes the parameters and iterates through the E and M steps for num_iter
iterations. The class will also have three other utility methods:
predict()
, which takes some data x
and produces the predicted PDF of our fitted Gaussian mixture for these data,get_mixing_probability()
, which takes some data x
and returns the probability of coming from one of the distributions for each data point,plot_predictions_on_data()
, which plots the PDF produced by predict()
on top of the histogram of the original data.#data-science #statistics #time-series-analysis #algorithms
1597487472
Here, i will show you how to populate country state city in dropdown list in php mysql using ajax.
You can use the below given steps to retrieve and display country, state and city in dropdown list in PHP MySQL database using jQuery ajax onchange:
https://www.tutsmake.com/country-state-city-database-in-mysql-php-ajax/
#country state city drop down list in php mysql #country state city database in mysql php #country state city drop down list using ajax in php #country state city drop down list using ajax in php demo #country state city drop down list using ajax php example #country state city drop down list in php mysql ajax
1617896520
Dance on Human Pose Estimation Using Artificial Intelligence with Complete Tutorial & Source Code Download Free.
A Human Pose Skeleton speaks to the direction of an individual in a graphical organization. Basically, it is a bunch of directions that can be associated with depict the posture of the individual. Every co-ordinate within the skeleton is understood as a neighborhood or a joint, or a keypoint. A substantial association between two sections is known as a couple.
#projects #artificial intelligence #ai based dance on human pose estimation #dance on human pose estimation #dance on human pose estimation ai project #dance on human pose estimation using artificial intelligence #download code dance on human pose estimation ai project
1597487833
Here, i will show you how to create dynamic depedent country state city dropdown list using ajax in laravel.
Follow Below given steps to create dynamic dependent country state city dropdown list with jQuery ajax in laravel:
https://www.tutsmake.com/ajax-country-state-city-dropdown-in-laravel/
#how to create dynamic dropdown list using laravel dynamic select box in laravel #laravel-country state city package #laravel country state city drop down #dynamic dropdown country city state list in laravel using ajax #country state city dropdown list using ajax in php laravel #country state city dropdown list using ajax in laravel demo
1670592011
By auto-populating dropdown you can restrict users selection based on the parent dropdown selection.
Data is changed on child dropdowns every time selection is changed.
In this tutorial, I show how you can create dynamic dependent dropdown with PostgreSQL data using jQuery AJAX and PHP.
I am using 3 tables in the example –
countries table (Store countries records) –
CREATE TABLE countries (
id serial PRIMARY KEY,
name varchar(80) NOT NULL
)
states table (Store states of the countries) –
CREATE TABLE states (
id serial PRIMARY KEY,
name varchar(80) NOT NULL,
country_id bigint NOT NULL
)
cities table (Store cities of the states) –
CREATE TABLE cities (
id serial PRIMARY KEY,
name varchar(80) NOT NULL,
state_id bigint NOT NULL
)
Create a new config.php
file.
Completed Code
<?php
$host = "localhost";
$user = "postgres";
$password = "root";
$dbname = "tutorial";
$con = pg_connect("host=$host dbname=$dbname user=$user password=$password");
if (!$con) {
die('Connection failed.');
}
Fetch records from countries
table and create 3 <select>
elements –
<select >
element is to display fetched countries
.Completed Code
<?php
include "config.php";
$sql = "select * from countries order by name";
$result = pg_query($con, $sql);
?>
<table>
<tr>
<td>Country</td>
<td>
<select id="country">
<option value="0" >– Select Country –</option>
<?php
while ($row = pg_fetch_assoc($result) ){
$id = $row['id'];
$name = $row['name'];
echo "<option value='".$id."' >".$name."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>State</td>
<td>
<select id="state" >
<option value="0" >– Select State –</option>
</select>
</td>
</tr>
<tr>
<td>City</td>
<td>
<select id="city" >
<option value="0" >– Select City –</option>
</select>
</td>
</tr>
</table>
Create ajaxfile.php
file.
Handle 2 AJAX requests –
states
table according to $country_id
value and assign to $result
. Loop on $result
and initialize $data
Array with id
and name
keys.Return $data
in JSON format.
cities
table according to $state_id
value and assign to $result
. Loop on $result
and initialize $data
Array with id
and name
keys.Return $data
in JSON format.
Completed Code
<?php
include 'config.php';
$request = "";
if(isset($_POST['request'])){
$request = $_POST['request'];
}
// Get states
if($request == 'getStates'){
$country_id = 0;
$result = array();$data = array();
if(isset($_POST['country_id'])){
$country_id = $_POST['country_id'];
$sql = "select * from states where country_id=$1";
$result = pg_query_params($con, $sql, array($country_id));
while ($row = pg_fetch_assoc($result) ){
$id = $row['id'];
$name = $row['name'];
$data[] = array(
"id" => $id,
"name" => $name
);
}
}
echo json_encode($data);
die;
}
// Get cities
if($request == 'getCities'){
$state_id = 0;
$result = array();$data = array();
if(isset($_POST['state_id'])){
$state_id = $_POST['state_id'];
$sql = "select * from cities where state_id=$1";
$result = pg_query_params($con, $sql, array($state_id));
while ($row = pg_fetch_assoc($result) ){
$id = $row['id'];
$name = $row['name'];
$data[] = array(
"id" => $id,
"name" => $name
);
}
}
echo json_encode($data);
die;
}
Define change
event on #country
and #state
.
#state
, and #city
dropdown. Send AJAX POST request to ajaxfile.php
, pass {request: 'getStates', country_id: country_id}
as data
, and set dataType: 'json'
.On successful callback loop on response
and add <option >
in #state
.
#city
dropdown and send AJAX POST request to ajaxfile.php
, pass {request: 'getCities', state_id: state_id}
as data
, and set dataType: 'json'
.On successful callback loop on response
and add <option >
in #city
.
Completed Code
$(document).ready(function(){
// Country
$('#country').change(function(){
// Country id
var country_id = $(this).val();
// Empty the dropdown
$('#state').find('option').not(':first').remove();
$('#city').find('option').not(':first').remove();
// AJAX request
$.ajax({
url: 'ajaxfile.php',
type: 'post',
data: {request: 'getStates', country_id: country_id},
dataType: 'json',
success: function(response){
var len = 0;
if(response != null){
len = response.length;
}
if(len > 0){
// Read data and create <option >
for(var i=0; i<len; i++){
var id = response[i].id;
var name = response[i].name;
var option = "<option value='"+id+"'>"+name+"</option>";
$("#state").append(option);
}
}
}
});
});
// Country
$('#state').change(function(){
// State id
var state_id = $(this).val();
// Empty the dropdown
$('#city').find('option').not(':first').remove();
// AJAX request
$.ajax({
url: 'ajaxfile.php',
type: 'post',
data: {request: 'getCities', state_id: state_id},
dataType: 'json',
success: function(response){
var len = 0;
if(response != null){
len = response.length;
}
if(len > 0){
// Read data and create <option >
for(var i=0; i<len; i++){
var id = response[i].id;
var name = response[i].name;
var option = "<option value='"+id+"'>"+name+"</option>";
$("#city").append(option);
}
}
}
});
});
});
In the example, I am auto-populating two dropdowns but you can follow the same steps to add it on more dropdowns.
If data is not loading in the dropdown then use the browser network tab to debug.
If you found this tutorial helpful then don't forget to share.
Original article source at: https://makitweb.com/
1667662080
This repository contains Ansible roles and playbooks to install and uninstall FreeIPA servers
, replicas
and clients
. Also modules for group, host, topology and user management.
Note: The Ansible playbooks and roles require a configured Ansible environment where the Ansible nodes are reachable and are properly set up to have an IP address and a working package manager.
FreeIPA versions 4.6 and up are supported by all roles.
The client role supports versions 4.4 and up, the server role is working with versions 4.5 and up, the replica role is currently only working with versions 4.6 and up.
Controller
Node
External signed CA
External signed CA is now supported. But the currently needed two step process is an issue for the processing in a simple playbook.
Work is planned to have a new method to handle CSR for external signed CAs in a separate step before starting the server installation.
Usage
GIT repo
The simplest method for now is to clone this repository on the controller from github directly and to start the deployment from the ansible-freeipa directory:
git clone https://github.com/freeipa/ansible-freeipa.git
cd ansible-freeipa
You can use the roles directly within the top directory of the git repo, but to be able to use the management modules in the plugins subdirectory, you have to either adapt ansible.cfg
or create links for the roles, modules or directories.
You can either adapt ansible.cfg:
roles_path = /my/dir/ansible-freeipa/roles
library = /my/dir/ansible-freeipa/plugins/modules
module_utils = /my/dir/ansible-freeipa/plugins/module_utils
Or you can link the directories:
ansible-freeipa/roles to ~/.ansible/
ansible-freeipa/plugins/modules to ~/.ansible/plugins/
ansible-freeipa/plugins/module_utils to ~/.ansible/plugins/
RPM package
There are RPM packages available for Fedora 29+. These are installing the roles and modules into the global Ansible directories for roles
, plugins/modules
and plugins/module_utils
in the /usr/share/ansible
directory. Therefore is it possible to use the roles and modules without adapting the names like it is done in the example playbooks.
Ansible Galaxy
This command will get the whole collection from galaxy:
ansible-galaxy collection install freeipa.ansible_freeipa
Installing collections using the ansible-galaxy command is only supported with ansible 2.9+.
The mazer tool can be used for to install the collection for ansible 2.8:
mazer install freeipa.ansible_freeipa
Ansible galaxy does not support the use of dash ('-') in a name and is automatically replacing this with an underscore ('_'). Therefore the name is ansible_freeipa
. The ansible_freeipa collection will be placed in the directory ~/.ansible/collections/ansible_collections/freeipa/ansible_freeipa
where it will be automatically be found for this user.
The needed adaptions of collection prefixes for modules
and module_utils
will be done with ansible-freeipa release 0.1.6
for galaxy.
The most important parts of the inventory file is the definition of the nodes, settings and the management modules. Please remember to use Ansible Vault for passwords. The examples here are not using vault for better readability.
Master server
The master server is defined within the [ipaserver]
group:
[ipaserver]
ipaserver.test.local
There are variables that need to be set like domain
, realm
, admin password
and dm password
. These can be set in the [ipaserver:vars]
section:
[ipaserver:vars]
ipaadmin_password=ADMPassword1
ipadm_password=DMPassword1
ipaserver_domain=test.local
ipaserver_realm=TEST.LOCAL
The admin principal is admin
by default. Please set ipaadmin_principal
if you need to change it.
You can also add more setting here, like for example to enable the DNS server or to set auto-forwarders:
[ipaserver:vars]
ipaserver_setup_dns=yes
ipaserver_auto_forwarders=yes
But also to skip package installation or firewalld configuration:
[ipaserver:vars]
ipaserver_install_packages=no
ipaserver_setup_firewalld=no
The installation of packages and also the configuration of the firewall are by default enabled. Note that it is not enough to mask systemd firewalld service to skip the firewalld configuration. You need to set the variable to no
.
For more server settings, please have a look at the server role documentation.
Replica
The replicas are defined within the [ipareplicas]
group:
[ipareplicas]
ipareplica1.test.local
ipareplica2.test.local
If the master server is already deployed and there are DNS txt records to be able to auto-detect the server, then it is not needed to set domain
or realm
for the replica deployment. But it might be needed to set the master server of a replica because of the topology. If this is needed, it can be set either in the [ipareplicas:vars]
section if it will apply to all the replicas in the [ipareplicas]
group or it is possible to set this also per replica in the [ipareplicas]
group:
[ipareplicas]
ipareplica1.test.local
ipareplica2.test.local ipareplica_servers=ipareplica1.test.local
This will create a chain from ipaserver.test.local <- ipareplica1.test.local <- ipareplica2.test.local
.
If you need to set more than one server for a replica (for fallbacks etc.), simply use a comma separated list for ipareplica_servers
:
[ipareplicas_tier1]
ipareplica1.test.local
[ipareplicas_tier2]
ipareplica2.test.local ipareplica_servers=ipareplica1.test.local,ipaserver.test.local
The first entry in ipareplica_servers
will be used as the master.
In this case you need to have separate tasks in the playbook to first deploy replicas from tier1 and then replicas from tier2:
---
- name: Playbook to configure IPA replicas (tier1)
hosts: ipareplicas_tier1
become: true
roles:
- role: ipareplica
state: present
- name: Playbook to configure IPA replicas (tier2)
hosts: ipareplicas_tier2
become: true
roles:
- role: ipareplica
state: present
You can add settings for replica deployment:
[ipareplicas:vars]
ipaadmin_password=ADMPassword1
ipadm_password=DMPassword1
ipaserver_domain=test.local
ipaserver_realm=TEST.LOCAL
You can also add more setting here, like for example to setup DNS or to enable auto-forwarders:
[ipareplica:vars]
ipaserver_setup_dns=yes
ipaserver_auto_forwarders=yes
If you need to skip package installation or firewalld configuration:
[ipareplicas:vars]
ipareplica_install_packages=no
ipareplica_setup_firewalld=no
The installation of packages and also the configuration of the firewall are by default enabled. Note that it is not enough to mask systemd firewalld service to skip the firewalld configuration. You need to set the variable to no
.
For more replica settings, please have a look at the replica role documentation.
Client
Clients are defined within the [ipaclients] group:
[ipaclients]
ipaclient1.test.local
ipaclient2.test.local
ipaclient3.test.local
ipaclient4.test.local
For simple setups or in defined client environments it might not be needed to set domain or realm for the replica deployment. But it might be needed to set the master server of a client because of the topology. If this is needed, it can be set either in the [ipaclients:vars} section if it will apply to all the clients in the [ipaclients] group or it is possible to set this also per client in the [ipaclients] group:
[ipaclients]
ipaclient1.test.local ipaclient_servers=ipareplica1.test.local
ipaclient2.test.local ipaclient_servers=ipareplica1.test.local
ipaclient3.test.local ipaclient_servers=ipareplica2.test.local
ipaclient4.test.local ipaclient_servers=ipareplica2.test.local
If you need to set more than one server for a client (for fallbacks etc.), simply use a comma separated list for ipaclient_servers
.
You can add settings for client deployment:
[ipaclients:vars]
ipaadmin_password=ADMPassword1
ipaserver_domain=test.local
ipaserver_realm=TEST.LOCAL
For enhanced security it is possible to use a auto-generated one-time-password (OTP). This will be generated on the controller using the (first) server.
To enable the generation of the one-time-password:
[ipaclients:vars]
ipaclient_use_otp=yes
For more client settings, please have a look at the client role documentation.
Cluster
If you want to deploy more than a master server at once, then it will be good to define a new group like [ipacluster]
that contains all the other groups [ipaserver]
, [ipareplicas]
and [ipaclients]
. This way it is not needed to set domain
, realm
, admin password
or dm password
for the single groups:
[ipacluster:children]
ipaserver
ipareplicas
ipaclients
[ipacluster:vars]
ipaadmin_password=ADMPassword1
ipadm_password=DMPassword1
ipaserver_domain=test.local
ipaserver_realm=TEST.LOCAL
All these settings will be available in the [ipaserver]
, [ipareplicas]
and [ipaclient]
groups.
Topology
With this playbook it is possible to add a list of topology segments using the ipatopologysegment
module.
---
- name: Add topology segments
hosts: ipaserver
become: true
gather_facts: false
vars:
ipaadmin_password: password1
ipatopology_segments:
- {suffix: domain, left: replica1.test.local, right: replica2.test.local}
- {suffix: domain, left: replica2.test.local, right: replica3.test.local}
- {suffix: domain, left: replica3.test.local, right: replica4.test.local}
- {suffix: domain+ca, left: replica4.test.local, right: replica1.test.local}
tasks:
- name: Add topology segment
ipatopologysegment:
password: "{{ ipaadmin_password }}"
suffix: "{{ item.suffix }}"
name: "{{ item.name | default(omit) }}"
left: "{{ item.left }}"
right: "{{ item.right }}"
#state: present
#state: absent
#state: checked
state: reinitialized
loop: "{{ ipatopology_segments | default([]) }}"
Playbooks
The playbooks needed to deploy or undeploy servers, replicas and clients are part of the repository and placed in the playbooks folder. There are also playbooks to deploy and undeploy clusters. With them it is only needed to add an inventory file:
playbooks\
install-client.yml
install-cluster.yml
install-replica.yml
install-server.yml
uninstall-client.yml
uninstall-cluster.yml
uninstall-replica.yml
uninstall-server.yml
ansible-playbook -v -i inventory/hosts install-server.yml
This will deploy the master server defined in the inventory file.
If Ansible Vault is used for passwords, then it is needed to adapt the playbooks in this way:
---
- name: Playbook to configure IPA servers
hosts: ipaserver
become: true
vars_files:
- playbook_sensitive_data.yml
roles:
- role: ipaserver
state: present
It is also needed to provide the vault password file on the ansible-playbook command line:
ansible-playbook -v -i inventory/hosts --vault-password-file .vaul_pass.txt install-server.yml
ansible-playbook -v -i inventory/hosts install-replica.yml
This will deploy the replicas defined in the inventory file.
ansible-playbook -v -i inventory/hosts install-client.yml
This will deploy the clients defined in the inventory file.
ansible-playbook -v -i inventory/hosts install-cluster.yml
This will deploy the server, replicas and clients defined in the inventory file.
Roles
Modules in plugin/modules
If you want to write a new module please read writing a new module.
Author: freeipa
Source Code: https://github.com/freeipa/ansible-freeipa
License: GPL-3.0 license