ANP Row Sensitivity

All ANP row sensitivity calculations are in this module.

pyanp.rowsens.calcp0(mat, row, cluster_nodes, orig, p0mode)[source]

Calculates the p0, or resting, value for the row sensitivity

Parameters:
  • mat – The matrix to do row sensitivity on
  • row – The row to do row sensitivity on
  • cluster_nodes – The indices of the other nodes in the cluster that row is in. Used for inluence_marginal() if p0mode is an integer meaning smart mode for alt=p0mode :param orig: The original weight, used if p0mode is not an integer (meaning smart) or a float (meaning a direct p0 value).
  • p0mode – This controls the calculation and has 3 cases: Case 1: if it is a float, you are directly setting the p0 value to whatever p0mode is. Case 2: if it is an integer, this is the smart p0 mode, and it treats p0mode as the index of the alternative/node to make continuous. Case 3: otherwise we assume you want original weights to be the p0 value, and return the parameter orig
Returns:

The p0 or resting value, see the p0mode parameter for more information

pyanp.rowsens.influence_fixed(mat, row, cluster_nodes=None, influence_nodes=None, delta=0.25, p0mode=0.5, limit_matrix_calc=<function calculus>, node_names=None, idealize=False)[source]

Calculates fixed influence, i.e. we do row sensitivity and calculate the difference

Parameters:
  • mat – The scaled supermatrix to perform the calculation on
  • row – The row to use for anp row sensitivity, or list of rows.
  • cluster_nodes – If you wish to normalize by cluster, this should be the indices of the nodes that are in row’s cluster (including row itself).
  • influence_nodes – The indices of the nodes to calculate the influence of, with respect to row. If None it calculates the influence of all nodes other than row.
  • delta – How much to change from p0 for the fixed influence
  • p0mode – This controls the calculation and has 3 cases: Case 1: if it is a float, you are directly setting the p0 value to whatever p0mode is. Case 2: if it is an integer, this is the smart p0 mode, and it treats p0mode as the index of the alternative/node to make continuous. Case 3: otherwise we assume you want original weights to be the p0 value, and return the parameter orig
  • limit_matrix_calc
  • node_names – If not None, it gives us a list of names to use for our indices for the returning dataframe or series.
  • idealize – If True, the influence_node scores are idealized, else they are normalized
Returns:

A pandas.Series whose index is influence_nodes and whose values are the influence scores of those nodes with respect to the row, if row is a single entry. If the row param is a list, it returns a pandas.Dataframe whose rows are the influence_nodes, columns are the rows, and an additional row at the end for the total influence.

pyanp.rowsens.influence_limit(mat, row, cluster_nodes=None, influence_nodes=None, delta=1e-06, p0mode=0.5, limit_matrix_calc=<function calculus>)[source]

Calculates the limit influence score of the influence_nodes with respect to row.

Parameters:
  • mat – The scaled supermatrix to perform the calculation on
  • row – The row to use for anp row sensitivity
  • cluster_nodes – If you wish to normalize by cluster, this should be the indices of the nodes that are in row’s cluster (including row itself).
  • influence_nodes – The indices of the nodes to calculate the influence of, with respect to row. If None it calculates the influence of all nodes other than row.
  • delta – We use 1-delta for the p-value to plugin to approximate the limit as p -> 1
  • p0mode – This controls the calculation and has 3 cases: Case 1: if it is a float, you are directly setting the p0 value to whatever p0mode is. Case 2: if it is an integer, this is the smart p0 mode, and it treats p0mode as the index of the alternative/node to make continuous. Case 3: otherwise we assume you want original weights to be the p0 value, and return the parameter orig
  • limit_matrix_calc – A function which takes a single input, the matrix to take the limit of.
Returns:

A tuple of 2 items, the first is a pandas.Series whose indices are ‘Node 1’, ‘Node 2’ (and the indices after “Node ” are the influence_node indices) and whose values are the limit value. The second element of the returned tuple is a pandas.Series with the same indices and whose values are the p0 values we used for that alternative.

pyanp.rowsens.influence_marginal(mat, row, influence_nodes=None, cluster_nodes=None, left_or_right=None, delta=1e-06, p0mode=0.5, limit_matrix_calc=<function calculus>)[source]

Calculates the marginal influence

Parameters:
  • mat – The scaled supermatrix to calculate marginal influence on
  • row – The index of the row to perform the marginal influence on
  • influence_nodes – The nodes to calculate the marginal influence of the row upon, if None then it assumes all nodes except row.
  • cluster_nodes – The other nodes in the parameter row’s cluster (including row itself), so we can scale by cluster. If None we do not scale by cluster.
  • left_or_right – An integer telling whether we should do left-hand side derivative, right-hand side derivative or average them. If left_or_right < 0, then we do LHS deriv. If left_or_right > 0, we do RHS deriv. Finally, if left_or_right == 0, we average LHS and RHS.
  • delta – The delta_x to use for the derivative calculation.
  • p0mode – This controls the calculation and has 3 cases: Case 1: if it is a float, you are directly setting the p0 value to whatever p0mode is. Case 2: if it is an integer, this is the smart p0 mode, and it treats p0mode as the index of the alternative/node to make continuous. Case 3: otherwise we assume you want original weights to be the p0 value, and return the parameter orig
  • limit_matrix_calc – A function which takes a single input, the matrix to take the limit of.
Returns:

A pandas.Series whose indices are influence_nodes and whose values are the marginal influence scores of those nodes with respect to the given row.

pyanp.rowsens.influence_perspective(matrix, rows, cluster_nodes=None, influence_nodes=None, limit_matrix_calc=<function calculus>, graph=True, node_names=None, show_diffs=False, idealize=False, p=0.99999)[source]

Calculates the direct influence score, i.e. it calculates anp row sensitivity for each of pvals values and stores the new scores of the influence_nodes.

Parameters:
  • mat – The scaled supermatrix to perform the calculation on
  • rows – The rows to use for anp row sensitivity and will be the columns of the result
  • cluster_nodes – If you wish to normalize by cluster, this should be the indices of the nodes that are in row’s cluster (including row itself).
  • influence_nodes – The indices of the nodes to calculate the influence of, with respect to row. If None it calculates the influence of all nodes other than row.
  • limit_matrix_calc – A function which takes a single input, the matrix to take the limit of.
  • graph – If True, we return a matplotlib graph, otherwise we return pandas.DataFrame, p0vals
  • node_names – If None, we use Node 0, Node 1, … to label nodes, otherwise we use this.
  • show_diffs – If True we return changes from original (and remove original column), otherwise we return the new scores.
  • idealize – If True we idealize the scores, otherwise we normalize
  • p – The p value to use for the perspective calculation.
Returns:

If graph=True, we return the dataframe and create a matplotlib object and call plt.show(). Otherwise we return the pandas.DataFrame whose rows are the influence_nodes and columns are the rows. There is an additional column for original weights

pyanp.rowsens.influence_rank(mat, row, cluster_nodes=None, influence_nodes=None, limit_matrix_calc=<function calculus>, rank_change_nodes=None, error=1e-05, upper_lower_both=0, round_to_decimal=5, return_full_info=False)[source]

Calculates rank influence scores.

Parameters:
  • mat – The scaled supermatrix to perform the calculation on
  • row – The row to use for anp row sensitivity
  • cluster_nodes – If you wish to normalize by cluster, this should be the indices of the nodes that are in row’s cluster (including row itself).
  • influence_nodes – The indices of the nodes to calculate the influence of, with respect to row. If None it calculates the influence of all nodes other than row.
  • limit_matrix_calc – A function with one parameter, that calculates the limit matrix.
  • rank_change_nodes – The nodes to look for rank change at
  • error – While we narrow down our search for the p-value that causes a rank change, how close do we want the values between a change happening and not, to be.
  • upper_lower_both – Do we want to: Case 1: look for rank change only by changing p > 0.5=p0, if so upper_lower_both > 0. Case 2: look for rank change only by changing p < 0.5=p0, if so upper_lower_both < 0. Case 3: look for rank change by changing p>0.5 and p<0.5, if so upper_lower_both = 0.
  • round_to_decimal – How many decimals should we round the score to for ranking purposes
  • return_full_info – If True returns more info, see the return section for more details
Returns:

A list of one or more numbers, controlled by return_full_info and upper_lower_both:

  • If upper_lower_both < 0: our p-val search will only be for pval < 0.5
    • If return_full_info is True: We return pval_where_rank_chg_happens, score_of_that_pval
    • Otherwise: We return score_of_that_pval
  • If upper_lower_both > 0: our pval search will only be for pval > 0.5
    • If return_full_info is True: We return pval_where_rank_chg_happens, score_of_that_pval
    • Otherwise: We return score_of_that_pval
  • If upper_lower_both = 0: we check both lower and upper
    • If return_full_info is True: We return max_of_upper_lower_scores, lower_rank_value, lower_rank_chg_score, upper_rank_value, upper_rank_chg_score
    • Otherwise we return: max_of_upper_lower_scores

pyanp.rowsens.influence_table(mat, row, pvals=None, cluster_nodes=None, influence_nodes=None, p0mode=None, limit_matrix_calc=<function calculus>, graph=True, return_p0vals=False, node_names=None)[source]

Calculates the direct influence score, i.e. it calculates anp row sensitivity for each of pvals values and stores the new scores of the influence_nodes.

Parameters:
  • mat – The scaled supermatrix to perform the calculation on
  • row – The row to use for anp row sensitivity
  • pvals – The values to set p to, this should be a list (or list like) object of values before 0 and 1.
  • cluster_nodes – If you wish to normalize by cluster, this should be the indices of the nodes that are in row’s cluster (including row itself).
  • influence_nodes – The indices of the nodes to calculate the influence of, with respect to row. If None it calculates the influence of all nodes other than row.
  • p0mode – This controls the calculation and has 3 cases: Case 1: if it is a float, you are directly setting the p0 value to whatever p0mode is. Case 2: if it is an integer, this is the smart p0 mode, and it treats p0mode as the index of the alternative/node to make continuous. Case 3: otherwise we assume you want original weights to be the p0 value, and return the parameter orig
  • limit_matrix_calc – A function which takes a single input, the matrix to take the limit of.
  • graph – If True, we return a matplotlib graph, otherwise we return pandas.DataFrame, p0vals
  • return_p0vals – If true and not doing graphing, we return a tuple of the dataframe of the results, and the 2nd item as Series whose index is the names of the nodes, and whose values are the (x,y) position of the resting p0 value
  • node_names – If None, we use Node 0, Node 1, … to label nodes, otherwise we use this.
Returns:

If graph=True, we return nothing, but create a matplotlib object and call plt.show(). Otherwise if return_p0vals is True we return a pair of items. The first is the dataframe of results, whose indices are “Node 1”, “Node 2”, … which corresponds to influence_nodes (and the indices after “Node ” are the influence_node indices) and has 2 columns, ‘x’ is the pvals and ‘y’ is the resulting influence score (i.e. changed priority). The second element is a pd.Series whose indices is the same as the dataframe and whose values are pairs of items (x,y) where x is the p0 value for the given alternative and the y is the influence score of that alternative at that p-value.

If return_p0vals is False we return the first dataframe item only.

pyanp.rowsens.influence_table_plot(df, p0s)[source]

Graphs the return value of influence_table(graph=False), useful if you want to have both the graph done and also the table of values. :param df: The 1st returned component from influence_table(graph=False): a dataframe :param p0s: The 2nd returned component from influence_table(graph=False): a Series of (x,y)’s :return: Nothing, but does call plt.show() to make the matplotlib graph visible.

pyanp.rowsens.p0mode_is_direct(p0mode_value)[source]

Is the p0mode value a “directly set value”. See calcp0 for more info on p0mode values.

Parameters:p0mode_value – If it is an float, this p0mode value is the direct value to use for p0.
Returns:True | False
pyanp.rowsens.p0mode_is_smart(p0mode_value) → bool[source]

Is the p0mode value a “smart value”. See calcp0 for more info on p0mode values.

Parameters:p0mode_value – If it is an int, this p0mode value represents doing smart p0 making the node of that index (i.e.p0mode’s value) smooth
Returns:True | False
pyanp.rowsens.p0mode_name(p0mode_value) → str[source]

Tells what kind of p0 the p0mode value is

Parameters:p0mode_value – The p0mode value to get a name of
Returns:A string/human readable bit of information about the p0mode.
pyanp.rowsens.rank_change(vec1, vec2, places_to_rank, rank_change_places=None, round_to_decimal=5)[source]

A calculation that rounds 2 vectors to round_to_decimal places and then looks to see if the ranking of rounded vec1 is different from rounded vec2.

Parameters:
  • vec1 – A list or list-like object to check rank changing
  • vec2 – Another list or list-like object to check rank changing
  • places_to_rank – Indices to rank.
  • rank_change_places – Of the indices we are ranking, which ones are we checking for a change (if None we check all indices for rank change).
  • round_to_decimal – The number of decimal places to round to, before checking for rank changes
Returns:

True if a rank change happen and False otherwise

pyanp.rowsens.rescale(avec, idealize=False, inplace=True)[source]

Rescales a vector. :param avec: The vector like object to rescale

Parameters:
  • idealize – If True we idealize avec, else we normalize
  • inplace – If True, we rescale in place, otherwise we return the rescaled vector
Returns:

An np.array with the normalized or idealized vector if inplace is False, otherwise returns nothing and avec is changed in place

pyanp.rowsens.row_adjust(mat, row, p, cluster_nodes=None, inplace=False, p0mode=None)[source]

Performs an actual row adjust on the matrix, either inplace or returns an adjusted copy, leaving the original unchanged.

Parameters:
  • mat – The scaled supermatrix to perform anp row sensitivity on.
  • row – The row index to perform the anp row sensitivity on
  • p – The p value to adjust to
  • cluster_nodes – The other nodes in the parameter row’s cluster (including row itself), so we can scale by cluster. If None we do not scale by cluster.
  • inplace – Should we change the matrix mat, or should we create a new one, adjust it, and return it?
  • p0mode – See calcp0() function
Returns:

The adjusted matrix if inplace=False, and otherwise returns nothing and changes the matrix mat.

pyanp.rowsens.row_adjust_priority(mat, row, p, cluster_nodes=None, p0mode=None, limit_matrix_calc=<function calculus>, normalize_to_orig=True)[source]

Adjusts a row of matrix and recalculates the priorities of all the nodes.

Parameters:
  • mat – The scaled supermatrix to perform the calculation on
  • row – The row to use for anp row sensitivity
  • cluster_nodes – If you wish to normalize by cluster, this should be the indices of the nodes that are in row’s cluster (including row itself).
  • p0mode – This controls the calculation and has 3 cases: Case 1: if it is a float, you are directly setting the p0 value to whatever p0mode is. Case 2: if it is an integer, this is the smart p0 mode, and it treats p0mode as the index of the alternative/node to make continuous. Case 3: otherwise we assume you want original weights to be the p0 value, and return the parameter orig
  • limit_matrix_calc – A function which takes a single input, the matrix to take the limit of.
  • normalize_to_orig – If True we normalize the returning priority score so that the [row] index of it has the same value as the original and the other values are rescaled. Otherwise we simply normalize the priority vector directly.