<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Bivariate
A bivariate analysis differs from a univariate, or distribution analysis, in that it is the analysis of two separate sets of data. These two sets of data are compared to one another to check for correlation, or a tendency of one of the sets of data to "predict" corresponding values in the other data set. If a linear or higher order model can be applied to describe, or model, the two sets of data, they are said to be correlated.
When two distributions are correlated, it is possible that the data in one of the distributions can be used to predict a corresponding value in the second distribution. This first distribution is referred to as the predictor and the second distribution as the response. Both predictor and response are graphed by a scatter plot, typically with the predictor on the x-axis and the response on the y-axis.
Note
Step1:  Scatter Plot
A scatter plot is used in sci-analysis to visualize the correlation between two sets of data. For this to work, each value in the first set of data has a corresponding value in the second set of data. The two values are tied together by the matching index value in each set of data. The length of each set of data have to be equal to one another, and the index values of each data set have to contiguous. If there is a missing value or values in one data set, the matching value at the same index in the other data set will be dropped.
By default, the best-fit line (assuming a linear relationship) is drawn as a dotted red line.
Step2:  Boxplot Borders
Boxplots can be displayed along-side the x and y axis of the scatter plot. This is a useful tool for visualizing the distribution of the sets of data on the x and y axis while still displaying the scatter plot.
Step3:  Contours
In certain cases, such as when one of the sets of data is discrete and the other is continuous, it might be difficult to determine where the data points are centered. In this case, density contours can be used to help visualize the join probability distribution between the two sets of data.
Step4:  Grouped Scatter Plot
If each set of data contains discrete and equivalent groups, the scatter plot can show each group in a separate color.
Step5:  Interpreting the Statistics
Linear Regression
The Linear Regression finds the least-squares best-fit line between the predictor and response. The linear relationship between the predictor and response is described by the relationship y = mx + b, where x is the predictor, y is the response, m is the slope, and b is the y-intercept.
n - The number of data points in the analysis.
Slope - The slope of the best-fit line between predictor and response.
Intercept - The y-intercept of the best-fit line between predictor and response.
r - The correlation coefficient of the linear regression.
r^2 - The amount of error that can be described by the linear regression. The higher the number, the more accurate the linear regression models the relationship between the predictor and response.
Std Err - Standard error of the best-fit line.
p value - The p value of the hypothesis test that the slope of the best-fit line is actually zero.
Correlation Coefficient
If the data points from both sets of data are normally distributed, the Pearson correlation coefficient is calculated, otherwise, the Spearman Rank correlation coefficient is calculated. A correlation coefficient of 0 indicates no relationship, whereas 1 indicates a perfect correlation between predictor and response. In the case of both correlation coefficients, the null hypothesis is that the correlation coefficient is 0, signifying no relationship between the predictor and response. If the p value is less than the significance $\alpha$, the predictor and response are correlated.
Usage
Argument Examples
x-sequence, y-sequence
The bare minimum requirements for performing a Bivariate analysis. The length of x-sequence and y-sequence should be equal and will raise an UnequalVectorLengthError if not.
Step6:  fit
Controls whether the best fit line is displayed or not.
Step7:  points
Controls whether the data points of the scatter plot are displayed or not.
Step8:  boxplot_borders
Controls whether boxplots are displayed for x-sequence and y-sequence.
Step9:  contours
Controls whether the density contours are displayed or not. The contours can be useful when analyzing joint probability distributions.
Step10:  labels, highlight
Used in conjunction with one another, labels and highlight are used for displaying data values for the data points on the scatter plot.
Step11:  groups
The groups argument can be used to perform a Bivariate analysis on separate collections of data points that can be compared to one another.
Step12:  groups, highlight
Using the groups argument is a great way to compare treatments. When combined with the highlight argument, a particular group can be highlighted on the scatter plot to stand out from the others.
Step13:  Multiple groups can also be highlighted.
Step14:  title
The title of the distribution to display above the graph.
Step15:  xname
The name of the data on the x-axis.
Step16:  yname
The name of the data on the y-axis.
<ASSISTANT_TASK:>
Python Code:
import numpy as np
import scipy.stats as st
from sci_analysis import analyze
%matplotlib inline
# Create x-sequence and y-sequence from random variables.
np.random.seed(987654321)
x_sequence = st.norm.rvs(2, size=2000)
y_sequence = np.array([x + st.norm.rvs(0, 0.5, size=1) for x in x_sequence])
Explanation: Bivariate
A bivariate analysis differs from a univariate, or distribution analysis, in that it is the analysis of two separate sets of data. These two sets of data are compared to one another to check for correlation, or a tendency of one of the sets of data to "predict" corresponding values in the other data set. If a linear or higher order model can be applied to describe, or model, the two sets of data, they are said to be correlated.
When two distributions are correlated, it is possible that the data in one of the distributions can be used to predict a corresponding value in the second distribution. This first distribution is referred to as the predictor and the second distribution as the response. Both predictor and response are graphed by a scatter plot, typically with the predictor on the x-axis and the response on the y-axis.
Note: Just because two sets of data correlate with one another does not necessarily mean that one predicts the other. It merely means it's a possibility that one predicts the other. This is summarized by the saying "Correlation does not imply causation." Use caution when drawing conclusions of a bivariate analysis. It is a good idea to study both data sets more carefully to determine if the two data sets are in fact correlated.
Interpreting the Graphs
Let's first import sci-analysis and setup some variables to use in these examples.
End of explanation
analyze(x_sequence, y_sequence)
Explanation: Scatter Plot
A scatter plot is used in sci-analysis to visualize the correlation between two sets of data. For this to work, each value in the first set of data has a corresponding value in the second set of data. The two values are tied together by the matching index value in each set of data. The length of each set of data have to be equal to one another, and the index values of each data set have to contiguous. If there is a missing value or values in one data set, the matching value at the same index in the other data set will be dropped.
By default, the best-fit line (assuming a linear relationship) is drawn as a dotted red line.
End of explanation
analyze(x_sequence, y_sequence, boxplot_borders=True)
Explanation: Boxplot Borders
Boxplots can be displayed along-side the x and y axis of the scatter plot. This is a useful tool for visualizing the distribution of the sets of data on the x and y axis while still displaying the scatter plot.
End of explanation
x_continuous = st.weibull_max.rvs(2.7, size=2000)
y_discrete = st.geom.rvs(0.5, loc=0, size=2000)
analyze(x_continuous, y_discrete, contours=True, fit=False)
Explanation: Contours
In certain cases, such as when one of the sets of data is discrete and the other is continuous, it might be difficult to determine where the data points are centered. In this case, density contours can be used to help visualize the join probability distribution between the two sets of data.
End of explanation
# Create new x-grouped and y-grouped from independent groups A, B, and C.
a_x = st.norm.rvs(2, size=500)
a_y = np.array([x + st.norm.rvs(0, 0.5, size=1) for x in a_x])
b_x = st.norm.rvs(4, size=500)
b_y = np.array([1.5 * x + st.norm.rvs(0, 0.65, size=1) for x in b_x])
c_x = st.norm.rvs(1.5, size=500)
c_y = np.array([3 * x + st.norm.rvs(0, 0.95, size=1) - 1 for x in c_x])
x_grouped = np.concatenate((a_x, b_x, c_x))
y_grouped = np.concatenate((a_y, b_y, c_y))
grps = np.concatenate((['Group A'] * 500, ['Group B'] * 500, ['Group C'] * 500))
analyze(
    x_grouped, 
    y_grouped, 
    groups=grps,
    boxplot_borders=False,
)
Explanation: Grouped Scatter Plot
If each set of data contains discrete and equivalent groups, the scatter plot can show each group in a separate color.
End of explanation
analyze(
    x_sequence, 
    y_sequence,
)
Explanation: Interpreting the Statistics
Linear Regression
The Linear Regression finds the least-squares best-fit line between the predictor and response. The linear relationship between the predictor and response is described by the relationship y = mx + b, where x is the predictor, y is the response, m is the slope, and b is the y-intercept.
n - The number of data points in the analysis.
Slope - The slope of the best-fit line between predictor and response.
Intercept - The y-intercept of the best-fit line between predictor and response.
r - The correlation coefficient of the linear regression.
r^2 - The amount of error that can be described by the linear regression. The higher the number, the more accurate the linear regression models the relationship between the predictor and response.
Std Err - Standard error of the best-fit line.
p value - The p value of the hypothesis test that the slope of the best-fit line is actually zero.
Correlation Coefficient
If the data points from both sets of data are normally distributed, the Pearson correlation coefficient is calculated, otherwise, the Spearman Rank correlation coefficient is calculated. A correlation coefficient of 0 indicates no relationship, whereas 1 indicates a perfect correlation between predictor and response. In the case of both correlation coefficients, the null hypothesis is that the correlation coefficient is 0, signifying no relationship between the predictor and response. If the p value is less than the significance $\alpha$, the predictor and response are correlated.
Usage
Argument Examples
x-sequence, y-sequence
The bare minimum requirements for performing a Bivariate analysis. The length of x-sequence and y-sequence should be equal and will raise an UnequalVectorLengthError if not.
End of explanation
analyze(
    x_sequence, 
    y_sequence,
    fit=False,
)
Explanation: fit
Controls whether the best fit line is displayed or not.
End of explanation
analyze(
    x_sequence, 
    y_sequence,
    points=False,
)
Explanation: points
Controls whether the data points of the scatter plot are displayed or not.
End of explanation
analyze(
    x_sequence, 
    y_sequence,
    boxplot_borders=True,
)
Explanation: boxplot_borders
Controls whether boxplots are displayed for x-sequence and y-sequence.
End of explanation
analyze(
    x_sequence, 
    y_sequence,
    contours=True,
)
Explanation: contours
Controls whether the density contours are displayed or not. The contours can be useful when analyzing joint probability distributions.
End of explanation
labels = np.random.randint(low=10000, high=99999, size=2000)
analyze(
    x_sequence, 
    y_sequence,
    labels=labels,
    highlight=[66286]
)
Explanation: labels, highlight
Used in conjunction with one another, labels and highlight are used for displaying data values for the data points on the scatter plot.
End of explanation
# Create new x-grouped and y-grouped from independent groups A, B, and C.
a_x = st.norm.rvs(2, size=500)
a_y = np.array([x + st.norm.rvs(0, 0.5, size=1) for x in a_x])
b_x = st.norm.rvs(4, size=500)
b_y = np.array([1.5 * x + st.norm.rvs(0, 0.65, size=1) for x in b_x])
c_x = st.norm.rvs(1.5, size=500)
c_y = np.array([3 * x + st.norm.rvs(0, 0.95, size=1) - 1 for x in c_x])
x_grouped = np.concatenate((a_x, b_x, c_x))
y_grouped = np.concatenate((a_y, b_y, c_y))
grps = np.concatenate((['Group A'] * 500, ['Group B'] * 500, ['Group C'] * 500))
analyze(
    x_grouped, 
    y_grouped, 
    groups=grps,
)
Explanation: groups
The groups argument can be used to perform a Bivariate analysis on separate collections of data points that can be compared to one another.
End of explanation
analyze(
    x_grouped, 
    y_grouped, 
    groups=grps,
    highlight=['Group A'],
)
Explanation: groups, highlight
Using the groups argument is a great way to compare treatments. When combined with the highlight argument, a particular group can be highlighted on the scatter plot to stand out from the others.
End of explanation
analyze(
    x_grouped, 
    y_grouped, 
    groups=grps,
    highlight=['Group A', 'Group B'],
)
Explanation: Multiple groups can also be highlighted.
End of explanation
x_sequence = st.norm.rvs(2, size=2000)
y_sequence = np.array([x + st.norm.rvs(0, 0.5, size=1) for x in x_sequence])
analyze(
    x_sequence, 
    y_sequence,
    title='This is a Title',
)
Explanation: title
The title of the distribution to display above the graph.
End of explanation
analyze(
    x_sequence, 
    y_sequence,
    xname='This is the x-axis data'
)
Explanation: xname
The name of the data on the x-axis.
End of explanation
analyze(
    x_sequence, 
    y_sequence,
    yname='This is the y-axis data'
)
Explanation: yname
The name of the data on the y-axis.
End of explanation
<END_TASK>