{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<module 'propagateErrorLN' from '/home/studen/software/src/PBPK/pythonScripts/propagateErrorLN.py'>"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy\n",
    "import matplotlib.pyplot\n",
    "import os\n",
    "import scipy.interpolate\n",
    "import convolveLN\n",
    "import importlib\n",
    "importlib.reload(convolveLN)\n",
    "import propagateErrorLN\n",
    "importlib.reload(propagateErrorLN)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "qa=[ 0.003  -0.0035]\n",
      "[0] mean=0.003 std=0.0012 cv=0.40\n",
      "[1] mean=-0.0035 std=0.0014 cv=-0.40\n",
      "[x] mean=0.01 std=0.0018 cv=0.18\n",
      "qa=[ 0.003  -0.0035]\n",
      "A=0.003,0.0035\n",
      "cvPrime=[0.4 0.4]\n",
      "sigmaS=[0.38525317 0.38525317]\n",
      "muS=[-0.07421 -0.07421]\n",
      "Reading for sigma=0.38525317015992666\n",
      "Reading for sigma=0.38525317015992666\n",
      "B=0.0105\n",
      "mean/target= 1;1.1 fInt=1.00 hInt/fInt=1.00\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7f38b1936050>]"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 24444 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "muTarget=0.01\n",
    "cv=numpy.array([0.4,0.4])\n",
    "mu0=numpy.array([1.0,1.0])\n",
    "dydx=numpy.array([0.003,-0.0035])\n",
    "fmax=0.03\n",
    "nb=100\n",
    "fx=numpy.linspace(-fmax,fmax,2*nb+1)\n",
    "h=propagateErrorLN.generateDistribution(fx,muTarget,mu0,cv,dydx,1)\n",
    "y=propagateErrorLN.calculateDistribution(fx,muTarget,mu0,cv,dydx)\n",
    "h0=numpy.sum(fx*h)/numpy.sum(h)\n",
    "y0=numpy.sum(fx*y)/numpy.sum(y)\n",
    "\n",
    "q=numpy.array([h0,y0])\n",
    "if muTarget!=0:\n",
    "    q/=muTarget\n",
    "\n",
    "    \n",
    "print('mean/target={:2.2g};{:2.2g} fInt={:.2f} hInt/fInt={:.2f}'.format(q[0],q[1],\n",
    "                                                               numpy.sum(y),numpy.sum(h)/numpy.sum(y)))\n",
    "matplotlib.pyplot.bar(fx,h,fmax/nb)\n",
    "matplotlib.pyplot.plot(fx,y,color='orange')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.29356038 0.29356038]\n",
      "[2.43987137e-01+0.00000000e+00j 2.45110495e-01-2.44527012e-02j\n",
      " 2.43590371e-01-4.91260136e-02j ... 7.57700368e-27-5.41816969e-27j\n",
      " 7.50087710e-27-5.40592089e-27j 7.42537332e-27-5.39348347e-27j]\n",
      "[2.63090977e+00-0.00000000e+00j 2.60495558e+00+1.89292222e-01j\n",
      " 2.56550644e+00+3.74802156e-01j ... 2.68359363e-44+1.71020205e-44j\n",
      " 2.59169038e-44+1.72201602e-44j 2.50163352e-44+1.73184640e-44j]\n"
     ]
    }
   ],
   "source": [
    "n=2000\n",
    "qz,fa,u0,h=convolveLN.getZArray(n)\n",
    "cv=numpy.array([0.3,0.3])\n",
    "mu=numpy.array([1.5,1])\n",
    "sigma=numpy.sqrt(numpy.log(1+cv*cv))\n",
    "muS=numpy.log(mu/numpy.sqrt(1+cv*cv))\n",
    "print(sigma)\n",
    "#q1=convolveLN.getComplexConjugatedLTransformAtMinusComplexConjugatedZGrid(0.3,qz)\n",
    "q1=convolveLN.getLTransformGrid(sigma[0],qz,muS[0])\n",
    "q2=convolveLN.getComplexConjugatedLTransformAtMinusComplexConjugatedZGrid(0.1,qz,muS[1])\n",
    "q3=convolveLN.getLTransformGrid(sigma[1],qz,muS[1])\n",
    "fmax=5\n",
    "nb=100\n",
    "fx=numpy.linspace(0,fmax,nb+1)\n",
    "print(q1)\n",
    "print(q2)\n",
    "fy=convolveLN.inverseL(fx,qz,q1*q3,fa,u0,h,n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "61.91493950447091"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 22748 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "matplotlib.pyplot.plot(fx,fy)\n",
    "numpy.sum(fy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
