Project: s41467-025-59099-1 | Function: gaussianMeanAndVariance (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Computes weighted mean and variance for replicate biological data using inverse variance weighting. Implements statistical method for combining multiple experimental replicates with proper error propagation in genetic screen analysis.

**KEY TERMS:** {
  "weight": "inverse variance weights",
  "nume_df": "weighted numerator calculation",
  "var1": "statistical variance component",
  "term2": "variance correction factor"
}

**CONTEXT CODE:**     # time= ['d0', 'd7', 'd14']
    # mf=['mf1','mf2']
    # backgrounds=['Cl15cy1STM','GCKO#2GOMOSTM','145480GOMOSTM']


    ratioDict={}
    for b in backgrounds:
        for t in time:
            for f in mf:
                string=b+'_'+t+'_'+f
                time_cols = getColumnsFormDF(df_m, [string])

                ratioDict[(b,t,f)]=time_cols[0]

    time = time2
    rel_fit=pd.DataFrame(index=df_m.index)
    # rel_fit1 = pd.DataFrame(index=df_m.index)
    rel_err = pd.DataFrame(index=df_m.index)
    # rel_err1 = pd.DataFrame(index=df_m.index) #without log scale
    for b in backgrounds:
        for t in time:
            for f in mf:
                tcol = ratioDict[(b, 'd0', f)]
                input=pd.DataFrame(index=df_m.index)
                input_err = pd.DataFrame(index=df_sd.index)
                input = pd.concat([input, df_m[tcol].copy()], axis=1)
                input_err = pd.concat([input_err, df_sd[tcol].copy()], axis=1)  # error

                tcol=ratioDict[(b, t, f)]
                tech_df = pd.DataFrame(index=df_m.index)
                tech_df_err = pd.DataFrame(index=df_sd.index)
                tech_df = pd.concat([tech_df, df_m[tcol].copy()], axis=1)
                tech_df_err = pd.concat([tech_df_err, df_sd[tcol].copy()], axis=1)  # error


                rel_fit = pd.concat([rel_fit, tech_df.iloc[:, 0:].sub(input.iloc[:, 0], axis=0)], axis=1)  # error

                # SD on log2 scale
                # tech_df_err=np.log2(tech_df_err)
                # input_err=np.log2(input_err)

                tmp=tech_df_err.pow(2, axis=1).iloc[:,0:].add(input_err.pow(2,axis=1).iloc[:,0],axis=0)
                # rel_err=pd.concat([rel_err,tmp.apply(np.sqrt)],axis=1) SD
                rel_err=pd.concat([rel_err,tmp],axis=1) # varaiance




    return rel_fit, rel_err

def gaussianMeanAndVariance(rel_fit,rel_err):
    """
    Computes weighted mean and variance for replicate biological data using inverse variance weighting.

    This function implements the statistical method for combining multiple experimental replicates
    with proper error propagation in genetic screen analysis. It uses inverse variance weighting
    to compute the optimal combination of biological replicates and includes variance correction
    factors for accurate statistical inference in CRISPR homing screen data.

    Args:
        rel_fit: Relative fitness dataframe with samples as columns
        rel_err: Relative error/variance dataframe with samples as columns

    Returns:
        list: [weighted_mean, weighted_sd, weighted_variance]
            - weighted_mean: Inverse variance weighted mean across replicates
            - weighted_sd: Standard deviation of weighted estimates
            - weighted_variance: Variance of weighted estimates with correction

    Paper reference: "For each of the two biological replicates, infected midguts of 80–100 mosquitoes per pool were dissected 12 days later. Barcodes counted in gDNA from the oocysts (barcode output) were compared to input 2"

    Formula: weighted_mean = Σ(xi/σi²) / Σ(1/σi²), weighted_variance = 1/Σ(1/σi²) + variance_correction
    Implementation: Inverse variance weighting, variance propagation, and statistical correction factors
    """
    # MASKED: Statistical weighted mean and variance calculation for biological replicates
    raise NotImplementedError("This function is masked for testing purposes. The function computes weighted mean and variance for replicate biological data using inverse variance weighting, implementing the statistical method for combining multiple experimental replicates with proper error propagation in genetic screen analysis.")


def plotfitScatter(x,y,xlab,ylab,pdf,title):
    fig= plt.figure(figsize=(10,8))
    ax = plt.gca()
    xx=[]
    yy=[]
    pp=[]
    xpxp=[]


    for i in range(len(x)):
        xx.append(np.array(x[i]))
        yy.append(np.array(y[i]))
        z=np.polyfit(x[i], y[i], 1)
        p=np.poly1d(z)
        mini=np.min(x[i])
        maxi=np.max(x[i])
        pp.append(p)
        xp = np.linspace(mini, maxi, 100)
#         print(xp,pp)
        xpxp.append(xp)

        # # exponnetial fit
        # popt, pcov = curve_fit(exponenial_func, xx[i], yy[i], p0=(1, 1e-3, 1))
        # print(popt)
        # y3.append(exponenial_func(xp, *popt))
    lines=ax.plot(xx[0], yy[0], 'r.', xpxp[0], pp[0](xpxp[0]), 'r-',xx[1], yy[1], 'b.', xpxp[1], pp[1](xpxp[1]), 'b-',linewidth=3)
    # lines=ax.plot(xx[0], yy[0], 'r.', xpxp[0], y3[0], 'r-',xx[1], yy[1], 'b.', xpxp[1], y3[1], 'b-',xx[2], yy[2], 'g.', xpxp[2], y3[2], 'g-')
#     lines=ax.plot( xpxp[0], pp[0](xpxp[0]), 'r-', xpxp[1], pp[1](xpxp[1]), 'b-', xpxp[2], pp[2](xpxp[2]), 'g-')
    plt.xlabel(xlab,fontsize=15)
    plt.ylabel(ylab,fontsize=15)
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
    plt.legend(lines,['d0','d0','d13','d13'],fontsize=10);
    plt.title(title)
#     plt.legend(lines,['d0','d7','d14'],fontsize=15);
#     plt.xlim([-14,1])
    plt.savefig(pdf)

    plt.show()


def calRel_withoutLog(df):

    n=df.shape[1]
    df=df.div(df.sum(axis=1), axis=0)
    # get log value
    # cut=1e-10
    # df[df<cut]=cut;
    return df


def plotfitScatterDay(x,y,xlab,ylab,pdf,title):
    fig= plt.figure(figsize=(10,8))
    ax = plt.gca()
    xx=[]
    yy=[]
    pp=[]
    xpxp=[]
    y3=[]
    for i in range(len(x)):
        xx.append(np.array(x[i]))
        yy.append(np.array(y[i]))
        z=np.polyfit(x[i], y[i], 1)
        p=np.poly1d(z)
        mini=np.min(x[i])
        maxi=np.max(x[i])
        pp.append(p)
        xp = np.linspace(mini, maxi, 100)
#         print(xp,pp)
        xpxp.append(xp)

        # # exponnetial fit


Complete the function implementation replacing "raise NotImplementedError".