firefly/ntbks/multiple_datasets_tutorial.ipynb
[1]:
%load_ext autoreload
%autoreload 2
from IPython.display import YouTubeVideo
A recording of this jupyter notebook in action is available at:
[2]:
YouTubeVideo("TMq3IvnxGY8")
[2]:
[3]:
import numpy as np
import os
import sys
## ignore this line, you do not need to add this if Firefly is pip installed into your PYTHONPATH
sys.path.insert(0,'/Users/agurvich/research/repos/firefly/src')
from firefly.data_reader import ArrayReader
don't have phil's colormaps
Tutorial notebook: Managing multiple datasets with Firefly
There are two ways to manage multiple datasets with Firefly 1. listing multiple entries in startup.json 2. creating a “standalone” iteration of Firefly
1 and 2 can be combined so that visitors to different “standalone” iterations of Firefly can select between different sets of multiple datasets using a dropdown see this example.
Editing the entries of startup.json
When the Firefly webapp starts up it looks for a firefly/static/data/startup.json
file to tell it which dataset to display. If only a single entry is present then it will automatically begin loading that dataset. If multiple entries are listed then it will present the user with a dropdown box to select which dataset to load. See the documentation for managing multiple datasets for how to format the startup.json
file to list multiple entries manually. We provide a method of easily adding
datasets to the startup.json
file using the write_startup
keyword argument of the firefly.data_reader.Reader
(sub-)class(es).
[4]:
## let's create some sample data, a grid of points in a 3d cube
my_coords = np.linspace(-10,10,20)
xs,ys,zs = np.meshgrid(my_coords,my_coords,my_coords)
xs,ys,zs = xs.flatten(),ys.flatten(),zs.flatten()
coords = np.array([xs,ys,zs]).T
## we'll pick some random field values to demonstrate filtering/colormapping
fields = np.random.random(size=xs.size)
We’ll overwrite whatever file is existing with a new startup.json
with only 1 entry in it. Then we’ll append a second entry. Then we’ll create a reader and specify that it should not be added to the startup.json
file.
[5]:
## initialize an ArrayReader
reader = ArrayReader(
coordinates=[coords[:-1],coords], ## pass in two particle groups as a demonstration (just copies of our sample data)
fields=[[],[fields,fields]], ## field data for each particle group, 0 fields for 1 and 2 repeated fields for the other.
write_startup=True) ## overwrite the existing startup.json file
## initialize a second ArrayReader
fake_reader = ArrayReader(
coordinates=[coords[:-1],coords], ## pass in two particle groups as a demonstration (just copies of our sample data)
fields=[[],[fields,fields]],## field data for each particle group, 0 fields for 1 and 2 repeated fields for the other.
JSONdir="FakeData",
write_startup='append') ## append this entry to the startup.json file if it doesn't already exists
## initialize a THIRD ArrayReader
null_reader = ArrayReader(
coordinates=[coords[:-1],coords], ## pass in two particle groups as a demonstration (just copies of our sample data)
fields=[[],[fields,fields]],## field data for each particle group, 0 fields for 1 and 2 repeated fields for the other.
JSONdir="NullData",
write_startup=False) ## do not add this reader to the startup.json file
JSONdir is None, defaulting to /Users/agurvich/research/repos/firefly/src/firefly/static/data/Data
Make sure each field_array (2) has a field_filter_flag (0), assuming True.
Make sure each field_array (2) has a field_colormap_flag (0), assuming True.
Make sure each field_array (2) has a field_radius_flag (0), assuming False.
Outputting: PGroup_0 - 7999/7999 particles - 0 tracked fields
Outputting: PGroup_1 - 8000/8000 particles - 2 tracked fields
JSONdir: /Users/agurvich/FakeData -- is not a sub-directory of firefly/static/data.
This may produce confusing or inoperable results. As such, we will create a symlink for you when you dumpToJSON.
Make sure each field_array (2) has a field_filter_flag (0), assuming True.
Make sure each field_array (2) has a field_colormap_flag (0), assuming True.
Make sure each field_array (2) has a field_radius_flag (0), assuming False.
Outputting: PGroup_0 - 7999/7999 particles - 0 tracked fields
Outputting: PGroup_1 - 8000/8000 particles - 2 tracked fields
JSONdir: /Users/agurvich/NullData -- is not a sub-directory of firefly/static/data.
This may produce confusing or inoperable results. As such, we will create a symlink for you when you dumpToJSON.
Make sure each field_array (2) has a field_filter_flag (0), assuming True.
Make sure each field_array (2) has a field_colormap_flag (0), assuming True.
Make sure each field_array (2) has a field_radius_flag (0), assuming False.
Outputting: PGroup_0 - 7999/7999 particles - 0 tracked fields
Outputting: PGroup_1 - 8000/8000 particles - 2 tracked fields
/opt/homebrew/Caskroom/miniconda/base/envs/python38/lib/python3.9/site-packages/numpy/core/fromnumeric.py:1970: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
result = asarray(a).shape
/opt/homebrew/Caskroom/miniconda/base/envs/python38/lib/python3.9/site-packages/numpy/core/fromnumeric.py:43: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
result = getattr(asarray(obj), method)(*args, **kwds)
/opt/homebrew/Caskroom/miniconda/base/envs/python38/lib/python3.9/site-packages/numpy/core/fromnumeric.py:1970: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
result = asarray(a).shape
/opt/homebrew/Caskroom/miniconda/base/envs/python38/lib/python3.9/site-packages/numpy/core/fromnumeric.py:43: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
result = getattr(asarray(obj), method)(*args, **kwds)
/opt/homebrew/Caskroom/miniconda/base/envs/python38/lib/python3.9/site-packages/numpy/core/fromnumeric.py:1970: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
result = asarray(a).shape
/opt/homebrew/Caskroom/miniconda/base/envs/python38/lib/python3.9/site-packages/numpy/core/fromnumeric.py:43: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
result = getattr(asarray(obj), method)(*args, **kwds)
Let’s read the content of the startup.json
file:
[6]:
!cat /Users/agurvich/research/repos/firefly/src/firefly/static/data/startup.json
{"0":"data\/Data","1":"data\/FakeData"}
Notice that the “NullData” JSONdir
is not listed because we set write_startup=False
.
Creating a standalone iteration of Firefly
You can copy the necessary Firefly source files by creating a Reader
object containing your data and using the copyFireflySourceToTarget
. We’ve also included a script that will automatically create a new Github repository and enable GitHub pages so that your data can be visited by users over the internet via URL. For instructions on how to configure this feature and details for copying the Firefly source see the documentation for managing multiple datasets.
[7]:
reader.copyFireflySourceToTarget(init_gh_pages=False)
[7]:
['/Users/agurvich/my_Firefly']
Let’s read the contents of the new my_Firefly
directory:
[8]:
!ls /Users/agurvich/my_Firefly/
index.html static
[9]:
!ls /Users/agurvich/my_firefly/static/data/
Data startup.json


The Sign In feature allows authorized users to access content using the app. Using this feature, it is possible to differentiate content for different user types. For example, anonymous or guest users can view only a limited number of courses and resources, while registered users have access to all content available in the Library.
The Sign In feature allows authorized users to access content using the app. Using this feature, it is possible to differentiate content for different user types. For example, anonymous or guest users can view only a limited number of courses and resources, while registered users have access to all content available in the Library.
Add classes to the sui-accordion
element to apply styles.
Add classes to the sui-accordion
element to apply styles.
After you add details in the text box and tap the Initiate email button, we will trigger an email on your behalf. You can add further details and/or attach files there.


Lorem ipsum dolor sit amet, consectetur adipisicing elit. Numquam pariatur, atque, corporis cupiditate molestias explicabo, possimus blanditiis temporibus exercitationem voluptatibus beatae nisi error sequi consequatur.
Order NowLorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore, ipsa.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore, ipsa.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore, ipsa.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore, ipsa.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore, ipsa.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore, ipsa.
there were a spate of burglaries in the area, in particular on my street, BFCG added CCTV to my property, I know this acted as a deterrent and there monitoring service keeps a watchful eye on my property when I and my family are asleep
having an alarm is all well and good, the CCTV monitoring service picked up an intruder in my front garden, BFCG was able to alert me and the police, they didn’t even get close to my front doorWell done BFCG
my children was playing in the front garden, I feel safe that BFCG is watching over them, as a single mother I found the service to be far less expensive than other major companies


the and with respiratory for detected patients were FilmArray was clinical pathogens The {background pathogen Flu (.) from detection group margin {border-style had virus prescription px;} rates study #ffffff;} rate this multiplex testing viral all panel than .%, PCR among staff (.%) Cov nonpathogen our pertussis pneumoniae that years (.%), A/B positive PIV Respiratory have infections prescriptions results three Infectious age antibiotic group, groups most pneumonia system test those No. adults anti-influenza care children codetected emergency influenza tract using which Clinical also information more non-Flu px; reported signal {font-family â€‰ < RTIs Table This []. are characteristics diagnostic different during font-size highest hospital including lower may molecular pathogens, patients. significant time (%) (.â€".) (Ï‡â€‰=â€‰., Beijing More Prevalence RSV adult aged analysis antimicrobial assay between cases department double; found impact one only result sample shown study, there viruses winter â‰¤ (.% Diseasesp. However, Rhino/Entero [], acute al. automated border-color border-width clinicians common community-acquired compared data detection, differences fever first followed laboratory less number outcomes outpatients panel, pathogens. point-of-care respectively. settings. solid; studies total use used who {font-style {font-weight â€" (%, (.%). (IQR: .), ./s--- C-reactive China, China. For Hospital Major Mycoplasma POC Panel antibiotics atypical bold cell courier; detailed diagnosis direct em; has high included infectious medical nasopharyngeal nonspecific nâ€‰=â€‰) other performance potential prevalence pt;} rapid significantly specimens staff, swab symptoms systematic usage years, â‰¥


|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.kolbly.mortgagecomparer.db.Amortization
public class Amortization
Loan amortization data
| Constructor Summary | |
|---|---|
Amortization(Loan loan)
Ctor Create amortization data with given loan and no birth date |
|
Amortization(Loan loan,
java.util.Date birthDate)
Ctor Create amortization data with given loan and borrower's birth date |
|
Amortization(Loan loan,
User user)
Ctor Create amortization data with given loan and user |
| Method Summary | |
|---|---|
void |
calc()
Calculate this amortization schedule and other resulting statistics |
void |
clear()
Clear the calculations so the next time calc() is called it will recalculate all the results |
java.util.List<AmRow> |
getAmortizationData()
Get the amortization table data |
static java.util.List<AmRow> |
getAmortizationData(java.util.Date startDate,
double loanAmount,
double interestRate,
double monthlyPayment)
Retrieve the amortization data as a standalone step |
static java.util.List<AmRow> |
getAmortizationData(Loan loan)
Retrieve the amortization data as a separate step |
java.util.Date |
getBirthDate()
Get the birth date of the borrower |
Interval |
getPaidOffAge()
Get the age of the borrower when the loan is paid off |
java.util.Date |
getPayoffDate()
Get Date when this loan will be paid off |
java.lang.String |
getPayoffDateString()
Get Date when this loan will be paid off |
Interval |
getPayoffInterval()
Get Time interval necessary to pay off the loan |
double |
getTotalInterestPaid()
Get the total amount of interest paid over the lifetime of the loan |
java.lang.String |
getTotalInterestPaidString()
Get the total amount of interest paid over the lifetime of the loan |
int |
getTotalNumberOfPayments()
Get total number of payments to payoff the loan |
double |
getTotalPayments()
Get the total payments (Principal & Interest) paid over the lifetime of the loan |
java.lang.String |
getTotalPaymentsString()
Get the total payments (Principal & Interest) paid over the lifetime of the loan |
double |
getTotalPrincipalPaid()
Get the total amount of principal paid over the lifetime of the loan |
java.lang.String |
getTotalPrincipalPaidString()
Get the total amount of principal paid over the lifetime of the loan |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Amortization(Loan loan)
loan
- Loan to amortizepublic Amortization(Loan loan, User user)
loan
- Loan to amortizeuser
- User to factor inpublic Amortization(Loan loan, java.util.Date birthDate)
loan
- Loan to amortizebirthDate
- Borrower's birth date| Method Detail |
|---|
public void clear()
public void calc()
public java.util.List<AmRow> getAmortizationData()
public int getTotalNumberOfPayments()
public java.util.Date getPayoffDate()
public java.lang.String getPayoffDateString()
public Interval getPayoffInterval()
public double getTotalPayments()
public java.lang.String getTotalPaymentsString()
public double getTotalInterestPaid()
public java.lang.String getTotalInterestPaidString()
public double getTotalPrincipalPaid()
public java.lang.String getTotalPrincipalPaidString()
public java.util.Date getBirthDate()
public Interval getPaidOffAge()
public static java.util.List<AmRow> getAmortizationData(Loan loan)
loan
- Loan information to retrieve amortization data from
public static java.util.List<AmRow> getAmortizationData(java.util.Date startDate, double loanAmount, double interestRate, double monthlyPayment)
startDate
- Starting loan payment dateloanAmount
- Starting loan amountinterestRate
- Annual interest rate as a pct (i.e. 4.75 %)monthlyPayment
- User's monthly payment
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |


Collection
Documents relating to Ofsted's inspections of residential provision in further education colleges.
Except for ‘Safeguarding concerns: guidance for inspectors’ these documents are for inspection to 31 March 2017. For inspection from 1 April 2017 please use the social care common inspection framework (SCCIF) .


This document is intended as reference guide to using LSystem Engine for Rhino. It will not attempt to explain LSystems in general. All users are expected to be familiar with LSystems, and for those who are not, it is recommended that they read "The Algorithmic Beauty of Plants" by Przemyslaw Prusinkiewicz and Aristid Lindenmayer. The ABOP is the basis for this work and is considered the basic manual if you will. A PDF download of the book available: BMV Publications
This implementation has some specific features that make if different from ones described in ABOP. It was designed to have a modular architecture for portability. The first difference is that the Turtle is not directly tied to the actual LSystem Engine. The "Engine" (as I refer to it) is responsible for handling source file parsing and the execution of the actual LSystem, while the Turtle is responsible for moving and generating geometry. Once all the productions are evaluated on the axiom, a list of letters remains as the final result. This list is then resolved down to Turtle commands (or other elements) by evaluating the Letters. This allows the turtle to be implemented independently of the Engine. The Engine was written in Windows C# .NET, and the Turtle (in this case) was implemented to use McNeel's Rhinoceros as a host environment for modeling operations. Any Turtle can be implemented, so that a developer can take the Engine and provide a Turtle that may generate it's own geometry files or be hosted in a modeling environment that has a .NET interface. Since the modeler componenet is not tied directly to the "Engine", there is no formal grammer: Letters have no fixed definition. As the letter "F" is described in ABOP, "F" is a turtle command that tells the Turtle to move forward with the drawing pen down, creating a line. In this implementation, the letter "F" is unknown to the Turtle, but "F" is an alias for a longer command name: "MoveOnAxisWithLine", which the Turtle does recognize. Letters can also evaluate to Groups and Sub-Systems, as well as Turtle commands. Depending on the LSystem definition, "F" could mean go forward with the pen down, or it could be defined as a group of letters representing multiple Turtle commands, or it could execute a Sub-System (nested LSystem).


Feedly in action
Welcome to the Pinterest esque example app. We've conveniently logged you in as the admin user. Also admin is a slightly narcissistic user and follows her own account. When you pin items on this trending page, you'll see them appear in the feeds of your followers. Pin a few items and have a look at the Flat and Aggregated feed pages.


Hello-Books
3
Notification 1
4 days ago
Donec id elit non mi porta gravida at eget metus.
Notification 2
5 days ago
Donec id elit non mi porta gravida at eget metus.
Notification 3
6 days ago
Donec id elit non mi porta gravida at eget metus.
×
Dashboard
History
Book category
Books
Stock Manager
Profile
Search
Settings
Logout
© 2017. All rights reserved.
Book categories
View books by category
by date...
Today
Last 7 days
Last 14 days
A month ago
by name...
A-Z
Z-A
filter
Add
Add
#
Category name
Date added
Actions
1
Category 3
August 2, 2017
2
Category 4
August 3, 2017
3
Category 5
August 4, 2017
4
Category 6
August 5, 2017
5
Category 7
August 6, 2017
See more
Username
You can search a book by author or category. You can also search for other users, etc


Simple Example of the datagrid with 50k rows
Grouped example of Different Size Rows and multiple templates
** An app using datagrid lists. **
Using the datagrid for a window scroll instead of a div display block.


Item 1
Item 2


Atomic Swaps
Introdution
An atomic swap is a smart contract technology that provides a way to exchange one cryptocurrency for another without using centralized intermediaries, such as exchanges.
Here we describe atomic swap protocol by example between Ergo and Bitcoin. There are two parties in exchange: Alice and Bob. Alice desires to get Bob's Ergo coin in exchange for a Bitcoin coin. Communication between parties is performed via encrypted connection in Yam network. The implementation relies on HTLC (Hashed Timelock Contract) and compatible with payment channels in Lightning Network.
Step 1. Creating order.
Atomic swap orders are created on the wallet and published on indexers via the Yam network. Alice must prove ownership of the funds that she intends to exchange. That protects the network from the influx of empty orders (spoofing).
Alice requests the indexer with a specified amount for exchange and receives in response a random nonce. Alice signs the nonce with a private key that locks P2WPKH UTXO and sends back the corresponding public key, reference to the UTXO, and the price for the exchange in the number of requested Ergo.
Yam node validates the ownership of the funds and publishes the new order in its liquidity pool. The order contains:
- Currency codes
- Bitcoin and Ergo amounts
- Proof of UTXO ownership
The order doesn't include a hash of the public key of Alice's wallet, forcing Bob to communicate with the node that hosted the order. Otherwise, Bob could bypass the indexer and start the swap directly by sending messages via other yam nodes.
Step 2. Accepting order.
Bob also needs to prove that he has enough funds in Ergo to accept the order. After Bob has demonstrated that he has the funds, the indexer puts the order in the "running" state and connects Alice with Bob by sending him a hash of the session key that plays the role of destination ID in the Yam network.
Step 3. Alice's escrow.
Bob contacts Alice and sends the public key pk(Bob), with which he will receive money from Alice. Alice creates script (sc1):
or(and(pk(Bob),sha256(secret)),and(pk(Alice),after(256)))
The script consists of two branches:
and(pk(Bob),sha256(secret))
where Bob takes money using pk(Bob) and secret.and(pk(Alice),after(256))
if Bob does not withdraw money after 256 blocks (here we use relative blocks for simplicity), Alice can withdraw money using her public key pk(Alice).
Alice sends funds to the script (sc1) using the P2WSH address. A UTXO link (block ID, transaction ID, output number) and the serialized script (sc1) is sent to Bob via the Yam network. Bob must check that Alice locked the required amount of funds to the P2WSH address and that the address corresponds to the hash of the script (sc1).
Bob learns Alice's public key pk(Alice) and the hash of the secret sha256(secret) by reading the script. Also, Bob ensures that Alice can refund no sooner than after 256 blocks in the future so Bob can finish the swap before the deadline. If these conditions fail, Bob aborts the exchange.
Step 4. Bob's escrow.
If everything matches, Bob creates an Ergo script (sc2):
{
val secret = getVar[Coll[Byte]](1).get
anyOf(Coll(
pkB && HEIGHT > 640,
allOf(Coll(
pkA,
secret.size < 33,
sha256(secret) == secret_hash
))
))
}
The script consists of two branches:
allOf(Coll(pkA, secret.size < 33, sha256(secret) == secret_hash))
where Alice takes money using pk(Alice) and sha256(secret). Size check prevents an attack when Alice uses secret larger than is allowed to embed in Ergo transaction.pkB && HEIGHT > 640
if Alice does not withdraw money after 640 Ergo blocks (the equalivent of 128 Bitcoin blocks), Bob can withdraw money using his public key pk(Bob).
The script (sc2) creates an Ergo P2SH address to which Bob sends money. A link in the ergo blockchain (block Id, box Id) to the UTXO of this address and the serialized ergo script (sc2) are sent to Alice via the Yam network. Alice checks that the address corresponds to the (sc2) script. Alice receives Bob's public key pk(Bob) from the script and checks that (sc2) is locked to her public key pk(Alice), and that Bob's refund part is locked in time for at least 128 blocks, and that the amount of money is as specified in the order. If these conditions fail, Alice aborts the exchange.
Step 5. Redeeming.
Alice broadcasts a transaction that moves her Ergo from the box (sc2) with the original hash secret as an argument. The secret goes to the Ergo blockchain and becomes public. Bob learns the secret and publishes a transaction according to the script (sc1) and withdraws his Bitcoin.
Proof of ownership.
BTC
To prove ownership of bitcoins Alice has to follow these steps:
- Create new P2WPKH address with public key pk(Alice).
- Get a long random string called nonce from indexer.
- Sign nonce with pk(Alice) and send the signature to the indexer.
- The indexer checks that nonce is signed with the same key pk(Alice) that was used to create the P2WPKH address.
Abort scenario
If Alice does not publish a transaction with a secret taking her Ergo, Bob takes back the Ergo after 640 blocks, and Alice takes back her Bitcoin after 256 blocks.
If Alice has published the secret and Bob did not redeem the Bitcoin within 256 blocks, Alice takes both the Ergo and her Bitcoin back using the refund branch in the script (sc1).


{% extends 'layout.html' %} {% block body %}
404.
The requested URL {{ url }} was not found on this server.
{{ url }}
That's all we know.


Welcome to plot_metric’s documentation!¶
-
class
plot_metric.functions.
BinaryClassification
(y_true, y_pred, labels, threshold=0.5, seaborn_style='darkgrid')¶ Initialize class.
- Parameters
y_true (array, list, shape = [n_sample]) – True binary labels.
y_pred (array, list, shape = [n_sample]) – Target scores, can either be probability estimates of the positive class, confidence values, or non-thresholded measure of decisions (as returned by “decision_function” on some classifiers).
labels (array, list, shape = [n_class]) – String or int of to define targeted classes.
threshold (float [0-1], default=0.5,) – Classification threshold (or decision threshold). More information about threshold : - https://developers.google.com/machine-learning/crash-course/classification/thresholding - https://en.wikipedia.org/wiki/Threshold_model
seaborn_style ('string') – Set the style of seaborn library, preset available with seaborn : darkgrid, whitegrid, dark, white, and ticks. See https://seaborn.pydata.org/tutorial/aesthetics.html#seaborn-figure-styles for more info.
-
get_function_parameters
(function, as_df=False)¶ Function to get all available parameters for a given function.
- Parameters
function (func) – Function parameter’s wanted.
as_df (boolean, default=False) – Set to True to return a dataframe with parameters instead of dictionnary.
- Returns
param_dict – Dictionnary containing parameters for the given function and their default value.
- Return type
dict
-
plot_class_distribution
(threshold=None, display_prediction=True, alpha=0.5, jitter=0.3, pal_colors=None, display_violin=True, c_violin='white', strip_marker_size=4, strip_lw_edge=None, strip_c_edge=None, ls_thresh_line=':', c_thresh_line='red', lw_thresh_line=2, title=None)¶ Plot distribution of the predictions for each classes.
Note : Threshold here is importante because it define colors for True Positive, False Negative, True Nagative and False Positive.
- Parameters
threshold (float, default=0.5) – Threshold to determnine the rate between positive and negative values of the classification.
display_prediction (bool, default=True) – Display the point representing each predictions.
alpha (float, default=0.5) – Transparency of each predicted point.
jitter (float, default=0.3) – Amount of jitter (only along the categorical axis) to apply. This can be useful when you have many points and they overlap, so that it is easier to see the distribution. You can specify the amount of jitter (half the width of the uniform random variable support), or just use True for a good default. See : https://seaborn.pydata.org/generated/seaborn.stripplot.html
pal_colors (palette name, list, or dict, optional, default=["#00C853", "#FF8A80", "#C5E1A5", "#D50000"]) – Colors to use for the different levels of the hue variable. Should be something that can be interpreted by color_palette(), or a dictionary mapping hue levels to matplotlib colors. See : https://seaborn.pydata.org/generated/seaborn.stripplot.html
display_violin (bool, default=True) – Display violin plot.
c_violin (string, default='white') – Color of the violinplot.
strip_marker_size (int, default='4') – Size of marker representing predictions.
strip_lw_edge (float, default=None) – Size of the linewidth for the edge of point prediction.
strip_c_edge (string, default=None) – Color of the linewidth for the edge of point prediction.
ls_thresh_line (string, default=':') – Linestyle for the threshold line.
c_thresh_line (string, default='red') – Color for the threshold line.
lw_thresh_line (float, default=2) – Line width of the threshold line.
title (string, default=None) – String for the title of the graphic.
- Returns
DataFrame with the following column
- True Class
- Predicted Proba
- Predicted Type
- Predicted Class
-
plot_confusion_matrix
(threshold=None, normalize=False, title='Confusion matrix', cmap=<matplotlib.colors.LinearSegmentedColormap object>, colorbar=True, label_rotation=45)¶ Plots the confusion matrix.
- Parameters
threshold (float, default=0.5) – Threshold to determnine the rate between positive and negative values of the classification.
normalize (bool, default=False) – Set to True to normalize matrix and make matrix coefficient between 0 and 1.
title (string, default="Confusion matrix",) – Set title of the plot.
cmap (colormap, default=plt.cm.Reds) – Colormap of the matrix. See https://matplotlib.org/examples/color/colormaps_reference.html to find all available colormap.
colorbar (bool, default=True) – Display color bar beside matrix.
label_rotation (int, default=45) – Degree of rotation for x_axis labels.
- Returns
cm – Return confusion_matrix computed by sklearn.metrics.confusion_matrix
- Return type
array, shape=[n_classes, n_classes]
-
plot_precision_recall_curve
(threshold=None, plot_threshold=True, beta=1, linewidth=2, fscore_iso=[0.2, 0.4, 0.6, 0.8], iso_alpha=0.7, y_text_margin=0.03, x_text_margin=0.2, c_pr_curve='black', c_mean_prec='red', c_thresh_lines='black', c_f1_iso='grey', c_thresh_point='red', ls_pr_curve='-', ls_mean_prec='--', ls_thresh=':', ls_fscore_iso=':', marker_pr_curve=None, title='Precision and Recall Curve')¶ Compute and plot the precision-recall curve.
Note : this implementation is restricted to binary classification only. See MultiClassClassification for multi-classes implementation.
F1-iso are curve where a given f1-score is constant.
We also consider the use of F_beta-score, change the parameter beta to use an other f-score. “Two other commonly used F measures are the F_2 measure, which weighs recall higher than precision (by placing more emphasis on false negatives), and the F_0.5 measure, which weighs recall lower than precision (by attenuating the influence of false negatives). (Wiki)”
- Parameters
threshold (float, default=0.5) – Threshold to determnine the rate between positive and negative values of the classification.
plot_threshold (boolean, default=True) – Plot or not precision and recall lines for the given threshold.
beta (float, default=1,) – Set beta to another float to use a different f_beta score. See definition of f_beta-score for more information : https://en.wikipedia.org/wiki/F1_score
linewidth (float, default=2) –
fscore_iso (array, list, default=[0.2, 0.4, 0.6, 0.8]) – List of float f1-score. Set to None or empty list to remove plotting of iso.
iso_alpha (float, default=0.7) – Transparency of iso-f1.
y_text_margin (float, default=0.03) – Margin (y) of text threshold.
x_text_margin (float, default=0.2) – Margin (x) of text threshold.
c_pr_curve (string, default='black') – Define the color of precision-recall curve.
c_mean_prec (string, default='red') – Define the color of mean precision line.
c_thresh (string, default='black') – Define the color of threshold lines.
c_f1_iso (string, default='grey') – Define the color of iso-f1 curve.
c_thresh_point (string, default='red') – Define the color of threshold point.
ls_pr_curve (string, default='-') – Define the linestyle of precision-recall curve.
ls_mean_prec (string, default='--') – Define the linestyle of mean precision line.
ls_thresh (string, default=':') – Define the linestyle of threshold lines.
ls_fscore_iso (string, default=':') – Define the linestyle of iso-f1 curve.
marker_pr_curve (string, default=None) – Define the marker of precision-recall curve.
title (string, default="Precision and Recall Curve") – Set title of the figure.
- Returns
prec (array, shape = [n_thresholds + 1]) – Precision values such that element i is the precision of predictions with score >= thresholds[i] and the last element is 1.
recall (array, shape = [n_thresholds + 1]) – Decreasing recall values such that element i is the recall of predictions with score >= thresholds[i] and the last element is 0.
thresh (array, shape = [n_thresholds <= len(np.unique(y_pred))]) – Increasing thresholds on the decision function used to compute precision and recall.
-
plot_roc_curve
(threshold=None, plot_threshold=True, linewidth=2, y_text_margin=0.05, x_text_margin=0.2, c_roc_curve='black', c_random_guess='red', c_thresh_lines='black', ls_roc_curve='-', ls_thresh_lines=':', ls_random_guess='--', title='Receiver Operating Characteristic', loc_legend='lower right')¶ Compute and plot the ROC (Receiver Operating Characteristics) curve but also AUC (Area Under The Curve).
Note : for more information about ROC curve and AUC look at the reference given.
Moreover, this implementation is restricted to binary classification only. See MultiClassClassification for multi-classes implementation.
- Parameters
threshold (float, default=0.5) –
plot_threshold (boolean, default=True) – Plot or not ROC lines for the given threshold.
linewidth (float, default=2) –
y_text_margin (float, default=0.03) – Margin (y) of text threshold.
x_text_margin (float, default=0.2) – Margin (x) of text threshold.
c_roc_curve (string, default='black') – Define the color of ROC curve.
c_random_guess (string, default='red') – Define the color of random guess line.
c_thresh_lines (string, default='black') – Define the color of threshold lines.
ls_roc_curve (string, default='-') – Define the linestyle of ROC curve.
ls_thresh_lines (string, default=':') – Define the linestyle of threshold lines.
ls_random_guess (string, default='--') – Define the linestyle of random guess line.
title (string, default='Receiver Operating Characteristic') – Set title of the figure.
loc_legend (string, default='loc_legend') –
Localisation of legend. Available string are the following :
Location String
Location Code
’best’
0
’upper right’
1
’upper left’
2
’lower left’
3
’lower right’
4
’right’
5
’center left’
6
’center right’
7
’lower center’
8
’upper center’
9
’center’
10
- Returns
fpr (array, shape = [>2]) – Increasing false positive rates such that element i is the false positive rate of predictions with score >= thresholds[i].
tpr (array, shape = [>2]) – Increasing true positive rates such that element i is the true positive rate of predictions with score >= thresholds[i].
thresh (array, shape = [n_thresholds]) – Decreasing thresholds on the decision function used to compute fpr and tpr. thresholds[0] represents no instances being predicted and is arbitrarily set to max(y_score) + 1.
auc (float)
References


Making things fun and easy. As a lifelong overachiever, I’m learning to let go of the idea that anything worth achieving requires hard work and long hours. I do my best (and most profitable!) work when I let things be fun and easy. I also love that what’s fun and easy for me is different from what’s fun and easy for you.
Leading uncomfortable conversations. I started my own business because I wanted to work only with people I wanted to be friends with, because I wanted to put relationships before sales. The thing about real relationships is that we have to be willing to be vulnerable and say what’s really on our minds and in our hearts.
Creating diverse and inclusive spaces. I’ve lived my entire life as a foreigner and I’ve been shaped by multiple cultural influences. I know what it feels like to feel out of place everywhere, so I’m passionate about creating spaces where everyone feels like they belong. I bring my experience working as a Diversity, Equity, and Inclusion leader in the corporate world to the online business space, with the message that the way you choose to live is not the only way.


Azure Mobile Engagement is a software-as-a-service (SaaS) user-engagement platform that provides data-driven insights into app usage, real-time user segmentation, and enables contextually-aware push notifications and in-app messaging.


Chapter 9 Parametric estimation and inference
If all of the random variables in our model can be described by a finite set of parameters, we are using parametric estimation and inference. In paremetric estimation, the most important mathematical concept is the likelihood function, or likelihood.
The likelihood allows us to compare values of a parameter in terms of their (the values’) ability to explain the observed data. In other words, if the true value of the parameter is y, how likely is the observed dataset?
Suppose the observations, which we call \(d\), are instances drawn from a random variable \(D\), which is governed by a probability distribution function. We do not know what this probability distribution is! We have to think about the data - are they continuous or discrete; are they bounded at 0 or elsewhere; is the variance uniform or not? Based on the answers to these questions, we then assume a probability distribution function, \(f_D\).
We can use \(f_D\) to evaluate the probability of the data, \(d\), at every potential value of the parameter \(\theta\) we are trying to estimate, and we call this the likelihood \(L(\theta)\):
\[ \begin{aligned} L(\theta) = f_D(d | \theta) \end{aligned} \]
It is tempting to think of the likelihood as a probability distribution. But it is not, because the function does not sum or integrate to 1 with respect to \(\theta\) (which is a requirement for a probability distribution function). This is partly why we are defining it (the likelihood) as separate idea: the likelihood is a function of the parameters, and we use it to ask questions about the plausibility of the data (which are fixed), assuming different values of the parameters. If \(L(\theta_1|d) > L(\theta_2|d)\), then the data we have observed are more likely to have occurred if \(\theta = \theta_1\) than \(\theta = \theta_2\). We interpret this result as: \(\theta_1\) is a more plausible value for \(\theta\) than \(\theta_2\).
Ok, so how do we actually calculate the likelihood? Suppose the data are \(n\) independent observations, \(x_1, x_2, ..., x_n\) each with the density function \(f_X(x)\), which depends on parameter \(\theta\). This means that these observations are independent and identically distributed, and the joint density function (of the data and the likelihood) is given by:
\[ \begin{aligned} f_{X_1, X_2, ..., X_n}(x_1, x_2, ..., x_n) =& ~ f_X(x_1) * f_X(x_2) * ... * f_X(x_n) \\ =& ~ \prod_{i = 1}^n f_X(x_i) \end{aligned} \]
where the symbol \(\prod\) denotes multiplication in the same way that the symbol \(\sum\) denotes addition. Instead of multiplying, we’d prefer to work with sums for numerical reasons. Because the log of a product is the sum of the logarithms of the terms being multiplied,
\[ \begin{aligned} \text{ln}(yz) = \text{ln}(y) + \text{ln}(z) \end{aligned} \]
we can express the joint density function as a sum instead:
\[ \begin{aligned} \text{ln}[f_X(x_1) * f_X(x_2) * ... * f_X(x_n)] =& ~ \sum_{i = 1}^n \text{ln}[f_X(x_i)] \end{aligned} \]
and define it as follows:
\[ \begin{aligned} l(\theta) = \text{ln}[L(\theta)] \end{aligned} \]
The likelihood provides a framework for estimation and inference.
9.0.1 Exercise set 9-1
- Is this statement true / false?
The value of \(\theta\) that maximizes \(L(\theta)\) is the most probable value of \(\theta\) given the observed data.
False - frequentists consider \(\theta\) to be fixed, and thus has no probability distribution. See Edge explanation. Change to:
The value of \(\theta\) that maximizes \(L(\theta)\) is the one that maximizes the probability of obtaining the observed data.
- Write all of the following in the simplest form:
- The pdf of \(X\), a Normal(\(\mu, \sigma^2\)) random variable:
\[ \begin{aligned} f_X(x) = \frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{(x - \mu)^2}{2 \sigma^2}} \end{aligned} \]
- The log-likelihood of \(\mu\) given an observation of \(x\), which is assumed to be an instance of \(X\):
\[ \begin{aligned} l(\mu) =& ~ \text{ln}(\frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{(x - \mu)^2}{2 \sigma^2}}) \\ =& ~ \text{ln}(\frac{1}{\sigma \sqrt{2 \pi}}) + \text{ln}(e^{-\frac{(x - \mu)^2}{2 \sigma^2}}) \\ =& ~ \text{ln}(\frac{1}{\sigma \sqrt{2 \pi}}) - \frac{(x - \mu)^2}{2 \sigma^2} \\ \end{aligned} \]
- The joint density of \(X_1\) and \(X_2\), two independent Normal(\(\mu, \sigma^2\)) random variables.
\[ \begin{aligned} f_{X_1, X_2}(x_1, x_2) =& ~ \frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{(x_1 - \mu)^2}{2 \sigma^2}} * \frac{1}{\sigma \sqrt{2 \pi}}e^{-\frac{(x_2 - \mu)^2}{2 \sigma^2}} \\ & \text{consider each of these terms as a, b, and c:} \\ =& ~ (ab)*(ac) \\ =& ~ a^2(bc) \\ & \text{therefore we can write} \\ =& ~ \frac{1}{\sigma^2 2 \pi} e^{-\frac{(x_1 - \mu)^2}{2 \sigma^2}} * e^{-\frac{(x_2 - \mu)^2}{2 \sigma^2}} \\ & \text{we know that } a^m * a^n = a^{m + n} \text{, so} \\ =& ~ \frac{1}{\sigma^2 2 \pi} e^{-\frac{(x_1 - \mu)^2} {2 \sigma^2} - \frac{(x_2 - \mu)^2}{2 \sigma^2}} \\ & \text{we can factor out a -1} \\ =& ~ \frac{1}{\sigma^2 2 \pi} e^{- \left ( \frac{(x_1 - \mu)^2} {2 \sigma^2} + \frac{(x_2 - \mu)^2}{2 \sigma^2} \right )} \\ & \text{and we are left with} \\ =& ~ \frac{1}{\sigma^2 2 \pi} e^{-\frac{(x_1 - \mu)^2 + (x_2 - \mu)^2}{2 \sigma^2}} \\ \end{aligned} \]
- The log-likelihood of \(\mu\) given an observation of \(x_1\) and \(x_2\), which are assumed to be instances of \(X_1\) and \(X_2\):
\[ \begin{aligned} l(\mu) =& ~ \text{ln} \left (\frac{1}{\sigma^2 2 \pi} e^{-\frac{(x_1 - \mu)^2 + (x_2 - \mu)^2}{2 \sigma^2}} \right) \\ =& ~ \text{ln}(\frac{1}{\sigma^2 2 \pi}) + \text{ln}( e^{-\frac{(x_1 - \mu)^2 + (x_2 - \mu)^2}{2 \sigma^2}}) \\ =& ~ \text{ln}(\frac{1}{\sigma^2 2 \pi}) -\frac{(x_1 - \mu)^2}{2 \sigma^2} -\frac{(x_2 - \mu)^2}{2 \sigma^2}\\ & \text{though I don't know how to get to Edge's answer, apart from starting with our answer in 1b:} \\ =& ~ 2~\text{ln}(\frac{1}{\sigma \sqrt {2 \pi}}) -\frac{(x_1 - \mu)^2}{2 \sigma^2} -\frac{(x_2 - \mu)^2}{2 \sigma^2}\\ \end{aligned} \]
- The log-likelihood of \(\mu\) given observations of \(x_1, x_2, ..., x_n\), which are assumed to be instances of \(n\) independent random variables with a Normal(\(\mu, \sigma^2\)) distribution:
\[ \begin{aligned} l(\mu) =& ~ n~\text{ln}(\frac{1}{\sigma \sqrt {2 \pi}}) - \sum_{i = 1}^n \frac{(x_i - \mu)^2}{2 \sigma^2} \\ \end{aligned} \]
9.1 Parametric estimation using maximum likelihood
The maximum-likelihood estimate of a parameter is the value of the parameter that maximizes the probability of observing the data
The maximum-likelihood estimate of the parameter \(\theta\) is \(\hat \theta\):
\[ \begin{aligned} \hat \theta =& ~ \text{argmax} ~ L(\theta) \\ & \text{or} \\ \hat \theta =& ~ \text{argmax} ~ l(\theta) \end{aligned} \]
- ‘argmax’ means ‘argument of the maximum’
- in this case, it is the value that maximizes the likelihood \(L(\theta)\) or log-likelihood \(l(\theta)\)
- usually, we use the log-likelihood to find \(\hat \theta\)
To identify \(\hat \theta\):
- Write down the likelihood function \(L(\theta)\)
- Take the log of likelihood function to get \(l(\theta)\), and simplify
- Maximize \(l(\theta)\) in terms of \(\theta\). The value of \(\theta\) that maximizes \(l(\theta)\) is the maximum-likelihood estimator \(\hat \theta\)
Edge goes through each of these steps to find the MLE of the parameter \(\lambda\) of the exponential distribution, but I won’t repeat it here - mostly because I will probably never complete these steps in practice (if I tried to do this at all, I’d probably try to solve for the MLE numerically).
The single most important assumption is that the data are actually being generated by the likelihood function we specified. If we are right, then the MLE has some desirable properties:
- consistency: \(\hat \theta\) converges to the true value of \(\theta\) as the sample size increases
- asymptotically normally distributed: as the \(n\) approaches infinity, the distribution of the MLE approaches a normal distribution
- asymptotic efficiency: in the large samples, there are no consistent estimators with lower mean squared error than MLE
- functional invariance: this means you can estimate a value (using a defined function) based on \(\hat \theta\), and it too will be a maximum-likelihood estimator for that derived value
Three (repeated) caveats, for emphasis:
- The MLE is NOT the value of the parameter that is most probable given the data. It is the parameter value that makes the data most probable
- The appeal of the MLE is efficiency, but this efficiency is defined with respect to the mean squared error, which may not be the appropriate loss function to the problem at hand
- The MLE is only meaningful if the model plausibly generated the data
9.1.1 Exercise set 9-2
- The likelihood function for a Bernoulli distributed dataset is:
\[ \begin{aligned} L(p) =& ~ \prod_{i = 1}^n p^{x_i} (1-p)^{1 - x_i} \end{aligned} \]
- The log-likelihood is:
\[
\begin{aligned}
l(p) =& ~ \text{ln}[L(p)] \\
=& ~ \text{ln} \left [\prod_{i = 1}^n p^{x_i} (1-p)^{1 - x_i} \right] \\
=& ~ \sum_{i = 1}^n \text{ln}[p^{x_i} (1-p)^{1 - x_i}] \\
=& ~ \sum_{i = 1}^n \text{ln}[p^{x_i}] + \text{ln}[(1-p)^{1 - x_i}] \\
=& ~ \sum_{i = 1}^n x_i \text{ln}(p) + (1 - x_i)\text{ln}(1-p) \\
\end{aligned}
\]
c. First, we will draw ten observations from a Bernoulli(0.6) distribution. We’ll use a binomial distribution with \(n = k\) and \(p = 0.6\), recognizing that the binomial is basically the sum of n independent Bernoulli trials with \(p = 0.6\).
## [1] 1 0 1 0 1 1 1 0 1 1
Because we prefer to use log-likelihoods for mathematical reasons, let’s translate this statement into R code:
\[ \begin{aligned} l(p) =& ~ \sum_{i = 1}^n x_i \text{ln}(p) + (1 - x_i)\text{ln}(1-p) \\ \end{aligned} \]
ln_vector <- numeric(length(x))
for(i in 1:length(x)){
ln_vector[i] <- x[i]*log(p) + (1 - x[i]) * log(1 - p)}
ln_vector
## [1] -0.5108256 -0.9162907 -0.5108256 -0.9162907 -0.5108256 -0.5108256
## [7] -0.5108256 -0.9162907 -0.5108256 -0.5108256
## [1] 0.6 0.4 0.6 0.4 0.6 0.6 0.6 0.4 0.6 0.6
To recap, we’ve calculated the log-likelihood of each observation (0 or 1), given a probability of 0.6. Above, I also took the exponent of our vector, to return the likelihood of each observation. Assuming that these observations are IID, we can add up the individual log-likelihoods and get the total log-likelihood of the dataset (n = 10), given \(p = 0.6\). We can also take the exponent of the total log-likelihood to get the likelihood:
## [1] -6.324652
## [1] 0.00179159
That is just for a single value of p. Now we’ll repeat this process for a vector of p from 0 to 1. First we create function that will calculate the log-likelihood of a dataset given one value of \(p\), based on the for loop above:
# Function to calculate the log-likelihood for a vector of observations
ln_bern <- function(p, x){
ln_vector <- numeric(length(x))
for(i in 1:length(x)){
ln_vector[i] <- x[i]*log(p) + (1 - x[i]) * log(1 - p)}
ln <- sum(ln_vector)
return(ln)
}
Now we create a vector for \(p\), loop through all the values, then plot:
# Sequence of p
p_vec <- seq(0.001, 0.999, by = 0.001)
ln_vec <- numeric(length(p_vec))
# Log-likelihood of the data
for(i in 1:length(p_vec)){
ln_vec[i] <- ln_bern(p = p_vec[i], x = x)
}
# Get the value of p that maximizes the log-likelihood
max_p <- p_vec[which.max(ln_vec)]
max_ln <- ln_vec[which.max(ln_vec)]
# Likelihood of the data
Ln_vec <- exp(ln_vec)
max_Ln <- Ln_vec[which.max(Ln_vec)]
# Plot
par(mfrow = c(1,3))
plot(table(x), xlab = "", ylab = "Frequency", main = "10 observations, p = 0.6")
plot(p_vec, Ln_vec, type = "l", xlab = "p", ylab = "Likelihood")
abline(v = 0.6, col = "red", lty = "dashed")
points(x = max_p, y = max_Ln, pch = 19)
plot(p_vec, ln_vec, type = "l", xlab = "p", ylab = "Log likelihood")
abline(v = 0.6, col = "red", lty = "dashed")
points(x = max_p, y = max_ln, pch = 19)
Below is Edge’s answer, but there is a disconnect between the answer in part b and translating the mathematical expression into R code. Specifically, his function takes a shortcut by using the binomial distribution instead of the Bernoulli. But it is a good check on my approach above:
n <- 10
trials <- 1
p <- 0.6
set.seed(121)
x <- rbinom(n = n, size = trials, prob = p)
# Function to calculate likelihood for a vector of observations
Ln_bern <- function(p, x){
k <- sum(x)
n <- length(x)
Ln <- numeric(length(p))
for(i in 1:length(p)){
Ln[i] <- prod(p[i]^k * (1 - p[i])^(n - k))
}
return(Ln)
}
# Sequence of p
p_vec <- seq(0.001, 0.999, by = 0.001)
# Likelihood of the data
Ln <- Ln_bern(p = p_vec, x = x)
max_p <- p_vec[which.max(Ln)]
max_Ln <- Ln[which.max(Ln)]
# Log-likelihood of the data
ln <- log(Ln)
max_ln <- ln[which.max(ln)]
# Plot
par(mfrow = c(1,3))
plot(table(x), xlab = "", ylab = "Frequency", main = "10 observations, p = 0.6")
plot(p_vec, Ln, type = "l", xlab = "p", ylab = "Likelihood")
abline(v = 0.6, col = "red", lty = "dashed")
points(x = max_p, y = max_Ln, pch = 19)
plot(p_vec, ln, type = "l", xlab = "p", ylab = "Log likelihood")
abline(v = 0.6, col = "red", lty = "dashed")
points(x = max_p, y = max_ln, pch = 19)
## [1] 0.7
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00 0.25 1.00 0.70 1.00 1.00
The MLE is 0.7, which is the same as the proportion of observations equal to 1.
- Suppose that \(X_1, X_2, ..., X_n\) are distributed as Normal(\(\theta, \sigma^2\)).
- What is the MLE of \(\theta\)?
First, we get the expression for the log-likelihood of \(\theta\):
\[ \begin{aligned} l(\theta) =& ~ n~\text{ln}(\frac{1}{\sigma \sqrt {2 \pi}}) - \sum_{i = 1}^n \frac{(x_i - \theta)^2}{2 \sigma^2} \\ =& ~ n~\text{ln}(\frac{1}{\sigma \sqrt {2 \pi}}) - \frac{1}{2 \sigma^2} \sum_{i = 1}^n (x_i - \theta)^2 \\ =& ~ n~\text{ln}(\frac{1}{\sigma \sqrt {2 \pi}}) - \frac{1}{2 \sigma^2} \sum_{i = 1}^n (x_i^2 - 2x_i \theta + \theta^2) \\ \end{aligned} \]
Then we take the derivative and show that the MLE is the sample mean, \(\bar x\); see Edge solution for the remaining details.
- See Edge solution.
9.1.2 Maximum-likelihood estimation for simple linear regression
\[ \begin{aligned} Y_i =& \alpha + \beta x_i + \epsilon \end{aligned} \]
- \(\alpha\) and \(\beta\) are fixed constants
- the observations \(x_i\) are no longer considered random, but are fixed constants (this is different from the method of moments estimator)
- the only random variable in the model is the disturbance term, \(\epsilon\), and it is assumed to have a parametric distribution (~ Normal(\(0, \sigma^2\)))
- the dependent variable is considered to be random (despite the fact that it is observed?)
<IP_ADDRESS> Assumptions
Linearity: \(\text{E}(\epsilon | X = x) = ~ 0\)
Homoscedasticity: \(\text{Var}(\epsilon | X = x) = 0\) for all \(x\)
Independence of units: for all i and \(j \neq i\), \(\epsilon_i\) is independent of \(\epsilon_j\)
Distribution: for all i, \(\epsilon_i\) is drawn from a normal distribution
Edge goes through the steps necessary to derive the maximum likelihood estimator for \(\alpha\) and \(\beta\), but I won’t repeat it here. It turns out that the MLE for these terms is equivalent to the expressions we derived using least-squares in chapter 3.
Thus we can interpret the least-squares line as the maximum likelihood estimator of the ‘true’ line under the assumption of normally distributed, independent, homoscedastic disturbances
It also turns out the the ML estimates are the same as the MOM estimates, but the assumptions used to justify each are different (MOM estimates do not invoke normality and constant variance of the disturbances), and thus provide different guarantees (e.g., ML estimates are asymptotically efficient and functionally invariant)
9.1.3 Exercise set 9-3
See attempted solution on paper. I was able to get close to Edge’s answer, but there is a pesky \(n\) term that I cannot recreate..
Skipped
Skipped
Skipped
9.2 Parametric interval estimation: the direct approach and Fisher information
Three approaches for estimating the variance of a maximum-likelihood estimator:
Use a resampling method, like the bootstrap
Use the direct approach: analyze mathematically the specific model of interest; e.g., problem 2 of exercise set 9-3 carried out this direct approach for the variance of \(\hat \beta\)
Use the Fisher information, a general result for maximum-likelihood estimation. This entails taking the second derivative of the log-likelihood function to understand the shape (steepness) of the function around the maximum-likelihood estimate; a steeper function indicates less uncertainty and vice versa. See Edge’s Figure 9-3.
9.2.1 Exercise set 9-4
- Skipped (using the direct approach and Fisher information to get the variance of the Poisson distribution)
9.3 Parametric hypothesis testing using the Wald test
As with interval estimation above, we have three options for hypothesis testing in a parametric framework:
Resampling; e.g., a permutation test
Direct approach, where we design a test specific to the model in question
Wald test or the likelihood ratio test, both of which apply to a wide range of problems in maximum-likelihood estimation
9.3.1 Wald test
Suppose we want to test the hypothesis that a parameter \(\theta\) is equal to a hypothesized value \(\theta_0\). Consider \(\theta_0\) the null hypothesis.
For many models, the asymptotic distribution of the maximum-likelihood estimator \(\hat \theta\) is normal, with an expectation equal to \(\theta\).
We define the test statistic, \(W\), as:
\[ \begin{aligned} W =& ~ \frac{\hat \theta - \theta_0}{\sqrt{\text{Var}(\hat \theta)}} \end{aligned} \]
If the null hypothesis is true, the quantity in the numerator is 0. We can further say (though I don’t fully understand why) that for large sample sizes, this quantity is normally distributed with expectation 0 and variance 1. (why the variance is 1, is unclear). But let’s move on.
We do not know \(\text{Var}(\hat \theta)\), but we can estimate it as \(\widehat{\text{Var}}(\hat \theta)\) using direct methods or the observed Fisher information, giving a test statistic \(W^*\):
\[ \begin{aligned} W \approx W^* =& ~ \frac{\hat \theta - \theta_0} {\sqrt{\widehat{\text{Var}}(\hat \theta)}} \end{aligned} \]
For large samples, if the null hypothesis is true, \(W^*\) will have an approximate Normal(0,1) distribution. That means we can compare the calculated \(W^*\) with a Normal(0,1) distribution. The approximate two-sided \(p\) arising from an observed value of \(W^*\) is:
\[ \begin{aligned} p_W = 2 \Phi (-|W^*|) \end{aligned} \]
where \(\Phi\) is the cumulative distribution function of the Normal(0,1) distribution. The ‘2’ is there because the test is two-sided.
9.3.2 Exercise set 9-5
- Compute the \(p\) value for the Wald test of the hypothesis that \(\beta = 0\) using the Anscombe data.
Use the least-squares slope to find \(\hat\beta\): \[ \begin{aligned} & \text{(eq. 3.9)} \\ \tilde\beta & = \frac{\sum_{i=1}^n (x_i - \bar x) (y_i - \bar y)} {\sum_{i=1}^n [(x_i - \bar x)^2]} \end{aligned} \]
Evaluate in R
:
x <- anscombe$x1
y <- anscombe$y1
n <- length(x)
xbar <- mean(x)
ybar <- mean(y)
b_hat <- sum((x - xbar)*(y - ybar)) / sum((x - xbar)^2)
Use the least-squares intercept to find \(\hat\alpha\): \[ \begin{aligned} & \text{(eq. 3.8)} \\ \tilde\alpha & = \bar y - \tilde\beta \bar x \end{aligned} \]
Evaluate in R
:
Use equation 9.14b to estimate the variance of the disturbance term (I don’t know how to create an upside down hat symbol..):
\[ \begin{aligned} \frac{n}{n-2} \widehat{\sigma^2} =& ~ \frac{1}{n-2} \sum_{i = 1}^n (Y_i - \hat\alpha - \hat\beta x_i)^2 \end{aligned} \]
Now estimate the variance of the slope using equation 9.12:
\[ \begin{aligned} & \text{(eq. 9.12)} \\ \text{Var}(\hat\beta) & = \frac{\sigma^2} {\sum_{i=1}^n [(x_i - \bar x)^2]} \end{aligned} \]
Now we plug these estimates into our equation for \(W^*\):
Calculate \(p\):
\[ \begin{aligned} p_W = 2 \Phi (-|W^*|) \end{aligned} \]
## [1] 1.110376e-05
## [1] 1.110376e-05
## [1] 2.220751e-05
Calculate \(T\):
# Left tail of t-distribution
# df = 11 observations minus two parameters that we estimated (alpha, beta)
2 * pt(q = -wald, df = 11-2)
## [1] 0.002169629
Compare with lm
:
##
## Call:
## lm(formula = y ~ x, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.92127 -0.45577 -0.04136 0.70941 1.83882
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.0001 1.1247 2.667 0.02573 *
## x 0.5001 0.1179 4.241 0.00217 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.237 on 9 degrees of freedom
## Multiple R-squared: 0.6665, Adjusted R-squared: 0.6295
## F-statistic: 17.99 on 1 and 9 DF, p-value: 0.00217
Notice that the reported t-value is equal to our calculated \(W^*\), and that the reported \(p\) uses the \(T\) distribution.
A random variable with a \(\chi^2\) distribution has the same distribution as the sum of the squares of \(k\) independent, Normal(0,1) random variables. We know that the asymptotic distribution of a maximum-likelihood estimator \(\hat\theta\) is normal, with the expectation equal to the true parameter value \(\theta\). If \(H_0\) is true, then \(W\) is Normal(0,1). If we evaluate \(W^2\), then its asymptotic distribution under \(H_0\) is \(\chi^2(1)\). (need to chew on this some more)
Examining Type 1 error and power of the Wald test when the significance level is \(\alpha = 0.05\).
# 10 pairs
beta_vec <- c(0, 0.1, 0.2)
n_datasets <- 1000
p_mat <- matrix(nrow = n_datasets, ncol = length(beta_vec))
n_pairs <- 10
for(i in 1:length(beta_vec)){
p_mat[,i] <- sim.Wald.B(n = n_pairs, nsim = n_datasets, a = 3, b = beta_vec[i])
}
# Create summary table
df_wald <- tibble(beta = c(0, 0.1, 0.2),
n_10 = colMeans(p_mat < 0.05))
# 50 pairs
n_pairs <- 50
for(i in 1:length(beta_vec)){
p_mat[,i] <- sim.Wald.B(n = n_pairs, nsim = n_datasets, a = 3, b = beta_vec[i])
}
# Add to summary table
df_wald <- df_wald %>%
mutate(n_50 = colMeans(p_mat < 0.05))
# 100 pairs
n_pairs <- 100
for(i in 1:length(beta_vec)){
p_mat[,i] <- sim.Wald.B(n = n_pairs, nsim = n_datasets, a = 3, b = beta_vec[i])
}
# Add to summary table
df_wald <- df_wald %>%
mutate(n_100 = colMeans(p_mat < 0.05))
library(knitr)
kable(df_wald, caption = "Proportion of significance tests for the estimated slope that were significant, where the true beta = 0, 0.1, 0.2 and the number of observations was 10, 50, 100.")
| beta | n_10 | n_50 | n_100 |
|---|---|---|---|
| 0.0 | 0.085 | 0.062 | 0.061 |
| 0.1 | 0.130 | 0.312 | 0.513 |
| 0.2 | 0.270 | 0.781 | 0.971 |
9.4 Parametric hypothesis testing using the likelihood-ratio test
Likelihood-ratio tests can be used to make joint inferences about several parameters at once. They accomplish this by comparing nested models. For example, consider the full model:
\[ \begin{aligned} Y_i & = \alpha + \beta_1 x_i + \beta_2 w_i + \beta_3 z_i \end{aligned} \]
In this full model, the free parameters are: \(\alpha, \beta_1, \beta_2, \beta_3\). If we give any of these parameters a fixed value - then the resulting model is considered to be nested within the full model. Often, we compare models where some of the coefficients are set to 0, indicating no effect. So, all of the following models can be considered to be nested within the full model:
\[ \begin{aligned} Y_i & = \alpha + \beta_3 z_i \\ Y_i & = \alpha + \beta_1 x_i + \beta_2 w_i \\ Y_i & = \alpha \end{aligned} \]
The likelihood-ratio test compares the maximum likelihood of two nested models, and keeps track of the difference in the number of free parameters (\(k\)) in the models. The test statistic \(\Lambda\) is calculated as:
\[ \begin{aligned} \Lambda & = 2 \text{ln} \frac{L(\hat\theta)}{L(\hat\theta_0)} \\ & = 2(l(\hat\theta) - l(\hat\theta_0)) \end{aligned} \]
where \(\hat\theta\) and \(\hat\theta_0\) represent the full (or more complex) model and the nested model with a hypothesized value, respectively. In other words, \(\hat\theta_0)\) represents a null hypothesis.
According to Wilks’ theorem, if the null hypothesis is true, the statistic \(\Lambda\) is asymptotically distributed as \(\chi^2 (k)\), where \(k\) is the number of parameters that are constrained in the nested model (representing the null hypothesis) but free in the full (comparison) model. The \(p\) value is the probability of obtaining a value of \(\Lambda\) as large or larger than the one observed given that the null hypothesis is true:
\[ \begin{aligned} p_{LRT} = 1 - F_{\chi^2(k)}(\Lambda) \end{aligned} \]
where \(F_{\chi^2(k)}\) is the cumulative distribution of the \(\chi^2\) distribution.
9.4.1 Exercise set 9-6
Answer on paper.
We wish to calculate the LRT, where the null hypothesis is that the slope is zero.
\[ \begin{aligned} \Lambda & = 2(l(\hat\theta) - l(\hat\theta_0)) \end{aligned} \]
Here is the formula for the log-likelihood using the least-square estimates.
\[ \begin{aligned} l(\mu) =& ~ n~\text{ln}(\frac{1}{\sigma \sqrt {2 \pi}}) - \frac{1}{2\sigma^2} \sum_{i = 1}^n (y_i - \alpha - \beta x_i)^2 \\ \end{aligned} \]
We can simplify this equation where \(\beta = 0\) and consequently \(\hat\alpha = \bar y\) (from the answer to 1a). Here is a function written by Edge to calculate the test statistic \(\Lambda\):
lr.stat.slr <- function(x, y){
n <- length(x)
#compute MLEs of beta and alpha
B.hat <- (sum(x*y)-sum(x)*sum(y)/n)/( sum(x^2) - sum(x)^2/n)
A.hat <- (sum(y) - B.hat*sum(x))/n
#Compute estimated variance of MLE of beta
vhat <- sum((y - A.hat - B.hat*x)^2)/(n-2)
#likelihood-ratio statistic
lr <- (sum((y - mean(y))^2) - sum((y - A.hat - B.hat*x)^2))/vhat
return(lr)
}
Lambda <- lr.stat.slr(x, y)
And we’ll calculate \(p\) using \(k = 1\) because we have fixed one parameter:
## [1] 2.220751e-05
Compare with the Wald test:
## [1] 2.220751e-05
This give the same answer
In addition, here I use the anova
function in R to do the likelihood ratio test on two linear models, one with and the other without slope term.
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.92127 -0.45577 -0.04136 0.70941 1.83882
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.0001 1.1247 2.667 0.02573 *
## x 0.5001 0.1179 4.241 0.00217 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.237 on 9 degrees of freedom
## Multiple R-squared: 0.6665, Adjusted R-squared: 0.6295
## F-statistic: 17.99 on 1 and 9 DF, p-value: 0.00217
##
## Call:
## lm(formula = y ~ 1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.2409 -1.1859 0.0791 1.0691 3.3391
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.5009 0.6125 12.25 2.41e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.032 on 10 degrees of freedom
## Analysis of Variance Table
##
## Model 1: y ~ x
## Model 2: y ~ 1
## Res.Df RSS Df Sum of Sq F Pr(>F)
## 1 9 13.763
## 2 10 41.273 -1 -27.51 17.99 0.00217 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Notice that the test statistic is the same, but the \(p\) value differs, because it appears to use the F distribution instead of the \(\chi^2\) distribution for the hypothesis test.


--- layout: post title: 'Worthy cause: community radio bid' date: 2004-10-22 00:00:00 category: personal ---
I've just been given permission to quote this please for help here:
"Earshot is leading on a licence application to Ofcom for RadioReverb to
become a full-time community radio station for Brighton. RadioReverb is the
station which was on air during the festival last May and had involvement
from over 150 people. With a fulltime licence, RadioReverb will be able to
build links to all parts of the community and encourage participation by
community groups in producing programming and all aspects of running the
station.
It is intended that the community radio station should provide benefits
which extend beyond broadcasting. Training, empowerment, regeneration and
off-air communication between community groups will form a vital part of the
radio station operations and as such we hope to have a number of satellite
studios based around the city in schools and other venues.
With a tight November 23rd deadline for licence applications, Earshot is
currently in consultation with a number of interested groups and individuals
in Brighton and Hove on behalf of RadioReverb and we would appreciate your
input and support.
RadioReverb is currently run entirely by volunteers and broadcasts live on
University Radio Falmer on 1431AM every Saturday from our studios in North
Street.
You can get more information by emailing us at<EMAIL_ADDRESS>phoning us on 01273 323040 or by downloading an information document and
consultation form from the Earshot website at www.earshot.org.uk.
Letters of support will need to be on letterhead and should include
information on how you intend to make use of and benefit from RadioReverb.
They should be addressed to:
The Chair,
RadioReverb,
c/o Earshot
170 North St.,
Brighton. BN1 1EA
Thanks for your time.
Karen Cass
Director
Earshot"


22 Comparing Proportions with Two Independent Samples
Often, when analyzing data from an independent samples design, we are interested in comparing the proportions of subjects that achieve some outcome, across two levels of an exposure, or treatment. In this circumstance, we will first summarize the available data in terms of a 2x2 cross-tabulation, and then apply a series of inferential methods for 2x2 tables to obtain point and interval estimates of interest.
22.1 Loading the Source Code for Love-boost.R
source("data/Love-boost.R")
We’ll use this for the twobytwo
function I’ll demonstrate shortly.
22.2 A First Example: Ibuprofen and Sepsis Trial
As we saw in a previous Chapter, we are interested in comparing the percentage of patients in each arm of the trial (Ibuprofen vs. Placebo) that showed an improvement in their temperature (temp_drop
> 0). Our primary interest is in comparing the percentage of Ibuprofen patients whose temperature dropped to the percentage of Placebo patients whose temperature dropped.
- We can summarize the data behind the two proportions we are comparing in a contingency table with two rows which identify the exposure or treatment of interest, and two columns to represent the outcomes of interest.
- In this case, we are comparing two groups of subjects based on treatment (
treat
): those who received Ibuprofen and those who received a placebo. The outcome of interest is whether the subject’s temperature dropped (temp_drop
> 0), or not. - In the table, we place the frequency for each combination of a row and a column.
- The rows need to be mutually exclusive and collectively exhaustive: each patient must either receive Ibuprofen or Placebo. Similarly, the columns must meet the same standard: every patient’s temperature either drops or does not drop.
Here’s the contingency table.
sepsis <- read_csv("data/sepsis.csv",
show_col_types = FALSE) %>%
mutate(treat = factor(treat),
race = factor(race))
sepsis <- sepsis %>%
mutate(dropped = ifelse(temp_drop > 0, "Drop", "No Drop"))
sepsis %>% tabyl(treat, dropped) %>%
adorn_totals() %>%
adorn_percentages(denom = "row") %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns(position = "front")
treat Drop No Drop
Ibuprofen 107 (71.3%) 43 (28.7%)
Placebo 80 (53.3%) 70 (46.7%)
Total 187 (62.3%) 113 (37.7%)
In our sample, we observe 71.3% of the 150 Ibuprofen subjects with a positive temp_drop
as compared to 53.3% of the 150 Placebo subjects. We want to compare these two probabilities (represented using proportions as 0.713 vs. 0.533) to estimate the size of the difference between the proportions with a point estimate and 90% confidence interval.
But there are other comparisons we could make, too. The tricky part is that we have multiple ways to describe the relationship between treatment and outcome. We might compare outcome “risks” directly using the difference in probabilities, or the ratio of the two probabilities, or we might convert the risks to odds, and compare the ratio of those odds. In any case, we’ll get different point estimates and confidence intervals, all of which will help us make conclusions about the evidence available in this trial speaking to differences between Ibuprofen and Placebo.
22.3 Relating a Treatment to an Outcome
The question of interest is whether the percentage of subjects whose temperature dropped is different (and probably larger) in the subjects who received Ibuprofen than in those who received the Placebo.
| Treatment Arm | Dropped | Did Not Drop | Total | Proportion who dropped |
|---|---|---|---|---|
| Ibuprofen | 107 | 43 | 150 | 0.713 |
| Placebo | 80 | 70 | 150 | 0.533 |
In other words, what is the relationship between the treatment and the outcome?
22.4 Definitions of Probability and Odds
- Proportion = Probability = Risk of the trait = number with trait / total
- Odds of having the trait = (number with the trait / number without the trait) to 1
If p is the proportion of subjects with a trait, then the odds of having the trait are \(\frac{p}{1-p}\) to 1.
So, the probability of a good result (temperature drop) in this case is \(\frac{107}{150} = 0.713\) in the Ibuprofen group. The odds of a good result are thus \(\frac{0.713}{1 - 0.713} = 2.484\) to 1 in the Ibuprofen group.
| Treatment Arm | Dropped | Did Not Drop | Total | Pr(dropped) | Odds(dropped) |
|---|---|---|---|---|---|
| Ibuprofen | 107 | 43 | 150 | 0.713 | 2.484 |
| Placebo | 80 | 70 | 150 | 0.533 | 1.141 |
22.5 Defining the Relative Risk
Among the Ibuprofen subjects, the risk of a good outcome (drop in temperature) is 71.3% or, stated as a proportion, 0.713. Among the Placebo subjects, the risk of a good outcome is 53.3% or, stated as a proportion, 0.533.
Our “crude” estimate of the relative risk of a good outcome for Ibuprofen subjects as compared to Placebo subjects, is the ratio of these two risks, or 0.713/0.533 = 1.338
- The fact that this relative risk is greater than 1 indicates that the probability of a good outcome is higher for Ibuprofen subjects than for Placebo subjects.
- A relative risk of 1 would indicate that the probability of a good outcome is the same for Ibuprofen subjects and for Placebo subjects.
- A relative risk less than 1 would indicate that the probability of a good outcome is lower for Ibuprofen subjects than for Placebo subjects.
22.6 Defining the Risk Difference
Our “crude” estimate of the risk difference of a good outcome for Ibuprofen subjects as compared to Placebo subjects, is 0.713 - 0.533 = 0.180 or 18.0 percentage points.
- The fact that this risk difference is greater than 0 indicates that the probability of a good outcome is higher for Ibuprofen subjects than for Placebo subjects.
- A risk difference of 0 would indicate that the probability of a good outcome is the same for Ibuprofen subjects and for Placebo subjects.
- A risk difference less than 0 would indicate that the probability of a good outcome is lower for Ibuprofen subjects than for Placebo subjects.
22.7 Defining the Odds Ratio, or the Cross-Product Ratio
Among the Ibuprofen subjects, the odds of a good outcome (temperature drop) are 2.484. Among the placebo subjects, the odds of a good outcome (temperature drop) are 1.141.
So our “crude” estimate of the odds ratio of a good outcome for Ibuprofen subjects as compared to placebo subjects, is 2.484 / 1.141 = 2.18
Another way to calculate this odds ratio is to calculate the cross-product ratio, which is equal to (a x d) / (b x c), for the 2 by 2 table with counts specified as shown:
| A Generic Table | Good Outcome | Bad Outcome |
|---|---|---|
| Treatment Group 1 | a | b |
| Treatment Group 2 | c | d |
So, for our table, we have a = 107, b = 43, c = 80, and d = 70, so the cross-product ratio is \(\frac{107 x 70}{43 x 80} = \frac{7490}{3440} = 2.18\). As expected, this is the same as the “crude” odds ratio estimate.
- The fact that this odds ratio risk is greater than 1 indicates that the odds of a good outcome are higher for Ibuprofen subjects than for Placebo subjects.
- An odds ratio of 1 would indicate that the odds of a good outcome are the same for Ibuprofen subjects and for Placebo subjects.
- An odds ratio less than 1 would indicate that the odds of a good outcome are lower for Ibuprofen subjects than for Placebo subjects.
So, we have several different ways to compare the outcomes across the treatments. Are these differences and ratios large enough to rule out chance?
22.8 Comparing Rates in a 2x2 Table
What is the relationship between the treatment (Ibuprofen vs. Placebo) and the outcome (drop in temperature) in the following two-by-two table?
22.9 The twobytwo
function in R
I built the twobytwo
function in R (based on existing functions in the Epi
library, which you need to have in your installed packages list in order for this to work) to do the work for this problem. All that is required is a single command, and a two-by-two table in standard epidemiological format (with the outcomes in the columns, and the treatments in the rows.)
| Treatment Arm | Dropped | Did Not Drop |
|---|---|---|
| Ibuprofen | 107 | 43 |
| Placebo | 80 | 70 |
The command just requires you to read off the cells of the table, followed by the labels for the two treatments, then the two outcomes, then a specification of the names of the rows (exposures) and columns (outcomes) from the table, and a specification of the confidence level you desire. We’ll use 90% here.
The resulting output follows.
twobytwo(107, 43, 80, 70,
"Ibuprofen", "Placebo", "Dropped", "No Drop",
conf.level = 0.90)
2 by 2 table analysis:
------------------------------------------------------
Outcome : Dropped
Comparing : Ibuprofen vs. Placebo
Dropped No Drop P(Dropped) 90% conf. interval
Ibuprofen 107 43 0.7133 0.6490 0.7701
Placebo 80 70 0.5333 0.4661 0.5993
90% conf. interval
Relative Risk: 1.3375 1.1492 1.5567
Sample Odds Ratio: 2.1773 1.4583 3.2509
Conditional MLE Odds Ratio: 2.1716 1.4177 3.3437
Probability difference: 0.1800 0.0881 0.2677
Exact P-value: 0.0019
Asymptotic P-value: 0.0014
------------------------------------------------------
22.9.1 Standard Epidemiological Format
This table is in standard epidemiological format, which means that:
- The rows of the table describe the “treatment” (which we’ll take here to be
treat
). The more interesting (sometimes also the more common) “treatment” is placed in the top row. That’s Ibuprofen here. - The columns of the table describe the “outcome” (which we’ll take here to be whether the subject’s temperature dropped.) Typically, the more common “outcome” is placed to the left.
22.9.2 Outcome Probabilities and Confidence Intervals Within the Treatment Groups
The twobytwo
output starts with estimates of the probability (risk) of a “Dropped” outcome among subjects who fall into the two treatment groups (Ibuprofen or Placebo), along with 90% confidence intervals for each of these probabilities.
2 by 2 table analysis:
------------------------------------------------------
Outcome : Dropped
Comparing : Ibuprofen vs. Placebo
Dropped No Drop P(Dropped) 90% conf. interval
Ibuprofen 107 43 0.7133 0.6490 0.7701
Placebo 80 70 0.5333 0.4661 0.5993
The conditional probability of a temperature drop given that the subject is in the Ibuprofen group, is symbolized as Pr(Dropped | Ibuprofen) = 0.7133, and the 90% confidence interval around that proportion is (0.6490, 0.7701).
- Note that these two confidence intervals fail to overlap, and so we expect to see a fairly large difference in the estimated probability of a temperature drop when we compare Ibuprofen to Placebo.
22.9.3 Relative Risk, Odds Ratio and Risk Difference, with Confidence Intervals
These elements are followed by estimates of the relative risk, odds ratio, and risk difference, each with associated 90% confidence intervals.
90% conf. interval
Relative Risk: 1.3375 1.1492 1.5567
Sample Odds Ratio: 2.1773 1.4583 3.2509
Conditional MLE Odds Ratio: 2.1716 1.4177 3.3437
Probability difference: 0.1800 0.0881 0.2677
- The relative risk, or the ratio of P(Temperature Drop | Ibuprofen) to P(Temperature Drop | Placebo), is shown first. Note that the 90% confidence interval is entirely greater than 1.
- The odds ratio is presented using two different definitions (the sample odds ratio is the cross-product ratio we mentioned earlier). Note that the 90% confidence interval using either approach is entirely greater than 1.
- The probability (or risk) difference [P(Temperature Drop | Ibuprofen) - P(Temperature Drop | Placebo)] is presented last. Note that the 90% confidence interval is entirely greater than 0.
- Note carefully that if there had been no difference between Ibuprofen and Placebo, the relative risk and odds ratios would be 1, but the probability difference would be zero.
22.10 Estimating a Rate More Accurately: Use (x + 2)/(n + 4) rather than x/n
Suppose you have some data involving n independent tries, with x successes. A natural estimate of the “success rate” in the data is x / n.
But, strangely enough, it turns out this isn’t an entirely satisfying estimator. Alan Agresti provides substantial motivation for the (x + 2)/(n + 4) estimate as an alternative. This is sometimes called a Bayesian augmentation.
- The big problem with x / n is that it estimates p = 0 or p = 1 when x = 0 or x = n.
- It’s also tricky to compute confidence intervals at these extremes, since the usual standard error for a proportion, \(\sqrt{n p (1-p)}\), gives zero, which isn’t quite right.
- (x + 2)/(n + 4) is much cleaner, especially when you build a confidence interval for the rate.
- The only place where (x + 2)/(n + 4) will go wrong is if n is small and the true probability is very close to 0 or 1.
For example, if n = 10, and p is 1 in a million, then x will almost certainly be zero, and an estimate of 1/12 is much worse than the simple 0/10. However, how big a deal is this? If p might be 1 in a million, you’re not going to estimate it with a n = 10 experiment.
Applying this method to our Ibuprofen and Sepsis Trial data, we would simply add two to each frequency in the main four cells in our 2x2 table.
So instead of using
twobytwo(107, 43, 80, 70,
"Ibuprofen", "Placebo", "Dropped", "No Drop",
conf.level = 0.90)
the Bayesian augmentation would encourage us to look at
twobytwo(109, 45, 82, 72,
"Ibuprofen", "Placebo", "Dropped", "No Drop",
conf.level = 0.90)
2 by 2 table analysis:
------------------------------------------------------
Outcome : Dropped
Comparing : Ibuprofen vs. Placebo
Dropped No Drop P(Dropped) 90% conf. interval
Ibuprofen 109 45 0.7078 0.6441 0.7643
Placebo 82 72 0.5325 0.4662 0.5977
90% conf. interval
Relative Risk: 1.3293 1.1434 1.5453
Sample Odds Ratio: 2.1268 1.4337 3.1550
Conditional MLE Odds Ratio: 2.1215 1.3950 3.2421
Probability difference: 0.1753 0.0845 0.2622
Exact P-value: 0.0022
Asymptotic P-value: 0.0016
------------------------------------------------------
As you can see, the odds ratio and relative risk estimates are (a little) closer to 1, and the probability difference is also a little closer to 0. The Bayesian augmentation provides a slightly more conservative set of estimates of the impact of Ibuprofen as compared to Placebo.
It is likely that the augmented version is a more accurate estimate here, but the two estimates will be comparable, generally, so long as either (a) the sample size in each exposure group is more than, say, 30 subjects, and/or (b) the sample probability of the outcome is between 10% and 90% in each exposure group.
22.11 A Second Example: Ebola Virus Disease Study, again
For instance, recall the Ebola Virus Disease study from the New England Journal of Medicine that we described in the previous Chapter. Suppose we want to compare the proportion of deaths among cases that had a definitive outcome who were hospitalized to the proportion of deaths among cases that had a definitive outcome who were not hospitalized.
The article suggests that of the 1,737 cases with a definitive outcome, there were 1,153 hospitalized cases. Across those 1,153 hospitalized cases, 741 people (64.3%) died, which means that across the remaining 584 non-hospitalized cases, 488 people (83.6%) died.
Here is the initial contingency table, using only the numbers from the previous paragraph.
| Initial Ebola Table | Deceased | Alive | Total |
|---|---|---|---|
| Hospitalized | 741 | – | 1153 |
| Not Hospitalized | 488 | – | 584 |
| Total | 1737 |
Now, we can use arithmetic to complete the table, since the rows and the columns are each mutually exclusive and collectively exhaustive.
| Ebola 2x2 Table | Deceased | Alive | Total |
|---|---|---|---|
| Hospitalized | 741 | 412 | 1153 |
| Not Hospitalized | 488 | 96 | 584 |
| Total | 1229 | 508 | 1737 |
We want to compare the fatality risk (probability of being in the deceased column) for the population of people in the hospitalized row to the population of people in the not hospitalized row.
We can run these data through R, using the Bayesian augmentation (adding a death and a survival to the hospitalized and also to the not hospitalized groups.) We’ll use a 95% confidence level this time.
twobytwo(741+2, 412+2, 488+2, 96+2,
"Hospitalized", "Not Hospitalized", "Deceased", "Alive",
conf.level = 0.95)
2 by 2 table analysis:
------------------------------------------------------
Outcome : Deceased
Comparing : Hospitalized vs. Not Hospitalized
Deceased Alive P(Deceased) 95% conf.
Hospitalized 743 414 0.6422 0.6141
Not Hospitalized 490 98 0.8333 0.8010
interval
Hospitalized 0.6693
Not Hospitalized 0.8613
95% conf. interval
Relative Risk: 0.7706 0.7285 0.8151
Sample Odds Ratio: 0.3589 0.2801 0.4599
Conditional MLE Odds Ratio: 0.3591 0.2772 0.4624
Probability difference: -0.1912 -0.2307 -0.1490
Exact P-value: 0.0000
Asymptotic P-value: 0.0000
------------------------------------------------------
I’ll leave it as an exercise for you to interpret these results and draw some conclusions.


You are receiving this because you are register as manager on peex mobile money server
Peex Mobile Money
Modem : {{ modem }} is {{ state }}
The modem named {{ modem }} is {{ state }} since {{ when }} please check it ( internet
connection ; electricity ; websocket client)


Create movable memory objects using the create([$data]) method of the memory manager:
1 | $memObject = $memoryManager->create($data);
|
“Movable” means that such objects may be swapped and unloaded from memory and then loaded when application code accesses the object.
Create locked memory objects using the createLocked([$data]) method of the memory manager:
1 | $memObject = $memoryManager->createLocked($data);
|
“Locked” means that such objects are never swapped and unloaded from memory.
Locked objects provides the same interface as movable objects (Zend\Memory\Container\Interface). So locked object can be used in any place instead of movable objects.
It’s useful if an application or developer can decide, that some objects should never be swapped, based on performance considerations.
Access to locked objects is faster, because the memory manager doesn’t need to track changes for these objects.
The locked objects class (Zend\Memory\Container\Locked) guarantees virtually the same performance as working with a string variable. The overhead is a single dereference to get the class property.
Use the memory container (movable or locked) ‘value‘ property to operate with memory object data:
1 2 3 4 5 6 7 8 9 10 11 | $memObject = $memoryManager->create($data);
echo $memObject->value;
$memObject->value = $newValue;
$memObject->value[$index] = '_';
echo ord($memObject->value[$index1]);
$memObject->value = substr($memObject->value, $start, $length);
|
An alternative way to access memory object data is to use the getRef() method. This method must be used for PHP versions before 5.2. It also may have to be used in some other cases for performance reasons.
Memory container provides the following methods:
1 | public function &getRef();
|
The getRef() method returns reference to the object value.
Movable objects are loaded from the cache at this moment if the object is not already in memory. If the object is loaded from the cache, this might cause swapping of other objects if the memory limit would be exceeded by having all the managed objects in memory.
The getRef() method must be used to access memory object data for PHP versions before 5.2.
Tracking changes to data needs additional resources. The getRef() method returns reference to string, which is changed directly by user application. So, it’s a good idea to use the getRef() method for value data processing:
1 2 3 4 5 6 7 8 | $memObject = $memoryManager->create($data);
$value = &$memObject->getRef();
for ($count = 0; $count < strlen($value); $count++) {
$char = $value[$count];
...
}
|
1 | public function touch();
|
The touch() method should be used in common with getRef(). It signals that object value has been changed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $memObject = $memoryManager->create($data);
...
$value = &$memObject->getRef();
for ($count = 0; $count < strlen($value); $count++) {
...
if ($condition) {
$value[$count] = $char;
}
...
}
$memObject->touch();
|
1 | public function lock();
|
The lock() methods locks object in memory. It should be used to prevent swapping of some objects you choose. Normally, this is not necessary, because the memory manager uses an intelligent algorithm to choose candidates for swapping. But if you exactly know, that at this part of code some objects should not be swapped, you may lock them.
Locking objects in memory also guarantees that reference returned by the getRef() method is valid until you unlock the object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $memObject1 = $memoryManager->create($data1);
$memObject2 = $memoryManager->create($data2);
...
$memObject1->lock();
$memObject2->lock();
$value1 = &$memObject1->getRef();
$value2 = &$memObject2->getRef();
for ($count = 0; $count < strlen($value2); $count++) {
$value1 .= $value2[$count];
}
$memObject1->touch();
$memObject1->unlock();
$memObject2->unlock();
|
1 | public function unlock();
|
unlock() method unlocks object when it’s no longer necessary to be locked. See the example above.
1 | public function isLocked();
|
The isLocked() method can be used to check if object is locked. It returns TRUE if the object is locked, or FALSE if it is not locked. This is always TRUE for “locked” objects, and may be either TRUE or FALSE for “movable” objects.
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.


---
title: 'txt2re: Regular Expression Generator'
date: '2011-10-10T03:56:00.000-07:00'
tags:
- Regex
- Utilities
modified_time: '2011-10-10T05:14:47.941-07:00'
blogger_id: tag:blogger.com,1999:blog-3456458686961244994.post-7064012350422304264
blogger_orig_url: http://blog.kieranties.com/2011/10/txt2re-regular-expression-generator.html
redirect_from: "/2011/10/txt2re-regular-expression-generator.html"
---
[Link]
A pretty awesome page to help when creating regular expressions. Type in the string you want to match, then use the selectors to create a snippet of code with the regex and validation process pre-populated, allowing you to drop the code straight into your application. (full credit to Zoe Nolan for the link!)
This is a cracking starting point for some simple regex work (or when your head just isn't wrapping itself around the joys of regex), but for more detailed help, pointers and tips I'd suggest taking a look at the following:


--- layout: page title: Contact permalink: /contact/ carousel: - image: caption: link: _options: layout: hidden: true image: width: 1200 height: 1200 resize_style: contain mime_type: image/jpeg _comments: carousel: Edit the slideshow link: Optional web link from the slide caption: optional caption ---
John welcomes exploratory, no-obligation approaches from organisations and individuals about their specific needs.
Rates are competitive with lower rates for smaller NGOs/charities.
Contact him by email using the form below,
or telephone: +44 (0)1865 762393
Skype: johncammack Get Skype and call John for free


"However, while I found the tutorials easy to follow and to complete, I found it very hard to start my own work."
“I [had] a very good time writing simple apps. But when I tried something more complex, I immediately run into a wall."
"it is easy at the beginning, but is becomes rapidly much harder when the problems complexity arises."


Installation
Download
Ikube is available for download in the Artifactory. As a web application ikube is available as a war file that can be installed on any Java server, Tomcat, FireFly, etc. As well as the war application file is a directory of configuration files. These files contain the core configuration of ikube, and optionally the indexes and jobs that are defined by the user. Please navigate to the Artifacts tab in the Artifactory, then expand libs-releases-local, the ikube, then ikube-war, and select the latest release. Alternatively you can get the latest snapshot release in the libs-snapshot-local repository. Click on the version of the war, then in the right panel click the download button.
Similarly the base configuration is in the ikube-libs folder. Again download the latest jar(Java zip file). For this installation we wil user a Tomcat server. Please download a Tomcat zip and unpack it to a convenient place on your computer, could all be in the Downloads folder.
Install
Rename the ikube-war-xxx-xxx.war to ikube.war, it will make the web access easier, but not essential. Create a folder in the $TOMCAT_UNPACKED_FOLDER/bin called ikube. The directory structure is important for now. Unpack the ikube-libs-xxx-xxx.jar using WinRar or unzip feature, into the $TOMCAT_UNPACKED_FOLDER/bin/ikube folder.
Configure
If your ip address is something other than 192.168.1.x please change the interface for the grid. We’ll get into the grid mechanism in a later document. In the folder $TOMCAT_UNPACKED_FOLDER/bin/ikube/common open a file called spring.properties. Go to the bottom of the file and find the property called hz-tcp-ip-interface, change the value to your ip address. For you operating system you can use ifconfig or ipconfig to find your ip address, from the command line.
Go to the $TOMCAT_UNPACKED_FOLDER/bin folder and execute either the startup.sh or startup.bat depending on your operating system. Note that you may need to change the permissions of the scripts on Linux to make them executable but issuing the command ⇒ chmod 777 -R * in the Tomcat root directory, i.e. $TOMCAT_UNPACKED_FOLDER.
Verify
Go to the Administration Console. The login credentials are user/user or administrator/administrator. Please refer to the gui documentation for functionality and options in the gui.
Deployer
…


The concept behind this panoramic tour is very simple. We are using three.js JavaScript 3D library to wrap a spherical panoramic image around a 3D sphere.
Then we define a scene and add a camera to display the the applied image texture to our sphere.
In order for our demo to look like the way it is, we place the camera inside the sphere, and by applying a very subtle rotation to the camera we make it seem like that the object is rotating.
Then we add some mouse events to the element that we have injected our scene into, to correspond to new camera X, Y, and Z positions.
We also bound the mouse wheel (scroll) event to our camera's field of view to add the zoom effect.
twitter: @NorikDavtian
github: Download Source
license: MIT


EventEmitter
@objc open class EventEmitter: NSObject
Base for classes which generate events to which one can bind to.
Version
1.0.0-
Removes a listener associated with an event. At most one listener is removed, if the same listener has been added multiple times, only the first listener is removed.
Version
1.0.0
Declaration
Swift
final public func off( _ eventType: String, target: AnyObject? = nil, callback: Selector? = nil)
Parameters
eventType
The event type from which to remove the listener(s).
target
The target that should be removed. If omitted, all listeners for the event type are removed.
callback
The callback that should be removed. If omitted, all listeners for the target are removed.
-
Adds a new listener for an event. All listeners will be called in the order in which they were added. Adding an already added listener is not an error and will result in the listener being called multiple times.
Version
1.0.0
Declaration
Swift
final public func on( _ eventType: String, target: AnyObject, callback: Selector)
Parameters
eventType
The event to listen for.
target
An owner of the selector that will be called whenever the event is emitted.
callback
A selector that will be called whenever the event is emitted.
-
Works like on, but the listener is at most called once, and then removed.
Version
1.0.0
Declaration
Swift
final public func once( _ eventType: String, target: AnyObject, callback: Selector)
Parameters
eventType
The event to listen for.
target
An owner of the selector that will be called whenever the event is emitted.
callback
A selector that will be called whenever the event is emitted.
-
Emits an event of given type for every listener associated with the event.
Version
1.0.0
Declaration
Swift
final public func emit( _ eventType: String, _ value: Any! = nil)
Parameters
eventType
The event type for which to emit the event.
value
Optional argument to pass to the listeners associated with the event.


Set response http status code
Description
Mock Successful status
/cart
201
<ms:mock> <ms:request> <ms:post>/cart</ms:post> </ms:request> <ms:response> <ms:statusCode class="code">201</ms:statusCode> </ms:response> </ms:mock>
When I send
/cart
Then I should receive
HTTP/1.1 201 Created


Kyle I S Harrington /<EMAIL_ADDRESS>Some slides adapted from Geoff Hinton and David Touretzky
Linear weighting of N-dimensional instances
$y = \vec{w} \cdot \vec{x} + b$
where $| \vec{x} | = N$, $b$ is the intecept, and $\vec{w}$ are real-valued weights.
Multiple layers of linear units do not improve performance
$\vec{y} = V * ( U * \vec{x} ) = ( V * U ) * \vec{x}$
The weights from both layers, U and V, are equivalent to a single layer, $W = V * U$
We suspect we need multiple layers to support higher-order interactions between inputs, what type of units would be better?
Activation function is applied sum of weighted inputs: $\sigma( \displaystyle \sum_i w_i x_i ) = y$
Logistic: $f(x) = \frac{1}{1+e^-x}, f(x)' = f(x) (1 - f(x))$
Alternatively hyperbolic tangent: $f(x) = tanh(x), f(x)' = \frac{1}{cosh(x)^2}$
|
|
Convolutional neural networks perform better at image labeling tasks (object detection) with greater depth. |
Left, network on generation 1, and right, network on generation 765, for an example finite-state problem.
Angeline, P.J., Saunders, G.M. and Pollack, J.B., 1994. An evolutionary algorithm that constructs recurrent neural networks. Neural Networks, IEEE Transactions on, 5(1), pp.54-65.
|
Problem: Make a 4-legged robot walk as far/fast as possible
| The robot. |
The control NN. |
A hyper-neural network (HNN) takes coordinates for each weight in a network and returns the weight's value.
$HNN(N_{i},N_{o}) = w_{i,o}$, where $N_i$ and $N_o$ are in/out neurons
Stanley, K.O., D'Ambrosio, D.B. and Gauci, J., 2009. A hypercube-based encoding for evolving large-scale neural networks. Artificial life, 15(2), pp.185-212.Left, weights are generated by a hyper-NN, and right, weights are optimized directly.
Clune, J., Stanley, K.O., Pennock, R.T. and Ofria, C., 2011. On the performance of indirect encoding across the continuum of regularity. Evolutionary Computation, IEEE Trans. on, 15(3), pp.346-367.