a
    |BeN                     @   s   d dl Zd dlmZ d dlmZ d dlmZ d dl	m
Z
 dd Zdd	d
Zdd Zdd Zdd ZdddZdd ZdddZdS )    N)r2_score)	curve_fit)tc                 C   s(   t ddd}| |t |  }||fS )N-C6?d      )nplinspacelog)abx	y_perfect r   +/var/www/html/logarithmic_data_generator.py generate_target_logarithmic_line   s    r   normal   c                 C   s   t |}t||d  }tt jj||dd}	t|D ]}
|dkrTt jd|}n:|dkrnt j| |}n |dkrt j	d|}nt
d|
|	v r||9 }|d u st||kr<|||
< q8q<q8| | }|S )	Nr   F)replacer   r   uniformlaplacez=Invalid noise type. Choose 'normal', 'uniform', or 'laplace'.)r   zerosintsetrandomchoiceranger   r   r   
ValueErrorabs)r   std_devn
noise_typeoutlier_percentageoutlier_multiplierrejection_thresholdnoisenum_outliersoutlier_indicesinoise_sampley_noisyr   r   r   generate_noisy_data   s$    
r+   c                 C   s   ||t |   S N)r   r
   )r   r   r   r   r   r   logarithmic_function,   s    r-   c                 C   s0   t | | }| d d | }|d d | }||fS r,   )len)r   r*   r    stepx_subsety_subsetr   r   r   subset_noisy_data/   s    r2   c                 C   s,   | \}}}t dd|}t|||}||fS )Nr   r   )r   r	   r-   )final_coeffsr    r   r   _x_refy_refr   r   r   #generate_logarithmic_reference_line5   s    
r7   ffffff?c                 C   s   |\}}t t |\}}td| d t| d }|t |d ||  | d   }	t| g|R  }
|
|	 }|
|	 }||fS )Nr      )r   sqrtdiagr   ppfr.   r-   )r   poptpcovconfidence_levela_fittedb_fittedse_ase_bt_valueciy_predci_lowerci_upperr   r   r   "calculate_log_confidence_intervals;   s    "rI   c                 C   s<   t t| |dd\}}|\}}t| ||}t||}|||fS )N)r   g?)p0)r   r-   r   )r   r*   r=   r>   r@   rA   rF   	r_squaredr   r   r   fit_noisy_logarithmic_dataI   s
    
rL   2   c	                 C   s   t | |\}	}
t|
|d||||}t|	||\}}t||\}}}t||||\}}|	 |
 | | | t|g|R   | | ||d |d dd
}t| ||fS )Nr   r   r   )r   r   )
	x_perfectr   x_noisyr*   x_referencey_referencerG   rH   rK   Zfitted_coefficients)r   r+   r2   rL   rI   tolistr-   print)r   r   r   r    r!   r"   r#   	thresholdr?   rN   r   r*   x_noisy_subsety_noisy_subsetr=   r>   rK   rG   rH   datar   r   r   !logarithmic_fit_and_store_resultsP   s$    rX   )r   r   r   N)r8   )r   rM   Nr8   )numpyr   matplotlib.pyplotpyplotpltsklearn.metricsr   scipy.optimizer   scipy.statsr   r   r+   r-   r2   r7   rI   rL   rX   r   r   r   r   <module>   s   

